Compute Library
 19.08
NEPixelWiseMultiplicationKernel Class Reference

Interface for the kernel to perform addition between two tensors. More...

#include <NEPixelWiseMultiplicationKernel.h>

Collaboration diagram for NEPixelWiseMultiplicationKernel:
[legend]

Public Member Functions

const char * name () const override
 Name of the kernel. More...
 
 NEPixelWiseMultiplicationKernel ()
 Default constructor. More...
 
 NEPixelWiseMultiplicationKernel (const NEPixelWiseMultiplicationKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
NEPixelWiseMultiplicationKerneloperator= (const NEPixelWiseMultiplicationKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 NEPixelWiseMultiplicationKernel (NEPixelWiseMultiplicationKernel &&)=default
 Allow instances of this class to be moved. More...
 
NEPixelWiseMultiplicationKerneloperator= (NEPixelWiseMultiplicationKernel &&)=default
 Allow instances of this class to be moved. More...
 
 ~NEPixelWiseMultiplicationKernel ()=default
 Default destructor. More...
 
void configure (const ITensor *input1, const ITensor *input2, ITensor *output, float scale, ConvertPolicy overflow_policy, RoundingPolicy rounding_policy)
 Initialise the kernel's input, output and border mode. More...
 
void run (const Window &window, const ThreadInfo &info) override
 Execute the kernel on the passed window. More...
 
BorderSize border_size () const override
 The size of the border for that kernel. More...
 
- Public Member Functions inherited from ICPPKernel
virtual ~ICPPKernel ()=default
 Default destructor. More...
 
- Public Member Functions inherited from IKernel
 IKernel ()
 Constructor. More...
 
virtual ~IKernel ()=default
 Destructor. More...
 
virtual bool is_parallelisable () const
 Indicates whether or not the kernel is parallelisable. More...
 
const Windowwindow () const
 The maximum window the kernel can be executed on. More...
 

Static Public Member Functions

static Status validate (const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale, ConvertPolicy overflow_policy, RoundingPolicy rounding_policy)
 Static function to check if given info will lead to a valid configuration of NEPixelWiseMultiplicationKernel. More...
 

Detailed Description

Interface for the kernel to perform addition between two tensors.

Definition at line 35 of file NEPixelWiseMultiplicationKernel.h.

Constructor & Destructor Documentation

◆ NEPixelWiseMultiplicationKernel() [1/3]

Default constructor.

Definition at line 555 of file NEPixelWiseMultiplicationKernel.cpp.

556  : _func_float(nullptr), _func_int(nullptr), _func_quantized(nullptr), _input1(nullptr), _input2(nullptr), _output(nullptr), _scale{ 0 }, _scale_exponent{ 0 }, _run_optimized_qasymm8(false)
557 {
558 }

◆ NEPixelWiseMultiplicationKernel() [2/3]

Prevent instances of this class from being copied (As this class contains pointers)

◆ NEPixelWiseMultiplicationKernel() [3/3]

Allow instances of this class to be moved.

◆ ~NEPixelWiseMultiplicationKernel()

Default destructor.

Member Function Documentation

◆ border_size()

BorderSize border_size ( ) const
overridevirtual

The size of the border for that kernel.

Returns
The width in number of elements of the border.

Reimplemented from IKernel.

Definition at line 784 of file NEPixelWiseMultiplicationKernel.cpp.

785 {
786  const unsigned int replicateSize = _output->info()->dimension(0) - std::min(_input1->info()->dimension(0), _input2->info()->dimension(0));
787  const unsigned int border = std::min<unsigned int>(num_elems_processed_per_iteration - 1U, replicateSize);
788  return BorderSize{ 0, border, 0, 0 };
789 }
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.

References ITensorInfo::dimension(), ITensor::info(), and arm_compute::U.

◆ configure()

void configure ( const ITensor input1,
const ITensor input2,
ITensor output,
float  scale,
ConvertPolicy  overflow_policy,
RoundingPolicy  rounding_policy 
)

Initialise the kernel's input, output and border mode.

Note
For scale equal to 1/255 only round to nearest even (implemented as round half up) is supported. For all other scale values only round to zero (implemented as round towards minus infinity) is supported.
Parameters
[in]input1An input tensor. Data types supported: U8/QASYMM8/S16/QSYMM16/F16/F32
[in]input2An input tensor. Data types supported: U8, QASYMM8 (only if input1 is QASYMM8), S16, QSYMM16 (only if input1 is QSYMM16), F16 (only if input1 is F16), F32 (only if input1 is F32).
[out]outputOutput tensor. Data types supported: U8 (Only if both inputs are U8), QASYMM8 (only if both inputs are QASYMM8), S16, QSYMM16 (only if both inputs are QSYMM16), F16 (only if input1 is F16), F32 (only if both inputs are F32).
[in]scaleScale to apply after multiplication. Scale must be positive and its value must be either 1/255 or 1/2^n where n is between 0 and 15.
[in]overflow_policyOverflow policy. ConvertPolicy cannot be WRAP if datatype is QASYMM8 or QSYMM16.
[in]rounding_policyRounding policy.

Definition at line 560 of file NEPixelWiseMultiplicationKernel.cpp.

561 {
563  ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
564 
565  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input1->info(), input2->info(), output->info(), scale, overflow_policy, rounding_policy));
566 
567  // Configure kernel window
568  auto win_config = validate_and_configure_window(input1->info(), input2->info(), output->info());
569  ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
570 
571  _input1 = input1;
572  _input2 = input2;
573  _output = output;
574  _scale = scale;
575  _scale_exponent = 0;
576  _func_quantized = nullptr;
577  _func_int = nullptr;
578  _func_float = nullptr;
579  _run_optimized_qasymm8 = false;
580 
581  bool is_scale_255 = false;
582  // Check and validate scaling factor
583  if(std::abs(scale - scale255_constant) < 0.00001f)
584  {
585  is_scale_255 = true;
586  }
587  else
588  {
589  int exponent = 0;
590 
591  std::frexp(scale, &exponent);
592 
593  // Store the positive exponent. We know that we compute 1/2^n
594  // Additionally we need to subtract 1 to compensate that frexp used a mantissa of 0.5
595  _scale_exponent = std::abs(exponent - 1);
596  }
597 
598  const DataType dt_input1 = input1->info()->data_type();
599  const DataType dt_input2 = input2->info()->data_type();
600  const DataType dt_output = output->info()->data_type();
601  const bool is_sat = (overflow_policy == ConvertPolicy::SATURATE);
602 
603  if(dt_input1 == DataType::QASYMM8 && dt_input2 == DataType::QASYMM8)
604  {
605  _run_optimized_qasymm8 = true;
606  }
607  else if(dt_input1 == DataType::QSYMM16 && dt_input2 == DataType::QSYMM16)
608  {
609  _func_quantized = &mul_saturate_QSYMM16_QSYMM16_QSYMM16_n;
610  }
611  else if(DataType::U8 == dt_input1 && DataType::U8 == dt_input2 && DataType::U8 == dt_output)
612  {
613  if(is_scale_255)
614  {
615  _func_int = is_sat ? &mul_U8_U8_U8_n<true, true> : &mul_U8_U8_U8_n<true, false>;
616  }
617  else
618  {
619  _func_int = is_sat ? &mul_U8_U8_U8_n<false, true> : &mul_U8_U8_U8_n<false, false>;
620  }
621  }
622  else if(DataType::S16 == dt_input1 && DataType::S16 == dt_input2 && DataType::S16 == dt_output)
623  {
624  if(is_scale_255)
625  {
626  _func_int = is_sat ? &mul_S16_S16_S16_n<true, true> : &mul_S16_S16_S16_n<true, false>;
627  }
628  else
629  {
630  _func_int = is_sat ? &mul_S16_S16_S16_n<false, true> : &mul_S16_S16_S16_n<false, false>;
631  }
632  }
633  else if(DataType::S16 == dt_input1 && DataType::U8 == dt_input2 && DataType::S16 == dt_output)
634  {
635  if(is_scale_255)
636  {
637  _func_int = is_sat ? &mul_S16_U8_S16_n<true, true> : &mul_S16_U8_S16_n<true, false>;
638  }
639  else
640  {
641  _func_int = is_sat ? &mul_S16_U8_S16_n<false, true> : &mul_S16_U8_S16_n<false, false>;
642  }
643  }
644  else if(DataType::U8 == dt_input1 && DataType::S16 == dt_input2 && DataType::S16 == dt_output)
645  {
646  if(is_scale_255)
647  {
648  _func_int = is_sat ? &mul_U8_S16_S16_n<true, true> : &mul_U8_S16_S16_n<true, false>;
649  }
650  else
651  {
652  _func_int = is_sat ? &mul_U8_S16_S16_n<false, true> : &mul_U8_S16_S16_n<false, false>;
653  }
654  }
655  else if(DataType::U8 == dt_input1 && DataType::U8 == dt_input2 && DataType::S16 == dt_output)
656  {
657  if(is_scale_255)
658  {
659  _func_int = is_sat ? &mul_U8_U8_S16_n<true, true> : &mul_U8_U8_S16_n<true, false>;
660  }
661  else
662  {
663  _func_int = is_sat ? &mul_U8_U8_S16_n<false, true> : &mul_U8_U8_S16_n<false, false>;
664  }
665  }
666  else if(DataType::F16 == dt_input1 && DataType::F16 == dt_input2 && DataType::F16 == dt_output)
667  {
668  _func_float = &mul_F16_F16_F16_n;
669  _func_int = nullptr;
670  }
671  else if(DataType::F32 == dt_input1 && DataType::F32 == dt_input2 && DataType::F32 == dt_output)
672  {
673  _func_float = &mul_F32_F32_F32_n;
674  _func_int = nullptr;
675  }
676  else
677  {
678  ARM_COMPUTE_ERROR("You called with the wrong img formats");
679  }
680 
681  INEKernel::configure(win_config.second);
682 }
#define ARM_COMPUTE_ERROR(...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:261
quantized, symmetric fixed-point 16-bit number
std::pair< Status, Window > validate_and_configure_window(ITensorInfo *input, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier, const Size2D &dilation)
1 channel, 1 U8 per channel
1 channel, 1 F32 per channel
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:327
1 channel, 1 F16 per channel
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:160
quantized, asymmetric fixed-point 8-bit number
1 channel, 1 S16 per channel
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
DataType
Available data types.
Definition: Types.h:74

References ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, ARM_COMPUTE_UNUSED, ITensorInfo::data_type(), arm_compute::F16, arm_compute::F32, ITensor::info(), arm_compute::QASYMM8, arm_compute::QSYMM16, arm_compute::test::validation::rounding_policy, arm_compute::S16, arm_compute::SATURATE, arm_compute::test::validation::scale, arm_compute::U8, and arm_compute::validate_and_configure_window().

Referenced by NENormalizationLayer::configure(), and NELSTMLayer::configure().

◆ name()

const char* name ( ) const
inlineoverridevirtual

Name of the kernel.

Returns
Kernel name

Implements ICPPKernel.

Definition at line 38 of file NEPixelWiseMultiplicationKernel.h.

39  {
40  return "NEPixelWiseMultiplicationKernel";
41  }

◆ operator=() [1/2]

Prevent instances of this class from being copied (As this class contains pointers)

◆ operator=() [2/2]

Allow instances of this class to be moved.

◆ run()

void run ( const Window window,
const ThreadInfo info 
)
overridevirtual

Execute the kernel on the passed window.

Warning
If is_parallelisable() returns false then the passed window must be equal to window()
Note
The window has to be a region within the window returned by the window() method
The width of the window has to be a multiple of num_elems_processed_per_iteration().
Parameters
[in]windowRegion on which to execute the kernel. (Must be a region of the window returned by window())
[in]infoInfo about executing thread and CPU.

Implements ICPPKernel.

Definition at line 694 of file NEPixelWiseMultiplicationKernel.cpp.

695 {
699 
700  const TensorShape &in_shape1 = _input1->info()->tensor_shape();
701  const TensorShape &in_shape2 = _input2->info()->tensor_shape();
702  const TensorShape &out_shape = _output->info()->tensor_shape();
703 
704  bool can_collapse = true;
705  if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1)
706  {
707  can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ);
708  for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); ++d)
709  {
710  can_collapse = (in_shape1[d] == in_shape2[d]);
711  }
712  }
713 
714  bool has_collapsed = false;
715  Window collapsed = can_collapse ? window.collapse_if_possible(INEKernel::window(), Window::DimZ, &has_collapsed) : window;
716 
717  const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1;
718  const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2;
719 
720  Window slice = collapsed.first_slice_window_3D();
721  Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed);
722  Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed);
723 
724  Iterator input1(_input1, slice_input1);
725  Iterator input2(_input2, slice_input2);
726  Iterator output(_output, slice);
727 
728  if(is_data_type_quantized(_input1->info()->data_type()))
729  {
730  if(_run_optimized_qasymm8)
731  {
732  const int32x4_t input1_voffset = vdupq_n_s32(_input1->info()->quantization_info().uniform().offset);
733  const float32x4_t input1_vscale = vdupq_n_f32(_input1->info()->quantization_info().uniform().scale);
734  const int32x4_t input2_voffset = vdupq_n_s32(_input2->info()->quantization_info().uniform().offset);
735  const float32x4_t input2_vscale = vdupq_n_f32(_input2->info()->quantization_info().uniform().scale);
736  const float32x4_t output_voffset = vdupq_n_f32(static_cast<float>(_output->info()->quantization_info().uniform().offset));
737  const float output_scale = _output->info()->quantization_info().uniform().scale;
738  const float32x4_t vinvscale = vdupq_n_f32(1.f / (output_scale / _scale));
739 
740  execute_window_loop(collapsed, [&](const Coordinates &)
741  {
742  mul_saturate_QASYMM8_QASYMM8_QASYMM8_n_opt(input1.ptr(), input2.ptr(), output.ptr(), _scale,
743  input1_vscale, input1_voffset, input2_vscale, input2_voffset, output_voffset, vinvscale);
744  ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input1));
745  ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input2));
746  },
747  input1, input2, output);
748  }
749  else
750  {
751  execute_window_loop(collapsed, [&](const Coordinates &)
752  {
753  (*_func_quantized)(input1.ptr(), input2.ptr(), output.ptr(), _scale,
754  _input1->info()->quantization_info().uniform(), _input2->info()->quantization_info().uniform(), _output->info()->quantization_info().uniform());
755  ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input1));
756  ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input2));
757  },
758  input1, input2, output);
759  }
760  }
761  else if(_func_int != nullptr)
762  {
763  execute_window_loop(collapsed, [&](const Coordinates &)
764  {
765  (*_func_int)(input1.ptr(), input2.ptr(), output.ptr(), _scale_exponent);
766  ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input1));
767  ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input2));
768  },
769  input1, input2, output);
770  }
771  else
772  {
773  ARM_COMPUTE_ERROR_ON(_func_float == nullptr);
774  execute_window_loop(collapsed, [&](const Coordinates &)
775  {
776  (*_func_float)(input1.ptr(), input2.ptr(), output.ptr(), _scale);
777  ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input1));
778  ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_input2));
779  },
780  input1, input2, output);
781  }
782 }
bool is_data_type_quantized(DataType dt)
Check if a given data type is of quantized type.
Definition: Utils.h:1010
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
virtual DataType data_type() const =0
Data type used for each element of the tensor.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:337
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:160
Window collapse_if_possible(const Window &full_window, size_t first, size_t last, bool *has_collapsed=nullptr) const
Collapse the dimensions between first and last if possible.
Definition: Window.inl:54
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
UniformQuantizationInfo uniform() const
Return per layer quantization info.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:122
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:940
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)

References ARM_COMPUTE_ERROR_ON, ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, ARM_COMPUTE_UNUSED, Window::collapse_if_possible(), TensorShape::collapsed_from(), ITensorInfo::data_type(), Window::DimZ, arm_compute::execute_window_loop(), Window::first_slice_window_3D(), ITensor::info(), arm_compute::test::validation::info, arm_compute::is_data_type_quantized(), Dimensions< T >::num_dimensions(), UniformQuantizationInfo::offset, Iterator::ptr(), ITensorInfo::quantization_info(), UniformQuantizationInfo::scale, arm_compute::test::validation::reference::slice(), Window::slide_window_slice_3D(), ITensorInfo::tensor_shape(), TensorShape::total_size(), QuantizationInfo::uniform(), and IKernel::window().

◆ validate()

Status validate ( const ITensorInfo input1,
const ITensorInfo input2,
const ITensorInfo output,
float  scale,
ConvertPolicy  overflow_policy,
RoundingPolicy  rounding_policy 
)
static

Static function to check if given info will lead to a valid configuration of NEPixelWiseMultiplicationKernel.

Note
For scale equal to 1/255 only round to nearest even (implemented as round half up) is supported. For all other scale values only round to zero (implemented as round towards minus infinity) is supported.
Parameters
[in]input1An input tensor info. Data types supported: U8/QASYMM8/QSYMM16/S16/F16/F32
[in]input2An input tensor info. Data types supported: U8, QASYMM8 (only if input1 is QASYMM8), S16, QSYMM16 (only if input1 is QSYMM16), F16 (only if input1 is F16), F32 (only if input1 is F32).
[in]outputOutput tensor info. Data types supported: U8 (Only if both inputs are U8), QASYMM8 (only if both inputs are QASYMM8), S16, QSYMM16 (only if both inputs are QSYMM16), F16 (only if input1 is F16), F32 (only if both inputs are F32).
[in]scaleScale to apply after multiplication. Scale must be positive and its value must be either 1/255 or 1/2^n where n is between 0 and 15.
[in]overflow_policyOverflow policy. ConvertPolicy cannot be WRAP if datatype is QASYMM8 or QSYMM16.
[in]rounding_policyRounding policy.
Returns
a status

Definition at line 684 of file NEPixelWiseMultiplicationKernel.cpp.

686 {
687  ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
688  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input1, input2, output, scale, overflow_policy, rounding_policy));
689  ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input1->clone().get(), input2->clone().get(), output->clone().get()).first);
690 
691  return Status{};
692 }
std::pair< Status, Window > validate_and_configure_window(ITensorInfo *input, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier, const Size2D &dilation)
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:193
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161

References ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_RETURN_ON_ERROR, ICloneable< T >::clone(), arm_compute::test::validation::rounding_policy, arm_compute::test::validation::scale, and arm_compute::validate_and_configure_window().

Referenced by NEPixelWiseMultiplication::validate(), NENormalizationLayer::validate(), and NELSTMLayer::validate().


The documentation for this class was generated from the following files: