deepspeedai / deepspeedai/DeepSpeed

[HELP] Perform backpropagation separately for the two losses and extract the corresponding gradients.

Open
#7,352 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
43.1k
Forks
5k
Avg merge
4d 15h
Merged PRs (30d)
112

Description

Hello, I currently have a requirement: during a single-time forward pass, I compute two losses, and I would like to backpropagate each loss separately to obtain their respective gradients. Since I made the modifications within a complex project codebase and cannot paste the original code, the following pseudocode illustrates the core logic:

#step1:  forward, and calculate two losses
output = model(input)
loss1 = loss1_func(output, target_1)
loss2 = loss2_func(output, target_2)

#step2:  loss1 backward and extract its grad
model.zero_grad()
accelerator.backward(loss1,  retain_graph=True)
grad_loss1 = safe_get_full_grad(model)

#step3:  loss2 backward and extract its grad
model.zero_grad()
accelerator.backward(loss2)
grad_loss2 = safe_get_full_grad(model)

However, when I run the code with ZeRO-1 and gradient_accumulation_steps=1, I find that the second call to model.zero_grad() (the first line of step3 ) does not take effect — that is, even though this line is executed, the gradients from loss1 still remain in the model. Moreover, the gradients from loss2 cannot overwrite or accumulate onto the existing gradients. In other words, once the backward pass for loss1 is completed, its gradients are forcibly retained in the model and cannot be cleared or overwritten.

I further found that when I set gradient_accumulation_steps=2, the gradients for the second loss can be backpropagated correctly. However, in my project, gradient_accumulation_steps must be strictly set to 1. Therefore, is there any way to successfully extract the gradients corresponding to both losses when gradient_accumulation_steps=1?

Thanks a lot!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the pseudocode with ZeRO-1 and gradient_accumulation_steps=1, then compare it with accumulation_steps=2. Start by tracing model.zero_grad(), accelerator.backward(), and safe_get_full_grad() through the ZeRO-1 gradient-management path. Done means both losses produce independently extractable gradients without loss1 gradients persisting into the loss2 pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
distributed-systems, machine-learning
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.