blob: 558f9d24fcdeb9cdbb5c46eb04ced690fb134b6f [file] [log] [blame]
Michalis Spyroue9362622018-11-23 17:41:37 +00001/*
Ramy Elgammal14d7b532023-01-30 04:56:47 +00002 * Copyright (c) 2018-2020, 2023 Arm Limited.
Michalis Spyroue9362622018-11-23 17:41:37 +00003 *
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 */
Jakub Sujakee301b32021-06-04 09:46:08 +010024#include "ElementwiseUnary.h"
Ramy Elgammal14d7b532023-01-30 04:56:47 +000025#include "tests/validation/Helpers.h"
26#include "utils/TypePrinter.h"
Michalis Spyroue9362622018-11-23 17:41:37 +000027namespace arm_compute
28{
29namespace test
30{
31namespace validation
32{
33namespace reference
34{
35template <typename T>
Ramy Elgammal14d7b532023-01-30 04:56:47 +000036SimpleTensor<T> elementwise_unary(const SimpleTensor<T> &src, SimpleTensor<T> &dst, ElementWiseUnary op)
Michalis Spyroue9362622018-11-23 17:41:37 +000037{
Michalis Spyroue9362622018-11-23 17:41:37 +000038 for(int i = 0; i < src.num_elements(); ++i)
39 {
40 switch(op)
41 {
42 case ElementWiseUnary::RSQRT:
43 dst[i] = 1.f / std::sqrt(src[i]);
44 break;
45 case ElementWiseUnary::EXP:
46 dst[i] = std::exp(src[i]);
47 break;
Usama Ariff6e475c2019-05-10 12:06:28 +010048 case ElementWiseUnary::NEG:
49 dst[i] = -src[i];
50 break;
Usama Arifc255aa72019-05-13 16:26:29 +010051 case ElementWiseUnary::LOG:
52 dst[i] = std::log(src[i]);
53 break;
Manuel Bottini6ac59922019-05-15 14:06:02 +010054 case ElementWiseUnary::ABS:
55 dst[i] = std::abs(src[i]);
56 break;
Michalis Spyrou0af44182019-05-17 14:04:47 +010057 case ElementWiseUnary::SIN:
58 dst[i] = std::sin(src[i]);
59 break;
Usama Arif0a5a57a2019-05-23 14:20:33 +010060 case ElementWiseUnary::ROUND:
Michalis Spyrou748a7c82019-10-07 13:00:44 +010061 dst[i] = arm_compute::support::cpp11::nearbyint(src[i]);
Usama Arif0a5a57a2019-05-23 14:20:33 +010062 break;
Michalis Spyroue9362622018-11-23 17:41:37 +000063 default:
64 ARM_COMPUTE_ERROR("Not implemented");
65 }
66 }
Ramy Elgammal14d7b532023-01-30 04:56:47 +000067 return dst;
68}
69template <>
70SimpleTensor<int8_t> elementwise_unary(const SimpleTensor<int8_t> &src, SimpleTensor<int8_t> &dst, ElementWiseUnary op)
71{
72 if(dst.data_type() == DataType::QASYMM8_SIGNED)
73 {
74 SimpleTensor<float> src_tmp = convert_from_asymmetric(src);
75 SimpleTensor<float> dst_tmp(src.shape(), DataType::F32);
76 for(int i = 0; i < src.num_elements(); ++i)
77 {
78 switch(op)
79 {
80 case ElementWiseUnary::RSQRT:
81 if(src_tmp[i] != 0)
82 {
83 dst_tmp[i] = 1.f / std::sqrt(src_tmp[i]);
84 }
85 else
86 {
87 // rsqrt(0) give 'inf' so set to the maximum in int8: 127
88 dst_tmp[i] = (127.0f - dst.quantization_info().uniform().offset) * dst.quantization_info().uniform().scale ;
89 }
90 break;
Viet-Hoa Dofd472f02023-03-15 14:05:06 +000091
92 case ElementWiseUnary::LOG:
93 if(src_tmp[i] != 0)
94 {
95 dst_tmp[i] = std::log(src_tmp[i]);
96 }
97 else
98 {
99 dst_tmp[i] = (-128.0f - dst.quantization_info().uniform().offset) * dst.quantization_info().uniform().scale ;
100 }
101 break;
102
Ramy Elgammal14d7b532023-01-30 04:56:47 +0000103 default:
Viet-Hoa Dofd472f02023-03-15 14:05:06 +0000104 elementwise_unary(src_tmp, dst_tmp, op);
105 break;
Ramy Elgammal14d7b532023-01-30 04:56:47 +0000106 }
107 }
108 dst = convert_to_asymmetric<int8_t>(dst_tmp, dst.quantization_info());
109 }
110 else
111 {
112 ARM_COMPUTE_ERROR("Not implemented");
113 }
114 return dst;
115}
116template <>
117SimpleTensor<uint8_t> elementwise_unary(const SimpleTensor<uint8_t> &src, SimpleTensor<uint8_t> &dst, ElementWiseUnary op)
118{
119 if(dst.data_type() == DataType::QASYMM8)
120 {
121 SimpleTensor<float> src_tmp = convert_from_asymmetric(src);
122 SimpleTensor<float> dst_tmp(src.shape(), DataType::F32);
123 for(int i = 0; i < src.num_elements(); ++i)
124 {
125 switch(op)
126 {
127 case ElementWiseUnary::RSQRT:
128 if(src_tmp[i] != 0)
129 {
130 dst_tmp[i] = 1.f / std::sqrt(src_tmp[i]);
131 }
132 else
133 {
134 // rsqrt(0) give 'inf' so set to the maximum in uint8: 255
135 dst_tmp[i] = (255.0f - dst.quantization_info().uniform().offset)* dst.quantization_info().uniform().scale;
136 }
137 break;
Viet-Hoa Dofd472f02023-03-15 14:05:06 +0000138
139 case ElementWiseUnary::LOG:
140 if(src_tmp[i] != 0)
141 {
142 dst_tmp[i] = std::log(src_tmp[i]);
143 }
144 else
145 {
146 dst_tmp[i] = -dst.quantization_info().uniform().offset * dst.quantization_info().uniform().scale;
147 }
148 break;
149
Ramy Elgammal14d7b532023-01-30 04:56:47 +0000150 default:
Viet-Hoa Dofd472f02023-03-15 14:05:06 +0000151 elementwise_unary(src_tmp, dst_tmp, op);
152 break;
Ramy Elgammal14d7b532023-01-30 04:56:47 +0000153 }
154 }
155 dst = convert_to_asymmetric<uint8_t>(dst_tmp, dst.quantization_info());
156 }
157 else
158 {
159 ARM_COMPUTE_ERROR("Not implemented");
160 }
Michalis Spyroue9362622018-11-23 17:41:37 +0000161 return dst;
162}
163
Ramy Elgammal14d7b532023-01-30 04:56:47 +0000164template SimpleTensor<float> elementwise_unary(const SimpleTensor<float> &src, SimpleTensor<float> &dst, ElementWiseUnary op);
165template SimpleTensor<half> elementwise_unary(const SimpleTensor<half> &src, SimpleTensor<half> &dst, ElementWiseUnary op);
166template SimpleTensor<int32_t> elementwise_unary(const SimpleTensor<int32_t> &src, SimpleTensor<int32_t> &dst, ElementWiseUnary op);
167
Michalis Spyroue9362622018-11-23 17:41:37 +0000168} // namespace reference
169} // namespace validation
170} // namespace test
171} // namespace arm_compute