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