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_FACTORY_H__ |
| 25 | #define __ARM_COMPUTE_TEST_TENSOR_FACTORY_H__ |
| 26 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Error.h" |
Moritz Pflanzer | e49e266 | 2017-07-21 15:55:28 +0100 | [diff] [blame] | 28 | #include "tests/RawTensor.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 29 | #include "tests/validation_old/Tensor.h" |
| 30 | #include "tests/validation_old/half.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 32 | #include "tests/validation_old/boost_wrapper.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 33 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | namespace test |
| 37 | { |
| 38 | namespace validation |
| 39 | { |
Moritz Pflanzer | e49e266 | 2017-07-21 15:55:28 +0100 | [diff] [blame] | 40 | using TensorVariant = boost::variant<Tensor<uint8_t>, Tensor<int8_t>, |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 41 | Tensor<uint16_t>, Tensor<int16_t>, |
| 42 | Tensor<uint32_t>, Tensor<int32_t>, |
Moritz Pflanzer | e49e266 | 2017-07-21 15:55:28 +0100 | [diff] [blame] | 43 | Tensor<half_float::half>, |
| 44 | Tensor<float>>; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 45 | |
| 46 | /** Helper to create a constant type if the passed reference is constant. */ |
| 47 | template <typename R, typename T> |
| 48 | struct match_const |
| 49 | { |
| 50 | using type = typename std::conditional<std::is_const<typename std::remove_reference<R>::type>::value, const T, T>::type; |
| 51 | }; |
| 52 | |
| 53 | class TensorFactory |
| 54 | { |
| 55 | public: |
| 56 | template <typename R> |
| 57 | static TensorVariant get_tensor(R &&raw) |
| 58 | { |
| 59 | TensorVariant v; |
| 60 | DataType dt = raw.data_type(); |
| 61 | int fixed_point_position = raw.fixed_point_position(); |
| 62 | auto shape = raw.shape(); |
| 63 | auto data = raw.data(); |
| 64 | |
| 65 | switch(dt) |
| 66 | { |
| 67 | case DataType::U8: |
| 68 | using value_type_u8 = typename match_const<R, uint8_t>::type; |
| 69 | v = Tensor<uint8_t>(shape, dt, fixed_point_position, reinterpret_cast<value_type_u8 *>(data)); |
| 70 | break; |
| 71 | case DataType::S8: |
| 72 | case DataType::QS8: |
| 73 | using value_type_s8 = typename match_const<R, int8_t>::type; |
| 74 | v = Tensor<int8_t>(shape, dt, fixed_point_position, reinterpret_cast<value_type_s8 *>(data)); |
| 75 | break; |
| 76 | case DataType::U16: |
| 77 | using value_type_u16 = typename match_const<R, uint16_t>::type; |
| 78 | v = Tensor<uint16_t>(shape, dt, fixed_point_position, reinterpret_cast<value_type_u16 *>(data)); |
| 79 | break; |
| 80 | case DataType::S16: |
Michalis Spyrou | 0a8334c | 2017-06-14 18:00:05 +0100 | [diff] [blame] | 81 | case DataType::QS16: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 82 | using value_type_s16 = typename match_const<R, int16_t>::type; |
| 83 | v = Tensor<int16_t>(shape, dt, fixed_point_position, reinterpret_cast<value_type_s16 *>(data)); |
| 84 | break; |
| 85 | case DataType::U32: |
| 86 | using value_type_u32 = typename match_const<R, uint32_t>::type; |
| 87 | v = Tensor<uint32_t>(shape, dt, fixed_point_position, reinterpret_cast<value_type_u32 *>(data)); |
| 88 | break; |
| 89 | case DataType::S32: |
| 90 | using value_type_s32 = typename match_const<R, int32_t>::type; |
| 91 | v = Tensor<int32_t>(shape, dt, fixed_point_position, reinterpret_cast<value_type_s32 *>(data)); |
| 92 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | case DataType::F16: |
Moritz Pflanzer | e49e266 | 2017-07-21 15:55:28 +0100 | [diff] [blame] | 94 | using value_type_f16 = typename match_const<R, half_float::half>::type; |
| 95 | v = Tensor<half_float::half>(shape, dt, fixed_point_position, reinterpret_cast<value_type_f16 *>(data)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 96 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 97 | case DataType::F32: |
| 98 | using value_type_f32 = typename match_const<R, float>::type; |
| 99 | v = Tensor<float>(shape, dt, fixed_point_position, reinterpret_cast<value_type_f32 *>(data)); |
| 100 | break; |
| 101 | default: |
| 102 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 103 | } |
| 104 | return v; |
| 105 | } |
| 106 | }; |
| 107 | } // namespace validation |
| 108 | } // namespace test |
| 109 | } // namespace arm_compute |
| 110 | |
| 111 | #endif /* __ARM_COMPUTE_TEST_TENSOR_FACTORY_H__ */ |