blob: 2f33dd283dbabd1ef7554c88388c5306e7bce0e3 [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
Anthony Barbierac69aa12017-07-03 17:39:37 +010035#endif /* ARM_COMPUTE_ENABLE_FP16 */
Pablo Tello383deec2017-06-23 10:40:05 +010036
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>,
Anthony Barbierac69aa12017-07-03 17:39:37 +010048#endif /* ARM_COMPUTE_ENABLE_FP16 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049 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:
Michalis Spyrou0a8334c2017-06-14 18:00:05 +010086 case DataType::QS16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087 using value_type_s16 = typename match_const<R, int16_t>::type;
88 v = Tensor<int16_t>(shape, dt, fixed_point_position, reinterpret_cast<value_type_s16 *>(data));
89 break;
90 case DataType::U32:
91 using value_type_u32 = typename match_const<R, uint32_t>::type;
92 v = Tensor<uint32_t>(shape, dt, fixed_point_position, reinterpret_cast<value_type_u32 *>(data));
93 break;
94 case DataType::S32:
95 using value_type_s32 = typename match_const<R, int32_t>::type;
96 v = Tensor<int32_t>(shape, dt, fixed_point_position, reinterpret_cast<value_type_s32 *>(data));
97 break;
Pablo Tello383deec2017-06-23 10:40:05 +010098#ifdef ARM_COMPUTE_ENABLE_FP16
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099 case DataType::F16:
100 using value_type_f16 = typename match_const<R, float16_t>::type;
Pablo Tello383deec2017-06-23 10:40:05 +0100101 v = Tensor<float16_t>(shape, dt, fixed_point_position, reinterpret_cast<value_type_f16 *>(data));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102 break;
Anthony Barbierac69aa12017-07-03 17:39:37 +0100103#endif /* ARM_COMPUTE_ENABLE_FP16 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104 case DataType::F32:
105 using value_type_f32 = typename match_const<R, float>::type;
106 v = Tensor<float>(shape, dt, fixed_point_position, reinterpret_cast<value_type_f32 *>(data));
107 break;
108 default:
109 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
110 }
111 return v;
112 }
113};
114} // namespace validation
115} // namespace test
116} // namespace arm_compute
117
118#endif /* __ARM_COMPUTE_TEST_TENSOR_FACTORY_H__ */