-
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 "subtract layer" - added a "model unit test" for sub layer. 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. **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
1 parent
eb7cf07
commit 6a06ec6
Showing
11 changed files
with
441 additions
and
141 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
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,49 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/** | ||
* Copyright (C) 2024 SeungBaek Hong <[email protected]> | ||
* | ||
* @file subtract_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 subtract layer class (operation layer) | ||
* | ||
*/ | ||
|
||
#include <nntrainer_error.h> | ||
#include <nntrainer_log.h> | ||
#include <node_exporter.h> | ||
#include <subtract_layer.h> | ||
#include <util_func.h> | ||
|
||
#include <layer_context.h> | ||
|
||
namespace nntrainer { | ||
|
||
void SubtractLayer::finalize(InitLayerContext &context) { | ||
context.setOutputDimensions({context.getInputDimensions()[0]}); | ||
} | ||
|
||
void SubtractLayer::forwarding_operation(const Tensor &input0, | ||
const Tensor &input1, Tensor &hidden) { | ||
input0.subtract(input1, hidden); | ||
} | ||
|
||
void SubtractLayer::calcDerivative(RunLayerContext &context) { | ||
context.getOutgoingDerivative(0).copy( | ||
context.getIncomingDerivative(SINGLE_INOUT_IDX)); | ||
|
||
context.getOutgoingDerivative(1).copy( | ||
context.getIncomingDerivative(SINGLE_INOUT_IDX).multiply(-1)); | ||
} | ||
|
||
void SubtractLayer::setProperty(const std::vector<std::string> &values) { | ||
auto remain_props = loadProperties(values, subtract_props); | ||
if (!remain_props.empty()) { | ||
std::string msg = "[SubtractLayer] 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 subtract_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 subtract layer class (operation layer) | ||
* | ||
*/ | ||
|
||
#ifndef __SUBTRACT_LAYER_H__ | ||
#define __SUBTRACT_LAYER_H__ | ||
#ifdef __cplusplus | ||
|
||
#include <common_properties.h> | ||
#include <layer_devel.h> | ||
#include <operation_layer.h> | ||
|
||
namespace nntrainer { | ||
|
||
/** | ||
* @class Subtract Layer | ||
* @brief Subtract Layer | ||
*/ | ||
class SubtractLayer : public BinaryOperationLayer { | ||
public: | ||
/** | ||
* @brief Constructor of Subtract Layer | ||
*/ | ||
SubtractLayer() : BinaryOperationLayer(), subtract_props(props::Print()) {} | ||
|
||
/** | ||
* @brief Destructor of Sub Layer | ||
*/ | ||
~SubtractLayer(){}; | ||
|
||
/** | ||
* @brief Move constructor of Sub Layer. | ||
* @param[in] SubtractLayer && | ||
*/ | ||
SubtractLayer(SubtractLayer &&rhs) noexcept = default; | ||
|
||
/** | ||
* @brief Move assignment operator. | ||
* @parma[in] rhs SubtractLayer to be moved. | ||
*/ | ||
SubtractLayer &operator=(SubtractLayer &&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 SubtractLayer::type; }; | ||
|
||
std::tuple<props::Print> subtract_props; | ||
|
||
inline static const std::string type = "subtract"; | ||
}; | ||
|
||
} // namespace nntrainer | ||
|
||
#endif /* __cplusplus */ | ||
#endif /* __SUBTRACT_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
Oops, something went wrong.