The SynetConvolution32f class is a C++ wrapper of FP32 convolution. More...
#include <SimdSynet.hpp>
Public Member Functions | |
| SynetConvolution32f () | |
| virtual | ~SynetConvolution32f () |
| SIMD_INLINE void | Init (size_t batch, const SimdConvolutionParameters *conv) |
| SIMD_INLINE bool | Enable () const |
| SIMD_INLINE size_t | ExternalBufferSize () const |
| SIMD_INLINE size_t | InternalBufferSize () const |
| SIMD_INLINE const char * | Info () const |
| SIMD_INLINE void | SetParams (const float *weight, SimdBool *internal, const float *bias, const float *params) |
| SIMD_INLINE void | Forward (const float *src, float *buf, float *dst) |
| SIMD_INLINE void | Clear () |
Detailed Description
The SynetConvolution32f class is a C++ wrapper of FP32 convolution.
The class wraps C API functions SimdSynetConvolution32fInit, SimdSynetConvolution32fExternalBufferSize, SimdSynetConvolution32fInternalBufferSize, SimdSynetConvolution32fInfo, SimdSynetConvolution32fSetParams and SimdSynetConvolution32fForward. It convolves each image in the batch, optionally adds bias and applies activation:
sum = bias == NULL ? 0 : bias[dc];
for(sc = 0; sc < srcC/group; ++sc)
for(ky = 0; ky < kernelY; ++ky)
for(kx = 0; kx < kernelX; ++kx)
sum += src[inputOffset] * weight[weightOffset];
dst[outputOffset] = Activate(sum, activation, params);
The exact offsets depend on tensor format, padding, dilation, stride and group. The current implementation supports FP32 source and destination tensors with matching NCHW or NHWC format. The destination spatial size must match convolution parameters:
dstH = (srcH + padY + padH - (dilationY*(kernelY - 1) + 1)) / strideY + 1 dstW = (srcW + padX + padW - (dilationX*(kernelX - 1) + 1)) / strideX + 1
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, srcC = 4, srcH = 8, srcW = 8, dstC = 8;
SimdConvolutionParameters conv = {};
conv.srcC = srcC;
conv.srcH = srcH;
conv.srcW = srcW;
conv.srcT = SimdTensorData32f;
conv.srcF = SimdTensorFormatNhwc;
conv.dstC = dstC;
conv.kernelY = 3;
conv.kernelX = 3;
conv.dilationY = 1;
conv.dilationX = 1;
conv.strideY = 1;
conv.strideX = 1;
conv.padY = 1;
conv.padX = 1;
conv.padH = 1;
conv.padW = 1;
conv.group = 1;
conv.activation = SimdConvolutionActivationIdentity;
conv.dstH = (conv.srcH + conv.padY + conv.padH - (conv.dilationY * (conv.kernelY - 1) + 1)) / conv.strideY + 1;
conv.dstW = (conv.srcW + conv.padX + conv.padW - (conv.dilationX * (conv.kernelX - 1) + 1)) / conv.strideX + 1;
conv.dstT = SimdTensorData32f;
conv.dstF = SimdTensorFormatNhwc;
std::vector<float> src(batch * srcH * srcW * srcC);
std::vector<float> weight(conv.kernelY * conv.kernelX * srcC * dstC / conv.group);
std::vector<float> bias(dstC, 0.0f);
std::vector<float> dst(batch * conv.dstH * conv.dstW * dstC, 0.0f);
for (size_t i = 0; i < src.size(); ++i)
src[i] = float(i) * 0.01f;
for (size_t i = 0; i < weight.size(); ++i)
weight[i] = float(i) * 0.02f;
Simd::SynetConvolution32f convolution;
convolution.Init(batch, &conv);
if (convolution.Enable())
{
convolution.SetParams(weight.data(), NULL, bias.data(), NULL);
convolution.Forward(src.data(), NULL, dst.data());
}
return 0;
}
Constructor & Destructor Documentation
◆ SynetConvolution32f()
Creates a new empty SynetConvolution32f class.
◆ ~SynetConvolution32f()
|
virtual |
SynetConvolution32f class destructor. Releases internal context.
Member Function Documentation
◆ Init()
| SIMD_INLINE void Init | ( | size_t | batch, |
| const SimdConvolutionParameters * | conv | ||
| ) |
Initializes (or re-initializes) an FP32 convolution context.
Creates an internal context with using of function SimdSynetConvolution32fInit. The context is recreated only if batch size or convolution parameters were changed.
- Note
- This function is a C++ wrapper for function SimdSynetConvolution32fInit.
- Parameters
-
[in] batch - a batch size. [in] conv - a pointer to convolution parameters. Source and destination tensor types must be FP32.
◆ Enable()
| SIMD_INLINE bool Enable | ( | ) | const |
Checks that the internal convolution context was created.
- Returns
- true if the context exists and Forward() can be called.
◆ ExternalBufferSize()
| SIMD_INLINE size_t ExternalBufferSize | ( | ) | const |
Gets the size of caller-provided temporary buffer for FP32 convolution.
The returned value is a number of FP32 elements. It depends on the implementation selected during initialization and can be used when allocating the buf argument of Forward(). Some implementations return 1 when they do not need external temporary storage.
- Note
- This function is a C++ wrapper for function SimdSynetConvolution32fExternalBufferSize.
- Returns
- a number of FP32 elements required for external temporary buffer.
◆ InternalBufferSize()
| SIMD_INLINE size_t InternalBufferSize | ( | ) | const |
Gets the size of internal storage used by the convolution context.
The returned value is a number of FP32 elements. It reports internal temporary buffers and implementation-specific reordered weights, bias or activation parameters already allocated by the context.
- Note
- This function is a C++ wrapper for function SimdSynetConvolution32fInternalBufferSize.
- Returns
- a number of FP32 elements used by internal buffers.
◆ Info()
| SIMD_INLINE const char * Info | ( | ) | const |
Gets a short description of the selected FP32 convolution implementation.
The returned string contains the implementation extension and algorithm name, for example a direct, depthwise, Winograd, NHWC direct or GEMM-based variant. The returned pointer is owned by the context and remains valid until the next call of this function or until the context is released.
- Note
- This function is a C++ wrapper for function SimdSynetConvolution32fInfo.
- Returns
- a string with description of internal implementation. NULL if the context was not created.
◆ SetParams()
| SIMD_INLINE void SetParams | ( | const float * | weight, |
| SimdBool * | internal, | ||
| const float * | bias, | ||
| const float * | params | ||
| ) |
Sets weights, bias and activation parameters for FP32 convolution.
This function must be called before Forward(). The weight array contains FP32 convolution weights with kernelY*kernelX*srcC*dstC/group elements. Depending on the selected implementation, weights can be used directly or transformed and stored inside the context. 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 SimdSynetConvolution32fSetParams.
- Parameters
-
[in] weight - a pointer to FP32 convolution weights. [out] internal - a pointer to a flag receiving weight storage mode. Can be NULL. [in] bias - a pointer to FP32 bias array with dstC 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 * | src, |
| float * | buf, | ||
| float * | dst | ||
| ) |
Performs FP32 convolution forward propagation.
The function convolves each image in the batch, adds bias when it was set, and applies the activation stored in the context created by Init() and SetParams(). The buf argument can be NULL (it causes usage of internal buffer).
- Note
- This function is a C++ wrapper for function SimdSynetConvolution32fForward.
- Parameters
-
[in] src - a pointer to FP32 input tensor. [out] buf - a pointer to external temporary FP32 buffer. Can be NULL. [out] dst - a pointer to FP32 output tensor.
◆ Clear()
| SIMD_INLINE void Clear | ( | ) |
Releases internal context and clears stored convolution parameters.