45 Status
validate_arguments(
const ITensorInfo *
input,
const ITensorInfo *weights,
const ITensorInfo *biases,
const ITensorInfo *output,
const PadStrideInfo &
conv_info)
58 "Weights feature map dimension should match the respective input's one");
60 ARM_COMPUTE_RETURN_ERROR_ON_MSG((weights->dimension(width_idx) == 1) && std::get<0>(conv_info.stride()) > 3,
"Strides larger than 3 not supported for 1x1 convolution.");
62 && std::get<0>(conv_info.stride()) > 2,
63 "Strides larger than 2 not supported for 3x3, 5x5, 9x9 convolution.");
69 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(width_idx) != 1 && weights->dimension(width_idx) != 3 && weights->dimension(width_idx) != 5 && weights->dimension(width_idx) != 9,
70 "Kernel sizes other than 1x1, 3x3, 5x5 or 9x9 are not supported with quantized data types");
75 "Kernel sizes other than 1x1, 3x3 or 5x5 are not supported with float data types");
90 "Biases size and number of input feature maps should match");
92 "Biases should be one dimensional");
96 if(output->total_size() != 0)
103 const auto data_type = input->data_type();
106 const UniformQuantizationInfo iqinfo = input->quantization_info().uniform();
107 const UniformQuantizationInfo wqinfo = weights->quantization_info().uniform();
108 const UniformQuantizationInfo oqinfo = output->quantization_info().uniform();
110 float multiplier = iqinfo.scale * wqinfo.scale / oqinfo.scale;
111 int output_multiplier = 0;
112 int output_shift = 0;
125 && (kernel_size <= 5)
126 && (conv_stride_x == 1) && (conv_stride_y == 1)
131 inline void setup_num_elems_nchw(
unsigned int &num_elems_read_per_iteration_x,
unsigned int &num_elems_read_per_iteration_y,
132 unsigned int &num_elems_written_per_iteration_x,
unsigned int &num_elems_written_per_iteration_y,
133 unsigned int kernel_size,
const PadStrideInfo &conv_info,
const GPUTarget target, ITensorInfo *input)
135 const DataType data_type = input->data_type();
136 const DataLayout data_layout = input->data_layout();
137 unsigned int conv_stride_x = std::get<0>(conv_info.stride());
138 unsigned int conv_stride_y = std::get<1>(conv_info.stride());
140 const bool run_optimized_bifrost = can_run_optimized_kernel_for_bifrost_nchw(target, conv_stride_x, conv_stride_y, kernel_size, data_type, data_layout);
142 if(run_optimized_bifrost)
149 num_elems_read_per_iteration_x = 4;
150 num_elems_read_per_iteration_y = 4;
151 num_elems_written_per_iteration_x = 4;
152 num_elems_written_per_iteration_y = 4;
157 num_elems_read_per_iteration_x = 6;
158 num_elems_read_per_iteration_y = 5;
159 num_elems_written_per_iteration_x = 4;
160 num_elems_written_per_iteration_y = 3;
165 num_elems_read_per_iteration_x = 8;
166 num_elems_read_per_iteration_y = 6;
167 num_elems_written_per_iteration_x = 4;
168 num_elems_written_per_iteration_y = 2;
179 num_elems_read_per_iteration_y = kernel_size;
180 num_elems_written_per_iteration_x = 8;
181 num_elems_written_per_iteration_y = 1;
185 switch(conv_stride_x)
188 num_elems_read_per_iteration_x = 8;
191 num_elems_read_per_iteration_x = 16;
194 switch(input->element_size())
197 num_elems_read_per_iteration_x = 28;
200 num_elems_read_per_iteration_x = 24;
203 num_elems_read_per_iteration_x = 22;
214 switch(conv_stride_x)
217 num_elems_read_per_iteration_x = 10;
220 num_elems_read_per_iteration_x = 17;
227 switch(conv_stride_x)
230 num_elems_read_per_iteration_x = 12;
233 num_elems_read_per_iteration_x = 20;
240 switch(conv_stride_x)
243 num_elems_read_per_iteration_x = 16;
246 num_elems_read_per_iteration_x = 24;
258 std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *weights, ITensorInfo *output,
const PadStrideInfo &conv_info,
const GPUTarget target)
260 const DataLayout data_layout = input->data_layout();
269 input->quantization_info());
273 const unsigned int vec_size = std::min(static_cast<unsigned int>(output->tensor_shape()[0]), 4u);
277 output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
278 Status err = Status{};
279 return std::make_pair(err, win);
284 const unsigned int kernel_size = weights->dimension(width_idx);
286 unsigned int num_elems_read_per_iteration_x = 0;
287 unsigned int num_elems_read_per_iteration_y = 0;
288 unsigned int num_elems_written_per_iteration_x = 0;
289 unsigned int num_elems_written_per_iteration_y = 0;
293 unsigned int conv_stride_x = std::get<0>(conv_info.stride());
294 unsigned int conv_stride_y = std::get<1>(conv_info.stride());
296 setup_num_elems_nchw(num_elems_read_per_iteration_x, num_elems_read_per_iteration_y,
297 num_elems_written_per_iteration_x, num_elems_written_per_iteration_y,
298 kernel_size, conv_info, target, input);
301 bool window_changed =
false;
302 Window win =
calculate_max_window(*output, Steps(num_elems_written_per_iteration_x, num_elems_written_per_iteration_y));
304 AccessWindowRectangle input_access(input, -conv_pad_left, -conv_pad_top, num_elems_read_per_iteration_x, num_elems_read_per_iteration_y, conv_stride_x, conv_stride_y);
305 AccessWindowStatic weights_access(weights, 0, 0, kernel_size, kernel_size);
306 AccessWindowRectangle output_access(output, 0, 0, num_elems_written_per_iteration_x, num_elems_written_per_iteration_y);
308 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
310 return std::make_pair(err, win);
320 : _input(nullptr), _biases(nullptr), _weights(nullptr), _output(nullptr), _data_layout(
DataLayout::
UNKNOWN), _border_size(0), _conv_stride_x(0), _conv_stride_y(0), _conv_info()
342 (biases !=
nullptr) ? biases->
info() :
nullptr,
358 const unsigned int kernel_size = weights->
info()->
dimension(width_idx);
364 auto win_config = validate_and_configure_window(input->
info(), weights->
info(), output->
info(),
conv_info, gpu_target);
366 ICLKernel::configure_internal(win_config.second);
375 kernel_name <<
"direct_convolution_nhwc";
377 const unsigned int n0 = win_config.second.x().step();
378 const unsigned int m0 = win_config.second.y().step();
382 const unsigned int pad_left = conv_info.
pad_left();
383 const unsigned int pad_top = conv_info.
pad_top();
387 build_options.
add_option(std::string(
"-DHAS_BIAS"));
419 zero_value.
get(zero_value_s32);
422 int output_multiplier = 0;
423 int output_shift = 0;
446 kernel_name <<
"direct_convolution" << kernel_size <<
"x" << kernel_size;
450 const bool run_optimized_for_bifrost = can_run_optimized_kernel_for_bifrost_nchw(gpu_target,
_conv_stride_x, _conv_stride_y, kernel_size, data_type, _data_layout);
452 if(run_optimized_for_bifrost)
456 kernel_name <<
"_f32_bifrost";
473 int output_multiplier = 0;
474 int output_shift = 0;
483 kernel_name.str(
"direct_convolution_quantized");
491 _config_id = kernel_name.str();
538 unsigned int idx = 0;
577 unsigned int idx = 0;
bool is_data_type_quantized(DataType dt)
Check if a given data type is of quantized type.
const size_t conv_pad_top
Class describing the value of a pixel for any image format.
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
#define ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(tensor)
const Window & window() const
The maximum window the kernel can be executed on.
const ICLTensor * _weights
void enqueue(IGCKernel &kernel, const Window &window, const gles::NDRange &lws=gles::NDRange(1U, 1U, 1U))
Add the kernel to the command queue with the given window.
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
Container for 2D border size.
const StringSet & options() const
Gets the current options list set.
TensorShape compute_deep_convolution_shape(const ITensorInfo &input, const ITensorInfo &weights, PadStrideInfo conv_info)
Calculate the deep convolution shape output shape of a tensor.
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
void get(uint8_t &v) const
Interpret the pixel value as a U8.
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
std::string to_string(T &&value)
Convert integer and float values to string.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
1 channel, 1 F32 per channel
const DataLayout data_layout
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Store the tensor's metadata.
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Describe one of the image's dimensions with a start, end and step.
unsigned int pad_top() const
Get the top padding.
Status calculate_quantized_multiplier(float multiplier, int32_t *quant_multiplier, int32_t *shift, bool ignore_epsilon=false)
Calculate quantized representation of multiplier.
std::string lower_string(const std::string &val)
Lower a given string.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, const GPUTarget target)
Static function to check if given info will lead to a valid configuration of CLDirectConvolutionLayer...
void add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx...
void use_tensor_dimensions(const TensorShape &shape, size_t first_dimension=Window::DimX)
Use the tensor's dimensions to fill the window dimensions.
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 F16 per channel
1 channel, 1 S32 per channel
void add_option(std::string option)
Adds option to the existing build option list.
cl::Kernel create_kernel(const CLCompileContext &ctx, const std::string &kernel_name, const std::set< std::string > &build_opts=std::set< std::string >())
Creates an opencl kernel using a compile context.
const std::string & string_from_data_type(DataType dt)
Convert a data type identity into a string.
std::string get_data_size_from_data_type(const DataType &dt)
Get the size of a data type in number of bits.
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
static constexpr unsigned int num_arguments_per_3D_tensor()
Returns the number of arguments enqueued per 3D tensor object.
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
quantized, asymmetric fixed-point 8-bit number unsigned
std::set< std::string > build_options
void run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue...
std::pair< unsigned int, unsigned int > stride() const
Get the stride.
const size_t conv_stride_x
GPUTarget get_target() const
Get the targeted GPU architecture.
UniformQuantizationInfo uniform() const
Return per layer quantization info.
std::string get_cl_type_from_data_type(const DataType &dt)
Translates a tensor data type to the appropriate OpenCL type.
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 std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
void add_option_if(bool cond, std::string option)
Adds option if a given condition is true;.
Padding and stride information class.
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
virtual PaddingSize padding() const =0
Padding of tensor.
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Num samples, channels, height, width.
bool is_data_type_quantized_asymmetric(DataType dt)
Check if a given data type is of asymmetric quantized type.
const size_t conv_stride_y
BorderSize border_size() const override
The size of the border for that kernel.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
void set_dimension_step(size_t dimension, int step)
Set the step of a given dimension.
CLDirectConvolutionLayerKernel()
Default constructor.
Interface for OpenCL tensor.
const std::string & string_from_data_layout(DataLayout dl)
Convert a data layout identity into a string.
#define ARM_COMPUTE_CREATE_ERROR(error_code, msg)
Creates an error with a given message.
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
GPUTarget
Available GPU Targets.
const ICLTensor * _biases
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context...
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Num samples, height, width, channels.
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
unsigned int adjust_vec_size(unsigned int vec_size, size_t dim0)
Returns the adjusted vector size in case it is less than the input's first dimension, getting rounded down to its closest valid vector size.
quantized, asymmetric fixed-point 8-bit number signed
virtual const Strides & strides_in_bytes() const =0
The strides in bytes for accessing each dimension of the tensor.
size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get the index of the given dimension.
void adjust(size_t dimension, int adjust_value, bool is_at_start)
Adjust the start or end of a given dimension by the given value.
void add_1D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx...
Window first_slice_window_3D() const
First 3D slice of the window.
DataType
Available data types.
unsigned int pad_left() const
Get the left padding.
DataLayout
[DataLayout enum definition]
const size_t conv_pad_left
Describe a multidimensional execution window.
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
bool gpu_target_is_in(GPUTarget target_to_check, GPUTarget target, Args... targets)
Helper function to check whether a gpu target is equal to the provided targets.
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.
void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info)
Set the input, weights, biases and output tensors.