Prhub

#29673 [AMD][diffusion] fix: disable layernorm torch.compile decorator in eager mode on ROCm to avoid memory-access fault

原始 PR 作者 sushildubey171 合并时间 2026-07-07 02:15 文件变更 1 提交数 3 评论 4 代码增减 +7 / -4

执行摘要

修复 ROCm 上 LayerNorm torch.compile 崩溃

在 8 卡 MI 系列 GPU 上以 eager 模式运行 Wan2.2-T2V-A14B 时,ScaleResidual 调制模块的 forward_native@torch.compile 装饰器未被禁用,触发 GPU 内存访问错误。该问题由 PR #25256 引入(该 PR 仅对 NPU 禁用了 compile)。

该 PR 值得快速合入,属于必要 bugfix。建议关注后续是否有更通用的方案(如全局 disable compile 开关),但当前修复已足够。

讨论亮点

无实质 review 讨论;mickqian 直接 Approved,HaiShaw 要求运行 pre-commit 以消除 lint 错误(已在第三次提交中修复)。

实现拆解

  1. 修改 layernorm.py 中的 @torch.compile 条件:将 forward_native 方法上的 torch.compile 装饰器的 disable 参数从 current_platform.is_npu() 扩展为 current_platform.is_npu() or current_platform.is_rocm()
  2. 对同类方法应用相同修复:在 ScaleResidual 类(第 572 行)、Fourier 类(第 740 行)和 TimestepEmbedBlock 类(第 837 行)的 forward_native 方法上做了同样的条件扩展,共计 4 处修改。
  3. 保持行为一致:所有修改仅影响编译行为,不影响 eager 模式下的计算逻辑,因此模型输出无变化。
文件 模块 状态 重要度
python/sglang/multimodal_gen/runtime/layers/layernorm.py 扩散模型 modified 5.77

关键符号

forward_native

关键源码片段

python/sglang/multimodal_gen/runtime/layers/layernorm.py core-logic

包含所有 `@torch.compile` 装饰器的修改,是修复核心文件。

# python/sglang/multimodal_gen/runtime/layers/layernorm.py# 第 369 行(LayerNorm 类):
@torch.compile(
    backend="inductor",
    disable=current_platform.is_npu() or current_platform.is_rocm(),
)
def forward_native(self, x, residual=None):
    # ... eager 实现 ...# 第 572 行(ScaleResidual 类):
@torch.compile(disable=current_platform.is_npu() or current_platform.is_rocm())
def forward_native(self, residual, x, gate, shift, scale):
    # ... eager 实现 ...# 第 740 行(Fourier 类):
@torch.compile(disable=current_platform.is_npu() or current_platform.is_rocm())
def forward_native(self, x, shift, scale):
    # ... eager 实现 ...# 第 837 行(TimestepEmbedBlock 类):
@torch.compile(disable=current_platform.is_npu() or current_platform.is_rocm())
def forward_native(self, x, scale, shift):
    # ... eager 实现 ...

评论区精华

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

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

风险与影响

风险极低:仅修改 torch.compile 的禁用条件,不涉及核心逻辑变更。在 ROCm 上通过 eager 模式运行可避免内存错误,但可能因禁用 compile 而损失部分性能(若未来 ROCm 支持 compile 时需重新评估)。

  • 用户:在 ROCm 上使用 Wan2.2 或其他扩散模型的用户可正常启动,不再崩溃。
  • 系统:对 CUDA/XPU/MUSA/NPU 等平台无影响,仅 ROCm 行为改变。
  • 团队:无额外维护负担,修复简单且可逆。
仅影响 ROCm 平台

关联 Issue

未识别关联 Issue

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

完整报告

参与讨论