Simd Library Documentation.

Home | Release Notes | Download | Documentation | Issues | GitHub
Other functions needed for quantization

These functions are used to accelerate quantization algorithms in Synet Framework. More...

Functions

SIMD_API void SimdSynetDequantizeLinear (const uint8_t *src, size_t size, int32_t bias, const float *norm, float *dst)
 Dequantizes a UINT8 tensor to FP32 with a single scale and zero-point correction. More...
 
SIMD_API void SimdSynetQuantizedConcatLayerForward (size_t count, const uint8_t **src, size_t num, const size_t *size, const int32_t *bias, const float *norm, const float *scale, int32_t zero, uint8_t *dst)
 Concatenates UINT8 tensors with requantization. More...
 
SIMD_API void SimdSynetQuantizedPoolingAverage (const uint8_t *src, const float *srcScale, int srcZero, size_t batch, size_t srcC, size_t srcH, size_t srcW, size_t kernelY, size_t kernelX, size_t strideY, size_t strideX, size_t padY, size_t padX, SimdBool excludePad, uint8_t *dst, const float *dstScale, int dstZero, size_t dstH, size_t dstW, SimdTensorFormatType format)
 Performs quantized 2D average pooling for an UINT8 tensor. More...
 
SIMD_API void SimdSynetQuantizedScaleLayerForward (const uint8_t *src, const float *srcScale, int srcZero, size_t channels, size_t spatial, const float *scale, const float *bias, uint8_t *dst, const float *dstScale, int dstZero, SimdTensorFormatType format)
 Performs forward propagation of UINT8 quantized scale layer. More...
 
SIMD_API void SimdSynetQuantizedShuffleLayerForward (const uint8_t *src0, int bias0, const float *norm0, size_t srcC0, const uint8_t *src1, int bias1, const float *norm1, size_t srcC1, size_t spatial, uint8_t *dst0, uint8_t *dst1, const float *scale, int zero, SimdTensorFormatType format, int type)
 Performs forward propagation of UINT8 quantized shuffle layer. More...
 
SIMD_API void SimdSynetQuantizeLinear (const float *src, size_t size, const float *norm, int32_t zero, uint8_t *dst)
 Performs FP32 to UINT8 linear quantization. More...
 

Detailed Description

These functions are used to accelerate quantization algorithms in Synet Framework.

Function Documentation

◆ SimdSynetDequantizeLinear()

void SimdSynetDequantizeLinear ( const uint8_t *  src,
size_t  size,
int32_t  bias,
const float *  norm,
float *  dst 
)

Dequantizes a UINT8 tensor to FP32 with a single scale and zero-point correction.

Algorithm's details for SimdSynetDequantizeLinear:

for(i = 0; i < size; ++i)
    dst[i] = (src[i] + bias) * norm[0];

This corresponds to dst = (src - zero) * scale when bias is equal to -zero and norm[0] is equal to scale.

Parameters
[in]src- a pointer to UINT8 input tensor.
[in]size- a number of elements in the input and output tensors.
[in]bias- an integer value added to every input element, typically negative zero-point.
[in]norm- a pointer to FP32 dequantization scale. Only norm[0] is used.
[out]dst- a pointer to FP32 output tensor.

◆ SimdSynetQuantizedConcatLayerForward()

void SimdSynetQuantizedConcatLayerForward ( size_t  count,
const uint8_t **  src,
size_t  num,
const size_t *  size,
const int32_t *  bias,
const float *  norm,
const float *  scale,
int32_t  zero,
uint8_t *  dst 
)

Concatenates UINT8 tensors with requantization.

Algorithm's details:

for(n = 0; n < num; ++n)
    for(s = 0, offset = 0; s < count; offset += size[s], ++s)
        for(i = 0; i < size[s]; ++i)
            dst[offset + i] = RestrictRange(Round((src[s][n*size[s] + i] + bias[s])*norm[s]*scale[0]) + zero, 0, 255);
Note
This function is used in Synet Framework.
Parameters
[in]count- a number of input tensors.
[in]src- an array with pointers to UINT8 input tensors.
[in]num- a number of concatenated blocks.
[in]size- an array with sizes of concatenated parts for each input tensor.
[in]bias- an array with dequantization biases of input tensors (usually -zero).
[in]norm- an array with dequantization scales of input tensors.
[in]scale- a pointer to output quantization norm (usually 1/scale).
[in]zero- an output quantization zero.
[out]dst- a pointer to the UINT8 output tensor.

◆ SimdSynetQuantizedPoolingAverage()

void SimdSynetQuantizedPoolingAverage ( const uint8_t *  src,
const float *  srcScale,
int  srcZero,
size_t  batch,
size_t  srcC,
size_t  srcH,
size_t  srcW,
size_t  kernelY,
size_t  kernelX,
size_t  strideY,
size_t  strideX,
size_t  padY,
size_t  padX,
SimdBool  excludePad,
uint8_t *  dst,
const float *  dstScale,
int  dstZero,
size_t  dstH,
size_t  dstW,
SimdTensorFormatType  format 
)

Performs quantized 2D average pooling for an UINT8 tensor.

For every output position the pooling window starts at (dstY*strideY - padY, dstX*strideX - padX), is clipped by input boundaries and is averaged independently for every channel. If excludePad is SimdTrue, the divisor is the clipped window area; otherwise it is kernelY*kernelX. It supports SimdTensorFormatNchw and SimdTensorFormatNhwc.

Algorithm's details:

for(b = 0; b < batch; ++b)
    for(c = 0; c < srcC; ++c)
        for(dy = 0; dy < dstH; ++dy)
            for(dx = 0; dx < dstW; ++dx)
            {
                yBeg = Max(0, dy*strideY - padY);
                yEnd = Min(srcH, dy*strideY - padY + kernelY);
                xBeg = Max(0, dx*strideX - padX);
                xEnd = Min(srcW, dx*strideX - padX + kernelX);
                sum = 0;
                for(sy = yBeg; sy < yEnd; ++sy)
                    for(sx = xBeg; sx < xEnd; ++sx)
                        sum += (src[b, c, sy, sx] - srcZero)*srcScale[0];
                val = sum / (excludePad ? (yEnd - yBeg)*(xEnd - xBeg) : kernelY*kernelX);
                dst[b, c, dy, dx] = RestrictRange(Round(val/dstScale[0]) + dstZero, 0, 255);
        }
Note
This function is used in Synet Framework.
Parameters
[in]src- a pointer to the input UINT8 tensor. The size of the array must be equal to srcC*srcH*srcW.
[in]srcScale- a pointer to quantization scale of input tensor.
[in]srcZero- a quantization zero parameter of input tensor.
[in]batch- a batch size.
[in]srcC- a number of input and output channels.
[in]srcH- an input height.
[in]srcW- an input width.
[in]kernelY- a height of the pooling kernel.
[in]kernelX- a width of the pooling kernel.
[in]strideY- a y-stride of the pooling.
[in]strideX- a x-stride of the pooling.
[in]padY- a pad to the top of the input image.
[in]padX- a pad to the left of the input image.
[in]excludePad- a flag that excludes padded positions from average value calculation.
[out]dst- a pointer to the output UINT8 tensor. The size of the array must be equal to srcC*dstH*dstW.
[in]dstScale- a pointer to output quantization scale.
[in]dstZero- an output quantization zero.
[in]dstH- an output height.
[in]dstW- an output width.
[in]format- a format of input and output tensor. It can be SimdTensorFormatNchw or SimdTensorFormatNhwc.

◆ SimdSynetQuantizedScaleLayerForward()

void SimdSynetQuantizedScaleLayerForward ( const uint8_t *  src,
const float *  srcScale,
int  srcZero,
size_t  channels,
size_t  spatial,
const float *  scale,
const float *  bias,
uint8_t *  dst,
const float *  dstScale,
int  dstZero,
SimdTensorFormatType  format 
)

Performs forward propagation of UINT8 quantized scale layer.

Algorithm's details:

value = (src - srcZero)*srcScale[0];
value = value*scale[c] + (bias ? bias[c] : 0);
dst = RestrictRange(Round(value/dstScale[0]) + dstZero, 0, 255);
Note
This function is used in Synet Framework.
Parameters
[in]src- a pointer to UINT8 input tensor.
[in]srcScale- a pointer to quantization scale of input tensor.
[in]srcZero- a quantization zero parameter of input tensor.
[in]channels- a number of channels in (input/output) tensors.
[in]spatial- a spatial size of (input/output) tensors.
[in]scale- a pointer to the 32-bit float array with scale coefficients. The size of the array is equal to channels.
[in]bias- a pointer to the 32-bit float array with bias coefficients. The size of the array is equal to channels. Can be NULL.
[out]dst- a pointer to UINT8 output tensor.
[in]dstScale- a pointer to output quantization scale.
[in]dstZero- an output quantization zero.
[in]format- a format of input and output tensors. It can be SimdTensorFormatNchw or SimdTensorFormatNhwc.

◆ SimdSynetQuantizedShuffleLayerForward()

void SimdSynetQuantizedShuffleLayerForward ( const uint8_t *  src0,
int  bias0,
const float *  norm0,
size_t  srcC0,
const uint8_t *  src1,
int  bias1,
const float *  norm1,
size_t  srcC1,
size_t  spatial,
uint8_t *  dst0,
uint8_t *  dst1,
const float *  scale,
int  zero,
SimdTensorFormatType  format,
int  type 
)

Performs forward propagation of UINT8 quantized shuffle layer.

The function dequantizes channels from two input tensors, performs channel shuffle and requantizes results to two output tensors. For type 0 pairs of channels from src0 and src1 are split between dst0 and dst1. For type 1 channels from src0 and src1 are interleaved back into dst0 and dst1.

Note
This function is used in Synet Framework.
Parameters
[in]src0- a pointer to UINT8 data of the first input tensor.
[in]bias0- a dequantization bias parameter of the first input tensor (-zero).
[in]norm0- a dequantization norm parameter of the first input tensor (scale).
[in]srcC0- a number of channels in the first input tensor.
[in]src1- a pointer to UINT8 data of the second input tensor.
[in]bias1- a dequantization bias parameter of the second input tensor (-zero).
[in]norm1- a dequantization norm parameter of the second input tensor (scale).
[in]srcC1- a number of channels in the second input tensor.
[in]spatial- a spatial size of (input/output) tensors.
[out]dst0- a pointer to UINT8 data of the first output tensor.
[out]dst1- a pointer to UINT8 data of the second output tensor.
[in]scale- an output quantization norm (1/scale).
[in]zero- an output quantization zero.
[in]format- a format of input and output tensors. It can be SimdTensorFormatNchw or SimdTensorFormatNhwc.
[in]type- a shuffle type: 0 for split operation, 1 for interleave operation.

◆ SimdSynetQuantizeLinear()

void SimdSynetQuantizeLinear ( const float *  src,
size_t  size,
const float *  norm,
int32_t  zero,
uint8_t *  dst 
)

Performs FP32 to UINT8 linear quantization.

Algorithm's details for SimdSynetQuantizeLinear:

for(i = 0; i < size; ++i)
    dst[i] = RestrictRange(Round(src[i]*norm[0]) + zero, 0, 255);
Parameters
[in]src- a pointer to FP32 input tensor.
[in]size- a size of the input and output tensors.
[in]norm- a pointer to quantization norm (usually 1/scale).
[in]zero- a quantization zero.
[out]dst- a pointer to UINT8 output tensor.