Add _metrics dict to Trainer for custom metric logging#43636
Open
abigailtech wants to merge 2 commits intohuggingface:mainfrom
Open
Add _metrics dict to Trainer for custom metric logging#43636abigailtech wants to merge 2 commits intohuggingface:mainfrom
abigailtech wants to merge 2 commits intohuggingface:mainfrom
Conversation
Member
qgallouedec
reviewed
Feb 3, 2026
| self._memory_tracker = TrainerMemoryTracker(self.args.skip_memory_metrics) | ||
| self._memory_tracker.start() | ||
|
|
||
| self._metrics = {"train": defaultdict(list), "eval": defaultdict(list)} |
Member
There was a problem hiding this comment.
I'm wondering if we can leverage this self._metrics to log the loss
Author
There was a problem hiding this comment.
though about it, but loss uses tensor accumulation to avoid TPU sync overhead. metrics is more for auxiliary custom metrics that don't need that optimization
evalstate
added a commit
to evalstate/transformers
that referenced
this pull request
Apr 29, 2026
This was referenced Apr 29, 2026
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.
Adds a private _metrics dict to the Trainer class that allows custom trainers to log metrics without overriding log.
Custom trainers can now simply do:
self._metrics[mode]["my_metric"].append(value)And the metrics will be automatically averaged, merged into logs and cleared when log() is called.
Fixes #43599