Skip to content

Commit 6041e36

Browse files
Fixing issues with multichoice_continuations_start_space - was not parsed properly (#232)
* fixing issues - was not parsed properly * Update src/lighteval/models/model_config.py Co-authored-by: Nathan Habib <[email protected]> * fix --------- Co-authored-by: Nathan Habib <[email protected]>
1 parent cbae17d commit 6041e36

File tree

6 files changed

+55
-36
lines changed

6 files changed

+55
-36
lines changed

examples/model_configs/base_model.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ model:
99
adapter_weights: false # set to True of your model has been trained with peft, also need to provide the base model name
1010
base_model: null # path to the base_model
1111
generation:
12-
multichoice_continuations_start_space: false # Whether to force multiple choice continuations to start with a space
13-
no_multichoice_continuations_start_space: false # Whether to force multiple choice continuations to not start with a space
12+
multichoice_continuations_start_space: null # If true/false, will force multiple choice continuations to start/not start with a space. If none, will do nothing

examples/nanotron/lighteval_config_override_template.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ lighteval:
2424
dataset_loading_processes: 8
2525
max_samples: 10
2626
multichoice_continuations_start_space: null
27-
no_multichoice_continuations_start_space: null
2827
num_fewshot_seeds: null
2928
tasks: early-signal
3029
# tasks: custom|hellaswag|0

src/lighteval/models/base_model.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,11 @@ def _check_continuations_start_space(self, continuation: str) -> str:
296296
- None (Don't touch - default)
297297
Todo: find a way to add this back WITHOUT breaking compatibility with the harness
298298
"""
299-
if self.multichoice_continuations_start_space is True and continuation[0] != " ":
300-
continuation = " " + continuation
301-
if self.multichoice_continuations_start_space is False and continuation[0] == " ":
302-
continuation = continuation.lstrip()
299+
if self.multichoice_continuations_start_space is not None:
300+
if self.multichoice_continuations_start_space and continuation[0] != " ":
301+
continuation = " " + continuation
302+
if not self.multichoice_continuations_start_space and continuation[0] == " ":
303+
continuation = continuation.lstrip()
303304
return continuation
304305

305306
def _model_call(self, inputs: torch.Tensor) -> torch.Tensor:
@@ -949,7 +950,7 @@ def loglikelihood_single_token(
949950
if any(len(c) > 1 for c in continuations_enc):
950951
raise ValueError(
951952
f"Trying to do single token multiple choice but one choice has several tokens: {continuations_enc}. "
952-
"If the additional pre-token is a space, try to set --no_multichoice_continuations_start_space "
953+
"If the additional pre-token is a space, try to set `multichoice_continuations_start_space=False` in the model parameters "
953954
)
954955
request.tokenized_continuation = continuations_enc
955956

src/lighteval/models/model_config.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
NO_AUTOGPTQ_ERROR_MSG,
3535
NO_BNB_ERROR_MSG,
3636
NO_PEFT_ERROR_MSG,
37+
boolstring_to_bool,
3738
is_accelerate_available,
3839
is_autogptq_available,
3940
is_bnb_available,
@@ -76,6 +77,7 @@ class BaseModelConfig:
7677
space at the start of each continuation in multichoice generation.
7778
For example, context: "What is the capital of France?" and choices: "Paris", "London".
7879
Will be tokenized as: "What is the capital of France? Paris" and "What is the capital of France? London".
80+
True adds a space, False strips a space, None does nothing
7981
subfolder (Optional[str]): The subfolder within the model repository.
8082
revision (str): The revision of the model.
8183
batch_size (int): The batch size for model training.
@@ -127,6 +129,9 @@ class BaseModelConfig:
127129
compile: bool = False
128130

129131
def __post_init__(self):
132+
# Making sure this parameter is a boolean
133+
self.multichoice_continuations_start_space = boolstring_to_bool(self.multichoice_continuations_start_space)
134+
130135
if self.quantization_config is not None and not is_bnb_available():
131136
raise ImportError(NO_BNB_ERROR_MSG)
132137

@@ -350,15 +355,21 @@ def create_model_config( # noqa: C901
350355
return InferenceModelConfig(model=config["base_params"]["endpoint_name"])
351356

352357
if config["type"] == "base":
353-
# Tests on the multichoice space parameters
354-
multichoice_continuations_start_space = config["generation"]["multichoice_continuations_start_space"]
355-
no_multichoice_continuations_start_space = config["generation"]["no_multichoice_continuations_start_space"]
356-
if not multichoice_continuations_start_space and not no_multichoice_continuations_start_space:
357-
multichoice_continuations_start_space = None
358-
if multichoice_continuations_start_space and no_multichoice_continuations_start_space:
359-
raise ValueError(
360-
"You cannot force both the multichoice continuations to start with a space and not to start with a space"
361-
)
358+
# Creating the multichoice space parameters
359+
# We need to take into account possible conversion issues from our different input formats
360+
multichoice_continuations_start_space = boolstring_to_bool(
361+
config["generation"]["multichoice_continuations_start_space"]
362+
)
363+
364+
if multichoice_continuations_start_space is not None:
365+
if multichoice_continuations_start_space:
366+
hlog(
367+
"You set `multichoice_continuations_start_space` to true. This will force multichoice continuations to use a starting space"
368+
)
369+
else:
370+
hlog(
371+
"You set `multichoice_continuations_start_space` to false. This will remove a leading space from multichoice continuations, if present."
372+
)
362373

363374
# Creating optional quantization configuration
364375
if config["base_params"]["dtype"] == "4bit":

src/lighteval/models/nanotron_model.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
LoglikelihoodRequest,
6464
LoglikelihoodRollingRequest,
6565
)
66-
from lighteval.utils import as_list
66+
from lighteval.utils import as_list, boolstring_to_bool
6767
from lighteval.utils_parallelism import find_executable_batch_size
6868

6969

@@ -113,20 +113,10 @@ def __init__(
113113
# To implement PP parallelism we need to think about how we want to sync the output for the PP ranks without outputs
114114
raise ValueError("PP parallelism is not supported yet")
115115

116-
multichoice_continuations_start_space = lighteval_config.tasks.multichoice_continuations_start_space
117-
if (
118-
not multichoice_continuations_start_space
119-
and not lighteval_config.tasks.no_multichoice_continuations_start_space
120-
):
121-
multichoice_continuations_start_space = None
122-
# multichoice_continuations_start_space can be True (forcing space), False (forcing no space) or None (no forcing)
123-
if (
116+
# multichoice_continuations_start_space can be True (forcing space), False (forcing no space) or None (no forcing)
117+
multichoice_continuations_start_space = boolstring_to_bool(
124118
lighteval_config.tasks.multichoice_continuations_start_space
125-
and lighteval_config.tasks.no_multichoice_continuations_start_space
126-
):
127-
raise ValueError(
128-
"You cannot force both the multichoice continuations to start with a space and not to start with a space"
129-
)
119+
)
130120

131121
self.generation_config = lighteval_config.generation
132122
if isinstance(self.generation_config, dict):
@@ -409,10 +399,11 @@ def _check_continuations_start_space(self, continuation: str) -> str:
409399
- None (Don't touch - default)
410400
Todo: find a way to add this back WITHOUT breaking compatibility with the harness
411401
"""
412-
if self.multichoice_continuations_start_space is True and continuation[0] != " ":
413-
continuation = " " + continuation
414-
if self.multichoice_continuations_start_space is False and continuation[0] == " ":
415-
continuation = continuation.lstrip()
402+
if self.multichoice_continuations_start_space is not None:
403+
if self.multichoice_continuations_start_space and continuation[0] != " ":
404+
continuation = " " + continuation
405+
if not self.multichoice_continuations_start_space and continuation[0] == " ":
406+
continuation = continuation.lstrip()
416407
return continuation
417408

418409
def loglikelihood_single_token(
@@ -443,7 +434,7 @@ def loglikelihood_single_token(
443434
if any(len(c) > 1 for c in continuations_enc):
444435
raise ValueError(
445436
f"Trying to do single token multiple choice but one choice has several tokens: {continuations_enc}. "
446-
"If the additional pre-token is a space, try to set --no_multichoice_continuations_start_space "
437+
"If the additional pre-token is a space, try to set multichoice_continuations_start_space=False in the model parameters "
447438
)
448439
request.tokenized_continuation = continuations_enc
449440

src/lighteval/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,24 @@ def flatten(item: list[Union[list, str]]) -> list[str]:
145145
return flat_item
146146

147147

148+
def boolstring_to_bool(x: Union[str, bool, int]) -> Union[bool, None]:
149+
"""Allows to manage string or bool to bool conversion, in case a configuration input is badly formatted.
150+
151+
Args:
152+
x (str): A string (true, false, True, False, ...)
153+
154+
Returns:
155+
Union[bool, None]: the corresponding boolean
156+
"""
157+
if x in [None, "None", "null", ""]:
158+
return None
159+
if x in ["True", "true", True, 1]:
160+
return True
161+
if x in ["False", "false", False, 0]:
162+
return False
163+
raise ValueError(f"You tried to convert {x} to a boolean but it's not possible.")
164+
165+
148166
def is_accelerate_available() -> bool:
149167
return importlib.util.find_spec("accelerate") is not None
150168

0 commit comments

Comments
 (0)