vllm-ascend Triton RoPE 内核详解:_triton_rope 的旋转位置编码实现与 FP8 E4M3 变体

原创2026-10-14 10:14:061,331 阅读
文章标签:人工智能大模型模型推理服务Ascend

vllm-ascend Triton RoPE 内核详解:_triton_rope 的旋转位置编码实现与 FP8 E4M3 变体

本篇技术指南以 vllm_ascend/ops/triton/docs/rope.md 为骨架,系统讲解 vllm-ascend 中基于 Triton 的旋转位置编码(RoPE)内核 _triton_rope 及其 FP8 E4M3 输出变体 _triton_rope_fp8。你将掌握该内核的旋转公式、双旋转表模式(cos_sin_cache + positions 与预选 cos/sin)、NeoX / GPT-J 两种布局、头部分块(head tiling)与 UB 容量约束、持久化网格的行步进调度,以及面向 MiniMax-M3 稀疏索引路径的固定缩放 E4M3 量化输出;文中所有结论均以仓库源码与测试用例为依据,可对照 rope.py 逐行验证。

一、内核定位与调用链

_triton_rope 是 vllm-ascend 为昇腾 NPU 实现的 Triton RoPE 内核,宿主包装函数为 rope_forward_triton(定义于 vllm_ascend/ops/triton/rope.py)。它在一个内核中**就地(in place)**同时对 Q 和 K 张量施加旋转位置编码,具备以下核心能力:

  • 支持部分旋转(rope_dim != head_dim,即 rotary_dim < head_size 时尾部直通);
  • 同时支持 NeoX 风格(半拆分)与 GPT-J(交错)两种旋转布局;
  • 提供两种旋转表供给方式:预选好的 cos/sin 对,或原始 cos_sin_cache 加 positions 索引;
  • 作为 rope_forward_oot(vllm_ascend/ops/rotary_embedding.py)中 torch_npu.npu_mrope 的替代实现,在 Triton 可用时被调度。

从调用链看,rope_forward_oot 被注册为自定义算子 npu_rotary_embedding(见 register_custom_ops.py 中 direct_register_custom_op(op_name="npu_rotary_embedding", op_func=rope_forward_oot, ...)),上层 AscendRotaryEmbedding.forward_oot(vllm_ascend/ops/rotary_embedding.py)经由 torch.ops.vllm.npu_rotary_embedding 触发。rope_forward_oot 内部逻辑为(vllm_ascend/ops/rotary_embedding.py):

  • out_dtype == torch.float8_e4m3fn 且 key is None 时直接抛 ValueError;
  • offsets is not None 时抛 NotImplementedError(NPU 暂不支持 batched rotary);
  • HAS_TRITON 为真时,将 Q/K reshape 为 [num_tokens, -1, head_size] 后调用 rope_forward_triton;否则回退到 torch_npu.npu_mrope(rotary_mode 为 "half" 或 "interleaved")。

支持的硬件模式:Atlas A2、Atlas A3 与 950PR&950DT Products。

二、旋转公式与数据布局

对于每一行 token m、每个注意力头以及每个头的前 rope_dim 个元素(剩余 head_dim - rope_dim 个元素保持不动),内核按如下公式计算:

if IS_NEOX_STYLE:  x1 = x[..., : rope_dim // 2],  x2 = x[..., rope_dim // 2 : rope_dim]
else:              x1 = x[..., 0 : rope_dim : 2], x2 = x[..., 1 : rope_dim : 2]

o1 = x1 * cos(m) - x2 * sin(m)
o2 = x2 * cos(m) + x1 * sin(m)

if IS_NEOX_STYLE:  out = cat((o1, o2), dim=-1)
else:              out = stack((o1, o2), dim=-1).flatten(-2)

其中 cos(m)、sin(m) 是位置 m 的 rope_dim / 2 个旋转系数,以 fp32 加载并完成全部旋转运算,写回时再转换回输入数据类型。

两种布局的差别在于 x1/x2 的切分方式:

  • NeoX 风格(IS_NEOX_STYLE=True):按维度中点把 rope_dim 切成前后两半,即 x[..., :rope_dim//2] 与 x[..., rope_dim//2:rope_dim],旋转结果拼接回原顺序;
  • GPT-J 风格(IS_NEOX_STYLE=False):按奇偶索引交错切分,即 x[..., ::2] 与 x[..., 1::2],旋转结果经 tl.split / tl.join 重新交错后再 flatten(-2) 写回。

源码层面,内核 docstring 与实现位于 vllm_ascend/ops/triton/rope.py,测试中的 PyTorch 原生参照 rotate_neox / rotate_gptj(test_rope.py)与上述公式一一对应。

三、算法流程(Host 侧与 Kernel 侧)

内核按 token 行独立、逐行处理,整体流程如下:

Host 侧(rope_forward_triton)

  1. 张量预处理:Q/K 已连续时直接使用,否则创建连续副本;随后从 q 读取 num_tokens, n_q_head, head_dim,从 k 读取 n_kv_head。
  2. 头部块大小选择:BLOCK_SIZE_HEAD 在 NeoX 风格下取 64,否则取 32;当 head_dim >= 256 时进一步收窄到 16,以避免 A2/A3 上 UB 溢出。
    • 需要说明的是,当前源码已经将这一“硬编码阈值”演进为基于 UB 的动态计算:_compute_rope_block_size_head(rope.py)依据 get_ub_size_bytes() 返回的 UB 大小、活张量数量(NeoX 为 8、非 NeoX 为 10,见 _ROPE_NUM_LIVE_TENSORS_NEOX / _ROPE_NUM_LIVE_TENSORS_NON_NEOX)与安全系数 _ROPE_UB_SAFETY_FACTOR = 0.5 计算最大块,再以默认上限 64/32 封顶。文档中“clamped to 16”对应的是极端大 head_dim 场景下的最终收窄结果。
  3. 旋转维度补齐:pad_rope_dim = next_power_of_2(rope_dim)。
  4. 网格发射:持久化网格 n_row = min(num_tokens, get_vectorcore_num()) 个 program,其中 get_vectorcore_num() 来自 triton_utils.py 的设备属性探测。
  5. 旋转表模式选择:
    • cos_sin_cache + positions(USE_COS_SIN=True);
    • 或已按位置选好的 cos/sin 对(USE_COS_SIN=False,当 rope_dim == -1 时由 rope_dim = cos.shape[-1] * 2 推断)。
    • 两者都不传时抛 ValueError(见 rope.py 的分支逻辑)。

Kernel 侧(_triton_rope)

  1. 行步进:每个 program 以 for row_idx in tl.range(pid, num_tokens, num_programs) 步进 token 行,因此固定网格可以覆盖任意 num_tokens(这是 kernel 对 graph-mode 友好的关键:网格只依赖向量核数与张量形状,不依赖运行时张量值)。
  2. 旋转系数加载:将当前行的系数加载并转为 fp32。
    • USE_COS_SIN=True 时,pos_idx = positions[row_idx] 索引 cos_sin_cache,其每行按 [cos(0 : rope_dim // 2), sin(rope_dim // 2 : rope_dim)] 排布;cos_mask = arange(0, pad_rope_dim // 2) < rope_dim // 2 用于屏蔽补齐(padding)通道。
    • USE_COS_SIN=False 时,直接用 row_idx 索引 cos/sin。
  3. Q 头分块旋转:以 BLOCK_SIZE_HEAD 为块大小循环处理 Q 头。
    • NeoX 风格:分别以偏移 0 与 rope_dim // 2 加载 [BLOCK_SIZE_HEAD, pad_rope_dim // 2] 两半,旋转后写回两半;
    • GPT-J 风格:加载 [BLOCK_SIZE_HEAD, pad_rope_dim // 2, 2] 的成对布局,tl.split 拆分、旋转、tl.join 重新交错后存储;
    • 头掩码与旋转通道掩码使尾部和补齐通道保持不活跃。
  4. K 头分块旋转:对 K 头(n_kh)重复同样的分块循环,然后推进到下一行。

四、完整参数表

[!NOTE] 所有参数均为必需参数。

参数 输入/输出/属性 说明 数据类型 数据格式
q_ptr Input / Output (in place) Query 张量 [num_tokens, n_qh, hd],连续;就地旋转 fp16 / bf16 / fp32 ND
q_row_stride Input (attribute) q 的行步长(q.stride(0),即 n_qh * hd) int32 scalar
k_ptr Input / Output (in place) Key 张量 [num_tokens, n_kh, hd],连续;就地旋转 同 q_ptr ND
k_row_stride Input (attribute) k 的行步长(k.stride(0),即 n_kh * hd) int32 scalar
cos_ptr Input 已按位置选好的余弦表 [num_tokens, rope_dim // 2];USE_COS_SIN=True 时为 None fp16 / bf16 / fp32 ND
cos_row_stride Input (attribute) cos 的行步长;USE_COS_SIN=True 时为 None int32 scalar
sin_ptr Input 已按位置选好的正弦表 [num_tokens, rope_dim // 2];USE_COS_SIN=True 时为 None fp16 / bf16 / fp32 ND
sin_row_stride Input (attribute) sin 的行步长;USE_COS_SIN=True 时为 None int32 scalar
cos_sin_ptr Input 原始旋转缓存 [max_position_embeddings, rope_dim],每行为 [cos, sin];USE_COS_SIN=False 时为 None fp16 / bf16 / fp32 ND
cos_sin_row_stride Input (attribute) cos_sin_cache 的行步长(rope_dim);USE_COS_SIN=False 时为 None int32 scalar
pos_ptr Input token 位置 [num_tokens],用于索引 cos_sin_cache;USE_COS_SIN=False 时为 None int64 ND
num_tokens Input (attribute) 要处理的 token 行数 int32 scalar
n_qh Input (attribute) Query 头数,编译期 constexpr int32 scalar
n_kh Input (attribute) Key/Value 头数,编译期 constexpr int32 scalar
hd Input (attribute) 头维度 head_size,编译期 constexpr int32 scalar
rope_dim Input (attribute) 旋转维度 rotary_dim,编译期 constexpr int32 scalar
pad_rope_dim Input (attribute) next_power_of_2(rope_dim),编译期 constexpr int32 scalar
BLOCK_SIZE_HEAD Input (attribute) 每块头数,编译期 constexpr(NeoX 为 64 / GPT-J 为 32,hd >= 256 时收窄为 16) int32 scalar
IS_NEOX_STYLE Input (attribute) True 为半拆分(NeoX)旋转,False 为交错(GPT-J)旋转,编译期 constexpr bool scalar
USE_COS_SIN Input (attribute) True 读取 cos_sin_ptr + pos_ptr,False 读取 cos_ptr/sin_ptr,编译期 constexpr bool scalar

参数语义补充(对照源码)

  • q.stride(0) / k.stride(0) 在连续张量下即 n_qh * hd / n_kh * hd,wrapper 直接以 q.stride(0) 传参(rope.py),与文档描述一致。
  • 在 USE_COS_SIN=True 分支中,positions.shape[0] == num_tokens 与 rope_dim <= head_dim 均通过 assert 强校验;USE_COS_SIN=False 分支校验 cos.shape[0] == num_tokens and sin.shape[0] == num_tokens。

五、使用约束

  • q 和 k 必须为 3 维 [num_tokens, num_heads, head_dim]。已连续的输入就地修改;非连续输入时 wrapper 会创建连续副本并旋转副本,调用方原视图不变,必须使用返回值。
  • rope_dim <= head_dim(由 wrapper 断言)且 rope_dim 必须为偶数;当 rope_dim < head_dim 时,每个头的尾部 head_dim - rope_dim 个元素直通不变。
  • 两种旋转表模式必须且只能提供一种:cos_sin_cache 与 positions 一起(要求 positions.shape[0] == num_tokens),或 cos 与 sin 一起(要求 cos.shape[0] == sin.shape[0] == num_tokens);否则 wrapper 抛 ValueError。
  • 使用 cos/sin 时,每行持有 rope_dim // 2 个系数(不得复制到 rope_dim 宽);传 rope_dim = -1 会让 wrapper 推断 rope_dim = cos.shape[-1] * 2。使用 cos_sin_cache 时,每行持有 rope_dim 个值、按 [cos | sin] 排布,且 rope_dim 必须为 2 的幂:内核从 pad_rope_dim // 2(而非 rope_dim // 2)开始加载正弦部分,其中 pad_rope_dim = next_power_of_2(rope_dim)。
  • positions 的取值必须落在 [0, cos_sin_cache.shape[0]) 内,按 int64 加载。
  • [BLOCK_SIZE_HEAD, pad_rope_dim] 的 tile(GPT-J 成对布局需要双倍大小)必须能放进 UB。这正是 head_dim >= 256 时把 BLOCK_SIZE_HEAD 收窄到 16 的动机;很大的 head_dim 搭配过大的 BLOCK_SIZE_HEAD 会溢出 UB。
  • 旋转在 fp32 中完成、存储时转回输入 dtype;q 与 k 必须共享同一 dtype 与头维度。
  • num_tokens 是动态的(网格以向量核数为上限、内核在行间步进),因此该内核在 graph 模式下友好:发射网格不依赖运行时张量值,内核内部也不发生 host 同步。

UB 容量机制的源码级解读

UB 容量约束是昇腾移植中最关键的工程点之一。_compute_rope_block_size_head 注释(rope.py)记录了一个真实排障案例:在 Ascend 910B 上,按旧估算(4 个活张量)设置 BLOCK_SIZE_HEAD=64、head_dim=128 时,编译器报 "ub overflow, requires 1839104 bits while 1572864 bits available!";将 NeoX 热循环的峰值活张量数修正为 8(含编译器为 cos/sin 广播物化的张量)后,BLOCK_SIZE_HEAD 降至 32 即可正常编译运行。非 NeoX 路径因 3 维成对布局峰值活张量为 10。UB 大小由 triton_utils.py 从设备属性自动探测(候选键如 ub_size、max_ub_size 等),缺失时回退到 DEFAULT_UB_SIZE_BYTES = 192 * 1024(覆盖 910B/A3),并支持环境变量 VLLM_ASCEND_ROPE_UB_SIZE_KB 覆盖(定义见 envs.py)。

六、FP8 E4M3 输出变体(_triton_rope_fp8)

功能定位

当 rope_forward_triton 以 out_dtype=torch.float8_e4m3fn 调用时,会分派到 _triton_rope_fp8(rope.py)。该变体对 bf16 的 Q/K 施加 NeoX 风格 RoPE,并直接写出固定缩放的 E4M3 输出,避免在 MiniMax-M3 稀疏索引路径中产生中间旋转 bf16 张量。对部分旋转场景,非旋转尾部也走同一 FP8 转换。

调度链为:MiniMax-M3 模型中 indexer.index_cache.dtype == torch.float8_e4m3fn 时,self.rotary_emb(positions, index_q, index_k, out_dtype=torch.float8_e4m3fn)(见 [vllm_ascend/models/minimax_m3/minimax_m3.py#L455-L461))→ AscendRotaryEmbedding.forward_oot → npu_rotary_embedding → rope_forward_oot 中 Triton 分支 → rope_forward_triton。

公式

设 half = rope_dim // 2:

x1 = x[..., :half]
x2 = x[..., half:rope_dim]
r1 = x1 * cos(m) - x2 * sin(m)
r2 = x2 * cos(m) + x1 * sin(m)

quant_fp8(v) = cast_float8_e4m3fn(clamp(v, -448, 448))
out[..., :rope_dim] = quant_fp8(cat((r1, r2), dim=-1))
out[..., rope_dim:] = quant_fp8(x[..., rope_dim:])

旋转运算在 fp32 中完成后才进行固定缩放 E4M3 转换。源码中 _store_fp8_e4m3 辅助函数(rope.py)执行 tl.minimum(tl.maximum(vals_fp32, -FP8_MAX), FP8_MAX) 后按目标元素类型存储,FP8_MAX = _FP8_E4M3_MAX = 448.0。

算法流程

  1. Ascend 的 rope_forward_oot 调度器在 out_dtype=torch.float8_e4m3fn 时选择 Triton 路径;rope_forward_triton 要求必须同时提供 cos_sin_cache、positions 和显式 rope_dim,随后 _rope_forward_triton_fp8(rope.py)将 Q/K 置为连续,并校验或分配连续的 E4M3 输出缓冲区(调用方未提供 q_out/k_out 时用 torch.empty_like(..., dtype=torch.float8_e4m3fn) 分配)。
  2. Host 计算 pass_dim = head_dim - rope_dim,将旋转半区与直通尾部各自独立补齐到 2 的幂(pad_half = next_power_of_2(rope_dim // 2)、pad_pass = next_power_of_2(pass_dim),尾部为空时 pad_pass = 1);Q/K 头块取 min(next_power_of_2(num_heads), 16)(_fp8_rope_head_block)。
  3. 发射 min(num_tokens, max(get_vectorcore_num() * 8, 256)) 个持久化 program;每个 program 按发射 program 数在 token 行间步进,以在 950PR&950DT Products 上暴露足够行级并行度。
  4. 每行内核以 positions[row_idx] 索引 cos_sin_cache,加载余弦与正弦两半并转 fp32。
  5. 以 BLOCK_QH 块处理 Q 头:fp32 旋转、结果裁剪到 [-448, 448]、直接以 E4M3 存储;非旋转尾部同样裁剪并转换。
  6. 以 BLOCK_KH 块对 K 重复,然后推进到下一 token 行。

FP8 变体参数表

参数 输入/输出/属性 说明 数据类型 数据格式
q_ptr Input Query 张量 [num_tokens, n_qh, hd],连续 bf16 ND
q_row_stride Input (attribute) q 的行步长(q.stride(0),通常为 n_qh * hd) int32 scalar
k_ptr Input Key 张量 [num_tokens, n_kh, hd],连续 bf16 ND
k_row_stride Input (attribute) k 的行步长(k.stride(0),通常为 n_kh * hd) int32 scalar
q_out_ptr Output 旋转并量化后的 Query 张量 [num_tokens, n_qh, hd],连续 float8_e4m3fn ND
q_out_row_stride Input (attribute) q_out 的行步长(q_out.stride(0)) int32 scalar
k_out_ptr Output 旋转并量化后的 Key 张量 [num_tokens, n_kh, hd],连续 float8_e4m3fn ND
k_out_row_stride Input (attribute) k_out 的行步长(k_out.stride(0)) int32 scalar
cos_sin_ptr Input 原始旋转缓存 [max_position_embeddings, rope_dim];每行为 [cos, sin] bf16 ND
cos_sin_row_stride Input (attribute) cos_sin_cache 的行步长 int32 scalar
pos_ptr Input token 位置 [num_tokens],用于索引 cos_sin_cache int64 ND
num_tokens Input (attribute) 要处理的 token 行数 int32 scalar
n_qh Input (attribute) Query 头数,编译期 constexpr int32 scalar
n_kh Input (attribute) Key 头数,编译期 constexpr int32 scalar
hd Input (attribute) 头维度,编译期 constexpr int32 scalar
rope_dim Input (attribute) 不大于 hd 的偶数旋转维度,编译期 constexpr int32 scalar
pad_half Input (attribute) next_power_of_2(rope_dim // 2),编译期 constexpr int32 scalar
pad_pass Input (attribute) 尾部非空时为 next_power_of_2(pass_dim),否则为 1;编译期 constexpr int32 scalar
pass_dim Input (attribute) 非旋转尾部宽度 hd - rope_dim,编译期 constexpr int32 scalar
BLOCK_QH Input (attribute) Query 头块 min(next_power_of_2(n_qh), 16),编译期 constexpr int32 scalar
BLOCK_KH Input (attribute) Key 头块 min(next_power_of_2(n_kh), 16),编译期 constexpr int32 scalar
FP8_MAX Input (attribute) E4M3 有限范围裁剪上界,固定为 448.0,编译期 constexpr fp32 scalar

FP8 变体约束

  • FP8 路径只能通过 rope_forward_triton(..., out_dtype=torch.float8_e4m3fn) 显式选择;其他非 None 输出 dtype 一律被拒绝(抛 NotImplementedError)。
  • Q 和 K 必须是具有相同 num_tokens 与 head_dim 的 3 维张量;已验证的模型路径与测试覆盖均使用 bf16 输入。
  • 输出是 out-of-place 的。调用方提供的 q_out、k_out 必须是形状匹配 Q/K 的连续 torch.float8_e4m3fn 张量,否则由 wrapper 分配。
  • 仅支持 NeoX 风格半拆分旋转;传 is_neox_style=False 抛 NotImplementedError。
  • FP8 路径必须使用 cos_sin_cache + positions;预选 cos/sin 模式不受支持。
  • rope_dim 必须显式给出、为偶数、大于零且不大于 head_dim;非旋转尾部仍会被裁剪并转换为 E4M3。
  • 每个位置必须落在 [0, cos_sin_cache.shape[0]);缓存每行包含 rope_dim // 2 个余弦值后接同样数量的正弦值。
  • 转换使用固定缩放 1.0:数值被裁剪到 [-448, 448] 后直接转为 torch.float8_e4m3fn,不计算也不返回动态缩放因子。
  • Q/K 头块上限为 16 以约束 UB 用量;掩码抑制补齐的头与补齐的旋转/直通通道。
  • 发射网格只依赖张量形状与向量核数,内核内部无数据相关的 host 同步,因此兼容 MiniMax-M3 使用的图捕获(graph-capture)路径。

[!NOTE] FP8 路径的支持硬件为 950PR&950DT Products;Atlas A2 与 Atlas A3 不支持该 FP8 执行路径。测试 test_rotary_embedding_triton_kernel_fp8 中也以 AscendDeviceType.A5 判定并 pytest.skip 非 A5 设备(见 test_rope.py)。

七、起源与差异

  • 起源:为 vllm-ascend 开发的 vLLM RotaryEmbedding.forward / rotary_embedding 自定义算子的 Triton 实现;在 Triton 可用时于 rope_forward_oot 中替换 torch_npu.npu_mrope。
  • 与原生路径的差异:
    • 面向 NPU 的性能适配:使用 min(num_tokens, get_vectorcore_num()) 个 program 的持久化网格加内部行步进循环,而非每 token 一个 program;通过 BLOCK_SIZE_HEAD 做头分块(head_dim >= 256 时降为 16)保证每个加载/存储块都在 UB 内;Q 与 K 在同一内核中旋转并就地写入,省去原生路径的额外分配。
    • 针对 vllm-ascend 特定逻辑/输入的改造:同时接受 rope_forward_oot 使用的 cos_sin_cache + positions 形式与融合注意力路径使用的预选 cos/sin 形式(USE_COS_SIN 开关);支持 rope_dim != head_dim 的部分旋转与直通尾部;通过 IS_NEOX_STYLE 在单内核中同时处理 NeoX 与 GPT-J 两种布局。

FP8 变体的起源与差异

  • 起源:由 _triton_rope 派生,用于以原生 E4M3 索引缓存支撑 MiniMax-M3 稀疏索引路径。
  • 差异:
    • 面向 NPU 的性能适配:将 RoPE 与固定缩放 E4M3 转换融合进输出存储;使用上限为 16 的独立 Q/K 头块;在 950PR&950DT Products 上发射最多 max(get_vectorcore_num() * 8, 256) 个 program 以提升行级并行。
    • 针对 vllm-ascend 特定逻辑/输入的改造:写出独立的 FP8 输出而非就地更新 bf16 Q/K;仅接受 cos_sin_cache + positions;仅支持 NeoX 布局;将旋转值与部分 RoPE 尾部裁剪到 E4M3 有限范围;使用固定量化缩放 1.0。

八、测试用例与精度验证

单算子精度测试统一放在 tests/e2e/nightly/single_node/ops/singlecard_ops/triton 下,文件为 test_rope.py。

常规路径

test_rotary_embedding_triton_kernel(预选 cos/sin)与 test_rotary_embedding_triton_kernel_with_cos_sin_cache(cos_sin_cache + positions,max_position_embeddings=262144)均以 PyTorch 原生实现作为参照(_rope_pytorch_native,test_rope.py)。形状覆盖 NPU 上服务模型的实际配置:

  • (head_size, rotary_dim):(128, 128)(全旋转,如 Qwen/Llama 类模型)、(64, 32)(部分旋转);
  • (num_q_heads, num_k_heads):(64, 1) 与 (96, 8)(GQA/MQA);
  • num_tokens:1, 4, 8, 16, 1024(覆盖 decode 到 prefill);
  • is_neox_style 两种取值;dtype 为 bf16 / fp16。

精度比较使用该逐元素 fp32 累加算子的统一容差:atol=rtol=1e-3(torch.testing.assert_close)。

运行命令:

pytest -sv tests/e2e/nightly/single_node/ops/singlecard_ops/triton/test_rope.py -k "not siso"

FP8 E4M3 变体

test_rotary_embedding_triton_kernel_fp8 将 rope_forward_triton(..., out_dtype=torch.float8_e4m3fn) 与“PyTorch 原生 fp32 累加参照 + 裁剪 + E4M3 转换”的结果对比(_rope_fp8_pytorch_native,test_rope.py)。覆盖 (num_tokens, num_q_heads, num_k_heads, head_size, rotary_dim) 组合:

  • (1, 2, 1, 128, 128):单 token decode、全旋转;
  • (17, 8, 1, 128, 64):部分旋转;
  • (1024, 8, 1, 128, 128):多行 prefill;
  • 另有 (17, 8, 1, 128, 96)(非 2 的幂旋转维度,触发 sin-offset 修复)与 (17, 8, 1, 256, 192)(大 head_dim 配合非 2 的幂旋转维度,同时覆盖 sin-offset 与 UB 动态分块)两个 pytest.param 用例(见 FP8_ROPE_CASES,test_rope.py)。

位置 0 的用例会注入超出 E4M3 有限范围的值,验证饱和到 +448 与 -448(断言 actual_query[0, 0, 0].to(torch.float32) == FP8_E4M3_MAX)。精度比较先将实际与参照的 E4M3 张量转 fp32,再使用 atol=rtol=0.125。

运行命令:

pytest -sv tests/e2e/nightly/single_node/ops/singlecard_ops/triton/test_rope.py -k "fp8"

[!NOTE] FP8 用例仅在 Ascend A5(950PR&950DT Products)上运行,其他设备被 pytest.skip 跳过。

九、延伸阅读

登录后查看全文
vllm-ascend