-
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 "div 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
11 changed files
with
290 additions
and
1 deletion.
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,97 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/** | ||
* Copyright (C) 2024 SeungBaek Hong <[email protected]> | ||
* | ||
* @file div_layer.cpp | ||
* @date 30 August 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 <div_layer.h> | ||
#include <nntrainer_error.h> | ||
#include <nntrainer_log.h> | ||
#include <node_exporter.h> | ||
#include <util_func.h> | ||
|
||
#include <layer_context.h> | ||
|
||
namespace nntrainer { | ||
|
||
static constexpr size_t SINGLE_INOUT_IDX = 0; | ||
|
||
void DivLayer::finalize(InitLayerContext &context) { | ||
context.setOutputDimensions({context.getInputDimensions()[0]}); | ||
} | ||
|
||
void DivLayer::forwarding(RunLayerContext &context, bool training) { | ||
Tensor &hidden_ = context.getOutput(SINGLE_INOUT_IDX); | ||
|
||
const Tensor &input0 = context.getInput(0); | ||
const Tensor &input1 = context.getInput(1); | ||
|
||
input0.divide(input1, hidden_); | ||
} | ||
|
||
void DivLayer::incremental_forwarding(RunLayerContext &context, | ||
unsigned int from, unsigned int to, | ||
bool training) { | ||
Tensor &hidden_ = context.getOutput(SINGLE_INOUT_IDX); | ||
TensorDim hidden_dim = hidden_.getDim(); | ||
TensorDim hidden_step_dim = hidden_dim; | ||
|
||
if (from) { | ||
NNTR_THROW_IF(to - from != 1, std::invalid_argument) | ||
<< "incremental step size is not 1"; | ||
from = 0; | ||
to = 1; | ||
} | ||
|
||
hidden_step_dim.batch(1); | ||
hidden_step_dim.height(to - from); | ||
|
||
for (unsigned int b = 0; b < hidden_.batch(); ++b) { | ||
Tensor hidden_step = hidden_.getSharedDataTensor( | ||
hidden_step_dim, b * hidden_dim.getFeatureLen(), true); | ||
|
||
const Tensor &input0 = context.getInput(0); | ||
const Tensor &input1 = context.getInput(1); | ||
|
||
TensorDim input_dim = input0.getDim(); | ||
TensorDim input_step_dim = input_dim; | ||
input_step_dim.batch(1); | ||
input_step_dim.height(to - from); | ||
|
||
Tensor input0_step = input0.getSharedDataTensor( | ||
input_step_dim, b * input_dim.getFeatureLen(), true); | ||
|
||
Tensor input1_step = input1.getSharedDataTensor( | ||
input_step_dim, b * input_dim.getFeatureLen(), true); | ||
|
||
input0_step.divide(input1_step, hidden_step); | ||
} | ||
} | ||
|
||
void DivLayer::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 DivLayer::setProperty(const std::vector<std::string> &values) { | ||
auto remain_props = loadProperties(values, div_props); | ||
if (!remain_props.empty()) { | ||
std::string msg = "[DivLayer] 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,103 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/** | ||
* Copyright (C) 2024 SeungBaek Hong <[email protected]> | ||
* | ||
* @file div_layer.h | ||
* @date 30 August 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 __DIV_LAYER_H__ | ||
#define __DIV_LAYER_H__ | ||
#ifdef __cplusplus | ||
|
||
#include <common_properties.h> | ||
#include <layer_devel.h> | ||
|
||
namespace nntrainer { | ||
|
||
/** | ||
* @class Div Layer | ||
* @brief Div Layer | ||
*/ | ||
class DivLayer : public Layer { | ||
public: | ||
/** | ||
* @brief Constructor of Div Layer | ||
*/ | ||
DivLayer() : Layer(), div_props(props::Print()) {} | ||
|
||
/** | ||
* @brief Destructor of Div Layer | ||
*/ | ||
~DivLayer(){}; | ||
|
||
/** | ||
* @brief Move constructor of Div Layer. | ||
* @param[in] DivLayer && | ||
*/ | ||
DivLayer(DivLayer &&rhs) noexcept = default; | ||
|
||
/** | ||
* @brief Move assignment operator. | ||
* @parma[in] rhs DivLayer to be moved. | ||
*/ | ||
DivLayer &operator=(DivLayer &&rhs) = default; | ||
|
||
/** | ||
* @copydoc Layer::finalize(InitLayerContext &context) | ||
*/ | ||
void finalize(InitLayerContext &context) override; | ||
|
||
/** | ||
* @copydoc Layer::forwarding(RunLayerContext &context, bool training) | ||
*/ | ||
void forwarding(RunLayerContext &context, bool training) override; | ||
|
||
/** | ||
* @copydoc Layer::incremental_forwarding(RunLayerContext &context, unsigned | ||
* int from, unsigned int to, bool training) | ||
*/ | ||
void incremental_forwarding(RunLayerContext &context, unsigned int from, | ||
unsigned int to, bool training) override; | ||
|
||
/** | ||
* @copydoc Layer::calcDerivative(RunLayerContext &context) | ||
*/ | ||
void calcDerivative(RunLayerContext &context) override; | ||
|
||
/** | ||
* @copydoc bool supportBackwarding() const | ||
*/ | ||
bool supportBackwarding() const override { return true; }; | ||
|
||
/** | ||
* @copydoc Layer::exportTo(Exporter &exporter, ml::train::ExportMethods | ||
* method) | ||
*/ | ||
void exportTo(Exporter &exporter, | ||
const ml::train::ExportMethods &method) const override {} | ||
|
||
/** | ||
* @copydoc Layer::setProperty(const std::vector<std::string> &values) | ||
*/ | ||
void setProperty(const std::vector<std::string> &values) override; | ||
|
||
/** | ||
* @copydoc Layer::getType() | ||
*/ | ||
const std::string getType() const override { return DivLayer::type; }; | ||
|
||
std::tuple<props::Print> div_props; | ||
|
||
inline static const std::string type = "div"; | ||
}; | ||
|
||
} // namespace nntrainer | ||
|
||
#endif /* __cplusplus */ | ||
#endif /* __DIV_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
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_div.cpp | ||
* @date 30 August 2024 | ||
* @brief Div 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 <div_layer.h> | ||
#include <layers_common_tests.h> | ||
|
||
auto semantic_div = LayerSemanticsParamType( | ||
nntrainer::createLayer<nntrainer::DivLayer>, nntrainer::DivLayer::type, {}, | ||
LayerCreateSetPropertyOptions::AVAILABLE_FROM_APP_CONTEXT, false, 1); | ||
|
||
auto semantic_div_multi = LayerSemanticsParamType( | ||
nntrainer::createLayer<nntrainer::DivLayer>, nntrainer::DivLayer::type, {}, | ||
LayerCreateSetPropertyOptions::AVAILABLE_FROM_APP_CONTEXT, false, 2); | ||
|
||
GTEST_PARAMETER_TEST(Div, LayerSemantics, | ||
::testing::Values(semantic_div, semantic_div_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