Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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 | #include "Reference.h" |
| 25 | |
| 26 | #include "Globals.h" |
| 27 | #include "Helpers.h" |
| 28 | #include "ReferenceCPP.h" |
| 29 | #include "TensorLibrary.h" |
| 30 | #include "validation/Helpers.h" |
| 31 | |
| 32 | #include <random> |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 33 | #include <vector> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | |
| 35 | using namespace arm_compute::test; |
| 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace test |
| 40 | { |
| 41 | namespace validation |
| 42 | { |
Giorgio Arena | 50f9fd7 | 2017-06-19 17:05:30 +0100 | [diff] [blame] | 43 | std::pair<RawTensor, RawTensor> Reference::compute_reference_sobel_3x3(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value) |
| 44 | { |
| 45 | // Create reference |
| 46 | RawTensor ref_src = library->get(shape, Format::U8); |
| 47 | RawTensor ref_dst_x = library->get(shape, Format::S16); |
| 48 | RawTensor ref_dst_y = library->get(shape, Format::S16); |
| 49 | |
| 50 | // Fill reference |
| 51 | library->fill_tensor_uniform(ref_src, 0); |
| 52 | |
| 53 | // Compute reference |
| 54 | ReferenceCPP::sobel_3x3(ref_src, ref_dst_x, ref_dst_y, border_mode, constant_border_value); |
| 55 | |
| 56 | return std::make_pair(ref_dst_x, ref_dst_y); |
| 57 | } |
| 58 | std::pair<RawTensor, RawTensor> Reference::compute_reference_sobel_5x5(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value) |
| 59 | { |
| 60 | // Create reference |
| 61 | RawTensor ref_src = library->get(shape, Format::U8); |
| 62 | RawTensor ref_dst_x = library->get(shape, Format::S16); |
| 63 | RawTensor ref_dst_y = library->get(shape, Format::S16); |
| 64 | |
| 65 | // Fill reference |
| 66 | library->fill_tensor_uniform(ref_src, 0); |
| 67 | |
| 68 | // Compute reference |
| 69 | ReferenceCPP::sobel_5x5(ref_src, ref_dst_x, ref_dst_y, border_mode, constant_border_value); |
| 70 | |
| 71 | return std::make_pair(ref_dst_x, ref_dst_y); |
| 72 | } |
Giorgio Arena | f795986 | 2017-06-13 15:19:51 +0100 | [diff] [blame] | 73 | std::pair<float, float> Reference::compute_reference_mean_and_standard_deviation(const TensorShape &shape) |
| 74 | { |
| 75 | // Create reference |
| 76 | RawTensor ref_src = library->get(shape, DataType::U8); |
| 77 | |
| 78 | // Create output variables |
| 79 | float mean; |
| 80 | float std_dev; |
| 81 | |
| 82 | // Fill reference |
| 83 | library->fill_tensor_uniform(ref_src, 0); |
| 84 | |
| 85 | // Compute reference |
| 86 | ReferenceCPP::mean_and_standard_deviation(ref_src, mean, std_dev); |
| 87 | |
| 88 | return std::make_pair(mean, std_dev); |
| 89 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 90 | RawTensor Reference::compute_reference_integral_image(const TensorShape &shape) |
| 91 | { |
| 92 | // Create reference |
| 93 | RawTensor ref_src = library->get(shape, DataType::U8); |
| 94 | RawTensor ref_dst = library->get(shape, DataType::U32); |
| 95 | |
| 96 | // Fill reference |
| 97 | library->fill_tensor_uniform(ref_src, 0); |
| 98 | |
| 99 | // Compute reference |
| 100 | ReferenceCPP::integral_image(ref_src, ref_dst); |
| 101 | |
| 102 | return ref_dst; |
| 103 | } |
| 104 | RawTensor Reference::compute_reference_absolute_difference(const TensorShape &shape, DataType dt_in0, DataType dt_in1, DataType dt_out) |
| 105 | { |
| 106 | // Create reference |
| 107 | RawTensor ref_src1 = library->get(shape, dt_in0); |
| 108 | RawTensor ref_src2 = library->get(shape, dt_in1); |
| 109 | RawTensor ref_dst = library->get(shape, dt_out); |
| 110 | |
| 111 | // Fill reference |
| 112 | library->fill_tensor_uniform(ref_src1, 0); |
| 113 | library->fill_tensor_uniform(ref_src2, 1); |
| 114 | |
| 115 | // Compute reference |
| 116 | ReferenceCPP::absolute_difference(ref_src1, ref_src2, ref_dst); |
| 117 | |
| 118 | return ref_dst; |
| 119 | } |
| 120 | |
| 121 | RawTensor Reference::compute_reference_accumulate(const TensorShape &shape) |
| 122 | { |
| 123 | // Create reference |
| 124 | RawTensor ref_src = library->get(shape, DataType::U8); |
| 125 | RawTensor ref_dst = library->get(shape, DataType::S16); |
| 126 | |
| 127 | // Fill reference |
| 128 | library->fill_tensor_uniform(ref_src, 0); |
| 129 | library->fill_tensor_uniform(ref_dst, 1); |
| 130 | |
| 131 | // Compute reference |
| 132 | ReferenceCPP::accumulate(ref_src, ref_dst); |
| 133 | |
| 134 | return ref_dst; |
| 135 | } |
| 136 | |
| 137 | RawTensor Reference::compute_reference_accumulate_squared(const TensorShape &shape, uint32_t shift) |
| 138 | { |
| 139 | // Create reference |
| 140 | RawTensor ref_src = library->get(shape, DataType::U8); |
| 141 | RawTensor ref_dst = library->get(shape, DataType::S16); |
| 142 | |
| 143 | // Fill reference |
| 144 | // ref_dst tensor filled with non-negative values |
| 145 | library->fill_tensor_uniform(ref_src, 0); |
| 146 | library->fill_tensor_uniform(ref_dst, 1, static_cast<int16_t>(0), std::numeric_limits<int16_t>::max()); |
| 147 | |
| 148 | // Compute reference |
| 149 | ReferenceCPP::accumulate_squared(ref_src, ref_dst, shift); |
| 150 | |
| 151 | return ref_dst; |
| 152 | } |
| 153 | |
| 154 | RawTensor Reference::compute_reference_accumulate_weighted(const TensorShape &shape, float alpha) |
| 155 | { |
| 156 | // Create reference |
| 157 | RawTensor ref_src = library->get(shape, DataType::U8); |
| 158 | RawTensor ref_dst = library->get(shape, DataType::U8); |
| 159 | |
| 160 | // Fill reference |
| 161 | library->fill_tensor_uniform(ref_src, 0); |
| 162 | library->fill_tensor_uniform(ref_dst, 1); |
| 163 | |
| 164 | // Compute reference |
| 165 | ReferenceCPP::accumulate_weighted(ref_src, ref_dst, alpha); |
| 166 | |
| 167 | return ref_dst; |
| 168 | } |
| 169 | |
| 170 | RawTensor Reference::compute_reference_arithmetic_addition(const TensorShape &shape, DataType dt_in0, DataType dt_in1, DataType dt_out, ConvertPolicy convert_policy) |
| 171 | { |
| 172 | // Create reference |
| 173 | RawTensor ref_src1 = library->get(shape, dt_in0); |
| 174 | RawTensor ref_src2 = library->get(shape, dt_in1); |
| 175 | RawTensor ref_dst = library->get(shape, dt_out); |
| 176 | |
| 177 | // Fill reference |
| 178 | library->fill_tensor_uniform(ref_src1, 0); |
| 179 | library->fill_tensor_uniform(ref_src2, 1); |
| 180 | |
| 181 | // Compute reference |
| 182 | ReferenceCPP::arithmetic_addition(ref_src1, ref_src2, ref_dst, convert_policy); |
| 183 | |
| 184 | return ref_dst; |
| 185 | } |
| 186 | |
| 187 | RawTensor Reference::compute_reference_arithmetic_subtraction(const TensorShape &shape, DataType dt_in0, DataType dt_in1, DataType dt_out, ConvertPolicy convert_policy) |
| 188 | { |
| 189 | // Create reference |
| 190 | RawTensor ref_src1 = library->get(shape, dt_in0); |
| 191 | RawTensor ref_src2 = library->get(shape, dt_in1); |
| 192 | RawTensor ref_dst = library->get(shape, dt_out); |
| 193 | |
| 194 | // Fill reference |
| 195 | library->fill_tensor_uniform(ref_src1, 0); |
| 196 | library->fill_tensor_uniform(ref_src2, 1); |
| 197 | |
| 198 | // Compute reference |
| 199 | ReferenceCPP::arithmetic_subtraction(ref_src1, ref_src2, ref_dst, convert_policy); |
| 200 | |
| 201 | return ref_dst; |
| 202 | } |
| 203 | |
| 204 | RawTensor Reference::compute_reference_bitwise_and(const TensorShape &shape) |
| 205 | { |
| 206 | // Create reference |
| 207 | RawTensor ref_src1 = library->get(shape, DataType::U8); |
| 208 | RawTensor ref_src2 = library->get(shape, DataType::U8); |
| 209 | RawTensor ref_dst = library->get(shape, DataType::U8); |
| 210 | |
| 211 | // Fill reference |
| 212 | library->fill_tensor_uniform(ref_src1, 0); |
| 213 | library->fill_tensor_uniform(ref_src2, 1); |
| 214 | |
| 215 | // Compute reference |
| 216 | ReferenceCPP::bitwise_and(ref_src1, ref_src2, ref_dst); |
| 217 | |
| 218 | return ref_dst; |
| 219 | } |
| 220 | |
| 221 | RawTensor Reference::compute_reference_bitwise_or(const TensorShape &shape) |
| 222 | { |
| 223 | // Create reference |
| 224 | RawTensor ref_src1 = library->get(shape, DataType::U8); |
| 225 | RawTensor ref_src2 = library->get(shape, DataType::U8); |
| 226 | RawTensor ref_dst = library->get(shape, DataType::U8); |
| 227 | |
| 228 | // Fill reference |
| 229 | library->fill_tensor_uniform(ref_src1, 0); |
| 230 | library->fill_tensor_uniform(ref_src2, 1); |
| 231 | |
| 232 | // Compute reference |
| 233 | ReferenceCPP::bitwise_or(ref_src1, ref_src2, ref_dst); |
| 234 | |
| 235 | return ref_dst; |
| 236 | } |
| 237 | |
| 238 | RawTensor Reference::compute_reference_bitwise_xor(const TensorShape &shape) |
| 239 | { |
| 240 | // Create reference |
| 241 | RawTensor ref_src1 = library->get(shape, DataType::U8); |
| 242 | RawTensor ref_src2 = library->get(shape, DataType::U8); |
| 243 | RawTensor ref_dst = library->get(shape, DataType::U8); |
| 244 | |
| 245 | // Fill reference |
| 246 | library->fill_tensor_uniform(ref_src1, 0); |
| 247 | library->fill_tensor_uniform(ref_src2, 1); |
| 248 | |
| 249 | // Compute reference |
| 250 | ReferenceCPP::bitwise_xor(ref_src1, ref_src2, ref_dst); |
| 251 | |
| 252 | return ref_dst; |
| 253 | } |
| 254 | |
| 255 | RawTensor Reference::compute_reference_bitwise_not(const TensorShape &shape) |
| 256 | { |
| 257 | // Create reference |
| 258 | RawTensor ref_src = library->get(shape, DataType::U8); |
| 259 | RawTensor ref_dst = library->get(shape, DataType::U8); |
| 260 | |
| 261 | // Fill reference |
| 262 | library->fill_tensor_uniform(ref_src, 0); |
| 263 | |
| 264 | // Compute reference |
| 265 | ReferenceCPP::bitwise_not(ref_src, ref_dst); |
| 266 | |
| 267 | return ref_dst; |
| 268 | } |
| 269 | |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 270 | RawTensor Reference::compute_reference_box3x3(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 271 | { |
| 272 | // Create reference |
| 273 | RawTensor ref_src = library->get(shape, DataType::U8); |
| 274 | RawTensor ref_dst = library->get(shape, DataType::U8); |
| 275 | |
| 276 | // Fill reference |
| 277 | library->fill_tensor_uniform(ref_src, 0); |
| 278 | |
| 279 | // Compute reference |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 280 | ReferenceCPP::box3x3(ref_src, ref_dst, border_mode, constant_border_value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 281 | |
| 282 | return ref_dst; |
| 283 | } |
| 284 | |
| 285 | RawTensor Reference::compute_reference_depth_convert(const TensorShape &shape, DataType dt_in, DataType dt_out, ConvertPolicy policy, uint32_t shift, uint32_t fixed_point_position) |
| 286 | { |
| 287 | RawTensor ref_src = library->get(shape, dt_in, 1, fixed_point_position); |
| 288 | RawTensor ref_dst = library->get(shape, dt_out, 1, fixed_point_position); |
| 289 | |
| 290 | // Fill reference |
| 291 | library->fill_tensor_uniform(ref_src, 0); |
| 292 | |
| 293 | // Compute reference |
| 294 | ReferenceCPP::depth_convert(ref_src, ref_dst, policy, shift); |
| 295 | |
| 296 | return ref_dst; |
| 297 | } |
| 298 | |
SiCong Li | 5a53664 | 2017-06-19 14:47:05 +0100 | [diff] [blame] | 299 | RawTensor Reference::compute_reference_gaussian3x3(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value) |
| 300 | { |
| 301 | // Create reference |
| 302 | RawTensor ref_src = library->get(shape, DataType::U8); |
| 303 | RawTensor ref_dst = library->get(shape, DataType::U8); |
| 304 | |
| 305 | // Fill reference |
| 306 | library->fill_tensor_uniform(ref_src, 0); |
| 307 | |
| 308 | // Compute reference |
| 309 | ReferenceCPP::gaussian3x3(ref_src, ref_dst, border_mode, constant_border_value); |
| 310 | |
| 311 | return ref_dst; |
| 312 | } |
| 313 | |
SiCong Li | 3eb263e | 2017-06-19 15:31:43 +0100 | [diff] [blame] | 314 | RawTensor Reference::compute_reference_gaussian5x5(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value) |
| 315 | { |
| 316 | // Create reference |
| 317 | RawTensor ref_src = library->get(shape, DataType::U8); |
| 318 | RawTensor ref_dst = library->get(shape, DataType::U8); |
| 319 | |
| 320 | // Fill reference |
| 321 | library->fill_tensor_uniform(ref_src, 0); |
| 322 | |
| 323 | // Compute reference |
| 324 | ReferenceCPP::gaussian5x5(ref_src, ref_dst, border_mode, constant_border_value); |
| 325 | |
| 326 | return ref_dst; |
| 327 | } |
| 328 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 329 | RawTensor Reference::compute_reference_gemm(const TensorShape &src_shape1, const TensorShape &src_shape2, const TensorShape &src_shape3, |
| 330 | const TensorShape &dst_shape, float alpha, float beta, DataType dt, int fixed_point_position) |
| 331 | { |
| 332 | RawTensor src1 = library->get(src_shape1, dt, 1, fixed_point_position); |
| 333 | RawTensor src2 = library->get(src_shape2, dt, 1, fixed_point_position); |
| 334 | RawTensor src3 = library->get(src_shape3, dt, 1, fixed_point_position); |
| 335 | RawTensor dst = library->get(dst_shape, dt, 1, fixed_point_position); |
| 336 | |
| 337 | // Fill reference |
Pablo Tello | 221f381 | 2017-06-28 17:27:56 +0100 | [diff] [blame] | 338 | if(dt == DataType::F16 || dt == DataType::F32) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 339 | { |
| 340 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
| 341 | library->fill(src1, distribution, 0); |
| 342 | library->fill(src2, distribution, 1); |
| 343 | library->fill(src3, distribution, 2); |
| 344 | } |
| 345 | else |
| 346 | { |
| 347 | library->fill_tensor_uniform(src1, 0); |
| 348 | library->fill_tensor_uniform(src2, 1); |
| 349 | library->fill_tensor_uniform(src3, 2); |
| 350 | } |
| 351 | |
| 352 | // Compute reference |
| 353 | ReferenceCPP::gemm(src1, src2, src3, dst, alpha, beta); |
| 354 | |
| 355 | return dst; |
| 356 | } |
| 357 | |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 358 | RawTensor Reference::compute_reference_non_linear_filter(const TensorShape &shape, NonLinearFilterFunction function, unsigned int mask_size, |
| 359 | MatrixPattern pattern, const uint8_t *mask, BorderMode border_mode, uint8_t constant_border_value) |
| 360 | { |
| 361 | // Create reference |
| 362 | RawTensor ref_src = library->get(shape, DataType::U8); |
| 363 | RawTensor ref_dst = library->get(shape, DataType::U8); |
| 364 | |
| 365 | // Fill reference |
| 366 | library->fill_tensor_uniform(ref_src, 0); |
| 367 | |
| 368 | // Compute reference |
| 369 | ReferenceCPP::non_linear_filter(ref_src, ref_dst, function, mask_size, pattern, mask, border_mode, constant_border_value); |
| 370 | |
| 371 | return ref_dst; |
| 372 | } |
| 373 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 374 | RawTensor Reference::compute_reference_pixel_wise_multiplication(const TensorShape &shape, DataType dt_in0, DataType dt_in1, DataType dt_out, float scale, ConvertPolicy convert_policy, |
| 375 | RoundingPolicy rounding_policy) |
| 376 | { |
| 377 | // Create reference |
| 378 | RawTensor ref_src1 = library->get(shape, dt_in0); |
| 379 | RawTensor ref_src2 = library->get(shape, dt_in1); |
| 380 | RawTensor ref_dst = library->get(shape, dt_out); |
| 381 | |
| 382 | // Fill reference |
| 383 | library->fill_tensor_uniform(ref_src1, 0); |
| 384 | library->fill_tensor_uniform(ref_src2, 1); |
| 385 | |
| 386 | // Compute reference |
| 387 | ReferenceCPP::pixel_wise_multiplication(ref_src1, ref_src2, ref_dst, scale, convert_policy, rounding_policy); |
| 388 | |
| 389 | return ref_dst; |
| 390 | } |
| 391 | |
| 392 | RawTensor Reference::compute_reference_fixed_point_pixel_wise_multiplication(const TensorShape &shape, DataType dt_in0, DataType dt_in1, DataType dt_out, float scale, int fixed_point_position, |
| 393 | ConvertPolicy convert_policy, RoundingPolicy rounding_policy) |
| 394 | { |
| 395 | // Create reference |
| 396 | RawTensor ref_src1 = library->get(shape, dt_in0, 1, fixed_point_position); |
| 397 | RawTensor ref_src2 = library->get(shape, dt_in1, 1, fixed_point_position); |
| 398 | RawTensor ref_dst = library->get(shape, dt_out, 1, fixed_point_position); |
| 399 | |
| 400 | // Fill reference |
| 401 | library->fill_tensor_uniform(ref_src1, 0); |
| 402 | library->fill_tensor_uniform(ref_src2, 1); |
| 403 | |
| 404 | // Compute reference |
| 405 | ReferenceCPP::fixed_point_pixel_wise_multiplication(ref_src1, ref_src2, ref_dst, scale, convert_policy, rounding_policy); |
| 406 | |
| 407 | return ref_dst; |
| 408 | } |
| 409 | |
| 410 | RawTensor Reference::compute_reference_threshold(const TensorShape &shape, uint8_t threshold, uint8_t false_value, uint8_t true_value, ThresholdType type, uint8_t upper) |
| 411 | { |
| 412 | // Create reference |
| 413 | RawTensor ref_src1 = library->get(shape, DataType::U8); |
| 414 | RawTensor ref_dst = library->get(shape, DataType::U8); |
| 415 | |
| 416 | // Fill reference |
| 417 | library->fill_tensor_uniform(ref_src1, 0); |
| 418 | |
| 419 | // Compute reference |
| 420 | ReferenceCPP::threshold(ref_src1, ref_dst, threshold, false_value, true_value, type, upper); |
| 421 | |
| 422 | return ref_dst; |
| 423 | } |
| 424 | |
| 425 | RawTensor Reference::compute_reference_activation_layer(const TensorShape &shape, DataType dt, ActivationLayerInfo act_info, int fixed_point_position) |
| 426 | { |
| 427 | // Create reference |
| 428 | RawTensor ref_src = library->get(shape, dt, 1, fixed_point_position); |
| 429 | RawTensor ref_dst = library->get(shape, dt, 1, fixed_point_position); |
| 430 | |
| 431 | // Fill reference |
| 432 | if(dt == DataType::F32) |
| 433 | { |
| 434 | float min_bound = 0; |
| 435 | float max_bound = 0; |
| 436 | std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<float>(act_info.activation()); |
| 437 | std::uniform_real_distribution<> distribution(min_bound, max_bound); |
| 438 | library->fill(ref_src, distribution, 0); |
| 439 | } |
| 440 | else |
| 441 | { |
| 442 | int min_bound = 0; |
| 443 | int max_bound = 0; |
| 444 | std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<int8_t>(act_info.activation(), fixed_point_position); |
| 445 | std::uniform_int_distribution<> distribution(min_bound, max_bound); |
| 446 | library->fill(ref_src, distribution, 0); |
| 447 | } |
| 448 | |
| 449 | // Compute reference |
| 450 | ReferenceCPP::activation_layer(ref_src, ref_dst, act_info); |
| 451 | |
| 452 | return ref_dst; |
| 453 | } |
| 454 | |
| 455 | RawTensor Reference::compute_reference_batch_normalization_layer(const TensorShape &shape0, const TensorShape &shape1, DataType dt, float epsilon, int fixed_point_position) |
| 456 | { |
| 457 | // Create reference |
| 458 | RawTensor ref_src = library->get(shape0, dt, 1, fixed_point_position); |
| 459 | RawTensor ref_dst = library->get(shape0, dt, 1, fixed_point_position); |
| 460 | RawTensor ref_mean = library->get(shape1, dt, 1, fixed_point_position); |
| 461 | RawTensor ref_var = library->get(shape1, dt, 1, fixed_point_position); |
| 462 | RawTensor ref_beta = library->get(shape1, dt, 1, fixed_point_position); |
| 463 | RawTensor ref_gamma = library->get(shape1, dt, 1, fixed_point_position); |
| 464 | |
| 465 | // Fill tensors with values from -1 to 1. |
| 466 | if(dt == DataType::F32) |
| 467 | { |
| 468 | float min_bound = 0.f; |
| 469 | float max_bound = 0.f; |
| 470 | std::tie(min_bound, max_bound) = get_batchnormalization_layer_test_bounds<float>(); |
| 471 | std::uniform_real_distribution<> distribution(min_bound, max_bound); |
| 472 | std::uniform_real_distribution<> distribution_var(0, max_bound); |
| 473 | library->fill(ref_src, distribution, 0); |
| 474 | library->fill(ref_mean, distribution, 1); |
| 475 | library->fill(ref_var, distribution_var, 0); |
| 476 | library->fill(ref_beta, distribution, 3); |
| 477 | library->fill(ref_gamma, distribution, 4); |
| 478 | } |
| 479 | else |
| 480 | { |
| 481 | int min_bound = 0; |
| 482 | int max_bound = 0; |
| 483 | std::tie(min_bound, max_bound) = get_batchnormalization_layer_test_bounds<int8_t>(fixed_point_position); |
| 484 | std::uniform_int_distribution<> distribution(min_bound, max_bound); |
| 485 | std::uniform_int_distribution<> distribution_var(0, max_bound); |
| 486 | library->fill(ref_src, distribution, 0); |
| 487 | library->fill(ref_mean, distribution, 1); |
| 488 | library->fill(ref_var, distribution_var, 0); |
| 489 | library->fill(ref_beta, distribution, 3); |
| 490 | library->fill(ref_gamma, distribution, 4); |
| 491 | } |
| 492 | |
| 493 | // Compute reference |
| 494 | ReferenceCPP::batch_normalization_layer(ref_src, ref_dst, ref_mean, ref_var, ref_beta, ref_gamma, epsilon, fixed_point_position); |
| 495 | |
| 496 | return ref_dst; |
| 497 | } |
| 498 | |
| 499 | RawTensor Reference::compute_reference_convolution_layer(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, DataType dt, |
| 500 | const PadStrideInfo &conv_info, int fixed_point_position) |
| 501 | { |
| 502 | // Create reference |
| 503 | RawTensor ref_src = library->get(input_shape, dt, 1, fixed_point_position); |
| 504 | RawTensor ref_weights = library->get(weights_shape, dt, 1, fixed_point_position); |
| 505 | RawTensor ref_bias = library->get(bias_shape, dt, 1, fixed_point_position); |
| 506 | RawTensor ref_dst = library->get(output_shape, dt, 1, fixed_point_position); |
| 507 | |
| 508 | // Fill reference |
Pablo Tello | 997aba2 | 2017-06-28 11:32:05 +0100 | [diff] [blame] | 509 | if(dt == DataType::F16 || dt == DataType::F32) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 510 | { |
| 511 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
| 512 | library->fill(ref_src, distribution, 0); |
| 513 | library->fill(ref_weights, distribution, 1); |
| 514 | library->fill(ref_bias, distribution, 2); |
| 515 | } |
| 516 | else |
| 517 | { |
| 518 | library->fill_tensor_uniform(ref_src, 0); |
| 519 | library->fill_tensor_uniform(ref_weights, 1); |
| 520 | library->fill_tensor_uniform(ref_bias, 2); |
| 521 | } |
| 522 | |
| 523 | // Compute reference |
| 524 | ReferenceCPP::convolution_layer(ref_src, ref_weights, ref_bias, ref_dst, conv_info); |
| 525 | |
| 526 | return ref_dst; |
| 527 | } |
| 528 | |
| 529 | RawTensor Reference::compute_reference_fully_connected_layer(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, |
| 530 | DataType dt, bool transpose_weights, int fixed_point_position) |
| 531 | { |
| 532 | // Create reference |
| 533 | RawTensor ref_src = library->get(input_shape, dt, 1, fixed_point_position); |
| 534 | RawTensor ref_bias = library->get(bias_shape, dt, 1, fixed_point_position); |
| 535 | RawTensor ref_dst = library->get(output_shape, dt, 1, fixed_point_position); |
| 536 | |
| 537 | // Swap the first and second dimension of weights' shape if transpose_weights is true |
| 538 | TensorShape ws = weights_shape; |
| 539 | if(transpose_weights) |
| 540 | { |
| 541 | const size_t dimx = ws.x(); |
| 542 | ws.set(0, ws.y()); |
| 543 | ws.set(1, dimx); |
| 544 | } |
| 545 | |
| 546 | RawTensor ref_weights = library->get(ws, dt, 1, fixed_point_position); |
| 547 | |
| 548 | // Fill reference |
| 549 | if(dt == DataType::F32) |
| 550 | { |
| 551 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
| 552 | library->fill(ref_src, distribution, 0); |
| 553 | library->fill(ref_weights, distribution, 1); |
| 554 | library->fill(ref_bias, distribution, 2); |
| 555 | } |
| 556 | else |
| 557 | { |
| 558 | library->fill_tensor_uniform(ref_src, 0); |
| 559 | library->fill_tensor_uniform(ref_weights, 1); |
| 560 | library->fill_tensor_uniform(ref_bias, 2); |
| 561 | } |
| 562 | |
| 563 | // Compute reference |
| 564 | ReferenceCPP::fully_connected_layer(ref_src, ref_weights, ref_bias, ref_dst); |
| 565 | |
| 566 | return ref_dst; |
| 567 | } |
| 568 | |
| 569 | RawTensor Reference::compute_reference_normalization_layer(const TensorShape &shape, DataType dt, NormalizationLayerInfo norm_info, int fixed_point_position) |
| 570 | { |
| 571 | // Create reference |
| 572 | RawTensor ref_src = library->get(shape, dt, 1, fixed_point_position); |
| 573 | RawTensor ref_dst = library->get(shape, dt, 1, fixed_point_position); |
| 574 | |
| 575 | // Fill reference |
| 576 | if(dt == DataType::QS8) |
| 577 | { |
| 578 | const int8_t one_fixed_point = 1 << fixed_point_position; |
| 579 | const int8_t minus_one_fixed_point = -one_fixed_point; |
| 580 | library->fill_tensor_uniform(ref_src, 0, minus_one_fixed_point, one_fixed_point); |
| 581 | } |
| 582 | else |
| 583 | { |
| 584 | library->fill_tensor_uniform(ref_src, 0); |
| 585 | } |
| 586 | |
| 587 | // Compute reference |
| 588 | ReferenceCPP::normalization_layer(ref_src, ref_dst, norm_info); |
| 589 | |
| 590 | return ref_dst; |
| 591 | } |
| 592 | |
| 593 | RawTensor Reference::compute_reference_pooling_layer(const TensorShape &shape_in, const TensorShape &shape_out, DataType dt, PoolingLayerInfo pool_info, int fixed_point_position) |
| 594 | { |
| 595 | // Create reference |
| 596 | RawTensor ref_src = library->get(shape_in, dt, 1, fixed_point_position); |
| 597 | RawTensor ref_dst = library->get(shape_out, dt, 1, fixed_point_position); |
| 598 | |
| 599 | // Fill reference |
| 600 | int min = 0; |
| 601 | int max = 0; |
| 602 | switch(dt) |
| 603 | { |
| 604 | case DataType::F32: |
| 605 | min = -1; |
| 606 | max = 1; |
| 607 | break; |
| 608 | case DataType::QS8: |
| 609 | min = -(1 << fixed_point_position); |
| 610 | max = (1 << fixed_point_position); |
| 611 | break; |
| 612 | default: |
| 613 | ARM_COMPUTE_ERROR("DataType not supported."); |
| 614 | } |
| 615 | std::uniform_real_distribution<> distribution(min, max); |
| 616 | library->fill(ref_src, distribution, 0.0); |
| 617 | |
| 618 | // Compute reference |
| 619 | ReferenceCPP::pooling_layer(ref_src, ref_dst, pool_info, fixed_point_position); |
| 620 | |
| 621 | return ref_dst; |
| 622 | } |
| 623 | |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 624 | RawTensor Reference::compute_reference_roi_pooling_layer(const TensorShape &shape, DataType dt, const std::vector<ROI> &rois, const ROIPoolingLayerInfo &pool_info) |
| 625 | { |
| 626 | TensorShape shape_dst; |
| 627 | shape_dst.set(0, pool_info.pooled_width()); |
| 628 | shape_dst.set(1, pool_info.pooled_height()); |
| 629 | shape_dst.set(2, shape.z()); |
| 630 | shape_dst.set(3, rois.size()); |
| 631 | |
| 632 | // Create reference |
| 633 | RawTensor ref_src = library->get(shape, dt); |
| 634 | RawTensor ref_dst = library->get(shape_dst, dt); |
| 635 | |
| 636 | // Fill reference |
| 637 | std::uniform_real_distribution<> distribution(-1, 1); |
| 638 | library->fill(ref_src, distribution, 0.0); |
| 639 | |
| 640 | // Compute reference |
| 641 | ReferenceCPP::roi_pooling_layer(ref_src, ref_dst, rois, pool_info); |
| 642 | |
| 643 | return ref_dst; |
| 644 | } |
| 645 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 646 | RawTensor Reference::compute_reference_softmax_layer(const TensorShape &shape, DataType dt, int fixed_point_position) |
| 647 | { |
| 648 | // Create reference |
| 649 | RawTensor ref_src = library->get(shape, dt, 1, fixed_point_position); |
| 650 | RawTensor ref_dst = library->get(shape, dt, 1, fixed_point_position); |
| 651 | |
| 652 | // Fill reference |
| 653 | if(arm_compute::is_data_type_float(dt)) |
| 654 | { |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 655 | std::uniform_real_distribution<> distribution(-1000.f, 1000.f); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 656 | library->fill(ref_src, distribution, 0); |
| 657 | } |
| 658 | else |
| 659 | { |
| 660 | int one_fixed = 1 << fixed_point_position; |
| 661 | std::uniform_int_distribution<> distribution(-one_fixed, one_fixed); |
| 662 | library->fill(ref_src, distribution, 0); |
| 663 | } |
| 664 | |
| 665 | // Compute reference |
| 666 | ReferenceCPP::softmax_layer(ref_src, ref_dst); |
| 667 | |
| 668 | return ref_dst; |
| 669 | } |
| 670 | |
| 671 | RawTensor Reference::compute_reference_fixed_point_operation(const TensorShape &shape, DataType dt_in, DataType dt_out, FixedPointOp op, int fixed_point_position) |
| 672 | { |
| 673 | // Create reference |
| 674 | RawTensor ref_src = library->get(shape, dt_in, 1, fixed_point_position); |
| 675 | RawTensor ref_dst = library->get(shape, dt_out, 1, fixed_point_position); |
| 676 | |
| 677 | // Fill reference |
| 678 | int min = 0; |
| 679 | int max = 0; |
| 680 | switch(op) |
| 681 | { |
| 682 | case(FixedPointOp::INV_SQRT): |
Michalis Spyrou | 0a8334c | 2017-06-14 18:00:05 +0100 | [diff] [blame] | 683 | min = 1; |
| 684 | max = (dt_in == DataType::QS8) ? 0x7F : 0x7FFF; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 685 | break; |
| 686 | case(FixedPointOp::LOG): |
| 687 | min = (1 << (fixed_point_position - 1)); |
Michalis Spyrou | 0a8334c | 2017-06-14 18:00:05 +0100 | [diff] [blame] | 688 | max = (dt_in == DataType::QS8) ? 0x3F : 0x3FFF; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 689 | break; |
| 690 | case(FixedPointOp::EXP): |
Michalis Spyrou | 0a8334c | 2017-06-14 18:00:05 +0100 | [diff] [blame] | 691 | min = -(1 << (fixed_point_position - 1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 692 | max = (1 << (fixed_point_position - 1)); |
| 693 | break; |
| 694 | case(FixedPointOp::RECIPROCAL): |
| 695 | min = 15; |
Michalis Spyrou | 0a8334c | 2017-06-14 18:00:05 +0100 | [diff] [blame] | 696 | max = (dt_in == DataType::QS8) ? 0x7F : 0x7FFF; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 697 | break; |
| 698 | default: |
| 699 | ARM_COMPUTE_ERROR("Fixed point operation not supported"); |
| 700 | } |
| 701 | std::uniform_int_distribution<> distribution(min, max); |
| 702 | library->fill(ref_src, distribution, 0); |
| 703 | |
| 704 | // Compute reference |
| 705 | ReferenceCPP::fixed_point_operation(ref_src, ref_dst, op); |
| 706 | |
| 707 | return ref_dst; |
| 708 | } |
| 709 | |
| 710 | } // namespace validation |
| 711 | } // namespace test |
| 712 | } // namespace arm_compute |