Compute Library
 22.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"
37 
38 namespace arm_compute
39 {
40 namespace
41 {
42 struct RangeSelectorData
43 {
45 };
46 
49 
50 struct RangeUKernel
51 {
52  const char *name;
53  const RangeSelectorPtr is_selected;
54  RangeUKernelPtr ukernel;
55 };
56 
57 static const RangeUKernel available_kernels[] =
58 {
59  {
60  "fp16_neon_range",
61  [](const RangeSelectorData & data) { return data.dt == DataType::F16; },
63  },
64  {
65  "f32_neon_range",
66  [](const RangeSelectorData & data) { return data.dt == DataType::F32; },
68  },
69  {
70  "u8_neon_range",
71  [](const RangeSelectorData & data) { return data.dt == DataType::U8; },
73  },
74  {
75  "u16_neon_range",
76  [](const RangeSelectorData & data) { return data.dt == DataType::U16; },
78  },
79  {
80  "u32_neon_range",
81  [](const RangeSelectorData & data) { return data.dt == DataType::U32; },
83  },
84  {
85  "s8_neon_range",
86  [](const RangeSelectorData & data) { return data.dt == DataType::S8; },
88  },
89  {
90  "s16_neon_range",
91  [](const RangeSelectorData & data) { return data.dt == DataType::S16; },
93  },
94  {
95  "s32_neon_range",
96  [](const RangeSelectorData & data) { return data.dt == DataType::S32; },
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;
117 }
118 
119 Status validate_arguments(const ITensorInfo &output, const float start, const float end, const float step)
120 {
121  const auto *uk = get_implementation(RangeSelectorData{ output.data_type() });
122  ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
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 
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");
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");
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");
136 
137  return Status{};
138 }
139 } // namespace
140 
142  : _start(0), _end(1), _step(1), _output(nullptr)
143 {
144 }
145 
146 void NERangeKernel::configure(ITensor *output, float start, float end, float step)
147 {
149 
150  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*(output->info()), start, end, step));
151 
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 
155  // Configure kernel window
156  Window win = calculate_max_window(*output->info(), Steps());
157 
158  _start = start;
159  _end = end;
160  _step = step;
161  _output = output;
162 
163  INEKernel::configure(win);
164 }
165 
166 Status NERangeKernel::validate(const ITensorInfo *output, float start, float end, float step)
167 {
169 
170  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*output, start, end, step));
171 
172  return Status{};
173 }
174 
176 {
177  ARM_COMPUTE_UNUSED(info);
180  const auto *uk = get_implementation(RangeSelectorData{ _output->info()->data_type() });
181 
182  uk->ukernel(_output, _start, _step, window);
183 }
184 } // namespace arm_compute
const char * name
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 REGISTER_FP16_NEON(func_name)
Definition: Registrars.h:48
1 channel, 1 U8 per channel
#define REGISTER_FP32_NEON(func_name)
Definition: Registrars.h:74
#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
const RangeSelectorPtr is_selected
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
void fp16_neon_range_function(ITensor *output, float start, float step, const Window &window)
Status class.
Definition: Error.h:52
Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *dst, const PadStrideInfo &conv_info)
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
decltype(strategy::transforms) typedef type
Interface for CPU tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2022 Arm Limited.
1 channel, 1 F16 per channel
#define REGISTER_INTEGER_NEON(func_name)
Definition: Registrars.h:171
#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.
#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:1161
void fp32_neon_range_function(ITensor *output, float start, float step, const Window &window)
Definition: fp32.cpp:34
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
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:1146
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
void s16_neon_range_function(ITensor *output, float start, float step, const Window &window)
Definition: integer.cpp:53
DataType dt
void s8_neon_range_function(ITensor *output, float start, float step, const Window &window)
Definition: integer.cpp:48
RangeUKernelPtr ukernel
void u8_neon_range_function(ITensor *output, float start, float step, const Window &window)
Definition: integer.cpp:33
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Information about executing thread and CPU.
Definition: CPPTypes.h:179
constexpr int step
Definition: fp32.cpp:35
#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
Includes all wrapper headers at once.
void u32_neon_range_function(ITensor *output, float start, float step, const Window &window)
Definition: integer.cpp:43
void u16_neon_range_function(ITensor *output, float start, float step, const Window &window)
Definition: integer.cpp:38
DataType
Available data types.
Definition: Types.h:79
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
void s32_neon_range_function(ITensor *output, float start, float step, const Window &window)
Definition: integer.cpp:58