• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017 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_DERIVATIVE_FIXTURE
25 #define ARM_COMPUTE_TEST_DERIVATIVE_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/Derivative.h"
33 
34 #include <memory>
35 
36 namespace arm_compute
37 {
38 namespace test
39 {
40 namespace validation
41 {
42 template <typename TensorType, typename AccessorType, typename FunctionType, typename T, typename U>
43 class DerivativeValidationFixture : public framework::Fixture
44 {
45 public:
46     template <typename...>
setup(TensorShape shape,BorderMode border_mode,Format format,GradientDimension gradient_dimension)47     void setup(TensorShape shape, BorderMode border_mode, Format format, GradientDimension gradient_dimension)
48     {
49         // Generate a random constant value
50         std::mt19937                           gen(library->seed());
51         std::uniform_int_distribution<uint8_t> int_dist(0, 255);
52         const uint8_t                          constant_border_value = int_dist(gen);
53 
54         _border_mode = border_mode;
55         _target      = compute_target(shape, border_mode, format, constant_border_value, gradient_dimension);
56         _reference   = compute_reference(shape, border_mode, format, constant_border_value, gradient_dimension);
57     }
58 
59 protected:
60     template <typename V>
fill(V && tensor)61     void fill(V &&tensor)
62     {
63         library->fill_tensor_uniform(tensor, 0);
64     }
65 
66     template <typename V>
fill_zero(V && tensor)67     void fill_zero(V &&tensor)
68     {
69         library->fill_tensor_uniform(tensor, 0, static_cast<U>(0), static_cast<U>(0));
70     }
71 
compute_target(const TensorShape & shape,BorderMode border_mode,Format format,uint8_t constant_border_value,GradientDimension gradient_dimension)72     std::pair<TensorType, TensorType> compute_target(const TensorShape &shape, BorderMode border_mode, Format format, uint8_t constant_border_value, GradientDimension gradient_dimension)
73     {
74         // Create tensors
75         TensorType src   = create_tensor<TensorType>(shape, data_type_from_format(format));
76         TensorType dst_x = create_tensor<TensorType>(shape, data_type_from_format(Format::S16));
77         TensorType dst_y = create_tensor<TensorType>(shape, data_type_from_format(Format::S16));
78 
79         src.info()->set_format(format);
80         dst_x.info()->set_format(Format::S16);
81         dst_y.info()->set_format(Format::S16);
82 
83         FunctionType derivative;
84 
85         switch(gradient_dimension)
86         {
87             case GradientDimension::GRAD_X:
88                 derivative.configure(&src, &dst_x, nullptr, border_mode, constant_border_value);
89                 break;
90             case GradientDimension::GRAD_Y:
91                 derivative.configure(&src, nullptr, &dst_y, border_mode, constant_border_value);
92                 break;
93             case GradientDimension::GRAD_XY:
94                 derivative.configure(&src, &dst_x, &dst_y, border_mode, constant_border_value);
95                 break;
96             default:
97                 ARM_COMPUTE_ERROR("Gradient dimension not supported");
98         }
99 
100         ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
101         ARM_COMPUTE_EXPECT(dst_x.info()->is_resizable(), framework::LogLevel::ERRORS);
102         ARM_COMPUTE_EXPECT(dst_y.info()->is_resizable(), framework::LogLevel::ERRORS);
103 
104         // Allocate tensors
105         src.allocator()->allocate();
106         dst_x.allocator()->allocate();
107         dst_y.allocator()->allocate();
108 
109         ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
110         ARM_COMPUTE_EXPECT(!dst_x.info()->is_resizable(), framework::LogLevel::ERRORS);
111         ARM_COMPUTE_EXPECT(!dst_y.info()->is_resizable(), framework::LogLevel::ERRORS);
112 
113         // Fill tensors
114         fill(AccessorType(src));
115         fill_zero(AccessorType(dst_x));
116         fill_zero(AccessorType(dst_y));
117 
118         // Compute function
119         derivative.run();
120 
121         return std::make_pair(std::move(dst_x), std::move(dst_y));
122     }
123 
compute_reference(const TensorShape & shape,BorderMode border_mode,Format format,uint8_t constant_border_value,GradientDimension gradient_dimension)124     std::pair<SimpleTensor<U>, SimpleTensor<U>> compute_reference(const TensorShape &shape, BorderMode border_mode, Format format, uint8_t constant_border_value, GradientDimension gradient_dimension)
125     {
126         // Create reference
127         SimpleTensor<T> src{ shape, format };
128 
129         // Fill reference
130         fill(src);
131 
132         return reference::derivative<U>(src, border_mode, constant_border_value, gradient_dimension);
133     }
134 
135     BorderMode _border_mode{ BorderMode::UNDEFINED };
136     std::pair<TensorType, TensorType>           _target{};
137     std::pair<SimpleTensor<U>, SimpleTensor<U>> _reference{};
138 };
139 } // namespace validation
140 } // namespace test
141 } // namespace arm_compute
142 #endif /* ARM_COMPUTE_TEST_DERIVATIVE_FIXTURE */
143