blob: 8e257909a37dd12be8171d034af306c9933101c1 [file] [log] [blame]
Pablo Tello000d33a2018-09-03 16:59:20 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2019 Arm Limited.
Pablo Tello000d33a2018-09-03 16:59:20 +01003 *
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
Pablo Tello8f43d742019-03-27 09:28:32 +000025#include "arm.hpp"
26#include "output.hpp"
Pablo Tello000d33a2018-09-03 16:59:20 +010027
Pablo Tello8f43d742019-03-27 09:28:32 +000028namespace winograd
Pablo Tello000d33a2018-09-03 16:59:20 +010029{
30
Pablo Tello8f43d742019-03-27 09:28:32 +000031template <>
32void OutputTransform<1, 7, 1, 8, float, float, WinogradRoots::Integers>::transform_tile(
Pablo Tello000d33a2018-09-03 16:59:20 +010033 const int n_channels,
Pablo Tello8f43d742019-03-27 09:28:32 +000034 const float* inptr,
Pablo Tello000d33a2018-09-03 16:59:20 +010035 const int matrix_stride,
Pablo Tello8f43d742019-03-27 09:28:32 +000036 const float* bptr,
Pablo Tello000d33a2018-09-03 16:59:20 +010037 float* const output,
Pablo Tello8f43d742019-03-27 09:28:32 +000038 const int, // No need to stride across rows
Pablo Tello5264b7d2019-10-21 14:25:41 +010039 const int output_col_stride,
40 const float output_min,
41 const float output_max
Pablo Tello000d33a2018-09-03 16:59:20 +010042)
43{
Pablo Tello000d33a2018-09-03 16:59:20 +010044 // Construct a map to the output cells
Pablo Tello8f43d742019-03-27 09:28:32 +000045 float *outptrs[output_tile_cols];
46 for (int j = 0; j < output_tile_cols; j++)
Pablo Tello000d33a2018-09-03 16:59:20 +010047 {
48 outptrs[j] = output + j*output_col_stride;
49 }
Pablo Tello000d33a2018-09-03 16:59:20 +010050
51 // For each channel of the output
52 int channels_remaining = n_channels;
53#ifdef __arm_any__
54 for (; channels_remaining >= 4; channels_remaining -= 4)
55 {
56 // Matrices used and computed during this transform
57 float32x4_t F[inner_tile_cols], f[output_tile_cols], b = vdupq_n_f32(0.0f);
58
59 // Read a 1x8 tile in the Winograd domain
60 for (int j = 0; j < inner_tile_cols; j++)
61 {
62 F[j] = vld1q_f32(inptr + j*matrix_stride);
63 }
64 inptr += 4;
65
66 f[0] = vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmulq_n_f32(F[6], 1), F[5], 1), F[4], 1), F[3], 1), F[2], 1), F[1], 1), F[0], 1);
67 f[1] = vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmulq_n_f32(F[7], 1), F[2], 1), F[6], 3), F[4], 2), F[3], -2), F[5], -3), F[1], -1);
68
69 // Write out the output tile
70 if (bptr != 0)
71 {
72 b = vld1q_f32(bptr);
73 bptr += 4;
74 }
Pablo Tello8f43d742019-03-27 09:28:32 +000075 for (int j = 0; j < output_tile_cols; j++)
Pablo Tello000d33a2018-09-03 16:59:20 +010076 {
Pablo Tello5264b7d2019-10-21 14:25:41 +010077 const auto y = vminq_f32(vmaxq_f32(f[j] + b, vdupq_n_f32(output_min)),
78 vdupq_n_f32(output_max));
79 vst1q_f32(outptrs[j], y);
Pablo Tello000d33a2018-09-03 16:59:20 +010080 outptrs[j] += 4;
81 }
82 }
83 for (; channels_remaining >= 2; channels_remaining -= 2)
84 {
85 // Matrices used and computed during this transform
86 float32x2_t F[inner_tile_cols], f[output_tile_cols], b = vdup_n_f32(0.0f);
87
88 // Read a 1x8 tile in the Winograd domain
89 for (int j = 0; j < inner_tile_cols; j++)
90 {
91 F[j] = vld1_f32(inptr + j*matrix_stride);
92 }
93 inptr += 2;
94
95 f[0] = vmla_n_f32(vmla_n_f32(vmla_n_f32(vmla_n_f32(vmla_n_f32(vmla_n_f32(vmul_n_f32(F[6], 1), F[5], 1), F[4], 1), F[3], 1), F[2], 1), F[1], 1), F[0], 1);
96 f[1] = vmla_n_f32(vmla_n_f32(vmla_n_f32(vmla_n_f32(vmla_n_f32(vmla_n_f32(vmul_n_f32(F[7], 1), F[2], 1), F[6], 3), F[4], 2), F[3], -2), F[5], -3), F[1], -1);
97
98 // Write out the output tile
99 if (bptr != 0)
100 {
101 b = vld1_f32(bptr);
102 bptr += 2;
103 }
Pablo Tello8f43d742019-03-27 09:28:32 +0000104 for (int j = 0; j < output_tile_cols; j++)
Pablo Tello000d33a2018-09-03 16:59:20 +0100105 {
Pablo Tello5264b7d2019-10-21 14:25:41 +0100106 const auto y = vmin_f32(vmax_f32(f[j] + b, vdup_n_f32(output_min)),
107 vdup_n_f32(output_max));
108 vst1_f32(outptrs[j], y);
Pablo Tello000d33a2018-09-03 16:59:20 +0100109 outptrs[j] += 2;
110 }
111 }
112#endif // __arm_any__
113 for (; channels_remaining; channels_remaining--)
114 {
115 // Matrices used and computed during this transform
116 float F[inner_tile_cols], f[output_tile_cols], b = 0.0f;
117
118 // Read a 1x8 tile in the Winograd domain
119 for (int j = 0; j < inner_tile_cols; j++)
120 {
121 F[j] = *(inptr + j*matrix_stride);
122 }
123 inptr++;
124
125 f[0] = F[0]*1 + F[1]*1 + F[2]*1 + F[3]*1 + F[4]*1 + F[5]*1 + F[6]*1;
126 f[1] = F[1]*-1 + F[5]*-3 + F[3]*-2 + F[4]*2 + F[6]*3 + F[2]*1 + F[7]*1;
127
128 // Write out the output tile
129 if (bptr != 0)
130 {
131 b = *(bptr++);
132 }
Pablo Tello8f43d742019-03-27 09:28:32 +0000133 for (int j = 0; j < output_tile_cols; j++)
Pablo Tello000d33a2018-09-03 16:59:20 +0100134 {
Pablo Tello5264b7d2019-10-21 14:25:41 +0100135 *(outptrs[j]++) = std::max(std::min(f[j] + b, output_max), output_min);
Pablo Tello000d33a2018-09-03 16:59:20 +0100136 }
137 }
138}
Pablo Tellod3d97d22018-10-05 10:59:48 +0100139
Pablo Tello8f43d742019-03-27 09:28:32 +0000140template class OutputTransform<1, 7, 1, 8, float, float, WinogradRoots::Integers>;
141template class OutputTransform<7, 1, 8, 1, float, float, WinogradRoots::Integers>;
Pablo Tello000d33a2018-09-03 16:59:20 +0100142
Pablo Tello000d33a2018-09-03 16:59:20 +0100143} // namespace winograd