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 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | namespace test |
| 36 | { |
| 37 | namespace validation |
| 38 | { |
| 39 | using TensorVariant = boost::variant < Tensor<uint8_t>, Tensor<int8_t>, |
| 40 | Tensor<uint16_t>, Tensor<int16_t>, |
| 41 | Tensor<uint32_t>, Tensor<int32_t>, |
| 42 | #ifdef ENABLE_FP16 |
| 43 | Tensor<float16_t>, |
| 44 | #endif |
| 45 | Tensor<float >>; |
| 46 | |
| 47 | /** Helper to create a constant type if the passed reference is constant. */ |
| 48 | template <typename R, typename T> |
| 49 | struct match_const |
| 50 | { |
| 51 | using type = typename std::conditional<std::is_const<typename std::remove_reference<R>::type>::value, const T, T>::type; |
| 52 | }; |
| 53 | |
| 54 | class TensorFactory |
| 55 | { |
| 56 | public: |
| 57 | template <typename R> |
| 58 | static TensorVariant get_tensor(R &&raw) |
| 59 | { |
| 60 | TensorVariant v; |
| 61 | DataType dt = raw.data_type(); |
| 62 | int fixed_point_position = raw.fixed_point_position(); |
| 63 | auto shape = raw.shape(); |
| 64 | auto data = raw.data(); |
| 65 | |
| 66 | switch(dt) |
| 67 | { |
| 68 | case DataType::U8: |
| 69 | using value_type_u8 = typename match_const<R, uint8_t>::type; |
| 70 | v = Tensor<uint8_t>(shape, dt, fixed_point_position, reinterpret_cast<value_type_u8 *>(data)); |
| 71 | break; |
| 72 | case DataType::S8: |
| 73 | case DataType::QS8: |
| 74 | using value_type_s8 = typename match_const<R, int8_t>::type; |
| 75 | v = Tensor<int8_t>(shape, dt, fixed_point_position, reinterpret_cast<value_type_s8 *>(data)); |
| 76 | break; |
| 77 | case DataType::U16: |
| 78 | using value_type_u16 = typename match_const<R, uint16_t>::type; |
| 79 | v = Tensor<uint16_t>(shape, dt, fixed_point_position, reinterpret_cast<value_type_u16 *>(data)); |
| 80 | break; |
| 81 | case DataType::S16: |
| 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; |
| 93 | #ifdef ENABLE_FP16 |
| 94 | case DataType::F16: |
| 95 | using value_type_f16 = typename match_const<R, float16_t>::type; |
| 96 | v = Tensor<float16_t>(raw.shape(), dt, reinterpret_cast<value_type_f16 *>(raw.data())); |
| 97 | break; |
| 98 | #endif |
| 99 | case DataType::F32: |
| 100 | using value_type_f32 = typename match_const<R, float>::type; |
| 101 | v = Tensor<float>(shape, dt, fixed_point_position, reinterpret_cast<value_type_f32 *>(data)); |
| 102 | break; |
| 103 | default: |
| 104 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 105 | } |
| 106 | return v; |
| 107 | } |
| 108 | }; |
| 109 | } // namespace validation |
| 110 | } // namespace test |
| 111 | } // namespace arm_compute |
| 112 | |
| 113 | #endif /* __ARM_COMPUTE_TEST_TENSOR_FACTORY_H__ */ |