feat(train): add gradient statistics#163
Merged
Merged
Conversation
Signed-off-by: Fumiya Watanabe <fumiya.watanabe.44@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
This PR adds per-step gradient statistics to the supervised training loop so that vanishing and exploding gradients can be observed during training. The statistics are logged to Weights & Biases through the existing logging path with no additional configuration.
What's added
A new helper
compute_grad_stats()inutils/train_utils.pycomputes the following over the concatenation of all parameter gradients (the global gradient vector):How it works
In
train_epoch.py, the statistics are computed right afterloss.backward()and beforeclip_grad_norm_(), so that exploding gradients are not masked by gradient clipping:The values are merged into the per-batch loss dict, so they automatically flow through the existing pipeline:
get_epoch_mean_loss()averages them per epoch, and they are logged to wandb astrain_loss/grad/*via the existingtrain_loss/{k}logging intrain_predictor.py.Notes
No new dependencies and no API/config changes; existing wandb dashboards pick up the new metrics automatically.
The GRPO training path (grpo_epoch.py) is intentionally left unchanged because it uses a keyed all-reduce across ranks that requires a consistent metric-key set; it can be addressed separately if needed.
Testing
Verified that train_epoch.py and train_utils.py parse without errors.