ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
25{
26
31
33 IPortableTensor *back_prop_input,
34 const IPortableTensor *back_prop_output,
35 float alpha, float beta,
37{
38 assert(input != nullptr);
39 assert(back_prop_input != nullptr);
40 assert(back_prop_output != nullptr);
41
42 _back_prop_input = back_prop_input;
43 _back_prop_output = back_prop_output;
44
45 _op_type = op_type;
46
47 switch (op_type)
48 {
50 if (input->data_type() == OperandType::FLOAT32)
51 {
52 if ((alpha == std::numeric_limits<float>::infinity() || alpha == 6.0f) && beta == 0.f)
53 {
54 auto relu_cker = [&alpha]() {
55 if (alpha == std::numeric_limits<float>::infinity())
57 else if (alpha == 6.0f)
59 else
60 throw std::runtime_error{"no supported relu kernel"};
61 }();
62
63 _backward_kernel = [relu_cker](const IPortableTensor *output,
64 const IPortableTensor *incoming,
65 IPortableTensor *outgoing) {
66 relu_cker(getShape(output), getBuffer<float>(output), getShape(incoming),
67 getBuffer<float>(incoming), getShape(outgoing), getBuffer<float>(outgoing));
68 };
69 }
70 else
71 {
72 throw std::runtime_error(
73 "train ElementwiseActivationLayer : Unsupported ReLU activation type");
74 }
75 }
76 else
77 {
78 throw std::runtime_error("train ElementwiseActivationLayer: Unsupported datatype");
79 }
80 break;
81 default:
82 throw std::runtime_error("train ElementwiseActivationLayer: Unsupported activation type yet");
83 }
84}
85
87
89{
90 _backward_kernel(_output, _back_prop_output, _back_prop_input);
91}
92
93} // namespace onert::backend::train::ops
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.