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