Prhub

#27646 [Intel GPU]Add sycl mrope pass for xpu device

原始 PR 作者 gaopengff 合并时间 2026-06-11 12:25 文件变更 1 提交数 3 评论 3 代码增减 +24 / -2

执行摘要

XPU 设备 mrope 改用 sycl 内核性能提升

PR body 中明确:'Improve mrope performance for xpu device'。性能对比数据显示 triton 内核在 XPU 上较慢,sycl 内核在 num_tokens 1024-8192 时加速比 115%-167%。

建议精读,性能优化思路清晰。可关注 gemini-code-assist 提出的 cos_sin_cache 类型匹配问题,未来可能需修复。

讨论亮点

gemini-code-assist[bot] 建议在调用 sycl 内核前添加 self._match_cos_sin_cache_dtype(query) 以确保 cos_sin_cache 与 query 的 device 和 dtype 一致,否则可能引发运行时错误。该建议未被采纳,PR 中未加入此调用。

实现拆解

  1. 新增 XPU 检测与内核导入:在文件顶部导入 is_xpu 并定义 _is_xpu 标记;当 _is_xpu 为 True 时,从 sgl_kernel 加载 multimodal_rotary_embedding sycl 内核。
  2. 修改 forward_xpu 方法:将原先的 triton 调用路径 self.forward_triton(positions, query, key) 替换为直接调用 multimodal_rotary_embedding,并传入 cos_sin_cache、mrope_section、head_size、rotary_dim、mrope_interleaved、mrope_interleaved_glm、is_neox_style、axis_map 等参数,调用后直接返回修改后的 query 和 key 原地张量。
  3. 保留 fallback 路径:若 positions.ndim != 2 或 self.mrope_section 为空,仍然回退到 forward_native 实现。
文件 模块 状态 重要度
python/sglang/srt/layers/rotary_embedding/mrope.py 旋转编码 modified 6.39

关键符号

forward_xpu

关键源码片段

python/sglang/srt/layers/rotary_embedding/mrope.py dependency-wiring

唯一修改文件,新增 XPU sycl 内核调用路径,替换原有 triton 内核。

# python/sglang/srt/layers/rotary_embedding/mrope.py (partial)
​
from sglang.srt.utils import (
    cpu_has_amx_support,
    is_cuda,
    is_npu,
    is_xpu, # 新增导入
    support_triton,
)
​
_is_cuda = is_cuda()
_is_npu = is_npu()
_is_xpu = is_xpu() # 新增 XPU 检测标识
_is_cpu_amx_available = cpu_has_amx_support()
​
if _is_xpu:
    from sgl_kernel import multimodal_rotary_embedding # 导入 sycl 内核
​
# ... ( 其他导入和内核定义 )
​
# forward_xpu 方法的核心变更:
def forward_xpu(self, positions, query, key, fused_set_kv_buffer_arg=None):
    assert positions.ndim in (1, 2)
    if positions.ndim == 2 and self.mrope_section:
        # 原先调用 self.forward_triton(positions, query, key)
        # 现改为直接调用 sycl 内核
        multimodal_rotary_embedding(
            query,
            key,
            self.cos_sin_cache,
            positions,
            self.mrope_section,
            self.head_size,
            self.rotary_dim,
            self.mrope_interleaved,
            self.mrope_interleaved_glm,
            self.is_neox_style,
            self.axis_map,
        )
        return query, key
    return self.forward_native(positions, query, key, fused_set_kv_buffer_arg)

评论区精华

cos_sin_cache 类型匹配建议 正确性

gemini-code-assist[bot] 在 review 评论中建议在调用 sycl 内核前先调用 self._match_cos_sin_cache_dtype(query),以确保 cos_sin_cache 与 query 的 device 和 dtype 一致。

结论:未采纳该建议,当前 PR 未添加该调用。 · unresolved

风险与影响

若 cos_sin_cache 与 query 的 device/dtype 不匹配,sycl 内核可能崩溃或产生错误结果。当前实现依赖调用方保证一致性,缺少显式校验。仅修改 XPU 分支,不影响 CUDA/NPU 路径。

仅影响 Intel XPU 设备上的 mrope 计算,性能提升显著(15%-167%)。代码改动量小(+24/-2),影响范围仅限于 mrope.py 中的 forward_xpu 方法。

缺少类型检查

关联 Issue

未识别关联 Issue

当前没有检测到明确关联的 Issue 链接,后续同步到相关引用后会出现在这里。

完整报告

参与讨论