已合并
fix(dvm): guard stride patch behind is_fx_dynamic in fallback kernel. #37883
fix(dvm): guard stride patch behind is_fx_dynamic in fallback kernel. #37883
已合并
Margaret_wangrui创建于 6月8日
Margaret_wangrui
Margaret_wangrui成员
6月8日

【合入来源】

说明:修复 DVM 图融合在动态 shape 场景下 codegen 失败的问题。动态子图中 Inductor buffer stride 为符号表达式,仍调用 patch_gm_placeholder_strides_from_codegen_args 会在 torch.empty_strided 处触发 TypeError(stride 含 Mul 等非 int 元素)。

【修改方案】

  1. 问题根因_dvm_generate_fallback_kernel 在生成 dvm::fused_graph_* fallback kernel 时,无条件调用 patch_gm_placeholder_strides_from_codegen_args,将 Inductor buffer 的真实 stride 写回 fused subgraph placeholder 的 meta["val"]。静态 shape 下该逻辑用于选择 k.view_load 路径;动态 shape 下 stride 为 SymInt/符号表达式,无法构造合法 fake tensor,导致 codegen 异常。

  2. 修改内容:在 torch_npu/_inductor/dvm/graph_fusion.py_dvm_generate_fallback_kernel 中,引入 is_fx_dynamic(来自 graph_build.py),仅当 fused subgraph 非动态 shape 时才执行 stride patch:

    if not is_fx_dynamic(meta.gm):
        patch_gm_placeholder_strides_from_codegen_args(meta.gm, args_list)
    
  3. 行为说明

    • 静态 shape:行为不变,继续 patch stride,支持 padding row stride 等非 contiguous 场景的 view_load 优化。
    • 动态 shape:跳过 patch,由 DvmCodegenInterpreterdyn_shape=Truecodegen_maybe_view_load 的 symbolic 分支,使用 k.load 并在必要时由 .contiguous() 保证输入合法。
  4. 涉及组件:Inductor PythonWrapperCodegen.generate_fallback_kernel(DVM patch 点)→ graph_fusion._dvm_generate_fallback_kernelutil.patch_gm_placeholder_strides_from_codegen_args / graph_build.DvmCodegenInterpreter

【资料变更】

不涉及

【接口变更】

不涉及

本次修改仅影响 DVM 图融合内部 codegen 逻辑,不涉及对外 Python/C++ API 及跨仓接口变更。

【功能验证】

测试场景

场景 预期
静态 shape + 非 contiguous stride(padding row stride 等) 仍 patch stride,生成 view_load,精度/行为与改前一致
动态 shape(含 SymInt/SymFloat 的 fused subgraph) codegen 不再因 empty_strided 报错,可正常生成 DVM kernel
动态 shape 非 contiguous 输入 k.load + .contiguous(),功能正确

验证方法

  1. 复现动态 shape DVM 融合模型(此前在 _dvm_generate_fallback_kernel / util.pyempty_strided stride 类型错误),确认 torch.compile(..., backend="inductor") 可完成 codegen 并正确执行。
  2. 回归静态 shape 用例(如非 contiguous view_load 相关场景),确认融合结果与 golden 一致。
  3. 可选:检查生成 wrapper 代码,动态场景下 placeholder 不再调用 stride patch,静态场景仍保留 patch 逻辑。

【CheckList】

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 Margaret_wangrui 的贡献)
Margaret_wangruiMargaret_wangrui成员
6月8日 创建了 pull request,commit 2f8750df
ascend-robotascend-robot成员
6月8日 添加了label:ascend-cla/yes
ascend-robot
ascend-robot成员
6月8日 评论:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here
You can get sig-info at here


PR Approval Progress

Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
test zichun_ye, hbhu_bin (2/2) zichun_ye (1/1)
torch_npu/_inductor zichun_ye, hbhu_bin (2/2) zichun_ye (1/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

Margaret_wangrui, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
Margaret_wangrui
Margaret_wangrui成员
6月8日 评论:

/retest

likedislike
此处折叠了57条消息 查看更多
ascend-robotascend-robot成员
6月16日 添加了label:approved
hbhu_bin成员
6月16日 评论:

/lgtm

likedislike
ascend-robotascend-robot成员
6月16日 添加了label:lgtm
ascend-robotascend-robot成员
6月16日 合入了pull request
ascend-robot
ascend-robot成员
6月16日 评论:
流水线 pytorch_gitcode_PR_multiVersion#10741 [ commitID:7f6533ea ] 已完成
likedislike