1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2018-2020 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "Winograd.h"
25*c217d954SCole Faust
26*c217d954SCole Faust #include "tests/validation/Helpers.h"
27*c217d954SCole Faust #include "tests/validation/reference/Utils.h"
28*c217d954SCole Faust
29*c217d954SCole Faust #include "arm_compute/core/Types.h"
30*c217d954SCole Faust
31*c217d954SCole Faust #include <algorithm>
32*c217d954SCole Faust #include <cmath>
33*c217d954SCole Faust
34*c217d954SCole Faust namespace arm_compute
35*c217d954SCole Faust {
36*c217d954SCole Faust namespace test
37*c217d954SCole Faust {
38*c217d954SCole Faust namespace validation
39*c217d954SCole Faust {
40*c217d954SCole Faust namespace reference
41*c217d954SCole Faust {
42*c217d954SCole Faust namespace
43*c217d954SCole Faust {
44*c217d954SCole Faust template <typename T>
initialize_matrix_transform(SimpleTensor<T> & src,const Size2D & output_tile_size,const Size2D & kernel_size,WinogradTransformType winograd_transform_type)45*c217d954SCole Faust void initialize_matrix_transform(SimpleTensor<T> &src, const Size2D &output_tile_size, const Size2D &kernel_size, WinogradTransformType winograd_transform_type)
46*c217d954SCole Faust {
47*c217d954SCole Faust // Winograd input transform matrices
48*c217d954SCole Faust static const std::array<float, 16> imatrix2x2_3x3 =
49*c217d954SCole Faust {
50*c217d954SCole Faust 1.0f, 0.0f, -1.0f, 0.0f,
51*c217d954SCole Faust 0.0f, 1.0f, 1.0f, 0.0f,
52*c217d954SCole Faust 0.0f, -1.0f, 1.0f, 0.0f,
53*c217d954SCole Faust 0.0f, 1.0f, 0.0f, -1.0f
54*c217d954SCole Faust };
55*c217d954SCole Faust
56*c217d954SCole Faust static const std::array<float, 36> imatrix4x4_3x3 =
57*c217d954SCole Faust {
58*c217d954SCole Faust 4.0f, 0.0f, -5.0f, 0.0f, 1.0f, 0.0f,
59*c217d954SCole Faust 0.0f, -4.0f, -4.0f, 1.0f, 1.0f, 0.0f,
60*c217d954SCole Faust 0.0f, 4.0f, -4.0f, -1.0f, 1.0f, 0.0f,
61*c217d954SCole Faust 0.0f, -2.0f, -1.0f, 2.0f, 1.0f, 0.0f,
62*c217d954SCole Faust 0.0f, 2.0f, -1.0f, -2.0f, 1.0f, 0.0f,
63*c217d954SCole Faust 0.0f, 4.0f, 0.0f, -5.0f, 0.0f, 1.0f,
64*c217d954SCole Faust };
65*c217d954SCole Faust
66*c217d954SCole Faust static const std::array<float, 64> imatrix4x4_5x5 =
67*c217d954SCole Faust {
68*c217d954SCole Faust 1.f, 0.f, -21.f / 4.f, 0.f, 21.f / 4.f, 0.f, -1.f, 0.f,
69*c217d954SCole Faust 0.f, 1.f, 1.f, -17.f / 4.f, -17.f / 4.f, 1.f, 1.f, 0.f,
70*c217d954SCole Faust 0.f, -1.f, 1.f, 17.f / 4.f, -17.f / 4.f, -1.f, 1.f, 0.f,
71*c217d954SCole Faust 0.f, 1.f / 2.f, 1.f / 4.f, -5.f / 2.f, -5.f / 4.f, 2.f, 1.f, 0.f,
72*c217d954SCole Faust 0.f, -1.f / 2.f, 1.f / 4.f, 5.f / 2.f, -5.f / 4.f, -2.f, 1.f, 0.f,
73*c217d954SCole Faust 0.f, 2.f, 4.f, -5.f / 2.f, -5.f, 1.f / 2.f, 1.f, 0.f,
74*c217d954SCole Faust 0.f, -2.f, 4.f, 5.f / 2.f, -5.f, -1.f / 2.f, 1.f, 0.f,
75*c217d954SCole Faust 0.f, -1.f, 0.f, 21.f / 4.f, 0.f, -21.f / 4.f, 0.f, 1.f
76*c217d954SCole Faust };
77*c217d954SCole Faust
78*c217d954SCole Faust static const std::array<float, 64> imatrix2x1_7x7 =
79*c217d954SCole Faust {
80*c217d954SCole Faust -36.0f, 0.0f, 49.0f, 0.0f, -14.0f, 0.0f, 1.0f, 0.0f,
81*c217d954SCole Faust 0.0f, -36.0f, 36.0f, 13.0f, -13.0f, -1.0f, 1.0f, 0.0f,
82*c217d954SCole Faust 0.0f, 36.0f, 36.0f, -13.0f, -13.0f, 1.0f, 1.0f, 0.0f,
83*c217d954SCole Faust 0.0f, -18.0f, 9.0f, 20.0f, -10.0f, -2.0f, 1.0f, 0.0f,
84*c217d954SCole Faust 0.0f, 18.0f, 9.0f, -20.0f, -10.0f, 2.0f, 1.0f, 0.0f,
85*c217d954SCole Faust 0.0f, -12.0f, 4.0f, 15.0f, -5.0f, -3.0f, 1.0f, 0.0f,
86*c217d954SCole Faust 0.0f, 12.0f, 4.0f, -15.0f, -5.0f, 3.0f, 1.0f, 0.0f,
87*c217d954SCole Faust 0.0f, -36.0f, 0.0f, 49.0f, 0.0f, -14.0f, 0.0f, 1.0f
88*c217d954SCole Faust };
89*c217d954SCole Faust
90*c217d954SCole Faust // ------------------------------------------
91*c217d954SCole Faust
92*c217d954SCole Faust // Winograd filter transform matrices
93*c217d954SCole Faust static const std::array<float, 12> fmatrix2x2_3x3 =
94*c217d954SCole Faust {
95*c217d954SCole Faust 1.0f, 0.0f, 0.0f,
96*c217d954SCole Faust 0.5f, 0.5f, 0.5f,
97*c217d954SCole Faust 0.5f, -0.5f, 0.5f,
98*c217d954SCole Faust 0.0f, 0.0f, 1.0f
99*c217d954SCole Faust };
100*c217d954SCole Faust
101*c217d954SCole Faust static const std::array<float, 18> fmatrix4x4_3x3 =
102*c217d954SCole Faust {
103*c217d954SCole Faust 0.25f, 0.0f, 0.0f,
104*c217d954SCole Faust -1.0f / 6.0f, -1.0f / 6.0f, -1.0f / 6.0f,
105*c217d954SCole Faust -1.0f / 6.0f, 1.0f / 6.0f, -1.0f / 6.0f,
106*c217d954SCole Faust 1.0f / 24.0f, 1.0f / 12.0f, 1.0f / 6.0f,
107*c217d954SCole Faust 1.0f / 24.0f, -1.0f / 12.0f, 1.0f / 6.0f,
108*c217d954SCole Faust 0.0f, 0.0f, 1.0f
109*c217d954SCole Faust };
110*c217d954SCole Faust
111*c217d954SCole Faust static const std::array<float, 40> fmatrix4x4_5x5 =
112*c217d954SCole Faust {
113*c217d954SCole Faust 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
114*c217d954SCole Faust -2.0f / 9.0f, -2.0f / 9.0f, -2.0f / 9.0f, -2.0f / 9.0f, -2.0f / 9.0f,
115*c217d954SCole Faust -2.0f / 9.0f, 2.0f / 9.0f, -2.0f / 9.0f, 2.0f / 9.0f, -2.0f / 9.0f,
116*c217d954SCole Faust 1.0f / 90.0f, 1.0f / 45.0f, 2.0f / 45.0f, 4.0f / 45.0f, 8.0f / 45.0f,
117*c217d954SCole Faust 1.0f / 90.0f, -1.0f / 45.0f, 2.0f / 45.0f, -4.0f / 45.0f, 8.0f / 45.0f,
118*c217d954SCole Faust 4.0f / 45.0f, 2.0f / 45.0f, 1.0f / 45.0f, 1.0f / 90.0f, 1.0f / 180.0f,
119*c217d954SCole Faust 4.0f / 45.0f, -2.0f / 45.0f, 1.0f / 45.0f, -1.0f / 90.0f, 1.0f / 180.0f,
120*c217d954SCole Faust 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
121*c217d954SCole Faust
122*c217d954SCole Faust };
123*c217d954SCole Faust
124*c217d954SCole Faust static const std::array<float, 56> fmatrix2x1_7x7 =
125*c217d954SCole Faust {
126*c217d954SCole Faust -1.0f / 36.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
127*c217d954SCole Faust 1.0f / 48.0f, -1.0f / 48.0f, 1.0f / 48.0f, -1.0f / 48.0f, 1.0f / 48.0f, -1.0f / 48.0f, 1.0f / 48.0f,
128*c217d954SCole Faust 1.0f / 48.0f, 1.0f / 48.0f, 1.0f / 48.0f, 1.0f / 48.0f, 1.0f / 48.0f, 1.0f / 48.0f, 1.0f / 48.0f,
129*c217d954SCole Faust -1.0f / 120.0f, 1.0f / 60.0f, -1.0f / 30.0f, 1.0f / 15.0f, -2.0f / 15.0f, 4.0f / 15.0f, -8.0f / 15.0f,
130*c217d954SCole Faust -1.0f / 120.0f, -1.0f / 60.0f, -1.0f / 30.0f, -1.0f / 15.0f, -2.0f / 15.0f, -4.0f / 15.0f, -8.0f / 15.0f,
131*c217d954SCole Faust 1.0f / 720.0f, -1.0f / 240.0f, 1.0f / 80.0f, -3.0f / 80.0f, 9.0f / 80.0f, -27.0f / 80.0f, 81.0f / 80.0f,
132*c217d954SCole Faust 1.0f / 720.0f, 1.0f / 240.0f, 1.0f / 80.0f, 3.0f / 80.0f, 9.0f / 80.0f, 27.0f / 80.0f, 81.0f / 80.0f,
133*c217d954SCole Faust 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
134*c217d954SCole Faust };
135*c217d954SCole Faust
136*c217d954SCole Faust // ------------------------------------------
137*c217d954SCole Faust
138*c217d954SCole Faust // Winograd output transform matrices
139*c217d954SCole Faust static const std::array<float, 8> omatrix2x2_3x3 =
140*c217d954SCole Faust {
141*c217d954SCole Faust 1.0f, 1.0f, 1.0f, 0.0f,
142*c217d954SCole Faust 0.0f, 1.0f, -1.0f, -1.0f
143*c217d954SCole Faust };
144*c217d954SCole Faust
145*c217d954SCole Faust static const std::array<float, 24> omatrix4x4_3x3 =
146*c217d954SCole Faust {
147*c217d954SCole Faust 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
148*c217d954SCole Faust 0.0f, 1.0f, -1.0f, 2.0f, -2.0f, 0.0f,
149*c217d954SCole Faust 0.0f, 1.0f, 1.0f, 4.0f, 4.0f, 0.0f,
150*c217d954SCole Faust 0.0f, 1.0f, -1.0f, 8.0f, -8.0f, 1.0f
151*c217d954SCole Faust };
152*c217d954SCole Faust
153*c217d954SCole Faust static const std::array<float, 36> omatrix4x4_5x5 =
154*c217d954SCole Faust {
155*c217d954SCole Faust 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 8.0f, 8.0f, 0.0f,
156*c217d954SCole Faust 0.0f, 1.0f, -1.0f, 2.0f, -2.0f, 4.0f, -4.0f, 0.0f,
157*c217d954SCole Faust 0.0f, 1.0f, 1.0f, 4.0f, 4.0f, 2.0f, 2.0f, 0.0f,
158*c217d954SCole Faust 0.0f, 1.0f, -1.0f, 8.0f, -8.0f, 1.0f, -1.0f, 1.0f
159*c217d954SCole Faust };
160*c217d954SCole Faust
161*c217d954SCole Faust static const std::array<float, 16> omatrix2x1_7x7 =
162*c217d954SCole Faust {
163*c217d954SCole Faust 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
164*c217d954SCole Faust 0.0f, -1.0f, 1.0f, -2.0f, 2.0f, -3.0f, 3.0f, 1.0f
165*c217d954SCole Faust };
166*c217d954SCole Faust
167*c217d954SCole Faust // ------------------------------------------
168*c217d954SCole Faust
169*c217d954SCole Faust using WinogradKey = std::tuple<std::pair<int, int>, std::pair<int, int>, WinogradTransformType>;
170*c217d954SCole Faust
171*c217d954SCole Faust // Key = (Output tile size, Kernel size, Winograd transform type)
172*c217d954SCole Faust static std::map<WinogradKey, const float *> matrix_map =
173*c217d954SCole Faust {
174*c217d954SCole Faust { WinogradKey(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3), WinogradTransformType::INPUT), imatrix2x2_3x3.data() },
175*c217d954SCole Faust { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3), WinogradTransformType::INPUT), imatrix4x4_3x3.data() },
176*c217d954SCole Faust { WinogradKey(std::pair<int, int>(2, 1), std::pair<int, int>(3, 1), WinogradTransformType::INPUT), imatrix2x2_3x3.data() },
177*c217d954SCole Faust { WinogradKey(std::pair<int, int>(4, 1), std::pair<int, int>(3, 1), WinogradTransformType::INPUT), imatrix4x4_3x3.data() },
178*c217d954SCole Faust { WinogradKey(std::pair<int, int>(1, 2), std::pair<int, int>(1, 3), WinogradTransformType::INPUT), imatrix2x2_3x3.data() },
179*c217d954SCole Faust { WinogradKey(std::pair<int, int>(1, 4), std::pair<int, int>(1, 3), WinogradTransformType::INPUT), imatrix4x4_3x3.data() },
180*c217d954SCole Faust { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5), WinogradTransformType::INPUT), imatrix4x4_5x5.data() },
181*c217d954SCole Faust { WinogradKey(std::pair<int, int>(4, 1), std::pair<int, int>(5, 1), WinogradTransformType::INPUT), imatrix4x4_5x5.data() },
182*c217d954SCole Faust { WinogradKey(std::pair<int, int>(2, 1), std::pair<int, int>(7, 1), WinogradTransformType::INPUT), imatrix2x1_7x7.data() },
183*c217d954SCole Faust { WinogradKey(std::pair<int, int>(1, 2), std::pair<int, int>(1, 7), WinogradTransformType::INPUT), imatrix2x1_7x7.data() },
184*c217d954SCole Faust { WinogradKey(std::pair<int, int>(2, 2), std::pair<int, int>(7, 7), WinogradTransformType::INPUT), imatrix2x1_7x7.data() },
185*c217d954SCole Faust { WinogradKey(std::pair<int, int>(1, 4), std::pair<int, int>(1, 5), WinogradTransformType::INPUT), imatrix4x4_5x5.data() },
186*c217d954SCole Faust { WinogradKey(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3), WinogradTransformType::FILTER), fmatrix2x2_3x3.data() },
187*c217d954SCole Faust { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3), WinogradTransformType::FILTER), fmatrix4x4_3x3.data() },
188*c217d954SCole Faust { WinogradKey(std::pair<int, int>(2, 1), std::pair<int, int>(3, 1), WinogradTransformType::FILTER), fmatrix2x2_3x3.data() },
189*c217d954SCole Faust { WinogradKey(std::pair<int, int>(4, 1), std::pair<int, int>(3, 1), WinogradTransformType::FILTER), fmatrix4x4_3x3.data() },
190*c217d954SCole Faust { WinogradKey(std::pair<int, int>(1, 2), std::pair<int, int>(1, 3), WinogradTransformType::FILTER), fmatrix2x2_3x3.data() },
191*c217d954SCole Faust { WinogradKey(std::pair<int, int>(1, 4), std::pair<int, int>(1, 3), WinogradTransformType::FILTER), fmatrix4x4_3x3.data() },
192*c217d954SCole Faust { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5), WinogradTransformType::FILTER), fmatrix4x4_5x5.data() },
193*c217d954SCole Faust { WinogradKey(std::pair<int, int>(4, 1), std::pair<int, int>(5, 1), WinogradTransformType::FILTER), fmatrix4x4_5x5.data() },
194*c217d954SCole Faust { WinogradKey(std::pair<int, int>(2, 1), std::pair<int, int>(7, 1), WinogradTransformType::FILTER), fmatrix2x1_7x7.data() },
195*c217d954SCole Faust { WinogradKey(std::pair<int, int>(1, 2), std::pair<int, int>(1, 7), WinogradTransformType::FILTER), fmatrix2x1_7x7.data() },
196*c217d954SCole Faust { WinogradKey(std::pair<int, int>(2, 2), std::pair<int, int>(7, 7), WinogradTransformType::FILTER), fmatrix2x1_7x7.data() },
197*c217d954SCole Faust { WinogradKey(std::pair<int, int>(1, 4), std::pair<int, int>(1, 5), WinogradTransformType::FILTER), fmatrix4x4_5x5.data() },
198*c217d954SCole Faust { WinogradKey(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3), WinogradTransformType::OUTPUT), omatrix2x2_3x3.data() },
199*c217d954SCole Faust { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3), WinogradTransformType::OUTPUT), omatrix4x4_3x3.data() },
200*c217d954SCole Faust { WinogradKey(std::pair<int, int>(2, 1), std::pair<int, int>(3, 1), WinogradTransformType::OUTPUT), omatrix2x2_3x3.data() },
201*c217d954SCole Faust { WinogradKey(std::pair<int, int>(4, 1), std::pair<int, int>(3, 1), WinogradTransformType::OUTPUT), omatrix4x4_3x3.data() },
202*c217d954SCole Faust { WinogradKey(std::pair<int, int>(1, 2), std::pair<int, int>(1, 3), WinogradTransformType::OUTPUT), omatrix2x2_3x3.data() },
203*c217d954SCole Faust { WinogradKey(std::pair<int, int>(1, 4), std::pair<int, int>(1, 3), WinogradTransformType::OUTPUT), omatrix4x4_3x3.data() },
204*c217d954SCole Faust { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5), WinogradTransformType::OUTPUT), omatrix4x4_5x5.data() },
205*c217d954SCole Faust { WinogradKey(std::pair<int, int>(4, 1), std::pair<int, int>(5, 1), WinogradTransformType::OUTPUT), omatrix4x4_5x5.data() },
206*c217d954SCole Faust { WinogradKey(std::pair<int, int>(2, 1), std::pair<int, int>(7, 1), WinogradTransformType::OUTPUT), omatrix2x1_7x7.data() },
207*c217d954SCole Faust { WinogradKey(std::pair<int, int>(1, 2), std::pair<int, int>(1, 7), WinogradTransformType::OUTPUT), omatrix2x1_7x7.data() },
208*c217d954SCole Faust { WinogradKey(std::pair<int, int>(2, 2), std::pair<int, int>(7, 7), WinogradTransformType::OUTPUT), omatrix2x1_7x7.data() },
209*c217d954SCole Faust { WinogradKey(std::pair<int, int>(1, 4), std::pair<int, int>(1, 5), WinogradTransformType::OUTPUT), omatrix4x4_5x5.data() },
210*c217d954SCole Faust };
211*c217d954SCole Faust
212*c217d954SCole Faust // Find transformation matrix
213*c217d954SCole Faust std::map<WinogradKey, const float *>::iterator it;
214*c217d954SCole Faust
215*c217d954SCole Faust it = matrix_map.find(WinogradKey(std::pair<int, int>(output_tile_size.width, output_tile_size.height),
216*c217d954SCole Faust std::pair<int, int>(kernel_size.width, kernel_size.height),
217*c217d954SCole Faust winograd_transform_type));
218*c217d954SCole Faust
219*c217d954SCole Faust float const *matrix_values = nullptr;
220*c217d954SCole Faust if(it != matrix_map.end())
221*c217d954SCole Faust {
222*c217d954SCole Faust // Get matrix pointer
223*c217d954SCole Faust matrix_values = it->second;
224*c217d954SCole Faust }
225*c217d954SCole Faust else
226*c217d954SCole Faust {
227*c217d954SCole Faust ARM_COMPUTE_ERROR("Winograd configuration not supported");
228*c217d954SCole Faust }
229*c217d954SCole Faust
230*c217d954SCole Faust // Copy values
231*c217d954SCole Faust std::copy(&matrix_values[0], &matrix_values[0] + src.num_elements(), &src[0]);
232*c217d954SCole Faust }
233*c217d954SCole Faust } // namespace
234*c217d954SCole Faust
235*c217d954SCole Faust template <typename T>
winograd_input_transform(const SimpleTensor<T> & in,const TensorShape & output_shape,const WinogradInfo & winograd_info)236*c217d954SCole Faust SimpleTensor<T> winograd_input_transform(const SimpleTensor<T> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info)
237*c217d954SCole Faust {
238*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(in.data_layout() != DataLayout::NCHW);
239*c217d954SCole Faust
240*c217d954SCole Faust const PadStrideInfo conv_info = winograd_info.convolution_info;
241*c217d954SCole Faust const Size2D output_tile_size = winograd_info.output_tile_size;
242*c217d954SCole Faust const Size2D kernel_size = winograd_info.kernel_size;
243*c217d954SCole Faust
244*c217d954SCole Faust SimpleTensor<T> out{ output_shape, in.data_type() };
245*c217d954SCole Faust
246*c217d954SCole Faust // Calculate dimensions for the tile
247*c217d954SCole Faust const unsigned int tile_w = output_tile_size.width + kernel_size.width - 1;
248*c217d954SCole Faust const unsigned int tile_h = output_tile_size.height + kernel_size.height - 1;
249*c217d954SCole Faust
250*c217d954SCole Faust // Get the maximum dimension from the tile size
251*c217d954SCole Faust const unsigned int tile_max_dim = std::max(tile_w, tile_h);
252*c217d954SCole Faust
253*c217d954SCole Faust TensorShape tile_dims(tile_max_dim, tile_max_dim);
254*c217d954SCole Faust
255*c217d954SCole Faust // Simple tensor for the input tile
256*c217d954SCole Faust SimpleTensor<T> src_tile{ tile_dims, in.data_type() };
257*c217d954SCole Faust
258*c217d954SCole Faust // Simple tensor for the temporary tile
259*c217d954SCole Faust SimpleTensor<T> tmp_tile{ tile_dims, in.data_type() };
260*c217d954SCole Faust
261*c217d954SCole Faust // Simple tensor for the output tile
262*c217d954SCole Faust SimpleTensor<T> dst_tile{ tile_dims, in.data_type() };
263*c217d954SCole Faust
264*c217d954SCole Faust // Simple tensor for the transformation matrix
265*c217d954SCole Faust SimpleTensor<T> matrix{ tile_dims, in.data_type() };
266*c217d954SCole Faust
267*c217d954SCole Faust // Simple tensor for the transformation matrix transposed
268*c217d954SCole Faust SimpleTensor<T> matrix_transposed{ tile_dims, in.data_type() };
269*c217d954SCole Faust
270*c217d954SCole Faust // Initialize matrix for the input transform
271*c217d954SCole Faust initialize_matrix_transform(matrix, output_tile_size, kernel_size, WinogradTransformType::INPUT);
272*c217d954SCole Faust
273*c217d954SCole Faust // Transpose matrix
274*c217d954SCole Faust transpose_matrix<T>(matrix, matrix_transposed);
275*c217d954SCole Faust
276*c217d954SCole Faust const int in_w = in.shape().x();
277*c217d954SCole Faust const int in_h = in.shape().y();
278*c217d954SCole Faust const int in_d = in.shape().z();
279*c217d954SCole Faust const int out_d = out.shape().z();
280*c217d954SCole Faust const int num_batches = in.shape().total_size() / (in_w * in_h * in_d);
281*c217d954SCole Faust const int step_x = output_tile_size.width;
282*c217d954SCole Faust const int step_y = output_tile_size.height;
283*c217d954SCole Faust
284*c217d954SCole Faust // Compute the number of output tiles along the x and y direction of size "output_tile_size"
285*c217d954SCole Faust const Size2D num_tiles = compute_winograd_convolution_tiles(Size2D(in_w, in_h),
286*c217d954SCole Faust kernel_size,
287*c217d954SCole Faust output_tile_size,
288*c217d954SCole Faust conv_info);
289*c217d954SCole Faust
290*c217d954SCole Faust const int num_tiles_x = num_tiles.width;
291*c217d954SCole Faust const int num_tiles_y = num_tiles.height;
292*c217d954SCole Faust
293*c217d954SCole Faust // In case of 1D convolution, the input tile has to be partially filled with zeros
294*c217d954SCole Faust int start_x_zero = 0;
295*c217d954SCole Faust int start_y_zero = 0;
296*c217d954SCole Faust int end_x_zero = 0;
297*c217d954SCole Faust int end_y_zero = 0;
298*c217d954SCole Faust
299*c217d954SCole Faust if(output_tile_size.width == 1)
300*c217d954SCole Faust {
301*c217d954SCole Faust start_x_zero = 1;
302*c217d954SCole Faust start_y_zero = 0;
303*c217d954SCole Faust end_x_zero = tile_max_dim - 1;
304*c217d954SCole Faust end_y_zero = tile_max_dim;
305*c217d954SCole Faust }
306*c217d954SCole Faust else if(output_tile_size.height == 1)
307*c217d954SCole Faust {
308*c217d954SCole Faust start_x_zero = 0;
309*c217d954SCole Faust start_y_zero = 1;
310*c217d954SCole Faust end_x_zero = tile_max_dim;
311*c217d954SCole Faust end_y_zero = tile_max_dim - 1;
312*c217d954SCole Faust }
313*c217d954SCole Faust
314*c217d954SCole Faust // Set the anchor and shape of the zeros area
315*c217d954SCole Faust const Coordinates anchor_zeros(start_x_zero, start_y_zero);
316*c217d954SCole Faust const TensorShape shape_zeros(end_x_zero, end_y_zero);
317*c217d954SCole Faust
318*c217d954SCole Faust // If we have a vertical filter (i.e. 1x3, 1x5,..), we need to take the elements along the y direction (step = width of the output tile)
319*c217d954SCole Faust const int step_y_transf_tile = kernel_size.width == 1 ? tile_max_dim : 1;
320*c217d954SCole Faust
321*c217d954SCole Faust ARM_COMPUTE_ERROR_ON((num_tiles_x * num_tiles_y) != static_cast<int>(out.shape().y()));
322*c217d954SCole Faust
323*c217d954SCole Faust for(int b = 0; b < num_batches; ++b)
324*c217d954SCole Faust {
325*c217d954SCole Faust for(int z = 0; z < in_d; ++z)
326*c217d954SCole Faust {
327*c217d954SCole Faust for(int y = 0; y < num_tiles_y; ++y)
328*c217d954SCole Faust {
329*c217d954SCole Faust for(int x = 0; x < num_tiles_x; ++x)
330*c217d954SCole Faust {
331*c217d954SCole Faust int xi = x * step_x - conv_info.pad_left();
332*c217d954SCole Faust int yi = y * step_y - conv_info.pad_top();
333*c217d954SCole Faust
334*c217d954SCole Faust // Get the tile from the input tensor
335*c217d954SCole Faust get_tile<T>(in, src_tile, Coordinates(xi, yi, z, b));
336*c217d954SCole Faust
337*c217d954SCole Faust // Fill partially with zeros in case of 1D convolution
338*c217d954SCole Faust zeros<T>(src_tile, anchor_zeros, shape_zeros);
339*c217d954SCole Faust
340*c217d954SCole Faust // Compute the transformation
341*c217d954SCole Faust matrix_multiply<T>(matrix, src_tile, tmp_tile);
342*c217d954SCole Faust matrix_multiply<T>(tmp_tile, matrix_transposed, dst_tile);
343*c217d954SCole Faust
344*c217d954SCole Faust // Store the output tile across the channels
345*c217d954SCole Faust for(int i = 0; i < out_d; ++i)
346*c217d954SCole Faust {
347*c217d954SCole Faust int xo = z;
348*c217d954SCole Faust int yo = x + y * num_tiles_x;
349*c217d954SCole Faust out[coords2index(out.shape(), Coordinates(xo, yo, i, b))] = dst_tile[i * step_y_transf_tile];
350*c217d954SCole Faust }
351*c217d954SCole Faust }
352*c217d954SCole Faust }
353*c217d954SCole Faust }
354*c217d954SCole Faust }
355*c217d954SCole Faust
356*c217d954SCole Faust return out;
357*c217d954SCole Faust }
358*c217d954SCole Faust
359*c217d954SCole Faust template <typename T>
winograd_filter_transform(const SimpleTensor<T> & in,const TensorShape & output_shape,const WinogradInfo & winograd_info)360*c217d954SCole Faust SimpleTensor<T> winograd_filter_transform(const SimpleTensor<T> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info)
361*c217d954SCole Faust {
362*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_MSG(in.data_layout() != DataLayout::NCHW, "Only supported NCHW data format");
363*c217d954SCole Faust
364*c217d954SCole Faust // Create reference
365*c217d954SCole Faust SimpleTensor<T> out{ output_shape, in.data_type(), 1 };
366*c217d954SCole Faust
367*c217d954SCole Faust const Size2D output_tile_size = winograd_info.output_tile_size;
368*c217d954SCole Faust const Size2D kernel_size = winograd_info.kernel_size;
369*c217d954SCole Faust
370*c217d954SCole Faust // Calculate dimensions for the tile
371*c217d954SCole Faust const unsigned int input_tile_w = output_tile_size.width + kernel_size.width - 1;
372*c217d954SCole Faust const unsigned int input_tile_h = output_tile_size.height + kernel_size.height - 1;
373*c217d954SCole Faust const unsigned int input_tile_area = input_tile_w * input_tile_h;
374*c217d954SCole Faust
375*c217d954SCole Faust // Get the maximum dimension from the filter size
376*c217d954SCole Faust const unsigned int kernel_max_dim = std::max(kernel_size.width, kernel_size.height);
377*c217d954SCole Faust
378*c217d954SCole Faust // Get the maximum dimension from the input tile
379*c217d954SCole Faust const unsigned int input_tile_max_dim = std::max(input_tile_w, input_tile_h);
380*c217d954SCole Faust
381*c217d954SCole Faust // Simple tensor for the input tile
382*c217d954SCole Faust SimpleTensor<T> input_tile{ TensorShape(kernel_max_dim, kernel_max_dim), in.data_type(), 1 };
383*c217d954SCole Faust
384*c217d954SCole Faust // Simple tensor for the transformation matrix
385*c217d954SCole Faust SimpleTensor<T> trans_matrix{ TensorShape(kernel_max_dim, input_tile_max_dim), in.data_type(), 1 };
386*c217d954SCole Faust
387*c217d954SCole Faust // Simple tensor for the transformation matrix transpose
388*c217d954SCole Faust SimpleTensor<T> trans_matrix_transposed{ TensorShape(input_tile_max_dim, kernel_max_dim), in.data_type(), 1 };
389*c217d954SCole Faust
390*c217d954SCole Faust // Simple tensor for the temporary tile
391*c217d954SCole Faust SimpleTensor<T> tmp_tile{ TensorShape(kernel_max_dim, input_tile_max_dim), in.data_type(), 1 };
392*c217d954SCole Faust
393*c217d954SCole Faust // Simple tensor for the output tile
394*c217d954SCole Faust SimpleTensor<T> transf_tile{ TensorShape(input_tile_max_dim, input_tile_max_dim), in.data_type(), 1 };
395*c217d954SCole Faust
396*c217d954SCole Faust // Initialize matrix for the filter transform
397*c217d954SCole Faust initialize_matrix_transform(trans_matrix, output_tile_size, kernel_size, WinogradTransformType::FILTER);
398*c217d954SCole Faust
399*c217d954SCole Faust // Transpose the transformation matrix
400*c217d954SCole Faust transpose_matrix<T>(trans_matrix, trans_matrix_transposed);
401*c217d954SCole Faust
402*c217d954SCole Faust const int num_channels = in.shape()[2];
403*c217d954SCole Faust const int num_filters = in.shape()[3];
404*c217d954SCole Faust const int num_batches = in.shape().total_size() / (kernel_size.area() * num_channels * num_filters);
405*c217d954SCole Faust
406*c217d954SCole Faust // If we have a vertical filter (i.e. 1x3, 1x5,..), we need to take the elements along the y direction (step_y_transf_tile = width of the output tile)
407*c217d954SCole Faust const int step_y_transf_tile = kernel_size.width == 1 ? input_tile_max_dim : 1;
408*c217d954SCole Faust
409*c217d954SCole Faust for(int n = 0; n < num_batches; ++n)
410*c217d954SCole Faust {
411*c217d954SCole Faust for(int w = 0; w < num_filters; ++w)
412*c217d954SCole Faust {
413*c217d954SCole Faust for(int z = 0; z < num_channels; ++z)
414*c217d954SCole Faust {
415*c217d954SCole Faust // Load the tile from the input tensor
416*c217d954SCole Faust get_tile<T>(in, input_tile, Coordinates(0, 0, z, w, n));
417*c217d954SCole Faust
418*c217d954SCole Faust // First transformation
419*c217d954SCole Faust matrix_multiply<T>(trans_matrix, input_tile, tmp_tile);
420*c217d954SCole Faust
421*c217d954SCole Faust // Second transformation
422*c217d954SCole Faust matrix_multiply<T>(tmp_tile, trans_matrix_transposed, transf_tile);
423*c217d954SCole Faust
424*c217d954SCole Faust // Store the output tile across the channels
425*c217d954SCole Faust const int output_offset = w + z * num_filters;
426*c217d954SCole Faust
427*c217d954SCole Faust // Store the values across the channels
428*c217d954SCole Faust for(unsigned int i = 0; i < input_tile_area; ++i)
429*c217d954SCole Faust {
430*c217d954SCole Faust out[output_offset + i * num_filters * num_channels] = transf_tile[i * step_y_transf_tile];
431*c217d954SCole Faust }
432*c217d954SCole Faust }
433*c217d954SCole Faust }
434*c217d954SCole Faust }
435*c217d954SCole Faust
436*c217d954SCole Faust return out;
437*c217d954SCole Faust }
438*c217d954SCole Faust
439*c217d954SCole Faust template <typename T>
winograd_output_transform(const SimpleTensor<T> & in,const SimpleTensor<T> & b,const TensorShape & output_shape,const WinogradInfo & winograd_info)440*c217d954SCole Faust SimpleTensor<T> winograd_output_transform(const SimpleTensor<T> &in, const SimpleTensor<T> &b, const TensorShape &output_shape, const WinogradInfo &winograd_info)
441*c217d954SCole Faust {
442*c217d954SCole Faust const PadStrideInfo conv_info = winograd_info.convolution_info;
443*c217d954SCole Faust const Size2D input_dimensions = winograd_info.input_dimensions;
444*c217d954SCole Faust const Size2D output_tile_size = winograd_info.output_tile_size;
445*c217d954SCole Faust const Size2D kernel_size = winograd_info.kernel_size;
446*c217d954SCole Faust
447*c217d954SCole Faust // Create reference
448*c217d954SCole Faust SimpleTensor<T> out{ output_shape, in.data_type(), 1 };
449*c217d954SCole Faust
450*c217d954SCole Faust // Calculate dimensions for the tiles
451*c217d954SCole Faust const unsigned int in_tile_w = output_tile_size.width + kernel_size.width - 1;
452*c217d954SCole Faust const unsigned int in_tile_h = output_tile_size.height + kernel_size.height - 1;
453*c217d954SCole Faust const unsigned int out_tile_w = output_tile_size.width;
454*c217d954SCole Faust const unsigned int out_tile_h = output_tile_size.height;
455*c217d954SCole Faust
456*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(in.shape()[2] != (in_tile_w * in_tile_h));
457*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(in.shape()[0] != out.shape()[get_data_layout_dimension_index(winograd_info.output_data_layout, DataLayoutDimension::CHANNEL)]);
458*c217d954SCole Faust
459*c217d954SCole Faust // Get the maximum dimension from the tile size
460*c217d954SCole Faust const unsigned int in_tile_max_dim = std::max(in_tile_w, in_tile_h);
461*c217d954SCole Faust const unsigned int out_tile_max_dim = std::max(output_tile_size.width, output_tile_size.height);
462*c217d954SCole Faust
463*c217d954SCole Faust // Compute tile dimensions
464*c217d954SCole Faust // Input tile dimensions
465*c217d954SCole Faust TensorShape in_tile_dims(in_tile_max_dim, in_tile_max_dim);
466*c217d954SCole Faust
467*c217d954SCole Faust // Output tile dimensions
468*c217d954SCole Faust TensorShape out_tile_dims(out_tile_max_dim, out_tile_max_dim);
469*c217d954SCole Faust
470*c217d954SCole Faust // Transformation matrix dimensions
471*c217d954SCole Faust TensorShape tr_tile_dims(in_tile_max_dim, out_tile_max_dim);
472*c217d954SCole Faust
473*c217d954SCole Faust // Create tensors
474*c217d954SCole Faust // Simple tensor for the input tile
475*c217d954SCole Faust SimpleTensor<T> input_tile{ in_tile_dims, in.data_type(), 1 };
476*c217d954SCole Faust
477*c217d954SCole Faust // Simple tensor for the transformation matrix
478*c217d954SCole Faust SimpleTensor<T> trans_matrix{ tr_tile_dims, in.data_type(), 1 };
479*c217d954SCole Faust
480*c217d954SCole Faust // Simple tensor for the transformation matrix transpose
481*c217d954SCole Faust SimpleTensor<T> trans_matrix_transposed{ TensorShape(tr_tile_dims[1], tr_tile_dims[0]), in.data_type(), 1 };
482*c217d954SCole Faust
483*c217d954SCole Faust // Simple tensor for the temporary tile
484*c217d954SCole Faust SimpleTensor<T> tmp_tile{ tr_tile_dims, in.data_type(), 1 };
485*c217d954SCole Faust
486*c217d954SCole Faust // Simple tensor for the output tile
487*c217d954SCole Faust SimpleTensor<T> output_tile{ out_tile_dims, in.data_type(), 1 };
488*c217d954SCole Faust
489*c217d954SCole Faust // Initialize matrix for the output transform
490*c217d954SCole Faust initialize_matrix_transform(trans_matrix, output_tile_size, kernel_size, WinogradTransformType::OUTPUT);
491*c217d954SCole Faust
492*c217d954SCole Faust // Transpose the transformation matrix
493*c217d954SCole Faust transpose_matrix<T>(trans_matrix, trans_matrix_transposed);
494*c217d954SCole Faust
495*c217d954SCole Faust const int w_in = in.shape()[0];
496*c217d954SCole Faust const int h_in = in.shape()[1];
497*c217d954SCole Faust const int c_in = in.shape()[2];
498*c217d954SCole Faust const int w_out = out.shape()[0];
499*c217d954SCole Faust const int h_out = out.shape()[1];
500*c217d954SCole Faust const int c_out = out.shape()[2];
501*c217d954SCole Faust const int num_batches = in.shape().total_size() / (w_in * h_in * c_in);
502*c217d954SCole Faust
503*c217d954SCole Faust // Input strides
504*c217d954SCole Faust const int stridey_in = w_in;
505*c217d954SCole Faust const int stridez_in = stridey_in * h_in;
506*c217d954SCole Faust const int stridew_in = stridez_in * c_in;
507*c217d954SCole Faust
508*c217d954SCole Faust // Output strides
509*c217d954SCole Faust const int stridey_out = w_out;
510*c217d954SCole Faust const int stridez_out = stridey_out * h_out;
511*c217d954SCole Faust const int stridew_out = stridez_out * c_out;
512*c217d954SCole Faust
513*c217d954SCole Faust // Compute the number of output tiles along the x and y direction of size "output_tile_size"
514*c217d954SCole Faust const Size2D num_tiles = compute_winograd_convolution_tiles(Size2D(input_dimensions.width, input_dimensions.height),
515*c217d954SCole Faust kernel_size,
516*c217d954SCole Faust output_tile_size,
517*c217d954SCole Faust conv_info);
518*c217d954SCole Faust
519*c217d954SCole Faust const int num_tiles_x = num_tiles.width;
520*c217d954SCole Faust const int num_tiles_y = num_tiles.height;
521*c217d954SCole Faust
522*c217d954SCole Faust ARM_COMPUTE_UNUSED(num_tiles_y);
523*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(in.shape()[1] != static_cast<unsigned int>(num_tiles_x * num_tiles_y));
524*c217d954SCole Faust
525*c217d954SCole Faust // If we have a vertical filter (i.e. 1x3, 1x5,..), we still need to take the elements along the x direction (step_y_transf_tile = 1)
526*c217d954SCole Faust const int step_y_transf_tile = kernel_size.width == 1 ? 1 : output_tile.shape()[0];
527*c217d954SCole Faust
528*c217d954SCole Faust // Initialize with zeros the input tile
529*c217d954SCole Faust zeros<T>(input_tile, Coordinates(0, 0), input_tile.shape());
530*c217d954SCole Faust
531*c217d954SCole Faust for(int n = 0; n < num_batches; ++n)
532*c217d954SCole Faust {
533*c217d954SCole Faust for(int y = 0; y < h_in; ++y)
534*c217d954SCole Faust {
535*c217d954SCole Faust for(int x = 0; x < w_in; ++x)
536*c217d954SCole Faust {
537*c217d954SCole Faust // Load the input tile tile across the channels of the input tensor
538*c217d954SCole Faust for(int z = 0; z < c_in; ++z)
539*c217d954SCole Faust {
540*c217d954SCole Faust input_tile[z] = in[x + (y * stridey_in) + (z * stridez_in) + (n * stridew_in)];
541*c217d954SCole Faust }
542*c217d954SCole Faust
543*c217d954SCole Faust // First transformation
544*c217d954SCole Faust matrix_multiply<T>(trans_matrix, input_tile, tmp_tile);
545*c217d954SCole Faust
546*c217d954SCole Faust // Second transformation
547*c217d954SCole Faust matrix_multiply<T>(tmp_tile, trans_matrix_transposed, output_tile);
548*c217d954SCole Faust
549*c217d954SCole Faust // Store the output tile
550*c217d954SCole Faust const int xo = (y % num_tiles_x) * out_tile_w;
551*c217d954SCole Faust const int yo = (y / num_tiles_x) * out_tile_h;
552*c217d954SCole Faust const int zo = x;
553*c217d954SCole Faust
554*c217d954SCole Faust const int output_offset = xo + (yo * stridey_out) + (zo * stridez_out) + (n * stridew_out);
555*c217d954SCole Faust
556*c217d954SCole Faust for(int yi = 0; yi < static_cast<int>(out_tile_h); ++yi)
557*c217d954SCole Faust {
558*c217d954SCole Faust for(int xi = 0; xi < static_cast<int>(out_tile_w); ++xi)
559*c217d954SCole Faust {
560*c217d954SCole Faust // Check out-of-bound writes
561*c217d954SCole Faust if((xo + xi < w_out) && (yo + yi < h_out))
562*c217d954SCole Faust {
563*c217d954SCole Faust out[output_offset + yi * stridey_out + xi] = output_tile[xi + yi * step_y_transf_tile];
564*c217d954SCole Faust
565*c217d954SCole Faust // Add bias
566*c217d954SCole Faust out[output_offset + yi * stridey_out + xi] += b[zo];
567*c217d954SCole Faust }
568*c217d954SCole Faust }
569*c217d954SCole Faust }
570*c217d954SCole Faust }
571*c217d954SCole Faust }
572*c217d954SCole Faust }
573*c217d954SCole Faust
574*c217d954SCole Faust return out;
575*c217d954SCole Faust }
576*c217d954SCole Faust
577*c217d954SCole Faust template SimpleTensor<float> winograd_filter_transform(const SimpleTensor<float> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info);
578*c217d954SCole Faust template SimpleTensor<float> winograd_input_transform(const SimpleTensor<float> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info);
579*c217d954SCole Faust template SimpleTensor<float> winograd_output_transform(const SimpleTensor<float> &in, const SimpleTensor<float> &b, const TensorShape &output_shape, const WinogradInfo &winograd_info);
580*c217d954SCole Faust template SimpleTensor<half> winograd_filter_transform(const SimpleTensor<half> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info);
581*c217d954SCole Faust template SimpleTensor<half> winograd_input_transform(const SimpleTensor<half> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info);
582*c217d954SCole Faust template SimpleTensor<half> winograd_output_transform(const SimpleTensor<half> &in, const SimpleTensor<half> &b, const TensorShape &output_shape, const WinogradInfo &winograd_info);
583*c217d954SCole Faust
584*c217d954SCole Faust } // namespace reference
585*c217d954SCole Faust } // namespace validation
586*c217d954SCole Faust } // namespace test
587*c217d954SCole Faust } // namespace arm_compute
588