-
Notifications
You must be signed in to change notification settings - Fork 382
Expand file tree
/
Copy pathtest_auto_bridge.py
More file actions
1729 lines (1408 loc) · 75 KB
/
Copy pathtest_auto_bridge.py
File metadata and controls
1729 lines (1408 loc) · 75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Unit tests for AutoBridge automatic bridge selection and bridge functionality.
"""
import json
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import Mock, PropertyMock, patch
import pytest
import torch
from transformers import LlamaConfig
from transformers.configuration_utils import PretrainedConfig
from megatron.bridge.models.conversion.auto_bridge import (
AutoBridge,
_config_disables_mtp,
_drop_readonly_config_properties,
_model_omits_mtp,
_mtp_source_key_prefixes,
_saved_config_disables_mtp,
)
from megatron.bridge.models.gpt_provider import GPTModelProvider
from megatron.bridge.models.hf_pretrained.causal_lm import PreTrainedCausalLM
from megatron.bridge.models.hf_pretrained.state import SafeTensorsStateSource
def create_mock_pretrained_causal_lm():
"""Helper function to create a mock PreTrainedCausalLM that passes isinstance checks."""
class MockPreTrainedCausalLM(PreTrainedCausalLM):
def __init__(self):
pass # Skip actual initialization
return MockPreTrainedCausalLM()
def _make_fake_source(present):
"""Build a ``SafeTensorsStateSource`` stand-in for ``save_hf_weights`` tests.
Uses ``Mock(spec=...)`` so the ``isinstance(source, SafeTensorsStateSource)``
gate in ``save_hf_weights`` stays satisfied without bypassing the real
``__init__``. ``has_glob`` reports which source-key globs exist; the captured
``save_generator`` kwargs are exposed on ``source.save_generator_kwargs`` for
assertions.
"""
source = Mock(spec=SafeTensorsStateSource)
source.save_generator_kwargs = None
source.has_glob.side_effect = lambda pattern: pattern in present
def _capture_save_generator(generator, path, **kwargs):
source.save_generator_kwargs = kwargs
source.save_generator.side_effect = _capture_save_generator
return source
class TestAutoBridge:
"""Test cases for AutoBridge automatic selection and full bridge functionality."""
@pytest.fixture
def llama_config(self):
"""Create a sample Llama configuration matching the provided example."""
return {
"architectures": ["LlamaForCausalLM"],
"attention_bias": False,
"attention_dropout": 0.0,
"bos_token_id": 128000,
"eos_token_id": 128001,
"head_dim": 64,
"hidden_act": "silu",
"hidden_size": 2048,
"initializer_range": 0.02,
"intermediate_size": 8192,
"max_position_embeddings": 131072,
"mlp_bias": False,
"model_type": "llama",
"num_attention_heads": 32,
"num_hidden_layers": 16,
"num_key_value_heads": 8,
"pretraining_tp": 1,
"rms_norm_eps": 1e-05,
"rope_scaling": {
"factor": 32.0,
"high_freq_factor": 4.0,
"low_freq_factor": 1.0,
"original_max_position_embeddings": 8192,
"rope_type": "llama3",
},
"rope_theta": 500000.0,
"tie_word_embeddings": True,
"torch_dtype": "bfloat16",
"transformers_version": "4.45.0.dev0",
"use_cache": True,
"vocab_size": 128256,
}
@pytest.fixture
def llama_config_mock(self):
"""Create a mock Llama configuration."""
config = Mock()
config.architectures = ["LlamaForCausalLM"]
config.model_type = "llama"
config.vocab_size = 32000
config.hidden_size = 2048
config.num_hidden_layers = 16
config.num_attention_heads = 32
return config
@pytest.fixture
def bert_config(self):
"""Create a mock BERT configuration (unsupported)."""
config = Mock()
config.architectures = ["BertForMaskedLM"]
config.model_type = "bert"
return config
@pytest.fixture
def gpt2_config(self):
"""Create a mock GPT2 configuration."""
config = Mock()
config.architectures = ["GPT2ForCausalLM", "GPT2LMHeadModel"]
config.model_type = "gpt2"
return config
def test_from_hf_pretrained_with_unsupported_model(self, bert_config):
"""Test AutoBridge raises ValueError for unsupported models."""
with patch(
"megatron.bridge.models.conversion.auto_bridge.safe_load_config_with_retry"
) as mock_safe_load_config:
# Setup mocks
mock_safe_load_config.return_value = bert_config
# Should raise ValueError
with pytest.raises(ValueError) as exc_info:
AutoBridge.from_hf_pretrained("bert-base-uncased")
assert "Model architecture not supported by AutoBridge" in str(exc_info.value)
assert "BertForMaskedLM" in str(exc_info.value)
def test_drop_readonly_config_properties(self):
"""Test auto-config synthesis drops properties HuggingFace configs cannot set."""
class CustomConfig(PretrainedConfig):
@property
def layers_block_type(self):
return ["mamba", "attention"]
config_dict = {
"hidden_size": 768,
"layers_block_type": ["mamba", "attention"],
"num_hidden_layers": 2,
}
filtered = _drop_readonly_config_properties(config_dict, CustomConfig)
assert filtered == {
"hidden_size": 768,
"num_hidden_layers": 2,
}
assert config_dict["layers_block_type"] == ["mamba", "attention"]
def test_from_pretrained_config_load_failure(self):
"""Test AutoBridge handles config loading failures gracefully."""
with patch(
"megatron.bridge.models.conversion.auto_bridge.safe_load_config_with_retry"
) as mock_safe_load_config:
# Setup mock to raise exception
mock_safe_load_config.side_effect = ValueError("Failed to load configuration: Config not found")
# Should raise ValueError with helpful message
with pytest.raises(ValueError) as exc_info:
AutoBridge.from_hf_pretrained("invalid/path")
assert "Failed to load configuration" in str(exc_info.value)
assert "Config not found" in str(exc_info.value)
def test_mtp_disabled_helpers(self, tmp_path):
"""Detect disabled MTP in object, nested, and saved HF configs."""
assert _config_disables_mtp(None) is False
assert _config_disables_mtp(Mock(num_nextn_predict_layers=0)) is True
assert _config_disables_mtp({"num_nextn_predict_layers": None, "text_config": {"mtp_num_layers": "0"}}) is True
assert _config_disables_mtp({"text_config": {"mtp_num_hidden_layers": 0}}) is True
assert _config_disables_mtp(Mock(num_nextn_predict_layers=1)) is False
assert _config_disables_mtp({"mtp_num_layers": "2"}) is False
assert _saved_config_disables_mtp(tmp_path) is False
with open(tmp_path / "config.json", "w") as f:
json.dump({"num_nextn_predict_layers": 0}, f)
assert _saved_config_disables_mtp(tmp_path) is True
def test_model_omits_mtp(self):
"""A built model with a falsy mtp_num_layers has no MTP head."""
assert _model_omits_mtp(None) is False
# Unset attribute -> unknown -> do not assume omitted.
assert _model_omits_mtp(SimpleNamespace()) is False
# SkyRL forces mtp_num_layers=None -> head omitted from export.
assert _model_omits_mtp(Mock(mtp_num_layers=None)) is True
assert _model_omits_mtp(Mock(mtp_num_layers=0)) is True
assert _model_omits_mtp(Mock(mtp_num_layers=1)) is False
def test_mtp_source_key_prefixes(self):
"""Resolve the MTP/nextn source-key prefixes to strip per architecture."""
def src(*present_globs):
present = set(present_globs)
return Mock(has_glob=lambda pattern: pattern in present)
# DeepSeek-style: dedicated mtp.* prefix.
assert _mtp_source_key_prefixes(src("mtp.*"), {}) == ("mtp.",)
# GLM glm4_moe_lite: nextn layer stored at index == num_hidden_layers.
glm_src = src("model.layers.47.*")
assert _mtp_source_key_prefixes(glm_src, {"num_hidden_layers": 47}) == ("model.layers.47.",)
# Nested text_config carries num_hidden_layers.
assert _mtp_source_key_prefixes(glm_src, {"text_config": {"num_hidden_layers": 47}}) == ("model.layers.47.",)
# No matching source keys -> nothing to strip.
assert _mtp_source_key_prefixes(src(), {"num_hidden_layers": 47}) == ()
# Both prefixes present.
both = src("mtp.*", "model.layers.47.*")
assert _mtp_source_key_prefixes(both, {"num_hidden_layers": 47}) == ("mtp.", "model.layers.47.")
def test_save_hf_weights_strips_nextn_prefix_when_mtp_omitted(self, tmp_path):
"""Regression: a model built without an MTP head must strip the GLM nextn
layer prefix from the source map before streaming save.
This is the actual bug being fixed (45/48-shard checkpoint dropping
boundary shards on GLM-4.x glm4_moe_lite). Unlike the helper-level tests,
this asserts the orchestration in ``save_hf_weights`` wires the stripped
prefixes through to ``save_generator``. It fails if the
``_model_omits_mtp(...)`` branch is removed, because the HF/saved configs
here do *not* explicitly disable MTP — the only signal is the built
model omitting the head.
"""
source = _make_fake_source(present={"model.layers.47.*", "model.layers.46.*"})
# Built megatron model omits the MTP head (SkyRL forces mtp_num_layers=None).
self._run_save_hf_weights(source, tmp_path, mtp_num_layers=None)
assert source.save_generator_kwargs is not None
assert source.save_generator_kwargs["ignored_source_key_prefixes"] == ("model.layers.47.",)
def test_save_hf_weights_keeps_all_keys_when_mtp_enabled(self, tmp_path):
"""Counterpart: when the model keeps its MTP head, nothing is stripped.
Also guards the ``if mtp_disabled`` gate: if a future refactor drops the
gate and always calls ``_mtp_source_key_prefixes``, the helper would strip
the real ``model.layers.47.`` layer here and this assertion would fail.
"""
source = _make_fake_source(present={"model.layers.47.*"})
self._run_save_hf_weights(source, tmp_path, mtp_num_layers=1)
assert source.save_generator_kwargs["ignored_source_key_prefixes"] is None
def _run_save_hf_weights(self, source, tmp_path, *, mtp_num_layers):
"""Drive ``save_hf_weights`` with a stubbed bridge/model so the only
behavior under test is the MTP prefix-resolution wiring.
``num_hidden_layers=47`` with no MTP-disable field means the export
decision hinges purely on whether the *built* model omits the head
(``mtp_num_layers``).
"""
hf_pretrained = create_mock_pretrained_causal_lm()
# HF config carries layer count but does NOT set any MTP-disable field.
hf_pretrained.config = SimpleNamespace(num_hidden_layers=47)
model_instance = SimpleNamespace(config=SimpleNamespace(mtp_num_layers=mtp_num_layers))
bridge_obj = object.__new__(AutoBridge)
bridge_obj.hf_pretrained = hf_pretrained
fake_model_bridge = Mock()
fake_model_bridge.stream_weights_megatron_to_hf.return_value = iter([])
with (
# ``state`` is a read-only property on PreTrainedBase, so patch it
# rather than assigning to the instance.
patch.object(
type(hf_pretrained),
"state",
new_callable=PropertyMock,
return_value=SimpleNamespace(source=source),
),
patch.object(AutoBridge, "_model_bridge", new_callable=PropertyMock) as mock_bridge,
patch.object(AutoBridge, "_get_model_instance", return_value=model_instance),
patch("megatron.bridge.models.conversion.auto_bridge.is_quantized", return_value=False),
):
mock_bridge.return_value = fake_model_bridge
bridge_obj.save_hf_weights([Mock()], tmp_path, show_progress=False)
def test_can_handle_supported_model(self, llama_config_mock):
"""Test can_handle returns True for supported models."""
with patch(
"megatron.bridge.models.conversion.auto_bridge.safe_load_config_with_retry"
) as mock_safe_load_config:
mock_safe_load_config.return_value = llama_config_mock
assert AutoBridge.can_handle("meta-llama/Meta-Llama-3-8B") is True
mock_safe_load_config.assert_called_with("meta-llama/Meta-Llama-3-8B", trust_remote_code=False)
def test_can_handle_unsupported_model(self, bert_config):
"""Test can_handle returns False for unsupported models."""
with patch(
"megatron.bridge.models.conversion.auto_bridge.safe_load_config_with_retry"
) as mock_safe_load_config:
mock_safe_load_config.return_value = bert_config
assert AutoBridge.can_handle("bert-base-uncased") is False
def test_can_handle_invalid_path(self):
"""Test can_handle returns False for invalid paths."""
with patch(
"megatron.bridge.models.conversion.auto_bridge.safe_load_config_with_retry"
) as mock_safe_load_config:
mock_safe_load_config.side_effect = Exception("Not found")
assert AutoBridge.can_handle("invalid/path") is False
# Test core bridge functionality (from original AutoBridge tests)
def test_from_hf_pretrained_with_model_id(self):
"""Test from_hf_pretrained with model ID string."""
# This test checks that from_hf_pretrained creates correct bridge instance
# We'll use a mock pretrained model
mock_model = Mock(spec=PreTrainedCausalLM)
mock_config = Mock(spec=PretrainedConfig)
mock_config.architectures = ["GPT2LMHeadModel"] # Use a real architecture
mock_model.config = mock_config
with patch(
"megatron.bridge.models.conversion.auto_bridge.PreTrainedCausalLM.from_pretrained"
) as mock_from_pretrained:
# Set up the from_pretrained class method properly
mock_from_pretrained.return_value = mock_model
with patch(
"megatron.bridge.models.conversion.auto_bridge.safe_load_config_with_retry"
) as mock_safe_load_config:
mock_safe_load_config.return_value = mock_config
# Skip architecture validation for this test
with patch.object(AutoBridge, "_validate_config"):
# Call from_hf_pretrained
model_id = "gpt2"
result = AutoBridge.from_hf_pretrained(model_id, trust_remote_code=True)
# Assertions
assert isinstance(result, AutoBridge)
assert result.hf_pretrained == mock_model
mock_from_pretrained.assert_called_once_with(model_id, trust_remote_code=True)
def test_from_pretrained_with_additional_kwargs(self):
"""Test from_pretrained with various kwargs."""
# Setup mocks
mock_model = Mock(spec=PreTrainedCausalLM)
mock_config = Mock(spec=PretrainedConfig)
mock_config.architectures = ["GPT2LMHeadModel"]
mock_model.config = mock_config
with patch(
"megatron.bridge.models.conversion.auto_bridge.PreTrainedCausalLM.from_pretrained"
) as mock_from_pretrained:
# Set up the from_pretrained class method properly
mock_from_pretrained.return_value = mock_model
with patch(
"megatron.bridge.models.conversion.auto_bridge.safe_load_config_with_retry"
) as mock_safe_load_config:
mock_safe_load_config.return_value = mock_config
# Skip architecture validation for this test
with patch.object(AutoBridge, "_validate_config"):
# Call with multiple kwargs
result = AutoBridge.from_hf_pretrained(
"model-id",
torch_dtype=torch.bfloat16,
low_cpu_mem_usage=True,
attn_implementation="flash_attention_2",
)
# Assertions
assert isinstance(result, AutoBridge)
assert result.hf_pretrained == mock_model
mock_from_pretrained.assert_called_once_with(
"model-id",
torch_dtype=torch.bfloat16,
low_cpu_mem_usage=True,
attn_implementation="flash_attention_2",
)
def test_to_megatron_provider_basic(self, llama_config):
"""Test basic to_megatron_provider conversion."""
# Setup mocks
mock_hf_model = Mock(spec=PreTrainedCausalLM)
mock_hf_model.config = LlamaConfig(**llama_config)
# Mock model bridge
mock_model_bridge = Mock()
mock_provider = Mock(spec=GPTModelProvider)
mock_model_bridge.provider_bridge.return_value = mock_provider
with patch.object(AutoBridge, "_model_bridge", mock_model_bridge):
# Create bridge and convert
bridge = AutoBridge(mock_hf_model)
result = bridge.to_megatron_provider(load_weights=False)
# Assertions
assert result == mock_provider
mock_model_bridge.provider_bridge.assert_called_once_with(mock_hf_model)
def test_to_megatron_provider_with_different_model_types(self):
"""Test to_megatron_provider with different model architectures."""
# Test with GPT2 model
mock_gpt2_model = Mock(spec=PreTrainedCausalLM)
mock_gpt2_model.config = Mock(model_type="gpt2")
# Mock model bridge
mock_model_bridge = Mock()
mock_provider = Mock(spec=GPTModelProvider)
mock_model_bridge.provider_bridge.return_value = mock_provider
with patch.object(AutoBridge, "_model_bridge", mock_model_bridge):
bridge = AutoBridge(mock_gpt2_model)
result = bridge.to_megatron_provider(load_weights=False)
assert result == mock_provider
mock_model_bridge.provider_bridge.assert_called_once_with(mock_gpt2_model)
def test_to_megatron_provider_with_custom_kwargs(self, llama_config):
"""Test to_megatron_provider with custom keyword arguments."""
# Setup mocks
mock_hf_model = Mock(spec=PreTrainedCausalLM)
mock_hf_model.config = LlamaConfig(**llama_config)
# Mock model bridge
mock_model_bridge = Mock()
mock_provider = Mock(spec=GPTModelProvider)
mock_provider.register_pre_wrap_hook = Mock()
mock_model_bridge.provider_bridge.return_value = mock_provider
mock_model_bridge.load_weights_hf_to_megatron = Mock()
with patch.object(AutoBridge, "_model_bridge", mock_model_bridge):
# Create bridge and convert with load_weights=True
bridge = AutoBridge(mock_hf_model)
result = bridge.to_megatron_provider(load_weights=True)
# Assertions
assert result == mock_provider
mock_model_bridge.provider_bridge.assert_called_once_with(mock_hf_model)
# Check that a pre-wrap hook was registered for loading weights
mock_provider.register_pre_wrap_hook.assert_called_once()
def test_to_megatron_provider_sets_hf_model_id_from_path(self):
"""to_megatron_provider tags providers with the provided HF path."""
mock_hf_model = Mock(spec=PreTrainedCausalLM)
mock_model_bridge = Mock()
mock_provider = Mock(spec=GPTModelProvider)
mock_provider.hf_model_id = None
mock_model_bridge.provider_bridge.return_value = mock_provider
with patch.object(AutoBridge, "_model_bridge", mock_model_bridge):
bridge = AutoBridge(mock_hf_model)
provider = bridge.to_megatron_provider(load_weights=False, hf_path="local/hf/path")
assert provider.hf_model_id == "local/hf/path"
def test_to_megatron_provider_sets_hf_model_id_from_pretrained(self):
"""to_megatron_provider falls back to HF model name_or_path."""
mock_hf_model = Mock(spec=PreTrainedCausalLM)
type(mock_hf_model).model_name_or_path = PropertyMock(return_value="hf/model-id")
mock_model_bridge = Mock()
mock_provider = Mock(spec=GPTModelProvider)
mock_provider.hf_model_id = None
mock_model_bridge.provider_bridge.return_value = mock_provider
with patch.object(AutoBridge, "_model_bridge", mock_model_bridge):
bridge = AutoBridge(mock_hf_model)
provider = bridge.to_megatron_provider(load_weights=False)
assert provider.hf_model_id == "hf/model-id"
def test_get_hf_model_id_from_checkpoint_delegates(self):
"""AutoBridge helper delegates to checkpoint utilities."""
with patch(
"megatron.bridge.training.utils.checkpoint_utils.get_hf_model_id_from_checkpoint",
return_value="delegated/model",
) as mock_infer:
result = AutoBridge.get_hf_model_id_from_checkpoint("/tmp/checkpoint")
assert result == "delegated/model"
mock_infer.assert_called_once_with("/tmp/checkpoint")
def test_to_megatron_provider_error_handling(self):
"""Test to_megatron_provider error handling."""
# Setup mock to raise an exception
mock_hf_model = Mock(spec=PreTrainedCausalLM)
# Mock model bridge to raise error
mock_model_bridge = Mock()
mock_model_bridge.provider_bridge.side_effect = ValueError("Unsupported model type")
with patch.object(AutoBridge, "_model_bridge", mock_model_bridge):
bridge = AutoBridge(mock_hf_model)
# Should propagate the exception
with pytest.raises(ValueError, match="Unsupported model type"):
bridge.to_megatron_provider()
def test_bridge_instance_creation(self):
"""Test AutoBridge instance creation."""
# Test with PreTrainedCausalLM
mock_model = Mock(spec=PreTrainedCausalLM)
bridge = AutoBridge(mock_model)
# Should have the expected methods
assert hasattr(bridge, "from_hf_pretrained")
assert hasattr(bridge, "to_megatron_provider")
assert hasattr(bridge, "load_hf_weights")
assert hasattr(bridge, "export_hf_weights")
assert hasattr(bridge, "save_hf_pretrained")
assert bridge.hf_pretrained == mock_model
# Test with PretrainedConfig
mock_config = Mock(spec=PretrainedConfig)
bridge_config = AutoBridge(mock_config)
assert bridge_config.hf_pretrained == mock_config
# Test with invalid type
with pytest.raises(
ValueError,
match="hf_pretrained must be a PreTrainedCausalLM or PretrainedConfig instance",
):
AutoBridge("invalid")
# from_hf_pretrained should be a classmethod
import inspect
assert inspect.ismethod(AutoBridge.from_hf_pretrained)
def test_from_hf_config(self):
"""Test creating bridge from config only."""
# Create a mock config
config = Mock(spec=PretrainedConfig)
config.architectures = ["GPT2LMHeadModel"]
# Skip architecture validation for this test
with patch.object(AutoBridge, "_validate_config"):
bridge = AutoBridge.from_hf_config(config)
assert isinstance(bridge, AutoBridge)
assert bridge.hf_pretrained == config
def test_from_hf_config_invalid_architecture(self):
"""Test from_hf_config with unsupported architecture."""
config = Mock(spec=PretrainedConfig)
config.architectures = ["BertForMaskedLM"] # Not a CausalLM
with pytest.raises(ValueError, match="Model architecture not supported by AutoBridge"):
AutoBridge.from_hf_config(config)
def test_from_auto_config_happy_path(self, tmp_path):
"""from_auto_config synthesizes config and tags bridge with source model id."""
ckpt_dir = tmp_path / "ckpt"
ckpt_dir.mkdir()
(ckpt_dir / "run_config.yaml").write_text("dummy: true\n")
mock_hf_cfg = Mock()
mock_hf_cfg.to_dict.return_value = {"vocab_size": 32000}
first_bridge = Mock()
first_bridge._model_bridge.megatron_to_hf_config.return_value = {"vocab_size": 64000}
second_bridge = Mock()
hf_model_id = "nvidia/NVIDIA-Nemotron-3-Nano-30B-A3B-BF16"
with patch("transformers.AutoConfig.from_pretrained", return_value=mock_hf_cfg) as mock_auto_cfg:
with patch(
"megatron.bridge.training.model_load_save.load_model_config",
return_value=(Mock(name="megatron_cfg"), None),
) as mock_load_cfg:
with patch(
"megatron.bridge.models.conversion.utils.conform_config_to_reference",
return_value={"vocab_size": 64000},
) as mock_conform:
with patch.object(AutoBridge, "from_hf_config", side_effect=[first_bridge, second_bridge]):
bridge = AutoBridge.from_auto_config(str(ckpt_dir), hf_model_id)
assert bridge is second_bridge
assert second_bridge.hf_model_id == hf_model_id
mock_auto_cfg.assert_called_once_with(hf_model_id, trust_remote_code=False)
mock_load_cfg.assert_called_once_with(str(ckpt_dir))
mock_conform.assert_called_once_with({"vocab_size": 64000}, {"vocab_size": 32000})
def test_from_auto_config_uses_latest_iter_run_config(self, tmp_path):
"""from_auto_config falls back to latest iter_* directory for run_config.yaml."""
ckpt_dir = tmp_path / "ckpt"
ckpt_dir.mkdir()
(ckpt_dir / "iter_0000001").mkdir()
iter_latest = ckpt_dir / "iter_0000003"
iter_latest.mkdir()
(iter_latest / "run_config.yaml").write_text("dummy: true\n")
mock_hf_cfg = Mock()
mock_hf_cfg.to_dict.return_value = {"vocab_size": 32000}
first_bridge = Mock()
first_bridge._model_bridge.megatron_to_hf_config.return_value = {"vocab_size": 64000}
second_bridge = Mock()
with patch("transformers.AutoConfig.from_pretrained", return_value=mock_hf_cfg):
with patch(
"megatron.bridge.training.model_load_save.load_model_config",
return_value=(Mock(name="megatron_cfg"), None),
) as mock_load_cfg:
with patch(
"megatron.bridge.models.conversion.utils.conform_config_to_reference",
return_value={"vocab_size": 64000},
):
with patch.object(AutoBridge, "from_hf_config", side_effect=[first_bridge, second_bridge]):
AutoBridge.from_auto_config(str(ckpt_dir), "nvidia/NVIDIA-Nemotron-3-Nano-30B-A3B-BF16")
mock_load_cfg.assert_called_once_with(str(iter_latest))
def test_from_auto_config_missing_checkpoint_path(self):
"""from_auto_config fails with clear message for nonexistent checkpoint root."""
with pytest.raises(FileNotFoundError, match="Megatron checkpoint not found"):
AutoBridge.from_auto_config("/definitely/not/a/path", "nvidia/NVIDIA-Nemotron-3-Nano-30B-A3B-BF16")
def test_from_auto_config_missing_run_config(self, tmp_path):
"""from_auto_config fails if no run_config.yaml is found."""
ckpt_dir = tmp_path / "ckpt"
ckpt_dir.mkdir()
(ckpt_dir / "iter_0000001").mkdir()
with pytest.raises(FileNotFoundError, match="Could not find run_config.yaml"):
AutoBridge.from_auto_config(str(ckpt_dir), "nvidia/NVIDIA-Nemotron-3-Nano-30B-A3B-BF16")
def test_supports_method(self):
"""Test the supports class method."""
# Supported config
config = Mock()
config.architectures = ["LlamaForCausalLM"]
assert AutoBridge.supports(config) is True
# Multiple architectures, one supported
config.architectures = ["LlamaModel", "LlamaForCausalLM"]
assert AutoBridge.supports(config) is True
# No CausalLM architecture
config.architectures = ["BertForMaskedLM"]
assert AutoBridge.supports(config) is False
# No architectures
config.architectures = []
assert AutoBridge.supports(config) is False
# Missing architectures attribute
config_no_arch = Mock(spec=[])
assert AutoBridge.supports(config_no_arch) is False
def test_list_supported_models(self):
"""Test listing supported models."""
# Since this method looks at internal dispatch registry,
# we'll just test that it returns a list
with patch("megatron.bridge.models.conversion.auto_bridge.model_bridge") as mock_bridge:
# Mock to avoid AttributeError
mock_bridge.get_model_bridge = Mock()
mock_bridge.get_model_bridge._exact_types = {}
supported = AutoBridge.list_supported_models()
assert isinstance(supported, list)
# The list might be empty if no models are registered in test environment
def test_load_hf_weights(self):
"""Test loading weights into a Megatron model."""
# Setup mocks
mock_hf_model = Mock(spec=PreTrainedCausalLM)
mock_config = Mock(spec=PretrainedConfig)
mock_hf_model.config = mock_config
mock_megatron_model = [Mock()] # List of model instances
mock_model_bridge = Mock()
mock_model_bridge.load_weights_hf_to_megatron = Mock()
with patch.object(AutoBridge, "_model_bridge", mock_model_bridge):
bridge = AutoBridge(mock_hf_model)
bridge.load_hf_weights(mock_megatron_model)
mock_model_bridge.load_weights_hf_to_megatron.assert_called_once_with(
mock_hf_model, mock_megatron_model, allowed_mismatched_params=None
)
def test_load_hf_weights_with_allowed_mismatched_params(self):
"""Test loading weights with allowed_mismatched_params."""
# Setup mocks
mock_hf_model = Mock(spec=PreTrainedCausalLM)
mock_config = Mock(spec=PretrainedConfig)
mock_hf_model.config = mock_config
mock_megatron_model = [Mock()]
mock_model_bridge = Mock()
mock_model_bridge.load_weights_hf_to_megatron = Mock()
with patch.object(AutoBridge, "_model_bridge", mock_model_bridge):
bridge = AutoBridge(mock_hf_model)
whitelist = ["*.bias", "layer.1.weight"]
bridge.load_hf_weights(mock_megatron_model, allowed_mismatched_params=whitelist)
mock_model_bridge.load_weights_hf_to_megatron.assert_called_once_with(
mock_hf_model, mock_megatron_model, allowed_mismatched_params=whitelist
)
def test_load_hf_weights_from_path(self):
"""Test loading weights from a different path."""
# Setup mocks
mock_hf_model = Mock(spec=PreTrainedCausalLM)
mock_config = Mock(spec=PretrainedConfig)
mock_hf_model.config = mock_config
mock_megatron_model = [Mock()]
mock_model_bridge = Mock()
mock_model_bridge.load_weights_hf_to_megatron = Mock()
# Create bridge first, then patch the from_pretrained method
with patch.object(AutoBridge, "_model_bridge", mock_model_bridge):
bridge = AutoBridge(mock_hf_model)
# Now patch the from_pretrained method
with patch(
"megatron.bridge.models.conversion.auto_bridge.PreTrainedCausalLM.from_pretrained"
) as mock_from_pretrained:
mock_loaded_model = Mock(spec=PreTrainedCausalLM)
mock_from_pretrained.return_value = mock_loaded_model
bridge.load_hf_weights(mock_megatron_model, "./custom_model")
mock_from_pretrained.assert_called_once_with("./custom_model", trust_remote_code=False)
mock_model_bridge.load_weights_hf_to_megatron.assert_called_once_with(
mock_loaded_model,
mock_megatron_model,
allowed_mismatched_params=None,
)
def test_load_hf_weights_no_path_config_only(self):
"""Test load_hf_weights fails when bridge has config only and no path provided."""
mock_config = Mock(spec=PretrainedConfig)
bridge = AutoBridge(mock_config)
with pytest.raises(
ValueError,
match="hf_path is required when hf_pretrained is not a PreTrainedCausalLM",
):
bridge.load_hf_weights([Mock()])
@patch("torch.distributed.get_rank", return_value=0)
@patch("torch.distributed.barrier")
@patch("torch.distributed.is_available", return_value=True)
@patch("torch.distributed.is_initialized", return_value=True)
def test_save_hf_pretrained(self, mock_is_init, mock_is_avail, mock_barrier, mock_get_rank):
"""Test saving a model in HuggingFace format."""
# Setup mocks
mock_hf_model = Mock(spec=PreTrainedCausalLM)
mock_hf_model.save_artifacts = Mock()
mock_hf_model.state = Mock()
mock_hf_model.state.source = Mock(spec=["save_generator"])
from megatron.bridge.models.hf_pretrained.state import SafeTensorsStateSource
mock_hf_model.state.source = Mock(spec=SafeTensorsStateSource)
mock_hf_model.state.source.save_generator = Mock()
mock_megatron_model = [Mock()]
with patch.object(AutoBridge, "save_hf_weights") as mock_save_hf_weights:
bridge = AutoBridge(mock_hf_model)
# Mock _model_bridge to have no ADDITIONAL_FILE_PATTERNS
with patch.object(
type(bridge),
"_model_bridge",
PropertyMock(return_value=Mock(ADDITIONAL_FILE_PATTERNS=None)),
):
bridge.save_hf_pretrained(mock_megatron_model, "./output_dir")
# Check artifacts were saved on rank 0
mock_hf_model.save_artifacts.assert_called_once_with(
"./output_dir", original_source_path=None, additional_files=None
)
mock_save_hf_weights.assert_called_once_with(
mock_megatron_model,
"./output_dir",
True,
True,
merge_adapter_weights=True,
distributed_save=False,
save_every_n_ranks=1,
)
@patch("torch.distributed.is_initialized", return_value=False)
@patch("torch.distributed.is_available", return_value=False)
def test_save_hf_pretrained_config_only(self, _mock_dist_avail, _mock_dist_init, tmp_path):
"""Config-only save writes config.json, calls save_hf_weights, and tolerates missing hub files."""
bridge = AutoBridge(PretrainedConfig())
bridge.hf_model_id = "some-org/some-model"
with patch.object(AutoBridge, "save_hf_weights") as mock_save_hf_weights:
with patch("huggingface_hub.list_repo_files", return_value=["config.json", "README.md"]):
with patch("huggingface_hub.hf_hub_download") as mock_download:
bridge.save_hf_pretrained([Mock()], str(tmp_path))
assert (tmp_path / "config.json").exists()
mock_download.assert_not_called()
mock_save_hf_weights.assert_called_once()
@patch("torch.distributed.is_initialized", return_value=False)
@patch("torch.distributed.is_available", return_value=False)
def test_save_hf_pretrained_config_only_strips_auto_map_without_remote_code(
self, _mock_dist_avail, _mock_dist_init, tmp_path
):
"""Config-only save omits stale remote-code metadata when remote code is not preserved."""
config = PretrainedConfig()
config.auto_map = {
"AutoConfig": "configuration_custom.CustomConfig",
"AutoModelForCausalLM": "modeling_custom.CustomForCausalLM",
}
bridge = AutoBridge(config)
bridge.hf_model_id = "some-org/some-model"
with patch.object(AutoBridge, "save_hf_weights"):
with patch("huggingface_hub.list_repo_files") as mock_list_repo_files:
bridge.save_hf_pretrained([Mock()], str(tmp_path))
saved_config = json.loads((tmp_path / "config.json").read_text())
assert "auto_map" not in saved_config
mock_list_repo_files.assert_not_called()
@patch("torch.distributed.is_initialized", return_value=False)
@patch("torch.distributed.is_available", return_value=False)
def test_save_hf_pretrained_config_only_preserves_auto_map_with_remote_code(
self, _mock_dist_avail, _mock_dist_init, tmp_path
):
"""Config-only save keeps auto_map and copies code when remote code is preserved."""
config = PretrainedConfig()
config.auto_map = {
"AutoConfig": "configuration_custom.CustomConfig",
"AutoModelForCausalLM": "modeling_custom.CustomForCausalLM",
}
bridge = AutoBridge(config)
bridge.hf_model_id = "some-org/some-model"
bridge.trust_remote_code = True
def fake_hf_hub_download(repo_id, filename, local_dir):
del repo_id
local_dir = Path(local_dir)
(local_dir / filename).write_text("# custom modeling code")
metadata_dir = local_dir / ".cache" / "huggingface" / "download"
metadata_dir.mkdir(parents=True)
(metadata_dir / f"{filename}.metadata").write_text("metadata")
with patch.object(AutoBridge, "save_hf_weights"):
with patch("huggingface_hub.list_repo_files", return_value=["modeling_custom.py", "README.md"]):
with patch("huggingface_hub.hf_hub_download", side_effect=fake_hf_hub_download) as mock_download:
bridge.save_hf_pretrained([Mock()], str(tmp_path))
saved_config = json.loads((tmp_path / "config.json").read_text())
assert saved_config["auto_map"] == {
"AutoConfig": "configuration_custom.CustomConfig",
"AutoModelForCausalLM": "modeling_custom.CustomForCausalLM",
}
assert (tmp_path / "modeling_custom.py").exists()
assert not (tmp_path / ".cache").exists()
mock_download.assert_called_once_with(
repo_id="some-org/some-model",
filename="modeling_custom.py",
local_dir=str(tmp_path),
)
@patch("torch.distributed.get_rank", return_value=1)
@patch("torch.distributed.is_initialized", return_value=True)
@patch("torch.distributed.is_available", return_value=True)
@patch("torch.distributed.barrier")
def test_save_hf_pretrained_non_zero_rank(
self, mock_barrier, mock_is_available, mock_is_initialized, mock_get_rank
):
"""Test save_hf_pretrained on non-zero rank (should not save artifacts)."""
mock_hf_model = Mock(spec=PreTrainedCausalLM)
mock_hf_model.save_artifacts = Mock()
mock_megatron_model = [Mock()]
with patch.object(AutoBridge, "save_hf_weights") as mock_save_hf_weights:
bridge = AutoBridge(mock_hf_model)
# Mock _model_bridge to have no ADDITIONAL_FILE_PATTERNS
with patch.object(
type(bridge),
"_model_bridge",
PropertyMock(return_value=Mock(ADDITIONAL_FILE_PATTERNS=None)),
):
bridge.save_hf_pretrained(mock_megatron_model, "./output_dir")
# Artifacts should NOT be saved on non-zero rank
mock_hf_model.save_artifacts.assert_not_called()
mock_save_hf_weights.assert_called_once_with(
mock_megatron_model,
"./output_dir",
True,
True,
merge_adapter_weights=True,
distributed_save=False,
save_every_n_ranks=1,
)
def test_export_hf_weights(self):
"""Test exporting weights from Megatron to HF format."""
mock_hf_model = Mock(spec=PreTrainedCausalLM)
mock_hf_model.config = Mock()
mock_hf_model.config.architectures = ["LlamaForCausalLM"]
mock_hf_model.config.auto_map = None
mock_megatron_model = [object()]
with patch.object(AutoBridge, "_model_bridge", new_callable=PropertyMock) as mock_model_bridge_prop:
mock_model_bridge = Mock()
mock_weight_iter = [("weight1", torch.randn(10, 10)), ("weight2", torch.randn(5, 5))]
mock_model_bridge.stream_weights_megatron_to_hf.return_value = iter(mock_weight_iter)
mock_model_bridge_prop.return_value = mock_model_bridge
with patch("megatron.bridge.models.conversion.auto_bridge.transformers") as mock_transformers:
mock_arch_class = Mock()
mock_transformers.LlamaForCausalLM = mock_arch_class
bridge = AutoBridge(mock_hf_model)
# Mock the cached property to avoid accessing transformers
with patch.object(AutoBridge, "_causal_lm_architecture", new_callable=PropertyMock) as mock_prop:
mock_prop.return_value = mock_arch_class
weights = list(bridge.export_hf_weights(mock_megatron_model, cpu=True))
assert len(weights) == 2
assert weights[0][0] == "weight1"
assert weights[1][0] == "weight2"
assert isinstance(weights[0][1], torch.Tensor)
assert isinstance(weights[1][1], torch.Tensor)
mock_model_bridge.stream_weights_megatron_to_hf.assert_called_once_with(
mock_megatron_model,
mock_hf_model,
cpu=True,
show_progress=True,
conversion_tasks=None,
merge_adapter_weights=True,
)
def test_export_adapter_weights(self):
"""Test exporting adapter weights from Megatron to HF format."""
mock_hf_model = Mock(spec=PreTrainedCausalLM)
mock_hf_model.config = Mock()
mock_hf_model.config.architectures = ["LlamaForCausalLM"]
mock_hf_model.config.auto_map = None
mock_megatron_model = [object()]
with patch.object(AutoBridge, "_model_bridge", new_callable=PropertyMock) as mock_model_bridge_prop:
mock_model_bridge = Mock()
mock_weight_iter = [("adapter.weight", torch.randn(4, 4))]
mock_model_bridge.stream_adapter_weights_megatron_to_hf.return_value = iter(mock_weight_iter)
mock_model_bridge_prop.return_value = mock_model_bridge
with patch("megatron.bridge.models.conversion.auto_bridge.transformers") as mock_transformers:
mock_arch_class = Mock()
mock_transformers.LlamaForCausalLM = mock_arch_class
bridge = AutoBridge(mock_hf_model)
with patch.object(AutoBridge, "_causal_lm_architecture", new_callable=PropertyMock) as mock_prop:
mock_prop.return_value = mock_arch_class
weights = list(bridge.export_adapter_weights(mock_megatron_model, cpu=False, show_progress=False))
assert len(weights) == 1
assert weights[0][0] == "adapter.weight"
assert isinstance(weights[0][1], torch.Tensor)
mock_model_bridge.stream_adapter_weights_megatron_to_hf.assert_called_once_with(
mock_megatron_model,
cpu=False,