From 616f3846bf351bcbe92f593b644516c5cea42791 Mon Sep 17 00:00:00 2001 From: Parichay Kapoor Date: Wed, 14 Jul 2021 21:03:22 +0900 Subject: [PATCH] [layer] Enable permute layer for V2 Enable permute layer for V2 design. Signed-off-by: Parichay Kapoor --- nntrainer/layers/permute_layer.cpp | 63 ++++++++---------------- nntrainer/layers/permute_layer.h | 51 ++++++++----------- test/unittest/layers/meson.build | 1 + test/unittest/layers/unittest_layers_permute.cpp | 24 +++++++++ 4 files changed, 66 insertions(+), 73 deletions(-) create mode 100644 test/unittest/layers/unittest_layers_permute.cpp diff --git a/nntrainer/layers/permute_layer.cpp b/nntrainer/layers/permute_layer.cpp index e717844..dec413d 100644 --- a/nntrainer/layers/permute_layer.cpp +++ b/nntrainer/layers/permute_layer.cpp @@ -22,6 +22,8 @@ namespace nntrainer { +static constexpr size_t SINGLE_INOUT_IDX = 0; + bool props::PermuteDims::isValid(const unsigned int &value) const { return 0 < value && value <= 3; } @@ -40,7 +42,7 @@ buildTrasposeString(const std::array &arr) { return ss.str(); } -int PermuteLayer::initialize(Manager &manager) { +void PermuteLayer::finalize(InitLayerContext &context) { auto initiate_direction = [this] { std::bitset<3> check_transpose; /**< check if transpose contains all axis */ @@ -59,62 +61,37 @@ int PermuteLayer::initialize(Manager &manager) { rdirection_str = buildTrasposeString(direction); }; - auto initiate_dimension = [this] { - output_dim[0] = input_dim[0].transpose(direction_str); - }; - - try { - initiate_direction(); - initiate_dimension(); - } catch (std::exception &e) { - ml_loge("[Permute] Initiation failed, reason: %s", e.what()); - return ML_ERROR_INVALID_PARAMETER; - } - - return ML_ERROR_NONE; + initiate_direction(); + context.setOutputDimensions( + {context.getInputDimensions()[SINGLE_INOUT_IDX].transpose(direction_str)}); } -void PermuteLayer::forwarding(bool training) { - auto &input_ = net_input[0]->getVariableRef(); - auto &hidden_ = net_hidden[0]->getVariableRef(); +void PermuteLayer::forwarding(RunLayerContext &context, bool training) { + Tensor &hidden_ = context.getOutput(SINGLE_INOUT_IDX); + Tensor &input_ = context.getInput(SINGLE_INOUT_IDX); input_.transpose(direction_str, hidden_); } -void PermuteLayer::calcDerivative() { - auto &input_grad = net_input[0]->getGradientRef(); - auto &hidden_grad = net_hidden[0]->getGradientRef(); +void PermuteLayer::calcDerivative(RunLayerContext &context) { + Tensor &hidden_grad = context.getIncomingDerivative(SINGLE_INOUT_IDX); + Tensor &input_grad = context.getOutgoingDerivative(SINGLE_INOUT_IDX); hidden_grad.transpose(rdirection_str, input_grad); } -void PermuteLayer::copy(std::shared_ptr l) { - LayerV1::copy(l); - - std::shared_ptr from = - std::static_pointer_cast(l); - - direction = from->direction; - direction_str = from->direction_str; - reverse_direction = from->reverse_direction; - rdirection_str = from->rdirection_str; -} - -void PermuteLayer::export_to(Exporter &exporter, ExportMethods method) const { - LayerV1::export_to(exporter, method); +void PermuteLayer::exportTo(Exporter &exporter, + const ExportMethods &method) const { exporter.saveResult(std::forward_as_tuple(direction), method); } -int PermuteLayer::setProperty(std::vector values) { - try { - auto left_values = loadProperties(values, std::forward_as_tuple(direction)); - LayerV1::setProperty(left_values); - } catch (std::invalid_argument &e) { - ml_loge("[PermuteLayer] failed to set property, reason: %s", e.what()); - return ML_ERROR_INVALID_PARAMETER; +void PermuteLayer::setProperty(const std::vector &values) { + auto left_values = loadProperties(values, std::forward_as_tuple(direction)); + if (!left_values.empty()) { + std::string msg = "[PermuteLayer] Unknown properties set with count" + + std::to_string(values.size()); + throw exception::not_supported(msg); } - - return ML_ERROR_NONE; } } // namespace nntrainer diff --git a/nntrainer/layers/permute_layer.h b/nntrainer/layers/permute_layer.h index 08bbed0..d7cbf82 100644 --- a/nntrainer/layers/permute_layer.h +++ b/nntrainer/layers/permute_layer.h @@ -10,6 +10,7 @@ * @author Jihoon Lee * @bug No known bugs except for NYI items */ + #ifndef __PERMUTE_LAYER_H__ #define __PERMUTE_LAYER_H__ @@ -17,7 +18,7 @@ #include #include -#include +#include #include namespace nntrainer { @@ -46,17 +47,13 @@ public: * @class PermuteLayer * @brief Permute layer to transpose a tensor */ -class PermuteLayer : public LayerV1 { +class PermuteLayer : public Layer { public: /** * @brief Constructor of Permute Layer * @param direction direction to permute */ - template - PermuteLayer(Args... args) : - LayerV1(args...), - direction(), - reverse_direction() {} + PermuteLayer() : Layer(), direction(), reverse_direction() {} /** * @brief Destructor of Permute Layer @@ -76,48 +73,42 @@ public: PermuteLayer &operator=(PermuteLayer &&rhs) = default; /** - * @copydoc Layer::forwarding(bool training) + * @copydoc Layer::finalize(InitLayerContext &context) */ - void forwarding(bool training = true) override; + void finalize(InitLayerContext &context) override; /** - * @copydoc Layer::calcDerivative() + * @copydoc Layer::forwarding(RunLayerContext &context, bool training) */ - void calcDerivative() override; + void forwarding(RunLayerContext &context, bool training) override; /** - * @brief copy layer - * @param[in] l layer to copy + * @copydoc Layer::calcDerivative(RunLayerContext &context) */ - void copy(std::shared_ptr l) override; + void calcDerivative(RunLayerContext &context) override; /** - * @brief initialize layer - * @retval #ML_ERROR_NONE Successful. - * @retval #ML_ERROR_INVALID_PARAMETER invalid parameter. + * @copydoc Layer::exportTo(Exporter &exporter, ExportMethods method) */ - int initialize(Manager &manager) override; - - /** - * @copydoc Layer::export_to(Exporter &exporter, ExportMethods method) - */ - void export_to( - Exporter &exporter, - ExportMethods method = ExportMethods::METHOD_STRINGVECTOR) const override; + void exportTo(Exporter &exporter, const ExportMethods &method) const override; /** * @copydoc Layer::getType() */ const std::string getType() const override { return PermuteLayer::type; }; - inline static const std::string type = "permute"; - - using LayerV1::setProperty; + /** + * @copydoc Layer::supportBackwarding() + */ + bool supportBackwarding() const { return true; } /** - * @copydoc Layer::setProperty(std::vector values); + * @copydoc Layer::setProperty(const PropertyType type, const std::string + * &value) */ - int setProperty(std::vector values) override; + void setProperty(const std::vector &values) override; + + inline static const std::string type = "permute"; private: std::string diff --git a/test/unittest/layers/meson.build b/test/unittest/layers/meson.build index 0ec97e3..8775b50 100644 --- a/test/unittest/layers/meson.build +++ b/test/unittest/layers/meson.build @@ -44,6 +44,7 @@ test_target = [ 'unittest_layers_embedding.cpp', 'unittest_layers_concat.cpp', 'unittest_layers_tflite.cpp', + 'unittest_layers_permute.cpp', ] exe = executable( diff --git a/test/unittest/layers/unittest_layers_permute.cpp b/test/unittest/layers/unittest_layers_permute.cpp new file mode 100644 index 0000000..22e724e --- /dev/null +++ b/test/unittest/layers/unittest_layers_permute.cpp @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: Apache-2.0 +/** + * Copyright (C) 2021 Parichay Kapoor + * + * @file unittest_layers_permute.cpp + * @date 7 July 2021 + * @brief Permute Layer Test + * @see https://github.com/nnstreamer/nntrainer + * @author Parichay Kapoor + * @bug No known bugs except for NYI items + */ +#include + +#include + +#include +#include + +auto semantic_permute = LayerSemanticsParamType( + nntrainer::createLayer, + nntrainer::PermuteLayer::type, {"direction=3,2,1"}, 0, false); + +INSTANTIATE_TEST_CASE_P(Permute, LayerSemantics, + ::testing::Values(semantic_permute)); -- 2.7.4