blob: 05f06a81ee3eceae8dce7acdbf6ad554822c3fe3 [file] [log] [blame]
Pablo Tellobda6e4b2018-08-22 11:40:33 +01001/*
Pablo Tello5264b7d2019-10-21 14:25:41 +01002 * Copyright (c) 2017-2019 ARM Limited.
Pablo Tellobda6e4b2018-08-22 11:40:33 +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 "output.hpp"
26#include "arm.hpp"
Pablo Tellobda6e4b2018-08-22 11:40:33 +010027
Pablo Tello8f43d742019-03-27 09:28:32 +000028namespace winograd
Pablo Tellobda6e4b2018-08-22 11:40:33 +010029{
30
Pablo Tello8f43d742019-03-27 09:28:32 +000031template <>
32void OutputTransform<1, 3, 1, 8, float, float, WinogradRoots::Integers>::transform_tile(
Pablo Tellobda6e4b2018-08-22 11:40:33 +010033 const int n_channels,
Pablo Tello8f43d742019-03-27 09:28:32 +000034 const float* inptr,
Pablo Tellobda6e4b2018-08-22 11:40:33 +010035 const int matrix_stride,
Pablo Tello8f43d742019-03-27 09:28:32 +000036 const float* bptr,
Pablo Tellobda6e4b2018-08-22 11:40:33 +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 Tellobda6e4b2018-08-22 11:40:33 +010042)
43{
Pablo Tellobda6e4b2018-08-22 11:40:33 +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 Tellobda6e4b2018-08-22 11:40:33 +010047 {
48 outptrs[j] = output + j*output_col_stride;
49 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +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(vmulq_n_f32(F[2], 1), F[6], 3), F[4], 2), F[3], -2), F[5], -3), F[1], -1);
68 f[2] = vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmulq_n_f32(F[2], 1), F[1], 1), F[6], 9), F[5], 9), F[4], 4), F[3], 4);
69 f[3] = vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmulq_n_f32(F[2], 1), F[6], 27), F[4], 8), F[3], -8), F[5], -27), F[1], -1);
70 f[4] = vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmlaq_n_f32(vmulq_n_f32(F[2], 1), F[1], 1), F[6], 81), F[5], 81), F[4], 16), F[3], 16);
71 f[5] = 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], 243), F[4], 32), F[3], -32), F[5], -243), F[1], -1);
72
73 // Write out the output tile
74 if (bptr != 0)
75 {
76 b = vld1q_f32(bptr);
77 bptr += 4;
78 }
Pablo Tello8f43d742019-03-27 09:28:32 +000079 for (int j = 0; j < output_tile_cols; j++)
Pablo Tellobda6e4b2018-08-22 11:40:33 +010080 {
Pablo Tello5264b7d2019-10-21 14:25:41 +010081 const auto y = vminq_f32(vmaxq_f32(f[j] + b, vdupq_n_f32(output_min)),
82 vdupq_n_f32(output_max));
83 vst1q_f32(outptrs[j], y);
Pablo Tellobda6e4b2018-08-22 11:40:33 +010084 outptrs[j] += 4;
85 }
86 }
87 for (; channels_remaining >= 2; channels_remaining -= 2)
88 {
89 // Matrices used and computed during this transform
90 float32x2_t F[inner_tile_cols], f[output_tile_cols], b = vdup_n_f32(0.0f);
91
92 // Read a 1x8 tile in the Winograd domain
93 for (int j = 0; j < inner_tile_cols; j++)
94 {
95 F[j] = vld1_f32(inptr + j*matrix_stride);
96 }
97 inptr += 2;
98
99 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);
100 f[1] = vmla_n_f32(vmla_n_f32(vmla_n_f32(vmla_n_f32(vmla_n_f32(vmul_n_f32(F[2], 1), F[6], 3), F[4], 2), F[3], -2), F[5], -3), F[1], -1);
101 f[2] = vmla_n_f32(vmla_n_f32(vmla_n_f32(vmla_n_f32(vmla_n_f32(vmul_n_f32(F[2], 1), F[1], 1), F[6], 9), F[5], 9), F[4], 4), F[3], 4);
102 f[3] = vmla_n_f32(vmla_n_f32(vmla_n_f32(vmla_n_f32(vmla_n_f32(vmul_n_f32(F[2], 1), F[6], 27), F[4], 8), F[3], -8), F[5], -27), F[1], -1);
103 f[4] = vmla_n_f32(vmla_n_f32(vmla_n_f32(vmla_n_f32(vmla_n_f32(vmul_n_f32(F[2], 1), F[1], 1), F[6], 81), F[5], 81), F[4], 16), F[3], 16);
104 f[5] = 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], 243), F[4], 32), F[3], -32), F[5], -243), F[1], -1);
105
106 // Write out the output tile
107 if (bptr != 0)
108 {
109 b = vld1_f32(bptr);
110 bptr += 2;
111 }
Pablo Tello8f43d742019-03-27 09:28:32 +0000112 for (int j = 0; j < output_tile_cols; j++)
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100113 {
Pablo Tello5264b7d2019-10-21 14:25:41 +0100114 const auto y = vmin_f32(vmax_f32(f[j] + b, vdup_n_f32(output_min)),
115 vdup_n_f32(output_max));
116 vst1_f32(outptrs[j], y);
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100117 outptrs[j] += 2;
118 }
119 }
120#endif // __arm_any__
121 for (; channels_remaining; channels_remaining--)
122 {
123 // Matrices used and computed during this transform
124 float F[inner_tile_cols], f[output_tile_cols], b = 0.0f;
125
126 // Read a 1x8 tile in the Winograd domain
127 for (int j = 0; j < inner_tile_cols; j++)
128 {
129 F[j] = *(inptr + j*matrix_stride);
130 }
131 inptr++;
132
133 f[0] = F[0]*1 + F[1]*1 + F[2]*1 + F[3]*1 + F[4]*1 + F[5]*1 + F[6]*1;
134 f[1] = F[1]*-1 + F[5]*-3 + F[3]*-2 + F[4]*2 + F[6]*3 + F[2]*1;
135 f[2] = F[3]*4 + F[4]*4 + F[5]*9 + F[6]*9 + F[1]*1 + F[2]*1;
136 f[3] = F[1]*-1 + F[5]*-27 + F[3]*-8 + F[4]*8 + F[6]*27 + F[2]*1;
137 f[4] = F[3]*16 + F[4]*16 + F[5]*81 + F[6]*81 + F[1]*1 + F[2]*1;
138 f[5] = F[1]*-1 + F[5]*-243 + F[3]*-32 + F[4]*32 + F[6]*243 + F[2]*1 + F[7]*1;
139
140 // Write out the output tile
141 if (bptr != 0)
142 {
143 b = *(bptr++);
144 }
Pablo Tello8f43d742019-03-27 09:28:32 +0000145 for (int j = 0; j < output_tile_cols; j++)
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100146 {
Pablo Tello5264b7d2019-10-21 14:25:41 +0100147 *(outptrs[j]++) = std::max(std::min(f[j] + b, output_max), output_min);
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100148 }
149 }
150}
151
Pablo Tello8f43d742019-03-27 09:28:32 +0000152template class OutputTransform<1, 3, 1, 8, float, float, WinogradRoots::Integers>;
153template class OutputTransform<3, 1, 8, 1, float, float, WinogradRoots::Integers>;
Pablo Tellod3d97d22018-10-05 10:59:48 +0100154
Pablo Tello8f43d742019-03-27 09:28:32 +0000155} // namespace