-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- added "divide layer" for division. **Self evaluation:** 1. Build test: [X]Passed [ ]Failed [ ]Skipped 2. Run test: [X]Passed [ ]Failed [ ]Skipped Signed-off-by: Seungbaek Hong <[email protected]>
- Loading branch information
Showing
10 changed files
with
231 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/** | ||
* Copyright (C) 2024 SeungBaek Hong <[email protected]> | ||
* | ||
* @file divide_layer.cpp | ||
* @date 10 Oct 2024 | ||
* @see https://github.com/nnstreamer/nntrainer | ||
* @author SeungBaek Hong <[email protected]> | ||
* @bug No known bugs except for NYI items | ||
* @brief This is div layer class (operation layer) | ||
* | ||
*/ | ||
|
||
#include <divide_layer.h> | ||
#include <nntrainer_error.h> | ||
#include <nntrainer_log.h> | ||
#include <node_exporter.h> | ||
#include <util_func.h> | ||
|
||
#include <layer_context.h> | ||
|
||
namespace nntrainer { | ||
|
||
void DivideLayer::finalize(InitLayerContext &context) { | ||
context.setOutputDimensions({context.getInputDimensions()[0]}); | ||
} | ||
|
||
void DivideLayer::forwarding_operation(const Tensor &input0, | ||
const Tensor &input1, Tensor &hidden) { | ||
input0.divide(input1, hidden); | ||
} | ||
|
||
void DivideLayer::calcDerivative(RunLayerContext &context) { | ||
context.getOutgoingDerivative(0).copy( | ||
context.getIncomingDerivative(SINGLE_INOUT_IDX) | ||
.divide(context.getInput(1))); | ||
|
||
context.getOutgoingDerivative(1).copy( | ||
context.getIncomingDerivative(SINGLE_INOUT_IDX) | ||
.multiply(context.getInput(0).multiply(-1)) | ||
.divide(context.getInput(1).pow(2))); | ||
} | ||
|
||
void DivideLayer::setProperty(const std::vector<std::string> &values) { | ||
auto remain_props = loadProperties(values, divide_props); | ||
if (!remain_props.empty()) { | ||
std::string msg = "[DivideLayer] 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,102 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/** | ||
* Copyright (C) 2024 SeungBaek Hong <[email protected]> | ||
* | ||
* @file divide_layer.h | ||
* @date 10 Oct 2024 | ||
* @see https://github.com/nnstreamer/nntrainer | ||
* @author SeungBaek Hong <[email protected]> | ||
* @bug No known bugs except for NYI items | ||
* @brief This is div layer class (operation layer) | ||
* | ||
*/ | ||
|
||
#ifndef __DIVIDE_LAYER_H__ | ||
#define __DIVIDE_LAYER_H__ | ||
#ifdef __cplusplus | ||
|
||
#include <common_properties.h> | ||
#include <layer_devel.h> | ||
#include <operation_layer.h> | ||
|
||
namespace nntrainer { | ||
|
||
/** | ||
* @class Divide Layer | ||
* @brief Divide Layer | ||
*/ | ||
class DivideLayer : public BinaryOperationLayer { | ||
public: | ||
/** | ||
* @brief Constructor of Divide Layer | ||
*/ | ||
DivideLayer() : BinaryOperationLayer(), divide_props(props::Print()) {} | ||
|
||
/** | ||
* @brief Destructor of Divide Layer | ||
*/ | ||
~DivideLayer(){}; | ||
|
||
/** | ||
* @brief Move constructor of Divide Layer. | ||
* @param[in] DivideLayer && | ||
*/ | ||
DivideLayer(DivideLayer &&rhs) noexcept = default; | ||
|
||
/** | ||
* @brief Move assignment operator. | ||
* @parma[in] rhs DivideLayer to be moved. | ||
*/ | ||
DivideLayer &operator=(DivideLayer &&rhs) = default; | ||
|
||
/** | ||
* @copydoc Layer::finalize(InitLayerContext &context) | ||
*/ | ||
void finalize(InitLayerContext &context) final; | ||
|
||
/** | ||
* @brief forwarding operation for add | ||
* | ||
* @param input0 input tensor 0 | ||
* @param input1 input tensor 1 | ||
* @param hidden tensor to store the result of addition | ||
*/ | ||
void forwarding_operation(const Tensor &input0, const Tensor &input1, | ||
Tensor &hidden) final; | ||
|
||
/** | ||
* @copydoc Layer::calcDerivative(RunLayerContext &context) | ||
*/ | ||
void calcDerivative(RunLayerContext &context) final; | ||
|
||
/** | ||
* @copydoc bool supportBackwarding() const | ||
*/ | ||
bool supportBackwarding() const final { return true; }; | ||
|
||
/** | ||
* @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 DivideLayer::type; }; | ||
|
||
std::tuple<props::Print> divide_props; | ||
|
||
inline static const std::string type = "divide"; | ||
}; | ||
|
||
} // namespace nntrainer | ||
|
||
#endif /* __cplusplus */ | ||
#endif /* __DIVIDE_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
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,28 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/** | ||
* Copyright (C) 2024 SeungBaek Hong <[email protected]> | ||
* | ||
* @file unittest_layers_divide.cpp | ||
* @date 30 August 2024 | ||
* @brief Divide Layer Test | ||
* @see https://github.com/nnstreamer/nntrainer | ||
* @author SeungBaek Hong <[email protected]> | ||
* @bug No known bugs except for NYI items | ||
*/ | ||
#include <tuple> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <divide_layer.h> | ||
#include <layers_common_tests.h> | ||
|
||
auto semantic_divide = LayerSemanticsParamType( | ||
nntrainer::createLayer<nntrainer::DivideLayer>, nntrainer::DivideLayer::type, | ||
{}, LayerCreateSetPropertyOptions::AVAILABLE_FROM_APP_CONTEXT, false, 1); | ||
|
||
auto semantic_divide_multi = LayerSemanticsParamType( | ||
nntrainer::createLayer<nntrainer::DivideLayer>, nntrainer::DivideLayer::type, | ||
{}, LayerCreateSetPropertyOptions::AVAILABLE_FROM_APP_CONTEXT, false, 2); | ||
|
||
GTEST_PARAMETER_TEST(Divide, LayerSemantics, | ||
::testing::Values(semantic_divide, semantic_divide_multi)); |
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