Prhub

#47868 [XPU] Fix Event init failure w/ blocking

原始 PR 作者 zhenwei-intel 合并时间 2026-07-07 22:54 文件变更 1 提交数 1 评论 1 代码增减 +7 / -1

执行摘要

修复 XPU Event 不支持 blocking 参数

修复 XPU 设备上因上游 PR #47081 引入 blocking=True 参数导致 torch.xpu.Event 抛出 TypeError: Event.__new__() got an unexpected keyword argument 'blocking' 的错误。

该 PR 值得合并,修复简单直接。建议后续跟踪上游是否会在 XPU 上支持 blocking 参数,届时可移除该 workaround。

讨论亮点

该 PR 讨论较少,仅 Claude bot 自动评论告知来自 fork 无法自动审查,PR 已被维护者 jikunshang 和 njhill 批准。

实现拆解

  1. vllm/v1/worker/xpu_model_runner.py_torch_cuda_wrapper 上下文中,将原本直接 torch.cuda.Event = partial(torch.xpu.Event) 替换为一个自定义函数 _xpu_event
  2. _xpu_event 函数签名接受任意位置参数和关键字参数,并显式吸收 blocking=None 参数,然后再调用 torch.xpu.Event(*args, **kwargs)。这样即使调用方传入了 blocking 参数,也不会报错。
  3. 添加注释说明原因,便于后续维护。
文件 模块 状态 重要度
vllm/v1/worker/xpu_model_runner.py 模型运行器 modified 6.28

关键符号

_xpu_event _torch_cuda_wrapper

关键源码片段

vllm/v1/worker/xpu_model_runner.py data-contract

核心修复文件,在 `_torch_cuda_wrapper` 中新增 `_xpu_event` 函数以忽略 `blocking` 参数。

# vllm/v1/worker/xpu_model_runner.py@contextmanager
def _torch_cuda_wrapper():
    # 将 CUDA API 替换为 XPU 等价物,每个可调用对象使用独立的
    # functools.partial,避免 Torch Dynamo 因重复注册而报错。
    torch.cuda.Stream = torch.xpu.Stream
    torch.cuda.default_stream = partial(torch.xpu.current_stream)
    torch.cuda.current_stream = partial(torch.xpu.current_stream)
    torch.cuda.stream = partial(torch.xpu.stream)
    torch.cuda.set_stream = partial(torch.xpu.set_stream)
​
    # torch.xpu.Event 不支持 `blocking` 关键字参数
    # 而 torch.cuda.Event 支持,因此使用包装函数吸收该参数。
    def _xpu_event(*args, blocking=None, **kwargs):
        # blocking 参数被静默忽略,确保调用兼容性
        return torch.xpu.Event(*args, **kwargs)
​
    torch.cuda.Event = _xpu_event
​
    if supports_xpu_graph():
        torch.cuda.graph = partial(torch.xpu.graph)
        torch.cuda.CUDAGraph = torch.xpu.XPUGraph
        torch.cuda.graph_pool_handle = partial(torch.xpu.graph_pool_handle)
    yield

评论区精华

没有提炼出高价值讨论线程

当前评论区没有形成足够清晰的争议点或结论,后续有更多讨论时会体现在这里。

风险与影响

风险极低:改动仅 7 行,且是 XPU 特定路径的兼容性修复,不影响 CUDA 或其他后端。新的 _xpu_event 函数会吸收并丢弃 blocking 参数,这意味着如果未来有其他代码依赖 blocking 的语义(如等待事件时),在 XPU 上这部分语义将被静默忽略,可能导致非阻塞行为。但目前看来 _xpu_event 仅用于事件创建,不影响等待语义。

仅影响 XPU 设备用户,修复了因上游 PR #47081 引入的阻塞 bug。其他后端不受影响。变更极小,不会带来回归风险。

静默忽略 blocking 语义

关联 Issue

未识别关联 Issue

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

完整报告

参与讨论