Merged
torch_extention编译优化:延迟编译 #8155
torch_extention编译优化:延迟编译 #8155
Merged
huangbolinAdd on Jul 3
huangbolin
huangbolinCollaborators
Jul 3

描述

torch_extention编译优化:将算子的编译从import阶段推迟到各个算子的接口被调用的时候,这样只有被用到的算子才被编译

关联的Issue

测试

文档更新

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for huangbolin's contribution.)
huangbolinhuangbolinCollaborators
Jul 3 Created pull request, commit 7334fd3b
CANN-robotCANN-robotCollaborators
Jul 3 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Jul 3 Comments:

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
sig-ops-transformer ✅ 蒋礼锐, 马兵 (2/2) ✅ 蒋礼锐 (1/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
atomgit-bot
atomgit-bot
Jul 3 Comments:

变更摘要

本PR对 torch_extention 下的自定义NPU算子加载机制进行优化:将各算子 builder 的 .load() 调用从模块顶层(导入时立即触发编译/加载)推迟到每个算子接口首次调用时执行,避免了在导入阶段就进行耗时的 .so 编译加载操作。改动涉及 24 个算子文件,核心模式一致——删除模块级别的 _op_module = _xxx_builder.load() 语句,改为在对应的 @impl(AS_LIBRARY, ...) 装饰的调度函数或公开接口函数内部按需调用 .load()。

主要改动

  • 算子模块延迟加载(覆盖绝大多数算子):在 causal_conv1d_fn.py、causal_conv1d_update.py、compressor.py、flash_attn.py、indexer_quant_cache.py、inplace_partial_rotary_mul.py、kv_compress_epilog.py、lightning_indexer.py、mega_moe.py、moe_distribute_combine.py、moe_distribute_dispatch.py、moe_token_permute.py、mixed_quant_sparse_flash_mla.py、quant_lightning_indexer.py、scatter_pa_kv_cache_with_k_scale.py、sparse_flash_mla.py、sparse_flash_mla_grad.py、sparse_lightning_indexer_kl_loss_grad.py 等文件中,删除模块顶层的 _op_module = _builder.load(),改为在各算子调度函数体内第一行调用 _builder.load() 获取模块引用。

  • comm_context.py 采用 __getattr__ 实现按需加载:不再在模块级别执行 comm_context_op_builder.load(),而是定义 __getattr__ 函数,当访问 CommContextManager 属性时才触发 .load() 并返回对应对象,其他属性访问则抛出 AttributeError。

  • 多调用点文件在各使用点分别加载:elastic_buffer.py 中原有一个模块级 _elastic_buffer_ops,现改为在 get_engram_storage_size_hint、moe_ep_dispatch、moe_ep_combine、_get_runtime、_prepare_host_counter 等多个方法内分别执行 _elastic_buffer_op_builder.load();类似地,mhc_post.py、mhc_pre_sinkhorn.py、mhc_post_backward.py、mhc_pre_sinkhorn_backward.py 也在各自的前向函数、调度函数和普通路径中分别添加了 .load() 调用。

likedislike
Incorrect?
huangbolinhuangbolinCollaborators
Jul 4 Pre merge succeeded (commit_id: 758b4a70dd73323b38d3b65b776af91379ee3dfd)
86 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Jul 6 Added label: approved
mabing
mabingCollaborators
Jul 6 Comments:

/lgtm

likedislike
CANN-robotCANN-robotCollaborators
Jul 6 Added label: lgtm
CANN-robotCANN-robotCollaborators
Jul 6 Merged pull request
huangbolinhuangbolinCollaborators
Jul 6 Associated issue: [Bug-Report|缺陷反馈]: torch extension导入阶段会触发全量算子编译