Simd Library Documentation.

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

The SynetScale8i class is a C++ wrapper of FP32/UINT8 scale and bias. More...

#include <SimdSynet.hpp>

Public Member Functions

 SynetScale8i ()
 
virtual ~SynetScale8i ()
 
SIMD_INLINE void Init (size_t batch, size_t channels, size_t spatial, SimdTensorDataType srcType, SimdTensorDataType dstType, SimdTensorFormatType format, SimdSynetCompatibilityType compatibility)
 
SIMD_INLINE bool Enable () const
 
SIMD_INLINE size_t InternalBufferSize () const
 
SIMD_INLINE void SetParams (const float *scale, const float *bias, const float *const *stats)
 
SIMD_INLINE void Forward (const uint8_t *src, uint8_t *dst)
 
SIMD_INLINE void Clear ()
 

Detailed Description

The SynetScale8i class is a C++ wrapper of FP32/UINT8 scale and bias.

The class wraps C API functions SimdSynetScale8iInit, SimdSynetScale8iInternalBufferSize, SimdSynetScale8iSetParams and SimdSynetScale8iForward. It performs per-channel affine transformation of FP32 or UINT8 tensors. When UINT8 is used, conversion parameters are derived from statistics passed to SetParams():

dst = Convert(src*internalScale[c] + internalShift[c]);

Algorithm's details after SetParams() prepares internal coefficients (example for NCHW format):

for(b = 0; b < batch; ++b)
    for(c = 0; c < channels; ++c)
        for(s = 0; s < spatial; ++s)
            dst[(b*channels + c)*spatial + s] = Convert(src[(b*channels + c)*spatial + s]*internalScale[c] + internalShift[c]);

The current implementation creates a context for FP32 or UINT8 input and output tensor types and SimdTensorFormatNchw or SimdTensorFormatNhwc tensor format. Compatibility flags select precise or narrowed UINT8 calculation mode. Narrowed mode uses unsigned range [0, 180]; otherwise the range is [0, 255]. Call Init() and SetParams() 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 batch = 1, channels = 4, spatial = 16;
    const SimdSynetCompatibilityType compatibility = (SimdSynetCompatibilityType)(SimdSynetCompatibility8iNarrowed | SimdSynetCompatibilityFmaUse);
    std::vector<uint8_t> src(batch * channels * spatial, 80), dst(batch * channels * spatial, 0);
    std::vector<float> scale(channels, 0.5f), bias(channels, 0.1f);
    std::vector<float> srcMin(channels, 0.0f), srcMax(channels, 1.0f);
    std::vector<float> dstMin(channels, 0.0f), dstMax(channels, 1.0f);
    const float * stats[4] = { srcMin.data(), srcMax.data(), dstMin.data(), dstMax.data() };

    Simd::SynetScale8i scale8i;
    scale8i.Init(batch, channels, spatial, SimdTensorData8u, SimdTensorData8u, SimdTensorFormatNhwc, compatibility);
    if (scale8i.Enable())
    {
        scale8i.SetParams(scale.data(), bias.data(), stats);
        scale8i.Forward(src.data(), dst.data());
    }

    return 0;
}

Constructor & Destructor Documentation

◆ SynetScale8i()

Creates a new empty SynetScale8i class.

◆ ~SynetScale8i()

virtual ~SynetScale8i ( )
virtual

SynetScale8i class destructor. Releases internal context.

Member Function Documentation

◆ Init()

SIMD_INLINE void Init ( size_t  batch,
size_t  channels,
size_t  spatial,
SimdTensorDataType  srcType,
SimdTensorDataType  dstType,
SimdTensorFormatType  format,
SimdSynetCompatibilityType  compatibility 
)

Initializes (or re-initializes) an FP32/UINT8 scale and bias context.

Creates an internal context with using of function SimdSynetScale8iInit. The context is recreated only if batch size, channel count, spatial size, tensor types, tensor format or compatibility flags were changed.

Note
This function is a C++ wrapper for function SimdSynetScale8iInit.
Parameters
[in]batch- a batch size.
[in]channels- a number of channels in input and output tensors.
[in]spatial- a spatial size (height*width) of input and output tensors.
[in]srcType- an input data type. It can be SimdTensorData32f or SimdTensorData8u.
[in]dstType- an output data type. It can be SimdTensorData32f or SimdTensorData8u.
[in]format- a format of input and output tensors. It can be SimdTensorFormatNchw or SimdTensorFormatNhwc.
[in]compatibility- calculation compatibility flags. They select precise or narrowed UINT8 calculation mode. Narrowed mode uses unsigned range [0, 180]; otherwise the range is [0, 255].

◆ Enable()

SIMD_INLINE bool Enable ( ) const

Checks that the internal scale context was created.

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

◆ InternalBufferSize()

SIMD_INLINE size_t InternalBufferSize ( ) const

Gets the size in bytes of internal storage used by the scale context.

The returned value reports internal storage used to store conversion parameters, scale and shift arrays.

Note
This function is a C++ wrapper for function SimdSynetScale8iInternalBufferSize.
Returns
a number of bytes used by internal buffers.

◆ SetParams()

SIMD_INLINE void SetParams ( const float *  scale,
const float *  bias,
const float *const *  stats 
)

Sets per-channel scale, bias and tensor statistics for FP32/UINT8 scale algorithm.

This function must be called before Forward(). The scale array contains FP32 per-channel scale coefficients with channels elements. Source statistics (stats[0], stats[1], each with channels elements) define per-channel source quantization parameters; destination statistics (stats[2], stats[3], each with channels elements) define per-channel output quantization parameters. After the first call stats can be NULL.

Note
This function is a C++ wrapper for function SimdSynetScale8iSetParams.
Parameters
[in]scale- a pointer to original FP32 per-channel scale coefficients.
[in]bias- a pointer to original FP32 per-channel bias coefficients. Can be NULL.
[in]stats- a pointer to pointers with input and output statistics: input min (stats[0]), input max (stats[1]), output min (stats[2]) and output max (stats[3]). Can be NULL for subsequent calls after statistics were initialized.

◆ Forward()

SIMD_INLINE void Forward ( const uint8_t *  src,
uint8_t *  dst 
)

Performs forward propagation of FP32/UINT8 scale algorithm.

The function applies per-channel scale and bias prepared by SetParams() and converts between FP32 and UINT8 according to the types stored in the context created by Init().

Note
This function is a C++ wrapper for function SimdSynetScale8iForward.
Parameters
[in]src- a pointer to input tensor data. Its type is defined by parameter srcType of Init().
[out]dst- a pointer to output tensor data. Its type is defined by parameter dstType of Init().

◆ Clear()

SIMD_INLINE void Clear ( )

Releases internal context and clears stored scale parameters.