Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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_compute/core/NEON/kernels/convolution/winograd/transforms/output.hpp" |
Pablo Tello | d3d97d2 | 2018-10-05 10:59:48 +0100 | [diff] [blame^] | 26 | #include "arm_compute/core/NEON/kernels/convolution/winograd/winograd_output_transform.hpp" |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/NEON/kernels/convolution/common/arm.hpp" |
| 28 | |
Pablo Tello | d3d97d2 | 2018-10-05 10:59:48 +0100 | [diff] [blame^] | 29 | namespace |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 30 | { |
| 31 | |
Pablo Tello | d3d97d2 | 2018-10-05 10:59:48 +0100 | [diff] [blame^] | 32 | template <bool Specialized, int PadRight=0> |
| 33 | void winograd_output_transform_2_7_fp32_process_tile( |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 34 | const int n_channels, |
| 35 | const float* const matrix_base, |
| 36 | const int matrix_stride, |
| 37 | const float* const biases, |
| 38 | float* const output, |
| 39 | const int output_row_stride, |
Pablo Tello | d3d97d2 | 2018-10-05 10:59:48 +0100 | [diff] [blame^] | 40 | const int output_col_stride, |
| 41 | const int _pad_bottom, |
| 42 | const int _pad_right |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 43 | ) |
| 44 | { |
| 45 | (void) output_row_stride; |
Pablo Tello | d3d97d2 | 2018-10-05 10:59:48 +0100 | [diff] [blame^] | 46 | (void) _pad_bottom; |
| 47 | constexpr int output_tile_cols = 2; |
| 48 | constexpr int inner_tile_cols = 8; |
| 49 | |
| 50 | const int pad_right = Specialized ? PadRight : _pad_right; |
| 51 | const int cells_j = output_tile_cols - pad_right; |
| 52 | |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 53 | |
| 54 | // Construct a map to the output cells |
| 55 | float *outptrs[cells_j]; |
| 56 | for (int j = 0; j < cells_j; j++) |
| 57 | { |
| 58 | outptrs[j] = output + j*output_col_stride; |
| 59 | } |
| 60 | const float *inptr = matrix_base; |
| 61 | const float *bptr = biases; |
| 62 | |
| 63 | // For each channel of the output |
| 64 | int channels_remaining = n_channels; |
| 65 | #ifdef __arm_any__ |
| 66 | for (; channels_remaining >= 4; channels_remaining -= 4) |
| 67 | { |
| 68 | // Matrices used and computed during this transform |
| 69 | float32x4_t F[inner_tile_cols], f[output_tile_cols], b = vdupq_n_f32(0.0f); |
| 70 | |
| 71 | // Read a 1x8 tile in the Winograd domain |
| 72 | for (int j = 0; j < inner_tile_cols; j++) |
| 73 | { |
| 74 | F[j] = vld1q_f32(inptr + j*matrix_stride); |
| 75 | } |
| 76 | inptr += 4; |
| 77 | |
| 78 | 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); |
| 79 | 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); |
| 80 | |
| 81 | // Write out the output tile |
| 82 | if (bptr != 0) |
| 83 | { |
| 84 | b = vld1q_f32(bptr); |
| 85 | bptr += 4; |
| 86 | } |
| 87 | for (int j = 0; j < cells_j; j++) |
| 88 | { |
| 89 | vst1q_f32(outptrs[j], f[j] + b); |
| 90 | outptrs[j] += 4; |
| 91 | } |
| 92 | } |
| 93 | for (; channels_remaining >= 2; channels_remaining -= 2) |
| 94 | { |
| 95 | // Matrices used and computed during this transform |
| 96 | float32x2_t F[inner_tile_cols], f[output_tile_cols], b = vdup_n_f32(0.0f); |
| 97 | |
| 98 | // Read a 1x8 tile in the Winograd domain |
| 99 | for (int j = 0; j < inner_tile_cols; j++) |
| 100 | { |
| 101 | F[j] = vld1_f32(inptr + j*matrix_stride); |
| 102 | } |
| 103 | inptr += 2; |
| 104 | |
| 105 | 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); |
| 106 | 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); |
| 107 | |
| 108 | // Write out the output tile |
| 109 | if (bptr != 0) |
| 110 | { |
| 111 | b = vld1_f32(bptr); |
| 112 | bptr += 2; |
| 113 | } |
| 114 | for (int j = 0; j < cells_j; j++) |
| 115 | { |
| 116 | vst1_f32(outptrs[j], f[j] + b); |
| 117 | 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 + F[7]*1; |
| 135 | |
| 136 | // Write out the output tile |
| 137 | if (bptr != 0) |
| 138 | { |
| 139 | b = *(bptr++); |
| 140 | } |
| 141 | for (int j = 0; j < cells_j; j++) |
| 142 | { |
| 143 | *(outptrs[j]++) = f[j] + b; |
| 144 | } |
| 145 | } |
| 146 | } |
Pablo Tello | d3d97d2 | 2018-10-05 10:59:48 +0100 | [diff] [blame^] | 147 | } // namespace (anonymous) |
| 148 | |
| 149 | namespace winograd |
| 150 | { |
| 151 | using Tiles = OutputTransformImplTiles<1, 7, 1, 8, float>; |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 152 | |
| 153 | template <> |
Pablo Tello | d3d97d2 | 2018-10-05 10:59:48 +0100 | [diff] [blame^] | 154 | const Tiles::TileFn Tiles::tilefn_unpadded = winograd_output_transform_2_7_fp32_process_tile<true>; |
| 155 | |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 156 | template <> |
Pablo Tello | d3d97d2 | 2018-10-05 10:59:48 +0100 | [diff] [blame^] | 157 | const Tiles::TileFn Tiles::tilefn_right_padded[n_pad_right] = { |
| 158 | winograd_output_transform_2_7_fp32_process_tile<true, 1> |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 159 | }; |
| 160 | |
Pablo Tello | d3d97d2 | 2018-10-05 10:59:48 +0100 | [diff] [blame^] | 161 | template class OutputTransform<1, 7, 1, 8, float>; |
| 162 | template class OutputTransform<7, 1, 8, 1, float>; |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 163 | } // namespace winograd |