已合并
fix: Fix the issue where the LoRA A/B matrices of the DeepSeek V4 linear_o_down_proj layer are not updated. #4922
xiejiahao2333创建于 5 天前
fix: Fix the issue where the LoRA A/B matrices of the DeepSeek V4 linear_o_down_proj layer are not updated. #4922
已合并
Pull Request已成功合入, 合并人@ascend-robot
(感谢 xiejiahao2333 的贡献)5 天前 创建了 pull request,commit 9111ce94
atomgit-bot
5 天前 评论:
5 天前 评论:
变更摘要
此 PR 修复了 DeepSeekV4 模型中 linear_o_down_proj 层 LoRA A/B 矩阵梯度不更新的问题。根本原因是原实现直接使用 self.linear_o_down_proj_weight 进行 einsum 计算,仅依赖原始权重,未将 LoRA A/B 矩阵纳入计算路径,导致 LoRA 参数无法获得梯度更新。修复方案是在 DeepSeek4SelfAttention 中新增 _linear_o_down_proj_lora 方法,显式计算 LoRA 分支的增量并叠加到基础输出上。
主要改动
- 新增
_linear_o_down_proj_lora方法:在DeepSeek4SelfAttention类中新增该方法,依次检查linear_o_down_proj是否存在lora_A/lora_B属性、适配器是否被禁用或已合并、以及活跃适配器列表;对每个活跃适配器,通过lora_A.weight和lora_B.weight计算 LoRA 增量(lora_delta = grouped_o * lora_A * lora_B * scaling),并叠加到基础输出上返回。 forward方法中保留原始输入grouped_o:在调用weight_woa的 einsum 之前,将未变换的o保存为grouped_o,供后续 LoRA 分支使用,确保 LoRA 计算的输入与基础路径一致。- 在 einsum 后追加 LoRA 分支调用:在
o = torch.einsum("sbgd,gld->sbgl", grouped_o, weight_woa)之后,立即调用o = self._linear_o_down_proj_lora(grouped_o, o),使 LoRA A/B 矩阵参与前向计算并能够接收梯度更新。


ascend-robot
5 天前 评论:
5 天前 评论:
atomgit-bot
5 天前 评论:
5 天前 评论:
5 天前 添加了label:ascend-cla/yes
此处折叠了82条消息 查看更多
ChenJingyi
1 天前 评论:
1 天前 评论:
/approve


1 天前 添加了label:approved
1 天前 合入了pull request
ascend-robot
1 天前 评论:
1 天前 评论:
Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.


ascend-robot
1 天前 评论:
1 天前 评论:
Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.


https://gitcode.com/Ascend/MindSpeed-LLM/issues/1772
What this PR does / why we need it?
Please describe the background and detailed changes of the PR. If it is a bugfix, please attach the related issue.
Fix the issue where the A/B matrices of the DeepSeek V4
linear_o_down_projlayer are not updated.一、Issue::The B matrix of
linear_o_down_projremains all zeros and is not updated. The A matrix also shows cases where it is not updated.二、Root cause::
The original implementation at
/opt/MindSpeed-LLM/mindspeed_llm/tasks/models/transformer/deepseek4/g2_attention.py:572directly readsself.linear_o_down_proj_weightfor theeinsumcomputation. This is equivalent to using only the original weight of this layer for computation, without adding the LoRA A/B matrices. As a result, the A/B matrices oflinear_o_down_projdo not receive gradients and are not updated.Does this PR introduce any user-facing change?
Please describe whether the PR will result in any user-facing usage changes. If there is related documentation, please specify its path.
Solution::
The newly added logic,
self._linear_o_down_proj_lora(), works as follows:a. First, check whether
linear_o_down_projhas LoRA A/B matrices.b. If LoRA is not enabled, directly return the original output, so normal training is not affected.
c. If LoRA is enabled, retrieve
lora_A,lora_B, andscaling.d. Use the input
grouped_oto compute an additional LoRA branch:e. Add the LoRA delta back to the original result:
How was this patch tested?
Please explain how to verify the correctness and effectiveness of this feature, as well as its usage constraints and limitations.
Commit ID:
1ed31e0e36aefdd2e868ec8e865164fd0b030480Training configuration:
Loss curve:



Update status of the A/B matrices:
vLLM test result after training: