CANN/cann-bench GRU算子评测

CANN/cann-bench GRU算子评测 GRU 算子 API 描述【免费下载链接】cann-bench评测AI在处理CANN领域代码任务的能力涵盖算子生成、算子优化等领域支撑模型选型、训练效果评估统一量化评估标准识别Agent能力短板构建CANN领域评测平台推动AI能力在CANN领域的持续演进。项目地址: https://gitcode.com/cann/cann-bench1. 算子简介Gated Recurrent Unit 循环神经网络算子实现带门控机制的循环单元通过更新门和重置门控制信息流动支持多层堆叠、双向处理和可选偏置。主要应用场景自然语言处理中的序列建模机器翻译、文本分类语音识别中的时序特征提取时间序列预测与异常检测作为 LSTM 的轻量替代方案参数更少无细胞状态算子特征难度等级L4FusedComposite多输入x, weight_ih, weight_hh, 可选 bias_ih, bias_hh, h0双输出y, hn支持多层堆叠、双向处理、batch_first 格式、层间 Dropout2. 算子定义数学公式对于每个时间步 $t$$$ z_t \sigma(W_z x_t U_z h_{t-1} b_z) $$$$ r_t \sigma(W_r x_t U_r h_{t-1} b_r) $$$$ n_t \tanh(W_n x_t r_t \odot (U_n h_{t-1} b_n)) $$$$ h_t (1 - z_t) \odot n_t z_t \odot h_{t-1} $$其中$z_t$ 为更新门控制前一时刻隐藏状态的保留比例$r_t$ 为重置门控制前一时刻隐藏状态对候选状态的影响$n_t$ 为候选隐藏状态$h_t$ 为当前时刻的隐藏状态$\sigma$ 为 sigmoid 函数$\odot$ 为逐元素乘法3. 接口规范算子原型cann_bench.gru(Tensor x, TensorList weight_ih, TensorList weight_hh, TensorList? bias_ih, TensorList? bias_hh, Tensor? h0, int inputSize, int hiddenSize, int numLayers, bool bias, bool batchFirst, float dropout, bool bidirectional) - (Tensor y, Tensor hn)输入参数说明参数类型默认值描述xTensor必选输入序列张量shape 为 (S, B, input_size) 或 (B, S, input_size)weight_ihTensorList必选输入到隐藏层权重列表每层/每个方向独立 tensor。详见权重列表格式weight_hhTensorList必选隐藏层到隐藏层权重列表每个 tensor shape 为 (3*hiddenSize, hiddenSize)bias_ihTensorListNone输入到隐藏层偏置列表可选每个 tensor shape 为 (3*hiddenSize)bias_hhTensorListNone隐藏层到隐藏层偏置列表可选每个 tensor shape 为 (3*hiddenSize)h0TensorNone初始隐藏状态可选默认全 0shape 为 (num_layers * num_directions, B, hiddenSize)inputSizeint必选输入特征维度hiddenSizeint必选隐藏状态特征维度numLayersint1循环层数biasbooltrue是否使用偏置batchFirstboolfalse输入是否为 (B, S, input_size) 格式dropoutfloat0.0Dropout 概率bidirectionalboolfalse是否双向 GRU权重列表格式GRU 有 3 个门z, r, n每个门都需要独立的权重矩阵。权重以 TensorList 形式传入每层、每个方向为独立的 tensor。TensorList 长度计算len(weight_ih) numLayers * num_directionslen(weight_hh) numLayers * num_directionslen(bias_ih) numLayers * num_directions如有偏置len(bias_hh) numLayers * num_directions如有偏置排列顺序[weight_ih_l0, weight_ih_l0_reverse, weight_ih_l1, weight_ih_l1_reverse, ...] (bidirectionaltrue) [weight_ih_l0, weight_ih_l1, ...] (bidirectionalfalse)每个 tensor shape参数Layer 0Layer k (k0)weight_ih (单向)(3*hiddenSize, inputSize)(3*hiddenSize, hiddenSize)weight_ih (双向)(3*hiddenSize, inputSize)(3hiddenSize, 2hiddenSize)weight_hh(3*hiddenSize, hiddenSize)(3*hiddenSize, hiddenSize)bias_ih/bias_hh(3*hiddenSize)(3*hiddenSize)示例单层单向:weight_ih [tensor(3*H, inputSize)],weight_hh [tensor(3*H, H)]单层双向:weight_ih [tensor(3*H, inputSize), tensor(3*H, inputSize)](forward reverse)两层双向:weight_ih [tensor(3*H, inputSize), tensor(3*H, inputSize), tensor(3*H, 2*H), tensor(3*H, 2*H)]输出参数Shapedtype描述y(S, B, num_directions * hiddenSize) 或 (B, S, num_directions * hiddenSize)与输入 x 相同输出序列hn(num_layers * num_directions, B, hiddenSize)与输入 x 相同最终隐藏状态数据类型输入 dtype输出 dtypefloat32float32float16float16bfloat16bfloat16规则与约束所有输入 Tensor 的 dtype 必须一致GRU 有 3 个门z, r, n因此权重矩阵行数为 3*hiddenSize多层 GRU 时Layer k 的输入来自前一层的输出因此 weight_ih 的列维度需要调整当biastrue时bias_ih和bias_hh必须提供当bidirectionaltrue时num_directions2否则为 1dropout仅在numLayers 1时生效作用于层间非最后一层batchFirsttrue时输入 x 的 shape 为 (B, S, input_size)输出 y 的 shape 为 (B, S, num_directions * hiddenSize)PyTorch GRU 内部使用 float32 计算float16/bfloat16 输入会转换为 float32 后计算结果再转回原 dtype4. 精度要求采用生态算子精度标准进行验证。误差指标平均相对误差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 from typing import List, Optional, Tuple def gru( x: torch.Tensor, weight_ih: List[torch.Tensor], weight_hh: List[torch.Tensor], bias_ih: Optional[List[torch.Tensor]] None, bias_hh: Optional[List[torch.Tensor]] None, h0: Optional[torch.Tensor] None, inputSize: int 0, hiddenSize: int 0, numLayers: int 1, bias: bool True, batchFirst: bool False, dropout: float 0.0, bidirectional: bool False ) - Tuple[torch.Tensor, torch.Tensor]: num_directions 2 if bidirectional else 1 gru_layer torch.nn.GRU( input_sizeinputSize, hidden_sizehiddenSize, num_layersnumLayers, biasbias, batch_firstbatchFirst, dropoutdropout if numLayers 1 else 0.0, bidirectionalbidirectional ) input_dtype x.dtype gru_layer gru_layer.float() with torch.no_grad(): for layer in range(numLayers): getattr(gru_layer, fweight_ih_l{layer}).copy_(weight_ih[layer * num_directions].float()) getattr(gru_layer, fweight_hh_l{layer}).copy_(weight_hh[layer * num_directions].float()) if bias and bias_ih is not None: getattr(gru_layer, fbias_ih_l{layer}).copy_(bias_ih[layer * num_directions].float()) if bias and bias_hh is not None: getattr(gru_layer, fbias_hh_l{layer}).copy_(bias_hh[layer * num_directions].float()) if bidirectional: getattr(gru_layer, fweight_ih_l{layer}_reverse).copy_(weight_ih[layer * num_directions 1].float()) getattr(gru_layer, fweight_hh_l{layer}_reverse).copy_(weight_hh[layer * num_directions 1].float()) if bias and bias_ih is not None: getattr(gru_layer, fbias_ih_l{layer}_reverse).copy_(bias_ih[layer * num_directions 1].float()) if bias and bias_hh is not None: getattr(gru_layer, fbias_hh_l{layer}_reverse).copy_(bias_hh[layer * num_directions 1].float()) x_float x.float() if h0 is None: batch_size x.shape[1] if not batchFirst else x.shape[0] h0 torch.zeros(numLayers * num_directions, batch_size, hiddenSize, dtypetorch.float32) else: h0 h0.float() y, hn gru_layer(x_float, h0) return y.to(input_dtype), hn.to(input_dtype)6. 额外信息算子调用示例import torch import cann_bench # 单层单向 GRUTensorList 格式 seq_len, batch, input_size, hidden_size 20, 8, 128, 256 x torch.randn(seq_len, batch, input_size, dtypetorch.float32, devicenpu) weight_ih [torch.randn(3 * hidden_size, input_size, dtypetorch.float32, devicenpu)] weight_hh [torch.randn(3 * hidden_size, hidden_size, dtypetorch.float32, devicenpu)] bias_ih [torch.randn(3 * hidden_size, dtypetorch.float32, devicenpu)] bias_hh [torch.randn(3 * hidden_size, dtypetorch.float32, devicenpu)] y, hn cann_bench.gru(x, weight_ih, weight_hh, bias_ih, bias_hh, None, inputSizeinput_size, hiddenSizehidden_size, numLayers1, biasTrue, batchFirstFalse, dropout0.0, bidirectionalFalse) # 双向 GRU weight_ih_bi [torch.randn(3 * hidden_size, input_size, dtypetorch.float32, devicenpu), torch.randn(3 * hidden_size, input_size, dtypetorch.float32, devicenpu)] weight_hh_bi [torch.randn(3 * hidden_size, hidden_size, dtypetorch.float32, devicenpu), torch.randn(3 * hidden_size, hidden_size, dtypetorch.float32, devicenpu)] y_bi, hn_bi cann_bench.gru(x, weight_ih_bi, weight_hh_bi, None, None, None, inputSizeinput_size, hiddenSizehidden_size, numLayers1, biasFalse, batchFirstFalse, dropout0.0, bidirectionalTrue)【免费下载链接】cann-bench评测AI在处理CANN领域代码任务的能力涵盖算子生成、算子优化等领域支撑模型选型、训练效果评估统一量化评估标准识别Agent能力短板构建CANN领域评测平台推动AI能力在CANN领域的持续演进。项目地址: https://gitcode.com/cann/cann-bench创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考