From 4f70946b931cbdfb8aedf75d05ea9e8ed9a52a90 Mon Sep 17 00:00:00 2001 From: Yongseop Kim Date: Fri, 26 Jan 2024 15:19:15 +0900 Subject: [PATCH 1/4] [onert/train] Add PadLayer op to train backend This commit adds PadLayer op to train backend. ONE-DCO-1.0-Signed-off-by: Yongseop Kim --- .../onert/backend/train/ops/OperationUtils.h | 1 + runtime/onert/backend/train/ops/PadLayer.cc | 120 ++++++++++++++++++ runtime/onert/backend/train/ops/PadLayer.h | 68 ++++++++++ 3 files changed, 189 insertions(+) create mode 100644 runtime/onert/backend/train/ops/PadLayer.cc create mode 100644 runtime/onert/backend/train/ops/PadLayer.h diff --git a/runtime/onert/backend/train/ops/OperationUtils.h b/runtime/onert/backend/train/ops/OperationUtils.h index 186d5eb4d8b..f58fc53ce18 100644 --- a/runtime/onert/backend/train/ops/OperationUtils.h +++ b/runtime/onert/backend/train/ops/OperationUtils.h @@ -35,6 +35,7 @@ using cpu::ops::getShape; using cpu::ops::getNumberOfDimensions; using cpu::ops::getNumberOfElements; using cpu::ops::getSizeOfDimension; +using cpu::ops::ConstDataPtr; /** * @brief backpropagate acitvation diff --git a/runtime/onert/backend/train/ops/PadLayer.cc b/runtime/onert/backend/train/ops/PadLayer.cc new file mode 100644 index 00000000000..b1c2493bce5 --- /dev/null +++ b/runtime/onert/backend/train/ops/PadLayer.cc @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "PadLayer.h" + +#include + +namespace onert +{ +namespace backend +{ +namespace train +{ +namespace ops +{ + +PadLayer::PadLayer() + : _input(nullptr), _output(nullptr), _padData(), _padRank(), _constantValueData(), + _back_prop_input{nullptr}, _back_prop_output{nullptr} +{ + // DO NOTHING +} + +template void PadLayer::padImpl(const T *constant_value_data) +{ + nnfw::cker::Pad(_padData, _padRank, + getShape(_input), getBuffer(_input), + getShape(_output), getBuffer(_output), + constant_value_data); +} + +template void PadLayer::depad() +{ + nnfw::cker::train::Depad(_padData, _padRank, + getShape(_back_prop_output), getBuffer(_back_prop_output), + getShape(_back_prop_input), getBuffer(_back_prop_input)); +} + +void PadLayer::configure(const IPortableTensor *input, IPortableTensor *output, + const int32_t *padData, int32_t padRank, const void *constantValueData, + IPortableTensor *back_prop_input, const IPortableTensor *back_prop_output) +{ + _input = input; + _output = output; + memcpy(_padData, padData, sizeof(_padData)); + _padRank = padRank; + _constantValueData.v = constantValueData; + _back_prop_input = back_prop_input; + _back_prop_output = back_prop_output; +} + +void PadLayer::forward(bool) +{ + switch (_input->data_type()) + { + case OperandType::FLOAT32: + padImpl(_constantValueData.f); + break; + case OperandType::QUANT_UINT8_ASYMM: + if (_constantValueData.u8 == nullptr) + { + uint8_t pad_value = static_cast(_output->data_zero_point()); + padImpl(&pad_value); + } + else + { + padImpl(_constantValueData.u8); + } + break; + case OperandType::QUANT_INT8_ASYMM: + if (_constantValueData.i8 == nullptr) + { + int8_t pad_value = static_cast(_output->data_zero_point()); + padImpl(&pad_value); + } + else + { + padImpl(_constantValueData.i8); + } + break; + default: + throw std::runtime_error{"Pad: unsupported data type"}; + } +} + +void PadLayer::backward() +{ + switch (_back_prop_output->data_type()) + { + case OperandType::FLOAT32: + depad(); + break; + case OperandType::QUANT_UINT8_ASYMM: + depad(); + break; + case OperandType::QUANT_INT8_ASYMM: + depad(); + break; + default: + throw std::runtime_error{"Pad: unsupported data type"}; + } +} + +} // namespace ops +} // namespace train +} // namespace backend +} // namespace onert diff --git a/runtime/onert/backend/train/ops/PadLayer.h b/runtime/onert/backend/train/ops/PadLayer.h new file mode 100644 index 00000000000..bc731d716b1 --- /dev/null +++ b/runtime/onert/backend/train/ops/PadLayer.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __ONERT_BACKEND_TRAIN_OPS_PADLAYER_H__ +#define __ONERT_BACKEND_TRAIN_OPS_PADLAYER_H__ + +#include +#include "OperationUtils.h" + +#include + +namespace onert +{ +namespace backend +{ +namespace train +{ +namespace ops +{ + +// Note, this is pad with mode=`CONSTANT`: it doesn't support `REFLECT` and +// `SYMMETRIC` +class PadLayer : public ::onert::exec::train::ITrainableFunction +{ +public: + PadLayer(); + +public: + template void padImpl(const T *constant_value_data); + template void depad(); + + void configure(const IPortableTensor *input, IPortableTensor *output, + const int32_t *padData, int32_t padRank, const void *constantValueData, + IPortableTensor *back_prop_input, const IPortableTensor *back_prop_output); + void forward(bool training) override; + void backward() override; + +private: + const IPortableTensor *_input; + IPortableTensor *_output; + + int32_t _padData[8]; + int32_t _padRank; + ConstDataPtr _constantValueData; + + IPortableTensor *_back_prop_input; + const IPortableTensor *_back_prop_output; +}; + +} // namespace ops +} // namespace train +} // namespace backend +} // namespace onert + +#endif // __ONERT_BACKEND_TRAIN_OPS_PADLAYER_H__ From 2a2d86d5494974ae1ce8a67ce0d54142b06c1e14 Mon Sep 17 00:00:00 2001 From: Yongseop Kim Date: Fri, 26 Jan 2024 16:19:02 +0900 Subject: [PATCH 2/4] Patch for format --- runtime/onert/backend/train/ops/PadLayer.cc | 12 +++++------- runtime/onert/backend/train/ops/PadLayer.h | 6 +++--- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/runtime/onert/backend/train/ops/PadLayer.cc b/runtime/onert/backend/train/ops/PadLayer.cc index b1c2493bce5..0b86a08a345 100644 --- a/runtime/onert/backend/train/ops/PadLayer.cc +++ b/runtime/onert/backend/train/ops/PadLayer.cc @@ -36,17 +36,15 @@ PadLayer::PadLayer() template void PadLayer::padImpl(const T *constant_value_data) { - nnfw::cker::Pad(_padData, _padRank, - getShape(_input), getBuffer(_input), - getShape(_output), getBuffer(_output), - constant_value_data); + nnfw::cker::Pad(_padData, _padRank, getShape(_input), getBuffer(_input), getShape(_output), + getBuffer(_output), constant_value_data); } template void PadLayer::depad() { - nnfw::cker::train::Depad(_padData, _padRank, - getShape(_back_prop_output), getBuffer(_back_prop_output), - getShape(_back_prop_input), getBuffer(_back_prop_input)); + nnfw::cker::train::Depad(_padData, _padRank, getShape(_back_prop_output), + getBuffer(_back_prop_output), getShape(_back_prop_input), + getBuffer(_back_prop_input)); } void PadLayer::configure(const IPortableTensor *input, IPortableTensor *output, diff --git a/runtime/onert/backend/train/ops/PadLayer.h b/runtime/onert/backend/train/ops/PadLayer.h index bc731d716b1..90fc592d6e1 100644 --- a/runtime/onert/backend/train/ops/PadLayer.h +++ b/runtime/onert/backend/train/ops/PadLayer.h @@ -42,9 +42,9 @@ class PadLayer : public ::onert::exec::train::ITrainableFunction template void padImpl(const T *constant_value_data); template void depad(); - void configure(const IPortableTensor *input, IPortableTensor *output, - const int32_t *padData, int32_t padRank, const void *constantValueData, - IPortableTensor *back_prop_input, const IPortableTensor *back_prop_output); + void configure(const IPortableTensor *input, IPortableTensor *output, const int32_t *padData, + int32_t padRank, const void *constantValueData, IPortableTensor *back_prop_input, + const IPortableTensor *back_prop_output); void forward(bool training) override; void backward() override; From dab9c599ca4f33bf29b5825fe321d0af296615fc Mon Sep 17 00:00:00 2001 From: Yongseop Kim Date: Mon, 29 Jan 2024 12:43:10 +0900 Subject: [PATCH 3/4] Apply cpu::ops::PadLayer to PadLayer in train --- runtime/onert/backend/cpu/ops/PadLayer.h | 2 +- runtime/onert/backend/train/ops/PadLayer.cc | 50 ++------------------- runtime/onert/backend/train/ops/PadLayer.h | 11 +---- 3 files changed, 6 insertions(+), 57 deletions(-) diff --git a/runtime/onert/backend/cpu/ops/PadLayer.h b/runtime/onert/backend/cpu/ops/PadLayer.h index efd73d5e5d2..24f18500646 100644 --- a/runtime/onert/backend/cpu/ops/PadLayer.h +++ b/runtime/onert/backend/cpu/ops/PadLayer.h @@ -46,7 +46,7 @@ class PadLayer : public ::onert::exec::IFunction void run() override; -private: +protected: const IPortableTensor *_input; IPortableTensor *_output; diff --git a/runtime/onert/backend/train/ops/PadLayer.cc b/runtime/onert/backend/train/ops/PadLayer.cc index 0b86a08a345..4add0c5a357 100644 --- a/runtime/onert/backend/train/ops/PadLayer.cc +++ b/runtime/onert/backend/train/ops/PadLayer.cc @@ -27,19 +27,11 @@ namespace train namespace ops { -PadLayer::PadLayer() - : _input(nullptr), _output(nullptr), _padData(), _padRank(), _constantValueData(), - _back_prop_input{nullptr}, _back_prop_output{nullptr} +PadLayer::PadLayer() : cpu::ops::PadLayer(), _back_prop_input{nullptr}, _back_prop_output{nullptr} { // DO NOTHING } -template void PadLayer::padImpl(const T *constant_value_data) -{ - nnfw::cker::Pad(_padData, _padRank, getShape(_input), getBuffer(_input), getShape(_output), - getBuffer(_output), constant_value_data); -} - template void PadLayer::depad() { nnfw::cker::train::Depad(_padData, _padRank, getShape(_back_prop_output), @@ -51,48 +43,12 @@ void PadLayer::configure(const IPortableTensor *input, IPortableTensor *output, const int32_t *padData, int32_t padRank, const void *constantValueData, IPortableTensor *back_prop_input, const IPortableTensor *back_prop_output) { - _input = input; - _output = output; - memcpy(_padData, padData, sizeof(_padData)); - _padRank = padRank; - _constantValueData.v = constantValueData; + cpu::ops::PadLayer::configure(input, output, padData, padRank, constantValueData); _back_prop_input = back_prop_input; _back_prop_output = back_prop_output; } -void PadLayer::forward(bool) -{ - switch (_input->data_type()) - { - case OperandType::FLOAT32: - padImpl(_constantValueData.f); - break; - case OperandType::QUANT_UINT8_ASYMM: - if (_constantValueData.u8 == nullptr) - { - uint8_t pad_value = static_cast(_output->data_zero_point()); - padImpl(&pad_value); - } - else - { - padImpl(_constantValueData.u8); - } - break; - case OperandType::QUANT_INT8_ASYMM: - if (_constantValueData.i8 == nullptr) - { - int8_t pad_value = static_cast(_output->data_zero_point()); - padImpl(&pad_value); - } - else - { - padImpl(_constantValueData.i8); - } - break; - default: - throw std::runtime_error{"Pad: unsupported data type"}; - } -} +void PadLayer::forward(bool) { cpu::ops::PadLayer::run(); } void PadLayer::backward() { diff --git a/runtime/onert/backend/train/ops/PadLayer.h b/runtime/onert/backend/train/ops/PadLayer.h index 90fc592d6e1..326ae6135be 100644 --- a/runtime/onert/backend/train/ops/PadLayer.h +++ b/runtime/onert/backend/train/ops/PadLayer.h @@ -17,6 +17,7 @@ #ifndef __ONERT_BACKEND_TRAIN_OPS_PADLAYER_H__ #define __ONERT_BACKEND_TRAIN_OPS_PADLAYER_H__ +#include #include #include "OperationUtils.h" @@ -33,13 +34,12 @@ namespace ops // Note, this is pad with mode=`CONSTANT`: it doesn't support `REFLECT` and // `SYMMETRIC` -class PadLayer : public ::onert::exec::train::ITrainableFunction +class PadLayer : public ::onert::exec::train::ITrainableFunction, public cpu::ops::PadLayer { public: PadLayer(); public: - template void padImpl(const T *constant_value_data); template void depad(); void configure(const IPortableTensor *input, IPortableTensor *output, const int32_t *padData, @@ -49,13 +49,6 @@ class PadLayer : public ::onert::exec::train::ITrainableFunction void backward() override; private: - const IPortableTensor *_input; - IPortableTensor *_output; - - int32_t _padData[8]; - int32_t _padRank; - ConstDataPtr _constantValueData; - IPortableTensor *_back_prop_input; const IPortableTensor *_back_prop_output; }; From 473902513eb17635014cb4f00ec7389fd7147049 Mon Sep 17 00:00:00 2001 From: Yongseop Kim Date: Mon, 29 Jan 2024 14:01:53 +0900 Subject: [PATCH 4/4] Fix unnecessary line --- runtime/onert/backend/train/ops/OperationUtils.h | 1 - 1 file changed, 1 deletion(-) diff --git a/runtime/onert/backend/train/ops/OperationUtils.h b/runtime/onert/backend/train/ops/OperationUtils.h index f58fc53ce18..186d5eb4d8b 100644 --- a/runtime/onert/backend/train/ops/OperationUtils.h +++ b/runtime/onert/backend/train/ops/OperationUtils.h @@ -35,7 +35,6 @@ using cpu::ops::getShape; using cpu::ops::getNumberOfDimensions; using cpu::ops::getNumberOfElements; using cpu::ops::getSizeOfDimension; -using cpu::ops::ConstDataPtr; /** * @brief backpropagate acitvation