Skip to content

Commit dbac8d5

Browse files
committed
fix(deploy): pass trust_remote_code=True to load_tokenizer in in-framework deploy
Megatron-Bridge added a load_tokenizer() guard that raises ValueError when the checkpoint tokenizer config has trust_remote_code=True baked in (e.g. Llama tokenizers) but the caller does not opt in. The in-framework deploy path called load_tokenizer(checkpoint_path) without it, breaking Ray and Triton deployments of converted MBridge checkpoints (regression in container 26.06.01.rc0). Adds a unit test covering the patched call (red-green verified). Detected by: hermes PR-to-testcase pipeline NVBug: 6393765 Signed-off-by: Pruthviraj Prakash <pruprakash@nvidia.com>
1 parent da45b11 commit dbac8d5

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

nemo_deploy/llm/inference/inference_base.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,11 @@ def setup_megatron_model_and_tokenizer_for_inference(
265265
megatron_args=mlm_args,
266266
use_cpu_init=False,
267267
)
268-
tokenizer = load_tokenizer(checkpoint_path)
268+
# In-framework deploy operates on a user-supplied checkpoint that is already trusted
269+
# (locally converted and served by the same user), so opt in to the Megatron-Bridge
270+
# load_tokenizer security guard that requires trust_remote_code=True when the checkpoint
271+
# tokenizer config has it baked in (e.g. Llama tokenizers).
272+
tokenizer = load_tokenizer(checkpoint_path, trust_remote_code=True)
269273
return model, tokenizer, mlm_args
270274

271275

tests/unit_tests/deploy/test_inference_base.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,43 @@ def test_attention_backend_conversion_flash(
434434
# Verify that attention_backend was converted to enum
435435
self.assertEqual(mock_config.attention_backend, AttnBackend.flash)
436436

437+
@patch("nemo_deploy.llm.inference.inference_base.torch_distributed_init")
438+
@patch("nemo_deploy.llm.inference.inference_base.load_model_config")
439+
@patch("nemo_deploy.llm.inference.inference_base.initialize_megatron_for_inference")
440+
@patch("nemo_deploy.llm.inference.inference_base.build_and_load_model")
441+
@patch("nemo_deploy.llm.inference.inference_base.load_tokenizer")
442+
def test_load_tokenizer_passes_trust_remote_code(
443+
self, mock_load_tokenizer, mock_build_model, mock_init_megatron, mock_load_config, mock_torch_dist
444+
):
445+
"""Regression (NVBug 6393765): load_tokenizer must be called with trust_remote_code=True.
446+
447+
Megatron-Bridge's load_tokenizer raises ValueError when the checkpoint tokenizer config
448+
has trust_remote_code=True baked in (e.g. Llama tokenizers) but the caller does not opt in.
449+
The in-framework deploy path must pass trust_remote_code=True so Ray/Triton deployments of
450+
converted MBridge checkpoints load the tokenizer instead of failing.
451+
"""
452+
mock_config = MagicMock()
453+
mock_config.attention_backend = None
454+
mock_config.tensor_model_parallel_size = 1
455+
mock_config.pipeline_model_parallel_size = 1
456+
mock_config.context_parallel_size = 1
457+
mock_config.expert_model_parallel_size = 1
458+
459+
mock_mlm_args = MagicMock()
460+
mock_load_config.return_value = (mock_config, mock_mlm_args)
461+
462+
mock_build_model.return_value = [MagicMock()]
463+
mock_load_tokenizer.return_value = MagicMock()
464+
465+
setup_megatron_model_and_tokenizer_for_inference(
466+
checkpoint_path=self.mock_path,
467+
tensor_model_parallel_size=1,
468+
pipeline_model_parallel_size=1,
469+
)
470+
471+
# Pre-fix this was load_tokenizer(checkpoint_path) (no kwarg) and the MB guard raised.
472+
mock_load_tokenizer.assert_called_once_with(self.mock_path, trust_remote_code=True)
473+
437474
@patch("nemo_deploy.llm.inference.inference_base.torch_distributed_init")
438475
@patch("nemo_deploy.llm.inference.inference_base.load_model_config")
439476
@patch("nemo_deploy.llm.inference.inference_base.initialize_megatron_for_inference")

0 commit comments

Comments
 (0)