Pull Request has been successfully merged by CANN-robot.
(Thank you for huangbolin's contribution.)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
/approveor/lgtm- Commenting
/approveimplies 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. 👍


变更摘要
本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()调用。


/lgtm


描述
torch_extention编译优化:将算子的编译从import阶段推迟到各个算子的接口被调用的时候,这样只有被用到的算子才被编译
关联的Issue
测试
文档更新
类型标签