blob: 3ed55fb9fca740330a0d176861e7b77e8eb59da0 [file] [log] [blame]
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +00001/*
2 * Copyright (c) 2018 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#include "Winograd.h"
25
26#include "tests/validation/Helpers.h"
27#include "tests/validation/reference/Utils.h"
28
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +000029#include "arm_compute/core/Types.h"
30
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000031namespace arm_compute
32{
33namespace test
34{
35namespace validation
36{
37namespace reference
38{
39namespace
40{
41template <typename T>
42void winograd_input_transform3x3(const SimpleTensor<T> &src, SimpleTensor<T> &dst, const PadStrideInfo &conv_info)
43{
44 TensorShape shape4x4(4u, 4u);
45
46 // Simple tensor for the 4x4 input tile
47 SimpleTensor<T> src_tile{ shape4x4, src.data_type() };
48
49 // Simple tensor for the 4x4 temporary tile
50 SimpleTensor<T> tmp_tile{ shape4x4, src.data_type() };
51
52 // Simple tensor for the 4x4 output tile
53 SimpleTensor<T> dst_tile{ shape4x4, src.data_type() };
54
55 // Simple tensor for the transformation matrix
56 SimpleTensor<T> matrix{ shape4x4, src.data_type() };
57
58 // Simple tensor for the transformation matrix transposed
59 SimpleTensor<T> matrix_transposed{ shape4x4, src.data_type() };
60
61 const float matrix_values[] = { 1.f, 0.f, -1.f, 0.f,
62 0.f, 1.f, 1.f, 0.f,
63 0.f, -1.f, 1.f, 0.f,
64 0.f, 1.f, 0.f, -1.f
65 };
66
67 for(int i = 0; i < matrix.num_elements(); ++i)
68 {
69 matrix[i] = matrix_values[i];
70 }
71
72 transpose_matrix(matrix, matrix_transposed);
73
74 const int in_w = src.shape().x();
75 const int in_h = src.shape().y();
76 const int in_d = src.shape().z();
77 const int num_batches = src.shape().total_size() / (in_w * in_h * in_d);
78 const int num_tiles_x = std::ceil((in_w - 2 + conv_info.pad_left() + conv_info.pad_right()) / 2.0f);
79 const int num_tiles_y = std::ceil((in_h - 2 + conv_info.pad_top() + conv_info.pad_bottom()) / 2.0f);
80
81 ARM_COMPUTE_ERROR_ON((num_tiles_x * num_tiles_y) != static_cast<int>(dst.shape().y()));
82
83 for(int b = 0; b < num_batches; ++b)
84 {
85 for(int z = 0; z < in_d; ++z)
86 {
87 for(int y = 0; y < num_tiles_y; ++y)
88 {
89 for(int x = 0; x < num_tiles_x; ++x)
90 {
91 int xi = x * 2 - conv_info.pad_left();
92 int yi = y * 2 - conv_info.pad_top();
93
94 // Get the 4x4 tile from the input tensor
95 get_tile(src, src_tile, Coordinates(xi, yi, z, b));
96
97 // Compute the transformation
98 matrix_multiply(matrix, src_tile, tmp_tile);
99 matrix_multiply(tmp_tile, matrix_transposed, dst_tile);
100
101 // Store the 4x4 output tile across the 16 channels
102 for(int i = 0; i < 16; ++i)
103 {
104 int xo = z;
105 int yo = x + y * num_tiles_x;
106 dst[coords2index(dst.shape(), Coordinates(xo, yo, i, b))] = dst_tile[i];
107 }
108 }
109 }
110 }
111 }
112}
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000113
114template <typename T>
115void winograd_filter_transform3x3(const SimpleTensor<T> &in, SimpleTensor<T> &out)
116{
117 // Simple tensor for the 3x3 input tile
118 SimpleTensor<T> input_tile{ TensorShape(3u, 3u), in.data_type(), 1 };
119
120 // Simple tensor for the transformation matrix
121 SimpleTensor<T> trans_matrix{ TensorShape(3u, 4u), in.data_type(), 1 };
122
123 // Simple tensor for the transformation matrix transpose
124 SimpleTensor<T> trans_matrix_transposed{ TensorShape(4u, 3u), in.data_type(), 1 };
125
126 // Simple tensor for the 4x3 temporary tile
127 SimpleTensor<T> tmp_tile{ TensorShape(3u, 4u), in.data_type(), 1 };
128
129 // Simple tensor for the 4x4 output tile
130 SimpleTensor<T> output_tile{ TensorShape(4u, 4u), in.data_type(), 1 };
131
132 // Initialize transformation matrix
133 // 1 | 0 | 0
134 // 0.5 | 0.5 | 0.5
135 // 0.5 |-0.5 | 0.5
136 // 0 | 0 | 1
137 trans_matrix[0 + 0 * 3] = 1.0f;
138 trans_matrix[1 + 0 * 3] = 0.0f;
139 trans_matrix[2 + 0 * 3] = 0.0f;
140 trans_matrix[0 + 1 * 3] = 0.5f;
141 trans_matrix[1 + 1 * 3] = 0.5f;
142 trans_matrix[2 + 1 * 3] = 0.5f;
143 trans_matrix[0 + 2 * 3] = 0.5f;
144 trans_matrix[1 + 2 * 3] = -0.5f;
145 trans_matrix[2 + 2 * 3] = 0.5f;
146 trans_matrix[0 + 3 * 3] = 0.0f;
147 trans_matrix[1 + 3 * 3] = 0.0f;
148 trans_matrix[2 + 3 * 3] = 1.0f;
149
150 // Transpose the transformation matrix
151 transpose_matrix(trans_matrix, trans_matrix_transposed);
152
153 const int num_channels = in.shape()[2];
154 const int num_filters = in.shape()[3];
155 const int num_batches = in.shape().total_size() / (9 * num_channels * num_filters);
156
157 for(int n = 0; n < num_batches; ++n)
158 {
159 for(int w = 0; w < num_filters; ++w)
160 {
161 for(int z = 0; z < num_channels; ++z)
162 {
163 // Load the 3x3 tile from the input tensor
164 get_tile(in, input_tile, Coordinates(0, 0, z, w, n));
165
166 // First transformation
167 matrix_multiply(trans_matrix, input_tile, tmp_tile);
168
169 // Second transformation
170 matrix_multiply(tmp_tile, trans_matrix_transposed, output_tile);
171
172 // Store the 4x4 output tile across the 16 channels
173 const int output_offset = w + z * num_filters;
174 out[output_offset + 0 * num_filters * num_channels] = output_tile[0 + 0 * 4];
175 out[output_offset + 1 * num_filters * num_channels] = output_tile[1 + 0 * 4];
176 out[output_offset + 2 * num_filters * num_channels] = output_tile[2 + 0 * 4];
177 out[output_offset + 3 * num_filters * num_channels] = output_tile[3 + 0 * 4];
178 out[output_offset + 4 * num_filters * num_channels] = output_tile[0 + 1 * 4];
179 out[output_offset + 5 * num_filters * num_channels] = output_tile[1 + 1 * 4];
180 out[output_offset + 6 * num_filters * num_channels] = output_tile[2 + 1 * 4];
181 out[output_offset + 7 * num_filters * num_channels] = output_tile[3 + 1 * 4];
182 out[output_offset + 8 * num_filters * num_channels] = output_tile[0 + 2 * 4];
183 out[output_offset + 9 * num_filters * num_channels] = output_tile[1 + 2 * 4];
184 out[output_offset + 10 * num_filters * num_channels] = output_tile[2 + 2 * 4];
185 out[output_offset + 11 * num_filters * num_channels] = output_tile[3 + 2 * 4];
186 out[output_offset + 12 * num_filters * num_channels] = output_tile[0 + 3 * 4];
187 out[output_offset + 13 * num_filters * num_channels] = output_tile[1 + 3 * 4];
188 out[output_offset + 14 * num_filters * num_channels] = output_tile[2 + 3 * 4];
189 out[output_offset + 15 * num_filters * num_channels] = output_tile[3 + 3 * 4];
190 }
191 }
192 }
193}
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000194} // namespace
195
196template <typename T>
197SimpleTensor<T> winograd_input_transform(const SimpleTensor<T> &src, const TensorShape &dst_shape, const PadStrideInfo &conv_info, const Size2D &kernel_dims)
198{
199 ARM_COMPUTE_ERROR_ON(kernel_dims.width != kernel_dims.height);
200 ARM_COMPUTE_ERROR_ON(src.data_layout() != DataLayout::NCHW);
201
202 SimpleTensor<T> dst{ dst_shape, src.data_type() };
203
204 switch(kernel_dims.width)
205 {
206 case 3:
207 winograd_input_transform3x3(src, dst, conv_info);
208 break;
209 default:
210 ARM_COMPUTE_ERROR("Only 3x3 kernels are supported");
211 }
212
213 return dst;
214}
215
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000216template <typename T>
217SimpleTensor<T> winograd_filter_transform(const SimpleTensor<T> &in, const TensorShape &output_shape)
218{
219 ARM_COMPUTE_ERROR_ON_MSG(in.data_layout() != DataLayout::NCHW, "Only supported NCHW data format");
220
221 // Create reference
222 SimpleTensor<T> out{ output_shape, in.data_type(), 1 };
223
224 switch(in.shape()[0])
225 {
226 case 3:
227 winograd_filter_transform3x3(in, out);
228 break;
229 default:
230 ARM_COMPUTE_ERROR("Only supported 3x3 kernel");
231 break;
232 }
233
234 return out;
235}
236
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000237template SimpleTensor<float> winograd_input_transform(const SimpleTensor<float> &src, const TensorShape &dst_shape, const PadStrideInfo &conv_info, const Size2D &kernel_dims);
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000238template SimpleTensor<float> winograd_filter_transform(const SimpleTensor<float> &in, const TensorShape &output_shape);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000239} // namespace reference
240} // namespace validation
241} // namespace test
242} // namespace arm_compute