-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multilingual NLI Tasks #329
Merged
Merged
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
5c69eb0
add multilignaul dynamic generative metrics
hynky1999 39c4220
Merge branch 'main' into geneartive_dynamic_metrics
hynky1999 2a5cdca
Merge branch 'geneartive_dynamic_metrics' into config_templates
hynky1999 2df9a08
draft
hynky1999 95729ee
finish multichoice config
hynky1999 3aa0579
Merge branch 'main' into geneartive_dynamic_metrics
hynky1999 b8f90a9
update tokenizers + install nltk reqs
hynky1999 f5a8717
use punkt tab
hynky1999 227f572
Update src/lighteval/utils/imports.py
hynky1999 d80b3ba
Update src/lighteval/metrics/normalizations.py
hynky1999 532bdad
fix imports
75f7ac5
remove unused import
f99e330
Merge branch 'main' into geneartive_dynamic_metrics
NathanHB 92daf90
Merge branch 'main' into geneartive_dynamic_metrics
clefourrier f2a801d
Merge branch 'main' into geneartive_dynamic_metrics
NathanHB 91d9d4f
finish implementation of templates + move stuff around
9356cc6
resolve nits
0fbc731
when in rome do as romans do (handle error messages the same way)
fa1fa83
fix utils
hynky1999 db36e16
Merge branch 'geneartive_dynamic_metrics' into config_templates
hynky1999 44aeecf
nicers tests + fix them
hynky1999 2bff963
nicer todo
hynky1999 3c9eb21
add nice doscrings 📃
hynky1999 4216ae2
add even more docstring
hynky1999 d8f56b8
nit
hynky1999 f26e88c
fix test
hynky1999 111d615
add multilingual to dev group
hynky1999 7ca4239
merge nli, add languagees to literals
hynky1999 22eeddb
translation literals
hynky1999 7faaa8a
add nli
hynky1999 ba44fe9
add rcb + chinese nli
hynky1999 2d09256
Merge branch 'geneartive_dynamic_metrics' into config_templates
hynky1999 7324e89
Merge branch 'config_templates' into multilnag_nli_tasks
hynky1999 ca865bd
Update src/lighteval/tasks/multilingual/tasks.py
hynky1999 1cc1187
Update src/lighteval/tasks/multilingual/tasks.py
hynky1999 d64251f
Update src/lighteval/tasks/multilingual/tasks.py
hynky1999 9806fab
Update src/lighteval/tasks/multilingual/tasks.py
hynky1999 35d7e6d
Update src/lighteval/tasks/multilingual/tasks.py
hynky1999 e560738
Update src/lighteval/tasks/multilingual/tasks.py
hynky1999 99524c5
Update src/lighteval/tasks/multilingual/tasks.py
hynky1999 150c76f
add two new tasks + docs
hynky1999 4e6100d
Merge branch 'multilnag_nli_tasks' of github.com:huggingface/lighteva…
hynky1999 7b561fe
Merge remote-tracking branch 'origin/main' into multilnag_nli_tasks
hynky1999 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,324 @@ | ||
# 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 langcodes import Language as LangCodeLanguage | ||
from langcodes import standardize_tag | ||
|
||
from lighteval.metrics.dynamic_metrics import loglikelihood_acc_metric | ||
from lighteval.metrics.normalizations import LogProbTokenNorm | ||
from lighteval.tasks.lighteval_task import LightevalTaskConfig | ||
from lighteval.tasks.templates.nli import get_nli_prompt_function | ||
from lighteval.tasks.templates.utils.formulation import ( | ||
CFFormulation, | ||
HybridFormulation, | ||
MCFFormulation, | ||
) | ||
from lighteval.utils.language import Language | ||
|
||
|
||
# ------------------------------- NLI Tasks ------------------------------- # | ||
# NLI (Natural Language Inference) tasks involve determining the logical relationship | ||
# between two given sentences: a premise and a hypothesis. The goal is to classify | ||
# whether the hypothesis is entailed by, contradicts, or is neutral with respect to | ||
# the premise. After our inspection we found the neutral label to be quite ambiguous | ||
# and decided to exclude it. But you can easily add it by modifying the adapters | ||
|
||
|
||
# The XNLI dataset is a multilingual variant of MultiNLI | ||
# https://aclanthology.org/D18-1269/ | ||
xnli_tasks = [ | ||
LightevalTaskConfig( | ||
name=f"xnli_{language.value}_{formulation.name.lower()}", | ||
suite=["lighteval"], | ||
metric=[loglikelihood_acc_metric(normalization=LogProbTokenNorm())], | ||
prompt_function=get_nli_prompt_function( | ||
language=language, | ||
adapter=lambda line: { | ||
"premise": line["premise"], | ||
"hypothesis": line["hypothesis"], | ||
# Since we ignore the neutral label | ||
"gold_idx": {0: 0, 2: 1}[line["label"]], | ||
}, | ||
relations=["entailment", "contradiction"], | ||
formulation=formulation, | ||
), | ||
hf_filter=lambda line: line["label"] in [0, 2], | ||
hf_repo="facebook/xnli", | ||
hf_subset=standardize_tag(language.value), | ||
evaluation_splits=["validation"], | ||
few_shots_split="train", | ||
) | ||
for language in [ | ||
Language.ARABIC, | ||
Language.ENGLISH, | ||
Language.FRENCH, | ||
Language.SPANISH, | ||
Language.BULGARIAN, | ||
Language.GERMAN, | ||
Language.GREEK, | ||
Language.ENGLISH, | ||
Language.FRENCH, | ||
Language.HINDI, | ||
Language.RUSSIAN, | ||
Language.SWAHILI, | ||
Language.THAI, | ||
Language.TURKISH, | ||
Language.URDU, | ||
Language.VIETNAMESE, | ||
Language.CHINESE, | ||
] | ||
for formulation in [MCFFormulation(), CFFormulation(), HybridFormulation()] | ||
] | ||
|
||
# Improvement on XNLI with better translation, from our experience models tend to | ||
# perform better on XNLI2.0 than XNLI | ||
# https://arxiv.org/abs/2301.06527 | ||
xnli2_tasks = [ | ||
LightevalTaskConfig( | ||
name=f"xnli2.0_{language.value}_{formulation.name.lower()}", | ||
suite=["lighteval"], | ||
metric=[loglikelihood_acc_metric(normalization=LogProbTokenNorm())], | ||
prompt_function=get_nli_prompt_function( | ||
language=language, | ||
adapter=lambda line: { | ||
"premise": line["premise"], | ||
"hypothesis": line["hypothesis"], | ||
# Since we ignore the neutral label | ||
"gold_idx": {0: 0, 2: 1}[line["label"]], | ||
}, | ||
relations=["entailment", "contradiction"], | ||
formulation=formulation, | ||
), | ||
hf_filter=lambda line: line["label"] in [0, 2], | ||
hf_repo=f"Harsit/xnli2.0_train_{LangCodeLanguage(standardize_tag(language.value)).language_name().lower()}", | ||
clefourrier marked this conversation as resolved.
Show resolved
Hide resolved
|
||
hf_subset="default", | ||
evaluation_splits=["train"], | ||
) | ||
for language in [ | ||
Language.ENGLISH, | ||
Language.FRENCH, | ||
Language.PUNJABI, | ||
Language.GUJARATI, | ||
Language.KANNADA, | ||
Language.ASSAMESE, | ||
Language.BENGALI, | ||
Language.MARATHI, | ||
Language.SANSKRIT, | ||
Language.TAMIL, | ||
Language.GERMAN, | ||
Language.ENGLISH, | ||
Language.URDU, | ||
Language.VIETNAMESE, | ||
Language.TURKISH, | ||
Language.THAI, | ||
Language.SWAHILI, | ||
Language.SPANISH, | ||
Language.RUSSIAN, | ||
Language.HINDI, | ||
Language.GREEK, | ||
Language.CHINESE, | ||
Language.BULGARIAN, | ||
Language.ARABIC, | ||
# Theoretically also: Bhojpuri, Gujarati, Odiya | ||
] | ||
for formulation in [MCFFormulation(), CFFormulation(), HybridFormulation()] | ||
] | ||
|
||
# Another variant of XNLI, with emphasis on Indic languages | ||
# https://arxiv.org/abs/2204.08776 | ||
xnli_indic_tasks = [ | ||
LightevalTaskConfig( | ||
name=f"indicnxnli_{language.value}_{formulation.name.lower()}", | ||
suite=["lighteval"], | ||
prompt_function=get_nli_prompt_function( | ||
language=language, | ||
adapter=lambda line: { | ||
"premise": line["premise"], | ||
"hypothesis": line["hypothesis"], | ||
# Since we ignore the neutral label | ||
"gold_idx": {0: 0, 2: 1}[line["label"]], | ||
}, | ||
relations=["entailment", "contradiction"], | ||
formulation=formulation, | ||
), | ||
hf_repo="Divyanshu/indicxnli", | ||
hf_subset=standardize_tag(language.value), | ||
# Ignore neutral | ||
hf_filter=lambda x: int(x["label"]) in [0, 2], | ||
evaluation_splits=["validation"], | ||
few_shots_split="train", | ||
few_shots_select=None, | ||
generation_size=-1, | ||
metric=[ | ||
loglikelihood_acc_metric(normalization=LogProbTokenNorm()), | ||
], | ||
) | ||
for language in [ | ||
Language.ASSAMESE, | ||
Language.BENGALI, | ||
Language.GUJARATI, | ||
Language.HINDI, | ||
Language.KANNADA, | ||
Language.MALAYALAM, | ||
Language.MARATHI, | ||
Language.ORIYA, | ||
Language.PUNJABI, | ||
Language.TAMIL, | ||
Language.TELUGU, | ||
] | ||
for formulation in [MCFFormulation(), CFFormulation(), HybridFormulation()] | ||
] | ||
|
||
# PAWS-X: A Cross-lingual Adversarial Dataset for Paraphrase Identification | ||
# This dataset contains paraphrase identification pairs in multiple languages. | ||
# It's derived from PAWS (Paraphrase Adversaries from Word Scrambling) and | ||
# We treat paraphrase as entailment and non-paraphrase as contradiction | ||
# https://arxiv.org/abs/1908.11828 | ||
|
||
paws_x_tasks = [ | ||
LightevalTaskConfig( | ||
name=f"pawsx_{language.value}_{formulation.name.lower()}", | ||
suite=("lighteval",), | ||
prompt_function=get_nli_prompt_function( | ||
language=language, | ||
adapter=lambda line: { | ||
"premise": line["sentence1"], | ||
"hypothesis": line["sentence2"], | ||
# Since we ignore the neutral label | ||
"gold_idx": int(line["label"]), | ||
}, | ||
relations=["entailment", "contradiction"], | ||
formulation=formulation, | ||
), | ||
hf_repo="google-research-datasets/paws-x", | ||
hf_subset=standardize_tag(language.value), | ||
evaluation_splits=("test",), | ||
few_shots_split="train", | ||
metric=[ | ||
loglikelihood_acc_metric(normalization=LogProbTokenNorm()), | ||
], | ||
) | ||
for language in [ | ||
Language.GERMAN, | ||
Language.ENGLISH, | ||
Language.SPANISH, | ||
Language.FRENCH, | ||
Language.JAPANESE, | ||
Language.KOREAN, | ||
Language.CHINESE, | ||
] | ||
for formulation in [MCFFormulation(), CFFormulation(), HybridFormulation()] | ||
] | ||
|
||
# Russian Commitment Bank (RCB) is a large-scale NLI dataset with Russian sentences, | ||
# collected from the web and crowdsourcing. | ||
# https://arxiv.org/abs/2401.04531 | ||
rcb_tasks = [ | ||
LightevalTaskConfig( | ||
name=f"rcb_{Language.RUSSIAN.value}_{formulation.name.lower()}", | ||
prompt_function=get_nli_prompt_function( | ||
language=Language.RUSSIAN, | ||
adapter=lambda line: { | ||
"premise": line["inputs"]["premise"], | ||
"hypothesis": line["inputs"]["hypothesis"], | ||
# Since we ignore the neutral label | ||
"gold_idx": int(line["outputs"]) - 1, | ||
}, | ||
relations=["entailment", "contradiction"], | ||
formulation=formulation, | ||
), | ||
suite=("lighteval",), | ||
hf_repo="ai-forever/MERA", | ||
hf_subset="rcb", | ||
# Ignore neutral label | ||
hf_filter=lambda x: int(x["outputs"] or "0") in [1, 2], | ||
evaluation_splits=("train", "validation"), | ||
metric=[ | ||
loglikelihood_acc_metric(normalization=LogProbTokenNorm()), | ||
], | ||
) | ||
for formulation in [MCFFormulation(), CFFormulation(), HybridFormulation()] | ||
] | ||
|
||
# Native Chinese NLI dataset based. | ||
# https://arxiv.org/pdf/2010.05444 | ||
# We find this benchmark to have really good signal compared to other Chinese NLI | ||
ocnli_tasks = [ | ||
LightevalTaskConfig( | ||
name=f"ocnli_{Language.CHINESE.value}_{formulation.name.lower()}", | ||
prompt_function=get_nli_prompt_function( | ||
language=Language.CHINESE, | ||
adapter=lambda line: { | ||
"premise": line["sentence1"], | ||
"hypothesis": line["sentence2"], | ||
# Since we ignore the neutral label | ||
"gold_idx": {1: 0, 2: 1}[line["label"]], | ||
}, | ||
relations=["entailment", "contradiction"], | ||
formulation=formulation, | ||
), | ||
suite=("lighteval",), | ||
hf_repo="clue/clue", | ||
hf_subset="ocnli", | ||
# Only keep the positive and negative examples | ||
hf_filter=lambda x: int(x["label"]) in [1, 2], | ||
evaluation_splits=("validation",), | ||
few_shots_split="train", | ||
metric=[ | ||
loglikelihood_acc_metric(normalization=LogProbTokenNorm()), | ||
], | ||
) | ||
for formulation in [MCFFormulation(), CFFormulation(), HybridFormulation()] | ||
] | ||
|
||
# https://arxiv.org/abs/2004.05986 | ||
# Native Chinese NLI dataset based on MNLI approach (Machine Translated) | ||
cmnli_tasks = [ | ||
LightevalTaskConfig( | ||
name=f"cmnli_{Language.CHINESE.value}_{formulation.name.lower()}", | ||
prompt_function=get_nli_prompt_function( | ||
language=Language.CHINESE, | ||
adapter=lambda line: { | ||
"premise": line["sentence1"], | ||
"hypothesis": line["sentence2"], | ||
# Since we ignore the neutral label | ||
"gold_idx": {"entailment": 0, "contradiction": 1}[line["label"]], | ||
}, | ||
relations=["entailment", "contradiction"], | ||
formulation=formulation, | ||
), | ||
suite=("lighteval",), | ||
hf_repo="fenffef/cmnli", | ||
hf_subset="default", | ||
hf_filter=lambda x: x["label"] in ["entailment", "contradiction"], | ||
# Only keep the positive and negative examples | ||
evaluation_splits=("validation",), | ||
few_shots_split="train", | ||
metric=[ | ||
loglikelihood_acc_metric(normalization=LogProbTokenNorm()), | ||
], | ||
) | ||
for formulation in [MCFFormulation(), CFFormulation(), HybridFormulation()] | ||
] | ||
|
||
|
||
TASKS_TABLE = [*xnli_tasks, *xnli2_tasks, *xnli_indic_tasks, *paws_x_tasks, *rcb_tasks, *ocnli_tasks, *cmnli_tasks] |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be nice to add just a bit of intro doc at the top of the file to explain what these tasks are overall about (= what is NLI, which datasets are used, etc)