Rate This Document
Findability
Accuracy
Completeness
Readability

Instructions

Interface Definition

Initialize ConvolutionLayerFWD. During the construction, the tensor information of the input matrix, weight matrix, and output matrix needs to be passed. The dilates parameter is optional. If it is not passed, {0, 0, 0} will be used by default.

ConvolutionLayerFWD(const TensorInfo &src, const TensorInfo &weights, const TensorInfo &dst, const TensorInfo &bias,const Shape &strides, const Shape &dilates, const Shape &paddingL, const Shape &paddingR,ConvolutionAlgorithm alg)->void

Table 1 Input parameters of ConvolutionLayerFWD

Parameter

Data Type

conv2d Value

conv3d Value

src

KuDNN::TensorInfo

{shape{N, IC, IH, IW}, type, layout}

{shape{N, IC, ID, IH, IW}, type, layout}

weights

KuDNN::TensorInfo

{shape{OC, IC, KH, KW}, type, layout}

{shape{OC, IC, KD, KH, KW}, type, layout}

dst

KuDNN::TensorInfo

{shape{N, OC, OH, OW}, type, layout}

{shape{N, OC, OD, OH, OW}, type, layout}

bias

KuDNN::TensorInfo

{shape{OC}, type, layout}

{shape{OC},type, layout}

strides

KuDNN::Shape

(SH, SW)

(SD, SH, SW)

dilates

KuDNN::Shape

(SD, SH, SW)

(DD, DH, DW)

paddingL

KuDNN::Shape

(PL_H, PL_W)

(PL_D, PL_H, PL_W)

paddingR

KuDNN::Shape

(PR_H, PR_W)

(PR_D, PR_H, PR_W)

alg

KuDNN::ConvolutionAlgorithm

UNIMPLEMENTED/AUTO/DIRECT/WINOGRAD

Same as conv2d.

Table 2 conv2d and conv3d value description

Value

Description

N

Batch size. IC, IH, and IW indicate the number of input channels, height, and width, respectively. ID indicates the depth.

OC, OD, OH, OW

Number of output channels, depth, height, and width, respectively.

KD, KH, KW

Depth, height, and width of the convolution kernel, respectively.

SD, SH, SW

Stride of the convolution kernel in the depth, height, and width, respectively.

DD, DH, DW

Adjacent distances between sampling points along the depth, height, and width for dilated convolution, respectively.

PL_D, PL_H, PL_W

Number of specified padding elements in front of the depth, above the height, and on the left of the width, respectively.

PR_D, PR_H, PR_W

Number of specified padding elements behind the depth, below the height, and on the right of the width, respectively.

To perform operator computation, the memory addresses for storing the input and output data must be passed.

Run(const void *src, const void *wei, void *dst, const void *bia)->void

Table 3 Input parameters of Run

Parameter

Data Type

Description

src

void *

Pointer to the source memory address

wei

void *

Pointer to the weight memory address

dst

void *

Pointer to the destination memory address

bias

void *

Pointer to the bias memory address

ValidateInput verifies the input parameters of ConvolutionLayerFWD. It is automatically triggered during operator construction.

ValidateInput(const TensorInfo &src, const TensorInfo &weights, const TensorInfo &dst, const TensorInfo &bias, const Shape &strides, const Shape &paddingL, const Shape &paddingR, ConvolutionAlgorithm alg)->KuDNN::Status

Table 4 Input parameters of ValidateInput

Parameter

Data Type

Description

Value Range

src

KuDNN::TensorInfo

Source tensor information.

{shape, type, layout}

weights

KuDNN::TensorInfo

Weight tensor information.

{shape, type, layout}

dst

KuDNN::TensorInfo

Destination tensor information.

{shape, type, layout}

bias

KuDNN::TensorInfo

Bias tensor information.

{shape, type, layout}

strides

KuDNN::Shape

Stride.

(SD, SH, SW)

paddingL

KuDNN::Shape

Number of padding elements.

(PL_D, PL_H, PL_W)

paddingR

KuDNN::Shape

Number of padding elements.

(PR_D, PR_H, PR_W)

algKind

KuDNN::SoftmaxAlgorithmKind

Computation type

KuDNN::SoftmaxAlgorithmKind::SOFTMAX

Supported Data Types

  • Conv2d&3d supports the FP16 and FP32 data types. (The Shape, Type, and Layout parameters must be passed during TensorInfo object initialization. The following lists the data types supported by Type.)
    Table 5 Type supported during TensorInfo object initialization

    src

    weights

    dst

    bias

    KuDNN::Element::TypeT::F16(fp16)

    KuDNN::Element::TypeT::F16(fp16)

    KuDNN::Element::TypeT::F16(fp16)

    KuDNN::Element::TypeT::F16(fp16)

    KuDNN::Element::TypeT::F32(fp32)

    KuDNN::Element::TypeT::F32(fp32)

    KuDNN::Element::TypeT::F32(fp32)

    KuDNN::Element::TypeT::F32(fp32)

  • A maximum of 5 dimensions are supported. The supported sequential data layouts include a, ab, abc, abcd, and abcde.

    They correspond to KuDNN::Layout::A, KuDNN::Layout::AB, KuDNN::Layout::ABC, KuDNN::Layout::ABCD, and KuDNN::Layout::ABCDE, respectively.

    Table 6 Layout supported during TensorInfo object initialization

    Dimension

    src

    wei

    dst

    bias

    4D

    abcd/abdc

    abcd/abdc

    abcd/abdc

    abcd/abdc

    5D

    abcde/abced

    abcde/abced

    abcde/abced

    abcde/abced

Examples

Convolution of four-dimensional FP32 data:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using SizeType = KuDNN::SizeType;
using Shape = KuDNN::Shape;
using Type KuDNN::Element::TypeT
SizeType N = 4, IC = 4, IH = 128, IW = 100;
SizeType OC = 5, OH = 0, OW = 0;
SizeType KH = 3, KW = 3;
Shape strides(1, 1), dilates(1, 1), paddingL(1, 1), paddingR(1, 1);
OH = (IH + paddingL[0] + paddingR[0] - 1 - (KH - 1)*(dilates[0] + 1)) / strides[0] + 1;
OW = (IW + paddingL[1] + paddingR[1] - 1 - (KW - 1)*(dilates[1] + 1)) / strides[1] + 1;
Shape srcShape(N, IC, IH, IW), weiShape(OC, IC, KH, KW), dstShape(N, OC, OH, OW);KuDNN::ConvolutionAlgorithm alg(KuDNN::ConvolutionAlgorithm::AUTO);
// Tensor initialization
const KuDNN::TensorInfo srcTensor = {srcShape, Type::F32, KuDNN::Layout::ABCD};
const KuDNN::TensorInfo weightsTensor = {weiShape, Type::F32, KuDNN::Layout::ABCD};
const KuDNN::TensorInfo dstTensor = {dstShape, Type::F32, KuDNN::Layout::ABCD};
const KuDNN::TensorInfo biasTensor = {{OC}, Type::F32, KuDNN::Layout::A};
// Construct the operator: KuDNN::ConvolutionLayerFWD convFwdLayer(srcTensor, weightsTensor, dstTensor, biasTensor, strides, dilates
paddingL, paddingR, alg);
SizeType srcSize = N * IC * IH * IW;
SizeType dstSize = N * OC * OH * OW;
SizeType weiSize = OC * IC * KH * KW;
SizeType biaSize = OC;
// Allocate memory.
float *src = (float *)malloc(srcSize * sizeof(float));
float *dst = (float *)malloc(dstSize * sizeof(float));
float *dstRef = (float *)malloc(dstSize * sizeof(float));
float *wei = (float *)malloc(weiSize * sizeof(float));
float *bia = (float *)malloc(biaSize * sizeof(float));
// Run the operator.
convFwdLayer.Run(src, wei, dst, bia);