Simd Library Documentation.

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

The SynetGatherElements class is a C++ wrapper of ONNX-style GatherElements. More...

#include <SimdSynet.hpp>

Public Member Functions

 SynetGatherElements ()
 
virtual ~SynetGatherElements ()
 
SIMD_INLINE void Init (SimdTensorDataType dataType, SimdTensorDataType indexType, SimdBool indexConst, size_t indexUsers, const Shape &outer, size_t srcCount, size_t inner, size_t idxCount)
 
SIMD_INLINE bool Enable () const
 
SIMD_INLINE size_t InternalBufferSize () const
 
SIMD_INLINE void SetIndex (const uint8_t *idx)
 
SIMD_INLINE void Forward (const uint8_t *src, const uint8_t *idx, uint8_t *dst)
 
SIMD_INLINE void Clear ()
 

Detailed Description

The SynetGatherElements class is a C++ wrapper of ONNX-style GatherElements.

The class wraps C API functions SimdSynetGatherElementsInit, SimdSynetGatherElementsSetIndex, SimdSynetGatherElementsInternalBufferSize and SimdSynetGatherElementsForward. It gathers elements from an input tensor along one dimension according to an index tensor. It supports FP32, BF16 and UINT8 data tensors and INT32 or INT64 index tensors. The input tensor shape is:

outer[0] * ... * outer[outer.size() - 1] * srcCount * inner

The index and output tensor shape is:

outer[0] * ... * outer[outer.size() - 1] * idxCount * inner

Algorithm's details:

for(b = 0; b < outer[0]*...*outer[outer.size() - 1]; ++b)
    for(c = 0; c < idxCount; ++c)
        for(i = 0; i < inner; ++i)
        {
            ic = idx[b, c, i];
            if (ic < 0)
                ic += srcCount;
            dst[b, c, i] = src[b, ic, i];
        }

If indexConst is SimdTrue, constant indexes can be analyzed by SetIndex() to avoid repeated negative-index checks and to reduce repeated outer index processing when possible. 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 srcCount = 4, inner = 1, idxCount = 3;
    std::vector<float> src(8), dst(6);
    std::vector<int32_t> idx(6);
    for (size_t i = 0; i < src.size(); ++i)
        src[i] = float(i);
    idx[0] = 0; idx[1] = 2; idx[2] = 1;
    idx[3] = 3; idx[4] = 1; idx[5] = 0;
    Simd::Shape outer = Simd::Shape({ 2 });

    Simd::SynetGatherElements gather;
    gather.Init(SimdTensorData32f, SimdTensorData32i, SimdFalse, 1, outer, srcCount, inner, idxCount);
    if (gather.Enable())
        gather.Forward((const uint8_t*)src.data(), (const uint8_t*)idx.data(), (uint8_t*)dst.data());

    return 0;
}

Constructor & Destructor Documentation

◆ SynetGatherElements()

Creates a new empty SynetGatherElements class.

◆ ~SynetGatherElements()

virtual ~SynetGatherElements ( )
virtual

SynetGatherElements class destructor. Releases internal context.

Member Function Documentation

◆ Init()

SIMD_INLINE void Init ( SimdTensorDataType  dataType,
SimdTensorDataType  indexType,
SimdBool  indexConst,
size_t  indexUsers,
const Shape outer,
size_t  srcCount,
size_t  inner,
size_t  idxCount 
)

Initializes (or re-initializes) a gather-elements context.

Creates an internal context with using of function SimdSynetGatherElementsInit. The context is recreated only if outer shape, srcCount, inner or idxCount were changed.

Note
This function is a C++ wrapper for function SimdSynetGatherElementsInit.
Parameters
[in]dataType- a type of input and output tensor. It can be SimdTensorData32f, SimdTensorData16b or SimdTensorData8u.
[in]indexType- a type of index tensor. It can be SimdTensorData32i or SimdTensorData64i.
[in]indexConst- a flag indicating that index tensor is constant and can be set once.
[in]indexUsers- a number of consumers sharing the same constant index tensor.
[in]outer- outer shape dimensions before the gathered dimension.
[in]srcCount- a length of the gathered dimension in the input tensor.
[in]inner- a product of dimensions after the gathered dimension.
[in]idxCount- a length of the gathered dimension in the index and output tensors.

◆ Enable()

SIMD_INLINE bool Enable ( ) const

Checks that the internal gather-elements 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 gather-elements context.

Note
This function is a C++ wrapper for function SimdSynetGatherElementsInternalBufferSize.
Returns
size of internal buffer in bytes used inside gather elements algorithm.

◆ SetIndex()

SIMD_INLINE void SetIndex ( const uint8_t *  idx)

Sets and analyzes constant gather-elements indexes.

The function has an effect only when the context was created with indexConst equal to SimdTrue.

Note
This function is a C++ wrapper for function SimdSynetGatherElementsSetIndex.
Parameters
[in]idx- a pointer to INT32 or INT64 index tensor.

◆ Forward()

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

Performs gather-elements forward propagation.

The function gathers elements from src according to idx. If SetIndex() was called, the context can use the analysis results, but idx must still point to the index tensor in the current implementation. Negative indexes are interpreted relative to srcCount.

Note
This function is a C++ wrapper for function SimdSynetGatherElementsForward.
Parameters
[in]src- a pointer to input tensor.
[in]idx- a pointer to INT32 or INT64 index tensor.
[out]dst- a pointer to output tensor.

◆ Clear()

SIMD_INLINE void Clear ( )

Releases internal context and clears stored tensor parameters.