Merged
inplace_partial_rotary_mul support autograd #8493
AlfengYuanAdd on Jul 9
inplace_partial_rotary_mul support autograd #8493
Merged
Merged fromalfengyuan/ops-transformermasterintocann/ops-transformermaster
Pull Request has been successfully merged by CANN-robot.
(Thank you for AlfengYuan's contribution.)Jul 9 Created pull request, commit 69346889
atomgit-bot
Jul 9 Comments:
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)被视为常量,不计算梯度。
主要改动
- 新增
InplacePartialRotaryMulFnautograd 包装类:继承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类中的字符串引号统一为双引号、部分长行拆分等样式修正。


Incorrect?
Jul 9 Added label: cann-cla/yes
CANN-robot
Jul 9 Comments:
Jul 9 Comments:
CANN-robot
Jul 9 Comments:
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
/approveor/lgtm- Commenting
/approveimplies 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. 👍


72 messages are collapsed here。 See more
liudan12
Jul 9 Comments:
Jul 9 Comments:
/approve
/lgtm


CANN-robot
Jul 9 Comments:
Jul 9 Comments:
Jul 9 Added label: lgtm
Jul 9 Closed associated issue
Jul 9 Merged pull request
描述
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
类型标签