blob: d5218d772ddbbb34e1aee61a3e97879198ac3634 [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;
91 default:
92 ARM_COMPUTE_ERROR("Not implemented");
93 }
94 }
95 dst = convert_to_asymmetric<int8_t>(dst_tmp, dst.quantization_info());
96 }
97 else
98 {
99 ARM_COMPUTE_ERROR("Not implemented");
100 }
101 return dst;
102}
103template <>
104SimpleTensor<uint8_t> elementwise_unary(const SimpleTensor<uint8_t> &src, SimpleTensor<uint8_t> &dst, ElementWiseUnary op)
105{
106 if(dst.data_type() == DataType::QASYMM8)
107 {
108 SimpleTensor<float> src_tmp = convert_from_asymmetric(src);
109 SimpleTensor<float> dst_tmp(src.shape(), DataType::F32);
110 for(int i = 0; i < src.num_elements(); ++i)
111 {
112 switch(op)
113 {
114 case ElementWiseUnary::RSQRT:
115 if(src_tmp[i] != 0)
116 {
117 dst_tmp[i] = 1.f / std::sqrt(src_tmp[i]);
118 }
119 else
120 {
121 // rsqrt(0) give 'inf' so set to the maximum in uint8: 255
122 dst_tmp[i] = (255.0f - dst.quantization_info().uniform().offset)* dst.quantization_info().uniform().scale;
123 }
124 break;
125 default:
126 ARM_COMPUTE_ERROR("Not implemented");
127 }
128 }
129 dst = convert_to_asymmetric<uint8_t>(dst_tmp, dst.quantization_info());
130 }
131 else
132 {
133 ARM_COMPUTE_ERROR("Not implemented");
134 }
Michalis Spyroue9362622018-11-23 17:41:37 +0000135 return dst;
136}
137
Ramy Elgammal14d7b532023-01-30 04:56:47 +0000138template SimpleTensor<float> elementwise_unary(const SimpleTensor<float> &src, SimpleTensor<float> &dst, ElementWiseUnary op);
139template SimpleTensor<half> elementwise_unary(const SimpleTensor<half> &src, SimpleTensor<half> &dst, ElementWiseUnary op);
140template SimpleTensor<int32_t> elementwise_unary(const SimpleTensor<int32_t> &src, SimpleTensor<int32_t> &dst, ElementWiseUnary op);
141
Michalis Spyroue9362622018-11-23 17:41:37 +0000142} // namespace reference
143} // namespace validation
144} // namespace test
145} // namespace arm_compute