64 "Weights feature map dimension should match the respective src's one");
69 "Strides larger than 2 not supported for 3x3, 5x5, 9x9 convolution.");
75 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,
76 "Kernel sizes other than 1x1, 3x3, 5x5 or 9x9 are not supported with quantized data types");
81 "Kernel sizes other than 1x1, 3x3 or 5x5 are not supported with float data types");
96 "Biases size and number of src feature maps should match");
98 "Biases should be one dimensional");
102 if(
dst->total_size() != 0)
112 const UniformQuantizationInfo iqinfo =
src->quantization_info().uniform();
113 const UniformQuantizationInfo wqinfo = weights->quantization_info().uniform();
114 const UniformQuantizationInfo oqinfo =
dst->quantization_info().uniform();
116 float multiplier = iqinfo.scale * wqinfo.scale / oqinfo.scale;
117 int output_multiplier = 0;
118 int output_shift = 0;
131 && (kernel_size <= 5)
137 inline void setup_num_elems_nchw(
unsigned int &num_elems_read_per_iteration_x,
unsigned int &num_elems_read_per_iteration_y,
138 unsigned int &num_elems_written_per_iteration_x,
unsigned int &num_elems_written_per_iteration_y,
148 if(run_optimized_bifrost)
155 num_elems_read_per_iteration_x = 4;
156 num_elems_read_per_iteration_y = 4;
157 num_elems_written_per_iteration_x = 4;
158 num_elems_written_per_iteration_y = 4;
163 num_elems_read_per_iteration_x = 6;
164 num_elems_read_per_iteration_y = 5;
165 num_elems_written_per_iteration_x = 4;
166 num_elems_written_per_iteration_y = 3;
171 num_elems_read_per_iteration_x = 8;
172 num_elems_read_per_iteration_y = 6;
173 num_elems_written_per_iteration_x = 4;
174 num_elems_written_per_iteration_y = 2;
185 num_elems_read_per_iteration_y = kernel_size;
186 num_elems_written_per_iteration_x = 8;
187 num_elems_written_per_iteration_y = 1;
194 num_elems_read_per_iteration_x = 8;
197 num_elems_read_per_iteration_x = 16;
200 switch(
src->element_size())
203 num_elems_read_per_iteration_x = 28;
206 num_elems_read_per_iteration_x = 24;
209 num_elems_read_per_iteration_x = 22;
223 num_elems_read_per_iteration_x = 10;
226 num_elems_read_per_iteration_x = 17;
236 num_elems_read_per_iteration_x = 12;
239 num_elems_read_per_iteration_x = 20;
249 num_elems_read_per_iteration_x = 16;
252 num_elems_read_per_iteration_x = 24;
264 std::pair<Status, Window> validate_and_configure_window(ITensorInfo *
src, ITensorInfo *weights, ITensorInfo *
dst,
const PadStrideInfo &
conv_info,
const GPUTarget target)
275 src->quantization_info());
279 const unsigned int vec_size = std::min(static_cast<unsigned int>(
dst->tensor_shape()[0]), 4u);
280 unsigned int num_rows = 1
U;
281 if(
dst->tensor_shape()[0] > 16)
288 return std::make_pair(Status{}, win);
293 const unsigned int kernel_size = weights->dimension(width_idx);
295 unsigned int num_elems_read_per_iteration_x = 0;
296 unsigned int num_elems_read_per_iteration_y = 0;
297 unsigned int num_elems_written_per_iteration_x = 0;
298 unsigned int num_elems_written_per_iteration_y = 0;
305 setup_num_elems_nchw(num_elems_read_per_iteration_x, num_elems_read_per_iteration_y,
306 num_elems_written_per_iteration_x, num_elems_written_per_iteration_y,
310 bool window_changed =
false;
311 Window win =
calculate_max_window(*
dst, Steps(num_elems_written_per_iteration_x, num_elems_written_per_iteration_y));
314 AccessWindowStatic weights_access(weights, 0, 0, kernel_size, kernel_size);
315 AccessWindowRectangle output_access(
dst, 0, 0, num_elems_written_per_iteration_x, num_elems_written_per_iteration_y);
317 output_access.set_valid_region(win, ValidRegion(Coordinates(),
dst->tensor_shape()));
319 return std::make_pair(err, win);
357 const size_t image_w = tensor->tensor_shape()[0] / 4;
358 const size_t image_h = tensor->tensor_shape()[1] * tensor->tensor_shape()[2] * tensor->tensor_shape()[3];
362 if(image_w > max_image_w || image_h > max_image_h)
385 (biases !=
nullptr) ? biases :
nullptr,
398 const unsigned int kernel_size = weights->
dimension(width_idx);
404 auto win_config = validate_and_configure_window(
src, weights,
dst,
conv_info, gpu_target);
406 ICLKernel::configure_internal(win_config.second);
417 const unsigned int n0 = win_config.second.x().step();
418 const unsigned int m0 = win_config.second.y().step();
420 const unsigned int partial_store_n0 =
dst->dimension(channel_idx) % n0;
421 const unsigned int pad_left =
conv_info.pad_left();
422 const unsigned int pad_top =
conv_info.pad_top();
423 const bool export_to_cl_image = export_to_cl_image_support(weights, gpu_target,
_data_layout);
426 if(export_to_cl_image)
431 if(biases !=
nullptr)
448 build_options.add_option_if_else(export_to_cl_image,
"-DWEI_TENSOR_TYPE=IMAGE",
"-DWEI_TENSOR_TYPE=BUFFER");
469 zero_value.
get(zero_value_s32);
472 int output_multiplier = 0;
473 int output_shift = 0;
497 kernel_name <<
"direct_convolution" << kernel_size <<
"x" << kernel_size;
499 build_options.add_option_if(biases !=
nullptr, std::string(
"-DHAS_BIAS"));
503 if(run_optimized_for_bifrost)
524 int output_multiplier = 0;
525 int output_shift = 0;
591 cl::Image2D weights_cl_image;
599 if(export_to_cl_image)
601 const size_t image_w = weights->info()->dimension(0) / 4;
602 const size_t image_h = weights->info()->dimension(1) * weights->info()->dimension(2) * weights->info()->dimension(3);
604 const size_t image_row_pitch = weights->info()->strides_in_bytes()[1];
610 unsigned int idx = 0;
613 if(export_to_cl_image)
615 _kernel.setArg(idx++, weights_cl_image);
618 if(biases !=
nullptr)
644 if(biases !=
nullptr)
651 _kernel.setArg(idx1++, static_cast<unsigned int>(weights->info()->strides_in_bytes()[3]));
655 unsigned int idx = 0;
bool is_data_type_quantized(DataType dt)
Check if a given data type is of quantized type.
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)
bool image2d_from_buffer_supported(const cl::Device &device)
Helper function to check whether the cl_khr_image2d_from_buffer extension is supported.
const Window & window() const
The maximum window the kernel can be executed on.
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
const size_t conv_pad_left
Container for 2D border size.
void enqueue(cl::CommandQueue &queue, ICLKernel &kernel, const Window &window, const cl::NDRange &lws_hint=CLKernelLibrary::get().default_ndrange(), bool use_dummy_work_items=false)
Add the kernel to the command queue with the given window.
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.
const size_t conv_stride_x
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.
static Status validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const PadStrideInfo &conv_info, const GPUTarget target)
Static function to check if given info will lead to a valid configuration of ClDirectConvolutionKerne...
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.
const size_t conv_stride_y
GPUTarget get_arch_from_target(GPUTarget target)
Helper function to get the GPU arch.
std::string lower_string(const std::string &val)
Lower a given string.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
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.
SimpleTensor< float > src
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 F16 per channel
1 channel, 1 S32 per channel
void run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
const ITensor * get_const_tensor(int id) const
Get constant tensor of a given id.
void update_padding_for_cl_image(ITensorInfo *tensor)
Update padding required to export the OpenCL buffer to OpenCL image2d.
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.
auto ceil_to_multiple(S value, T divisor) -> decltype(((value+divisor - 1)/divisor) *divisor)
Computes the smallest number larger or equal to value that is a multiple of divisor.
quantized, asymmetric fixed-point 8-bit number unsigned
std::set< std::string > build_options
std::pair< unsigned int, unsigned int > stride() const
Get the stride.
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.
Padding and stride information class.
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.
size_t get_cl_image_pitch_alignment(const cl::Device &device)
Helper function to get the cl_image pitch alignment in pixels.
void configure(const CLCompileContext &compile_context, ITensorInfo *src, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *dst, const PadStrideInfo &conv_info)
Set the src, weights, biases and dst tensors info.
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.
ITensor * get_tensor(int id)
Get tensor of a given id from the pac.
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.
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(...)
cl::Image2D create_image2d_from_buffer(const cl::Context &ctx, const cl::Buffer &buffer, const TensorShape &shape2d, DataType data_type, size_t image_row_pitch)
Create a cl::Image2D object from an OpenCL buffer.
const size_t conv_pad_top
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,...
quantized, asymmetric fixed-point 8-bit number signed
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.
BorderSize border_size() const override
The size of the border for that kernel.
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.
void add_4D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 4D tensor's parameters to the object's kernel's arguments starting from the index idx.
unsigned int pad_left() const
Get the left padding.
DataLayout
[DataLayout enum definition]
Describe a multidimensional execution window.
bool is_data_type_float(DataType dt)
Check if a given data type is of floating point type.
#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)
const cl::Device & get_device()
Gets the CL device for which the programs are created.