Skip to content

Add _metrics dict to Trainer for custom metric logging#43636

Open
abigailtech wants to merge 2 commits intohuggingface:mainfrom
abigailtech:add-trainer-metrics-dict
Open

Add _metrics dict to Trainer for custom metric logging#43636
abigailtech wants to merge 2 commits intohuggingface:mainfrom
abigailtech:add-trainer-metrics-dict

Conversation

@abigailtech
Copy link
Copy Markdown

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

@Rocketknight1
Copy link
Copy Markdown
Member

cc @qgallouedec @SunMarc

self._memory_tracker = TrainerMemoryTracker(self.args.skip_memory_metrics)
self._memory_tracker.start()

self._metrics = {"train": defaultdict(list), "eval": defaultdict(list)}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we can leverage this self._metrics to log the loss

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use a private _metrics dict to allow for additional metric logging

3 participants