58 cpu::AsmGemmInfo init_assembly_metadata(
const GEMMInfo &
info)
60 cpu::AsmGemmInfo asm_info;
62 asm_info.reinterpret_input_as_3d = info.reinterpret_input_as_3d();
63 asm_info.depth_output_gemm3d = info.depth_output_gemm3d();
64 asm_info.activation_info = info.activation_info();
65 asm_info.output_stage = info.gemmlowp_output_stage();
66 asm_info.fast_mode = info.fast_math();
72 CpuGemmLowpMatrixMultiplyCore::CpuGemmLowpMatrixMultiplyCore()
75 _mtx_a_reshape_kernel(),
76 _mtx_b_reshape_kernel(),
77 _mtx_a_reduction_kernel(),
78 _mtx_b_reduction_kernel(),
79 _offset_contribution_kernel(),
80 _offset_contribution_output_stage_kernel(),
82 _convert_to_signed_asymm(),
83 _convert_from_signed_asymm(),
93 _run_vector_matrix_multiplication(false),
94 _assembly_path(false),
95 _fused_assembly_path(false),
96 _reshape_b_only_on_first_run(false),
98 _fuse_output_stage(false),
99 _run_activation(false),
100 _flip_signedness(false),
119 _run_vector_matrix_multiplication = a->
dimension(1) < 2;
121 _is_prepared =
false;
122 _fused_assembly_path =
false;
124 _gemm_info = gemm_info;
126 _asm_glue = std::make_unique<cpu::CpuGemmAssemblyDispatch>();
133 const int32_t offset_correction = 128;
138 _convert_to_signed_asymm = std::make_unique<kernels::CpuConvertQuantizedSignednessKernel>();
139 _convert_to_signed_asymm->configure(a_to_use, &_signed_a);
140 a_to_use = &_signed_a;
154 matrix_a = &_signed_a;
160 _fuse_output_stage =
true;
176 auto c_info_to_use = c ==
nullptr ? nullptr : c;
177 _asm_glue->configure(a_to_use, b, c_info_to_use, dst, asm_info);
178 _fused_assembly_path = _asm_glue->is_configured();
182 auto output_to_use = (_fuse_output_stage ? &_mm_result_s32 :
dst);
183 _asm_glue->configure(a_to_use, b,
nullptr, output_to_use, asm_info);
185 _assembly_path = _asm_glue->is_configured();
195 if(!(_assembly_path || _run_vector_matrix_multiplication))
206 _mtx_a_reshape_kernel = std::make_unique<kernels::CpuGemmInterleave4x4Kernel>();
207 _mtx_a_reshape_kernel->configure(a_to_use, &_tmp_a);
210 _mtx_b_reshape_kernel = std::make_unique<kernels::CpuGemmTranspose1xWKernel>();
211 _mtx_b_reshape_kernel->configure(b, &_tmp_b);
214 if(!_fused_assembly_path)
225 _mtx_b_reduction_kernel = std::make_unique<kernels::CpuGemmLowpMatrixBReductionKernel>();
226 _mtx_b_reduction_kernel->configure(b, &_vector_sum_col, reduction_info);
235 _mtx_a_reduction_kernel = std::make_unique<kernels::CpuGemmLowpMatrixAReductionKernel>();
236 _mtx_a_reduction_kernel->configure(a_to_use, &_vector_sum_row, reduction_info);
239 if(_fuse_output_stage)
244 _mm_kernel = std::make_unique<kernels::CpuGemmLowpMatrixMultiplyKernel>();
245 _mm_kernel->configure(matrix_a, matrix_b, &_mm_result_s32);
248 _offset_contribution_output_stage_kernel = std::make_unique<kernels::CpuGemmLowpOffsetContributionOutputStageKernel>();
249 _offset_contribution_output_stage_kernel->configure(&_mm_result_s32,
250 _a_offset == 0 ?
nullptr : &_vector_sum_col,
251 _b_offset == 0 ?
nullptr : &_vector_sum_row, c,
252 _flip_signedness ? &_signed_output : dst,
258 _convert_from_signed_asymm = std::make_unique<kernels::CpuConvertQuantizedSignednessKernel>();
259 _convert_from_signed_asymm->configure(&_signed_output, dst);
267 _mm_kernel = std::make_unique<kernels::CpuGemmLowpMatrixMultiplyKernel>();
268 _mm_kernel->configure(matrix_a, matrix_b, dst);
271 _offset_contribution_kernel = std::make_unique<kernels::CpuGemmLowpOffsetContributionKernel>();
272 _offset_contribution_kernel->configure(dst, _a_offset == 0 ?
nullptr : &_vector_sum_col, _b_offset == 0 ?
nullptr : &_vector_sum_row, a_to_use->
dimension(0),
273 _a_offset, _b_offset);
281 _activation_func = std::make_unique<CpuActivation>();
282 _activation_func->configure(dst,
nullptr, activation);
287 auto asm_mem_req = _asm_glue->workspace();
288 _aux_mem[AsmGemmWorkspace] = asm_mem_req[AsmGemmWorkspace];
289 _aux_mem[Pretranspose] = asm_mem_req[Pretranspose];
294 && _reshape_b_only_on_first_run ?
295 MemoryLifetime::Persistent :
296 MemoryLifetime::Temporary,
313 "The product AB is defined only if the number of columns in A is equal to the number of rows in B");
331 if(fuse_output_stage)
342 const int32_t offset_correction = 128;
348 a_to_use = &signed_a;
356 output_stage_corr.
gemmlowp_offset = signed_output.quantization_info().uniform().offset;
359 info.set_gemmlowp_output_stage(output_stage_corr);
362 matrix_a_info = &signed_a;
366 const AsmGemmInfo asm_info = init_assembly_metadata(info);
369 bool run_optimised =
false;
370 bool run_optimised_requantized =
false;
374 run_optimised_requantized = run_optimised;
384 if(info.depth_output_gemm3d() != 0)
386 if(info.reinterpret_input_as_3d())
406 const bool run_vector_matrix_multiplication = a->
dimension(1) < 2;
407 if(!run_vector_matrix_multiplication)
409 matrix_a_info = &tmp_a_info;
410 matrix_b_info = &tmp_b_info;
431 if(!run_optimised_requantized)
456 if(fuse_output_stage)
468 a_offset == 0 ?
nullptr : &info_vector_sum_col,
469 b_offset == 0 ?
nullptr : &info_vector_sum_row,
471 flip_signedness ? &signed_output : output,
473 info.gemmlowp_output_stage()));
486 a_offset == 0 ?
nullptr : &info_vector_sum_col,
487 b_offset == 0 ?
nullptr : &info_vector_sum_row,
488 a_offset, b_offset));
531 a_to_use = signed_a.
get();
532 matrix_a = signed_a.
get();
536 if(_asm_glue->is_configured())
539 auto output_to_use = (_fuse_output_stage ? mm_result_s32.
get() :
dst);
553 _asm_glue->run(asm_glue_tensors);
557 if(!_run_vector_matrix_multiplication)
559 matrix_a = tmp_a.
get();
560 matrix_b = tmp_b.
get();
569 if(!_reshape_b_only_on_first_run)
585 if(_fuse_output_stage)
596 if(!_fused_assembly_path)
610 if(_a_offset != 0 && !_reshape_b_only_on_first_run)
620 if(_fuse_output_stage)
645 if(!_fused_assembly_path && _fuse_output_stage && _flip_signedness)
663 _activation_func->run(pack);
673 if(_asm_glue->is_configured())
675 _asm_glue->prepare(tensors);
678 else if(_reshape_b_only_on_first_run && !_run_vector_matrix_multiplication && !_asm_glue->is_configured())
692 if(!_fused_assembly_path && _a_offset != 0 && _reshape_b_only_on_first_run)
static Status validate(const ITensorInfo *src, const ITensorInfo *dst, const GEMMLowpReductionKernelInfo &info)
Static function to check if given info will lead to a valid configuration.
Quantize using a fixed point multiplication.
std::unique_ptr< ITensorInfo > clone() const override
Provide a clone of the current object of class T.
static Status validate(const ITensorInfo *src, const ITensorInfo *dst)
Static function to check if given info will lead to a valid configuration.
bool enabled() const
Check if initialised.
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
void add_const_tensor(int id, const ITensor *tensor)
Add const tensor to the pack.
void prepare(ITensorPack &tensors) override
Prepare the function for executing.
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
1 channel, 1 U8 per channel
static bool is_activation_supported(const ActivationLayerInfo &activation)
Checks if activation is supported by the gemm assembly dispatcher.
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
size_t dimension(size_t index) const override
Return the size of the requested dimension.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
virtual void schedule_op(ICPPKernel *kernel, const Hints &hints, const Window &window, ITensorPack &tensors)=0
Runs the kernel in the same thread as the caller synchronously.
GEMMLowpOutputStageInfo gemmlowp_output_stage() const
GEMMLowp output stage.
TensorShape compute_reductionA_shape(const ITensorInfo &b)
Calculate the reductionA shape used in GEMMLowp.
QuantizationInfo quantization_info() const override
Get the quantization settings (scale and offset) of the tensor.
static Status validate(const ITensorInfo *src, const ITensorInfo *dst)
Static function to check if given info will lead to a valid configuration of CpuGemmTranspose1xWKerne...
Store the tensor's metadata.
#define ARM_COMPUTE_ERROR_THROW_ON(status)
int32_t gemmlowp_offset
GEMMLowp output stage offset used for quantizing to QASYMM8.
int32_t gemmlowp_max_bound
GEMMLowp max value used to saturate down the output result before converting back to QASYMM8...
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Activation Layer Information class.
GEMMLowpOutputStageType type
GEMMLowp output stage type.
Interface for CPU tensor.
TensorShape compute_interleaved_shape(const ITensorInfo &a, int mult_interleave4x4_height=1, bool reinterpret_input_as_3d=false)
Calculate the interleaved shape of an input tensor.
Copyright (c) 2017-2021 Arm Limited.
bool is_b_reshaped() const
Flag which specifies if the matrix B has been reshaped.
std::vector< MemoryInfo > MemoryRequirements
1 channel, 1 S32 per channel
const ITensor * get_const_tensor(int id) const
Get constant tensor of a given id.
Quantization information.
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *d, const AsmGemmInfo &info)
Indicates whether or not this function can be used to process the given parameters.
static Status validate(const ITensorInfo *src, const ITensorInfo *dst, const GEMMLowpReductionKernelInfo &info)
Static function to check if given info will lead to a valid configuration.
bool is_data_type_quantized_per_channel(DataType dt)
Check if a given data type is of per channel type.
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
quantized, asymmetric fixed-point 8-bit number unsigned
size_t total_size() const override
Returns the total size of the tensor in bytes.
void configure(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, ITensorInfo *dst, const GEMMInfo &gemm_info=GEMMInfo())
Initialise the kernel's inputs, output.
UniformQuantizationInfo uniform() const
Return per layer quantization info.
~CpuGemmLowpMatrixMultiplyCore()
Destructor.
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.
GEMMLowp output stage info.
void run(ITensorPack &tensors) override
Run the kernels contained in the function.
static Status validate(const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row, int32_t a_offset, int32_t b_offset)
Static function to check if given info will lead to a valid configuration.
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
quantized, symmetric fixed-point 8-bit number
bool is_data_type_quantized_asymmetric(DataType dt)
Check if a given data type is of asymmetric quantized type.
bool is_a_reshaped() const
Flag which specifies if the matrix A has been reshaped.
static Status validate(const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row, const ITensorInfo *bias, const ITensorInfo *dst, int32_t a_offset, int32_t b_offset, GEMMLowpOutputStageInfo output_stage)
Static function to check if given info will lead to a valid configuration.
quantized, symmetric per channel fixed-point 8-bit number
static Status validate(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *dst)
Static function to check if given info will lead to a valid configuration.
TensorShape compute_reductionB_shape(const ITensorInfo &a)
Calculate the reductionB shape used in GEMMLowp.
static Status validate(const ITensorInfo *src, const ITensorInfo *dst)
Static function to check if given info will lead to a valid configuration of CpuGemmInterleave4x4Kern...
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
ITensor * get_tensor(int id)
Get tensor of a given id from the pac.
void set_gemmlowp_output_stage(GEMMLowpOutputStageInfo &output_stage)
Sets GEMMLowp output stage.
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info)
Static function to check if given info will lead to a valid configuration.
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
TensorShape compute_transpose1xW_shape(const ITensorInfo &b)
Calculate the transposed 1xW shape.
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Store the tensor's metadata.
bool reshape_b_only_on_first_run() const
Flag which specifies if the reshape of matrix B should executed only for the first.
int offset_int_vec(int offset)
experimental::MemoryRequirements workspace() const override
Return the memory requirements required by the workspace.
quantized, asymmetric fixed-point 8-bit number signed
int32_t gemmlowp_min_bound
GEMMLowp min value used to saturate down the output result before converting back to QASYMM8...
DataType
Available data types.
ActivationLayerInfo activation_info() const
Activation layer to apply after the matrix multiplication.
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.
void add_tensor(int id, ITensor *tensor)
Add tensor to the pack.
static IScheduler & get()
Access the scheduler singleton.
static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *dst, const GEMMInfo &gemm_info=GEMMInfo())
Static function to check if given info will lead to a valid configuration.