xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/fixtures/PixelWiseMultiplicationFixture.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2017-2021 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust #ifndef ARM_COMPUTE_TEST_PIXEL_WISE_MULTIPLICATION_FIXTURE
25*c217d954SCole Faust #define ARM_COMPUTE_TEST_PIXEL_WISE_MULTIPLICATION_FIXTURE
26*c217d954SCole Faust 
27*c217d954SCole Faust #include "arm_compute/core/TensorShape.h"
28*c217d954SCole Faust #include "arm_compute/core/Types.h"
29*c217d954SCole Faust #include "tests/AssetsLibrary.h"
30*c217d954SCole Faust #include "tests/Globals.h"
31*c217d954SCole Faust #include "tests/IAccessor.h"
32*c217d954SCole Faust #include "tests/framework/Asserts.h"
33*c217d954SCole Faust #include "tests/framework/Fixture.h"
34*c217d954SCole Faust #include "tests/validation/reference/ActivationLayer.h"
35*c217d954SCole Faust #include "tests/validation/reference/PixelWiseMultiplication.h"
36*c217d954SCole Faust 
37*c217d954SCole Faust namespace arm_compute
38*c217d954SCole Faust {
39*c217d954SCole Faust namespace test
40*c217d954SCole Faust {
41*c217d954SCole Faust namespace validation
42*c217d954SCole Faust {
43*c217d954SCole Faust template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2, typename T3 = T2>
44*c217d954SCole Faust class PixelWiseMultiplicationGenericValidationFixture : public framework::Fixture
45*c217d954SCole Faust {
46*c217d954SCole Faust public:
47*c217d954SCole Faust     template <typename...>
setup(const TensorShape & shape0,const TensorShape & shape1,DataType dt_in1,DataType dt_in2,DataType dt_out,float scale,ConvertPolicy convert_policy,RoundingPolicy rounding_policy,QuantizationInfo qinfo0,QuantizationInfo qinfo1,QuantizationInfo qinfo_out,ActivationLayerInfo act_info,bool is_inplace)48*c217d954SCole Faust     void setup(const TensorShape &shape0,
49*c217d954SCole Faust                const TensorShape &shape1,
50*c217d954SCole Faust                DataType            dt_in1,
51*c217d954SCole Faust                DataType            dt_in2,
52*c217d954SCole Faust                DataType            dt_out,
53*c217d954SCole Faust                float               scale,
54*c217d954SCole Faust                ConvertPolicy       convert_policy,
55*c217d954SCole Faust                RoundingPolicy      rounding_policy,
56*c217d954SCole Faust                QuantizationInfo    qinfo0,
57*c217d954SCole Faust                QuantizationInfo    qinfo1,
58*c217d954SCole Faust                QuantizationInfo    qinfo_out,
59*c217d954SCole Faust                ActivationLayerInfo act_info,
60*c217d954SCole Faust                bool                is_inplace)
61*c217d954SCole Faust     {
62*c217d954SCole Faust         _is_inplace = is_inplace;
63*c217d954SCole Faust         _target     = compute_target(shape0, shape1, dt_in1, dt_in2, dt_out, scale, convert_policy, rounding_policy, qinfo0, qinfo1, qinfo_out, act_info);
64*c217d954SCole Faust         _reference  = compute_reference(shape0, shape1, dt_in1, dt_in2, dt_out, scale, convert_policy, rounding_policy, qinfo0, qinfo1, qinfo_out, act_info);
65*c217d954SCole Faust     }
66*c217d954SCole Faust 
67*c217d954SCole Faust protected:
68*c217d954SCole Faust     template <typename U>
fill(U && tensor,unsigned int seed_offset)69*c217d954SCole Faust     void fill(U &&tensor, unsigned int seed_offset)
70*c217d954SCole Faust     {
71*c217d954SCole Faust         library->fill_tensor_uniform(tensor, seed_offset);
72*c217d954SCole Faust     }
73*c217d954SCole Faust 
compute_target(const TensorShape & shape0,const TensorShape & shape1,DataType dt_in1,DataType dt_in2,DataType dt_out,float scale,ConvertPolicy convert_policy,RoundingPolicy rounding_policy,QuantizationInfo qinfo0,QuantizationInfo qinfo1,QuantizationInfo qinfo_out,ActivationLayerInfo act_info)74*c217d954SCole Faust     TensorType compute_target(const TensorShape &shape0, const TensorShape &shape1, DataType dt_in1, DataType dt_in2, DataType dt_out,
75*c217d954SCole Faust                               float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy,
76*c217d954SCole Faust                               QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out, ActivationLayerInfo act_info)
77*c217d954SCole Faust     {
78*c217d954SCole Faust         // Create tensors
79*c217d954SCole Faust         const TensorShape out_shape = TensorShape::broadcast_shape(shape0, shape1);
80*c217d954SCole Faust         TensorType        src1      = create_tensor<TensorType>(shape0, dt_in1, 1, qinfo0);
81*c217d954SCole Faust         TensorType        src2      = create_tensor<TensorType>(shape1, dt_in2, 1, qinfo1);
82*c217d954SCole Faust         TensorType        dst       = create_tensor<TensorType>(out_shape, dt_out, 1, qinfo_out);
83*c217d954SCole Faust 
84*c217d954SCole Faust         // Check whether do in-place computation and whether inputs are broadcast compatible
85*c217d954SCole Faust         TensorType *actual_dst = &dst;
86*c217d954SCole Faust         if(_is_inplace)
87*c217d954SCole Faust         {
88*c217d954SCole Faust             bool src1_is_inplace = !arm_compute::detail::have_different_dimensions(out_shape, shape0, 0) && (qinfo0 == qinfo_out) && (dt_in1 == dt_out);
89*c217d954SCole Faust             bool src2_is_inplace = !arm_compute::detail::have_different_dimensions(out_shape, shape1, 0) && (qinfo1 == qinfo_out) && (dt_in2 == dt_out);
90*c217d954SCole Faust             bool do_in_place     = out_shape.total_size() != 0 && (src1_is_inplace || src2_is_inplace);
91*c217d954SCole Faust             ARM_COMPUTE_ASSERT(do_in_place);
92*c217d954SCole Faust 
93*c217d954SCole Faust             if(src1_is_inplace)
94*c217d954SCole Faust             {
95*c217d954SCole Faust                 actual_dst = &src1;
96*c217d954SCole Faust             }
97*c217d954SCole Faust             else
98*c217d954SCole Faust             {
99*c217d954SCole Faust                 actual_dst = &src2;
100*c217d954SCole Faust             }
101*c217d954SCole Faust         }
102*c217d954SCole Faust 
103*c217d954SCole Faust         auto allocate_tensor = [](TensorType & t)
104*c217d954SCole Faust         {
105*c217d954SCole Faust             ARM_COMPUTE_ASSERT(t.info()->is_resizable());
106*c217d954SCole Faust             t.allocator()->allocate();
107*c217d954SCole Faust             ARM_COMPUTE_ASSERT(!t.info()->is_resizable());
108*c217d954SCole Faust         };
109*c217d954SCole Faust 
110*c217d954SCole Faust         // Create and configure function
111*c217d954SCole Faust         FunctionType multiply;
112*c217d954SCole Faust         multiply.configure(&src1, &src2, actual_dst, scale, convert_policy, rounding_policy, act_info);
113*c217d954SCole Faust 
114*c217d954SCole Faust         allocate_tensor(src1);
115*c217d954SCole Faust         allocate_tensor(src2);
116*c217d954SCole Faust 
117*c217d954SCole Faust         // If don't do in-place computation, still need to allocate original dst
118*c217d954SCole Faust         if(!_is_inplace)
119*c217d954SCole Faust         {
120*c217d954SCole Faust             allocate_tensor(dst);
121*c217d954SCole Faust         }
122*c217d954SCole Faust 
123*c217d954SCole Faust         // Fill tensors
124*c217d954SCole Faust         fill(AccessorType(src1), 0);
125*c217d954SCole Faust         fill(AccessorType(src2), 1);
126*c217d954SCole Faust 
127*c217d954SCole Faust         // Compute function
128*c217d954SCole Faust         multiply.run();
129*c217d954SCole Faust 
130*c217d954SCole Faust         return std::move(*actual_dst);
131*c217d954SCole Faust     }
132*c217d954SCole Faust 
compute_reference(const TensorShape & shape0,const TensorShape & shape1,DataType dt_in1,DataType dt_in2,DataType dt_out,float scale,ConvertPolicy convert_policy,RoundingPolicy rounding_policy,QuantizationInfo qinfo0,QuantizationInfo qinfo1,QuantizationInfo qinfo_out,ActivationLayerInfo act_info)133*c217d954SCole Faust     SimpleTensor<T3> compute_reference(const TensorShape &shape0, const TensorShape &shape1, DataType dt_in1, DataType dt_in2, DataType dt_out,
134*c217d954SCole Faust                                        float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy,
135*c217d954SCole Faust                                        QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out, ActivationLayerInfo act_info)
136*c217d954SCole Faust     {
137*c217d954SCole Faust         // Create reference
138*c217d954SCole Faust         SimpleTensor<T1> src1{ shape0, dt_in1, 1, qinfo0 };
139*c217d954SCole Faust         SimpleTensor<T2> src2{ shape1, dt_in2, 1, qinfo1 };
140*c217d954SCole Faust 
141*c217d954SCole Faust         // Fill reference
142*c217d954SCole Faust         fill(src1, 0);
143*c217d954SCole Faust         fill(src2, 1);
144*c217d954SCole Faust 
145*c217d954SCole Faust         auto result = reference::pixel_wise_multiplication<T1, T2, T3>(src1, src2, scale, convert_policy, rounding_policy, dt_out, qinfo_out);
146*c217d954SCole Faust         return act_info.enabled() ? reference::activation_layer(result, act_info, qinfo_out) : result;
147*c217d954SCole Faust     }
148*c217d954SCole Faust 
149*c217d954SCole Faust     TensorType       _target{};
150*c217d954SCole Faust     SimpleTensor<T3> _reference{};
151*c217d954SCole Faust     bool             _is_inplace{ false };
152*c217d954SCole Faust };
153*c217d954SCole Faust 
154*c217d954SCole Faust template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2, typename T3 = T2>
155*c217d954SCole Faust class PixelWiseMultiplicationValidationFixture : public PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2, T3>
156*c217d954SCole Faust {
157*c217d954SCole Faust public:
158*c217d954SCole Faust     template <typename...>
setup(const TensorShape & shape,DataType dt_in1,DataType dt_in2,DataType dt_out,float scale,ConvertPolicy convert_policy,RoundingPolicy rounding_policy,bool is_inplace)159*c217d954SCole Faust     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)
160*c217d954SCole Faust     {
161*c217d954SCole Faust         PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2, T3>::setup(shape, shape, dt_in1, dt_in2, dt_out, scale, convert_policy, rounding_policy,
162*c217d954SCole Faust                                                                                                                    QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), ActivationLayerInfo(), is_inplace);
163*c217d954SCole Faust     }
164*c217d954SCole Faust };
165*c217d954SCole Faust 
166*c217d954SCole Faust template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2, typename T3 = T2>
167*c217d954SCole Faust class PixelWiseMultiplicationBroadcastValidationFixture : public PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2, T3>
168*c217d954SCole Faust {
169*c217d954SCole Faust public:
170*c217d954SCole Faust     template <typename...>
setup(const TensorShape & shape0,const TensorShape & shape1,DataType dt_in1,DataType dt_in2,DataType dt_out,float scale,ConvertPolicy convert_policy,RoundingPolicy rounding_policy,bool is_inplace)171*c217d954SCole Faust     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,
172*c217d954SCole Faust                bool is_inplace)
173*c217d954SCole Faust     {
174*c217d954SCole Faust         PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2, T3>::setup(shape0, shape1, dt_in1, dt_in2, dt_out, scale, convert_policy, rounding_policy,
175*c217d954SCole Faust                                                                                                                    QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), ActivationLayerInfo(), is_inplace);
176*c217d954SCole Faust     }
177*c217d954SCole Faust };
178*c217d954SCole Faust 
179*c217d954SCole Faust template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2>
180*c217d954SCole Faust class PixelWiseMultiplicationValidationFloatFixture : public PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2>
181*c217d954SCole Faust {
182*c217d954SCole Faust public:
183*c217d954SCole Faust     template <typename...>
setup(const TensorShape & shape,DataType dt_in1,DataType dt_in2,float scale,ConvertPolicy convert_policy,RoundingPolicy rounding_policy,ActivationLayerInfo act_info,bool is_inplace)184*c217d954SCole Faust     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)
185*c217d954SCole Faust     {
186*c217d954SCole Faust         PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2>::setup(shape, shape, dt_in1, dt_in2, dt_in2, scale, convert_policy, rounding_policy,
187*c217d954SCole Faust                                                                                                                QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), act_info, is_inplace);
188*c217d954SCole Faust     }
189*c217d954SCole Faust };
190*c217d954SCole Faust 
191*c217d954SCole Faust template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2>
192*c217d954SCole Faust class PixelWiseMultiplicationValidationIntegerFixture : public PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2>
193*c217d954SCole Faust {
194*c217d954SCole Faust public:
195*c217d954SCole Faust     template <typename...>
setup(const TensorShape & shape,DataType dt_in1,DataType dt_in2,float scale,ConvertPolicy convert_policy,RoundingPolicy rounding_policy,ActivationLayerInfo act_info,bool is_inplace)196*c217d954SCole Faust     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)
197*c217d954SCole Faust     {
198*c217d954SCole Faust         PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2>::setup(shape, shape, dt_in1, dt_in2, dt_in2, scale, convert_policy, rounding_policy,
199*c217d954SCole Faust                                                                                                                QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), act_info, is_inplace);
200*c217d954SCole Faust     }
201*c217d954SCole Faust };
202*c217d954SCole Faust 
203*c217d954SCole Faust template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2>
204*c217d954SCole Faust class PixelWiseMultiplicationBroadcastValidationFloatFixture : public PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2>
205*c217d954SCole Faust {
206*c217d954SCole Faust public:
207*c217d954SCole Faust     template <typename...>
setup(const TensorShape & shape0,const TensorShape & shape1,DataType dt_in1,DataType dt_in2,float scale,ConvertPolicy convert_policy,RoundingPolicy rounding_policy,ActivationLayerInfo act_info,bool is_inplace)208*c217d954SCole Faust     void setup(const TensorShape &shape0, const TensorShape &shape1, DataType dt_in1, DataType dt_in2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy,
209*c217d954SCole Faust                ActivationLayerInfo act_info, bool is_inplace)
210*c217d954SCole Faust     {
211*c217d954SCole Faust         PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2>::setup(shape0, shape1, dt_in1, dt_in2, dt_in2, scale, convert_policy, rounding_policy,
212*c217d954SCole Faust                                                                                                                QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), act_info, is_inplace);
213*c217d954SCole Faust     }
214*c217d954SCole Faust };
215*c217d954SCole Faust 
216*c217d954SCole Faust template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2, typename T3 = T2>
217*c217d954SCole Faust class PixelWiseMultiplicationValidationQuantizedFixture : public PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2, T3>
218*c217d954SCole Faust {
219*c217d954SCole Faust public:
220*c217d954SCole Faust     template <typename...>
setup(const TensorShape & shape,DataType dt_in1,DataType dt_in2,DataType dt_out,float scale,ConvertPolicy convert_policy,RoundingPolicy rounding_policy,QuantizationInfo qinfo0,QuantizationInfo qinfo1,QuantizationInfo qinfo_out,bool is_inplace)221*c217d954SCole Faust     void setup(const TensorShape &shape, DataType dt_in1, DataType dt_in2, DataType dt_out, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy,
222*c217d954SCole Faust                QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out, bool is_inplace)
223*c217d954SCole Faust     {
224*c217d954SCole Faust         PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2, T3>::setup(shape, shape, dt_in1, dt_in2, dt_out, scale, convert_policy, rounding_policy,
225*c217d954SCole Faust                                                                                                                    qinfo0, qinfo1, qinfo_out, ActivationLayerInfo(), is_inplace);
226*c217d954SCole Faust     }
227*c217d954SCole Faust };
228*c217d954SCole Faust 
229*c217d954SCole Faust template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2, typename T3 = T2>
230*c217d954SCole Faust class PixelWiseMultiplicationBroadcastValidationQuantizedFixture : public PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2, T3>
231*c217d954SCole Faust {
232*c217d954SCole Faust public:
233*c217d954SCole Faust     template <typename...>
setup(const TensorShape & shape0,const TensorShape & shape1,DataType dt_in1,DataType dt_in2,DataType dt_out,float scale,ConvertPolicy convert_policy,RoundingPolicy rounding_policy,QuantizationInfo qinfo0,QuantizationInfo qinfo1,QuantizationInfo qinfo_out,bool is_inplace)234*c217d954SCole Faust     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,
235*c217d954SCole Faust                QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out, bool is_inplace)
236*c217d954SCole Faust     {
237*c217d954SCole Faust         PixelWiseMultiplicationGenericValidationFixture<TensorType, AccessorType, FunctionType, T1, T2, T3>::setup(shape0, shape1, dt_in1, dt_in2, dt_out, scale, convert_policy, rounding_policy,
238*c217d954SCole Faust                                                                                                                    qinfo0, qinfo1, qinfo_out, ActivationLayerInfo(), is_inplace);
239*c217d954SCole Faust     }
240*c217d954SCole Faust };
241*c217d954SCole Faust } // namespace validation
242*c217d954SCole Faust } // namespace test
243*c217d954SCole Faust } // namespace arm_compute
244*c217d954SCole Faust #endif /* ARM_COMPUTE_TEST_PIXEL_WISE_MULTIPLICATION_FIXTURE */
245