Matthew Sloyan | af3a4ef | 2021-10-22 15:48:12 +0100 | [diff] [blame] | 1 | // |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 2 | // Copyright © 2021, 2023 Arm Ltd and Contributors. All rights reserved. |
Matthew Sloyan | af3a4ef | 2021-10-22 15:48:12 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "PadTestHelper.hpp" |
| 7 | |
| 8 | #include <armnn_delegate.hpp> |
| 9 | |
| 10 | #include <flatbuffers/flatbuffers.h> |
Matthew Sloyan | af3a4ef | 2021-10-22 15:48:12 +0100 | [diff] [blame] | 11 | |
| 12 | #include <doctest/doctest.h> |
| 13 | |
| 14 | namespace armnnDelegate |
| 15 | { |
| 16 | |
| 17 | void MirrorPadSymmetric2dTest(std::vector<armnn::BackendId>& backends) |
| 18 | { |
| 19 | // Set input data |
| 20 | std::vector<int32_t> inputShape { 3, 3 }; |
| 21 | std::vector<int32_t> outputShape { 7, 7 }; |
| 22 | std::vector<int32_t> paddingShape { 2, 2 }; |
| 23 | |
| 24 | std::vector<float> inputValues = |
| 25 | { |
| 26 | 1.0f, 2.0f, 3.0f, |
| 27 | 4.0f, 5.0f, 6.0f, |
| 28 | 7.0f, 8.0f, 9.0f |
| 29 | }; |
| 30 | |
| 31 | std::vector<float> expectedOutputValues = |
| 32 | { |
| 33 | 5.0f, 4.0f, 4.0f, 5.0f, 6.0f, 6.0f, 5.0f, |
| 34 | 2.0f, 1.0f, 1.0f, 2.0f, 3.0f, 3.0f, 2.0f, |
| 35 | 2.0f, 1.0f, 1.0f, 2.0f, 3.0f, 3.0f, 2.0f, |
| 36 | 5.0f, 4.0f, 4.0f, 5.0f, 6.0f, 6.0f, 5.0f, |
| 37 | 8.0f, 7.0f, 7.0f, 8.0f, 9.0f, 9.0f, 8.0f, |
| 38 | 8.0f, 7.0f, 7.0f, 8.0f, 9.0f, 9.0f, 8.0f, |
| 39 | 5.0f, 4.0f, 4.0f, 5.0f, 6.0f, 6.0f, 5.0f |
| 40 | }; |
| 41 | |
| 42 | std::vector<int32_t> paddingDim = { 2, 2, 2, 2 }; |
| 43 | |
| 44 | PadTest<float>(tflite::BuiltinOperator_MIRROR_PAD, |
| 45 | ::tflite::TensorType_FLOAT32, |
| 46 | backends, |
| 47 | inputShape, |
| 48 | paddingShape, |
| 49 | outputShape, |
| 50 | inputValues, |
| 51 | paddingDim, |
| 52 | expectedOutputValues, |
| 53 | 0, // Padding value - Not used in these tests. |
| 54 | 1.0f, // Scale |
| 55 | 0, // Offset |
| 56 | tflite::MirrorPadMode_SYMMETRIC); |
| 57 | } |
| 58 | |
| 59 | void MirrorPadReflect2dTest(std::vector<armnn::BackendId>& backends) |
| 60 | { |
| 61 | // Set input data |
| 62 | std::vector<int32_t> inputShape { 3, 3 }; |
| 63 | std::vector<int32_t> outputShape { 7, 7 }; |
| 64 | std::vector<int32_t> paddingShape { 2, 2 }; |
| 65 | |
| 66 | std::vector<float> inputValues = |
| 67 | { |
| 68 | 1.0f, 2.0f, 3.0f, |
| 69 | 4.0f, 5.0f, 6.0f, |
| 70 | 7.0f, 8.0f, 9.0f |
| 71 | }; |
| 72 | |
| 73 | std::vector<float> expectedOutputValues = |
| 74 | { |
| 75 | 9.0f, 8.0f, 7.0f, 8.0f, 9.0f, 8.0f, 7.0f, |
| 76 | 6.0f, 5.0f, 4.0f, 5.0f, 6.0f, 5.0f, 4.0f, |
| 77 | 3.0f, 2.0f, 1.0f, 2.0f, 3.0f, 2.0f, 1.0f, |
| 78 | 6.0f, 5.0f, 4.0f, 5.0f, 6.0f, 5.0f, 4.0f, |
| 79 | 9.0f, 8.0f, 7.0f, 8.0f, 9.0f, 8.0f, 7.0f, |
| 80 | 6.0f, 5.0f, 4.0f, 5.0f, 6.0f, 5.0f, 4.0f, |
| 81 | 3.0f, 2.0f, 1.0f, 2.0f, 3.0f, 2.0f, 1.0f |
| 82 | }; |
| 83 | |
| 84 | std::vector<int32_t> paddingDim = { 2, 2, 2, 2 }; |
| 85 | |
| 86 | PadTest<float>(tflite::BuiltinOperator_MIRROR_PAD, |
| 87 | ::tflite::TensorType_FLOAT32, |
| 88 | backends, |
| 89 | inputShape, |
| 90 | paddingShape, |
| 91 | outputShape, |
| 92 | inputValues, |
| 93 | paddingDim, |
| 94 | expectedOutputValues, |
| 95 | 0, // Padding value - Not used in these tests. |
| 96 | 1.0f, // Scale |
| 97 | 0, // Offset |
| 98 | tflite::MirrorPadMode_REFLECT); |
| 99 | } |
| 100 | |
| 101 | void MirrorPadSymmetric3dTest(std::vector<armnn::BackendId>& backends) |
| 102 | { |
| 103 | // Set input data |
| 104 | std::vector<int32_t> inputShape { 2, 2, 2 }; |
| 105 | std::vector<int32_t> outputShape { 4, 4, 4 }; |
| 106 | std::vector<int32_t> paddingShape { 3, 2 }; |
| 107 | |
| 108 | std::vector<float> inputValues = |
| 109 | { |
| 110 | // Channel 0, Height (2) x Width (2) |
| 111 | 1.0f, 2.0f, |
| 112 | 3.0f, 4.0f, |
| 113 | |
| 114 | // Channel 1, Height (2) x Width (2) |
| 115 | 5.0f, 6.0f, |
| 116 | 7.0f, 8.0f |
| 117 | }; |
| 118 | |
| 119 | std::vector<float> expectedOutputValues = |
| 120 | { |
| 121 | 1.0f, 1.0f, 2.0f, 2.0f, |
| 122 | 1.0f, 1.0f, 2.0f, 2.0f, |
| 123 | 3.0f, 3.0f, 4.0f, 4.0f, |
| 124 | 3.0f, 3.0f, 4.0f, 4.0f, |
| 125 | |
| 126 | 1.0f, 1.0f, 2.0f, 2.0f, |
| 127 | 1.0f, 1.0f, 2.0f, 2.0f, |
| 128 | 3.0f, 3.0f, 4.0f, 4.0f, |
| 129 | 3.0f, 3.0f, 4.0f, 4.0f, |
| 130 | |
| 131 | 5.0f, 5.0f, 6.0f, 6.0f, |
| 132 | 5.0f, 5.0f, 6.0f, 6.0f, |
| 133 | 7.0f, 7.0f, 8.0f, 8.0f, |
| 134 | 7.0f, 7.0f, 8.0f, 8.0f, |
| 135 | |
| 136 | 5.0f, 5.0f, 6.0f, 6.0f, |
| 137 | 5.0f, 5.0f, 6.0f, 6.0f, |
| 138 | 7.0f, 7.0f, 8.0f, 8.0f, |
| 139 | 7.0f, 7.0f, 8.0f, 8.0f |
| 140 | }; |
| 141 | |
| 142 | std::vector<int32_t> paddingDim = { 1, 1, 1, 1, 1, 1 }; |
| 143 | |
| 144 | PadTest<float>(tflite::BuiltinOperator_MIRROR_PAD, |
| 145 | ::tflite::TensorType_FLOAT32, |
| 146 | backends, |
| 147 | inputShape, |
| 148 | paddingShape, |
| 149 | outputShape, |
| 150 | inputValues, |
| 151 | paddingDim, |
| 152 | expectedOutputValues, |
| 153 | 0, // Padding value - Not used in these tests. |
| 154 | 1.0f, // Scale |
| 155 | 0, // Offset |
| 156 | tflite::MirrorPadMode_SYMMETRIC); |
| 157 | } |
| 158 | |
| 159 | void MirrorPadReflect3dTest(std::vector<armnn::BackendId>& backends) |
| 160 | { |
| 161 | // Set input data |
| 162 | std::vector<int32_t> inputShape { 2, 2, 2 }; |
| 163 | std::vector<int32_t> outputShape { 4, 4, 4 }; |
| 164 | std::vector<int32_t> paddingShape { 3, 2 }; |
| 165 | |
| 166 | std::vector<float> inputValues = |
| 167 | { |
| 168 | // Channel 0, Height (2) x Width (2) |
| 169 | 1.0f, 2.0f, |
| 170 | 3.0f, 4.0f, |
| 171 | |
| 172 | // Channel 1, Height (2) x Width (2) |
| 173 | 5.0f, 6.0f, |
| 174 | 7.0f, 8.0f |
| 175 | }; |
| 176 | |
| 177 | std::vector<float> expectedOutputValues = |
| 178 | { |
| 179 | 8.0f, 7.0f, 8.0f, 7.0f, |
| 180 | 6.0f, 5.0f, 6.0f, 5.0f, |
| 181 | 8.0f, 7.0f, 8.0f, 7.0f, |
| 182 | 6.0f, 5.0f, 6.0f, 5.0f, |
| 183 | |
| 184 | 4.0f, 3.0f, 4.0f, 3.0f, |
| 185 | 2.0f, 1.0f, 2.0f, 1.0f, |
| 186 | 4.0f, 3.0f, 4.0f, 3.0f, |
| 187 | 2.0f, 1.0f, 2.0f, 1.0f, |
| 188 | |
| 189 | 8.0f, 7.0f, 8.0f, 7.0f, |
| 190 | 6.0f, 5.0f, 6.0f, 5.0f, |
| 191 | 8.0f, 7.0f, 8.0f, 7.0f, |
| 192 | 6.0f, 5.0f, 6.0f, 5.0f, |
| 193 | |
| 194 | 4.0f, 3.0f, 4.0f, 3.0f, |
| 195 | 2.0f, 1.0f, 2.0f, 1.0f, |
| 196 | 4.0f, 3.0f, 4.0f, 3.0f, |
| 197 | 2.0f, 1.0f, 2.0f, 1.0f |
| 198 | }; |
| 199 | |
| 200 | std::vector<int32_t> paddingDim = { 1, 1, 1, 1, 1, 1 }; |
| 201 | |
| 202 | PadTest<float>(tflite::BuiltinOperator_MIRROR_PAD, |
| 203 | ::tflite::TensorType_FLOAT32, |
| 204 | backends, |
| 205 | inputShape, |
| 206 | paddingShape, |
| 207 | outputShape, |
| 208 | inputValues, |
| 209 | paddingDim, |
| 210 | expectedOutputValues, |
| 211 | 0, // Padding value - Not used in these tests. |
| 212 | 1.0f, // Scale |
| 213 | 0, // Offset |
| 214 | tflite::MirrorPadMode_REFLECT); |
| 215 | } |
| 216 | |
| 217 | void MirrorPadSymmetricUint8Test(std::vector<armnn::BackendId>& backends) |
| 218 | { |
| 219 | // Set input data |
| 220 | std::vector<int32_t> inputShape { 3, 3 }; |
| 221 | std::vector<int32_t> outputShape { 5, 7 }; |
| 222 | std::vector<int32_t> paddingShape { 2, 2 }; |
| 223 | |
| 224 | std::vector<uint8_t> inputValues = |
| 225 | { |
| 226 | 1, 2, 3, |
| 227 | 4, 5, 6, |
| 228 | 7, 8, 9 |
| 229 | }; |
| 230 | |
| 231 | std::vector<uint8_t> expectedOutputValues = |
| 232 | { |
| 233 | 2, 1, 1, 2, 3, 3, 2, |
| 234 | 2, 1, 1, 2, 3, 3, 2, |
| 235 | 5, 4, 4, 5, 6, 6, 5, |
| 236 | 8, 7, 7, 8, 9, 9, 8, |
| 237 | 8, 7, 7, 8, 9, 9, 8, |
| 238 | }; |
| 239 | |
| 240 | std::vector<int32_t> paddingDim = { 1, 1, 2, 2 }; |
| 241 | |
| 242 | PadTest<uint8_t>(tflite::BuiltinOperator_MIRROR_PAD, |
| 243 | ::tflite::TensorType_UINT8, |
| 244 | backends, |
| 245 | inputShape, |
| 246 | paddingShape, |
| 247 | outputShape, |
| 248 | inputValues, |
| 249 | paddingDim, |
| 250 | expectedOutputValues, |
| 251 | 0, // Padding value - Not used in these tests. |
| 252 | 1.0f, // Scale |
| 253 | 1, // Offset |
| 254 | tflite::MirrorPadMode_SYMMETRIC); |
| 255 | } |
| 256 | |
| 257 | void MirrorPadReflectInt8Test(std::vector<armnn::BackendId>& backends) |
| 258 | { |
| 259 | // Set input data |
| 260 | std::vector<int32_t> inputShape { 3, 3 }; |
| 261 | std::vector<int32_t> outputShape { 7, 5 }; |
| 262 | std::vector<int32_t> paddingShape { 2, 2 }; |
| 263 | |
| 264 | std::vector<int8_t> inputValues = |
| 265 | { |
| 266 | 1, 2, 3, |
| 267 | 4, 5, 6, |
| 268 | 7, 8, 9 |
| 269 | }; |
| 270 | |
| 271 | std::vector<int8_t> expectedOutputValues = |
| 272 | { |
| 273 | 8, 7, 8, 9, 8, |
| 274 | 5, 4, 5, 6, 5, |
| 275 | 2, 1, 2, 3, 2, |
| 276 | 5, 4, 5, 6, 5, |
| 277 | 8, 7, 8, 9, 8, |
| 278 | 5, 4, 5, 6, 5, |
| 279 | 2, 1, 2, 3, 2 |
| 280 | }; |
| 281 | |
| 282 | std::vector<int32_t> paddingDim = { 2, 2, 1, 1 }; |
| 283 | |
| 284 | PadTest<int8_t>(tflite::BuiltinOperator_MIRROR_PAD, |
| 285 | ::tflite::TensorType_INT8, |
| 286 | backends, |
| 287 | inputShape, |
| 288 | paddingShape, |
| 289 | outputShape, |
| 290 | inputValues, |
| 291 | paddingDim, |
| 292 | expectedOutputValues, |
| 293 | 0, // Padding value - Not used in these tests. |
| 294 | 1.0f, // Scale |
| 295 | 1, // Offset |
| 296 | tflite::MirrorPadMode_REFLECT); |
| 297 | } |
| 298 | |
| 299 | TEST_SUITE("MirrorPad_CpuRefTests") |
| 300 | { |
| 301 | |
| 302 | TEST_CASE ("MirrorPadSymmetric2d_CpuRef_Test") |
| 303 | { |
| 304 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef }; |
| 305 | MirrorPadSymmetric2dTest(backends); |
| 306 | } |
| 307 | |
| 308 | TEST_CASE ("MirrorPadReflect2d_CpuRef_Test") |
| 309 | { |
| 310 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef }; |
| 311 | MirrorPadReflect2dTest(backends); |
| 312 | } |
| 313 | |
| 314 | TEST_CASE ("MirrorPadSymmetric3d_CpuRef_Test") |
| 315 | { |
| 316 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef }; |
| 317 | MirrorPadSymmetric3dTest(backends); |
| 318 | } |
| 319 | |
| 320 | TEST_CASE ("MirrorPadReflect3d_CpuRef_Test") |
| 321 | { |
| 322 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef }; |
| 323 | MirrorPadReflect3dTest(backends); |
| 324 | } |
| 325 | |
| 326 | TEST_CASE ("MirrorPadSymmetricUint8_CpuRef_Test") |
| 327 | { |
| 328 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef }; |
| 329 | MirrorPadSymmetricUint8Test(backends); |
| 330 | } |
| 331 | |
| 332 | TEST_CASE ("MirrorPadSymmetricInt8_CpuRef_Test") |
| 333 | { |
| 334 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef }; |
| 335 | MirrorPadReflectInt8Test(backends); |
| 336 | } |
| 337 | |
| 338 | } // TEST_SUITE("MirrorPad_CpuRefTests") |
| 339 | |
| 340 | } // namespace armnnDelegate |