Pull Request已成功合入, 合并人@ascend-robot
(感谢 Xuan Peng 的贡献)变更摘要
本次 PR 对 torch_npu 的 Inductor 后端进行了大规模重构,核心目标是简化代码结构、消除与上游 PyTorch 的耦合冲突、并建立更清晰的设备分发机制。主要涉及以下几个方面:引入基于 lowering_patch 的设备 lowering 分发器,使 NPU 算子注册不再覆盖社区 handler;重构 AlgorithmSelectorCache 的 monkey-patch 机制,使其返回 (node, choice) 元组并按设备类型正确派发;移除 NPUTritonKernelWithLoop 与 inductor_ascend_linear_mode 配置项,统一走 index-first fallback 路径;重构 NPUCachingAutotuner 的预编译流程,采用 plugin 模式并统一串行/并行预编译逻辑;将 fasta_autotune.py 和 tile_generator.py 从 codegen 迁移至 runtime,明确运行时与代码生成层的边界。
主要改动
-
新增
lowering_patch设备分发机制: 在torch_npu/_inductor/lowering_patch.py中新增install_device_lowering_dispatch函数,通过_make_device_lowering_dispatcher创建包装器,当layout.device.type == "npu"时调用 NPU handler,否则委托给上游社区 handler;在__init__.py中通过LOWERING_OVERRIDE_OP列表调用该函数,替代了原先修改TritonScheduling的patch_triton_scheduling调用。 -
AlgorithmSelectorCache接口重构:select_algorithm.py中的__call__和make_benchmark_fn现在通过functools.wraps包装原始社区函数,按layout.device.type判断派发;NPU 路径的__call__返回值从单个output_node()改为(selected_node, selected_key)元组;相应地,mm.py、addmm、bmm.py、flex_attention.py、mm_grouped.py等调用方全部解包该元组。 -
移除
NPUTritonKernelWithLoop与inductor_ascend_linear_mode: 从triton.py中删除约 500 行的NPUTritonKernelWithLoop类及其codegen_kernel/codegen_body等复杂方法;从config.py删除inductor_ascend_linear_mode = "linear"配置项;NPUCombinedScheduling.codegen_node简化为始终先尝试_triton_scheduling(index 模式),异常时回退到_nolinear_triton_scheduling;block remap 逻辑浓缩为_remap_fallback_block_subs函数,由inductor_meta["requires_no_linear_block_remap"]标志控制。 -
NPUCachingAutotuner预编译流程重构: 引入_NPUSkipPrecompilePlugin插件机制替代硬编码的skip_precompile检查;将precompile_parallel、benchmark_all_configs、_benchmark_all_configs等独立函数整合进类方法_precompile_worker_serial/_precompile_worker_parallel;提取_create_launcher_grid函数统一 Grid 创建逻辑;_precompile_configs_with_vf_retry统一了串行与并行路径的 vf_fusion 重试逻辑。 -
post_grad与joint_graph的 NPU 感知改造:post_grad.py中patch_pattern_mm_plus_mm不再删除mm_plus_mm的 pattern entry,而是通过_npu_aware_extra_check包裹extra_check,使 NPU 场景跳过该融合但不影响其他设备;joint_graph.py中constant_fold_uniform_value增加_is_npu_graph判断,仅对 NPU 图执行eliminate_dead_code;autotune_process.py移除patch_tuning_process_pool,get_device_list回归社区实现。 -
模块重组与清理:
fasta_autotune.py和tile_generator.py从torch_npu/_inductor/codegen/重命名移动至torch_npu/_inductor/runtime/,tile_generator的导入路径相应更新;NPUNoLinearTritonScheduling.kernel_type改为类级别属性(= NPUTritonKernel);删除了codegen/triton.py中的patch_triton_scheduling和select_index_dtype静态方法。


代码审查
审查总结
本次 PR 共审查 27 个文件,发现 5 个问题:
| 优先级 | 数量 | 说明 |
|---|---|---|
| P1 | 1 | test_cpu_compile.py 缺少 import torch_npu._inductor,导致 lowering dispatch 测试必然失败 |
| P2 | 2 | post_grad.py 中 _npu_aware_extra_check 对 None extra_check 无防护;select_algorithm.py 中 __call__ 返回值从单值变为二元组的 breaking change |
| P3 | 2 | npu_combined_scheduling.py 中裸 except Exception 过于宽泛;test_inductor_graph_partition.py 中测试对上游实现有侵入性假设 |
整体风险评估:中低风险。
重构范围大但逻辑合理——主要是将 NPU Inductor 的实现从 monkey-patch 模式迁移到更规范的设备分发(device dispatch)和插件(plugin)模式,与 PyTorch v2.13 的接口契约对齐。所有调用方均已适配新的二元组返回值。
各文件审查结果
| 文件 | 结果 |
|---|---|
test/_inductor/test_algorithm_selector_dispatch.py |
无问题 |
test/_inductor/test_autotune_process.py |
无问题 |
test/_inductor/test_cpu_compile.py |
P1:缺少 import torch_npu._inductor |
test/_inductor/test_exceptions.py |
无问题 |
test/_inductor/test_inductor_graph_partition.py |
P3(架构注释) |
test/_inductor/test_lowering_device_dispatch.py |
无问题 |
test/_inductor/test_scheduling_contract.py |
无问题 |
test/npu/test_public_bindings.py |
无问题 |
torch_npu/_inductor/__init__.py |
无问题 |
torch_npu/_inductor/autotune_process.py |
无问题 |
torch_npu/_inductor/codegen/npu_combined_scheduling.py |
P3:裸 except Exception |
torch_npu/_inductor/codegen/scheduling.py |
无问题 |
torch_npu/_inductor/codegen/triton.py |
无问题 |
torch_npu/_inductor/config.py |
无问题 |
torch_npu/_inductor/fx_passes/joint_graph.py |
无问题 |
torch_npu/_inductor/fx_passes/post_grad.py |
P2:None extra_check 崩溃风险 |
torch_npu/_inductor/kernel/bmm.py |
无问题 |
torch_npu/_inductor/kernel/flex_attention.py |
无问题 |
torch_npu/_inductor/kernel/mm.py |
无问题 |
torch_npu/_inductor/kernel/mm_grouped.py |
无问题 |
torch_npu/_inductor/lowering_patch.py |
无问题 |
torch_npu/_inductor/profiler.py |
无问题 |
torch_npu/_inductor/runtime/fasta_autotune.py |
无问题 |
torch_npu/_inductor/runtime/tile_generator.py |
无问题 |
torch_npu/_inductor/runtime/triton_heuristics.py |
无问题 |
torch_npu/_inductor/select_algorithm.py |
P2:返回值 ABI breaking change |
torch_npu/_inductor/utils.py |
无问题 |
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 1 |
| 🟡 建议 | 1 |
⛔ 需要修改


The following users do not have permission to comment /lgtm or /approve on any module in this PR:
zhucehw


Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.




Issue: https://gitcode.com/Ascend/pytorch/issues/3329
Summary
PyTorch Inductor keeps
loweringsin a global registry. Previously, importingtorch_npureplaced community lowerings globally, so CPU nodes in a mixedCPU/NPU FX graph could enter NPU-specific lowering and autotuning paths.
This change installs a device-aware lowering dispatcher: NPU nodes use the
NPU lowering, while non-NPU nodes retain the original upstream lowering.
Changes
by the device-specific lowering path.
_make_device_lowering_dispatcherand install it around overriddenInductor lowerings.
devices.
mm,bmm,flex_attention, and grouped MM call sites for thePyTorch 2.13
(node, choice)algorithm-selector ABI.selector with the full PyTorch 2.13 call contract
and mixed-device graph partitioning.
【合入来源】
【修改方案】
【资料变更】
【接口变更】
【功能验证】
【CheckList】