conv2d
Description
Computes 2D convolution with the given 5HD data and FracZ weight.
The shape of the data tensor is 5HD, that is, (N, C1, H, W, C0);
The shape of the weight tensor is FracZ, that is, (C1*filter_h*filter_w, N1, N0, C0).
This API supports bias.
The shape of res_tensor is [N, N1, H_out*W_out, N0].
Prototype
conv(data, weight, para_dict, optim_dict=None, dsl_flag=True)
Parameters
- data: a 5HD Tensor of type float16, for the feature map of 2D convolution.
Atlas 200/300/500 Inference Product : supports float16.Atlas Training Series Product : supports float16. - weight: 2D convolution weight, a tensor in FracZ format.
Atlas 200/300/500 Inference Product : supports float16.Atlas Training Series Product : supports float16. - para_dict: a dictionary for the key-value pairs, including the following keys:
- pad_h: an int list, which is [pad_top, pad_bottom], indicating padding in the H direction of the feature map for 2D convolution. If padding is not involved, set each element to 0.
- pad_w: an int list, which is [pad_left, pad_right], indicating padding in the W direction of the feature map for 2D convolution. If padding is not involved, set each element to 0.
- stride_h: an int for the height stride of the feature map.
- stride_w: an int for the width stride of the feature map.
- filter_h: an int for the filter height.
- filter_w: an int for the filter width.
Currently, para_dict also supports the following optional argument:
- bias: a tensor of float16 type, equivalent to the bias in 2D convolution. If this argument is used, bias is included in the convolutional computation. Otherwise, bias is not included. In addition, Bias must be of the same size of cout.
- offset_x: an int for the negative offset added to the feature map of type int8, which is used to ensure that the output is valid. The default value is 0.
- kernel_name: a string for the kernel name of 2D convolution. The default value is conv2d.
- mad_dtype: a string for the output data type of 2D convolution. If the weight type is int4 or int8, the default value is int32. In other cases, the default value is float32.
- dilate_h: an int for the distance of each data point in the H direction in the kernel of 2D convolution. The value ranges from 1 to 255. The default value is 1, that is, each data point is next to the previous data point.
- dilate_w: an int for the distance of each data point in the W direction in the kernel of 2D convolution. The value ranges from 1 to 255. The default value is 1, that is, each data point is next to the previous data point.
- optim_dict: a dictionary for enabling optimization features.The key values are of type bool.
- True: feature enabled.
- False: feature disabled.
Defaults to optim_dict = {"c0_optim_flg": False}, indicating that the C0 = 4 feature is disabled.
Note that only c0_optim_flg is currently configurable.
- dsl_flag: a bool. If True, enables UB fusion; if False, otherwise.
Returns
res_tensor: result tensor.
Restrictions
This API cannot be used in conjunction with other TBE DSL APIs.
Applicability
Example
Take 2D convolution of the float16 type as an example. The feature map is (Batch=1, C=32, H=16, W=8), Cout is 32, and Weight is (KernelH=KernelW=2).
Then, the shape of the 5HD feature map tensor is (Batch, C/16, H, W, 16) = (1, 2, 16, 8, 16).
The shape of the FracZ weight tensor is (KernelH x KernelW x C/16, Cout/16, 16, 16) = (8, 2, 16, 16).
The shape of the Bias tensor is (Cout,) = (32,).
from tbe import tvm
from tbe import dsl
shape_in = (1, 2, 16, 8, 16)
shape_w = (8, 2, 16, 16)
pad_h = [1, 1]
pad_w = [1, 1]
stride_h = 1
stride_w = 1
dilate_h = 1
dilate_w = 1
filter_h = 2
filter_w = 2
Data = tvm.placeholder(shape_in, name='FmapW', dtype="float16")
Weight = tvm.placeholder(shape_w, name='FilterW', dtype="float16")
bias_tensor = tvm.placeholder((shape_w[1] * shape_w[2], ), name='Bias', dtype="float16")
res_tensor = dsl.conv(
Data,
Weight,
{"bias_tensor": bias_tensor,
"pad_h": pad_h, "pad_w": pad_w,
"stride_h": stride_h, "stride_w": stride_w,
"dilate_h": dilate_h, "dilate_w": dilate_w,
"filter_h": filter_h, "filter_w": filter_w,
"offset_x":0})