xref: /aosp_15_r20/external/ComputeLibrary/src/core/NEON/kernels/convolution/winograd/padding.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
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 #include <cstring>
25 #include <cstdint>
26 
27 #include "padding.hpp"
28 
29 namespace padding
30 {
31 template <typename T>
copy_and_pad_tile(unsigned int tile_rows,unsigned int tile_cols,unsigned int n_channels,const T * inptr,unsigned int in_row_stride,unsigned int in_col_stride,T * outptr,unsigned int out_row_stride,unsigned int out_col_stride,unsigned int pad_top,unsigned int pad_left,unsigned int pad_bottom,unsigned int pad_right,T pad_value)32 void copy_and_pad_tile(
33   unsigned int tile_rows,
34   unsigned int tile_cols,
35   unsigned int n_channels,
36   const T *inptr,
37   unsigned int in_row_stride,
38   unsigned int in_col_stride,
39   T* outptr,
40   unsigned int out_row_stride,
41   unsigned int out_col_stride,
42   unsigned int pad_top,
43   unsigned int pad_left,
44   unsigned int pad_bottom,
45   unsigned int pad_right,
46   T pad_value
47 )
48 {
49   for (unsigned int out_i = 0; out_i < tile_rows; out_i++)
50   {
51     for (unsigned int out_j = 0; out_j < tile_cols; out_j++)
52     {
53       T* const output = outptr + out_i*out_row_stride + out_j*out_col_stride;
54 
55       if (out_i < pad_top || tile_rows - pad_bottom <= out_i ||
56           out_j < pad_left || tile_cols - pad_right <= out_j)
57       {
58         for (unsigned int n = 0; n < n_channels; n++)
59         {
60           output[n] = pad_value;
61         }
62       }
63       else
64       {
65         const auto in_i = out_i - pad_top, in_j = out_j - pad_left;
66         const T* const input = inptr + in_i*in_row_stride + in_j*in_col_stride;
67         std::memcpy(output, input, n_channels * sizeof(T));
68       }
69     }
70   }
71 }
72 
73 template void copy_and_pad_tile(
74   unsigned int, unsigned int, unsigned int,
75   const uint8_t *, unsigned int, unsigned int,
76   uint8_t *, unsigned int, unsigned int,
77   unsigned int, unsigned int, unsigned int, unsigned int, uint8_t
78 );
79 
80 template void copy_and_pad_tile(
81   unsigned int, unsigned int, unsigned int,
82   const float *, unsigned int, unsigned int,
83   float *, unsigned int, unsigned int,
84   unsigned int, unsigned int, unsigned int, unsigned int, float
85 );
86 
87 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
88 template void copy_and_pad_tile(
89     unsigned int, unsigned int, unsigned int,
90     const __fp16 *, unsigned int, unsigned int,
91     __fp16 *, unsigned int, unsigned int,
92     unsigned int, unsigned int, unsigned int, unsigned int, __fp16
93 );
94 #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
95 
96 template <unsigned int TileRows, unsigned int TileCols>
execute(const size_t size,const void * const inptr,const size_t in_row_stride,const size_t in_col_stride,void * const outptr,const size_t out_row_stride,const size_t out_col_stride,const unsigned int pad_top,const unsigned int pad_left,const unsigned int pad_bottom,const unsigned int pad_right)97 void CopyCropped<TileRows, TileCols>::execute(
98   const size_t size,
99   const void * const inptr,
100   const size_t in_row_stride,
101   const size_t in_col_stride,
102   void * const outptr,
103   const size_t out_row_stride,
104   const size_t out_col_stride,
105   const unsigned int pad_top,
106   const unsigned int pad_left,
107   const unsigned int pad_bottom,
108   const unsigned int pad_right
109 )
110 {
111   for (unsigned int out_i = 0, in_i = pad_top; in_i < TileRows - pad_bottom; out_i++, in_i++)
112   {
113     for (unsigned int out_j = 0, in_j = pad_left; in_j < TileCols - pad_right; out_j++, in_j++)
114     {
115       std::memcpy(
116         static_cast<uint8_t *>(outptr) + out_i*out_row_stride + out_j*out_col_stride,
117         static_cast<const uint8_t *>(inptr) + in_i*in_row_stride + in_j*in_col_stride,
118         size
119       );
120     }
121   }
122 }
123 
124 template class CopyCropped<2, 2>;
125 template class CopyCropped<3, 3>;
126 template class CopyCropped<4, 4>;
127 
128 template <typename T>
crop_and_copy_tile(unsigned int tile_rows,unsigned int tile_cols,unsigned int n_channels,const T * inptr,unsigned int in_row_stride,unsigned int in_col_stride,T * outptr,unsigned int out_row_stride,unsigned int out_col_stride,unsigned int crop_top,unsigned int crop_left,unsigned int crop_bottom,unsigned int crop_right)129 void crop_and_copy_tile(
130   unsigned int tile_rows,
131   unsigned int tile_cols,
132   unsigned int n_channels,
133   const T *inptr,
134   unsigned int in_row_stride,
135   unsigned int in_col_stride,
136   T *outptr,
137   unsigned int out_row_stride,
138   unsigned int out_col_stride,
139   unsigned int crop_top,
140   unsigned int crop_left,
141   unsigned int crop_bottom,
142   unsigned int crop_right
143 )
144 {
145   for (unsigned int out_i = 0, in_i = crop_top; in_i < tile_rows - crop_bottom; out_i++, in_i++)
146   {
147     for (unsigned int out_j = 0, in_j = crop_left; in_j < tile_cols - crop_right; out_j++, in_j++)
148     {
149       std::memcpy(
150         outptr + out_i*out_row_stride + out_j*out_col_stride,
151         inptr + in_i*in_row_stride + in_j*in_col_stride,
152         sizeof(T) * n_channels
153       );
154     }
155   }
156 }
157 
158 template void crop_and_copy_tile(
159   unsigned int tile_rows,
160   unsigned int tile_cols,
161   unsigned int n_channels,
162   const float *inptr,
163   unsigned int in_row_stride,
164   unsigned int in_col_stride,
165   float *outptr,
166   unsigned int out_row_stride,
167   unsigned int out_col_stride,
168   unsigned int crop_top,
169   unsigned int crop_left,
170   unsigned int crop_bottom,
171   unsigned int crop_right
172 );
173 
174 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
175 template void crop_and_copy_tile(
176     unsigned int tile_rows,
177     unsigned int tile_cols,
178     unsigned int n_channels,
179     const __fp16 *inptr,
180     unsigned int in_row_stride,
181     unsigned int in_col_stride,
182     __fp16 *outptr,
183     unsigned int out_row_stride,
184     unsigned int out_col_stride,
185     unsigned int crop_top,
186     unsigned int crop_left,
187     unsigned int crop_bottom,
188     unsigned int crop_right
189 );
190 #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
191 }  // namespace padding
192