• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2019 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 
25 #include "arm.hpp"
26 #include "input.hpp"
27 
28 namespace winograd
29 {
30 
31 template <>
transform_tile(const int n_channels,const float * const input_base,const int,const int input_col_stride,float * outptr,const int matrix_stride)32 void InputTransform<1, 8, float, float, WinogradRoots::Integers>::transform_tile(
33   const int n_channels,
34   const float* const input_base,
35   const int,  // We don't need to stride over rows
36   const int input_col_stride,
37   float* outptr,
38   const int matrix_stride
39 )
40 {
41   constexpr int inner_tile_cols = 8;
42 
43   // Get pointers into the input tile
44   const float *x_ptrs[inner_tile_cols];
45   for (int j = 0, xj = 0; j < inner_tile_cols; j++, xj++)
46   {
47     x_ptrs[j] = input_base + xj*input_col_stride;
48   }
49 
50   // Vectors used/computed in this kernel.
51   float x[inner_tile_cols];
52   float U[inner_tile_cols];
53 
54   for (int j = 0; j < inner_tile_cols; j++)
55   {
56     x[j] = 0.0f;
57   }
58 
59   // Perform the Winograd input transformation for each channel in the input
60   // tensor.
61   int channels_remaining = n_channels;
62 #ifdef _arm_any_
63   for (; channels_remaining >= 4; channels_remaining -= 4)
64   {
65     float32x4_t x[inner_tile_cols], U[inner_tile_cols];
66     for (int j = 0; j < inner_tile_cols; j++)
67     {
68       x[j] = vdupq_n_f32(0.0f);
69     }
70 
71     // Load x
72     for (int j = 0; j < inner_tile_cols; j++)
73     {
74       x[j] = vld1q_f32(x_ptrs[j]);
75       x_ptrs[j] += 4;
76     }
77 
78     // Compute U = x . X
79     U[0] = vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmulq_n_f32(x[6], 1), x[2], 49), x[4], -14), x[0], -36);
80     U[1] = vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmulq_n_f32(x[6], 1), x[2], 36), x[3], 13), x[4], -13), x[1], -36), x[5], -1);
81     U[2] = vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmulq_n_f32(x[6], 1), x[5], 1), x[2], 36), x[1], 36), x[4], -13), x[3], -13);
82     U[3] = vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmulq_n_f32(x[6], 1), x[3], 20), x[2], 9), x[5], -2), x[4], -10), x[1], -18);
83     U[4] = vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmulq_n_f32(x[6], 1), x[1], 18), x[2], 9), x[5], 2), x[4], -10), x[3], -20);
84     U[5] = vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmulq_n_f32(x[6], 1), x[3], 15), x[2], 4), x[5], -3), x[4], -5), x[1], -12);
85     U[6] = vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmulq_n_f32(x[6], 1), x[1], 12), x[2], 4), x[5], 3), x[4], -5), x[3], -15);
86     U[7] = vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmulq_n_f32(x[7], 1), x[3], 49), x[5], -14), x[1], -36);
87 
88     // Store the transformed vector
89     for (int j = 0; j < inner_tile_cols; j++)
90     {
91       vst1q_f32(outptr + j*matrix_stride, U[j]);
92     }
93     outptr += 4;
94   }
95   for (; channels_remaining >= 2; channels_remaining -= 2)
96   {
97     float32x2_t x[inner_tile_cols], U[inner_tile_cols];
98     for (int j = 0; j < inner_tile_cols; j++)
99     {
100       x[j] = vdup_n_f32(0.0f);
101     }
102 
103     // Load x
104     for (int j = 0; j < inner_tile_cols; j++)
105     {
106       x[j] = vld1_f32(x_ptrs[j]);
107       x_ptrs[j] += 2;
108     }
109 
110     // Compute U = x . X
111     U[0] = vmla_n_f32(vmla_n_f32(vmla_n_f32(vmul_n_f32(x[6], 1), x[2], 49), x[4], -14), x[0], -36);
112     U[1] = vmla_n_f32(vmla_n_f32(vmla_n_f32(vmla_n_f32(vmla_n_f32(vmul_n_f32(x[6], 1), x[2], 36), x[3], 13), x[4], -13), x[1], -36), x[5], -1);
113     U[2] = vmla_n_f32(vmla_n_f32(vmla_n_f32(vmla_n_f32(vmla_n_f32(vmul_n_f32(x[6], 1), x[5], 1), x[2], 36), x[1], 36), x[4], -13), x[3], -13);
114     U[3] = vmla_n_f32(vmla_n_f32(vmla_n_f32(vmla_n_f32(vmla_n_f32(vmul_n_f32(x[6], 1), x[3], 20), x[2], 9), x[5], -2), x[4], -10), x[1], -18);
115     U[4] = vmla_n_f32(vmla_n_f32(vmla_n_f32(vmla_n_f32(vmla_n_f32(vmul_n_f32(x[6], 1), x[1], 18), x[2], 9), x[5], 2), x[4], -10), x[3], -20);
116     U[5] = vmla_n_f32(vmla_n_f32(vmla_n_f32(vmla_n_f32(vmla_n_f32(vmul_n_f32(x[6], 1), x[3], 15), x[2], 4), x[5], -3), x[4], -5), x[1], -12);
117     U[6] = vmla_n_f32(vmla_n_f32(vmla_n_f32(vmla_n_f32(vmla_n_f32(vmul_n_f32(x[6], 1), x[1], 12), x[2], 4), x[5], 3), x[4], -5), x[3], -15);
118     U[7] = vmla_n_f32(vmla_n_f32(vmla_n_f32(vmul_n_f32(x[7], 1), x[3], 49), x[5], -14), x[1], -36);
119 
120     // Store the transformed vector
121     for (int j = 0; j < inner_tile_cols; j++)
122     {
123       vst1_f32(outptr + j*matrix_stride, U[j]);
124     }
125     outptr += 2;
126   }
127 #endif  // _arm_any_
128   for (; channels_remaining; channels_remaining--)
129   {
130     // Load x
131     for (int j = 0; j < inner_tile_cols; j++)
132     {
133       x[j] = *(x_ptrs[j]++);
134     }
135 
136     // Compute U = x . X
137     U[0] = x[0]*-36 + x[4]*-14 + x[2]*49 + x[6]*1;
138     U[1] = x[5]*-1 + x[1]*-36 + x[4]*-13 + x[3]*13 + x[2]*36 + x[6]*1;
139     U[2] = x[3]*-13 + x[4]*-13 + x[1]*36 + x[2]*36 + x[5]*1 + x[6]*1;
140     U[3] = x[1]*-18 + x[4]*-10 + x[5]*-2 + x[2]*9 + x[3]*20 + x[6]*1;
141     U[4] = x[3]*-20 + x[4]*-10 + x[5]*2 + x[2]*9 + x[1]*18 + x[6]*1;
142     U[5] = x[1]*-12 + x[4]*-5 + x[5]*-3 + x[2]*4 + x[3]*15 + x[6]*1;
143     U[6] = x[3]*-15 + x[4]*-5 + x[5]*3 + x[2]*4 + x[1]*12 + x[6]*1;
144     U[7] = x[1]*-36 + x[5]*-14 + x[3]*49 + x[7]*1;
145 
146     // Store the transformed vector
147     for (int j = 0; j < inner_tile_cols; j++)
148     {
149       *(outptr + j*matrix_stride) = U[j];
150     }
151     outptr++;
152   }
153 }
154 
155 template class InputTransform<1, 8, float, float, WinogradRoots::Integers>;
156 template class InputTransform<8, 1, float, float, WinogradRoots::Integers>;
157 
158 }  // namespace winograd
159