xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/fixtures/ScharrFixture.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-2021 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 #ifndef ARM_COMPUTE_TEST_SCHARR_FIXTURE
25 #define ARM_COMPUTE_TEST_SCHARR_FIXTURE
26 
27 #include "tests/Globals.h"
28 #include "tests/IAccessor.h"
29 #include "tests/Types.h"
30 #include "tests/framework/Asserts.h"
31 #include "tests/framework/Fixture.h"
32 #include "tests/validation/reference/Scharr.h"
33 
34 #include <memory>
35 
36 namespace arm_compute
37 {
38 class CLScharr3x3;
39 class NEScharr3x3;
40 
41 namespace test
42 {
43 namespace validation
44 {
45 namespace
46 {
47 template <typename Function>
48 struct info;
49 
50 template <>
51 struct info<NEScharr3x3>
52 {
53     static const Format dst_format  = Format::S16;
54     static const int    filter_size = 3;
55 };
56 
57 template <>
58 struct info<CLScharr3x3>
59 {
60     static const Format dst_format  = Format::S16;
61     static const int    filter_size = 3;
62 };
63 } // namespace
64 
65 template <typename TensorType, typename AccessorType, typename FunctionType, typename T, typename U>
66 class ScharrValidationFixture : public framework::Fixture
67 {
68 public:
69     template <typename...>
70     void setup(TensorShape shape, BorderMode border_mode, Format format, GradientDimension gradient_dimension)
71     {
72         // Generate a random constant value
73         std::mt19937                           gen(library->seed());
74         std::uniform_int_distribution<uint8_t> int_dist(0, 255);
75         const uint8_t                          constant_border_value = int_dist(gen);
76 
77         _border_mode = border_mode;
78         _target      = compute_target(shape, border_mode, format, constant_border_value, gradient_dimension);
79         _reference   = compute_reference(shape, info<FunctionType>::filter_size, border_mode, format, constant_border_value, gradient_dimension);
80     }
81 
82 protected:
83     template <typename V>
84     void fill(V &&tensor)
85     {
86         library->fill_tensor_uniform(tensor, 0);
87     }
88 
89     template <typename V>
90     void fill_zero(V &&tensor)
91     {
92         library->fill_tensor_uniform(tensor, 0, static_cast<U>(0), static_cast<U>(0));
93     }
94 
95     std::pair<TensorType, TensorType> compute_target(const TensorShape &shape, BorderMode border_mode, Format format, uint8_t constant_border_value, GradientDimension gradient_dimension)
96     {
97         // Create tensors
98         TensorType src   = create_tensor<TensorType>(shape, data_type_from_format(format));
99         TensorType dst_x = create_tensor<TensorType>(shape, data_type_from_format(info<FunctionType>::dst_format));
100         TensorType dst_y = create_tensor<TensorType>(shape, data_type_from_format(info<FunctionType>::dst_format));
101 
102         src.info()->set_format(format);
103         dst_x.info()->set_format(info<FunctionType>::dst_format);
104         dst_y.info()->set_format(info<FunctionType>::dst_format);
105 
106         FunctionType scharr;
107 
108         switch(gradient_dimension)
109         {
110             case GradientDimension::GRAD_X:
111                 scharr.configure(&src, &dst_x, nullptr, border_mode, constant_border_value);
112                 break;
113             case GradientDimension::GRAD_Y:
114                 scharr.configure(&src, nullptr, &dst_y, border_mode, constant_border_value);
115                 break;
116             case GradientDimension::GRAD_XY:
117                 scharr.configure(&src, &dst_x, &dst_y, border_mode, constant_border_value);
118                 break;
119             default:
120                 ARM_COMPUTE_ERROR("Gradient dimension not supported");
121         }
122 
123         ARM_COMPUTE_ASSERT(src.info()->is_resizable());
124         ARM_COMPUTE_ASSERT(dst_x.info()->is_resizable());
125         ARM_COMPUTE_ASSERT(dst_y.info()->is_resizable());
126 
127         // Allocate tensors
128         src.allocator()->allocate();
129         dst_x.allocator()->allocate();
130         dst_y.allocator()->allocate();
131 
132         ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
133         ARM_COMPUTE_ASSERT(!dst_x.info()->is_resizable());
134         ARM_COMPUTE_ASSERT(!dst_y.info()->is_resizable());
135 
136         // Fill tensors
137         fill(AccessorType(src));
138         fill_zero(AccessorType(dst_x));
139         fill_zero(AccessorType(dst_y));
140 
141         // Compute function
142         scharr.run();
143 
144         return std::make_pair(std::move(dst_x), std::move(dst_y));
145     }
146 
147     std::pair<SimpleTensor<U>, SimpleTensor<U>> compute_reference(const TensorShape &shape, int filter_size, BorderMode border_mode, Format format, uint8_t constant_border_value,
148                                                                   GradientDimension gradient_dimension)
149     {
150         // Create reference
151         SimpleTensor<T> src{ shape, format };
152 
153         // Fill reference
154         fill(src);
155 
156         return reference::scharr<U>(src, filter_size, border_mode, constant_border_value, gradient_dimension);
157     }
158 
159     BorderMode _border_mode{ BorderMode::UNDEFINED };
160     std::pair<TensorType, TensorType>           _target{};
161     std::pair<SimpleTensor<U>, SimpleTensor<U>> _reference{};
162 };
163 } // namespace validation
164 } // namespace test
165 } // namespace arm_compute
166 #endif /* ARM_COMPUTE_TEST_SCHARR_FIXTURE */
167