-
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 "add layer" - added a "model unit test" for add layer. Since 'model-level unit test' havn't been run for a long time, I diabled some test cases that causing issues when running model unit test. Many people gave great feedback, so I've improved the structure accordingly. - An upper class called "OperationLayer" was added to reduce redundant code. - Based on the number of input tensors, the behavior of "OperationLayer" has been classified into two types: unary and binary operations. - Various additional code cleanups have also taken place. - there is an issue where committing compressed files containing golden data for unit test prevents pushing changes to the remote server. (I've confirmed that all unit tests pass locally using that golden data.) **Self evaluation:** 1. Build test: [X]Passed [X]Failed [ ]Skipped 2. Run test: [X]Passed [X]Failed [ ]Skipped Signed-off-by: Seungbaek Hong <[email protected]>
- Loading branch information
Showing
12 changed files
with
700 additions
and
159 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,54 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/** | ||
* Copyright (C) 2024 SeungBaek Hong <[email protected]> | ||
* | ||
* @file add_layer.cpp | ||
* @date 7 Oct 2024 | ||
* @see https://github.com/nnstreamer/nntrainer | ||
* @author SeungBaek Hong <[email protected]> | ||
* @bug No known bugs except for NYI items | ||
* @brief This is add layer class (operation layer) | ||
* | ||
*/ | ||
|
||
#include <add_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 AddLayer::finalize(InitLayerContext &context) { | ||
op_type = OperationType::BINARY; | ||
context.setOutputDimensions({context.getInputDimensions()[0]}); | ||
} | ||
|
||
void AddLayer::forwarding_operation(const Tensor &input, Tensor &hidden) {} | ||
|
||
void AddLayer::forwarding_operation(const Tensor &input0, const Tensor &input1, | ||
Tensor &hidden) { | ||
input0.add(input1, hidden); | ||
} | ||
|
||
void AddLayer::calcDerivative(RunLayerContext &context) { | ||
context.getOutgoingDerivative(0).copy( | ||
context.getIncomingDerivative(SINGLE_INOUT_IDX)); | ||
|
||
context.getOutgoingDerivative(1).copy( | ||
context.getIncomingDerivative(SINGLE_INOUT_IDX)); | ||
} | ||
|
||
void AddLayer::setProperty(const std::vector<std::string> &values) { | ||
auto remain_props = loadProperties(values, add_props); | ||
if (!remain_props.empty()) { | ||
std::string msg = "[AddLayer] 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,111 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/** | ||
* Copyright (C) 2024 SeungBaek Hong <[email protected]> | ||
* | ||
* @file add_layer.h | ||
* @date 2 August 2024 | ||
* @see https://github.com/nnstreamer/nntrainer | ||
* @author SeungBaek Hong <[email protected]> | ||
* @bug No known bugs except for NYI items | ||
* @brief This is add layer class (operation layer) | ||
* | ||
*/ | ||
|
||
#ifndef __ADD_LAYER_H__ | ||
#define __ADD_LAYER_H__ | ||
#ifdef __cplusplus | ||
|
||
#include <common_properties.h> | ||
#include <layer_devel.h> | ||
#include <operation_layer.h> | ||
|
||
namespace nntrainer { | ||
|
||
/** | ||
* @class Add Layer | ||
* @brief Add Layer | ||
*/ | ||
class AddLayer : public OperationLayer { | ||
public: | ||
/** | ||
* @brief Destructor of Add Layer | ||
*/ | ||
~AddLayer() {} | ||
|
||
/** | ||
* @brief Constructor of Add Layer | ||
*/ | ||
AddLayer() : OperationLayer(), add_props(props::Print()) {} | ||
|
||
/** | ||
* @brief Move constructor of Add Layer. | ||
* @param[in] AddLayer && | ||
*/ | ||
AddLayer(AddLayer &&rhs) noexcept = default; | ||
|
||
/** | ||
* @brief Move assignment operator. | ||
* @parma[in] rhs AddLayer to be moved. | ||
*/ | ||
AddLayer &operator=(AddLayer &&rhs) = default; | ||
|
||
/** | ||
* @copydoc Layer::finalize(InitLayerContext &context) | ||
*/ | ||
void finalize(InitLayerContext &context) final; | ||
|
||
void forwarding_operation(const Tensor &input, Tensor &hidden) 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::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) 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 AddLayer::type; } | ||
|
||
std::tuple<props::Print> add_props; | ||
|
||
inline static const std::string type = "add"; | ||
}; | ||
|
||
} // namespace nntrainer | ||
|
||
#endif /* __cplusplus */ | ||
#endif /* __ADD_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/** | ||
* Copyright (C) 2024 SeungBaek Hong <[email protected]> | ||
* | ||
* @file operation_layer.h | ||
* @date 4 Oct 2024 | ||
* @see https://github.com/nnstreamer/nntrainer | ||
* @author SeungBaek Hong <[email protected]> | ||
* @bug No known bugs except for NYI items | ||
* @brief This is common class for operation layers | ||
* | ||
*/ | ||
|
||
#include <layer_devel.h> | ||
#include <nntrainer_error.h> | ||
#include <nntrainer_log.h> | ||
#include <node_exporter.h> | ||
#include <tensor.h> | ||
#include <util_func.h> | ||
|
||
#include <layer_context.h> | ||
|
||
#include <fstream> | ||
#include <iostream> | ||
|
||
namespace nntrainer { | ||
|
||
enum class OperationType { NONE, UNARY, BINARY }; | ||
|
||
class OperationLayer : public Layer { | ||
public: | ||
virtual void forwarding_operation(const Tensor &input, Tensor &hidden) = 0; | ||
|
||
virtual void forwarding_operation(const Tensor &input0, const Tensor &input1, | ||
Tensor &hidden) = 0; | ||
|
||
void forwarding(RunLayerContext &context, bool training) override { | ||
Tensor &hidden_ = context.getOutput(SINGLE_INOUT_IDX); | ||
|
||
if (op_type == OperationType::UNARY) { | ||
const Tensor input = context.getInput(0); | ||
forwarding_operation(input, hidden_); | ||
} else if (op_type == OperationType::BINARY) { | ||
const Tensor &input0 = context.getInput(0); | ||
const Tensor &input1 = context.getInput(1); | ||
forwarding_operation(input0, input1, hidden_); | ||
} else { | ||
throw std::invalid_argument("Operation type is not defined"); | ||
} | ||
} | ||
|
||
void incremental_forwarding(RunLayerContext &context, unsigned int from, | ||
unsigned int to, bool training) override { | ||
if (from) { | ||
NNTR_THROW_IF(to - from != 1, std::invalid_argument) | ||
<< "incremental step size is not 1"; | ||
from = 0; | ||
to = 1; | ||
} | ||
|
||
Tensor &hidden_ = context.getOutput(SINGLE_INOUT_IDX); | ||
TensorDim hidden_dim = hidden_.getDim(); | ||
TensorDim hidden_step_dim = hidden_dim; | ||
|
||
hidden_step_dim.batch(1); | ||
hidden_step_dim.height(to - from); | ||
|
||
if (op_type == OperationType::UNARY) { | ||
const Tensor &input = context.getInput(0); | ||
TensorDim input_dim = input.getDim(); | ||
TensorDim input_step_dim = input_dim; | ||
input_step_dim.batch(1); | ||
input_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); | ||
|
||
Tensor input_step = input.getSharedDataTensor( | ||
input_step_dim, b * input_dim.getFeatureLen(), true); | ||
|
||
forwarding_operation(input_step, hidden_step); | ||
} | ||
} else if (op_type == OperationType::BINARY) { | ||
const Tensor &input0 = context.getInput(0); | ||
const Tensor &input1 = context.getInput(1); | ||
|
||
TensorDim input0_dim = input0.getDim(); | ||
TensorDim input1_dim = input1.getDim(); | ||
if (input0_dim != input1_dim) { | ||
throw std::invalid_argument( | ||
"If the two input dimensions are different, the incremental " | ||
"forwarding implementation must be overridden."); | ||
} | ||
|
||
TensorDim input_step_dim = input0_dim; | ||
input_step_dim.batch(1); | ||
input_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); | ||
|
||
Tensor input0_step = input0.getSharedDataTensor( | ||
input_step_dim, b * input0_dim.getFeatureLen(), true); | ||
|
||
Tensor input1_step = input1.getSharedDataTensor( | ||
input_step_dim, b * input1_dim.getFeatureLen(), true); | ||
|
||
forwarding_operation(input0_step, input1_step, hidden_step); | ||
} | ||
} else { | ||
throw std::invalid_argument("Operation type is not defined"); | ||
} | ||
} | ||
|
||
OperationType op_type = OperationType::NONE; /**< type of operation */ | ||
static constexpr size_t SINGLE_INOUT_IDX = 0; | ||
}; | ||
} // 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
Oops, something went wrong.