使用说明
接口定义
初始化ConvolutionLayerFWD。构造时需要传入输入矩阵、权重矩阵、输出矩阵的tensor信息,其中dilates可以不送,会默认使用{0, 0, 0}。
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
参数名称 |
数据类型 |
conv2d取值 |
conv3d取值 |
|---|---|---|---|
src |
KDNN::TensorInfo |
{shape{N, IC, IH, IW}, type, layout} |
{shape{N, IC, ID, IH, IW}, type, layout} |
weights |
KDNN::TensorInfo |
{shape{OC, IC, KH, KW}, type, layout} |
{shape{OC, IC, KD, KH, KW}, type, layout} |
dst |
KDNN::TensorInfo |
{shape{N, OC, OH, OW}, type, layout} |
{shape{N, OC, OD, OH, OW}, type, layout} |
bias |
KDNN::TensorInfo |
{shape{OC}, type, layout} |
{shape{OC},type, layout} |
strides |
KDNN::Shape |
(SH, SW) |
(SD, SH, SW) |
dilates |
KDNN::Shape |
(SD, SH, SW) |
(DD, DH, DW) |
paddingL |
KDNN::Shape |
(PL_H, PL_W) |
(PL_D, PL_H, PL_W) |
paddingR |
KDNN::Shape |
(PR_H, PR_W) |
(PR_D, PR_H, PR_W) |
alg |
KDNN::ConvolutionAlgorithm |
UNIMPLEMENTED/AUTO/DIRECT/WINOGRAD |
同conv2d取值 |
取值 |
描述 |
|---|---|
N |
批大小batchSize。IC、IH和IW分别表示输入的通道数、高度和宽度。ID表示深度。 |
OC、OD、OH、OW |
分别表示输出的通道数、深度、高度和宽度。 |
KD、KH、KW |
分别表示卷积kernel的深度、高度和宽度。 |
SD、SH、SW |
分别表示在卷积核在输入深度、高度和宽度上的移动步长。 |
DD、DH、DW |
分别表示空洞卷积在输入深度、高度和宽度对应点的相邻距离。 |
PL_D、PL_H、PL_W |
分别表示在深度前方、高度上方和宽度左边的填充指定个数。 |
PR_D、PR_H、PR_W |
分别表示在深度后方、高度下方和宽度右边的填充指定个数。 |
执行算子运算,要传入存放输入的内存地址和输出的内存地址。
Run(const void *src, const void *wei, void *dst, const void *bia)->void
参数名称 |
数据类型 |
描述 |
|---|---|---|
src |
void * |
指向src的内存地址 |
wei |
void * |
指向wei的内存地址 |
dst |
void * |
指向dst的内存地址 |
bias |
void * |
指向bias的内存地址 |
ValidateInput负责验证ConvolutionLayerFWD输入参数。在算子构造过程中自动触发执行。
ValidateInput(const TensorInfo &src, const TensorInfo &weights, const TensorInfo &dst, const TensorInfo &bias, const Shape &strides, const Shape &paddingL, const Shape &paddingR, ConvolutionAlgorithm alg)->KDNN::Status
参数名称 |
数据类型 |
描述 |
取值范围 |
|---|---|---|---|
src |
KDNN::TensorInfo |
src张量信息 |
{shape,type,layout} |
weights |
KDNN::TensorInfo |
wei张量信息 |
{shape,type,layout} |
dst |
KDNN::TensorInfo |
dst张量信息 |
{shape,type,layout} |
bias |
KDNN::TensorInfo |
bias张量信息 |
{shape,type,layout} |
strides |
KDNN::Shape |
移动步长 |
(SD, SH, SW) |
paddingL |
KDNN::Shape |
填充指定个数 |
(PL_D, PL_H, PL_W) |
paddingR |
KDNN::Shape |
填充指定个数 |
(PR_D, PR_H, PR_W) |
algKind |
KDNN::SoftmaxAlgorithmKind |
计算类型 |
KDNN::SoftmaxAlgorithmKind::SOFTMAX |
支持数据类型
- Conv2D&3D支持fp16/fp32数据类型(TensorInfo对象初始化时需传入Shape、Type、Layout参数,此处列出为Type支持数据类型)
表5 TensorInfo对象初始化时支持的Type类型 src
weights
dst
bias
KDNN::Element::TypeT::F16(fp16)
KDNN::Element::TypeT::F16(fp16)
KDNN::Element::TypeT::F16(fp16)
KDNN::Element::TypeT::F16(fp16)
KDNN::Element::TypeT::F32(fp32)
KDNN::Element::TypeT::F32(fp32)
KDNN::Element::TypeT::F32(fp32)
KDNN::Element::TypeT::F32(fp32)
- 最高支持5Dtensor,支持顺序数据排布:a、ab、abc、abcd、abcde。
对应KDNN::Layout::A、KDNN::Layout::AB、KDNN::Layout::ABC、KDNN::Layout::ABCD、KDNN::Layout::ABCDE。
表6 TensorInfo对象初始化时支持的Layout类型 dimension
src
wei
dst
bias
4D
abcd/abdc
abcd/abdc
abcd/abdc
abcd/abdc
5D
abcde/abced
abcde/abced
abcde/abced
abcde/abced
使用示例
四维fp32数据类型的卷积操作。
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 30 31 | using SizeType = KDNN::SizeType; using Shape = KDNN::Shape; using Type KDNN::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); KDNN::ConvolutionAlgorithm alg(KDNN::ConvolutionAlgorithm::AUTO); // Tensor初始化 const KDNN::TensorInfo srcTensor = {srcShape, Type::F32, KDNN::Layout::ABCD}; const KDNN::TensorInfo weightsTensor = {weiShape, Type::F32, KDNN::Layout::ABCD}; const KDNN::TensorInfo dstTensor = {dstShape, Type::F32, KDNN::Layout::ABCD}; const KDNN::TensorInfo biasTensor = {{OC}, Type::F32, KDNN::Layout::A}; // 构造算子 KDNN::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; // 申请内存 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)); // 执行算子计算 convFwdLayer.Run(src, wei, dst, bia); |