-
Notifications
You must be signed in to change notification settings - Fork 2
Mul, Sub and Add Layer with broadcasting support #186
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7929440
init layer
Semyon1104 39a3e1d
add tests
Semyon1104 80404b6
add resnet test
Semyon1104 a158b0a
fix static
Semyon1104 eb19788
add&sub + Impl
Semyon1104 8588f3f
override impl run
Semyon1104 df85f60
fix return and copy
Semyon1104 eade653
fix tidy
Semyon1104 e5b0715
change namespace
Semyon1104 7dcc170
fix
Semyon1104 0f04838
del impl_class, fix calculate strides, +div, add run_broadcast_impl, …
Semyon1104 d661cb2
fix tidy
Semyon1104 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
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
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
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
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
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
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
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,72 @@ | ||
| #pragma once | ||
| #include <algorithm> | ||
| #include <memory> | ||
| #include <stdexcept> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include "Tensor.hpp" | ||
| #include "layers/Layer.hpp" | ||
|
|
||
| namespace it_lab_ai { | ||
|
|
||
| class BinaryOpLayer : public Layer { | ||
| public: | ||
| enum class Operation : uint8_t { kMul, kAdd, kSub }; | ||
|
|
||
| BinaryOpLayer() = default; | ||
| explicit BinaryOpLayer(Operation op) : op_(op) {} | ||
|
|
||
| static std::string get_name() { return "Binary Operation Layer"; } | ||
| void run(const Tensor& input, Tensor& output) override; | ||
| void run(const Tensor& A, const Tensor& B, Tensor& output); | ||
| static bool is_scalar_tensor(const Tensor& t); | ||
|
|
||
| #ifdef ENABLE_STATISTIC_WEIGHTS | ||
| Tensor get_weights() override { | ||
| std::vector<int> v = {0}; | ||
| return make_tensor(v); | ||
| } | ||
| #endif | ||
|
|
||
| private: | ||
| Operation op_ = Operation::kMul; | ||
| std::shared_ptr<void> impl_; | ||
aobolensk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| template <typename ValueType> | ||
| void run_with_scalar_impl(const Tensor& input, ValueType scalar, | ||
| Tensor& output) const; | ||
| void run_with_scalar(const Tensor& input, float scalar, Tensor& output); | ||
aobolensk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| static bool can_broadcast(const Shape& shape_A, const Shape& shape_B); | ||
| static Shape calculate_broadcasted_shape(const Shape& shape_A, | ||
| const Shape& shape_B); | ||
| static std::vector<size_t> get_strides(const Shape& shape); | ||
| static size_t get_broadcasted_index(size_t flat_index, | ||
| const Shape& input_shape, | ||
| const Shape& output_shape); | ||
|
|
||
| template <typename ValueType> | ||
| class BinaryOpLayerImpl; | ||
| }; | ||
|
|
||
| template <typename ValueType> | ||
| class BinaryOpLayer::BinaryOpLayerImpl : public LayerImpl<ValueType> { | ||
| public: | ||
| BinaryOpLayerImpl() = delete; | ||
| explicit BinaryOpLayerImpl(BinaryOpLayer::Operation op); | ||
|
|
||
| std::vector<ValueType> run( | ||
| const std::vector<ValueType>& input) const override { | ||
| (void)input; | ||
| throw std::runtime_error("BinaryOpLayer requires two inputs"); | ||
| } | ||
|
|
||
| std::vector<ValueType> run(const std::vector<ValueType>& inputA, | ||
| const std::vector<ValueType>& inputB) const; | ||
|
|
||
| private: | ||
| BinaryOpLayer::Operation op_; | ||
| }; | ||
|
|
||
| } // namespace it_lab_ai | ||
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
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
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
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
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
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
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
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
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
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
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
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
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.
It's part of elementwise operation
Uh oh!
There was an error while loading. Please reload this page.
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.
I thought it would be better to put the logic of interaction between two tensors with a fairly comprehensive broadcast logic in an another layer to separate the situations where the input is a single tensor, EWLayer, and where there are two input arrays and 1 output
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.
Or is it better to combine these layers into one?
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.
Ok we can choose compromise, please create operation - Logic and unite all logic operations
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.
Do you mean to add the kDiv operation and name the enumerated type logic?