1 /*
2  * Copyright (c) 2022-2023 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 <cstddef>
26 #include <arm_neon.h>
27 
28 namespace arm_conv {
29 namespace winograd {
30 namespace input_transform {
31 
arm_fp32_1x8(unsigned int n_channels,const float * input_base,size_t,size_t input_col_stride,float * outptr,size_t matrix_stride)32 void arm_fp32_1x8(
33   unsigned int n_channels,
34   const float * input_base,
35   size_t,  // We don't need to stride over rows
36   size_t input_col_stride,
37   float *outptr,
38   size_t 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   for (; channels_remaining >= 4; channels_remaining -= 4)
63   {
64     float32x4_t x[inner_tile_cols], U[inner_tile_cols];
65     for (int j = 0; j < inner_tile_cols; j++)
66     {
67       x[j] = vdupq_n_f32(0.0f);
68     }
69 
70     // Load x
71     for (int j = 0; j < inner_tile_cols; j++)
72     {
73       x[j] = vld1q_f32(x_ptrs[j]);
74       x_ptrs[j] += 4;
75     }
76 
77     // Compute U = x . X
78     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);
79     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);
80     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);
81     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);
82     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);
83     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);
84     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);
85     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);
86 
87     // Store the transformed vector
88     for (int j = 0; j < inner_tile_cols; j++)
89     {
90       vst1q_f32(outptr + j*matrix_stride, U[j]);
91     }
92     outptr += 4;
93   }
94   for (; channels_remaining >= 2; channels_remaining -= 2)
95   {
96     float32x2_t x[inner_tile_cols], U[inner_tile_cols];
97     for (int j = 0; j < inner_tile_cols; j++)
98     {
99       x[j] = vdup_n_f32(0.0f);
100     }
101 
102     // Load x
103     for (int j = 0; j < inner_tile_cols; j++)
104     {
105       x[j] = vld1_f32(x_ptrs[j]);
106       x_ptrs[j] += 2;
107     }
108 
109     // Compute U = x . X
110     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);
111     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);
112     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);
113     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);
114     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);
115     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);
116     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);
117     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);
118 
119     // Store the transformed vector
120     for (int j = 0; j < inner_tile_cols; j++)
121     {
122       vst1_f32(outptr + j*matrix_stride, U[j]);
123     }
124     outptr += 2;
125   }
126   for (; channels_remaining; channels_remaining--)
127   {
128     // Load x
129     for (int j = 0; j < inner_tile_cols; j++)
130     {
131       x[j] = *(x_ptrs[j]++);
132     }
133 
134     // Compute U = x . X
135     U[0] = x[0]*-36 + x[4]*-14 + x[2]*49 + x[6]*1;
136     U[1] = x[5]*-1 + x[1]*-36 + x[4]*-13 + x[3]*13 + x[2]*36 + x[6]*1;
137     U[2] = x[3]*-13 + x[4]*-13 + x[1]*36 + x[2]*36 + x[5]*1 + x[6]*1;
138     U[3] = x[1]*-18 + x[4]*-10 + x[5]*-2 + x[2]*9 + x[3]*20 + x[6]*1;
139     U[4] = x[3]*-20 + x[4]*-10 + x[5]*2 + x[2]*9 + x[1]*18 + x[6]*1;
140     U[5] = x[1]*-12 + x[4]*-5 + x[5]*-3 + x[2]*4 + x[3]*15 + x[6]*1;
141     U[6] = x[3]*-15 + x[4]*-5 + x[5]*3 + x[2]*4 + x[1]*12 + x[6]*1;
142     U[7] = x[1]*-36 + x[5]*-14 + x[3]*49 + x[7]*1;
143 
144     // Store the transformed vector
145     for (int j = 0; j < inner_tile_cols; j++)
146     {
147       *(outptr + j*matrix_stride) = U[j];
148     }
149     outptr++;
150   }
151 }
152 
153 }  // namespace input_transform
154 }  // namespace winograd
155 }  // namespace arm_conv
156