ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
BinaryArithmeticLayer.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
18
19#include "OperationUtils.h"
20
21#include <cker/Shape.h>
26
28{
29
31 : cpu::ops::BinaryArithmeticLayer(), _back_prop_lhs{nullptr}, _back_prop_rhs{nullptr},
32 _back_prop_output{nullptr}, _arithmetic_type{ArithmeticType::kAdd},
33 _activation{ir::Activation::NONE}, _act_back_prop_output{nullptr}
34{
35 // DO NOTHING
36}
37
39 IPortableTensor *back_prop_rhs,
40 const IPortableTensor *back_prop_output,
41 const ir::Activation activation,
42 const ArithmeticType arithmetic_type)
43{
44 _back_prop_lhs = back_prop_lhs;
45 _back_prop_rhs = back_prop_rhs;
46 _back_prop_output = back_prop_output;
47 _arithmetic_type = arithmetic_type;
48 _activation = activation;
49
50 if (activation != ir::Activation::NONE)
51 {
52 _act_back_prop_output = std::make_unique<Tensor>(_output->get_info());
53 _act_back_prop_output->setBuffer(std::make_shared<basic::Allocator>(_output->total_size()));
54 }
55}
56
58
60{
61 // Calculate gradient for activation
62 if (_back_prop_output->data_type() != OperandType::FLOAT32)
63 throw std::runtime_error{"Unsupported Data Type"};
64
65 const IPortableTensor *backprop_act;
66 try
67 {
68 backprop_act =
69 backpropActivation(_activation, _output, _back_prop_output, _act_back_prop_output.get());
70 }
71 catch (const std::exception &e)
72 {
73 throw std::runtime_error{"train BinaryArithmeticLayer: " + std::string(e.what())};
74 }
75 assert(backprop_act != nullptr);
76
78 getShape(_lhs), getBuffer<float>(_lhs), getShape(_rhs), getBuffer<float>(_rhs),
79 getShape(backprop_act), getBuffer<float>(backprop_act), getShape(_back_prop_lhs),
80 getBuffer<float>(_back_prop_lhs), getShape(_back_prop_rhs), getBuffer<float>(_back_prop_rhs),
81 static_cast<nnfw::cker::train::ArithmeticType>(_arithmetic_type));
82}
83
84} // namespace onert::backend::train::ops
A tensor class that is portable for other backends.
size_t total_size() const override final
const ir::OperandInfo & get_info() const
ir::DataType data_type() const override final
void configureBackward(IPortableTensor *back_prop_lhs, IPortableTensor *back_prop_rhs, const IPortableTensor *back_prop_output, const ir::Activation activation, const ArithmeticType arithmetic_type)
void BinaryArithmeticGrad(const Shape &lhs_shape, const T *lhs_data, const Shape &rhs_shape, const T *rhs_data, const Shape &incoming_shape, const T *incoming_data, const Shape &lhs_grad_shape, T *lhs_grad_data, const Shape &rhs_grad_shape, T *rhs_grad_data, ArithmeticType arithmetic_type)
const IPortableTensor * backpropActivation(const ir::Activation &activation, const IPortableTensor *output, const IPortableTensor *input_backprop, IPortableTensor *output_backprop)
backpropagate acitvation
nnfw::cker::Shape getShape(const IPortableTensor *tensor)
Get shape of tensor.