Add trust_remote_code configuration option (#31)

* Add `trust_remote_code` configuration option and apply it when loading models and tokenizers

* Default `trust_remote_code` to `None` and set it to `True` if previously `None` so the user wouldn't be asked multiple times

* Consistently access `trust_remote_code` from `self.settings` instead of the global `settings` object.

* Introduce `trusted_models` dictionary to manage and confirm `trust_remote_code` settings during model loading

* Assign `trust_remote_code` to `evaluate_model` in `trusted_models` instead of `model`
This commit is contained in:
Nikolai Kolodziej
2025-11-24 01:57:44 +01:00
committed by GitHub
parent b79b8b1475
commit 452b35e7b7
2 changed files with 21 additions and 1 deletions
+5
View File
@@ -46,6 +46,11 @@ class Settings(BaseSettings):
description="Device map to pass to Accelerate when loading the model.", description="Device map to pass to Accelerate when loading the model.",
) )
trust_remote_code: bool | None = Field(
default=None,
description="Whether to trust remote code when loading the model.",
)
batch_size: int = Field( batch_size: int = Field(
default=0, # auto default=0, # auto
description="Number of input sequences to process in parallel (0 = auto).", description="Number of input sequences to process in parallel (0 = auto).",
+16 -1
View File
@@ -39,7 +39,8 @@ class Model:
print(f"Loading model [bold]{settings.model}[/]...") print(f"Loading model [bold]{settings.model}[/]...")
self.tokenizer: PreTrainedTokenizerBase = AutoTokenizer.from_pretrained( self.tokenizer: PreTrainedTokenizerBase = AutoTokenizer.from_pretrained(
settings.model settings.model,
trust_remote_code=settings.trust_remote_code,
) )
# Fallback for tokenizers that don't declare a special pad token. # Fallback for tokenizers that don't declare a special pad token.
@@ -48,6 +49,10 @@ class Model:
self.tokenizer.padding_side = "left" self.tokenizer.padding_side = "left"
self.model = None self.model = None
self.trusted_models = {settings.model: settings.trust_remote_code}
if self.settings.evaluate_model is not None:
self.trusted_models[settings.evaluate_model] = settings.trust_remote_code
for dtype in settings.dtypes: for dtype in settings.dtypes:
print(f"* Trying dtype [bold]{dtype}[/]... ", end="") print(f"* Trying dtype [bold]{dtype}[/]... ", end="")
@@ -57,8 +62,14 @@ class Model:
settings.model, settings.model,
dtype=dtype, dtype=dtype,
device_map=settings.device_map, device_map=settings.device_map,
trust_remote_code=self.trusted_models.get(settings.model),
) )
# If we reach this point and the model requires trust_remote_code,
# the user must have confirmed it.
if self.trusted_models.get(settings.model) is None:
self.trusted_models[settings.model] = True
# A test run can reveal dtype-related problems such as the infamous # A test run can reveal dtype-related problems such as the infamous
# "RuntimeError: probability tensor contains either `inf`, `nan` or element < 0" # "RuntimeError: probability tensor contains either `inf`, `nan` or element < 0"
# (https://github.com/meta-llama/llama/issues/380). # (https://github.com/meta-llama/llama/issues/380).
@@ -93,8 +104,12 @@ class Model:
self.settings.model, self.settings.model,
dtype=dtype, dtype=dtype,
device_map=self.settings.device_map, device_map=self.settings.device_map,
trust_remote_code=self.trusted_models.get(self.settings.model),
) )
if self.trusted_models.get(self.settings.model) is None:
self.trusted_models[self.settings.model] = True
def get_layers(self) -> ModuleList: def get_layers(self) -> ModuleList:
# Most multimodal models. # Most multimodal models.
with suppress(Exception): with suppress(Exception):