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