ONE - On-device Neural Engine
Loading...
Searching...
No Matches
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
27namespace onert
28{
29namespace backend
30{
31namespace train
32{
33namespace ops
34{
35
37 : cpu::ops::BinaryArithmeticLayer(), _back_prop_lhs{nullptr}, _back_prop_rhs{nullptr},
38 _back_prop_output{nullptr}, _arithmetic_type{ArithmeticType::kAdd},
39 _activation{ir::Activation::NONE}, _act_back_prop_output{nullptr}
40{
41 // DO NOTHING
42}
43
45 IPortableTensor *back_prop_rhs,
46 const IPortableTensor *back_prop_output,
47 const ir::Activation activation,
48 const ArithmeticType arithmetic_type)
49{
50 _back_prop_lhs = back_prop_lhs;
51 _back_prop_rhs = back_prop_rhs;
52 _back_prop_output = back_prop_output;
53 _arithmetic_type = arithmetic_type;
54 _activation = activation;
55
56 if (activation != ir::Activation::NONE)
57 {
58 _act_back_prop_output = std::make_unique<Tensor>(_output->get_info());
59 _act_back_prop_output->setBuffer(std::make_shared<basic::Allocator>(_output->total_size()));
60 }
61}
62
64
66{
67 // Calculate gradient for activation
68 if (_back_prop_output->data_type() != OperandType::FLOAT32)
69 throw std::runtime_error{"Unsupported Data Type"};
70
71 const IPortableTensor *backprop_act;
72 try
73 {
74 backprop_act =
75 backpropActivation(_activation, _output, _back_prop_output, _act_back_prop_output.get());
76 }
77 catch (const std::exception &e)
78 {
79 throw std::runtime_error{"train BinaryArithmeticLayer: " + std::string(e.what())};
80 }
81 assert(backprop_act != nullptr);
82
84 getShape(_lhs), getBuffer<float>(_lhs), getShape(_rhs), getBuffer<float>(_rhs),
85 getShape(backprop_act), getBuffer<float>(backprop_act), getShape(_back_prop_lhs),
86 getBuffer<float>(_back_prop_lhs), getShape(_back_prop_rhs), getBuffer<float>(_back_prop_rhs),
87 static_cast<nnfw::cker::train::ArithmeticType>(_arithmetic_type));
88}
89
90} // namespace ops
91} // namespace train
92} // namespace backend
93} // namespace onert
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.