|
1 | 1 | /* |
2 | | - * Copyright (c) 2024 Arm Limited. |
| 2 | + * Copyright (c) 2024-2025 Arm Limited. |
3 | 3 | * |
4 | 4 | * SPDX-License-Identifier: MIT |
5 | 5 | * |
|
32 | 32 | #include "tests/IAccessor.h" |
33 | 33 | #include "tests/framework/Asserts.h" |
34 | 34 | #include "tests/framework/Fixture.h" |
| 35 | +#include "tests/validation/Helpers.h" |
35 | 36 | #include "tests/validation/reference/Permute.h" |
36 | 37 |
|
| 38 | +#if !defined(BARE_METAL) |
| 39 | +#include <thread> |
| 40 | +#include <vector> |
| 41 | +#endif // !defined(BARE_METAL) |
| 42 | + |
37 | 43 | namespace arm_compute |
38 | 44 | { |
39 | 45 | namespace test |
40 | 46 | { |
41 | 47 | namespace validation |
42 | 48 | { |
| 49 | +namespace |
| 50 | +{ |
| 51 | +constexpr int NUM_THREADS = 3; |
| 52 | +}// namespace |
43 | 53 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
44 | | -class CpuTransposeValidationFixture : public framework::Fixture |
| 54 | +class CpuTransposeGenericFixture : public framework::Fixture |
45 | 55 | { |
46 | 56 | public: |
47 | | - void setup(TensorShape shape, DataType data_type) |
| 57 | + void setup(TensorShape shape, DataType data_type, QuantizationInfo qinfo, TestType test_type = TestType::ConfigureOnceRunOnce) |
48 | 58 | { |
49 | | - _target = compute_target(shape, data_type); |
50 | | - _reference = compute_reference(shape, data_type); |
| 59 | + if (std::is_same<TensorType, Tensor>::value && // Cpu |
| 60 | + data_type == DataType::F16 && !CPUInfo::get().has_fp16()) |
| 61 | + { |
| 62 | + return; |
| 63 | + } |
| 64 | + _test_type = test_type; |
| 65 | + _num_parallel_runs = (_test_type == TestType::ConfigureOnceRunMultiThreaded ? NUM_THREADS : 1); |
| 66 | + |
| 67 | + compute_target(shape, data_type, qinfo); |
| 68 | + compute_reference(shape, data_type, qinfo); |
51 | 69 | } |
52 | 70 |
|
53 | 71 | protected: |
54 | 72 | template <typename U> |
55 | 73 | void fill(U &&tensor) |
56 | 74 | { |
57 | | - library->fill_tensor_uniform(tensor, 0); |
| 75 | + if(tensor.data_type() == DataType::F32) |
| 76 | + { |
| 77 | + std::uniform_real_distribution<float> distribution(-10.0f, 10.0f); |
| 78 | + library->fill(tensor, distribution, 0); |
| 79 | + } |
| 80 | + else if(tensor.data_type() == DataType::F16) |
| 81 | + { |
| 82 | + arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -10.0f, 10.0f }; |
| 83 | + library->fill(tensor, distribution, 0); |
| 84 | + } |
| 85 | + else if(!is_data_type_quantized(tensor.data_type())) |
| 86 | + { |
| 87 | + std::uniform_int_distribution<> distribution(0, 100); |
| 88 | + library->fill(tensor, distribution, 0); |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + library->fill_tensor_uniform(tensor, 0); |
| 93 | + } |
58 | 94 | } |
59 | 95 |
|
60 | | - TensorType compute_target(const TensorShape &shape, DataType data_type) |
61 | | - { |
62 | | - // Make rows the columns of the original shape |
63 | | - TensorShape output_shape{ shape[1], shape[0] }; |
| 96 | + void allocate_and_fill_tensors(TensorType *src, TensorType *dst){ |
| 97 | + for(int i = 0; i < _num_parallel_runs; ++i) { |
64 | 98 |
|
65 | | - // Create tensors |
66 | | - TensorType src = create_tensor<TensorType>(shape, data_type); |
67 | | - TensorType dst = create_tensor<TensorType>(output_shape, data_type); |
| 99 | + ARM_COMPUTE_ASSERT(src[i].info()->is_resizable()); |
| 100 | + ARM_COMPUTE_ASSERT(dst[i].info()->is_resizable()); |
68 | 101 |
|
69 | | - // Create and configure function |
70 | | - FunctionType trans_func; |
71 | | - trans_func.configure(src.info(), dst.info()); |
| 102 | + // Allocate tensors |
| 103 | + src[i].allocator()->allocate(); |
| 104 | + dst[i].allocator()->allocate(); |
72 | 105 |
|
73 | | - ARM_COMPUTE_ASSERT(src.info()->is_resizable()); |
74 | | - ARM_COMPUTE_ASSERT(dst.info()->is_resizable()); |
| 106 | + ARM_COMPUTE_ASSERT(!src[i].info()->is_resizable()); |
| 107 | + ARM_COMPUTE_ASSERT(!dst[i].info()->is_resizable()); |
75 | 108 |
|
76 | | - // Allocate tensors |
77 | | - src.allocator()->allocate(); |
78 | | - dst.allocator()->allocate(); |
| 109 | + // Fill tensors |
| 110 | + fill(AccessorType(src[i])); |
| 111 | + } |
| 112 | + } |
79 | 113 |
|
80 | | - ARM_COMPUTE_ASSERT(!src.info()->is_resizable()); |
81 | | - ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); |
| 114 | + void compute_target(const TensorShape &shape, DataType data_type, QuantizationInfo qinfo) |
| 115 | + { |
| 116 | + // Create tensors |
| 117 | + TensorType src[NUM_THREADS]; |
| 118 | + TensorType dst[NUM_THREADS]; |
| 119 | + TensorType *dst_ptrs[NUM_THREADS]; |
82 | 120 |
|
83 | | - // Fill tensors |
84 | | - fill(AccessorType(src)); |
| 121 | + // Retain the shape but make rows the columns of the original shape |
| 122 | + TensorShape output_shape = shape; |
| 123 | + std::swap(output_shape[0], output_shape[1]); |
85 | 124 |
|
86 | | - // Compute function |
87 | | - ITensorPack run_pack{ { arm_compute::TensorType::ACL_SRC, &src }, { arm_compute::TensorType::ACL_DST, &dst } }; |
88 | | - trans_func.run(run_pack); |
| 125 | + for(int i = 0; i < _num_parallel_runs; ++i){ |
| 126 | + src[i] = create_tensor<TensorType>(shape, data_type, 1, qinfo); |
| 127 | + dst[i] = create_tensor<TensorType>(output_shape, data_type, 1, qinfo); |
| 128 | + dst_ptrs[i] = &dst[i]; |
| 129 | + } |
89 | 130 |
|
90 | | - return dst; |
| 131 | + // Create and configure function |
| 132 | + FunctionType trans_func; |
| 133 | + trans_func.configure(src[0].info(), dst_ptrs[0]->info()); |
| 134 | + |
| 135 | + allocate_and_fill_tensors(src, dst); |
| 136 | + |
| 137 | + if(_test_type == TestType::ConfigureOnceRunMultiThreaded) |
| 138 | + { |
| 139 | +#ifndef BARE_METAL |
| 140 | + |
| 141 | + ITensorPack run_pack[NUM_THREADS]; |
| 142 | + std::vector<std::thread> threads; |
| 143 | + |
| 144 | + threads.reserve(_num_parallel_runs); |
| 145 | + for(int i = 0; i < _num_parallel_runs; ++i) |
| 146 | + { |
| 147 | + // Compute function |
| 148 | + run_pack[i] = { {arm_compute::TensorType::ACL_SRC, &src[i]}, |
| 149 | + {arm_compute::TensorType::ACL_DST, dst_ptrs[i]}}; |
| 150 | + |
| 151 | + threads.emplace_back([&,i] |
| 152 | + { |
| 153 | + trans_func.run(run_pack[i]); |
| 154 | + _target[i] = std::move(*(dst_ptrs[i])); |
| 155 | + }); |
| 156 | + } |
| 157 | + for(int i = 0; i < _num_parallel_runs; ++i) |
| 158 | + { |
| 159 | + threads[i].join(); |
| 160 | + } |
| 161 | +#endif // ifndef BARE_METAL |
| 162 | + } |
| 163 | + else |
| 164 | + { |
| 165 | + // Compute function |
| 166 | + ITensorPack run_pack{{ arm_compute::TensorType::ACL_SRC, &src[0]}, |
| 167 | + {arm_compute::TensorType::ACL_DST, dst_ptrs[0]}}; |
| 168 | + trans_func.run(run_pack); |
| 169 | + _target[0] = std::move(*(dst_ptrs[0])); |
| 170 | + } |
91 | 171 | } |
92 | 172 |
|
93 | | - SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type) |
| 173 | + void compute_reference(const TensorShape &shape, DataType data_type, QuantizationInfo qinfo) |
94 | 174 | { |
95 | 175 | // Create reference |
96 | | - SimpleTensor<T> src{ shape, data_type }; |
| 176 | + SimpleTensor<T> src{shape, data_type, 1, qinfo}; |
| 177 | + |
| 178 | + for(int i = 0; i < _num_parallel_runs; ++i) |
| 179 | + { |
| 180 | + // Fill reference |
| 181 | + fill(src); |
| 182 | + _reference[i] = reference::permute<T>(src, PermutationVector(1U, 0U)); |
| 183 | + } |
| 184 | + } |
97 | 185 |
|
98 | | - // Fill reference |
99 | | - fill(src); |
| 186 | + TensorType _target[NUM_THREADS]; |
| 187 | + SimpleTensor<T> _reference[NUM_THREADS]; |
| 188 | + TestType _test_type{}; |
| 189 | + int _num_parallel_runs{}; |
| 190 | +}; |
100 | 191 |
|
101 | | - return reference::permute<T>(src, PermutationVector(1U, 0U)); |
| 192 | +template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 193 | +class CpuTransposeValidationFixture |
| 194 | + : public CpuTransposeGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 195 | +{ |
| 196 | +public: |
| 197 | + void setup(const TensorShape &shape, DataType data_type) |
| 198 | + { |
| 199 | + CpuTransposeGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, QuantizationInfo()); |
102 | 200 | } |
| 201 | +}; |
103 | 202 |
|
104 | | - TensorType _target{}; |
105 | | - SimpleTensor<T> _reference{}; |
| 203 | +template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 204 | +class CpuTransposeThreadSafeValidationFixture |
| 205 | + : public CpuTransposeGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 206 | +{ |
| 207 | +public: |
| 208 | + void setup(const TensorShape &shape, DataType data_type) |
| 209 | + { |
| 210 | + CpuTransposeGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, QuantizationInfo(), |
| 211 | + TestType::ConfigureOnceRunMultiThreaded); |
| 212 | + } |
106 | 213 | }; |
| 214 | + |
| 215 | +template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 216 | +class CpuTransposeQuantizedThreadSafeValidationFixture |
| 217 | + : public CpuTransposeGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 218 | +{ |
| 219 | +public: |
| 220 | + void setup(const TensorShape &shape, DataType data_type, QuantizationInfo qinfo) |
| 221 | + { |
| 222 | + CpuTransposeGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, qinfo, |
| 223 | + TestType::ConfigureOnceRunMultiThreaded); |
| 224 | + } |
| 225 | +}; |
| 226 | + |
107 | 227 | } // namespace validation |
108 | 228 | } // namespace test |
109 | 229 | } // namespace arm_compute |
|
0 commit comments