deepspeedai / deepspeedai/DeepSpeed

How can I extract and log grad norm for individual layers

Open
#4,555 3 comments 4 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

I want to use gradients to monitor if the model is training properly, like this

image

I change the transformers.Trainer https://github.com/huggingface/transformers/blob/main/src/transformers/trainer.py#L1857 ,by add a function to get the parm.grad to log grad norm front the zero_grad.

But it not work for the model when use the DeepSpeed, parm.grad is None for all layers, I read the code and found comments said there is no way to return gradients when use DeepSpeed.

 elif self.distributed_type == DistributedType.DEEPSPEED:
    # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed
    # We cannot return the gradient norm because DeepSpeed does it.
    return None

https://github.com/huggingface/accelerate/blob/69e4c3c54da3201eda288b500d138761e7a5221c/src/accelerate/accelerator.py#L1499

So how can I get the grad norm for track training state when I am using the DeepSpeed?

I found a similar problem in megatron-deepspeed, but there seems to be no follow-up

https://github.com/bigscience-workshop/Megatron-DeepSpeed/issues/149

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

Start with the linked transformers Trainer location and accelerate.py around line 1499 to trace how DeepSpeed handles gradients, then compare the related Megatron-DeepSpeed issue. A complete resolution would need an agreed, tested way to obtain per-layer gradient norms during DeepSpeed training, but the issue does not specify the intended API or implementation location.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
distributed-systems, machine-learning
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.