Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 "Winograd.h" |
| 25 | |
| 26 | #include "tests/validation/Helpers.h" |
| 27 | #include "tests/validation/reference/Utils.h" |
| 28 | |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/Types.h" |
| 30 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 31 | #include <algorithm> |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 32 | #include <cmath> |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 33 | |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 34 | namespace arm_compute |
| 35 | { |
| 36 | namespace test |
| 37 | { |
| 38 | namespace validation |
| 39 | { |
| 40 | namespace reference |
| 41 | { |
| 42 | namespace |
| 43 | { |
| 44 | template <typename T> |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 45 | void initialize_matrix_transform(SimpleTensor<T> &src, const Size2D &output_tile_size, const Size2D &kernel_size, WinogradTransformType winograd_transform_type) |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 46 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 47 | // Winograd input transform matrices |
| 48 | static const float imatrix2x2_3x3[] = |
Giorgio Arena | 2d9de0a | 2018-03-15 17:58:20 +0000 | [diff] [blame] | 49 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 50 | 1.0f, 0.0f, -1.0f, 0.0f, |
| 51 | 0.0f, 1.0f, 1.0f, 0.0f, |
| 52 | 0.0f, -1.0f, 1.0f, 0.0f, |
| 53 | 0.0f, 1.0f, 0.0f, -1.0f |
| 54 | }; |
| 55 | |
| 56 | static const float imatrix4x4_3x3[] = |
| 57 | { |
| 58 | 4.0f, 0.0f, -5.0f, 0.0f, 1.0f, 0.0f, |
| 59 | 0.0f, -4.0f, -4.0f, 1.0f, 1.0f, 0.0f, |
| 60 | 0.0f, 4.0f, -4.0f, -1.0f, 1.0f, 0.0f, |
| 61 | 0.0f, -2.0f, -1.0f, 2.0f, 1.0f, 0.0f, |
| 62 | 0.0f, 2.0f, -1.0f, -2.0f, 1.0f, 0.0f, |
| 63 | 0.0f, 4.0f, 0.0f, -5.0f, 0.0f, 1.0f, |
| 64 | }; |
| 65 | |
Giorgio Arena | fe5ef38 | 2018-04-17 10:14:10 +0100 | [diff] [blame] | 66 | static const float imatrix4x4_5x5[] = |
| 67 | { |
| 68 | 1.f, 0.f, -21.f / 4.f, 0.f, 21.f / 4.f, 0.f, -1.f, 0.f, |
| 69 | 0.f, 1.f, 1.f, -17.f / 4.f, -17.f / 4.f, 1.f, 1.f, 0.f, |
| 70 | 0.f, -1.f, 1.f, 17.f / 4.f, -17.f / 4.f, -1.f, 1.f, 0.f, |
| 71 | 0.f, 1.f / 2.f, 1.f / 4.f, -5.f / 2.f, -5.f / 4.f, 2.f, 1.f, 0.f, |
| 72 | 0.f, -1.f / 2.f, 1.f / 4.f, 5.f / 2.f, -5.f / 4.f, -2.f, 1.f, 0.f, |
| 73 | 0.f, 2.f, 4.f, -5.f / 2.f, -5.f, 1.f / 2.f, 1.f, 0.f, |
| 74 | 0.f, -2.f, 4.f, 5.f / 2.f, -5.f, -1.f / 2.f, 1.f, 0.f, |
| 75 | 0.f, -1.f, 0.f, 21.f / 4.f, 0.f, -21.f / 4.f, 0.f, 1.f |
| 76 | }; |
| 77 | |
Pablo Tello | 96e922e | 2018-09-26 11:25:15 +0100 | [diff] [blame] | 78 | static const float imatrix2x1_7x7[] = |
| 79 | { |
| 80 | -36.0f, 0.0f, 49.0f, 0.0f, -14.0f, 0.0f, 1.0f, 0.0f, |
| 81 | 0.0f, -36.0f, 36.0f, 13.0f, -13.0f, -1.0f, 1.0f, 0.0f, |
| 82 | 0.0f, 36.0f, 36.0f, -13.0f, -13.0f, 1.0f, 1.0f, 0.0f, |
| 83 | 0.0f, -18.0f, 9.0f, 20.0f, -10.0f, -2.0f, 1.0f, 0.0f, |
| 84 | 0.0f, 18.0f, 9.0f, -20.0f, -10.0f, 2.0f, 1.0f, 0.0f, |
| 85 | 0.0f, -12.0f, 4.0f, 15.0f, -5.0f, -3.0f, 1.0f, 0.0f, |
| 86 | 0.0f, 12.0f, 4.0f, -15.0f, -5.0f, 3.0f, 1.0f, 0.0f, |
| 87 | 0.0f, -36.0f, 0.0f, 49.0f, 0.0f, -14.0f, 0.0f, 1.0f |
| 88 | }; |
| 89 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 90 | // ------------------------------------------ |
| 91 | |
| 92 | // Winograd filter transform matrices |
| 93 | static const float fmatrix2x2_3x3[] = |
| 94 | { |
| 95 | 1.0f, 0.0f, 0.0f, |
| 96 | 0.5f, 0.5f, 0.5f, |
| 97 | 0.5f, -0.5f, 0.5f, |
| 98 | 0.0f, 0.0f, 1.0f |
| 99 | }; |
| 100 | |
| 101 | static const float fmatrix4x4_3x3[] = |
| 102 | { |
| 103 | 0.25f, 0.0f, 0.0f, |
| 104 | -1.0f / 6.0f, -1.0f / 6.0f, -1.0f / 6.0f, |
| 105 | -1.0f / 6.0f, 1.0f / 6.0f, -1.0f / 6.0f, |
| 106 | 1.0f / 24.0f, 1.0f / 12.0f, 1.0f / 6.0f, |
| 107 | 1.0f / 24.0f, -1.0f / 12.0f, 1.0f / 6.0f, |
| 108 | 0.0f, 0.0f, 1.0f |
| 109 | }; |
| 110 | |
Giorgio Arena | 9373c8b | 2018-04-11 19:07:17 +0100 | [diff] [blame] | 111 | static const float fmatrix4x4_5x5[] = |
| 112 | { |
| 113 | 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, |
| 114 | -2.0f / 9.0f, -2.0f / 9.0f, -2.0f / 9.0f, -2.0f / 9.0f, -2.0f / 9.0f, |
| 115 | -2.0f / 9.0f, 2.0f / 9.0f, -2.0f / 9.0f, 2.0f / 9.0f, -2.0f / 9.0f, |
| 116 | 1.0f / 90.0f, 1.0f / 45.0f, 2.0f / 45.0f, 4.0f / 45.0f, 8.0f / 45.0f, |
| 117 | 1.0f / 90.0f, -1.0f / 45.0f, 2.0f / 45.0f, -4.0f / 45.0f, 8.0f / 45.0f, |
| 118 | 4.0f / 45.0f, 2.0f / 45.0f, 1.0f / 45.0f, 1.0f / 90.0f, 1.0f / 180.0f, |
| 119 | 4.0f / 45.0f, -2.0f / 45.0f, 1.0f / 45.0f, -1.0f / 90.0f, 1.0f / 180.0f, |
| 120 | 0.0f, 0.0f, 0.0f, 0.0f, 1.0f |
| 121 | |
| 122 | }; |
| 123 | |
Pablo Tello | 96e922e | 2018-09-26 11:25:15 +0100 | [diff] [blame] | 124 | static const float fmatrix2x1_7x7[] = |
| 125 | { |
| 126 | -1.0f / 36.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, |
| 127 | 1.0f / 48.0f, -1.0f / 48.0f, 1.0f / 48.0f, -1.0f / 48.0f, 1.0f / 48.0f, -1.0f / 48.0f, 1.0f / 48.0f, |
| 128 | 1.0f / 48.0f, 1.0f / 48.0f, 1.0f / 48.0f, 1.0f / 48.0f, 1.0f / 48.0f, 1.0f / 48.0f, 1.0f / 48.0f, |
| 129 | -1.0f / 120.0f, 1.0f / 60.0f, -1.0f / 30.0f, 1.0f / 15.0f, -2.0f / 15.0f, 4.0f / 15.0f, -8.0f / 15.0f, |
| 130 | -1.0f / 120.0f, -1.0f / 60.0f, -1.0f / 30.0f, -1.0f / 15.0f, -2.0f / 15.0f, -4.0f / 15.0f, -8.0f / 15.0f, |
| 131 | 1.0f / 720.0f, -1.0f / 240.0f, 1.0f / 80.0f, -3.0f / 80.0f, 9.0f / 80.0f, -27.0f / 80.0f, 81.0f / 80.0f, |
| 132 | 1.0f / 720.0f, 1.0f / 240.0f, 1.0f / 80.0f, 3.0f / 80.0f, 9.0f / 80.0f, 27.0f / 80.0f, 81.0f / 80.0f, |
| 133 | 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f |
| 134 | }; |
| 135 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 136 | // ------------------------------------------ |
| 137 | |
| 138 | // Winograd output transform matrices |
| 139 | static const float omatrix2x2_3x3[] = |
| 140 | { |
| 141 | 1.0f, 1.0f, 1.0f, 0.0f, |
| 142 | 0.0f, 1.0f, -1.0f, -1.0f |
| 143 | }; |
| 144 | |
| 145 | static const float omatrix4x4_3x3[] = |
| 146 | { |
| 147 | 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, |
| 148 | 0.0f, 1.0f, -1.0f, 2.0f, -2.0f, 0.0f, |
| 149 | 0.0f, 1.0f, 1.0f, 4.0f, 4.0f, 0.0f, |
| 150 | 0.0f, 1.0f, -1.0f, 8.0f, -8.0f, 1.0f |
| 151 | }; |
| 152 | |
Giorgio Arena | dd03870 | 2018-04-16 11:20:11 +0100 | [diff] [blame] | 153 | static const float omatrix4x4_5x5[] = |
| 154 | { |
| 155 | 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 8.0f, 8.0f, 0.0f, |
| 156 | 0.0f, 1.0f, -1.0f, 2.0f, -2.0f, 4.0f, -4.0f, 0.0f, |
| 157 | 0.0f, 1.0f, 1.0f, 4.0f, 4.0f, 2.0f, 2.0f, 0.0f, |
| 158 | 0.0f, 1.0f, -1.0f, 8.0f, -8.0f, 1.0f, -1.0f, 1.0f |
| 159 | }; |
| 160 | |
Pablo Tello | 96e922e | 2018-09-26 11:25:15 +0100 | [diff] [blame] | 161 | static const float omatrix2x1_7x7[] = |
| 162 | { |
| 163 | 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, |
| 164 | 0.0f, -1.0f, 1.0f, -2.0f, 2.0f, -3.0f, 3.0f, 1.0f |
| 165 | }; |
| 166 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 167 | // ------------------------------------------ |
| 168 | |
| 169 | using WinogradKey = std::tuple<std::pair<int, int>, std::pair<int, int>, WinogradTransformType>; |
| 170 | |
| 171 | // Key = (Output tile size, Kernel size, Winograd transform type) |
| 172 | static std::map<WinogradKey, const float *> matrix_map = |
| 173 | { |
| 174 | { WinogradKey(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3), WinogradTransformType::INPUT), imatrix2x2_3x3 }, |
| 175 | { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3), WinogradTransformType::INPUT), imatrix4x4_3x3 }, |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 176 | { WinogradKey(std::pair<int, int>(2, 1), std::pair<int, int>(3, 1), WinogradTransformType::INPUT), imatrix2x2_3x3 }, |
| 177 | { WinogradKey(std::pair<int, int>(4, 1), std::pair<int, int>(3, 1), WinogradTransformType::INPUT), imatrix4x4_3x3 }, |
| 178 | { WinogradKey(std::pair<int, int>(1, 2), std::pair<int, int>(1, 3), WinogradTransformType::INPUT), imatrix2x2_3x3 }, |
| 179 | { WinogradKey(std::pair<int, int>(1, 4), std::pair<int, int>(1, 3), WinogradTransformType::INPUT), imatrix4x4_3x3 }, |
Giorgio Arena | fe5ef38 | 2018-04-17 10:14:10 +0100 | [diff] [blame] | 180 | { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5), WinogradTransformType::INPUT), imatrix4x4_5x5 }, |
Gian Marco Iodice | 876be2a | 2018-07-03 12:22:09 +0100 | [diff] [blame] | 181 | { WinogradKey(std::pair<int, int>(4, 1), std::pair<int, int>(5, 1), WinogradTransformType::INPUT), imatrix4x4_5x5 }, |
Pablo Tello | 96e922e | 2018-09-26 11:25:15 +0100 | [diff] [blame] | 182 | { WinogradKey(std::pair<int, int>(2, 1), std::pair<int, int>(7, 1), WinogradTransformType::INPUT), imatrix2x1_7x7 }, |
| 183 | { WinogradKey(std::pair<int, int>(1, 2), std::pair<int, int>(1, 7), WinogradTransformType::INPUT), imatrix2x1_7x7 }, |
Gian Marco Iodice | 876be2a | 2018-07-03 12:22:09 +0100 | [diff] [blame] | 184 | { WinogradKey(std::pair<int, int>(1, 4), std::pair<int, int>(1, 5), WinogradTransformType::INPUT), imatrix4x4_5x5 }, |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 185 | { WinogradKey(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3), WinogradTransformType::FILTER), fmatrix2x2_3x3 }, |
| 186 | { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3), WinogradTransformType::FILTER), fmatrix4x4_3x3 }, |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 187 | { WinogradKey(std::pair<int, int>(2, 1), std::pair<int, int>(3, 1), WinogradTransformType::FILTER), fmatrix2x2_3x3 }, |
| 188 | { WinogradKey(std::pair<int, int>(4, 1), std::pair<int, int>(3, 1), WinogradTransformType::FILTER), fmatrix4x4_3x3 }, |
| 189 | { WinogradKey(std::pair<int, int>(1, 2), std::pair<int, int>(1, 3), WinogradTransformType::FILTER), fmatrix2x2_3x3 }, |
| 190 | { WinogradKey(std::pair<int, int>(1, 4), std::pair<int, int>(1, 3), WinogradTransformType::FILTER), fmatrix4x4_3x3 }, |
Giorgio Arena | 9373c8b | 2018-04-11 19:07:17 +0100 | [diff] [blame] | 191 | { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5), WinogradTransformType::FILTER), fmatrix4x4_5x5 }, |
Gian Marco Iodice | 876be2a | 2018-07-03 12:22:09 +0100 | [diff] [blame] | 192 | { WinogradKey(std::pair<int, int>(4, 1), std::pair<int, int>(5, 1), WinogradTransformType::FILTER), fmatrix4x4_5x5 }, |
Pablo Tello | 96e922e | 2018-09-26 11:25:15 +0100 | [diff] [blame] | 193 | { WinogradKey(std::pair<int, int>(2, 1), std::pair<int, int>(7, 1), WinogradTransformType::FILTER), fmatrix2x1_7x7 }, |
| 194 | { WinogradKey(std::pair<int, int>(1, 2), std::pair<int, int>(1, 7), WinogradTransformType::FILTER), fmatrix2x1_7x7 }, |
Gian Marco Iodice | 876be2a | 2018-07-03 12:22:09 +0100 | [diff] [blame] | 195 | { WinogradKey(std::pair<int, int>(1, 4), std::pair<int, int>(1, 5), WinogradTransformType::FILTER), fmatrix4x4_5x5 }, |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 196 | { WinogradKey(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3), WinogradTransformType::OUTPUT), omatrix2x2_3x3 }, |
| 197 | { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3), WinogradTransformType::OUTPUT), omatrix4x4_3x3 }, |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 198 | { WinogradKey(std::pair<int, int>(2, 1), std::pair<int, int>(3, 1), WinogradTransformType::OUTPUT), omatrix2x2_3x3 }, |
| 199 | { WinogradKey(std::pair<int, int>(4, 1), std::pair<int, int>(3, 1), WinogradTransformType::OUTPUT), omatrix4x4_3x3 }, |
| 200 | { WinogradKey(std::pair<int, int>(1, 2), std::pair<int, int>(1, 3), WinogradTransformType::OUTPUT), omatrix2x2_3x3 }, |
| 201 | { WinogradKey(std::pair<int, int>(1, 4), std::pair<int, int>(1, 3), WinogradTransformType::OUTPUT), omatrix4x4_3x3 }, |
Giorgio Arena | dd03870 | 2018-04-16 11:20:11 +0100 | [diff] [blame] | 202 | { WinogradKey(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5), WinogradTransformType::OUTPUT), omatrix4x4_5x5 }, |
Gian Marco Iodice | 876be2a | 2018-07-03 12:22:09 +0100 | [diff] [blame] | 203 | { WinogradKey(std::pair<int, int>(4, 1), std::pair<int, int>(5, 1), WinogradTransformType::OUTPUT), omatrix4x4_5x5 }, |
Pablo Tello | 96e922e | 2018-09-26 11:25:15 +0100 | [diff] [blame] | 204 | { WinogradKey(std::pair<int, int>(2, 1), std::pair<int, int>(7, 1), WinogradTransformType::OUTPUT), omatrix2x1_7x7 }, |
| 205 | { WinogradKey(std::pair<int, int>(1, 2), std::pair<int, int>(1, 7), WinogradTransformType::OUTPUT), omatrix2x1_7x7 }, |
Gian Marco Iodice | 876be2a | 2018-07-03 12:22:09 +0100 | [diff] [blame] | 206 | { WinogradKey(std::pair<int, int>(1, 4), std::pair<int, int>(1, 5), WinogradTransformType::OUTPUT), omatrix4x4_5x5 }, |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 207 | }; |
| 208 | |
Giorgio Arena | 9373c8b | 2018-04-11 19:07:17 +0100 | [diff] [blame] | 209 | // Find transformation matrix |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 210 | std::map<WinogradKey, const float *>::iterator it; |
| 211 | |
| 212 | it = matrix_map.find(WinogradKey(std::pair<int, int>(output_tile_size.width, output_tile_size.height), |
| 213 | std::pair<int, int>(kernel_size.width, kernel_size.height), |
| 214 | winograd_transform_type)); |
| 215 | |
| 216 | float const *matrix_values = nullptr; |
| 217 | if(it != matrix_map.end()) |
| 218 | { |
| 219 | // Get matrix pointer |
| 220 | matrix_values = it->second; |
Giorgio Arena | 2d9de0a | 2018-03-15 17:58:20 +0000 | [diff] [blame] | 221 | } |
| 222 | else |
| 223 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 224 | ARM_COMPUTE_ERROR("Winograd configuration not supported"); |
Giorgio Arena | 2d9de0a | 2018-03-15 17:58:20 +0000 | [diff] [blame] | 225 | } |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 226 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 227 | // Copy values |
| 228 | std::copy(&matrix_values[0], &matrix_values[0] + src.num_elements(), &src[0]); |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 229 | } |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 230 | } // namespace |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 231 | |
| 232 | template <typename T> |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 233 | SimpleTensor<T> winograd_input_transform(const SimpleTensor<T> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info) |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 234 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 235 | ARM_COMPUTE_ERROR_ON(in.data_layout() != DataLayout::NCHW); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 236 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 237 | const PadStrideInfo conv_info = winograd_info.convolution_info; |
| 238 | const Size2D output_tile_size = winograd_info.output_tile_size; |
| 239 | const Size2D kernel_size = winograd_info.kernel_size; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 240 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 241 | SimpleTensor<T> out{ output_shape, in.data_type() }; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 242 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 243 | // Calculate dimensions for the tile |
| 244 | const unsigned int tile_w = output_tile_size.width + kernel_size.width - 1; |
| 245 | const unsigned int tile_h = output_tile_size.height + kernel_size.height - 1; |
| 246 | |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 247 | // Get the maximum dimension from the tile size |
| 248 | const unsigned int tile_max_dim = std::max(tile_w, tile_h); |
| 249 | |
| 250 | TensorShape tile_dims(tile_max_dim, tile_max_dim); |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 251 | |
| 252 | // Simple tensor for the input tile |
| 253 | SimpleTensor<T> src_tile{ tile_dims, in.data_type() }; |
| 254 | |
| 255 | // Simple tensor for the temporary tile |
| 256 | SimpleTensor<T> tmp_tile{ tile_dims, in.data_type() }; |
| 257 | |
| 258 | // Simple tensor for the output tile |
| 259 | SimpleTensor<T> dst_tile{ tile_dims, in.data_type() }; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 260 | |
| 261 | // Simple tensor for the transformation matrix |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 262 | SimpleTensor<T> matrix{ tile_dims, in.data_type() }; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 263 | |
| 264 | // Simple tensor for the transformation matrix transposed |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 265 | SimpleTensor<T> matrix_transposed{ tile_dims, in.data_type() }; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 266 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 267 | // Initialize matrix for the input transform |
| 268 | initialize_matrix_transform(matrix, output_tile_size, kernel_size, WinogradTransformType::INPUT); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 269 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 270 | // Transpose matrix |
Vidhya Sudhan Loganathan | 71ecf39 | 2018-08-31 16:10:16 +0100 | [diff] [blame] | 271 | transpose_matrix<T>(matrix, matrix_transposed); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 272 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 273 | const int in_w = in.shape().x(); |
| 274 | const int in_h = in.shape().y(); |
| 275 | const int in_d = in.shape().z(); |
| 276 | const int out_d = out.shape().z(); |
| 277 | const int num_batches = in.shape().total_size() / (in_w * in_h * in_d); |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 278 | const int step_x = output_tile_size.width; |
| 279 | const int step_y = output_tile_size.height; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 280 | |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 281 | // Compute the number of output tiles along the x and y direction of size "output_tile_size" |
| 282 | const Size2D num_tiles = compute_winograd_convolution_tiles(Size2D(in_w, in_h), |
| 283 | kernel_size, |
| 284 | output_tile_size, |
| 285 | conv_info); |
| 286 | |
| 287 | const int num_tiles_x = num_tiles.width; |
| 288 | const int num_tiles_y = num_tiles.height; |
| 289 | |
| 290 | // In case of 1D convolution, the input tile has to be partially filled with zeros |
| 291 | int start_x_zero = 0; |
| 292 | int start_y_zero = 0; |
| 293 | int end_x_zero = 0; |
| 294 | int end_y_zero = 0; |
| 295 | |
| 296 | if(output_tile_size.width == 1) |
| 297 | { |
| 298 | start_x_zero = 1; |
| 299 | start_y_zero = 0; |
| 300 | end_x_zero = tile_max_dim - 1; |
| 301 | end_y_zero = tile_max_dim; |
| 302 | } |
| 303 | else if(output_tile_size.height == 1) |
| 304 | { |
| 305 | start_x_zero = 0; |
| 306 | start_y_zero = 1; |
| 307 | end_x_zero = tile_max_dim; |
| 308 | end_y_zero = tile_max_dim - 1; |
| 309 | } |
| 310 | |
| 311 | // Set the anchor and shape of the zeros area |
| 312 | const Coordinates anchor_zeros(start_x_zero, start_y_zero); |
| 313 | const TensorShape shape_zeros(end_x_zero, end_y_zero); |
| 314 | |
| 315 | // If we have a vertical filter (i.e. 1x3, 1x5,..), we need to take the elements along the y direction (step = width of the output tile) |
| 316 | const int step_y_transf_tile = kernel_size.width == 1 ? tile_max_dim : 1; |
| 317 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 318 | ARM_COMPUTE_ERROR_ON((num_tiles_x * num_tiles_y) != static_cast<int>(out.shape().y())); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 319 | |
| 320 | for(int b = 0; b < num_batches; ++b) |
| 321 | { |
| 322 | for(int z = 0; z < in_d; ++z) |
| 323 | { |
| 324 | for(int y = 0; y < num_tiles_y; ++y) |
| 325 | { |
| 326 | for(int x = 0; x < num_tiles_x; ++x) |
| 327 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 328 | int xi = x * step_x - conv_info.pad_left(); |
| 329 | int yi = y * step_y - conv_info.pad_top(); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 330 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 331 | // Get the tile from the input tensor |
Vidhya Sudhan Loganathan | 71ecf39 | 2018-08-31 16:10:16 +0100 | [diff] [blame] | 332 | get_tile<T>(in, src_tile, Coordinates(xi, yi, z, b)); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 333 | |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 334 | // Fill partially with zeros in case of 1D convolution |
Vidhya Sudhan Loganathan | 71ecf39 | 2018-08-31 16:10:16 +0100 | [diff] [blame] | 335 | zeros<T>(src_tile, anchor_zeros, shape_zeros); |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 336 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 337 | // Compute the transformation |
Vidhya Sudhan Loganathan | 71ecf39 | 2018-08-31 16:10:16 +0100 | [diff] [blame] | 338 | matrix_multiply<T>(matrix, src_tile, tmp_tile); |
| 339 | matrix_multiply<T>(tmp_tile, matrix_transposed, dst_tile); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 340 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 341 | // Store the output tile across the channels |
| 342 | for(int i = 0; i < out_d; ++i) |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 343 | { |
| 344 | int xo = z; |
| 345 | int yo = x + y * num_tiles_x; |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 346 | out[coords2index(out.shape(), Coordinates(xo, yo, i, b))] = dst_tile[i * step_y_transf_tile]; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | } |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 352 | |
| 353 | return out; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | template <typename T> |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 357 | SimpleTensor<T> winograd_filter_transform(const SimpleTensor<T> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info) |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 358 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 359 | ARM_COMPUTE_ERROR_ON_MSG(in.data_layout() != DataLayout::NCHW, "Only supported NCHW data format"); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 360 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 361 | // Create reference |
| 362 | SimpleTensor<T> out{ output_shape, in.data_type(), 1 }; |
| 363 | |
| 364 | const Size2D output_tile_size = winograd_info.output_tile_size; |
| 365 | const Size2D kernel_size = winograd_info.kernel_size; |
| 366 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 367 | // Calculate dimensions for the tile |
| 368 | const unsigned int input_tile_w = output_tile_size.width + kernel_size.width - 1; |
| 369 | const unsigned int input_tile_h = output_tile_size.height + kernel_size.height - 1; |
| 370 | const unsigned int input_tile_area = input_tile_w * input_tile_h; |
| 371 | |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 372 | // Get the maximum dimension from the filter size |
| 373 | const unsigned int kernel_max_dim = std::max(kernel_size.width, kernel_size.height); |
| 374 | |
| 375 | // Get the maximum dimension from the input tile |
| 376 | const unsigned int input_tile_max_dim = std::max(input_tile_w, input_tile_h); |
| 377 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 378 | // Simple tensor for the input tile |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 379 | SimpleTensor<T> input_tile{ TensorShape(kernel_max_dim, kernel_max_dim), in.data_type(), 1 }; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 380 | |
| 381 | // Simple tensor for the transformation matrix |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 382 | SimpleTensor<T> trans_matrix{ TensorShape(kernel_max_dim, input_tile_max_dim), in.data_type(), 1 }; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 383 | |
| 384 | // Simple tensor for the transformation matrix transpose |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 385 | SimpleTensor<T> trans_matrix_transposed{ TensorShape(input_tile_max_dim, kernel_max_dim), in.data_type(), 1 }; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 386 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 387 | // Simple tensor for the temporary tile |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 388 | SimpleTensor<T> tmp_tile{ TensorShape(kernel_max_dim, input_tile_max_dim), in.data_type(), 1 }; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 389 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 390 | // Simple tensor for the output tile |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 391 | SimpleTensor<T> transf_tile{ TensorShape(input_tile_max_dim, input_tile_max_dim), in.data_type(), 1 }; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 392 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 393 | // Initialize matrix for the filter transform |
| 394 | initialize_matrix_transform(trans_matrix, output_tile_size, kernel_size, WinogradTransformType::FILTER); |
| 395 | |
| 396 | // Transpose the transformation matrix |
Vidhya Sudhan Loganathan | 71ecf39 | 2018-08-31 16:10:16 +0100 | [diff] [blame] | 397 | transpose_matrix<T>(trans_matrix, trans_matrix_transposed); |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 398 | |
| 399 | const int num_channels = in.shape()[2]; |
| 400 | const int num_filters = in.shape()[3]; |
| 401 | const int num_batches = in.shape().total_size() / (kernel_size.area() * num_channels * num_filters); |
| 402 | |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 403 | // If we have a vertical filter (i.e. 1x3, 1x5,..), we need to take the elements along the y direction (step_y_transf_tile = width of the output tile) |
| 404 | const int step_y_transf_tile = kernel_size.width == 1 ? input_tile_max_dim : 1; |
| 405 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 406 | for(int n = 0; n < num_batches; ++n) |
| 407 | { |
| 408 | for(int w = 0; w < num_filters; ++w) |
| 409 | { |
| 410 | for(int z = 0; z < num_channels; ++z) |
| 411 | { |
| 412 | // Load the tile from the input tensor |
Vidhya Sudhan Loganathan | 71ecf39 | 2018-08-31 16:10:16 +0100 | [diff] [blame] | 413 | get_tile<T>(in, input_tile, Coordinates(0, 0, z, w, n)); |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 414 | |
| 415 | // First transformation |
Vidhya Sudhan Loganathan | 71ecf39 | 2018-08-31 16:10:16 +0100 | [diff] [blame] | 416 | matrix_multiply<T>(trans_matrix, input_tile, tmp_tile); |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 417 | |
| 418 | // Second transformation |
Vidhya Sudhan Loganathan | 71ecf39 | 2018-08-31 16:10:16 +0100 | [diff] [blame] | 419 | matrix_multiply<T>(tmp_tile, trans_matrix_transposed, transf_tile); |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 420 | |
| 421 | // Store the output tile across the channels |
| 422 | const int output_offset = w + z * num_filters; |
| 423 | |
| 424 | // Store the values across the channels |
| 425 | for(unsigned int i = 0; i < input_tile_area; ++i) |
| 426 | { |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 427 | out[output_offset + i * num_filters * num_channels] = transf_tile[i * step_y_transf_tile]; |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | return out; |
| 434 | } |
| 435 | |
| 436 | template <typename T> |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 437 | SimpleTensor<T> winograd_output_transform(const SimpleTensor<T> &in, const SimpleTensor<T> &b, const TensorShape &output_shape, const WinogradInfo &winograd_info) |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 438 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 439 | const PadStrideInfo conv_info = winograd_info.convolution_info; |
| 440 | const Size2D input_dimensions = winograd_info.input_dimensions; |
| 441 | const Size2D output_tile_size = winograd_info.output_tile_size; |
| 442 | const Size2D kernel_size = winograd_info.kernel_size; |
| 443 | |
| 444 | // Create reference |
| 445 | SimpleTensor<T> out{ output_shape, in.data_type(), 1 }; |
| 446 | |
| 447 | // Calculate dimensions for the tiles |
| 448 | const unsigned int in_tile_w = output_tile_size.width + kernel_size.width - 1; |
| 449 | const unsigned int in_tile_h = output_tile_size.height + kernel_size.height - 1; |
| 450 | const unsigned int out_tile_w = output_tile_size.width; |
| 451 | const unsigned int out_tile_h = output_tile_size.height; |
| 452 | |
| 453 | ARM_COMPUTE_ERROR_ON(in.shape()[2] != (in_tile_w * in_tile_h)); |
Giorgio Arena | 3695f9a | 2018-04-23 17:41:22 +0100 | [diff] [blame] | 454 | ARM_COMPUTE_ERROR_ON(in.shape()[0] != out.shape()[get_data_layout_dimension_index(winograd_info.output_data_layout, DataLayoutDimension::CHANNEL)]); |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 455 | |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 456 | // Get the maximum dimension from the tile size |
| 457 | const unsigned int in_tile_max_dim = std::max(in_tile_w, in_tile_h); |
| 458 | const unsigned int out_tile_max_dim = std::max(output_tile_size.width, output_tile_size.height); |
| 459 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 460 | // Compute tile dimensions |
| 461 | // Input tile dimensions |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 462 | TensorShape in_tile_dims(in_tile_max_dim, in_tile_max_dim); |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 463 | |
| 464 | // Output tile dimensions |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 465 | TensorShape out_tile_dims(out_tile_max_dim, out_tile_max_dim); |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 466 | |
| 467 | // Transformation matrix dimensions |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 468 | TensorShape tr_tile_dims(in_tile_max_dim, out_tile_max_dim); |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 469 | |
| 470 | // Create tensors |
| 471 | // Simple tensor for the input tile |
| 472 | SimpleTensor<T> input_tile{ in_tile_dims, in.data_type(), 1 }; |
| 473 | |
| 474 | // Simple tensor for the transformation matrix |
| 475 | SimpleTensor<T> trans_matrix{ tr_tile_dims, in.data_type(), 1 }; |
| 476 | |
| 477 | // Simple tensor for the transformation matrix transpose |
| 478 | SimpleTensor<T> trans_matrix_transposed{ TensorShape(tr_tile_dims[1], tr_tile_dims[0]), in.data_type(), 1 }; |
| 479 | |
| 480 | // Simple tensor for the temporary tile |
| 481 | SimpleTensor<T> tmp_tile{ tr_tile_dims, in.data_type(), 1 }; |
| 482 | |
| 483 | // Simple tensor for the output tile |
| 484 | SimpleTensor<T> output_tile{ out_tile_dims, in.data_type(), 1 }; |
| 485 | |
| 486 | // Initialize matrix for the output transform |
| 487 | initialize_matrix_transform(trans_matrix, output_tile_size, kernel_size, WinogradTransformType::OUTPUT); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 488 | |
| 489 | // Transpose the transformation matrix |
Vidhya Sudhan Loganathan | 71ecf39 | 2018-08-31 16:10:16 +0100 | [diff] [blame] | 490 | transpose_matrix<T>(trans_matrix, trans_matrix_transposed); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 491 | |
| 492 | const int w_in = in.shape()[0]; |
| 493 | const int h_in = in.shape()[1]; |
| 494 | const int c_in = in.shape()[2]; |
| 495 | const int w_out = out.shape()[0]; |
| 496 | const int h_out = out.shape()[1]; |
| 497 | const int c_out = out.shape()[2]; |
| 498 | const int num_batches = in.shape().total_size() / (w_in * h_in * c_in); |
| 499 | |
| 500 | // Input strides |
| 501 | const int stridey_in = w_in; |
| 502 | const int stridez_in = stridey_in * h_in; |
| 503 | const int stridew_in = stridez_in * c_in; |
| 504 | |
| 505 | // Output strides |
| 506 | const int stridey_out = w_out; |
| 507 | const int stridez_out = stridey_out * h_out; |
| 508 | const int stridew_out = stridez_out * c_out; |
| 509 | |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 510 | // Compute the number of output tiles along the x and y direction of size "output_tile_size" |
| 511 | const Size2D num_tiles = compute_winograd_convolution_tiles(Size2D(input_dimensions.width, input_dimensions.height), |
| 512 | kernel_size, |
| 513 | output_tile_size, |
| 514 | conv_info); |
| 515 | |
| 516 | const int num_tiles_x = num_tiles.width; |
| 517 | const int num_tiles_y = num_tiles.height; |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 518 | |
| 519 | ARM_COMPUTE_UNUSED(num_tiles_y); |
| 520 | ARM_COMPUTE_ERROR_ON(in.shape()[1] != static_cast<unsigned int>(num_tiles_x * num_tiles_y)); |
| 521 | |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 522 | // If we have a vertical filter (i.e. 1x3, 1x5,..), we still need to take the elements along the x direction (step_y_transf_tile = 1) |
| 523 | const int step_y_transf_tile = kernel_size.width == 1 ? 1 : output_tile.shape()[0]; |
| 524 | |
| 525 | // Initialize with zeros the input tile |
Vidhya Sudhan Loganathan | 71ecf39 | 2018-08-31 16:10:16 +0100 | [diff] [blame] | 526 | zeros<T>(input_tile, Coordinates(0, 0), input_tile.shape()); |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 527 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 528 | for(int n = 0; n < num_batches; ++n) |
| 529 | { |
| 530 | for(int y = 0; y < h_in; ++y) |
| 531 | { |
| 532 | for(int x = 0; x < w_in; ++x) |
| 533 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 534 | // Load the input tile tile across the channels of the input tensor |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 535 | for(int z = 0; z < c_in; ++z) |
| 536 | { |
| 537 | input_tile[z] = in[x + (y * stridey_in) + (z * stridez_in) + (n * stridew_in)]; |
| 538 | } |
| 539 | |
| 540 | // First transformation |
Vidhya Sudhan Loganathan | 71ecf39 | 2018-08-31 16:10:16 +0100 | [diff] [blame] | 541 | matrix_multiply<T>(trans_matrix, input_tile, tmp_tile); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 542 | |
| 543 | // Second transformation |
Vidhya Sudhan Loganathan | 71ecf39 | 2018-08-31 16:10:16 +0100 | [diff] [blame] | 544 | matrix_multiply<T>(tmp_tile, trans_matrix_transposed, output_tile); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 545 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 546 | // Store the output tile |
| 547 | const int xo = (y % num_tiles_x) * out_tile_w; |
| 548 | const int yo = (y / num_tiles_x) * out_tile_h; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 549 | const int zo = x; |
| 550 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 551 | const int output_offset = xo + (yo * stridey_out) + (zo * stridez_out) + (n * stridew_out); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 552 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 553 | for(int yi = 0; yi < static_cast<int>(out_tile_h); ++yi) |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 554 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 555 | for(int xi = 0; xi < static_cast<int>(out_tile_w); ++xi) |
| 556 | { |
| 557 | // Check out-of-bound writes |
| 558 | if((xo + xi < w_out) && (yo + yi < h_out)) |
| 559 | { |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 560 | out[output_offset + yi * stridey_out + xi] = output_tile[xi + yi * step_y_transf_tile]; |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 561 | |
| 562 | // Add bias |
| 563 | out[output_offset + yi * stridey_out + xi] += b[zo]; |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 564 | } |
| 565 | } |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | } |
| 569 | } |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 570 | |
| 571 | return out; |
| 572 | } |
| 573 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 574 | template SimpleTensor<float> winograd_filter_transform(const SimpleTensor<float> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info); |
| 575 | template SimpleTensor<float> winograd_input_transform(const SimpleTensor<float> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info); |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 576 | template SimpleTensor<float> winograd_output_transform(const SimpleTensor<float> &in, const SimpleTensor<float> &b, const TensorShape &output_shape, const WinogradInfo &winograd_info); |
Vidhya Sudhan Loganathan | 71ecf39 | 2018-08-31 16:10:16 +0100 | [diff] [blame] | 577 | template SimpleTensor<half> winograd_filter_transform(const SimpleTensor<half> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info); |
| 578 | template SimpleTensor<half> winograd_input_transform(const SimpleTensor<half> &in, const TensorShape &output_shape, const WinogradInfo &winograd_info); |
| 579 | template SimpleTensor<half> winograd_output_transform(const SimpleTensor<half> &in, const SimpleTensor<half> &b, const TensorShape &output_shape, const WinogradInfo &winograd_info); |
| 580 | |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 581 | } // namespace reference |
| 582 | } // namespace validation |
| 583 | } // namespace test |
| 584 | } // namespace arm_compute |