-
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 "multiply layer" for multiplication **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
246 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
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,51 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/** | ||
* Copyright (C) 2024 SeungBaek Hong <[email protected]> | ||
* | ||
* @file multiply_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 multiply layer class (operation layer) | ||
* | ||
*/ | ||
|
||
#include <multiply_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 MultiplyLayer::finalize(InitLayerContext &context) { | ||
context.setOutputDimensions({context.getInputDimensions()[0]}); | ||
} | ||
|
||
void MultiplyLayer::forwarding_operation(const Tensor &input0, | ||
const Tensor &input1, Tensor &hidden) { | ||
input0.multiply(input1, hidden); | ||
} | ||
|
||
void MultiplyLayer::calcDerivative(RunLayerContext &context) { | ||
context.getOutgoingDerivative(0).copy( | ||
context.getIncomingDerivative(SINGLE_INOUT_IDX) | ||
.multiply(context.getInput(1))); | ||
|
||
context.getOutgoingDerivative(1).copy( | ||
context.getIncomingDerivative(SINGLE_INOUT_IDX) | ||
.multiply(context.getInput(0))); | ||
} | ||
|
||
void MultiplyLayer::setProperty(const std::vector<std::string> &values) { | ||
auto remain_props = loadProperties(values, multiply_props); | ||
if (!remain_props.empty()) { | ||
std::string msg = "[MultiplyLayer] 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 multiply_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 mul layer class (operation layer) | ||
* | ||
*/ | ||
|
||
#ifndef __MULTIPLY_LAYER_H__ | ||
#define __MULTIPLY_LAYER_H__ | ||
#ifdef __cplusplus | ||
|
||
#include <common_properties.h> | ||
#include <layer_devel.h> | ||
#include <operation_layer.h> | ||
|
||
namespace nntrainer { | ||
|
||
/** | ||
* @class Multiply Layer | ||
* @brief Multiply Layer | ||
*/ | ||
class MultiplyLayer : public BinaryOperationLayer { | ||
public: | ||
/** | ||
* @brief Constructor of Multiply Layer | ||
*/ | ||
MultiplyLayer() : BinaryOperationLayer(), multiply_props(props::Print()) {} | ||
|
||
/** | ||
* @brief Destructor of Multiply Layer | ||
*/ | ||
~MultiplyLayer(){}; | ||
|
||
/** | ||
* @brief Move constructor of Multiply Layer. | ||
* @param[in] MultiplyLayer && | ||
*/ | ||
MultiplyLayer(MultiplyLayer &&rhs) noexcept = default; | ||
|
||
/** | ||
* @brief Move assignment operator. | ||
* @parma[in] rhs MultiplyLayer to be moved. | ||
*/ | ||
MultiplyLayer &operator=(MultiplyLayer &&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 MultiplyLayer::type; }; | ||
|
||
std::tuple<props::Print> multiply_props; | ||
|
||
inline static const std::string type = "multiply"; | ||
}; | ||
|
||
} // namespace nntrainer | ||
|
||
#endif /* __cplusplus */ | ||
#endif /* __MULTIPLY_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,31 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/** | ||
* Copyright (C) 2024 SeungBaek Hong <[email protected]> | ||
* | ||
* @file unittest_layers_multiply.cpp | ||
* @date 30 August 2024 | ||
* @brief Mul 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 <layers_common_tests.h> | ||
#include <multiply_layer.h> | ||
|
||
auto semantic_multiply = LayerSemanticsParamType( | ||
nntrainer::createLayer<nntrainer::MultiplyLayer>, | ||
nntrainer::MultiplyLayer::type, {}, | ||
LayerCreateSetPropertyOptions::AVAILABLE_FROM_APP_CONTEXT, false, 1); | ||
|
||
auto semantic_multiply_multi = LayerSemanticsParamType( | ||
nntrainer::createLayer<nntrainer::MultiplyLayer>, | ||
nntrainer::MultiplyLayer::type, {}, | ||
LayerCreateSetPropertyOptions::AVAILABLE_FROM_APP_CONTEXT, false, 2); | ||
|
||
GTEST_PARAMETER_TEST(Multiply, LayerSemantics, | ||
::testing::Values(semantic_multiply, | ||
semantic_multiply_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