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 | #ifndef __ARM_COMPUTE_TEST_TENSOR_VISITORS_H__ |
| 25 | #define __ARM_COMPUTE_TEST_TENSOR_VISITORS_H__ |
| 26 | |
| 27 | #include "Tensor.h" |
| 28 | #include "TensorOperations.h" |
| 29 | #include "arm_compute/core/Error.h" |
Isabella Gottardi | b797fa2 | 2017-06-23 15:02:11 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/Lut.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | |
| 32 | #include "boost_wrapper.h" |
| 33 | |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 34 | #include <algorithm> |
Isabella Gottardi | b797fa2 | 2017-06-23 15:02:11 +0100 | [diff] [blame] | 35 | #include <map> |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 36 | #include <memory> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | #include <ostream> |
Georgios Pinitas | d4f8c27 | 2017-06-30 16:16:19 +0100 | [diff] [blame] | 38 | #include <vector> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | |
| 40 | namespace arm_compute |
| 41 | { |
| 42 | namespace test |
| 43 | { |
| 44 | namespace validation |
| 45 | { |
| 46 | namespace tensor_visitors |
| 47 | { |
Giorgio Arena | 2ca209e | 2017-06-13 15:49:37 +0100 | [diff] [blame] | 48 | // Min max location visitor |
| 49 | struct min_max_location_visitor : public boost::static_visitor<> |
| 50 | { |
| 51 | public: |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 52 | explicit min_max_location_visitor(void *min, void *max, IArray<Coordinates2D> &min_loc, IArray<Coordinates2D> &max_loc, uint32_t &min_count, uint32_t &max_count) |
Giorgio Arena | 2ca209e | 2017-06-13 15:49:37 +0100 | [diff] [blame] | 53 | : _min(min), _max(max), _min_loc(min_loc), _max_loc(max_loc), _min_count(min_count), _max_count(max_count) |
| 54 | { |
| 55 | } |
| 56 | template <typename T1> |
| 57 | void operator()(const Tensor<T1> &in) const |
| 58 | { |
| 59 | tensor_operations::min_max_location(in, _min, _max, _min_loc, _max_loc, _min_count, _max_count); |
| 60 | } |
| 61 | |
| 62 | private: |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 63 | void *_min; |
| 64 | void *_max; |
Giorgio Arena | 935deee | 2017-06-14 13:40:36 +0100 | [diff] [blame] | 65 | IArray<Coordinates2D> &_min_loc; |
| 66 | IArray<Coordinates2D> &_max_loc; |
| 67 | uint32_t &_min_count; |
| 68 | uint32_t &_max_count; |
Giorgio Arena | 2ca209e | 2017-06-13 15:49:37 +0100 | [diff] [blame] | 69 | }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 70 | // Absolute Difference visitor |
| 71 | struct absolute_difference_visitor : public boost::static_visitor<> |
| 72 | { |
| 73 | public: |
| 74 | template <typename T1, typename T2, typename T3> |
| 75 | void operator()(const Tensor<T1> &in1, const Tensor<T2> &in2, Tensor<T3> &out) const |
| 76 | { |
| 77 | tensor_operations::absolute_difference(in1, in2, out); |
| 78 | } |
| 79 | }; |
| 80 | // Arithmetic Addition visitor |
| 81 | struct arithmetic_addition_visitor : public boost::static_visitor<> |
| 82 | { |
| 83 | public: |
| 84 | explicit arithmetic_addition_visitor(ConvertPolicy convert_policy) |
| 85 | : _policy(convert_policy) |
| 86 | { |
| 87 | } |
| 88 | |
| 89 | template <typename T1, typename T2, typename T3> |
| 90 | void operator()(const Tensor<T1> &in1, const Tensor<T2> &in2, Tensor<T3> &out) const |
| 91 | { |
| 92 | tensor_operations::arithmetic_addition(in1, in2, out, _policy); |
| 93 | } |
| 94 | |
| 95 | private: |
| 96 | ConvertPolicy _policy; |
| 97 | }; |
| 98 | // Arithmetic Subtraction visitor |
| 99 | struct arithmetic_subtraction_visitor : public boost::static_visitor<> |
| 100 | { |
| 101 | public: |
| 102 | explicit arithmetic_subtraction_visitor(ConvertPolicy convert_policy) |
| 103 | : _policy(convert_policy) |
| 104 | { |
| 105 | } |
| 106 | |
| 107 | template <typename T1, typename T2, typename T3> |
| 108 | void operator()(const Tensor<T1> &in1, const Tensor<T2> &in2, Tensor<T3> &out) const |
| 109 | { |
| 110 | tensor_operations::arithmetic_subtraction(in1, in2, out, _policy); |
| 111 | } |
| 112 | |
| 113 | private: |
| 114 | ConvertPolicy _policy; |
| 115 | }; |
| 116 | // Depth Convert visitor |
| 117 | struct depth_convert_visitor : public boost::static_visitor<> |
| 118 | { |
| 119 | public: |
| 120 | explicit depth_convert_visitor(ConvertPolicy policy, uint32_t shift) |
| 121 | : _policy(policy), _shift(shift) |
| 122 | { |
| 123 | } |
| 124 | |
| 125 | template <typename T1, typename T2> |
| 126 | void operator()(const Tensor<T1> &in, Tensor<T2> &out) const |
| 127 | { |
| 128 | tensor_operations::depth_convert(in, out, _policy, _shift); |
| 129 | } |
| 130 | |
| 131 | private: |
| 132 | ConvertPolicy _policy; |
| 133 | uint32_t _shift; |
| 134 | }; |
| 135 | // GEMM visitor |
| 136 | struct gemm_visitor : public boost::static_visitor<> |
| 137 | { |
| 138 | public: |
| 139 | explicit gemm_visitor(const TensorVariant &in1, const TensorVariant &in2, const TensorVariant &in3, float alpha, float beta) |
| 140 | : _in1(in1), _in2(in2), _in3(in3), _alpha(alpha), _beta(beta) |
| 141 | { |
| 142 | } |
| 143 | |
| 144 | template <typename T> |
| 145 | void operator()(Tensor<T> &out) const |
| 146 | { |
| 147 | const Tensor<T> &in1 = boost::get<Tensor<T>>(_in1); |
| 148 | const Tensor<T> &in2 = boost::get<Tensor<T>>(_in2); |
| 149 | const Tensor<T> &in3 = boost::get<Tensor<T>>(_in3); |
| 150 | tensor_operations::gemm(in1, in2, in3, out, _alpha, _beta); |
| 151 | } |
| 152 | |
| 153 | private: |
| 154 | const TensorVariant &_in1, &_in2, &_in3; |
| 155 | float _alpha; |
| 156 | float _beta; |
| 157 | }; |
| 158 | // Pixel-wise Multiplication visitor |
| 159 | struct pixel_wise_multiplication_visitor : public boost::static_visitor<> |
| 160 | { |
| 161 | public: |
| 162 | explicit pixel_wise_multiplication_visitor(float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy) |
| 163 | : _scale(scale), _convert_policy(convert_policy), _rounding_policy(rounding_policy) |
| 164 | { |
| 165 | } |
| 166 | |
| 167 | template <typename T1, typename T2, typename T3> |
| 168 | void operator()(const Tensor<T1> &in1, const Tensor<T2> &in2, Tensor<T3> &out) const |
| 169 | { |
| 170 | tensor_operations::pixel_wise_multiplication(in1, in2, out, _scale, _convert_policy, _rounding_policy); |
| 171 | } |
| 172 | |
| 173 | private: |
| 174 | float _scale; |
| 175 | ConvertPolicy _convert_policy; |
| 176 | RoundingPolicy _rounding_policy; |
| 177 | }; |
| 178 | // Fixed Point Pixel-wise Multiplication visitor |
| 179 | struct fixed_point_pixel_wise_multiplication_visitor : public boost::static_visitor<> |
| 180 | { |
| 181 | public: |
| 182 | explicit fixed_point_pixel_wise_multiplication_visitor(const TensorVariant &in1, const TensorVariant &in2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy) |
| 183 | : _in1(in1), _in2(in2), _scale(scale), _convert_policy(convert_policy), _rounding_policy(rounding_policy) |
| 184 | { |
| 185 | } |
| 186 | |
| 187 | template <typename T, typename = typename std::enable_if<std::is_integral<T>::value>::type> |
| 188 | void operator()(Tensor<T> &out) const |
| 189 | { |
| 190 | const Tensor<T> &in1 = boost::get<Tensor<T>>(_in1); |
| 191 | const Tensor<T> &in2 = boost::get<Tensor<T>>(_in2); |
| 192 | tensor_operations::fixed_point_pixel_wise_multiplication(in1, in2, out, _scale, _convert_policy, _rounding_policy); |
| 193 | } |
| 194 | template < typename T, typename std::enable_if < !std::is_integral<T>::value, int >::type = 0 > |
| 195 | void operator()(Tensor<T> &out) const |
| 196 | { |
| 197 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 198 | } |
| 199 | |
| 200 | private: |
| 201 | const TensorVariant &_in1; |
| 202 | const TensorVariant &_in2; |
| 203 | float _scale; |
| 204 | ConvertPolicy _convert_policy; |
| 205 | RoundingPolicy _rounding_policy; |
| 206 | }; |
Isabella Gottardi | b797fa2 | 2017-06-23 15:02:11 +0100 | [diff] [blame] | 207 | // Table lookup operation |
| 208 | template <typename T1> |
| 209 | struct table_lookup : public boost::static_visitor<> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 210 | { |
Isabella Gottardi | b797fa2 | 2017-06-23 15:02:11 +0100 | [diff] [blame] | 211 | public: |
| 212 | explicit table_lookup(const TensorVariant &in, std::map<T1, T1> &lut) |
| 213 | : _in(in), _lut(lut) |
| 214 | { |
| 215 | } |
| 216 | |
| 217 | template <typename T> |
| 218 | void operator()(Tensor<T> &out) const |
| 219 | { |
| 220 | const auto &in = boost::get<Tensor<T>>(_in); |
| 221 | tensor_operations::table_lookup(in, out, _lut); |
| 222 | } |
| 223 | |
| 224 | private: |
| 225 | const TensorVariant &_in; |
| 226 | std::map<T1, T1> &_lut; |
| 227 | }; |
| 228 | template struct arm_compute::test::validation::tensor_visitors::table_lookup<uint8_t>; |
| 229 | template struct arm_compute::test::validation::tensor_visitors::table_lookup<int16_t>; |
| 230 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 231 | // Batch Normalization Layer visitor |
| 232 | struct batch_normalization_layer_visitor : public boost::static_visitor<> |
| 233 | { |
| 234 | public: |
| 235 | explicit batch_normalization_layer_visitor(const TensorVariant &in, const TensorVariant &mean, const TensorVariant &var, const TensorVariant &beta, const TensorVariant &gamma, float epsilon, |
| 236 | int fixed_point_position = 0) |
| 237 | : _in(in), _mean(mean), _var(var), _beta(beta), _gamma(gamma), _epsilon(epsilon), _fixed_point_position(fixed_point_position) |
| 238 | { |
| 239 | } |
| 240 | |
| 241 | template <typename T> |
| 242 | void operator()(Tensor<T> &out) const |
| 243 | { |
| 244 | const Tensor<T> &in = boost::get<Tensor<T>>(_in); |
| 245 | const Tensor<T> &mean = boost::get<Tensor<T>>(_mean); |
| 246 | const Tensor<T> &var = boost::get<Tensor<T>>(_var); |
| 247 | const Tensor<T> &beta = boost::get<Tensor<T>>(_beta); |
| 248 | const Tensor<T> &gamma = boost::get<Tensor<T>>(_gamma); |
| 249 | tensor_operations::batch_normalization_layer(in, out, mean, var, beta, gamma, _epsilon, _fixed_point_position); |
| 250 | } |
| 251 | |
| 252 | private: |
| 253 | const TensorVariant &_in, &_mean, &_var, &_beta, &_gamma; |
| 254 | float _epsilon; |
| 255 | int _fixed_point_position; |
| 256 | }; |
| 257 | // Convolution Layer visitor |
| 258 | struct convolution_layer_visitor : public boost::static_visitor<> |
| 259 | { |
| 260 | public: |
| 261 | explicit convolution_layer_visitor(const TensorVariant &in, const TensorVariant &weights, const TensorVariant &bias, PadStrideInfo conv_info) |
| 262 | : _in(in), _weights(weights), _bias(bias), _conv_info(conv_info) |
| 263 | { |
| 264 | } |
| 265 | |
| 266 | template <typename T> |
| 267 | void operator()(Tensor<T> &out) const |
| 268 | { |
| 269 | const Tensor<T> &in = boost::get<Tensor<T>>(_in); |
| 270 | const Tensor<T> &weights = boost::get<Tensor<T>>(_weights); |
| 271 | const Tensor<T> &bias = boost::get<Tensor<T>>(_bias); |
| 272 | tensor_operations::convolution_layer(in, weights, bias, out, _conv_info); |
| 273 | } |
| 274 | |
| 275 | private: |
| 276 | const TensorVariant &_in; |
| 277 | const TensorVariant &_weights; |
| 278 | const TensorVariant &_bias; |
| 279 | PadStrideInfo _conv_info; |
| 280 | }; |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 281 | // Depth Concatenate Layer visitor |
| 282 | struct depth_concatenate_layer_visitor : public boost::static_visitor<> |
| 283 | { |
| 284 | public: |
| 285 | explicit depth_concatenate_layer_visitor(const std::vector<TensorVariant> &srcs) |
| 286 | : _srcs(srcs) |
| 287 | { |
| 288 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 289 | |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 290 | template <typename T> |
| 291 | void operator()(Tensor<T> &out) const |
| 292 | { |
| 293 | std::vector<const Tensor<T> *> srcs; |
| 294 | srcs.resize(_srcs.size()); |
| 295 | std::transform(_srcs.begin(), _srcs.end(), srcs.begin(), [](const TensorVariant & t) |
| 296 | { |
| 297 | return &(boost::get<Tensor<T>>(t)); |
| 298 | }); |
| 299 | tensor_operations::depth_concatenate_layer(srcs, out); |
| 300 | } |
| 301 | |
| 302 | private: |
| 303 | const std::vector<TensorVariant> &_srcs; |
| 304 | }; |
| 305 | // Fully Connected Layer visitor |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 306 | struct fully_connected_layer_visitor : public boost::static_visitor<> |
| 307 | { |
| 308 | public: |
| 309 | explicit fully_connected_layer_visitor(const TensorVariant &in, const TensorVariant &weights, const TensorVariant &bias) |
| 310 | : _in(in), _weights(weights), _bias(bias) |
| 311 | { |
| 312 | } |
| 313 | template <typename T> |
| 314 | void operator()(Tensor<T> &out) const |
| 315 | { |
| 316 | const Tensor<T> &in = boost::get<Tensor<T>>(_in); |
| 317 | const Tensor<T> &weights = boost::get<Tensor<T>>(_weights); |
| 318 | const Tensor<T> &bias = boost::get<Tensor<T>>(_bias); |
| 319 | tensor_operations::fully_connected_layer(in, weights, bias, out); |
| 320 | } |
| 321 | |
| 322 | private: |
| 323 | const TensorVariant &_in; |
| 324 | const TensorVariant &_weights; |
| 325 | const TensorVariant &_bias; |
| 326 | }; |
| 327 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 328 | // Pooling layer |
| 329 | struct pooling_layer_visitor : public boost::static_visitor<> |
| 330 | { |
| 331 | public: |
| 332 | explicit pooling_layer_visitor(const TensorVariant &in, PoolingLayerInfo pool_info, int fixed_point_position = 0) |
| 333 | : _in(in), _pool_info(pool_info), _fixed_point_position(fixed_point_position) |
| 334 | { |
| 335 | } |
| 336 | |
| 337 | template <typename T> |
| 338 | void operator()(Tensor<T> &out) const |
| 339 | { |
| 340 | const Tensor<T> &in = boost::get<Tensor<T>>(_in); |
| 341 | tensor_operations::pooling_layer(in, out, _pool_info, _fixed_point_position); |
| 342 | } |
| 343 | |
| 344 | private: |
| 345 | const TensorVariant &_in; |
| 346 | PoolingLayerInfo _pool_info; |
| 347 | int _fixed_point_position; |
| 348 | }; |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 349 | |
| 350 | // ROI Pooling layer |
| 351 | struct roi_pooling_layer_visitor : public boost::static_visitor<> |
| 352 | { |
| 353 | public: |
| 354 | explicit roi_pooling_layer_visitor(const TensorVariant &in, const std::vector<ROI> &rois, ROIPoolingLayerInfo pool_info) |
| 355 | : _in(in), _rois(rois), _pool_info(pool_info) |
| 356 | { |
| 357 | } |
| 358 | |
| 359 | template <typename T> |
| 360 | void operator()(Tensor<T> &out) const |
| 361 | { |
| 362 | const Tensor<T> &in = boost::get<Tensor<T>>(_in); |
| 363 | tensor_operations::roi_pooling_layer(in, out, _rois, _pool_info); |
| 364 | } |
| 365 | |
| 366 | private: |
| 367 | const TensorVariant &_in; |
| 368 | const std::vector<ROI> &_rois; |
| 369 | ROIPoolingLayerInfo _pool_info; |
| 370 | }; |
| 371 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 372 | // Fixed Point operations visitor |
| 373 | struct fixed_point_operation_visitor : public boost::static_visitor<> |
| 374 | { |
| 375 | public: |
| 376 | explicit fixed_point_operation_visitor(const TensorVariant &in, FixedPointOp op) |
| 377 | : _in(in), _op(op) |
| 378 | { |
| 379 | } |
| 380 | |
| 381 | template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0> |
| 382 | void operator()(Tensor<T> &out) const |
| 383 | { |
| 384 | const Tensor<T> &in = boost::get<Tensor<T>>(_in); |
| 385 | tensor_operations::fixed_point_operation(in, out, _op); |
| 386 | } |
| 387 | template < typename T, typename std::enable_if < !std::is_integral<T>::value, int >::type = 0 > |
| 388 | void operator()(Tensor<T> &out) const |
| 389 | { |
| 390 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 391 | } |
| 392 | |
| 393 | private: |
| 394 | const TensorVariant &_in; |
| 395 | FixedPointOp _op; |
| 396 | }; |
| 397 | // Print Tensor visitor |
| 398 | struct print_visitor : public boost::static_visitor<> |
| 399 | { |
| 400 | public: |
| 401 | explicit print_visitor(std::ostream &out) |
| 402 | : _out(out) |
| 403 | { |
| 404 | } |
| 405 | |
| 406 | template <typename T> |
| 407 | void operator()(const Tensor<T> &in) const |
| 408 | { |
| 409 | tensor_operations::print(in, _out); |
| 410 | } |
| 411 | |
| 412 | private: |
| 413 | std::ostream &_out; |
| 414 | }; |
| 415 | } // namespace tensor_visitors |
| 416 | } // namespace validation |
| 417 | } // namespace test |
| 418 | } // namespace arm_compute |
| 419 | |
| 420 | #endif /* __ARM_COMPUTE_TEST_TENSOR_VISITORS_H__ */ |