blob: 4345d8a13f0adae7b926547dfd62c51894703ab9 [file] [log] [blame]
John Richardsondd715f22017-09-18 16:10:48 +01001/*
Matthew Bentham945b8da2023-07-12 11:54:59 +00002 * Copyright (c) 2017-2021, 2023 Arm Limited.
John Richardsondd715f22017-09-18 16:10:48 +01003 *
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#ifndef ARM_COMPUTE_TEST_PIXEL_WISE_MULTIPLICATION_FIXTURE
25#define ARM_COMPUTE_TEST_PIXEL_WISE_MULTIPLICATION_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29#include "tests/AssetsLibrary.h"
30#include "tests/Globals.h"
31#include "tests/IAccessor.h"
32#include "tests/framework/Asserts.h"
33#include "tests/framework/Fixture.h"
Giorgio Arena8b2a7d32020-02-11 17:21:31 +000034#include "tests/validation/reference/ActivationLayer.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000035#include "tests/validation/reference/PixelWiseMultiplication.h"
John Richardsondd715f22017-09-18 16:10:48 +010036
37namespace arm_compute
38{
39namespace test
40{
41namespace validation
42{
Michele Di Giorgio9428a182020-03-30 14:10:20 +010043template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2, typename T3 = T2>
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +010044class PixelWiseMultiplicationGenericValidationFixture : public framework::Fixture
John Richardsondd715f22017-09-18 16:10:48 +010045{
46public:
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +000047 void setup(const TensorShape &shape0,
48 const TensorShape &shape1,
Giorgio Arena8b2a7d32020-02-11 17:21:31 +000049 DataType dt_in1,
50 DataType dt_in2,
51 DataType dt_out,
52 float scale,
53 ConvertPolicy convert_policy,
54 RoundingPolicy rounding_policy,
55 QuantizationInfo qinfo0,
56 QuantizationInfo qinfo1,
57 QuantizationInfo qinfo_out,
Sang-Hoon Park95a6f722020-06-19 15:31:29 +010058 ActivationLayerInfo act_info,
59 bool is_inplace)
John Richardsondd715f22017-09-18 16:10:48 +010060 {
Sang-Hoon Park95a6f722020-06-19 15:31:29 +010061 _is_inplace = is_inplace;
62 _target = compute_target(shape0, shape1, dt_in1, dt_in2, dt_out, scale, convert_policy, rounding_policy, qinfo0, qinfo1, qinfo_out, act_info);
63 _reference = compute_reference(shape0, shape1, dt_in1, dt_in2, dt_out, scale, convert_policy, rounding_policy, qinfo0, qinfo1, qinfo_out, act_info);
John Richardsondd715f22017-09-18 16:10:48 +010064 }
65
66protected:
67 template <typename U>
68 void fill(U &&tensor, unsigned int seed_offset)
69 {
70 library->fill_tensor_uniform(tensor, seed_offset);
71 }
72
Michele Di Giorgio9428a182020-03-30 14:10:20 +010073 TensorType compute_target(const TensorShape &shape0, const TensorShape &shape1, DataType dt_in1, DataType dt_in2, DataType dt_out,
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +010074 float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy,
Giorgio Arena8b2a7d32020-02-11 17:21:31 +000075 QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out, ActivationLayerInfo act_info)
John Richardsondd715f22017-09-18 16:10:48 +010076 {
77 // Create tensors
Sheri Zhanga387e272021-06-29 17:34:06 +010078 const TensorShape out_shape = TensorShape::broadcast_shape(shape0, shape1);
79 TensorType src1 = create_tensor<TensorType>(shape0, dt_in1, 1, qinfo0);
80 TensorType src2 = create_tensor<TensorType>(shape1, dt_in2, 1, qinfo1);
81 TensorType dst = create_tensor<TensorType>(out_shape, dt_out, 1, qinfo_out);
82
83 // Check whether do in-place computation and whether inputs are broadcast compatible
84 TensorType *actual_dst = &dst;
85 if(_is_inplace)
86 {
87 bool src1_is_inplace = !arm_compute::detail::have_different_dimensions(out_shape, shape0, 0) && (qinfo0 == qinfo_out) && (dt_in1 == dt_out);
88 bool src2_is_inplace = !arm_compute::detail::have_different_dimensions(out_shape, shape1, 0) && (qinfo1 == qinfo_out) && (dt_in2 == dt_out);
89 bool do_in_place = out_shape.total_size() != 0 && (src1_is_inplace || src2_is_inplace);
90 ARM_COMPUTE_ASSERT(do_in_place);
91
92 if(src1_is_inplace)
93 {
94 actual_dst = &src1;
95 }
96 else
97 {
98 actual_dst = &src2;
99 }
100 }
John Richardsondd715f22017-09-18 16:10:48 +0100101
Manuel Bottini36b8f052020-06-23 12:22:15 +0100102 auto allocate_tensor = [](TensorType & t)
Sang-Hoon Park95a6f722020-06-19 15:31:29 +0100103 {
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100104 ARM_COMPUTE_ASSERT(t.info()->is_resizable());
Sang-Hoon Park95a6f722020-06-19 15:31:29 +0100105 t.allocator()->allocate();
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100106 ARM_COMPUTE_ASSERT(!t.info()->is_resizable());
Sang-Hoon Park95a6f722020-06-19 15:31:29 +0100107 };
108
John Richardsondd715f22017-09-18 16:10:48 +0100109 // Create and configure function
110 FunctionType multiply;
Sheri Zhanga387e272021-06-29 17:34:06 +0100111 multiply.configure(&src1, &src2, actual_dst, scale, convert_policy, rounding_policy, act_info);
John Richardsondd715f22017-09-18 16:10:48 +0100112
Sang-Hoon Park95a6f722020-06-19 15:31:29 +0100113 allocate_tensor(src1);
114 allocate_tensor(src2);
John Richardsondd715f22017-09-18 16:10:48 +0100115
Sheri Zhanga387e272021-06-29 17:34:06 +0100116 // If don't do in-place computation, still need to allocate original dst
Sang-Hoon Park95a6f722020-06-19 15:31:29 +0100117 if(!_is_inplace)
118 {
119 allocate_tensor(dst);
120 }
John Richardsondd715f22017-09-18 16:10:48 +0100121
122 // Fill tensors
123 fill(AccessorType(src1), 0);
124 fill(AccessorType(src2), 1);
125
126 // Compute function
127 multiply.run();
128
Sheri Zhanga387e272021-06-29 17:34:06 +0100129 return std::move(*actual_dst);
John Richardsondd715f22017-09-18 16:10:48 +0100130 }
131
Michele Di Giorgio9428a182020-03-30 14:10:20 +0100132 SimpleTensor<T3> compute_reference(const TensorShape &shape0, const TensorShape &shape1, DataType dt_in1, DataType dt_in2, DataType dt_out,
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100133 float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy,
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000134 QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out, ActivationLayerInfo act_info)
John Richardsondd715f22017-09-18 16:10:48 +0100135 {
136 // Create reference
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100137 SimpleTensor<T1> src1{ shape0, dt_in1, 1, qinfo0 };
138 SimpleTensor<T2> src2{ shape1, dt_in2, 1, qinfo1 };
John Richardsondd715f22017-09-18 16:10:48 +0100139
140 // Fill reference
141 fill(src1, 0);
142 fill(src2, 1);
143
Sheri Zhanga387e272021-06-29 17:34:06 +0100144 auto result = reference::pixel_wise_multiplication<T1, T2, T3>(src1, src2, scale, convert_policy, rounding_policy, dt_out, qinfo_out);
145 return act_info.enabled() ? reference::activation_layer(result, act_info, qinfo_out) : result;
John Richardsondd715f22017-09-18 16:10:48 +0100146 }
147
148 TensorType _target{};
Michele Di Giorgio9428a182020-03-30 14:10:20 +0100149 SimpleTensor<T3> _reference{};
Sang-Hoon Park95a6f722020-06-19 15:31:29 +0100150 bool _is_inplace{ false };
Manuel Bottini79fa9a22019-02-22 17:54:22 +0000151};
152
Sheri Zhangfcf6f4e2020-06-25 20:01:00 +0100153template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2, typename T3 = T2>
154class PixelWiseMultiplicationValidationFixture : public PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2, T3>
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000155{
156public:
Sheri Zhangfcf6f4e2020-06-25 20:01:00 +0100157 void setup(const TensorShape &shape, DataType dt_in1, DataType dt_in2, DataType dt_out, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy, bool is_inplace)
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000158 {
Sheri Zhangfcf6f4e2020-06-25 20:01:00 +0100159 PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2, T3>::setup(shape, shape, dt_in1, dt_in2, dt_out, scale, convert_policy, rounding_policy,
160 QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), ActivationLayerInfo(), is_inplace);
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000161 }
162};
163
Sheri Zhangfcf6f4e2020-06-25 20:01:00 +0100164template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2, typename T3 = T2>
165class PixelWiseMultiplicationBroadcastValidationFixture : public PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2, T3>
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100166{
167public:
Sheri Zhangfcf6f4e2020-06-25 20:01:00 +0100168 void setup(const TensorShape &shape0, const TensorShape &shape1, DataType dt_in1, DataType dt_in2, DataType dt_out, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy,
169 bool is_inplace)
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100170 {
Sheri Zhangfcf6f4e2020-06-25 20:01:00 +0100171 PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2, T3>::setup(shape0, shape1, dt_in1, dt_in2, dt_out, scale, convert_policy, rounding_policy,
172 QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), ActivationLayerInfo(), is_inplace);
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000173 }
174};
175
176template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2>
177class PixelWiseMultiplicationValidationFloatFixture : public PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2>
178{
179public:
Sang-Hoon Park95a6f722020-06-19 15:31:29 +0100180 void setup(const TensorShape &shape, DataType dt_in1, DataType dt_in2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy, ActivationLayerInfo act_info, bool is_inplace)
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000181 {
182 PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2>::setup(shape, shape, dt_in1, dt_in2, dt_in2, scale, convert_policy, rounding_policy,
Sang-Hoon Park95a6f722020-06-19 15:31:29 +0100183 QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), act_info, is_inplace);
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000184 }
185};
186
187template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2>
Suhail Munshi448cb452021-04-23 16:23:25 +0100188class PixelWiseMultiplicationValidationIntegerFixture : public PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2>
189{
190public:
Suhail Munshi448cb452021-04-23 16:23:25 +0100191 void setup(const TensorShape &shape, DataType dt_in1, DataType dt_in2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy, ActivationLayerInfo act_info, bool is_inplace)
192 {
193 PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2>::setup(shape, shape, dt_in1, dt_in2, dt_in2, scale, convert_policy, rounding_policy,
194 QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), act_info, is_inplace);
195 }
196};
197
198template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2>
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000199class PixelWiseMultiplicationBroadcastValidationFloatFixture : public PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2>
200{
201public:
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000202 void setup(const TensorShape &shape0, const TensorShape &shape1, DataType dt_in1, DataType dt_in2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy,
Sang-Hoon Park95a6f722020-06-19 15:31:29 +0100203 ActivationLayerInfo act_info, bool is_inplace)
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000204 {
205 PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2>::setup(shape0, shape1, dt_in1, dt_in2, dt_in2, scale, convert_policy, rounding_policy,
Sang-Hoon Park95a6f722020-06-19 15:31:29 +0100206 QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), act_info, is_inplace);
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100207 }
208};
209
Michele Di Giorgio9428a182020-03-30 14:10:20 +0100210template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2, typename T3 = T2>
211class PixelWiseMultiplicationValidationQuantizedFixture : public PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2, T3>
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100212{
213public:
Michele Di Giorgio9428a182020-03-30 14:10:20 +0100214 void setup(const TensorShape &shape, DataType dt_in1, DataType dt_in2, DataType dt_out, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy,
Sang-Hoon Park95a6f722020-06-19 15:31:29 +0100215 QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out, bool is_inplace)
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100216 {
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000217 PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2, T3>::setup(shape, shape, dt_in1, dt_in2, dt_out, scale, convert_policy, rounding_policy,
Sang-Hoon Park95a6f722020-06-19 15:31:29 +0100218 qinfo0, qinfo1, qinfo_out, ActivationLayerInfo(), is_inplace);
Georgios Pinitasbf28a3c2018-09-18 14:34:48 +0100219 }
220};
Sheri Zhanga449a362020-07-16 15:52:25 +0100221
222template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2, typename T3 = T2>
223class PixelWiseMultiplicationBroadcastValidationQuantizedFixture : public PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2, T3>
224{
225public:
Sheri Zhanga449a362020-07-16 15:52:25 +0100226 void setup(const TensorShape &shape0, const TensorShape &shape1, DataType dt_in1, DataType dt_in2, DataType dt_out, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy,
227 QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out, bool is_inplace)
228 {
229 PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2, T3>::setup(shape0, shape1, dt_in1, dt_in2, dt_out, scale, convert_policy, rounding_policy,
230 qinfo0, qinfo1, qinfo_out, ActivationLayerInfo(), is_inplace);
231 }
232};
John Richardsondd715f22017-09-18 16:10:48 +0100233} // namespace validation
234} // namespace test
235} // namespace arm_compute
236#endif /* ARM_COMPUTE_TEST_PIXEL_WISE_MULTIPLICATION_FIXTURE */