Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 1 | /* |
Sheri Zhang | ac6499a | 2021-02-10 15:32:38 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021 Arm Limited. |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +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 | */ |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 24 | #include "src/core/NEON/kernels/NERangeKernel.h" |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/ITensor.h" |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/TensorInfo.h" |
| 30 | #include "arm_compute/core/Validate.h" |
Georgios Pinitas | ddb93bb | 2020-10-02 16:38:59 +0100 | [diff] [blame] | 31 | #include "src/core/NEON/NEAsymm.h" |
| 32 | #include "src/core/NEON/wrapper/wrapper.h" |
Yair Schwarzbaum | 9e8a770 | 2021-11-08 10:58:06 +0200 | [diff] [blame] | 33 | #include "src/core/common/Registrars.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 34 | #include "src/core/helpers/AutoConfiguration.h" |
| 35 | #include "src/core/helpers/WindowHelpers.h" |
Yair Schwarzbaum | 9e8a770 | 2021-11-08 10:58:06 +0200 | [diff] [blame] | 36 | #include "src/cpu/kernels/range/list.h" |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | namespace |
| 41 | { |
Yair Schwarzbaum | 9e8a770 | 2021-11-08 10:58:06 +0200 | [diff] [blame] | 42 | struct RangeSelectorData |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 43 | { |
Yair Schwarzbaum | 9e8a770 | 2021-11-08 10:58:06 +0200 | [diff] [blame] | 44 | DataType dt; |
| 45 | }; |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 46 | |
Yair Schwarzbaum | 9e8a770 | 2021-11-08 10:58:06 +0200 | [diff] [blame] | 47 | using RangeSelectorPtr = std::add_pointer<bool(const RangeSelectorData &data)>::type; |
| 48 | using RangeUKernelPtr = std::add_pointer<void(ITensor *, float, float, const Window &)>::type; |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 49 | |
Yair Schwarzbaum | 9e8a770 | 2021-11-08 10:58:06 +0200 | [diff] [blame] | 50 | struct RangeUKernel |
| 51 | { |
| 52 | const char *name; |
| 53 | const RangeSelectorPtr is_selected; |
| 54 | RangeUKernelPtr ukernel; |
| 55 | }; |
Michalis Spyrou | cd5b4a3 | 2020-02-27 15:35:53 +0000 | [diff] [blame] | 56 | |
Yair Schwarzbaum | 9e8a770 | 2021-11-08 10:58:06 +0200 | [diff] [blame] | 57 | static const RangeUKernel available_kernels[] = |
| 58 | { |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 59 | { |
Yair Schwarzbaum | 9e8a770 | 2021-11-08 10:58:06 +0200 | [diff] [blame] | 60 | "fp16_neon_range", |
| 61 | [](const RangeSelectorData & data) { return data.dt == DataType::F16; }, |
| 62 | REGISTER_FP16_NEON(arm_compute::cpu::fp16_neon_range_function) |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 63 | }, |
Yair Schwarzbaum | 9e8a770 | 2021-11-08 10:58:06 +0200 | [diff] [blame] | 64 | { |
| 65 | "f32_neon_range", |
| 66 | [](const RangeSelectorData & data) { return data.dt == DataType::F32; }, |
| 67 | REGISTER_FP32_NEON(arm_compute::cpu::fp32_neon_range_function) |
| 68 | }, |
| 69 | { |
| 70 | "u8_neon_range", |
| 71 | [](const RangeSelectorData & data) { return data.dt == DataType::U8; }, |
| 72 | REGISTER_INTEGER_NEON(arm_compute::cpu::u8_neon_range_function) |
| 73 | }, |
| 74 | { |
| 75 | "u16_neon_range", |
| 76 | [](const RangeSelectorData & data) { return data.dt == DataType::U16; }, |
| 77 | REGISTER_INTEGER_NEON(arm_compute::cpu::u16_neon_range_function) |
| 78 | }, |
| 79 | { |
| 80 | "u32_neon_range", |
| 81 | [](const RangeSelectorData & data) { return data.dt == DataType::U32; }, |
| 82 | REGISTER_INTEGER_NEON(arm_compute::cpu::u32_neon_range_function) |
| 83 | }, |
| 84 | { |
| 85 | "s8_neon_range", |
| 86 | [](const RangeSelectorData & data) { return data.dt == DataType::S8; }, |
| 87 | REGISTER_INTEGER_NEON(arm_compute::cpu::s8_neon_range_function) |
| 88 | }, |
| 89 | { |
| 90 | "s16_neon_range", |
| 91 | [](const RangeSelectorData & data) { return data.dt == DataType::S16; }, |
| 92 | REGISTER_INTEGER_NEON(arm_compute::cpu::s16_neon_range_function) |
| 93 | }, |
| 94 | { |
| 95 | "s32_neon_range", |
| 96 | [](const RangeSelectorData & data) { return data.dt == DataType::S32; }, |
| 97 | REGISTER_INTEGER_NEON(arm_compute::cpu::s32_neon_range_function) |
| 98 | }, |
| 99 | }; |
| 100 | |
| 101 | /** Micro-kernel selector |
| 102 | * |
| 103 | * @param[in] data Selection data passed to help pick the appropriate micro-kernel |
| 104 | * |
| 105 | * @return A matching micro-kernel else nullptr |
| 106 | */ |
| 107 | const RangeUKernel *get_implementation(const RangeSelectorData &data) |
| 108 | { |
| 109 | for(const auto &uk : available_kernels) |
| 110 | { |
| 111 | if(uk.is_selected(data)) |
| 112 | { |
| 113 | return &uk; |
| 114 | } |
| 115 | } |
| 116 | return nullptr; |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | Status validate_arguments(const ITensorInfo &output, const float start, const float end, const float step) |
| 120 | { |
Yair Schwarzbaum | 9e8a770 | 2021-11-08 10:58:06 +0200 | [diff] [blame] | 121 | const auto *uk = get_implementation(RangeSelectorData{ output.data_type() }); |
| 122 | ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr); |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 123 | |
| 124 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((start == end), "start of the requested sequence must not be equal to the end"); |
| 125 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(((start < end) && (step <= 0)), "step must be greater than 0 when start < end"); |
| 126 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(((start > end) && (step >= 0)), "step must be less than 0 when start > end"); |
| 127 | |
Michalis Spyrou | f63885b | 2019-01-16 14:18:09 +0000 | [diff] [blame] | 128 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!check_value_range(start, output.data_type(), output.quantization_info()), "start value is outside the range of the data type"); |
| 129 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!check_value_range(end, output.data_type(), output.quantization_info()), "end value is outside the range of the data type"); |
| 130 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!check_value_range(step, output.data_type(), output.quantization_info()), "step value is outside the range of the data type"); |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 131 | |
| 132 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((start == end), "start of the requested sequence must not be equal to the end"); |
| 133 | |
| 134 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output.num_dimensions() != 1, "Output has to be a 1-D tensor"); |
Michalis Spyrou | f63885b | 2019-01-16 14:18:09 +0000 | [diff] [blame] | 135 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output.tensor_shape().total_size() < num_of_elements_in_range(start, end, step), "Output tensor size is incorrect"); |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 136 | |
| 137 | return Status{}; |
| 138 | } |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 139 | } // namespace |
| 140 | |
| 141 | NERangeKernel::NERangeKernel() |
Yair Schwarzbaum | 9e8a770 | 2021-11-08 10:58:06 +0200 | [diff] [blame] | 142 | : _start(0), _end(1), _step(1), _output(nullptr) |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 143 | { |
| 144 | } |
| 145 | |
| 146 | void NERangeKernel::configure(ITensor *output, float start, float end, float step) |
| 147 | { |
| 148 | ARM_COMPUTE_ERROR_ON_NULLPTR(output); |
| 149 | |
| 150 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*(output->info()), start, end, step)); |
| 151 | |
Michalis Spyrou | cd5b4a3 | 2020-02-27 15:35:53 +0000 | [diff] [blame] | 152 | // Auto initialize output if not initialized |
| 153 | auto_init_if_empty(*output->info(), TensorShape(num_of_elements_in_range(start, end, step)), 1, output->info()->data_type(), output->info()->quantization_info()); |
| 154 | |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 155 | // Configure kernel window |
SiCongLi | b88272e | 2021-02-24 15:40:57 +0000 | [diff] [blame] | 156 | Window win = calculate_max_window(*output->info(), Steps()); |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 157 | |
| 158 | _start = start; |
| 159 | _end = end; |
| 160 | _step = step; |
| 161 | _output = output; |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 162 | |
Michalis Spyrou | cd5b4a3 | 2020-02-27 15:35:53 +0000 | [diff] [blame] | 163 | INEKernel::configure(win); |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | Status NERangeKernel::validate(const ITensorInfo *output, float start, float end, float step) |
| 167 | { |
| 168 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); |
| 169 | |
| 170 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*output, start, end, step)); |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 171 | |
| 172 | return Status{}; |
| 173 | } |
| 174 | |
| 175 | void NERangeKernel::run(const Window &window, const ThreadInfo &info) |
| 176 | { |
| 177 | ARM_COMPUTE_UNUSED(info); |
| 178 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 179 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
Yair Schwarzbaum | 9e8a770 | 2021-11-08 10:58:06 +0200 | [diff] [blame] | 180 | const auto *uk = get_implementation(RangeSelectorData{ _output->info()->data_type() }); |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 181 | |
Yair Schwarzbaum | 9e8a770 | 2021-11-08 10:58:06 +0200 | [diff] [blame] | 182 | uk->ukernel(_output, _start, _step, window); |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 183 | } |
Sheri Zhang | ac6499a | 2021-02-10 15:32:38 +0000 | [diff] [blame] | 184 | } // namespace arm_compute |