Compute Library
 21.08
ActivationLayer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "ActivationLayer.h"
25 
26 #include "arm_compute/core/Types.h"
28 
29 namespace arm_compute
30 {
31 namespace test
32 {
33 namespace validation
34 {
35 namespace reference
36 {
37 template <typename T>
39 {
40  ARM_COMPUTE_UNUSED(oq_info);
41 
42  // Create reference
43  SimpleTensor<T> dst{ src.shape(), src.data_type(), 1 };
44 
45  // Compute reference
46  const T a(info.a());
47  const T b(info.b());
48 #if defined(_OPENMP)
49  #pragma omp parallel for
50 #endif /* _OPENMP */
51  for(int i = 0; i < src.num_elements(); ++i)
52  {
53  dst[i] = activate_float<T>(src[i], a, b, info.activation());
54  }
55 
56  return dst;
57 }
58 
59 template <>
61 {
62  const QuantizationInfo dst_qinfo = oq_info.empty() ? src.quantization_info() : oq_info;
63 
65  SimpleTensor<float> dst_tmp = activation_layer<float>(src_tmp, info);
66  SimpleTensor<uint8_t> dst = convert_to_asymmetric<uint8_t>(dst_tmp, dst_qinfo);
67  return dst;
68 }
69 
70 template <>
72 {
73  const QuantizationInfo dst_qinfo = oq_info.empty() ? src.quantization_info() : oq_info;
74 
76  SimpleTensor<float> dst_tmp = activation_layer<float>(src_tmp, info);
77  SimpleTensor<int8_t> dst = convert_to_asymmetric<int8_t>(dst_tmp, dst_qinfo);
78  return dst;
79 }
80 
81 template <>
83 {
84  const QuantizationInfo dst_qinfo = oq_info.empty() ? src.quantization_info() : oq_info;
85 
87  SimpleTensor<float> dst_tmp = activation_layer<float>(src_tmp, info);
88  SimpleTensor<int16_t> dst = convert_to_symmetric<int16_t>(dst_tmp, dst_qinfo);
89  return dst;
90 }
94 } // namespace reference
95 } // namespace validation
96 } // namespace test
97 } // namespace arm_compute
SimpleTensor< T > activation_layer(const SimpleTensor< T > &src, ActivationLayerInfo info, const QuantizationInfo &oq_info)
SimpleTensor< uint8_t > activation_layer< uint8_t >(const SimpleTensor< uint8_t > &src, ActivationLayerInfo info, const QuantizationInfo &oq_info)
SimpleTensor< float > b
Definition: DFT.cpp:157
float a() const
Get the alpha value.
Definition: Types.h:1515
SimpleTensor< int16_t > activation_layer< int16_t >(const SimpleTensor< int16_t > &src, ActivationLayerInfo info, const QuantizationInfo &oq_info)
DataType data_type() const override
Data type of the tensor.
Definition: SimpleTensor.h:357
SimpleTensor< float > convert_from_asymmetric(const SimpleTensor< uint8_t > &src)
Definition: Helpers.cpp:36
TensorShape shape() const override
Shape of the tensor.
Definition: SimpleTensor.h:320
Activation Layer Information class.
Definition: Types.h:1475
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2021 Arm Limited.
Quantization information.
SimpleTensor< int8_t > activation_layer< int8_t >(const SimpleTensor< int8_t > &src, ActivationLayerInfo info, const QuantizationInfo &oq_info)
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:58
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
bool empty() const
Indicates whether this QuantizationInfo has valid settings or not.
int num_elements() const override
Number of elements of the tensor.
Definition: SimpleTensor.h:406
ActivationFunction activation() const
Get the type of activation function.
Definition: Types.h:1510
float b() const
Get the beta value.
Definition: Types.h:1520
QuantizationInfo quantization_info() const override
Quantization info in case of asymmetric quantized type.
Definition: SimpleTensor.h:332
SimpleTensor< float > convert_from_symmetric(const SimpleTensor< int16_t > &src)
Definition: Helpers.cpp:147