Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 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 | */ |
| 24 | #include "arm_compute/core/NEON/kernels/NERangeKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/IAccessWindow.h" |
| 29 | #include "arm_compute/core/ITensor.h" |
| 30 | #include "arm_compute/core/NEON/NEAsymm.h" |
| 31 | #include "arm_compute/core/NEON/wrapper/wrapper.h" |
| 32 | #include "arm_compute/core/TensorInfo.h" |
| 33 | #include "arm_compute/core/Validate.h" |
| 34 | |
Michalis Spyrou | f63885b | 2019-01-16 14:18:09 +0000 | [diff] [blame] | 35 | #include "arm_compute/core/Utils.h" |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace |
| 40 | { |
| 41 | template <typename T> |
| 42 | void range_function(ITensor *output, float start, float step, const Window &window) |
| 43 | { |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 44 | /** NEON vector tag type. */ |
| 45 | using ExactTagType = typename wrapper::traits::neon_bitvector<T, wrapper::traits::BitWidth::W128>::tag_type; |
| 46 | |
| 47 | const auto step_vec = wrapper::vdup_n(static_cast<T>(step), ExactTagType{}); |
| 48 | const auto start_vec = wrapper::vdup_n(static_cast<T>(start), ExactTagType{}); |
| 49 | auto id_vec = wrapper::vdup_n(static_cast<T>(0.f), ExactTagType{}); |
| 50 | |
Michalis Spyrou | cd5b4a3 | 2020-02-27 15:35:53 +0000 | [diff] [blame] | 51 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 52 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 53 | const int window_step_x = 16 / sizeof(T); |
| 54 | |
| 55 | Window win{ window }; |
| 56 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 57 | Iterator output_it(output, win); |
| 58 | |
| 59 | execute_window_loop(win, [&](const Coordinates &) |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 60 | { |
Michalis Spyrou | cd5b4a3 | 2020-02-27 15:35:53 +0000 | [diff] [blame] | 61 | int x = window_start_x; |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 62 | const auto out_ptr = reinterpret_cast<T *>(output_it.ptr()); |
Michalis Spyrou | cd5b4a3 | 2020-02-27 15:35:53 +0000 | [diff] [blame] | 63 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 64 | { |
Michalis Spyrou | 10b49c7 | 2020-04-02 13:12:32 +0100 | [diff] [blame] | 65 | for(int count = 0; count < window_step_x; ++count) |
Michalis Spyrou | cd5b4a3 | 2020-02-27 15:35:53 +0000 | [diff] [blame] | 66 | { |
| 67 | id_vec = wrapper::vsetlane(static_cast<T>(x + count), id_vec, count); |
| 68 | } |
| 69 | |
| 70 | // start + step * id |
| 71 | const auto res_vec = wrapper::vmla(start_vec, id_vec, step_vec); |
| 72 | wrapper::vstore(out_ptr + x, res_vec); |
| 73 | } |
| 74 | |
| 75 | // Compute left-over elements |
| 76 | for(; x < window_end_x; ++x) |
| 77 | { |
| 78 | const auto res = start + x * step; |
| 79 | *(out_ptr + x) = res; |
| 80 | } |
| 81 | |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 82 | }, |
| 83 | output_it); |
| 84 | } |
| 85 | |
| 86 | Status validate_arguments(const ITensorInfo &output, const float start, const float end, const float step) |
| 87 | { |
| 88 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&output, |
| 89 | 1, |
| 90 | DataType::U8, DataType::S8, |
| 91 | DataType::U16, DataType::S16, |
| 92 | DataType::U32, DataType::S32, |
| 93 | DataType::F16, DataType::F32); |
| 94 | |
| 95 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((start == end), "start of the requested sequence must not be equal to the end"); |
| 96 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(((start < end) && (step <= 0)), "step must be greater than 0 when start < end"); |
| 97 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(((start > end) && (step >= 0)), "step must be less than 0 when start > end"); |
| 98 | |
Michalis Spyrou | f63885b | 2019-01-16 14:18:09 +0000 | [diff] [blame] | 99 | 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"); |
| 100 | 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"); |
| 101 | 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] | 102 | |
| 103 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((start == end), "start of the requested sequence must not be equal to the end"); |
| 104 | |
| 105 | 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] | 106 | 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] | 107 | |
| 108 | return Status{}; |
| 109 | } |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 110 | } // namespace |
| 111 | |
| 112 | NERangeKernel::NERangeKernel() |
| 113 | : _func(nullptr), _start(0), _end(1), _step(1), _output(nullptr) |
| 114 | { |
| 115 | } |
| 116 | |
| 117 | void NERangeKernel::configure(ITensor *output, float start, float end, float step) |
| 118 | { |
| 119 | ARM_COMPUTE_ERROR_ON_NULLPTR(output); |
| 120 | |
| 121 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*(output->info()), start, end, step)); |
| 122 | |
Michalis Spyrou | cd5b4a3 | 2020-02-27 15:35:53 +0000 | [diff] [blame] | 123 | // Auto initialize output if not initialized |
| 124 | auto_init_if_empty(*output->info(), TensorShape(num_of_elements_in_range(start, end, step)), 1, output->info()->data_type(), output->info()->quantization_info()); |
| 125 | |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 126 | // Configure kernel window |
Michalis Spyrou | cd5b4a3 | 2020-02-27 15:35:53 +0000 | [diff] [blame] | 127 | Window win = calculate_max_window(*output->info(), Steps()); |
| 128 | Coordinates coord; |
| 129 | coord.set_num_dimensions(output->info()->num_dimensions()); |
| 130 | output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape())); |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 131 | |
| 132 | _start = start; |
| 133 | _end = end; |
| 134 | _step = step; |
| 135 | _output = output; |
| 136 | switch(_output->info()->data_type()) |
| 137 | { |
| 138 | case DataType::U8: |
| 139 | _func = &range_function<uint8_t>; |
| 140 | break; |
| 141 | case DataType::U16: |
| 142 | _func = &range_function<uint16_t>; |
| 143 | break; |
| 144 | case DataType::U32: |
| 145 | _func = &range_function<uint32_t>; |
| 146 | break; |
| 147 | case DataType::S8: |
| 148 | _func = &range_function<int8_t>; |
| 149 | break; |
| 150 | case DataType::S16: |
| 151 | _func = &range_function<int16_t>; |
| 152 | break; |
| 153 | case DataType::S32: |
| 154 | _func = &range_function<int32_t>; |
| 155 | break; |
| 156 | case DataType::F32: |
| 157 | _func = &range_function<float>; |
| 158 | break; |
| 159 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 160 | case DataType::F16: |
| 161 | _func = &range_function<float16_t>; |
| 162 | break; |
| 163 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 164 | default: |
| 165 | ARM_COMPUTE_ERROR("Unsupported data type."); |
| 166 | break; |
| 167 | } |
| 168 | |
Michalis Spyrou | cd5b4a3 | 2020-02-27 15:35:53 +0000 | [diff] [blame] | 169 | INEKernel::configure(win); |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | Status NERangeKernel::validate(const ITensorInfo *output, float start, float end, float step) |
| 173 | { |
| 174 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); |
| 175 | |
| 176 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*output, start, end, step)); |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 177 | |
| 178 | return Status{}; |
| 179 | } |
| 180 | |
| 181 | void NERangeKernel::run(const Window &window, const ThreadInfo &info) |
| 182 | { |
| 183 | ARM_COMPUTE_UNUSED(info); |
| 184 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 185 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 186 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 187 | |
| 188 | (*_func)(_output, _start, _step, window); |
| 189 | } |
| 190 | } // namespace arm_compute |