Merged
处理swiglu_group和swiglu_group_quant的自动反向未在dispatcher层注册的问题 #8389
liuhongpengAdd on Aug 7
处理swiglu_group和swiglu_group_quant的自动反向未在dispatcher层注册的问题 #8389
Merged
liuhongpengAdd on Aug 7
liuhongpeng
Aug 7

描述

swiglu_group和swiglu_group_quant的自动反向未在dispatcher层,导致在跑图模式情况下自动反向部分场景出现问题。

关联的Issue

https://gitcode.com/cann/ops-nn/issues/4636

测试

自动反向功能的单算子和图模式验证OK,ST用例OK,冒烟OK。

文档更新

类型标签

AI/Agent生成声明

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for liuhongpeng's contribution.)
liuhongpengliuhongpeng
Aug 7 Created pull request, commit c62c2749
atomgit-bot
atomgit-bot
Aug 7 Comments:

变更摘要

此 PR 主要解决 swiglu_group 算子的自动反向传播未在 dispatcher 层注册的问题,导致图模式(torch.compile)下部分场景的反向传播异常。核心改动是将反向传播逻辑从自定义的 torch.autograd.Function 子类 SwigluGroupFn 迁移至 torch.library.register_autograd 进行注册,使 autograd 在 dispatcher 层生效;同时调整了算子签名中参数的位置/关键字属性,将 weight 和 group_index 变为位置参数,clamp_limit 变为 keyword-only 参数。

主要改动

  • 用 torch.library.register_autograd 替换自定义 SwigluGroupFn:移除原有的 SwigluGroupFn(torch.autograd.Function) 类,新增 _swiglu_group_backward_autograd 和 _swiglu_group_setup_context 函数,并通过 torch.library.register_autograd("cann_ops_nn::swiglu_group", ...) 在 dispatcher 层注册 autograd,使反向传播在图模式下正常工作。
  • 调整算子签名中参数的关键字属性:将 swiglu_group 的 schema 从 (Tensor x, *, Tensor? weight, Tensor? group_index, float clamp_limit) 改为 (Tensor x, Tensor? weight, Tensor? group_index, *, float clamp_limit),即 weight 和 group_index 变为位置参数,clamp_limit 变为 keyword-only 参数。
  • 同步修改 meta 注册和实现函数的参数顺序:swiglu_group_meta 和 swiglu_group 实现的函数签名均从 (x, *, weight, group_index, clamp_limit) 调整为 (x, weight, group_index, *, clamp_limit),与 schema 保持一致。
  • 同步调整 graph_convert_swiglu_group.py 中的 FX 转换器签名:将 convert_swiglu_group 函数的 weight 和 group_index 参数从 * 之后移至 * 之前,匹配算子签名变更。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Aug 7 Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Aug 7 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Aug 7 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
activation ✅ 黄迪, 唐玮玮 (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

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

likedislike
65 messages are collapsed here。 See more
kevin_huang1234
kevin_huang1234Collaborators
Aug 12 Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
Aug 12 Added label: lgtmapproved
CANN-robotCANN-robotCollaborators
Aug 12 Closed associated issue
CANN-robotCANN-robotCollaborators
Aug 12 Merged pull request
CANN-robot
CANN-robotCollaborators
Aug 12 Comments:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike