ONE - On-device Neural Engine
Loading...
Searching...
No Matches
ElementwiseActivationLayer.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 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
23
24namespace onert
25{
26namespace backend
27{
28namespace train
29{
30namespace ops
31{
32
37
39 IPortableTensor *back_prop_input,
40 const IPortableTensor *back_prop_output,
41 float alpha, float beta,
43{
44 assert(input != nullptr);
45 assert(back_prop_input != nullptr);
46 assert(back_prop_output != nullptr);
47
48 _back_prop_input = back_prop_input;
49 _back_prop_output = back_prop_output;
50
51 _op_type = op_type;
52
53 switch (op_type)
54 {
56 if (input->data_type() == OperandType::FLOAT32)
57 {
58 if ((alpha == std::numeric_limits<float>::infinity() || alpha == 6.0f) && beta == 0.f)
59 {
60 auto relu_cker = [&alpha]() {
61 if (alpha == std::numeric_limits<float>::infinity())
63 else if (alpha == 6.0f)
65 else
66 throw std::runtime_error{"no supported relu kernel"};
67 }();
68
69 _backward_kernel = [relu_cker](const IPortableTensor *output,
70 const IPortableTensor *incoming,
71 IPortableTensor *outgoing) {
72 relu_cker(getShape(output), getBuffer<float>(output), getShape(incoming),
73 getBuffer<float>(incoming), getShape(outgoing), getBuffer<float>(outgoing));
74 };
75 }
76 else
77 {
78 throw std::runtime_error(
79 "train ElementwiseActivationLayer : Unsupported ReLU activation type");
80 }
81 }
82 else
83 {
84 throw std::runtime_error("train ElementwiseActivationLayer: Unsupported datatype");
85 }
86 break;
87 default:
88 throw std::runtime_error("train ElementwiseActivationLayer: Unsupported activation type yet");
89 }
90}
91
93
95{
96 _backward_kernel(_output, _back_prop_output, _back_prop_input);
97}
98
99} // namespace ops
100} // namespace train
101} // namespace backend
102} // namespace onert
A tensor class that is portable for other backends.
void configureBackward(const IPortableTensor *input, IPortableTensor *back_prop_input, const IPortableTensor *back_prop_output, float alpha, float beta, ElementwiseActivationType op_type)
void ReLUGrad(const Shape &output_shape, const float *output_data, const Shape &incoming_shape, const float *incoming_data, const Shape &grad_shape, float *grad_data)
Definition ReLU.h:32
void ReLU6Grad(const Shape &output_shape, const float *output_data, const Shape &incoming_shape, const float *incoming_data, const Shape &grad_shape, float *grad_data)
Definition ReLU6.h:31
nnfw::cker::Shape getShape(const IPortableTensor *tensor)
Get shape of tensor.