-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- This layer contains only weights for building tensor-level graph **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
05f7b4a
commit 23c0983
Showing
11 changed files
with
267 additions
and
5 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
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,87 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/** | ||
* Copyright (C) 2024 SeungBaek Hong <[email protected]> | ||
* | ||
* @file weight_layer.cpp | ||
* @date 2 August 2024 | ||
* @brief This is a layer that simply stores a weight tensor without any | ||
* operation. | ||
* @see https://github.com/nnstreamer/nntrainer | ||
* @author SeungBaek Hong <[email protected]> | ||
* @bug No known bugs except for NYI items | ||
* | ||
*/ | ||
|
||
#include <common_properties.h> | ||
#include <layer_context.h> | ||
#include <lazy_tensor.h> | ||
#include <nntrainer_error.h> | ||
#include <nntrainer_log.h> | ||
#include <node_exporter.h> | ||
#include <util_func.h> | ||
#include <weight_layer.h> | ||
|
||
#include <iostream> | ||
|
||
namespace nntrainer { | ||
|
||
static constexpr size_t SINGLE_INOUT_IDX = 0; | ||
|
||
WeightLayer::WeightLayer() : LayerImpl() { | ||
weight_idx.fill(std::numeric_limits<unsigned>::max()); | ||
} | ||
|
||
void WeightLayer::finalize(InitLayerContext &context) { | ||
auto &weight_regularizer = | ||
std::get<props::WeightRegularizer>(*layer_impl_props); | ||
auto &weight_regularizer_constant = | ||
std::get<props::WeightRegularizerConstant>(*layer_impl_props); | ||
auto &weight_initializer = | ||
std::get<props::WeightInitializer>(*layer_impl_props); | ||
auto &weight_decay = std::get<props::WeightDecay>(*layer_impl_props); | ||
|
||
const auto &weight_dim = std::get<props::TensorDimension>(weight_props).get(); | ||
|
||
std::vector<TensorDim> output_dims(1); | ||
|
||
output_dims[0] = weight_dim; | ||
|
||
output_dims[0].setTensorType( | ||
{context.getFormat(), context.getActivationDataType()}); | ||
|
||
context.setOutputDimensions(output_dims); | ||
|
||
weight_idx[0] = context.requestWeight( | ||
weight_dim, weight_initializer, weight_regularizer, | ||
weight_regularizer_constant, weight_decay, "weight", true); | ||
} | ||
|
||
void WeightLayer::exportTo(Exporter &exporter, | ||
const ml::train::ExportMethods &method) const { | ||
LayerImpl::exportTo(exporter, method); | ||
exporter.saveResult(weight_props, method, this); | ||
} | ||
|
||
void WeightLayer::setProperty(const std::vector<std::string> &values) { | ||
auto remain_props = loadProperties(values, weight_props); | ||
LayerImpl::setProperty(remain_props); | ||
} | ||
|
||
void WeightLayer::forwarding(RunLayerContext &context, bool training) { | ||
Tensor &weight = context.getWeight(weight_idx[0]); | ||
Tensor &output = context.getOutput(SINGLE_INOUT_IDX); | ||
output.copy(weight); | ||
} | ||
|
||
void WeightLayer::calcDerivative(RunLayerContext &context) { | ||
throw exception::not_supported( | ||
"calcDerivative for weight layer is not supported"); | ||
} | ||
|
||
void WeightLayer::calcGradient(RunLayerContext &context) { | ||
Tensor &djdw = context.getWeightGrad(weight_idx[0]); | ||
const Tensor &derivative_ = context.getIncomingDerivative(SINGLE_INOUT_IDX); | ||
djdw.copy(derivative_); | ||
} | ||
|
||
} /* 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,104 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/** | ||
* Copyright (C) 2024 SeungBaek Hong <[email protected]> | ||
* | ||
* @file weight_layer.h | ||
* @date 2 August 2024 | ||
* @brief This is a layer that simply stores a weight tensor without any | ||
* operation. | ||
* @see https://github.com/nnstreamer/nntrainer | ||
* @author SeungBaek Hong <[email protected]> | ||
* @bug No known bugs except for NYI items | ||
* | ||
*/ | ||
|
||
#ifndef __WEIGHT_LAYER_H__ | ||
#define __WEIGHT_LAYER_H__ | ||
#ifdef __cplusplus | ||
|
||
#include <common_properties.h> | ||
#include <layer_impl.h> | ||
|
||
namespace nntrainer { | ||
|
||
/** | ||
* @class Weight Layer | ||
* @brief A layer that simply stores a weight tensor | ||
*/ | ||
class WeightLayer : public LayerImpl { | ||
public: | ||
/** | ||
* @brief Constructor of Weight Layer | ||
*/ | ||
WeightLayer(); | ||
|
||
/** | ||
* @brief Destructor of Weight Layer | ||
*/ | ||
~WeightLayer() = default; | ||
|
||
/** | ||
* @brief Move constructor. | ||
* @param[in] WeightLayer && | ||
*/ | ||
WeightLayer(WeightLayer &&rhs) noexcept = default; | ||
|
||
/** | ||
* @brief Move assignment operator. | ||
* @parma[in] rhs WeightLayer to be moved. | ||
*/ | ||
WeightLayer &operator=(WeightLayer &&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::calcDerivative(RunLayerContext &context) | ||
*/ | ||
void calcDerivative(RunLayerContext &context) override; | ||
|
||
/** | ||
* @copydoc Layer::calcGradient(RunLayerContext &context) | ||
*/ | ||
void calcGradient(RunLayerContext &context) override; | ||
|
||
/** | ||
* @copydoc Layer::exportTo(Exporter &exporter, ml::train::ExportMethods | ||
* method) | ||
*/ | ||
void exportTo(Exporter &exporter, | ||
const ml::train::ExportMethods &method) const override; | ||
|
||
/** | ||
* @copydoc Layer::getType() | ||
*/ | ||
const std::string getType() const override { return WeightLayer::type; }; | ||
|
||
/** | ||
* @copydoc Layer::supportBackwarding() | ||
*/ | ||
bool supportBackwarding() const override { return true; } | ||
|
||
/** | ||
* @copydoc Layer::setProperty(const PropertyType type, const std::string | ||
* &value) | ||
*/ | ||
void setProperty(const std::vector<std::string> &values) override; | ||
|
||
inline static const std::string type = "weight"; | ||
|
||
private: | ||
std::tuple<props::TensorDimension> weight_props; | ||
std::array<unsigned int, 1> weight_idx; /**< indices of the weights */ | ||
}; | ||
} // namespace nntrainer | ||
|
||
#endif /* __cplusplus */ | ||
#endif /* __WEIGHT_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,30 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/** | ||
* Copyright (C) 2024 SeungBaek Hong <[email protected]> | ||
* | ||
* @file unittest_layers_weight.cpp | ||
* @date 30 July 2024 | ||
* @brief Weight 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 <weight_layer.h> | ||
|
||
auto semantic_weight = LayerSemanticsParamType( | ||
nntrainer::createLayer<nntrainer::WeightLayer>, nntrainer::WeightLayer::type, | ||
{"dim=1:1:1"}, LayerCreateSetPropertyOptions::AVAILABLE_FROM_APP_CONTEXT, | ||
false, 1); | ||
|
||
auto semantic_weight_multi = LayerSemanticsParamType( | ||
nntrainer::createLayer<nntrainer::WeightLayer>, nntrainer::WeightLayer::type, | ||
{"dim=1:1:1"}, LayerCreateSetPropertyOptions::AVAILABLE_FROM_APP_CONTEXT, | ||
false, 2); | ||
|
||
GTEST_PARAMETER_TEST(Weight, LayerSemantics, | ||
::testing::Values(semantic_weight, semantic_weight_multi)); |