-
Notifications
You must be signed in to change notification settings - Fork 80
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
[layer] add pow operation layer @open sesame 12/06 20:13 #2801
Merged
Merged
Changes from all commits
Commits
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 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 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 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 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 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 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 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 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 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 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 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 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,50 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/** | ||
* Copyright (C) 2024 SeungBaek Hong <[email protected]> | ||
* | ||
* @file pow_layer.cpp | ||
* @date 20 Nov 2024 | ||
* @see https://github.com/nnstreamer/nntrainer | ||
* @author SeungBaek Hong <[email protected]> | ||
* @bug No known bugs except for NYI items | ||
* @brief This is pow layer class (operation layer) | ||
* | ||
*/ | ||
|
||
#include "common_properties.h" | ||
#include <nntrainer_error.h> | ||
#include <nntrainer_log.h> | ||
#include <node_exporter.h> | ||
#include <pow_layer.h> | ||
#include <util_func.h> | ||
|
||
#include <layer_context.h> | ||
|
||
namespace nntrainer { | ||
|
||
void PowLayer::finalize(InitLayerContext &context) { | ||
context.setOutputDimensions({context.getInputDimensions()[0]}); | ||
} | ||
|
||
void PowLayer::forwarding_operation(const Tensor &input, Tensor &hidden) { | ||
float exponent = std::get<props::Exponent>(pow_props).get(); | ||
input.pow(exponent, hidden); | ||
} | ||
|
||
void PowLayer::calcDerivative(RunLayerContext &context) { | ||
float exp = std::get<props::Exponent>(pow_props).get(); | ||
context.getOutgoingDerivative(0).copy( | ||
context.getIncomingDerivative(SINGLE_INOUT_IDX) | ||
.multiply(exp) | ||
.multiply(context.getInput(0).pow(exp - 1.0f))); | ||
} | ||
|
||
void PowLayer::setProperty(const std::vector<std::string> &values) { | ||
auto remain_props = loadProperties(values, pow_props); | ||
if (!remain_props.empty()) { | ||
std::string msg = "[PowLayer] Unknown Layer Properties count " + | ||
std::to_string(values.size()); | ||
throw exception::not_supported(msg); | ||
} | ||
} | ||
} /* namespace nntrainer */ |
This file contains 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,124 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/** | ||
* Copyright (C) 2024 SeungBaek Hong <[email protected]> | ||
* | ||
* @file pow_layer.h | ||
* @date 20 Nov 2024 | ||
* @see https://github.com/nnstreamer/nntrainer | ||
* @author SeungBaek Hong <[email protected]> | ||
* @bug No known bugs except for NYI items | ||
* @brief This is pow layer class (operation layer) | ||
* | ||
*/ | ||
|
||
#ifndef __POW_LAYER_H__ | ||
#define __POW_LAYER_H__ | ||
#ifdef __cplusplus | ||
|
||
#include <common_properties.h> | ||
#include <layer_devel.h> | ||
#include <operation_layer.h> | ||
|
||
namespace nntrainer { | ||
|
||
/** | ||
* @class Pow Layer | ||
* @brief Pow Layer | ||
*/ | ||
class PowLayer : public UnaryOperationLayer { | ||
public: | ||
/** | ||
* @brief Constructor of Pow Layer | ||
*/ | ||
PowLayer() : | ||
UnaryOperationLayer(), | ||
pow_props(props::Print(), props::InPlaceProp(), props::Exponent()), | ||
support_backwarding(true) {} | ||
|
||
/** | ||
* @brief Destructor of Pow Layer | ||
*/ | ||
~PowLayer(){}; | ||
|
||
/** | ||
* @brief Move constructor of Pow Layer. | ||
* @param[in] PowLayer && | ||
*/ | ||
PowLayer(PowLayer &&rhs) noexcept = default; | ||
|
||
/** | ||
* @brief Move assignment operator. | ||
* @parma[in] rhs PowLayer to be moved. | ||
*/ | ||
PowLayer &operator=(PowLayer &&rhs) = default; | ||
|
||
/** | ||
* @copydoc Layer::finalize(InitLayerContext &context) | ||
*/ | ||
void finalize(InitLayerContext &context) final; | ||
|
||
/** | ||
* @brief forwarding operation for pow | ||
* | ||
* @param input input tensor | ||
* @param hidden tensor to store the result value | ||
*/ | ||
void forwarding_operation(const Tensor &input, Tensor &hidden) final; | ||
|
||
/** | ||
* @copydoc Layer::calcDerivative(RunLayerContext &context) | ||
*/ | ||
void calcDerivative(RunLayerContext &context) final; | ||
|
||
/** | ||
* @copydoc bool supportBackwarding() const | ||
*/ | ||
bool supportBackwarding() const final { return support_backwarding; }; | ||
|
||
/** | ||
* @brief Initialize the in-place settings of the layer | ||
* @return InPlaceType | ||
*/ | ||
InPlaceType initializeInPlace() final { | ||
if (std::get<props::InPlaceProp>(pow_props).empty() || | ||
!std::get<props::InPlaceProp>(pow_props).get()) { | ||
is_inplace = false; | ||
support_backwarding = true; | ||
} else { | ||
is_inplace = true; | ||
support_backwarding = false; | ||
} | ||
|
||
if (!supportInPlace()) | ||
return InPlaceType::NONE; | ||
else | ||
return InPlaceType::NON_RESTRICTING; | ||
} | ||
|
||
/** | ||
* @copydoc Layer::exportTo(Exporter &exporter, ml::train::ExportMethods | ||
* method) | ||
*/ | ||
void exportTo(Exporter &exporter, | ||
const ml::train::ExportMethods &method) const final {} | ||
|
||
/** | ||
* @copydoc Layer::setProperty(const std::vector<std::string> &values) | ||
*/ | ||
void setProperty(const std::vector<std::string> &values) final; | ||
|
||
/** | ||
* @copydoc Layer::getType() | ||
*/ | ||
const std::string getType() const final { return PowLayer::type; }; | ||
|
||
std::tuple<props::Print, props::InPlaceProp, props::Exponent> pow_props; | ||
bool support_backwarding; /**< support backwarding */ | ||
|
||
inline static const std::string type = "pow"; | ||
}; | ||
|
||
} // namespace nntrainer | ||
|
||
#endif /* __cplusplus */ | ||
#endif /* __POW_LAYER_H__ */ |
This file contains 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
Binary file not shown.
This file contains 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 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.
This is a simple suggestion (not directly related to this PR though).
What about using prefix like 'op_' to operation layers?
It might reduce the confusion on the layer types (e.g., add_layer & addition_layer).
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.
That sounds good idea. I'll reflect it in later pr. thanks!