Skip to content

Commit caa5177

Browse files
authored
fix: respect trust_remote_code when building AutoConfig (#1007)
1 parent 0a18faf commit caa5177

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

nemo_automodel/recipes/llm/train_ft.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -405,13 +405,24 @@ def build_loss_fn(cfg_loss):
405405
return cfg_loss.instantiate()
406406

407407

408-
def _build_tokenizer(cfg_model, cfg_ds):
409-
def compute_trust_remote_code():
410-
if hasattr(cfg_model, "trust_remote_code"):
411-
return getattr(cfg_model, "trust_remote_code")
412-
return resolve_trust_remote_code(_get_model_name(cfg_model))
408+
def compute_trust_remote_code_from_model(cfg_model):
409+
"""Compute the value of trust_remote_code based on the model configuration.
410+
411+
Args:
412+
cfg_model (ConfigNode): Model configuration.
413413
414-
trust_remote_code = compute_trust_remote_code()
414+
Returns:
415+
bool: Whether to trust remote code.
416+
"""
417+
if hasattr(cfg_model, "trust_remote_code"):
418+
return getattr(cfg_model, "trust_remote_code")
419+
elif hasattr(cfg_model, "config") and hasattr(cfg_model.config, "trust_remote_code"):
420+
return getattr(cfg_model.config, "trust_remote_code")
421+
return resolve_trust_remote_code(_get_model_name(cfg_model))
422+
423+
424+
def _build_tokenizer(cfg_model, cfg_ds):
425+
trust_remote_code = compute_trust_remote_code_from_model(cfg_model)
415426
# if tokenizer is not provided, use the model config to instantiate it
416427
if "tokenizer" not in cfg_ds and _get_model_name(cfg_model) is not None:
417428
logging.info("Using model config to instantiate tokenizer")
@@ -592,7 +603,9 @@ def build_dataloader(
592603
if pp_enabled:
593604
from nemo_automodel.components.datasets.utils import add_causal_masks_to_batch
594605

595-
hf_model_config = AutoConfig.from_pretrained(_get_model_name(cfg_model))
606+
hf_model_config = AutoConfig.from_pretrained(
607+
_get_model_name(cfg_model), trust_remote_code=compute_trust_remote_code_from_model(cfg_model)
608+
)
596609

597610
if "collate_fn" in dl_kwargs:
598611
# Case 1: PP enabled + collate_fn exists -> chain them

tests/unit_tests/recipes/test_train_ft.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
build_dataloader,
2929
build_model_and_optimizer,
3030
build_validation_dataloader,
31+
compute_trust_remote_code_from_model,
3132
)
3233
from torch.utils.data import IterableDataset
3334

@@ -632,3 +633,36 @@ def _build_model_and_optimizer_stub(*args, **kwargs):
632633
(parts[0], "PipelineStage_0"),
633634
(parts[1], "PipelineStage_1"),
634635
]
636+
637+
638+
def test_compute_trust_remote_code_prefers_cfg_flag():
639+
cfg_model = ConfigNode({"trust_remote_code": False, "pretrained_model_name_or_path": "ignored"})
640+
641+
with patch("nemo_automodel.recipes.llm.train_ft.resolve_trust_remote_code") as mock_resolve:
642+
result = compute_trust_remote_code_from_model(cfg_model)
643+
644+
assert result is False
645+
mock_resolve.assert_not_called()
646+
647+
648+
def test_compute_trust_remote_code_prefers_nested_config():
649+
cfg_model = ConfigNode({"config": {"trust_remote_code": True}})
650+
651+
with patch("nemo_automodel.recipes.llm.train_ft.resolve_trust_remote_code") as mock_resolve:
652+
result = compute_trust_remote_code_from_model(cfg_model)
653+
654+
assert result is True
655+
mock_resolve.assert_not_called()
656+
657+
658+
def test_compute_trust_remote_code_falls_back_to_resolve():
659+
cfg_model = ConfigNode({"pretrained_model_name_or_path": "nvidia/foo"})
660+
661+
with patch(
662+
"nemo_automodel.recipes.llm.train_ft.resolve_trust_remote_code",
663+
return_value=True,
664+
) as mock_resolve:
665+
result = compute_trust_remote_code_from_model(cfg_model)
666+
667+
assert result is True
668+
mock_resolve.assert_called_once_with("nvidia/foo")

0 commit comments

Comments
 (0)