已合并
fix: torch_extension 算子load()由导入期改为首次调用时触发, 消除整包导入即JIT编译 #12563
fix: torch_extension 算子load()由导入期改为首次调用时触发, 消除整包导入即JIT编译 #12563
已合并
weihao18创建于 9月21日
weihao18成员
9月21日

描述

fix: torch_extension 算子load()由导入期改为首次调用时触发, 消除整包导入即JIT编译

关联的Issue

https://gitcode.com/cann/ops-transformer/issues/5177

测试

文档更新

类型标签

likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 weihao18 的贡献)
Wweihao18成员
9月21日 创建了 pull request,commit 63ecd944
Wweihao18成员
9月21日 关联了issue:[Bug-Report|缺陷反馈]: torch_extension编译问题
atomgit-bot
atomgit-bot
9月21日 评论:

变更摘要

本次变更将各 torch_extension 算子的算子加载时机从模块导入期改为首次调用时:删除模块级别的 _op_module = builder.load()(或裸调用 builder.load()),改为在算子执行函数内部按需调用 builder.load(),从而避免整包导入即触发 JIT 编译。涉及 block_sparse_attention、all_gather_quant_matmul、apply_rotary_pos_emb、apply_rotary_pos_emb_grad 等算子模块,其中 block_sparse_attention.py 在正式目录与 experimental 目录下各有一份同步修改。其余改动为导入顺序整理与代码格式规整,不改变对外行为。

主要改动

  • 算子加载时机延迟到首次调用: 在 block_sparse_attention.py(含 attention/ 与 experimental/attention/ 两份)中删除模块级 _op_module = _op_builder.load(),并在 block_sparse_attention 函数内改为 op_module = _op_builder.load() 后再调用 op_module.npu_block_sparse_attention(...)。
  • all_gather_quant_matmul 同样按需加载: 删除模块级 _op_module = _all_gather_quant_matmul_op_builder.load(),在 _npu_all_gather_quant_matmul 中改为先 op_module = _all_gather_quant_matmul_op_builder.load(),再调用 op_module.npu_all_gather_quant_matmul(...)。
  • apply_rotary_pos_emb 去除导入期加载: 删除模块级 apply_rotary_pos_emb_op_builder.load() 调用,ApplyRotaryPosEmbOpBuilder 的构造与调用路径不再在导入时触发加载。
  • apply_rotary_pos_emb_grad 去除导入期加载: 删除模块级 apply_rotary_pos_emb_grad_op_builder.load() 调用,加载行为不再于模块导入阶段发生。
  • 导入顺序与格式整理: 多个文件统一将 cann_ops_transformer.op_builder 的导入调整到 torch.library 之前,并合并了多行 raise ValueError、张量构造及函数调用表达式为单行。
likedislike
不准确?
CANN-robotCANN-robot成员
9月21日 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
9月21日 评论:

CLA Signature Pass

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

likedislike
CANN-robot
CANN-robot成员
9月21日 评论:

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.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
attention ✅ 於欣洁, Wei_NaChuan (2/2) ✅ 於欣洁 (1/1)
experimental/attention ✅ 於欣洁, Wei_NaChuan (2/2) ✅ 於欣洁 (1/1)
mc2 ✅ wang-minbo, chenjunjian11 (2/2) ✅ wang-minbo (1/1)
posembedding ✅ 於欣洁, Wei_NaChuan (2/2) ✅ 於欣洁 (1/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)
likedislike
CANN-robotCANN-robot成员
9月21日 将guijianwei,hz36amy_00,yangziqi_007,coder_linx,captainmiaow,juyangokok,monologue815,xuanyuandy,chen-linxin4,crystalhu,yu-xinjie62,huangwei791,jiang-lirui,yang-binrong,Allan_Yu,wang-minbo,huang-wei-chen,tgwsakiko_,songkai111,liudan12,chenjunjian11,shasha_an,wangzhe123456789,yangzeheng,Wei_NaChuan,liuboxi,zhangshuoleiokok,luwei_1104,cc-z,kdy18482276080,xuchengyang,luobaiqing,shen_weiling,zhong-zixin,gaoxiang618,libohao6,songjionghui,wenxin_fight,miaofangzheng,mabing1118,tang-hao-hw-gitcode,haijie_699874,macech,zzy__,Julius47,yuhao_,L_Euler,wangxun21设为评审人
CANN-robotCANN-robot成员
9月21日 将guijianwei,hz36amy_00,yangziqi_007,coder_linx,captainmiaow,juyangokok,monologue815,xuanyuandy,chen-linxin4,crystalhu,yu-xinjie62,huangwei791,jiang-lirui,yang-binrong,Allan_Yu,wang-minbo,huang-wei-chen,tgwsakiko_,songkai111,liudan12,chenjunjian11,shasha_an,wangzhe123456789,yangzeheng,Wei_NaChuan,liuboxi,zhangshuoleiokok,luwei_1104,cc-z,kdy18482276080,xuchengyang,luobaiqing,shen_weiling,zhong-zixin,gaoxiang618,libohao6,songjionghui,wenxin_fight,miaofangzheng,mabing1118,tang-hao-hw-gitcode,haijie_699874,macech,zzy__,Julius47,yuhao_,L_Euler,wangxun21设为审查人
Wweihao18成员
9月21日 修改了pull request 的描述
weihao18成员
9月21日 评论:

/compile

likedislike
Wweihao18成员
9月21日 预合并成功(commit_id: 6a9440ddedd8309d7cf30bbaf570cd5510765250)
CANN-robot
CANN-robot成员
9月21日 评论:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robot成员
9月21日 添加了label:ci-pipeline-running
此处折叠了6条事件消息 查看更多
CANN-robotCANN-robot成员
9月21日 添加了label:cann-cla/yes
weihao18成员
9月21日 评论:

/compile

likedislike
Wweihao18成员
9月21日 预合并成功(commit_id: 0261b85359972b8b0553ab845a064886f2cb8ffc)
CANN-robot
CANN-robot成员
9月21日 评论:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robot成员
9月21日 删除了label:ci-pipeline-failed
CANN-robotCANN-robot成员
9月21日 添加了label:ci-pipeline-running
CANN-robotCANN-robot成员
9月21日 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
9月21日 添加了label:ci-pipeline-passed
wang-minbo成员
9月21日 评论:

/lgtm
/approve

likedislike
於欣洁成员
9月21日 评论:

/lgtm
/approve

likedislike
CANN-robotCANN-robot成员
9月21日 添加了label:approved
chenjianjun11成员
9月21日 评论:

/lgtm

likedislike
weinachuan成员
9月21日 评论:

/lgtm

likedislike
CANN-robotCANN-robot成员
9月21日 添加了label:lgtm
CANN-robotCANN-robot成员
9月21日 合入了pull request
游游震成员
9月21日 删除了关联的issue:[Bug-Report|缺陷反馈]: torch_extension编译问题
游游震成员
9月21日 关联了issue:[Bug-Report|缺陷反馈]: torch_extension 部分 C++ 扩展算子缺少懒加载,import 时即触发编译