Simd Library Documentation.

Home | Release Notes | Download | Documentation | Issues | GitHub
SynetQuantizedMul Class Reference

The SynetQuantizedMul class is a C++ wrapper of quantized (UINT8/FP32) element-wise multiplication. More...

#include <SimdSynet.hpp>

Public Member Functions

 SynetQuantizedMul ()
 
virtual ~SynetQuantizedMul ()
 
SIMD_INLINE void Init (const Shape &aShape, SimdTensorDataType aType, float aScale, int32_t aZero, const Shape &bShape, SimdTensorDataType bType, float bScale, int32_t bZero, SimdTensorDataType dstType, float dstScale, int32_t dstZero)
 
SIMD_INLINE bool Enable () const
 
SIMD_INLINE void Forward (const uint8_t *a, const uint8_t *b, uint8_t *dst)
 
SIMD_INLINE void Clear ()
 

Detailed Description

The SynetQuantizedMul class is a C++ wrapper of quantized (UINT8/FP32) element-wise multiplication.

The class wraps C API functions SimdSynetQuantizedMulInit and SimdSynetQuantizedMulForward. It dequantizes UINT8 inputs as (value - zero)*scale, multiplies the two values and converts the result to FP32 or UINT8 output. FP32 inputs and outputs ignore the corresponding quantization zero. Algorithm's details for UINT8 output:

for(i = 0; i < size; ++i)
{
    _a = (a[i] - aZero)*aScale;
    _b = (b[i] - bZero)*bScale;
    dst[i] = RestrictRange(Round((_a * _b)/dstScale) + dstZero, 0, 255);
}

The current implementation creates a context for compatible input shapes (equal or broadcast) and FP32/UINT8 input and output tensor types. Call Init() before Forward(). Use Enable() to check that a context was created. The context is released by Clear() or by the destructor.

Using example:

#include "Simd/SimdSynet.hpp"

int main()
{
    const size_t n = 64;
    std::vector<uint8_t> a(n, 50), b(n, 80), dst(n, 0);
    Simd::Shape dims = Simd::Shape({ n });
    float aScale = 0.010f, bScale = 0.020f, dstScale = 0.015f;
    int32_t aZero = 47, bZero = 30, dstZero = 38;

    Simd::SynetQuantizedMul mul;
    mul.Init(dims, SimdTensorData8u, aScale, aZero, dims, SimdTensorData8u, bScale, bZero,
        SimdTensorData8u, dstScale, dstZero);
    if (mul.Enable())
        mul.Forward(a.data(), b.data(), dst.data());

    return 0;
}

Constructor & Destructor Documentation

◆ SynetQuantizedMul()

Creates a new empty SynetQuantizedMul class.

◆ ~SynetQuantizedMul()

virtual ~SynetQuantizedMul ( )
virtual

SynetQuantizedMul class destructor. Releases internal context.

Member Function Documentation

◆ Init()

SIMD_INLINE void Init ( const Shape aShape,
SimdTensorDataType  aType,
float  aScale,
int32_t  aZero,
const Shape bShape,
SimdTensorDataType  bType,
float  bScale,
int32_t  bZero,
SimdTensorDataType  dstType,
float  dstScale,
int32_t  dstZero 
)

Initializes (or re-initializes) element-wise quantized multiplication of two UINT8/FP32 tensors.

Creates an internal context with using of function SimdSynetQuantizedMulInit. The context is recreated only if input tensor shapes were changed.

Note
This function is a C++ wrapper for function SimdSynetQuantizedMulInit.
Parameters
[in]aShape- a shape of input A tensor.
[in]aType- a type of input A tensor. Can be SimdTensorData32f or SimdTensorData8u.
[in]aScale- a quantization scale of input A tensor.
[in]aZero- a quantization zero of input A tensor.
[in]bShape- a shape of input B tensor.
[in]bType- a type of input B tensor. Can be SimdTensorData32f or SimdTensorData8u.
[in]bScale- a quantization scale of input B tensor.
[in]bZero- a quantization zero of input B tensor.
[in]dstType- a type of output tensor. Can be SimdTensorData32f or SimdTensorData8u.
[in]dstScale- an output quantization scale.
[in]dstZero- an output quantization zero.

◆ Enable()

SIMD_INLINE bool Enable ( ) const

Checks that the internal quantized multiplication context was created.

Returns
true if the context exists and Forward() can be called.

◆ Forward()

SIMD_INLINE void Forward ( const uint8_t *  a,
const uint8_t *  b,
uint8_t *  dst 
)

Performs element-wise quantized multiplication of two UINT8/FP32 tensors.

The function multiplies corresponding elements of input tensors A and B with dequantization and output quantization. The actual data types, tensor shape and quantization parameters are stored in the context created by Init().

Note
This function is a C++ wrapper for function SimdSynetQuantizedMulForward.
Parameters
[in]a- a pointer to input A tensor.
[in]b- a pointer to input B tensor.
[out]dst- a pointer to output tensor.

◆ Clear()

SIMD_INLINE void Clear ( )

Releases internal context and clears stored tensor shapes.