Simd Library Documentation.

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

The SynetInnerProduct32f class is a C++ wrapper of FP32 inner product (matrix multiplication). More...

#include <SimdSynet.hpp>

Public Member Functions

 SynetInnerProduct32f ()
 
virtual ~SynetInnerProduct32f ()
 
SIMD_INLINE void Init (size_t M, size_t N, size_t K, SimdBool transB, SimdBool constB, SimdBool bias, SimdConvolutionActivationType activation)
 
SIMD_INLINE bool Enable () const
 
SIMD_INLINE size_t InternalBufferSize () const
 
SIMD_INLINE size_t ExternalBufferSize () const
 
SIMD_INLINE void SetParams (const float *weight, SimdBool *internal, const float *bias, const float *params)
 
SIMD_INLINE void Forward (const float *A, const float *B, float *buf, float *C)
 
SIMD_INLINE void Clear ()
 

Detailed Description

The SynetInnerProduct32f class is a C++ wrapper of FP32 inner product (matrix multiplication).

The class wraps C API functions SimdSynetInnerProduct32fInit, SimdSynetInnerProduct32fInternalBufferSize, SimdSynetInnerProduct32fExternalBufferSize, SimdSynetInnerProduct32fSetParams and SimdSynetInnerProduct32fForward. It computes C = A*B, optionally adds bias and applies activation:

for(i = 0; i < M; ++i)
    for(j = 0; j < N; ++j)
    {
        sum = bias ? bias[j] : 0;
        for(k = 0; k < K; ++k)
            sum += A[i, k] * (transB ? B[j, k] : B[k, j]);
        C[i, j] = Activate(sum, activation, params);
    }

When constB is SimdTrue, matrix B must be supplied to SetParams() and can be reordered or cached inside the context. 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 M = 4, N = 8, K = 16;
    std::vector<float> A(M * K), B(K * N), C(M * N), bias(N, 0.0f);
    for (size_t i = 0; i < A.size(); ++i)
        A[i] = float(i) * 0.01f;
    for (size_t i = 0; i < B.size(); ++i)
        B[i] = float(i) * 0.02f;

    Simd::SynetInnerProduct32f innerProduct;
    innerProduct.Init(M, N, K, SimdFalse, SimdTrue, SimdTrue, SimdConvolutionActivationIdentity);
    if (innerProduct.Enable())
    {
        innerProduct.SetParams(B.data(), NULL, bias.data(), NULL);
        innerProduct.Forward(A.data(), NULL, NULL, C.data());
    }

    return 0;
}

Constructor & Destructor Documentation

◆ SynetInnerProduct32f()

Creates a new empty SynetInnerProduct32f class.

◆ ~SynetInnerProduct32f()

virtual ~SynetInnerProduct32f ( )
virtual

SynetInnerProduct32f class destructor. Releases internal context.

Member Function Documentation

◆ Init()

SIMD_INLINE void Init ( size_t  M,
size_t  N,
size_t  K,
SimdBool  transB,
SimdBool  constB,
SimdBool  bias,
SimdConvolutionActivationType  activation 
)

Initializes (or re-initializes) an FP32 inner-product context.

Creates an internal context with using of function SimdSynetInnerProduct32fInit. The context is recreated only if matrix sizes or inner-product flags were changed.

Note
This function is a C++ wrapper for function SimdSynetInnerProduct32fInit.
Parameters
[in]M- a height of A and C matrices.
[in]N- a width of B and C matrices.
[in]K- a width of A and height of B matrices.
[in]transB- a flag indicating that B is stored as N*K instead of K*N.
[in]constB- a flag indicating that matrix B is constant and can be set once.
[in]bias- a flag to add bias to output matrix C.
[in]activation- an activation function type used after inner product.

◆ Enable()

SIMD_INLINE bool Enable ( ) const

Checks that the internal inner-product context was created.

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

◆ InternalBufferSize()

SIMD_INLINE size_t InternalBufferSize ( ) const

Gets the size of internal storage used by the inner-product context.

The returned value is a number of FP32 elements.

Note
This function is a C++ wrapper for function SimdSynetInnerProduct32fInternalBufferSize.
Returns
a number of FP32 elements used by internal buffers.

◆ ExternalBufferSize()

SIMD_INLINE size_t ExternalBufferSize ( ) const

Gets the size of caller-provided temporary buffer for FP32 inner product.

The returned value is a number of FP32 elements. The current FP32 implementations do not require an external buffer and return 0, but callers can use this value when allocating the buf argument of Forward().

Note
This function is a C++ wrapper for function SimdSynetInnerProduct32fExternalBufferSize.
Returns
a number of FP32 elements required for external temporary buffer.

◆ SetParams()

SIMD_INLINE void SetParams ( const float *  weight,
SimdBool internal,
const float *  bias,
const float *  params 
)

Sets weights, bias and activation parameters for FP32 inner product.

This function must be called before Forward(). If constB was SimdTrue during initialization, weight provides matrix B and the implementation may reorder and store it internally. If internal is not NULL, SimdTrue means the weights were copied/reordered into the context; SimdFalse means the original weight pointer can be used by later forward calls and must remain valid.

Note
This function is a C++ wrapper for function SimdSynetInnerProduct32fSetParams.
Parameters
[in]weight- a pointer to FP32 matrix B weights.
[out]internal- a pointer to a flag receiving weight storage mode. Can be NULL.
[in]bias- a pointer to FP32 bias array with N elements. Can be NULL.
[in]params- a pointer to FP32 parameters of activation function (see SimdConvolutionActivationType). Can be NULL when activation does not require parameters.

◆ Forward()

SIMD_INLINE void Forward ( const float *  A,
const float *  B,
float *  buf,
float *  C 
)

Performs FP32 inner-product forward propagation.

The function computes C = A*B, optionally adds bias and applies activation stored in the context created by Init() and SetParams(). If B is constant, it can be NULL when it was set by SetParams(). The buf argument can be NULL (it causes usage of internal buffer).

Note
This function is a C++ wrapper for function SimdSynetInnerProduct32fForward.
Parameters
[in]A- a pointer to FP32 A matrix with M*K elements.
[in]B- a pointer to FP32 B matrix. Can be NULL if B is constant.
[out]buf- a pointer to external temporary FP32 buffer. Can be NULL.
[out]C- a pointer to FP32 output matrix with M*N elements.

◆ Clear()

SIMD_INLINE void Clear ( )

Releases internal context and clears stored inner-product parameters.