generated from embedded-dev-research/cpp-project-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Implement Layer abstraction for all layers #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chekalexey
wants to merge
24
commits into
embedded-dev-research:main
Choose a base branch
from
chekalexey:All_layers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
5f1561b
Add layer examples
chekalexey 58a016c
Add layer examples
chekalexey a19ca04
fixed
chekalexey 6f75b73
correction of inclusions
chekalexey 968c92a
Update cmakelist
chekalexey 7c87d3d
Add a ElementwiseLayer Example
chekalexey 9bfa382
Add a pooling layer example
chekalexey f02551d
add a MatmulLayer example
chekalexey f81c4bc
Delete ElementwiseLayer.cpp
chekalexey 87764d6
Delete ConvolutionLayer.cpp
chekalexey 2561b5e
Delete PoolingLayer.cpp
chekalexey daaa472
Update CMakeLists
chekalexey aa8d4f1
Update CMakeLists
chekalexey 2e2fe44
Update Cmakelist
chekalexey e874928
correction
chekalexey 0cd5425
Update CMakeLists
chekalexey d7cf926
Add dependencies in CMakeLists
chekalexey 51b0a08
Add a Reshape Layer example
chekalexey 3bab99a
Add a Slice Layer example
chekalexey fa78bd3
Added a split layer example
chekalexey 07de634
Added a Concat Layer example
chekalexey 349c865
Implement Layer abstraction for all layers
chekalexey d0a66bd
Update matmul and pool layers
chekalexey bcf4536
Update other layers
chekalexey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
add_subdirectory(example) | ||
add_subdirectory(layer_example) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
add_executable(example main.cpp) | ||
add_executable(example main.cpp) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
set(ARM_DIR "${CMAKE_SOURCE_DIR}/3rdparty/ComputeLibrary") | ||
|
||
add_executable(Concat ConcatLayer.cpp) | ||
|
||
include_directories(${ARM_DIR}) | ||
include_directories(${ARM_DIR}/include) | ||
target_link_directories(Concat PUBLIC ${ARM_DIR}/build) | ||
|
||
target_link_libraries(Concat arm_compute) | ||
|
||
add_dependencies(Concat build_compute_library) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <iostream> | ||
#include "arm_compute/runtime/NEON/NEFunctions.h" | ||
#include "utils/Utils.h" | ||
|
||
using namespace arm_compute; | ||
using namespace utils; | ||
|
||
int main() { | ||
Tensor input1, input2; | ||
Tensor output; | ||
std::vector<const ITensor *> input; | ||
|
||
const int input_width = 3; | ||
const int input_height = 3; | ||
const int axis = 2; | ||
|
||
input1.allocator()->init(TensorInfo(TensorShape(input_width, input_height, 1), 1, DataType::F32)); | ||
input2.allocator()->init(TensorInfo(TensorShape(input_width, input_height, 1), 1, DataType::F32)); | ||
|
||
input1.allocator()->allocate(); | ||
input2.allocator()->allocate(); | ||
|
||
fill_random_tensor(input1, 0.f, 1.f); | ||
fill_random_tensor(input2, 0.f, 1.f); | ||
|
||
input.push_back(&input1); | ||
input.push_back(&input2); | ||
|
||
NEConcatenateLayer concat; | ||
concat.configure(input, &output, axis); | ||
output.allocator()->allocate(); | ||
|
||
concat.run(); | ||
|
||
output.print(std::cout); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#ifndef LAYER_H | ||
#define LAYER_H | ||
|
||
#include <list> | ||
|
||
#include "arm_compute/runtime/NEON/NEFunctions.h" | ||
#include "utils/Utils.h" | ||
|
||
using namespace arm_compute; | ||
using namespace utils; | ||
|
||
struct LayerAttributes { | ||
int id = -1; | ||
}; | ||
|
||
class Layer { | ||
protected: | ||
int id_; | ||
|
||
public: | ||
Layer() = default; | ||
explicit Layer(const LayerAttributes& attrs) : id_(attrs.id) {} | ||
virtual ~Layer() = default; | ||
void setID(int id) { id_ = id; } | ||
int getID() const { return id_; } | ||
virtual std::string getInfoString() const; | ||
virtual void exec(Tensor& input, Tensor& output) = 0; | ||
virtual void exec(Tensor& input1, Tensor& input2, Tensor& output) = 0; | ||
virtual void exec() = 0; | ||
//virtual Shape get_output_shape() = 0; | ||
|
||
virtual std::string get_type_name() const = 0; | ||
void addNeighbor(Layer* neighbor); | ||
void removeNeighbor(Layer* neighbor); | ||
std::list<Layer*> neighbors_; | ||
}; | ||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#ifndef ACL_CONCATENATE_LAYER_H | ||
#define ACL_CONCATENATE_LAYER_H | ||
|
||
#include <numeric> | ||
#include <stdexcept> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "include/layer/layer.h" | ||
|
||
class ConcatenateLayer : public Layer { | ||
private: | ||
NEConcatenateLayer concat; | ||
bool configured_ = false; | ||
|
||
public: | ||
ConcatenateLayer(int id) { setID(id); } | ||
|
||
void configure(const std::vector<TensorShape>& inputs_shapes, unsigned int axis, TensorShape& output_shape, | ||
std::vector<Tensor*>& input, Tensor& output) { | ||
|
||
if (inputs_shapes.empty()) { | ||
throw std::runtime_error("Concat: Input shapes list cannot be empty."); | ||
} | ||
if (inputs_shapes.size() != input.size()) { | ||
throw std::runtime_error("Concat: vector size mismatch."); | ||
} | ||
std::vector<const ITensor*> inpcopy; | ||
for (int i = 0; i < input.size(); i++) { | ||
input[i]->allocator()->init(TensorInfo(inputs_shapes[i], 1, DataType::F32)); | ||
input[i]->allocator()->allocate(); | ||
inpcopy.push_back(input[i]); | ||
} | ||
output.allocator()->init(TensorInfo(output_shape, 1, DataType::F32)); | ||
concat.configure(inpcopy, &output, axis); | ||
output.allocator()->allocate(); | ||
configured_ = true; | ||
} | ||
|
||
void exec() override { | ||
if (!configured_) { | ||
throw std::runtime_error("ConcatenateLayer: Layer not configured."); | ||
} | ||
concat.run(); | ||
} | ||
|
||
std::string get_type_name() const override { | ||
return "ConcatenateLayer"; | ||
} | ||
}; | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#ifndef ACL_CONVOLUTION_LAYER_SIMPLIFIED_H | ||
#define ACL_CONVOLUTION_LAYER_SIMPLIFIED_H | ||
|
||
#include <numeric> | ||
#include <stdexcept> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "include/layer/layer.h" | ||
|
||
class ConvolutionLayer : public Layer { | ||
private: | ||
NEConvolutionLayer conv; | ||
bool configured_ = false; | ||
|
||
public: | ||
ConvolutionLayer(int id) { setID(id); } | ||
|
||
void configure( | ||
const TensorShape& input_shape, | ||
const TensorShape& weights_shape, | ||
const TensorShape& biases_shape, | ||
TensorShape& output_shape, | ||
const PadStrideInfo& info, | ||
Tensor& input, | ||
Tensor& weights, | ||
Tensor& biases, | ||
Tensor& output | ||
) { | ||
|
||
input.allocator()->init(TensorInfo(input_shape, 1, DataType::F32)); | ||
weights.allocator()->init(TensorInfo(weights_shape, 1, DataType::F32)); | ||
biases.allocator()->init(TensorInfo(biases_shape, 1, DataType::F32)); | ||
output.allocator()->init(TensorInfo(output_shape, 1, DataType::F32)); | ||
|
||
input.allocator()->allocate(); | ||
weights.allocator()->allocate(); | ||
biases.allocator()->allocate(); | ||
output.allocator()->allocate(); | ||
|
||
conv.configure(&input, &weights, &biases, &output, info); | ||
configured_ = true; | ||
} | ||
|
||
void exec() override { | ||
if (!configured_) { | ||
throw std::runtime_error("ConvolutionLayer: Layer not configured."); | ||
} | ||
conv.run(); | ||
} | ||
|
||
std::string get_type_name() const override { | ||
return "ConvolutionLayer"; | ||
} | ||
}; | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
#ifndef ACL_ELEMENTWISE_LAYER_H | ||
#define ACL_ELEMENTWISE_LAYER_H | ||
|
||
#include <numeric> | ||
#include <stdexcept> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "include/layer/layer.h" | ||
|
||
using namespace arm_compute; | ||
using namespace utils; | ||
|
||
enum class ElementwiseOp { | ||
ADD, | ||
DIV, | ||
ABS, | ||
SIGM, | ||
SWISH, | ||
SQUARED_DIFF | ||
}; | ||
|
||
class ElementwiseLayer : public Layer { | ||
private: | ||
ElementwiseOp op_type; | ||
NEActivationLayer act; | ||
NEArithmeticAddition add; | ||
NEElementwiseDivision div; | ||
NEElementwiseSquaredDiff sqdiff; | ||
bool configured_ = false; | ||
|
||
public: | ||
ElementwiseLayer(int id, ElementwiseOp op) : op_type(op) { setID(id); } | ||
|
||
ElementwiseLayer() : ElementwiseLayer(0, ElementwiseOp::ADD) { } | ||
|
||
void configure(const TensorShape& input_shape, TensorShape& output_shape, Tensor& input, Tensor& output) { | ||
input.allocator()->init(TensorInfo(input_shape, 1, DataType::F32)); | ||
output.allocator()->init(TensorInfo(output_shape, 1, DataType::F32)); | ||
|
||
input.allocator()->allocate(); | ||
output.allocator()->allocate(); | ||
|
||
switch (op_type) { | ||
case ElementwiseOp::ABS: { | ||
act.configure(&input, &output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS)); | ||
act.run(); | ||
break; | ||
} | ||
case ElementwiseOp::SIGM: { | ||
act.configure(&input, &output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); | ||
act.run(); | ||
break; | ||
} | ||
case ElementwiseOp::SWISH: { | ||
act.configure(&input, &output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::SWISH)); | ||
act.run(); | ||
break; | ||
} | ||
default: | ||
throw std::runtime_error("ElementwiseLayer: This operation requires two inputs"); | ||
} | ||
configured_ = true; | ||
} | ||
|
||
void configure(const TensorShape& input1_shape, const TensorShape& input2_shape, TensorShape& output_shape, | ||
Tensor& input1, Tensor& input2, Tensor& output) { | ||
if (input1_shape.total_size() != input2_shape.total_size()) { | ||
throw std::runtime_error( | ||
"ElementwiseLayer: Input shapes must have same total size"); | ||
} | ||
input1.allocator()->init(TensorInfo(input1_shape, 1, DataType::F32)); | ||
input2.allocator()->init(TensorInfo(input2_shape, 1, DataType::F32)); | ||
output.allocator()->init(TensorInfo(output_shape, 1, DataType::F32)); | ||
|
||
input1.allocator()->allocate(); | ||
input2.allocator()->allocate(); | ||
output.allocator()->allocate(); | ||
|
||
switch (op_type) { | ||
case ElementwiseOp::ADD: { | ||
add.configure(&input1, &input2, &output, ConvertPolicy::WRAP); | ||
add.run(); | ||
break; | ||
} | ||
case ElementwiseOp::DIV: { | ||
div.configure(&input1, &input2, &output); | ||
div.run(); | ||
break; | ||
} | ||
case ElementwiseOp::SQUARED_DIFF: { | ||
sqdiff.configure(&input1, &input2, &output); | ||
sqdiff.run(); | ||
break; | ||
} | ||
default: | ||
throw std::runtime_error("ElementwiseLayer: This operation requires single input"); | ||
} | ||
configured_ = true; | ||
} | ||
|
||
void exec() override { | ||
if (!configured_) { | ||
throw std::runtime_error("ElementwiseLayer: Layer not configured before exec."); | ||
} | ||
switch (op_type) { | ||
case ElementwiseOp::ABS: | ||
case ElementwiseOp::SIGM: | ||
case ElementwiseOp::SWISH: | ||
act.run(); | ||
break; | ||
case ElementwiseOp::ADD: { | ||
add.run(); | ||
break; | ||
} | ||
case ElementwiseOp::DIV: { | ||
div.run(); | ||
break; | ||
} | ||
case ElementwiseOp::SQUARED_DIFF: { | ||
sqdiff.run(); | ||
break; | ||
} | ||
default: | ||
throw std::runtime_error("ElementwiseLayer: This operation requires single input"); | ||
} | ||
} | ||
|
||
std::string get_type_name() const override { | ||
switch (op_type) { | ||
case ElementwiseOp::ADD: return "ElementwiseAddLayer"; | ||
case ElementwiseOp::DIV: return "ElementwiseDivLayer"; | ||
case ElementwiseOp::ABS: return "ElementwiseAbsLayer"; | ||
case ElementwiseOp::SIGM: return "ElementwiseSigmoidLayer"; | ||
case ElementwiseOp::SWISH: return "ElementwiseSwishLayer"; | ||
case ElementwiseOp::SQUARED_DIFF: return "ElementwiseSquaredDiffLayer"; | ||
default:return "ElementwiseUnknownLayer"; | ||
} | ||
} | ||
}; | ||
|
||
#endif |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setID call can be moved to the parent class