已合并
[sync] PR-38749: Fix mfusion FX-to-torch-mlir conversion for torch.ops.higher_order.run_and_save_rng_state #40153
[sync] PR-38749: Fix mfusion FX-to-torch-mlir conversion for torch.ops.higher_order.run_and_save_rng_state #40153
已合并
ascend-robot创建于 7月6日
ascend-robot
ascend-robot成员
7月6日

1. Origin pull request:

https://gitcode.com/Ascend/pytorch/merge_requests/38749

https://gitcode.com/Ascend/pytorch/issues/1978

Sha Datetime Message
81993d3a 2026-06-22 10:30:38 +0800 CST Fix mfusion FX-to-torch-mlir conversion for torch.ops.higher_order.run_and_save_rng_state

Co-authored-by: Cursor cursoragent@cursor.com
likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 ascend-robot 的贡献)
ascend-robotascend-robot成员
7月6日 创建了 pull request,commit 6e773e20
ascend-robotascend-robot成员
7月6日 关联了issue:[Feature]: 26.1.0版本图模式功能增强
ascend-robotascend-robot成员
7月6日 添加了label:ascend-cla/yes
ascend-robot
ascend-robot成员
7月6日 评论:

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

ascend-ds-bot, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
ascend-robotascend-robot成员
7月6日 添加了label:ci-pipeline-running
ascend-robot
ascend-robot成员
7月6日 评论:

ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
7月6日 添加了label:docs-ci-pipeline-running
atomgit-bot
atomgit-bot
7月6日 评论:

变更摘要

此PR修复了mfusion FX-to-torch-mlir转换中 torch.ops.higher_order.run_and_save_rng_state 等不支持的 HigherOrderOperator 无法正确往返(roundtrip)的问题。核心思路是:在导入时对不支持的算子通过 _TORCH_MLIR_SUPPORTED_HOPS 白名单进行过滤,将其包裹为 opaque custom op 并通过 result_paths 机制保留原始嵌套元组结果结构;在导出时根据 result_paths 恢复嵌套的 operator.getitem 调用链,从而完整还原原始计算图。

主要改动

  • 新增 HigherOrderOperator 白名单过滤:在 fx_importer.py 中引入 _TORCH_MLIR_SUPPORTED_HOPS(仅含 auto_functionalized),修改 _is_supported_call_function_target 使不支持的 HigherOrderOperator(如 run_and_save_rng_state)经由 opaque 路径处理,而非直接传递给 torch-mlir。
  • 支持嵌套元组结果的 opaque 往返:在 Payload 中新增 result_paths 字段记录每个叶子结果在原始元组树中的路径;导入端通过 _flatten_result_meta_tree 展平嵌套结果并生成多输出的 schema,导出端通过 _meta_value_at_path 和路径驱动方式重建 operator.getitem 调用链。
  • 新增 opaque 多结果用户重写逻辑:在 fx_importer.py 中新增 _rewrite_opaque_result_users 函数,递归地将原节点的 operator.getitem 用户重新关联到平坦化后的叶节点上,确保嵌套解构语义不丢失。
  • 新增相关单元测试:在 test_fx_mlir_converter.py 中新增 test_opaque_run_and_save_rng_state_roundtriptest_opaque_nested_tuple_result_roundtrip,覆盖 run_and_save_rng_state 往返与嵌套元组结果的 opaque 往返场景。
likedislike
ascend-robot
ascend-robot成员
7月6日 评论:

✅ 跳过 docs ci 检查,没有需要检查的文档文件

likedislike
ascend-robotascend-robot成员
7月6日 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
7月6日 添加了label:docs-ci-pipeline-success
atomgit-bot
atomgit-bot
7月6日 评论:

代码审查

审查结束总结

审查覆盖文件

文件 审查结果
test/_inductor/test_fx_mlir_converter.py ✅ 无问题 — 测试代码正确,import 补充完整,FakeTensorMode 用法正确,断言逻辑合理
torch_npu/_inductor/mfusion/fx_mlir_converter/fx_exporter.py ✅ 无问题 — _meta_value_at_path 实现正确,_process_opaque_operator 的 result_paths 重构逻辑与 importer 一致
torch_npu/_inductor/mfusion/fx_mlir_converter/fx_importer.py ⚠️ 发现 2 个问题(见下表)
torch_npu/_inductor/mfusion/fx_mlir_converter/opaque_registry.py ✅ 无问题 — result_paths 字段正确添加,默认值 () 为不可变类型,无副作用

发现的问题

优先级 数量 说明
P0 0
P1 0
P2 1 空元组返回值导致 _wrap_unsupported_call_function_targets 中 IndexError 崩溃
P3 1 _rewrite_opaque_result_users 未处理 output 节点直接引用多结果 opaque 节点的情况(make_fx 路径不受影响,仅手工构造图可能触发)

整体风险评估

低风险。此 diff 的核心逻辑(将非白名单 HigherOrderOperator 包装为 opaque custom op,支持嵌套元组返回值的 roundtrip)设计正确,importer 与 exporter 双向一致,测试覆盖了 run_and_save_rng_state 和嵌套元组两种场景。发现的两个问题均属于边界情况(空元组返回值和 output 节点直接引用),在实际使用中触发概率较低。建议在合入前至少修复 P2 的 IndexError 问题,增加显式的空元组检查以避免非预期崩溃。

⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。

likedislike
ascend-robotascend-robot成员
7月6日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
7月6日 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
7月6日 评论:
流水线 PR-pipeline_pytorch#41420 [ commitID:b7bdf208 ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 Build_X86 >>>
Build_ARM >>>
Build_LibTorch_x86 >>>
Build_LibTorch_ARM >>>
Build_X86_torchair 🛑 >>>
Build_ARM_torchair 🛑 >>>
patch_test 🛑 >>>
恶意代码检查 Antipoison >>>
编码安全与规范检查 CodeCheck >>>
check_error >>>
CodeCheck_lintrunner >>>
开源片段检查 SCA >>>
开发者测试 UT_X86_Part_01 🛑 >>>
UT_X86_Part_02 🛑 >>>
UT_ARM_A3_Part_01 🛑 >>>
UT_ARM_A3_Part_02 🛑 >>>
UT_ARM_A2_Part_01 >>>
UT_ARM_A2_Part_02 >>>
UT_ARM_A2_Part_03 >>>
UT_inductor_Part_01 >>>
UT_inductor_Part_02 >>>
UT_inductor_Part_03 >>>
UT_inductor_Part_04 >>>
UT_DIST_ARM_Part_01 🛑 >>>
UT_DIST_ARM_Part_02 🛑 >>>
UT_DIST_ARM_Part_03 🛑 >>>
UT_DIST_ARM_Part_04 🛑 >>>
UT_ARM_A2_Select_Part_01 >>>
UT_ARM_A2_Select_Part_02 >>>
流水线 PR-pipeline_pytorch >>>
此流水线已支持下列评论快捷指令,仅PR创建者和白名单成员[wujinyuan1, huangjingwei, liangsongwei, yashi999, culechan, Dring, wuyouqi1, L1919_snow, qq_52711437, WhiteNight12, nomiz, xiu_21, ffmh, wanglijun55, hss-shuai, husichao, smallsilly, lanshaozuishuai, jimmyisme1, lzy0920232, alpha-junh, Sunshine_Youngster, wei_zhuoyi, zhangyihuiben, zyw-hw, zzzkeke, rmch, yangch0324, LucciC, AACAES, renyujin, wjlflyer, senzhen-town, pengjingyou, qsc97, limuan, yule100, xiaoqi-zhou, kuhn7, chenxingying, hanye02, zichun_ye, anyrenwei, kkjocker, wangzili121, Lu_G, yvjc, puddingfjz, HandsoemLemon, bigprestigee1, huawuyi, zhenyu10, dairenjie, du-jin-hang, zou-jieyu, adelaideliu, TrHan, wanlinan, Windwindzzz, pengqihw, kisnwang, yuheng_wang, honghao_wang, jizewei, zhangguoguang, sunyu-xuan, chenrayray, hbhu_bin, liujunzhu, c_34, LiNuoh, maoyuanpeng1, zzhongmin, zhaoyu65, bellatan, jiabaolin, zhuofanshen, wencaiwen, lu_zhuge, caoshuyang, molly12, lyx324521, LQ1206, gitcode-chenjiao, cai-weiwei1989, CHDong, ogqin, yuanlipingGit, xuqinglin1, lqz2, zouwei1, chaoluoa, paradox325, jackzhang1116, yaoyao, akh, yujiacheng, dengjie0116, Hubert11111, Shine_Ws, wslhj555, longqiand, OYtao666, JiaqingQiang, luyyyy, Kingbelial, zhanghaiyu0101, wenxp1018, yanliu-luoluo, ksun_sekiro, liyong328, wgzheng, tangky, vivi_is_coding, aoiaoisola, weixin_44494597, wangmengmengwang65667, hid57809721, qq_35468730, comeonup, C547032, gcw_m5OQChA4, yao_yao_ling_xian, cnnbwcy, szqfes_12, cora_19, cann_lilin, can, shawnylee233, fanglanyue0916]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
hbhu_bin成员
7月7日 评论:

/lgtm

likedislike
zichun_ye
zichun_ye成员
7月8日 评论:

/approve

likedislike
ascend-robotascend-robot成员
7月8日 添加了label:approvedlgtm
ascend-robotascend-robot成员
7月8日 合入了pull request