Simd Library Documentation.

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

The SynetAdd16b class is a C++ wrapper of 16-bit (BF16/FP32) element-wise addition. More...

#include <SimdSynet.hpp>

Public Member Functions

 SynetAdd16b ()
 
virtual ~SynetAdd16b ()
 
SIMD_INLINE void Init (const Shape &aShape, SimdTensorDataType aType, const Shape &bShape, SimdTensorDataType bType, SimdTensorDataType dstType, SimdTensorFormatType format)
 
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 SynetAdd16b class is a C++ wrapper of 16-bit (BF16/FP32) element-wise addition.

The class wraps C API functions SimdSynetAdd16bInit and SimdSynetAdd16bForward. It adds two tensors with equal shapes. BF16 values are converted to FP32 before addition and converted back after addition when the corresponding tensor type is SimdTensorData16b:

for(i = 0; i < shapeSize; ++i)
{
    A = aType == SimdTensorData16b ? BFloat16ToFloat32(a[i]) : a[i];
    B = bType == SimdTensorData16b ? BFloat16ToFloat32(b[i]) : b[i];
    D = A + B;
    dst[i] = dstType == SimdTensorData16b ? Float32ToBFloat16(D) : D;
}

The current implementation creates a context only for equal input shapes, FP32/BF16 input and output tensor types, and SimdTensorFormatUnknown, SimdTensorFormatNchw or SimdTensorFormatNhwc tensor format. 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<float> a(n, 1.0f), b(n, 2.0f), dst(n, 0.0f);
    Simd::Shape dims = Simd::Shape({ n });

    Simd::SynetAdd16b add;
    add.Init(dims, SimdTensorData32f, dims, SimdTensorData32f, SimdTensorData32f, SimdTensorFormatNhwc);
    if (add.Enable())
        add.Forward((const uint8_t*)a.data(), (const uint8_t*)b.data(), (uint8_t*)dst.data());

    return 0;
}

Constructor & Destructor Documentation

◆ SynetAdd16b()

Creates a new empty SynetAdd16b class.

◆ ~SynetAdd16b()

virtual ~SynetAdd16b ( )
virtual

SynetAdd16b class destructor. Releases internal context.

Member Function Documentation

◆ Init()

SIMD_INLINE void Init ( const Shape aShape,
SimdTensorDataType  aType,
const Shape bShape,
SimdTensorDataType  bType,
SimdTensorDataType  dstType,
SimdTensorFormatType  format 
)

Initializes (or re-initializes) element-wise addition of two FP32/BF16 tensors.

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

Note
This function is a C++ wrapper for function SimdSynetAdd16bInit.
Parameters
[in]aShape- a shape of input A tensor.
[in]aType- a type of input A tensor. Can be SimdTensorData32f or SimdTensorData16b.
[in]bShape- a shape of input B tensor.
[in]bType- a type of input B tensor. Can be SimdTensorData32f or SimdTensorData16b.
[in]dstType- a type of output tensor. Can be SimdTensorData32f or SimdTensorData16b.
[in]format- a format of input / output tensors.

◆ Enable()

SIMD_INLINE bool Enable ( ) const

Checks that the internal addition 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 addition of two FP32/BF16 tensors.

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

Note
This function is a C++ wrapper for function SimdSynetAdd16bForward.
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.