Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 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_RANGE_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_RANGE_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | #include "tests/AssetsLibrary.h" |
| 30 | #include "tests/Globals.h" |
| 31 | #include "tests/IAccessor.h" |
| 32 | #include "tests/framework/Asserts.h" |
| 33 | #include "tests/framework/Fixture.h" |
| 34 | #include "tests/validation/Helpers.h" |
| 35 | #include "tests/validation/reference/Range.h" |
| 36 | |
| 37 | #include <algorithm> |
| 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | namespace test |
| 42 | { |
| 43 | namespace validation |
| 44 | { |
| 45 | namespace |
| 46 | { |
| 47 | size_t num_of_elements_in_range(float start, float end, float step) |
| 48 | { |
| 49 | ARM_COMPUTE_ERROR_ON(step == 0); |
| 50 | return size_t(std::ceil((end - start) / step)); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 55 | class RangeFixture : public framework::Fixture |
| 56 | { |
| 57 | public: |
| 58 | template <typename...> |
| 59 | void setup(const DataType data_type0, float start, float step, const QuantizationInfo qinfo0 = QuantizationInfo()) |
| 60 | { |
| 61 | _target = compute_target(data_type0, qinfo0, start, step); |
| 62 | _reference = compute_reference(data_type0, qinfo0, start, step); |
| 63 | } |
| 64 | |
| 65 | protected: |
| 66 | float get_random_end(const DataType output_data_type, const QuantizationInfo qinfo_out, float start, float step) |
| 67 | { |
| 68 | std::uniform_real_distribution<> distribution(1, 100); |
| 69 | std::mt19937 gen(library->seed()); |
| 70 | float end = start; |
| 71 | switch(output_data_type) |
| 72 | { |
| 73 | case DataType::U8: |
| 74 | end += std::max((uint8_t)1, static_cast<uint8_t>(distribution(gen))) * step; |
| 75 | return utility::clamp<float, uint8_t>(end); |
| 76 | case DataType::U16: |
| 77 | end += std::max((uint16_t)1, static_cast<uint16_t>(distribution(gen))) * step; |
| 78 | return utility::clamp<float, uint16_t>(end); |
| 79 | case DataType::U32: |
| 80 | end += std::max((uint32_t)1, static_cast<uint32_t>(distribution(gen))) * step; |
| 81 | return utility::clamp<float, uint32_t>(end); |
| 82 | case DataType::S8: |
| 83 | end += std::max((int8_t)1, static_cast<int8_t>(distribution(gen))) * step; |
| 84 | return utility::clamp<float, int8_t>(end); |
| 85 | case DataType::S16: |
| 86 | end += std::max((int16_t)1, static_cast<int16_t>(distribution(gen))) * step; |
| 87 | return utility::clamp<float, int16_t>(end); |
| 88 | case DataType::S32: |
| 89 | end += std::max((int32_t)1, static_cast<int32_t>(distribution(gen))) * step; |
| 90 | return utility::clamp<float, int32_t>(end); |
| 91 | case DataType::F32: |
| 92 | end += std::max(1.0f, static_cast<float>(distribution(gen))) * step; |
| 93 | return end; |
| 94 | case DataType::F16: |
| 95 | end += std::max(half(1.0f), static_cast<half>(distribution(gen))) * step; |
| 96 | return utility::clamp<float, half>(end); |
| 97 | case DataType::QASYMM8: |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 98 | return utility::clamp<float, uint8_t>(end + (float)distribution(gen) * step, |
| 99 | dequantize_qasymm8(0, qinfo_out.uniform()), |
| 100 | dequantize_qasymm8(std::numeric_limits<uint8_t>::max(), qinfo_out.uniform())); |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 101 | default: |
| 102 | return 0; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | TensorType compute_target(const DataType output_data_type, const QuantizationInfo qinfo_out, float start, float step) |
| 107 | { |
| 108 | float end = get_random_end(output_data_type, qinfo_out, start, step); |
| 109 | size_t num_of_elements = num_of_elements_in_range(start, end, step); |
| 110 | // Create tensor |
| 111 | TensorType dst = create_tensor<TensorType>(TensorShape(num_of_elements), output_data_type, 1, qinfo_out); |
| 112 | // Create and configure function |
| 113 | FunctionType range_func; |
| 114 | range_func.configure(&dst, start, end, step); |
| 115 | |
| 116 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 117 | // Allocate tensors |
| 118 | dst.allocator()->allocate(); |
| 119 | |
| 120 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 121 | |
| 122 | // Compute function |
| 123 | range_func.run(); |
| 124 | return dst; |
| 125 | } |
| 126 | |
| 127 | SimpleTensor<T> compute_reference(const DataType output_data_type, const QuantizationInfo qinfo_out, float start, float step) |
| 128 | { |
| 129 | // Create tensor |
| 130 | const float end = get_random_end(output_data_type, qinfo_out, start, step); |
| 131 | size_t num_of_elements = num_of_elements_in_range(start, end, step); |
| 132 | SimpleTensor<T> ref_dst{ TensorShape(num_of_elements ? num_of_elements : 1), output_data_type, 1, qinfo_out }; |
| 133 | return reference::range<T>(ref_dst, start, num_of_elements, step); |
| 134 | } |
| 135 | |
| 136 | TensorType _target{}; |
| 137 | SimpleTensor<T> _reference{}; |
| 138 | }; |
| 139 | } // namespace validation |
| 140 | } // namespace test |
| 141 | } // namespace arm_compute |
| 142 | #endif /* ARM_COMPUTE_TEST_RANGE_FIXTURE */ |