blob: 371bb6348e78b2b52f81d8efd3473285d782d694 [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
29namespace arm_compute
30{
31namespace test
32{
33namespace validation
34{
35namespace reference
36{
37namespace
38{
39template <typename T>
40void winograd_input_transform3x3(const SimpleTensor<T> &src, SimpleTensor<T> &dst, const PadStrideInfo &conv_info)
41{
42 TensorShape shape4x4(4u, 4u);
43
44 // Simple tensor for the 4x4 input tile
45 SimpleTensor<T> src_tile{ shape4x4, src.data_type() };
46
47 // Simple tensor for the 4x4 temporary tile
48 SimpleTensor<T> tmp_tile{ shape4x4, src.data_type() };
49
50 // Simple tensor for the 4x4 output tile
51 SimpleTensor<T> dst_tile{ shape4x4, src.data_type() };
52
53 // Simple tensor for the transformation matrix
54 SimpleTensor<T> matrix{ shape4x4, src.data_type() };
55
56 // Simple tensor for the transformation matrix transposed
57 SimpleTensor<T> matrix_transposed{ shape4x4, src.data_type() };
58
59 const float matrix_values[] = { 1.f, 0.f, -1.f, 0.f,
60 0.f, 1.f, 1.f, 0.f,
61 0.f, -1.f, 1.f, 0.f,
62 0.f, 1.f, 0.f, -1.f
63 };
64
65 for(int i = 0; i < matrix.num_elements(); ++i)
66 {
67 matrix[i] = matrix_values[i];
68 }
69
70 transpose_matrix(matrix, matrix_transposed);
71
72 const int in_w = src.shape().x();
73 const int in_h = src.shape().y();
74 const int in_d = src.shape().z();
75 const int num_batches = src.shape().total_size() / (in_w * in_h * in_d);
76 const int num_tiles_x = std::ceil((in_w - 2 + conv_info.pad_left() + conv_info.pad_right()) / 2.0f);
77 const int num_tiles_y = std::ceil((in_h - 2 + conv_info.pad_top() + conv_info.pad_bottom()) / 2.0f);
78
79 ARM_COMPUTE_ERROR_ON((num_tiles_x * num_tiles_y) != static_cast<int>(dst.shape().y()));
80
81 for(int b = 0; b < num_batches; ++b)
82 {
83 for(int z = 0; z < in_d; ++z)
84 {
85 for(int y = 0; y < num_tiles_y; ++y)
86 {
87 for(int x = 0; x < num_tiles_x; ++x)
88 {
89 int xi = x * 2 - conv_info.pad_left();
90 int yi = y * 2 - conv_info.pad_top();
91
92 // Get the 4x4 tile from the input tensor
93 get_tile(src, src_tile, Coordinates(xi, yi, z, b));
94
95 // Compute the transformation
96 matrix_multiply(matrix, src_tile, tmp_tile);
97 matrix_multiply(tmp_tile, matrix_transposed, dst_tile);
98
99 // Store the 4x4 output tile across the 16 channels
100 for(int i = 0; i < 16; ++i)
101 {
102 int xo = z;
103 int yo = x + y * num_tiles_x;
104 dst[coords2index(dst.shape(), Coordinates(xo, yo, i, b))] = dst_tile[i];
105 }
106 }
107 }
108 }
109 }
110}
111} // namespace
112
113template <typename T>
114SimpleTensor<T> winograd_input_transform(const SimpleTensor<T> &src, const TensorShape &dst_shape, const PadStrideInfo &conv_info, const Size2D &kernel_dims)
115{
116 ARM_COMPUTE_ERROR_ON(kernel_dims.width != kernel_dims.height);
117 ARM_COMPUTE_ERROR_ON(src.data_layout() != DataLayout::NCHW);
118
119 SimpleTensor<T> dst{ dst_shape, src.data_type() };
120
121 switch(kernel_dims.width)
122 {
123 case 3:
124 winograd_input_transform3x3(src, dst, conv_info);
125 break;
126 default:
127 ARM_COMPUTE_ERROR("Only 3x3 kernels are supported");
128 }
129
130 return dst;
131}
132
133template SimpleTensor<float> winograd_input_transform(const SimpleTensor<float> &src, const TensorShape &dst_shape, const PadStrideInfo &conv_info, const Size2D &kernel_dims);
134} // namespace reference
135} // namespace validation
136} // namespace test
137} // namespace arm_compute