blob: a5d6fc966d07d78a7d289d9e60cdaed090fd69c1 [file] [log] [blame]
Pablo Tello89519332017-11-17 11:52:36 +00001/*
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_WINOGRAD_LAYER_FIXTURE
25#define ARM_COMPUTE_TEST_WINOGRAD_LAYER_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29#include "arm_compute/runtime/NEON/NEScheduler.h"
30#include "tests/AssetsLibrary.h"
31#include "tests/Globals.h"
32#include "tests/IAccessor.h"
33#include "tests/framework/Asserts.h"
34#include "tests/framework/Fixture.h"
35#include "tests/validation/CPP/ConvolutionLayer.h"
36#include "tests/validation/CPP/Utils.h"
37#include "tests/validation/Helpers.h"
38
39#include <random>
40
41namespace arm_compute
42{
43class NEWinogradLayer;
44
45namespace test
46{
47namespace validation
48{
49template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
50class WinogradLayerValidationFixture : public framework::Fixture
51{
52public:
53 template <typename...>
54 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info)
55 {
56 _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info);
57 _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info);
58 }
59
60protected:
61 template <typename U>
62 void fill(U &&tensor, int i, float min, float max)
63 {
64 switch(tensor.data_type())
65 {
66 case DataType::F32:
67 {
68 std::uniform_real_distribution<> distribution(min, max);
69 library->fill(tensor, distribution, i);
70 break;
71 }
72 default:
73 {
74 ARM_COMPUTE_ERROR("Not supported");
75 library->fill_tensor_uniform(tensor, i);
76 break;
77 }
78 }
79 }
80
81 TensorType compute_target(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info)
82 {
83 // Create tensors
84 TensorType src = create_tensor<TensorType>(input_shape, DataType::F32, 1);
85 TensorType weights = create_tensor<TensorType>(weights_shape, DataType::F32, 1);
86 TensorType bias = create_tensor<TensorType>(bias_shape, DataType::F32, 1);
87 TensorType dst = create_tensor<TensorType>(output_shape, DataType::F32, 1);
88
89 // Create and configure function
90 FunctionType conv;
91 conv.configure(&src, &weights, nullptr, &dst, info);
92
93 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
94 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
95 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
96 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
97
98 // Allocate tensors
99 src.allocator()->allocate();
100 weights.allocator()->allocate();
101 bias.allocator()->allocate();
102 dst.allocator()->allocate();
103
104 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
105 ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS);
106 ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS);
107 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
108
109 // Fill tensors
110 fill(AccessorType(src), 0, -1.f, 1.f);
111 fill(AccessorType(weights), 1, -1.f, 1.f);
112 fill(AccessorType(bias), 2, 0.f, 0.f);
113 fill(AccessorType(dst), 3, -1.f, 1.f);
114
115 // Compute NEWinogradLayer function
116 conv.run();
117
118 return dst;
119 }
120
121 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info)
122 {
123 // Create reference
124 SimpleTensor<T> src{ input_shape, DataType::F32, 1 };
125 SimpleTensor<T> weights{ weights_shape, DataType::F32, 1 };
126 SimpleTensor<T> bias{ bias_shape, DataType::F32, 1 };
127
128 // Fill reference
129 fill(src, 0, -1.f, 1.f);
130 fill(weights, 1, -1.f, 1.f);
131 fill(bias, 2, 0.f, 0.f);
132
133 return reference::convolution_layer<T>(src, weights, bias, output_shape, info);
134 }
135
136 TensorType _target{};
137 SimpleTensor<T> _reference{};
138 int _fractional_bits{};
139 DataType _data_type{};
140};
141
142} // namespace validation
143} // namespace test
144} // namespace arm_compute
145#endif /* ARM_COMPUTE_TEST_WINOGRAD_LAYER_FIXTURE */