-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Probability Metric + New Normalization (#276)
What does this implement/fix? Explain your changes. --------------------------------------------------- This PR adds two new features: 1) New Probability Metric, allowing to collect probability of correct answer. This can be either raw prob or prob mass (normalized by other choices) 2) Revamps Acc/Prob normalization and adds two new normalizations a) Token normalization, which we found to be better at most of the non-english langauges compared to acc norm. b) PointwiseMutualInformation normalization, which is good way for testing tasks with unlikely token see: https://arxiv.org/abs/2406.08446 Lastly I have done some small changes to the requests processing, removing parts, which are not needed and can easily cause bugs. Comments ---------- - I am not really content with having new category just for normalization but I didn't find a better way in the current system. The problem is that when creating requests we only have access to sample fc, but nothing else, thus we can't really do any kind of structural decomposition :( - This new norms are only added for non-single token types of tasks. Adding them to single token would require improving the requests creating logic to be maintanable and can be done in other PR PS: Relevant disscusion about token norm EleutherAI/lm-evaluation-harness#1396 --------- Co-authored-by: Hynek Kydlicek <[email protected]> Co-authored-by: Nathan Habib <[email protected]>
- Loading branch information
1 parent
cdeb6c2
commit 8c787df
Showing
17 changed files
with
939 additions
and
92 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# MIT License | ||
|
||
# Copyright (c) 2024 The HuggingFace Team | ||
|
||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
|
||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
|
||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
from typing import Callable | ||
|
||
import numpy as np | ||
|
||
from lighteval.metrics.metrics_sample import LoglikelihoodAcc, NormalizedMultiChoiceProbability, Probability | ||
from lighteval.metrics.normalizations import LogProbNormalization, LogProbPMINorm, LogProbTokenNorm | ||
from lighteval.metrics.utils import MetricCategory, MetricUseCase, SampleLevelMetric | ||
|
||
|
||
def loglikelihood_acc_metric(normalization: LogProbNormalization | None = None) -> SampleLevelMetric: | ||
""" | ||
Creates a accuracy (loglikelihood) metric, which returns accuracy given normalization. | ||
""" | ||
|
||
normalization_str = normalization.name if normalization else "" | ||
metric_name = f"acc_{normalization_str}" | ||
return SampleLevelMetric( | ||
metric_name=metric_name, | ||
sample_level_fn=LoglikelihoodAcc(logprob_normalization=normalization).compute, | ||
category=MetricCategory.MULTICHOICE | ||
if not normalization == LogProbPMINorm() | ||
else MetricCategory.MULTICHOICE_PMI, | ||
use_case=MetricUseCase.ACCURACY, | ||
corpus_level_fn=np.mean, | ||
higher_is_better=True, | ||
) | ||
|
||
|
||
def normalized_multi_choice_prob_metric( | ||
normalization: LogProbNormalization | None = None, | ||
aggregation_function: Callable[[np.ndarray], float] = np.max, | ||
) -> SampleLevelMetric: | ||
""" | ||
Creates a normalized multi-choice probability metric, which returns the probability of the gold choice / sum of probabilities of all choices (after logprobs are normalized). | ||
""" | ||
|
||
normalization_str = normalization.name if normalization else "" | ||
metric_name = "_".join(filter(None, ["normalized_mc_prob_", normalization_str])) | ||
|
||
return SampleLevelMetric( | ||
metric_name=metric_name, | ||
sample_level_fn=NormalizedMultiChoiceProbability( | ||
log_prob_normalization=normalization, aggregation_function=aggregation_function | ||
).compute, | ||
category=MetricCategory.MULTICHOICE | ||
if not normalization == LogProbPMINorm() | ||
else MetricCategory.MULTICHOICE_PMI, | ||
use_case=MetricUseCase.ACCURACY, | ||
corpus_level_fn=np.mean, | ||
higher_is_better=True, | ||
) | ||
|
||
|
||
def probability_metric( | ||
normalization: LogProbTokenNorm | None = None, | ||
aggregation_function: Callable[[np.ndarray], float] = np.max, | ||
) -> SampleLevelMetric: | ||
""" | ||
Creates a probability metric, which returns the probability of the gold choice given normalization. | ||
""" | ||
|
||
normalization_str = normalization.name if normalization else "" | ||
metric_name = "_".join(filter(None, ["prob", normalization_str])) | ||
|
||
return SampleLevelMetric( | ||
metric_name=metric_name, | ||
sample_level_fn=Probability(normalization=normalization, aggregation_function=aggregation_function).compute, | ||
category=MetricCategory.TARGET_PERPLEXITY, | ||
use_case=MetricUseCase.PERPLEXITY, | ||
corpus_level_fn=np.mean, | ||
higher_is_better=True, | ||
) |
This file contains 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
This file contains 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
Oops, something went wrong.