Merged
inplace_partial_rotary_mul support autograd #8493
inplace_partial_rotary_mul support autograd #8493
Merged
AlfengYuanAdd on Jul 9
AlfengYuan
AlfengYuanCollaborators
Jul 9

描述

inplace_partial_rotary_mul support autograd

关联的Issue

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

测试

参考算子torch接口自动微分调用说明和样例,成功运行,并与手动调用结果做对比完全一致。

文档更新

torch_extension/cann_ops_transformer/docs/zh/inplace_partial_rotary_mul.md

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for AlfengYuan's contribution.)
AlfengYuanAlfengYuanCollaborators
Jul 9 Created pull request, commit 69346889
atomgit-bot
atomgit-bot
Jul 9 Comments:

变更摘要

此 PR 为 inplace_partial_rotary_mul 算子新增自动微分(autograd)支持。通过引入自定义 torch.autograd.Function 子类 InplacePartialRotaryMulFn,在 x.requires_grad 为 True 时自动走 autograd 路径,forward 完成就地 RoPE 变换并保存上下文,backward 调用对应的 inplace_partial_rotary_mul_backward 算子对梯度进行就地逆变换。r1(cos)和 r2(sin)被视为常量,不计算梯度。

主要改动

  • 新增 InplacePartialRotaryMulFn autograd 包装类:继承 torch.autograd.Function,forward 中保存 r1、r2、rotary_mode、partial_slice 到 ctx 并执行就地算子,backward 中调用 inplace_partial_rotary_mul_backward 对 grad_output 就地变换,返回 dx 及 r1/r2 的 None 梯度。
  • inplace_partial_rotary_mul 函数增加 autograd 分支逻辑:当 x.requires_grad 为 True 时,通过 InplacePartialRotaryMulFn.apply(...) 走 autograd 路径;否则保持原有的直接 backend 调用路径不变。
  • 更新函数文档字符串:在 inplace_partial_rotary_mul 的 docstring 中声明自动微分支持,说明 backward 会调用 inplace_partial_rotary_mul_backward,且 r1 和 r2 不计算梯度。
  • 代码格式化调整:InplacePartialRotaryMulOpBuilder 类中的字符串引号统一为双引号、部分长行拆分等样式修正。
likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Jul 9 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Jul 9 Comments:

CLA Signature Pass

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

likedislike
CANN-robot
CANN-robotCollaborators
Jul 9 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) ✅ 马兵, 刘丹 (2/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
72 messages are collapsed here。 See more
liudan12
liudan12Collaborators
Jul 9 Comments:

/approve
/lgtm

likedislike
CANN-robot
CANN-robotCollaborators
Jul 9 Comments:

The following users do not have permission to comment /lgtm or /approve on any module in this PR:
於欣洁, 宋恺

likedislike
CANN-robotCANN-robotCollaborators
Jul 9 Added label: lgtm
CANN-robotCANN-robotCollaborators
Jul 9 Closed associated issue
CANN-robotCANN-robotCollaborators
Jul 9 Merged pull request