blob: 3f5b960927f7854657d2302a32dd218db7e296ec [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
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/Error.h"
Moritz Pflanzere49e2662017-07-21 15:55:28 +010028#include "tests/RawTensor.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010029#include "tests/validation_old/Tensor.h"
30#include "tests/validation_old/half.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010032#include "tests/validation_old/boost_wrapper.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033
34namespace arm_compute
35{
36namespace test
37{
38namespace validation
39{
Moritz Pflanzere49e2662017-07-21 15:55:28 +010040using TensorVariant = boost::variant<Tensor<uint8_t>, Tensor<int8_t>,
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041 Tensor<uint16_t>, Tensor<int16_t>,
42 Tensor<uint32_t>, Tensor<int32_t>,
Moritz Pflanzere49e2662017-07-21 15:55:28 +010043 Tensor<half_float::half>,
44 Tensor<float>>;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045
46/** Helper to create a constant type if the passed reference is constant. */
47template <typename R, typename T>
48struct 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
53class TensorFactory
54{
55public:
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 Spyrou0a8334c2017-06-14 18:00:05 +010081 case DataType::QS16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082 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 Barbier6ff3b192017-09-04 18:44:23 +010093 case DataType::F16:
Moritz Pflanzere49e2662017-07-21 15:55:28 +010094 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 Barbier6ff3b192017-09-04 18:44:23 +010096 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097 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__ */