aboutsummaryrefslogtreecommitdiff
path: root/tests/validation/reference/Winograd.cpp
blob: 3ed55fb9fca740330a0d176861e7b77e8eb59da0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * Copyright (c) 2018 ARM Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include "Winograd.h"

#include "tests/validation/Helpers.h"
#include "tests/validation/reference/Utils.h"

#include "arm_compute/core/Types.h"

namespace arm_compute
{
namespace test
{
namespace validation
{
namespace reference
{
namespace
{
template <typename T>
void winograd_input_transform3x3(const SimpleTensor<T> &src, SimpleTensor<T> &dst, const PadStrideInfo &conv_info)
{
    TensorShape shape4x4(4u, 4u);

    // Simple tensor for the 4x4 input tile
    SimpleTensor<T> src_tile{ shape4x4, src.data_type() };

    // Simple tensor for the 4x4 temporary tile
    SimpleTensor<T> tmp_tile{ shape4x4, src.data_type() };

    // Simple tensor for the 4x4 output tile
    SimpleTensor<T> dst_tile{ shape4x4, src.data_type() };

    // Simple tensor for the transformation matrix
    SimpleTensor<T> matrix{ shape4x4, src.data_type() };

    // Simple tensor for the transformation matrix transposed
    SimpleTensor<T> matrix_transposed{ shape4x4, src.data_type() };

    const float matrix_values[] = { 1.f, 0.f, -1.f, 0.f,
                                    0.f, 1.f, 1.f, 0.f,
                                    0.f, -1.f, 1.f, 0.f,
                                    0.f, 1.f, 0.f, -1.f
                                  };

    for(int i = 0; i < matrix.num_elements(); ++i)
    {
        matrix[i] = matrix_values[i];
    }

    transpose_matrix(matrix, matrix_transposed);

    const int in_w        = src.shape().x();
    const int in_h        = src.shape().y();
    const int in_d        = src.shape().z();
    const int num_batches = src.shape().total_size() / (in_w * in_h * in_d);
    const int num_tiles_x = std::ceil((in_w - 2 + conv_info.pad_left() + conv_info.pad_right()) / 2.0f);
    const int num_tiles_y = std::ceil((in_h - 2 + conv_info.pad_top() + conv_info.pad_bottom()) / 2.0f);

    ARM_COMPUTE_ERROR_ON((num_tiles_x * num_tiles_y) != static_cast<int>(dst.shape().y()));

    for(int b = 0; b < num_batches; ++b)
    {
        for(int z = 0; z < in_d; ++z)
        {
            for(int y = 0; y < num_tiles_y; ++y)
            {
                for(int x = 0; x < num_tiles_x; ++x)
                {
                    int xi = x * 2 - conv_info.pad_left();
                    int yi = y * 2 - conv_info.pad_top();

                    // Get the 4x4 tile from the input tensor
                    get_tile(src, src_tile, Coordinates(xi, yi, z, b));

                    // Compute the transformation
                    matrix_multiply(matrix, src_tile, tmp_tile);
                    matrix_multiply(tmp_tile, matrix_transposed, dst_tile);

                    // Store the 4x4 output tile across the 16 channels
                    for(int i = 0; i < 16; ++i)
                    {
                        int xo = z;
                        int yo = x + y * num_tiles_x;
                        dst[coords2index(dst.shape(), Coordinates(xo, yo, i, b))] = dst_tile[i];
                    }
                }
            }
        }
    }
}

template <typename T>
void winograd_filter_transform3x3(const SimpleTensor<T> &in, SimpleTensor<T> &out)
{
    // Simple tensor for the 3x3 input tile
    SimpleTensor<T> input_tile{ TensorShape(3u, 3u), in.data_type(), 1 };

    // Simple tensor for the transformation matrix
    SimpleTensor<T> trans_matrix{ TensorShape(3u, 4u), in.data_type(), 1 };

    // Simple tensor for the transformation matrix transpose
    SimpleTensor<T> trans_matrix_transposed{ TensorShape(4u, 3u), in.data_type(), 1 };

    // Simple tensor for the 4x3 temporary tile
    SimpleTensor<T> tmp_tile{ TensorShape(3u, 4u), in.data_type(), 1 };

    // Simple tensor for the 4x4 output tile
    SimpleTensor<T> output_tile{ TensorShape(4u, 4u), in.data_type(), 1 };

    // Initialize transformation matrix
    // 1   | 0   | 0
    // 0.5 | 0.5 | 0.5
    // 0.5 |-0.5 | 0.5
    // 0   | 0   | 1
    trans_matrix[0 + 0 * 3] = 1.0f;
    trans_matrix[1 + 0 * 3] = 0.0f;
    trans_matrix[2 + 0 * 3] = 0.0f;
    trans_matrix[0 + 1 * 3] = 0.5f;
    trans_matrix[1 + 1 * 3] = 0.5f;
    trans_matrix[2 + 1 * 3] = 0.5f;
    trans_matrix[0 + 2 * 3] = 0.5f;
    trans_matrix[1 + 2 * 3] = -0.5f;
    trans_matrix[2 + 2 * 3] = 0.5f;
    trans_matrix[0 + 3 * 3] = 0.0f;
    trans_matrix[1 + 3 * 3] = 0.0f;
    trans_matrix[2 + 3 * 3] = 1.0f;

    // Transpose the transformation matrix
    transpose_matrix(trans_matrix, trans_matrix_transposed);

    const int num_channels = in.shape()[2];
    const int num_filters  = in.shape()[3];
    const int num_batches  = in.shape().total_size() / (9 * num_channels * num_filters);

    for(int n = 0; n < num_batches; ++n)
    {
        for(int w = 0; w < num_filters; ++w)
        {
            for(int z = 0; z < num_channels; ++z)
            {
                // Load the 3x3 tile from the input tensor
                get_tile(in, input_tile, Coordinates(0, 0, z, w, n));

                // First transformation
                matrix_multiply(trans_matrix, input_tile, tmp_tile);

                // Second transformation
                matrix_multiply(tmp_tile, trans_matrix_transposed, output_tile);

                // Store the 4x4 output tile across the 16 channels
                const int output_offset                              = w + z * num_filters;
                out[output_offset + 0 * num_filters * num_channels]  = output_tile[0 + 0 * 4];
                out[output_offset + 1 * num_filters * num_channels]  = output_tile[1 + 0 * 4];
                out[output_offset + 2 * num_filters * num_channels]  = output_tile[2 + 0 * 4];
                out[output_offset + 3 * num_filters * num_channels]  = output_tile[3 + 0 * 4];
                out[output_offset + 4 * num_filters * num_channels]  = output_tile[0 + 1 * 4];
                out[output_offset + 5 * num_filters * num_channels]  = output_tile[1 + 1 * 4];
                out[output_offset + 6 * num_filters * num_channels]  = output_tile[2 + 1 * 4];
                out[output_offset + 7 * num_filters * num_channels]  = output_tile[3 + 1 * 4];
                out[output_offset + 8 * num_filters * num_channels]  = output_tile[0 + 2 * 4];
                out[output_offset + 9 * num_filters * num_channels]  = output_tile[1 + 2 * 4];
                out[output_offset + 10 * num_filters * num_channels] = output_tile[2 + 2 * 4];
                out[output_offset + 11 * num_filters * num_channels] = output_tile[3 + 2 * 4];
                out[output_offset + 12 * num_filters * num_channels] = output_tile[0 + 3 * 4];
                out[output_offset + 13 * num_filters * num_channels] = output_tile[1 + 3 * 4];
                out[output_offset + 14 * num_filters * num_channels] = output_tile[2 + 3 * 4];
                out[output_offset + 15 * num_filters * num_channels] = output_tile[3 + 3 * 4];
            }
        }
    }
}
} // namespace

template <typename T>
SimpleTensor<T> winograd_input_transform(const SimpleTensor<T> &src, const TensorShape &dst_shape, const PadStrideInfo &conv_info, const Size2D &kernel_dims)
{
    ARM_COMPUTE_ERROR_ON(kernel_dims.width != kernel_dims.height);
    ARM_COMPUTE_ERROR_ON(src.data_layout() != DataLayout::NCHW);

    SimpleTensor<T> dst{ dst_shape, src.data_type() };

    switch(kernel_dims.width)
    {
        case 3:
            winograd_input_transform3x3(src, dst, conv_info);
            break;
        default:
            ARM_COMPUTE_ERROR("Only 3x3 kernels are supported");
    }

    return dst;
}

template <typename T>
SimpleTensor<T> winograd_filter_transform(const SimpleTensor<T> &in, const TensorShape &output_shape)
{
    ARM_COMPUTE_ERROR_ON_MSG(in.data_layout() != DataLayout::NCHW, "Only supported NCHW data format");

    // Create reference
    SimpleTensor<T> out{ output_shape, in.data_type(), 1 };

    switch(in.shape()[0])
    {
        case 3:
            winograd_filter_transform3x3(in, out);
            break;
        default:
            ARM_COMPUTE_ERROR("Only supported 3x3 kernel");
            break;
    }

    return out;
}

template SimpleTensor<float> winograd_input_transform(const SimpleTensor<float> &src, const TensorShape &dst_shape, const PadStrideInfo &conv_info, const Size2D &kernel_dims);
template SimpleTensor<float> winograd_filter_transform(const SimpleTensor<float> &in, const TensorShape &output_shape);
} // namespace reference
} // namespace validation
} // namespace test
} // namespace arm_compute