[Trainer] Support multi-loss component logging#45270
Draft
madhav1k wants to merge 1 commit intohuggingface:mainfrom
Draft
[Trainer] Support multi-loss component logging#45270madhav1k wants to merge 1 commit intohuggingface:mainfrom
madhav1k wants to merge 1 commit intohuggingface:mainfrom
Conversation
Introduce logging of individual loss components when models return a dict of losses. - Add TrainingArguments.logging_loss_components flag to enable/disable this behavior. - Track per-component running sums with _tr_loss_components and aggregate scalars in _total_loss_components_scalar. - Extend NaN/Inf filtering to handle dict-form losses and preserve previous logged averages per component. - When enabled, compute_loss is called to return outputs; Trainer extracts scalar loss-like tensors (components and main loss), accumulates them, and includes them in logs. - Add tests (tests/trainer/test_multi_loss.py) to verify logging enabled/disabled behavior and presence/absence of loss_part_* entries in training logs. This lets users surface and monitor auxiliary loss terms alongside the main loss during training.
Contributor
|
View the CircleCI Test Summary for this PR: https://huggingface.co/spaces/transformers-community/circle-ci-viz?pr=45270&sha=eb3a15 |
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.
Introduce logging of individual loss components when models return a dict of losses.
This lets users surface and monitor auxiliary loss terms alongside the main loss during training.
What does this PR do?
This PR introduces the ability for the Trainer to automatically log individual loss components when a model returns a dictionary of losses. This is particularly useful for multi-task learning or models with auxiliary loss terms (e.g., Distillation, MoE).
Adds TrainingArguments.logging_loss_components to toggle this behavior.
Extends training_step to extract and accumulate scalar loss components.
Handles distributed training by using nested_gather to aggregate components across processes before logging.
Updates the NaN/Inf filter to support dictionary-based losses.
Fixes: This work is coordinated on issue #31081 (also addresses the duplicate #30725).
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Test Results
I created a dedicated test file tests/trainer/test_multi_loss.py and ran it in a python 3.12 venv:
Results:
test_multi_loss_logging: PASSED (Verified components appear in log_history)
test_multi_loss_logging_disabled: PASSED (Verified backward compatibility)
Who can review?
@SunMarc @ArthurZucker (Trainer and Distributed expertise)