blob: 610425bbfb88f1f3016f775a00fca0c9738aba6a [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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 Tello383deec2017-06-23 10:40:05 +010033#if ARM_COMPUTE_ENABLE_FP16
34#include <arm_fp16.h> // needed for float16_t
35#endif
36
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037namespace arm_compute
38{
39namespace test
40{
41namespace validation
42{
43using TensorVariant = boost::variant < Tensor<uint8_t>, Tensor<int8_t>,
44 Tensor<uint16_t>, Tensor<int16_t>,
45 Tensor<uint32_t>, Tensor<int32_t>,
Pablo Tello383deec2017-06-23 10:40:05 +010046#ifdef ARM_COMPUTE_ENABLE_FP16
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047 Tensor<float16_t>,
48#endif
49 Tensor<float >>;
50
51/** Helper to create a constant type if the passed reference is constant. */
52template <typename R, typename T>
53struct 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
58class TensorFactory
59{
60public:
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 Tello383deec2017-06-23 10:40:05 +010097#ifdef ARM_COMPUTE_ENABLE_FP16
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098 case DataType::F16:
99 using value_type_f16 = typename match_const<R, float16_t>::type;
Pablo Tello383deec2017-06-23 10:40:05 +0100100 v = Tensor<float16_t>(shape, dt, fixed_point_position, reinterpret_cast<value_type_f16 *>(data));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101 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__ */