Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 1 | /* |
Pablo Tello | 5264b7d | 2019-10-21 14:25:41 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 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 | |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 25 | #include "arm.hpp" |
| 26 | #include "output.hpp" |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 27 | |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 28 | namespace winograd |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 29 | { |
| 30 | |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 31 | template <> |
| 32 | void OutputTransform<1, 7, 1, 8, float, float, WinogradRoots::Integers>::transform_tile( |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 33 | const int n_channels, |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 34 | const float* inptr, |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 35 | const int matrix_stride, |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 36 | const float* bptr, |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 37 | float* const output, |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 38 | const int, // No need to stride across rows |
Pablo Tello | 5264b7d | 2019-10-21 14:25:41 +0100 | [diff] [blame] | 39 | const int output_col_stride, |
| 40 | const float output_min, |
| 41 | const float output_max |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 42 | ) |
| 43 | { |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 44 | // Construct a map to the output cells |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 45 | float *outptrs[output_tile_cols]; |
| 46 | for (int j = 0; j < output_tile_cols; j++) |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 47 | { |
| 48 | outptrs[j] = output + j*output_col_stride; |
| 49 | } |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 50 | |
| 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 Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 75 | for (int j = 0; j < output_tile_cols; j++) |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 76 | { |
Pablo Tello | 5264b7d | 2019-10-21 14:25:41 +0100 | [diff] [blame] | 77 | 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 Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 80 | 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 Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 104 | for (int j = 0; j < output_tile_cols; j++) |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 105 | { |
Pablo Tello | 5264b7d | 2019-10-21 14:25:41 +0100 | [diff] [blame] | 106 | 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 Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 109 | 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 Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 133 | for (int j = 0; j < output_tile_cols; j++) |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 134 | { |
Pablo Tello | 5264b7d | 2019-10-21 14:25:41 +0100 | [diff] [blame] | 135 | *(outptrs[j]++) = std::max(std::min(f[j] + b, output_max), output_min); |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | } |
Pablo Tello | d3d97d2 | 2018-10-05 10:59:48 +0100 | [diff] [blame] | 139 | |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 140 | template class OutputTransform<1, 7, 1, 8, float, float, WinogradRoots::Integers>; |
| 141 | template class OutputTransform<7, 1, 8, 1, float, float, WinogradRoots::Integers>; |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 142 | |
Pablo Tello | 000d33a | 2018-09-03 16:59:20 +0100 | [diff] [blame] | 143 | } // namespace winograd |