Compute Library
 21.02
NERangeKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2021 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  */
25 
26 #include "arm_compute/core/Error.h"
32 #include "src/core/NEON/NEAsymm.h"
36 
37 #include "arm_compute/core/Utils.h"
38 
39 namespace arm_compute
40 {
41 namespace
42 {
43 template <typename T>
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 
88 Status validate_arguments(const ITensorInfo &output, const float start, const float end, const float step)
89 {
91  1,
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 
115  : _func(nullptr), _start(0), _end(1), _step(1), _output(nullptr)
116 {
117 }
118 
119 void NERangeKernel::configure(ITensor *output, float start, float end, float step)
120 {
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 
174 Status NERangeKernel::validate(const ITensorInfo *output, float start, float end, float step)
175 {
177 
178  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*output, start, end, step));
179 
180  return Status{};
181 }
182 
183 void NERangeKernel::run(const Window &window, const ThreadInfo &info)
184 {
185  ARM_COMPUTE_UNUSED(info);
188  ARM_COMPUTE_ERROR_ON(_func == nullptr);
189 
190  (*_func)(_output, _start, _step, window);
191 }
192 } // namespace arm_compute
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
Shape of a tensor.
Definition: TensorShape.h:39
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
1 channel, 1 U8 per channel
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
virtual DataType data_type() const =0
Data type used for each element of the tensor.
void configure(ITensor *output, float start, float end, float step)
Initialize the kernel&#39;s output tensor, start, end and step of the sequence.
1 channel, 1 F32 per channel
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
1 channel, 1 U16 per channel
Status class.
Definition: Error.h:52
Interface for Neon tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2021 Arm Limited.
virtual void set_valid_region(const ValidRegion &valid_region)=0
Set the valid region of the tensor.
1 channel, 1 F16 per channel
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
1 channel, 1 S32 per channel
static Status validate(const ITensorInfo *output, float start, float end, float step)
Static function to check if given info will lead to a valid configuration of NERangeKernel.
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
1 channel, 1 U32 per channel
bool check_value_range(T val, DataType dt, QuantizationInfo qinfo=QuantizationInfo())
Returns true if the value can be represented by the given data type.
Definition: Utils.h:1299
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
Coordinates of an item.
Definition: Coordinates.h:37
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
void end(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:290
uint8x8_t vsetlane(const uint8_t value, const uint8x8_t vector, const unsigned int lane)
Definition: setlane.h:91
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
size_t num_of_elements_in_range(const float start, const float end, const float step)
Returns the number of elements required to go from start to end with the wanted step.
Definition: Utils.h:1284
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
1 channel, 1 S16 per channel
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Information about executing thread and CPU.
Definition: CPPTypes.h:235
constexpr int step
Definition: fp32.cpp:35
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:792
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
void vstore(uint8_t *ptr, uint8x8_t val)
Definition: store.h:39
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
Definition: Error.h:244
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
uint8x8_t vdup_n(uint8_t value, traits::vector_64_tag)
Definition: dup_n.h:41
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:77
void set_num_dimensions(size_t num_dimensions)
Set number of dimensions.
Definition: Dimensions.h:149
Includes all wrapper headers at once.
uint8x8_t vmla(const uint8x8_t &a, const uint8x8_t &b, const uint8x8_t &c)
Definition: mla.h:46
Container for valid region of a window.
Definition: Types.h:188
signed 8-bit number
Describe a multidimensional execution window.
Definition: Window.h:39
NERangeKernel()
Default constructor.
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205