Pull Request已成功合入, 合并人@ascend-robot
(感谢 ascend-robot 的贡献)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
/approveor/lgtm- Commenting
/approveimplies 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. 👍


ascend docs pipeline is running...


变更摘要
此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_roundtrip和test_opaque_nested_tuple_result_roundtrip,覆盖run_and_save_rng_state往返与嵌套元组结果的 opaque 往返场景。


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


代码审查
审查结束总结
审查覆盖文件
| 文件 | 审查结果 |
|---|---|
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 问题,增加显式的空元组检查以避免非预期崩溃。
⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。


| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | 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 | ✅ | >>> |
- compile、compile_inductor、compile_torchair : 运行流水线
- retry : 重试流水线所有失败子任务
- retry <任务名> : 仅重试指定失败子任务
- stop : 停止流水线


/approve


1. Origin pull request:
https://gitcode.com/Ascend/pytorch/merge_requests/38749
2. Original pull request related issue(s):
https://gitcode.com/Ascend/pytorch/issues/1978
3. Original pull request related commit(s):
Co-authored-by: Cursor cursoragent@cursor.com