58 using ElementsProcessed =
Steps;
80 "Bias addition only supported with broadcast mode in case the input or output has to be reinterpreted as 3D");
84 const unsigned int m = gemm_info.
m;
85 const unsigned int n = gemm_info.
n;
86 const unsigned int k = gemm_info.
k;
89 tensor_shape0.
set(0, k);
90 tensor_shape0.
set(1, m);
93 tensor_shape1.
set(0, n);
94 tensor_shape1.
set(1, k);
98 const unsigned int input2_dim0 = input2->
dimension(0);
99 const unsigned int input2_dim1 = input2->
dimension(1);
112 const TensorInfo tensor_info0 = input0->
clone()->set_tensor_shape(tensor_shape0);
113 const TensorInfo tensor_info1 = input1->
clone()->set_tensor_shape(tensor_shape1);
133 const GEMMKernelInfo &gemm_info, ElementsProcessed &num_elements_processed)
135 unsigned int &num_elems_processed_per_iteration_x = num_elements_processed[0];
136 unsigned int &num_elems_processed_per_iteration_y = num_elements_processed[1];
141 bool window_changed =
false;
148 if(reinterpret_output_as_3d)
154 tmp_info.set_tensor_shape(tmp_shape);
158 num_elems_processed_per_iteration_x = rhs_info.
n0;
159 num_elems_processed_per_iteration_y = lhs_info.
m0;
162 win_out =
calculate_max_window(*output,
Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
164 if(input2 !=
nullptr)
166 const int bias_processed_per_iteration_x = num_elems_processed_per_iteration_x;
168 const int bias_processed_per_iteration_y = gemm_info.
broadcast_bias ? 1 : num_elems_processed_per_iteration_y;
180 const unsigned int dimension_to_collapse = std::min(static_cast<unsigned int>(output->
num_dimensions()), 2u);
181 collapsed = win.
collapse(win, dimension_to_collapse);
184 return std::make_pair(err, collapsed);
189 : _input0(nullptr), _input1(nullptr), _input2(nullptr), _output(nullptr), _slide_matrix_b(true), _reinterpret_output_as_3d(false), _use_dummy_work_items(false), _add_bias(false),
190 _broadcast_bias(false), _export_to_cl_image(false), _k(1)
217 _add_bias = _input2 !=
nullptr;
219 _export_to_cl_image = rhs_info.export_to_cl_image;
223 const unsigned int num_dimensions_input0 = _input0->info()->num_dimensions();
226 ElementsProcessed num_elements_processed{};
229 auto win_config = validate_and_configure_window(input0->
info(), input1->
info(), input2 !=
nullptr ? input2->
info() :
nullptr, output->
info(), lhs_info, rhs_info, gemm_info, num_elements_processed);
231 ICLKernel::configure_internal(win_config.second);
233 const bool enable_mixed_precision = gemm_info.fp_mixed_precision;
237 const unsigned int internal_m = _reinterpret_output_as_3d ? gemm_info.m : output->
info()->
dimension(1);
239 const unsigned int partial_store_m0 = internal_m % lhs_info.m0;
240 const unsigned int partial_store_n0 = gemm_info.n % rhs_info.n0;
247 build_opts.
add_option_if(_reinterpret_output_as_3d,
"-DREINTERPRET_OUTPUT_AS_3D");
250 build_opts.
add_option_if(gemm_info.broadcast_bias,
"-DBROADCAST_BIAS");
252 build_opts.
add_option_if(lhs_info.interleave,
"-DLHS_INTERLEAVE");
253 build_opts.
add_option_if(rhs_info.interleave,
"-DRHS_INTERLEAVE");
254 build_opts.
add_option_if(lhs_info.transpose,
"-DLHS_TRANSPOSE");
255 build_opts.
add_option_if(_use_dummy_work_items,
"-DDUMMY_WORK_ITEMS");
259 build_opts.
add_option_if(enable_mixed_precision,
"-DMIXED_PRECISION");
260 build_opts.
add_option_if(rhs_info.export_to_cl_image,
"-DOPENCL_IMAGE_SUPPORT");
276 kernel_name += lhs_info.transpose ?
"lhs_t_" :
"lhs_nt_";
277 kernel_name += rhs_info.transpose ?
"rhs_t" :
"rhs_nt";
278 kernel_name += rhs_info.export_to_cl_image ?
"_texture" :
"";
286 _config_id += (_add_bias ?
"add_bias_" :
"");
287 _config_id += (_broadcast_bias ?
"broadcast_bias_" :
"");
288 _config_id += (_reinterpret_output_as_3d ?
"3do_" :
"");
289 _config_id += (gemm_info.activation_info.enabled() ?
"fused_activation_" :
"");
292 _config_id += (enable_mixed_precision ?
"mixed_precision_" :
"");
322 ElementsProcessed num_elements_processed{};
325 input1->
clone().get(),
326 input2 !=
nullptr ? input2->
clone().get() :
nullptr,
327 output->
clone().get(),
331 num_elements_processed)
356 cl::Image2D input1_image2d;
358 if(_export_to_cl_image)
373 slice_b = slice_matrix_b;
376 unsigned int idx = 0;
382 if(_export_to_cl_image)
384 _kernel.setArg(idx++, input1_image2d);
398 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_k));
401 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input0->
info()->
strides_in_bytes()[2]));
404 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input1->
info()->
strides_in_bytes()[2]));
409 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input2->
info()->
strides_in_bytes()[2]));
413 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_output->
info()->
strides_in_bytes()[2]));
416 if(_reinterpret_output_as_3d)
418 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(total_cross_plane_pad));
unsigned int top
top of the border
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
bool is_one(float a, float epsilon=0.00001f)
Checks if the input floating point number is 1.0f checking if the difference is within a range define...
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
bool broadcast_bias
Flag used to broadcast the bias addition.
#define ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(tensor)
const Window & window() const
The maximum window the kernel can be executed on.
static Status validate(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float alpha, float beta, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, const GEMMKernelInfo &gemm_info)
Static function to check if given info will lead to a valid configuration of CLGEMMMatrixMultiplyResh...
bool fp_mixed_precision
Flag used to indicate wider accumulators (32 bit instead of 16 for FP16).
void run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
Descriptor used by the GEMM kernels.
void add_2D_tensor_argument_if(bool cond, unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx ...
std::unordered_map< const ITensorInfo *, PaddingSize > get_padding_info(std::initializer_list< const ITensorInfo * > infos)
Stores padding information before configuring a kernel.
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
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.
const StringSet & options() const
Gets the current options list set.
void configure(const ICLTensor *input0, const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, float alpha, float beta, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, const GEMMKernelInfo &gemm_info)
Initialise the kernel's input and output.
unsigned int depth_output_gemm3d
Depth of the output tensor in case is reinterpreted as 3D.
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
bool preferred_dummy_work_items_support(const cl::Device &device)
Helper function to check if "dummy work-items" are preferred to have a power of two NDRange In case d...
#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.
TensorShape compute_mm_shape(const ITensorInfo &input0, const ITensorInfo &input1, bool is_interleaved_transposed, const GEMMReshapeInfo &reshape_info)
Calculate the matrix multiplication output shape of two tensors.
Status validate_image2d_support_on_rhs(const ITensorInfo &tensor_reshaped_info, const GEMMRHSMatrixInfo &rhs_info)
Utility function to validate the image2d OpenCL object support on the RHS reshaped matrix.
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.
const std::string & string_from_activation_func(ActivationLayerInfo::ActivationFunction act)
Translates a given activation function to a string.
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
GEMM LHS (Left Hand Side) matrix information.
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 bottom
bottom of the border
std::string lower_string(const std::string &val)
Lower a given string.
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
bool transpose
True if the (k0xn0) block has to be transposed before been stored.
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 F16 per channel
Implementation of a static rectangular access pattern.
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
void add_option(std::string option)
Adds option to the existing build option list.
bool transpose
True if the (m0xk0) block has to be transposed before been stored.
Window collapse(const Window &full_window, size_t first, size_t last=Coordinates::num_max_dimensions) const
Collapse the dimensions between first and last.
unsigned int k0
Number of partial accumulations performed by the matrix multiplication.
unsigned int m
Number of LHS rows.
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.
unsigned int n
Number of RHS columns.
const std::string & string_from_data_type(DataType dt)
Convert a data type identity into a string.
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.
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
TensorShape compute_lhs_reshaped_shape(const ITensorInfo &a, const GEMMLHSMatrixInfo &lhs_info, bool reinterpret_input_as_3d=false)
Calculate the Left Hand Side matrix reshaped shape.
GEMM RHS (Right Hand Side) matrix information.
std::string float_to_string_with_full_precision(float val)
Create a string with the float in full precision.
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
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.
unsigned int n0
Number of columns processed by the matrix multiplication.
CLGEMMMatrixMultiplyReshapedKernel()
Default Constructor.
Class to describe a number of elements in each dimension.
std::string get_cl_type_from_data_type(const DataType &dt)
Translates a tensor data type to the appropriate OpenCL type.
TensorShape compute_rhs_reshaped_shape(const ITensorInfo &a, const GEMMRHSMatrixInfo &rhs_info)
Calculate the Right Hand Side matrix reshaped shape.
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...
bool reinterpret_input_as_3d
Flag used to reinterpret the input as 3D.
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;.
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.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
bool has_padding_changed(const std::unordered_map< const ITensorInfo *, PaddingSize > &padding_map)
Check if the previously stored padding info has changed after configuring a kernel.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
void add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx.
Interface for OpenCL tensor.
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
#define ARM_COMPUTE_CREATE_ERROR(error_code, msg)
Creates an error with a given message.
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
#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)
Wrapper to configure the Khronos OpenCL C++ header.
unsigned int k
Number of LHS columns or RHS rows.
bool is_zero(float a, float epsilon=0.00001f)
Checks if the input floating point number is 0.0f checking if the difference is within a range define...
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
virtual const cl::Buffer & cl_buffer() const =0
Interface to be implemented by the child class to return a reference to the OpenCL buffer containing ...
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Store the tensor's metadata.
unsigned int k0
Number of partial accumulations performed by the matrix multiplication.
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.
unsigned int m0
Number of rows processed by the matrix multiplication.
virtual const Strides & strides_in_bytes() const =0
The strides in bytes for accessing each dimension of the tensor.
Window first_slice_window_3D() const
First 3D slice of the window.
DataType
Available data types.
Describe a multidimensional execution window.
void collapse(size_t n, size_t first=0)
Collapse the first n dimensions.
TensorShape & set(size_t dimension, size_t value, bool apply_dim_correction=true, bool increase_dim_unit=true)
Accessor to set the value of one of the dimensions.
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)