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