The SynetDeconvolution16b class is a C++ wrapper of BF16/FP32 deconvolution (transposed convolution). More...
#include <SimdSynet.hpp>
Public Member Functions | |
| SynetDeconvolution16b () | |
| virtual | ~SynetDeconvolution16b () |
| SIMD_INLINE void | Init (size_t batch, const SimdConvolutionParameters *conv, SimdSynetCompatibilityType compatibility=SimdSynetCompatibilityDefault) |
| 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, const float *bias, const float *params) |
| SIMD_INLINE void | Forward (const uint8_t *src, uint8_t *buf, uint8_t *dst) |
| SIMD_INLINE void | Clear () |
Detailed Description
The SynetDeconvolution16b class is a C++ wrapper of BF16/FP32 deconvolution (transposed convolution).
The class wraps C API functions SimdSynetDeconvolution16bInit, SimdSynetDeconvolution16bExternalBufferSize, SimdSynetDeconvolution16bInternalBufferSize, SimdSynetDeconvolution16bInfo, SimdSynetDeconvolution16bSetParams and SimdSynetDeconvolution16bForward. It applies transposed convolution to each image in the batch, optionally adds bias and applies activation. Source and destination tensors can be FP32 or BF16:
dst[:] = 0;
for(sc = 0; sc < srcC/group; ++sc)
for(sy = 0; sy < srcH; ++sy)
for(sx = 0; sx < srcW; ++sx)
for(ky = 0; ky < kernelY; ++ky)
for(kx = 0; kx < kernelX; ++kx)
dst[outputOffset] += inputValue * weightValue;
value = Activate(dst[outputOffset] + bias[dc], activation, params);
dst[outputOffset] = dstT == SimdTensorData16b ? Float32ToBFloat16(value) : value;
The input value is read as BF16 or converted from FP32 to BF16 according to srcT. The weight value comes from the internal representation prepared by SetParams().
The exact offsets depend on tensor format, padding, dilation, stride and group. The current implementation supports FP32 or BF16 source and destination tensors with matching NCHW format, or matching NHWC format when group is 1. The destination spatial size must match deconvolution parameters:
dstH = strideY*(srcH - 1) + dilationY*(kernelY - 1) + 1 - padY - padH dstW = strideX*(srcW - 1) + dilationX*(kernelX - 1) + 1 - padX - padW
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 = 3, srcW = 3, dstC = 4;
SimdConvolutionParameters conv = {};
conv.srcC = srcC;
conv.srcH = srcH;
conv.srcW = srcW;
conv.srcT = SimdTensorData32f;
conv.srcF = SimdTensorFormatNhwc;
conv.dstC = dstC;
conv.kernelY = 2;
conv.kernelX = 2;
conv.dilationY = 1;
conv.dilationX = 1;
conv.strideY = 2;
conv.strideX = 2;
conv.padY = 0;
conv.padX = 0;
conv.padH = 0;
conv.padW = 0;
conv.group = 1;
conv.activation = SimdConvolutionActivationIdentity;
conv.dstH = conv.strideY * (conv.srcH - 1) + conv.dilationY * (conv.kernelY - 1) + 1 - conv.padY - conv.padH;
conv.dstW = conv.strideX * (conv.srcW - 1) + conv.dilationX * (conv.kernelX - 1) + 1 - conv.padX - conv.padW;
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::SynetDeconvolution16b deconvolution;
deconvolution.Init(batch, &conv);
if (deconvolution.Enable())
{
deconvolution.SetParams(weight.data(), bias.data(), NULL);
deconvolution.Forward((const uint8_t*)src.data(), NULL, (uint8_t*)dst.data());
}
return 0;
}
Constructor & Destructor Documentation
◆ SynetDeconvolution16b()
Creates a new empty SynetDeconvolution16b class.
◆ ~SynetDeconvolution16b()
|
virtual |
SynetDeconvolution16b class destructor. Releases internal context.
Member Function Documentation
◆ Init()
| SIMD_INLINE void Init | ( | size_t | batch, |
| const SimdConvolutionParameters * | conv, | ||
| SimdSynetCompatibilityType | compatibility = SimdSynetCompatibilityDefault |
||
| ) |
Initializes (or re-initializes) a BF16/FP32 deconvolution context.
Creates an internal context with using of function SimdSynetDeconvolution16bInit. The context is recreated only if batch size, deconvolution parameters or compatibility flags were changed.
- Note
- This function is a C++ wrapper for function SimdSynetDeconvolution16bInit.
- Parameters
-
[in] batch - a batch size. [in] conv - a pointer to deconvolution parameters. Source and destination tensor types must be FP32 or BF16. [in] compatibility - calculation compatibility flags.
◆ Enable()
| SIMD_INLINE bool Enable | ( | ) | const |
Checks that the internal deconvolution context was created.
- Returns
- true if the context exists and Forward() can be called.
◆ ExternalBufferSize()
| SIMD_INLINE size_t ExternalBufferSize | ( | ) | const |
Gets the size in bytes of caller-provided temporary buffer for BF16 deconvolution.
The returned value is a number of bytes. It depends on the implementation selected during initialization and can be used when allocating the buf argument of Forward(). Some implementations return 1 or 0 when they do not need external temporary storage.
- Note
- This function is a C++ wrapper for function SimdSynetDeconvolution16bExternalBufferSize.
- Returns
- a number of bytes required for external temporary buffer.
◆ InternalBufferSize()
| SIMD_INLINE size_t InternalBufferSize | ( | ) | const |
Gets the size in bytes of internal storage used by the deconvolution context.
The returned value reports internal storage tracked by the selected implementation, including internal temporary buffers, transformed weights, copied bias and copied activation parameters.
- Note
- This function is a C++ wrapper for function SimdSynetDeconvolution16bInternalBufferSize.
- Returns
- a number of bytes used by internal buffers.
◆ Info()
| SIMD_INLINE const char * Info | ( | ) | const |
Gets a short description of the selected BF16 deconvolution implementation.
The returned string contains the implementation extension and algorithm name, for example a GEMM or NHWC GEMM 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 SimdSynetDeconvolution16bInfo.
- Returns
- a string with description of internal implementation. NULL if the context was not created.
◆ SetParams()
| SIMD_INLINE void SetParams | ( | const float * | weight, |
| const float * | bias, | ||
| const float * | params | ||
| ) |
Sets weights, bias and activation parameters for BF16 deconvolution.
This function must be called before Forward(). The weight array contains FP32 deconvolution weights with kernelY*kernelX*srcC*dstC/group elements. The selected implementation transforms weights to its internal BF16/reordered representation. Bias is copied to an internal FP32 array; when bias is NULL, zeros are used. Activation parameters are copied or expanded to the internal FP32 array according to SimdConvolutionActivationType.
- Note
- This function is a C++ wrapper for function SimdSynetDeconvolution16bSetParams.
- Parameters
-
[in] weight - a pointer to FP32 deconvolution weights. [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 uint8_t * | src, |
| uint8_t * | buf, | ||
| uint8_t * | dst | ||
| ) |
Performs BF16/FP32 deconvolution forward propagation.
The function converts FP32 input to BF16 when the context source type is FP32, uses BF16 input directly when the source type is BF16, accumulates transposed convolution sums in FP32, adds bias, applies activation and writes FP32 or BF16 output according to the context destination type. The buf argument can be NULL (it causes usage of internal buffer).
- Note
- This function is a C++ wrapper for function SimdSynetDeconvolution16bForward.
- Parameters
-
[in] src - a pointer to input tensor. Actual element type is defined by srcT in deconvolution parameters. [out] buf - a pointer to external temporary byte buffer. Can be NULL. [out] dst - a pointer to output tensor. Actual element type is defined by dstT in deconvolution parameters.
◆ Clear()
| SIMD_INLINE void Clear | ( | ) |
Releases internal context and clears stored deconvolution parameters.