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