CANN/cann-bench Conv2D算子

CANN/cann-bench Conv2D算子 Conv2D 算子 API 描述【免费下载链接】cann-bench评测AI在处理CANN领域代码任务的能力涵盖算子生成、算子优化等领域支撑模型选型、训练效果评估统一量化评估标准识别Agent能力短板构建CANN领域评测平台推动AI能力在CANN领域的持续演进。项目地址: https://gitcode.com/cann/cann-bench1. 算子简介计算2D卷积。主要应用场景图像分类、目标检测、语义分割等视觉任务的核心运算CNN 网络中特征提取的基础模块信号处理中的2D滤波操作算子特征难度等级L3Contraction三输入特征图、卷积核、偏置单输出支持分组卷积、膨胀卷积输入 x 为 [N, C_in, H, W]卷积核 filter 为 [C_out, C_in, K_h, K_w]2. 算子定义数学公式$$ y \text{CONV}(x, \text{filter}) \text{bias} $$即对输入特征图 $x$ 使用卷积核 $\text{filter}$ 进行2D卷积运算并加上偏置 $\text{bias}$。卷积运算支持通过 strides 控制步长、pads 控制填充、dilations 控制膨胀率、groups 控制分组数。3. 接口规范算子原型cann_bench.conv2_d(Tensor x, Tensor filter, Tensor bias, int[] strides, int[] pads, int[] dilations, int groups) - Tensor y输入参数说明参数类型默认值描述xTensor必选输入特征图shape 为 [N, C_in, H, W]filterTensor必选卷积核shape 为 [C_out, C_in, K_h, K_w]biasTensor必选偏置stridesint[]必选步长padsint[]必选填充dilationsint[][1, 1]膨胀率groupsint1分组数输出参数Shapedtype描述y由输入尺寸、卷积核、步长、填充和膨胀率决定与输入 x 相同输出特征图数据类型输入 (x, filter, bias) dtype输出 dtypefloat16float16float32float32bfloat16bfloat16规则与约束x 的 shape 格式为 [N, C_in, H, W]filter 的 shape 格式为 [C_out, C_in/groups, K_h, K_w]x、filter、bias 的 dtype 须一致strides 指定卷积的步长pads 指定四方向填充 [pad_top, pad_bottom, pad_left, pad_right]dilations 指定膨胀率默认 [1, 1]groups 指定分组数C_in 和 C_out 都须能被 groups 整除4. 精度要求采用生态算子精度标准进行验证。误差指标平均相对误差MERE采样点中相对误差平均值$$ \text{MERE} \text{avg}(\frac{\text{abs}(actual - golden)}{\text{abs}(golden)\text{1e-7}}) $$最大相对误差MARE采样点中相对误差最大值$$ \text{MARE} \max(\frac{\text{abs}(actual - golden)}{\text{abs}(golden)\text{1e-7}}) $$通过标准数据类型FLOAT16BFLOAT16FLOAT32HiFLOAT32FLOAT8 E4M3FLOAT8 E5M2通过阈值(Threshold)2^-102^-72^-132^-112^-32^-2当平均相对误差 MERE Threshold最大相对误差 MARE 10 * Threshold 时判定为通过。5. 标准 Golden 代码import torch Conv2D算子Torch Golden参考实现 计算2D卷积 公式: y CONV(x, filter) bias def conv2_d( x: torch.Tensor, filter: torch.Tensor, bias: torch.Tensor, strides: list, pads: list, dilations: list [1, 1], groups: int 1 ) - torch.Tensor: 计算2D卷积 公式: y CONV(x, filter) bias Args: x: 输入特征图 filter: 卷积核 bias: 偏置 strides: 步长 pads: 填充 [pad_top, pad_bottom, pad_left, pad_right] dilations: 膨胀率 groups: 分组数 Returns: 输出特征图 # pads 格式: [pad_top, pad_bottom, pad_left, pad_right] # PyTorch conv2d padding 格式: (left, right, top, bottom) 或 (pad_h, pad_w) 对称模式 # 检查是否对称填充 if pads[0] pads[1] and pads[2] pads[3]: # 对称模式: (pad_height, pad_width) padding (pads[0], pads[2]) else: # 非对称模式: (left, right, top, bottom) padding (pads[2], pads[3], pads[0], pads[1]) stride (strides[0], strides[1]) dilation (dilations[0], dilations[1]) y torch.nn.functional.conv2d(x, filter, bias, stridestride, paddingpadding, dilationdilation, groupsgroups) return y6. 额外信息算子调用示例import torch import cann_bench x torch.randn(1, 64, 56, 56, dtypetorch.float16, devicenpu) weight torch.randn(128, 64, 3, 3, dtypetorch.float16, devicenpu) bias torch.randn(128, dtypetorch.float16, devicenpu) y cann_bench.conv2_d(x, weight, bias, strides[1, 1], pads[1, 1, 1, 1], dilations[1, 1], groups1)【免费下载链接】cann-bench评测AI在处理CANN领域代码任务的能力涵盖算子生成、算子优化等领域支撑模型选型、训练效果评估统一量化评估标准识别Agent能力短板构建CANN领域评测平台推动AI能力在CANN领域的持续演进。项目地址: https://gitcode.com/cann/cann-bench创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考