[Trainer] Add group-aware evaluation metrics using dataset metadata columns
Feature request
Adding group aware evaluation metrics using dataset metadata columns It would be useful to introduce native support for computing evaluation metrics grouped by metadata columns in the evaluation dataset.
Motivation
When evaluating multilingual models, a single aggregate evaluation score does not always provide enough information about model performance across different languages or data groups.
For example, when evaluating a multilingual model across European languages, the overall accuracy might look good while performance on certain languages is significantly lower.
The same problem applies to other scenarios:
Biomedical NLP models evaluated across different publication sources.
Multilingual classification and translation models evaluated across languages.
Models evaluated across different domains or data categories.
Models evaluated across different datasets or geographical groups.
Currently, users often need to implement custom evaluation loops or subclass Trainer to retain metadata and calculate metrics separately for each group.
Your contribution
Proposed functionality
Introduce an optional eval_group_by parameter that allows users to specify a metadata column for grouped evaluation.
For example:
training_args = TrainingArguments( output_dir="./results", eval_strategy="epoch", eval_group_by="language", )
The evaluation dataset could contain:
{ "input_ids": [...], "attention_mask": [...], "labels": [...], "language": ["en", "fr", "de", "es"] }
When trainer.evaluate() is called, the Trainer should calculate the existing evaluation metrics separately for each group.
Expected output:
{ "eval_loss": 0.42, "eval_en_accuracy": 0.94, "eval_fr_accuracy": 0.89, "eval_de_accuracy": 0.91, "eval_es_accuracy": 0.87, }
The exact API and metric naming can be discussed with maintainers.
Implementation considerations
Preserve selected metadata columns during evaluation without passing them unnecessarily to model.forward().
Keep metadata correctly aligned with predictions and labels.
Support distributed evaluation without introducing duplicated or incorrectly grouped samples.
Ensure compatibility with existing compute_metrics implementations.
Preserve existing behavior when grouped evaluation is not enabled.
Expected benefits
Makes multilingual model evaluation more transparent.
Helps identify performance differences between individual languages and datasets.
Reduces the need for custom Trainer subclasses.
Provides a reusable evaluation mechanism for NLP, speech, biomedical and multimodal research.
Acceptance criteria
Add an optional grouped-evaluation API.
Calculate metrics separately for each metadata group.
Keep metadata separate from model inputs.
Add unit tests for multiple groups and unequal group sizes.
Test distributed evaluation and metadata alignment.
Document an example using a multilingual evaluation dataset.
I would be interested in contributing an implementation if this direction aligns with the Trainer API roadmap.