forked from vllm-project/vllm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_config.py
More file actions
1830 lines (1588 loc) · 63.3 KB
/
Copy pathtest_config.py
File metadata and controls
1830 lines (1588 loc) · 63.3 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
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import logging
import os
from dataclasses import MISSING, Field, asdict, dataclass, field
from types import SimpleNamespace
from unittest.mock import patch
import pydantic
import pytest
from huggingface_hub import ResolvedRevision
from pydantic import ValidationError
import vllm.config.vllm as vllm_config_module
import vllm.envs as envs
from vllm.compilation.backends import VllmBackend
from vllm.config import (
CompilationConfig,
KernelConfig,
ModelConfig,
ParallelConfig,
PoolerConfig,
SchedulerConfig,
SpeculativeConfig,
VllmConfig,
update_config,
)
from vllm.config.compilation import CompilationMode, CUDAGraphMode
from vllm.config.kernel import IrOpPriorityConfig
from vllm.config.load import LoadConfig
from vllm.config.utils import get_field
from vllm.config.vllm import OPTIMIZATION_LEVEL_TO_CONFIG, OptimizationLevel
from vllm.platforms import current_platform
from vllm.v1.attention.backend import AttentionCGSupport
DEVICE_TYPE = current_platform.device_type
def test_compile_config_repr_succeeds():
# setup: VllmBackend mutates the config object
config = VllmConfig()
backend = VllmBackend(config)
backend.configure_post_pass()
# test that repr(config) succeeds
val = repr(config)
assert "VllmConfig" in val
assert "inductor_passes" in val
@pytest.mark.parametrize(
("env_value", "expected"),
[
(None, None),
("0", False),
("1", True),
],
)
def test_v2_model_runner_env_tri_state(monkeypatch, env_value, expected):
if env_value is None:
monkeypatch.delenv("VLLM_USE_V2_MODEL_RUNNER", raising=False)
else:
monkeypatch.setenv("VLLM_USE_V2_MODEL_RUNNER", env_value)
assert envs.VLLM_USE_V2_MODEL_RUNNER is expected
@pytest.mark.parametrize(
("use_v2_model_runner", "expected_capture_sizes"),
[
(False, [4, 8, 12, 16]),
(True, list(range(1, 17))),
],
)
def test_resolve_cudagraph_mode_adjusts_spec_decode_sizes_only_for_v1(
use_v2_model_runner,
expected_capture_sizes,
):
compilation_config = CompilationConfig(
cudagraph_mode=CUDAGraphMode.FULL_AND_PIECEWISE,
cudagraph_capture_sizes=list(range(1, 17)),
)
compilation_config.max_cudagraph_capture_size = 16
compilation_config.post_init_cudagraph_sizes()
cudagraph_mode = compilation_config.resolve_cudagraph_mode_and_sizes(
AttentionCGSupport.ALWAYS,
"FakeAttentionBackend",
uniform_decode_query_len=4,
use_v2_model_runner=use_v2_model_runner,
tensor_parallel_size=1,
)
assert cudagraph_mode == CUDAGraphMode.FULL_AND_PIECEWISE
assert compilation_config.cudagraph_capture_sizes == expected_capture_sizes
@pytest.mark.parametrize(
("model_config", "expected"),
[
(
SimpleNamespace(
model="Qwen/Qwen3-1.7B-Base",
architectures=["Qwen3ForCausalLM"],
runner_type="generate",
is_moe=False,
is_quantized=False,
),
True,
),
(
SimpleNamespace(
model="Qwen/Qwen3-32B",
architectures=["Qwen3ForCausalLM"],
runner_type="generate",
is_moe=False,
is_quantized=False,
),
True,
),
(
SimpleNamespace(
model="meta-llama/Llama-3.2-1B",
architectures=["LlamaForCausalLM"],
runner_type="generate",
is_moe=False,
is_quantized=False,
),
True,
),
(
SimpleNamespace(
model="mistralai/Mistral-7B-v0.1",
architectures=["MistralForCausalLM"],
runner_type="generate",
is_moe=False,
is_quantized=False,
),
True,
),
(
SimpleNamespace(
model="facebook/opt-125m",
architectures=["OPTForCausalLM"],
runner_type="generate",
is_moe=False,
is_quantized=False,
),
True,
),
(
SimpleNamespace(
model="google/gemma-2-2b",
architectures=["Gemma2ForCausalLM"],
runner_type="generate",
is_moe=False,
is_quantized=False,
),
True,
),
(
SimpleNamespace(
model="deepseek-ai/DeepSeek-V2-Lite-Chat",
architectures=["DeepseekV2ForCausalLM"],
runner_type="generate",
is_moe=True,
is_quantized=False,
),
True,
),
(
SimpleNamespace(
model="deepseek-ai/DeepSeek-V2-Chat",
architectures=["DeepseekV2ForCausalLM"],
runner_type="generate",
is_moe=True,
is_quantized=False,
),
True,
),
(
SimpleNamespace(
model="Qwen/Qwen1.5-MoE-A2.7B",
architectures=["Qwen2MoeForCausalLM"],
runner_type="generate",
is_moe=True,
is_quantized=False,
),
True,
),
(
SimpleNamespace(
model="Qwen/Qwen1.5-MoE-A2.7B-Chat",
architectures=["Qwen2MoeForCausalLM"],
runner_type="generate",
is_moe=True,
is_quantized=False,
),
True,
),
(
SimpleNamespace(
model="ibm-research/PowerMoE-3b",
architectures=["GraniteMoeForCausalLM"],
runner_type="generate",
is_moe=True,
is_quantized=False,
),
True,
),
(
SimpleNamespace(
model="thinkingmachines/Inkling",
architectures=["InklingForCausalLM"],
runner_type="generate",
is_moe=True,
is_quantized=False,
),
True,
),
(
SimpleNamespace(
model="thinkingmachines/Inkling",
architectures=["InklingForConditionalGeneration"],
runner_type="generate",
is_moe=True,
is_quantized=False,
),
True,
),
(
SimpleNamespace(
model="mistralai/Mixtral-8x7B-Instruct-v0.1",
architectures=["MixtralForCausalLM"],
runner_type="generate",
is_moe=True,
is_quantized=False,
),
False,
),
(
SimpleNamespace(
model="Qwen/Qwen3-1.7B-FP8",
architectures=["Qwen3ForCausalLM"],
runner_type="generate",
is_moe=False,
is_quantized=True,
),
True,
),
(
SimpleNamespace(
model="Qwen/Qwen3.5-4B",
architectures=["Qwen3_5ForConditionalGeneration"],
runner_type="generate",
is_moe=False,
is_quantized=False,
is_hybrid=True,
),
False,
),
(
SimpleNamespace(
model="state-spaces/mamba-130m-hf",
architectures=["MambaForCausalLM"],
runner_type="generate",
is_moe=False,
is_quantized=False,
is_attention_free=True,
),
False,
),
(
SimpleNamespace(
model="Qwen/Qwen3-Embedding-0.6B",
architectures=["Qwen3ForCausalLM"],
runner_type="pooling",
is_moe=False,
is_quantized=False,
),
False,
),
],
)
def test_is_default_v2_model_runner_model(model_config, expected):
config = SimpleNamespace(model_config=model_config)
assert VllmConfig._is_default_v2_model_runner_model(config) is expected
@pytest.mark.skip_global_cleanup
def test_with_hf_config_populates_missing_architectures_from_causal_lm_mapping(
monkeypatch,
):
monkeypatch.setattr(
vllm_config_module,
"replace",
lambda self, **kwargs: SimpleNamespace(**kwargs),
)
cfg = SimpleNamespace(
model_config=SimpleNamespace(
is_multimodal_model=False,
hf_config=SimpleNamespace(),
get_model_arch_config=lambda: "arch-config",
)
)
hf_config = SimpleNamespace(model_type="mistral", architectures=None)
updated = VllmConfig.with_hf_config(cfg, hf_config)
assert updated.model_config.hf_config.architectures == ["MistralForCausalLM"]
assert hf_config.architectures is None
@pytest.mark.skip_global_cleanup
def test_with_hf_config_preserves_explicit_architectures_override(monkeypatch):
monkeypatch.setattr(
vllm_config_module,
"replace",
lambda self, **kwargs: SimpleNamespace(**kwargs),
)
cfg = SimpleNamespace(
model_config=SimpleNamespace(
is_multimodal_model=False,
hf_config=SimpleNamespace(),
get_model_arch_config=lambda: "arch-config",
)
)
hf_config = SimpleNamespace(model_type="mistral", architectures=None)
updated = VllmConfig.with_hf_config(
cfg,
hf_config,
architectures=["Ministral3ForCausalLM"],
)
assert updated.model_config.hf_config.architectures == ["Ministral3ForCausalLM"]
@pytest.mark.skip_global_cleanup
def test_with_hf_config_leaves_unknown_model_type_without_architectures(
monkeypatch,
):
monkeypatch.setattr(
vllm_config_module,
"replace",
lambda self, **kwargs: SimpleNamespace(**kwargs),
)
cfg = SimpleNamespace(
model_config=SimpleNamespace(
is_multimodal_model=False,
hf_config=SimpleNamespace(),
get_model_arch_config=lambda: "arch-config",
)
)
hf_config = SimpleNamespace(
model_type="not_a_real_model",
architectures=None,
)
updated = VllmConfig.with_hf_config(cfg, hf_config)
assert updated.model_config.hf_config.architectures is None
def test_async_scheduling_with_pipeline_parallelism_is_allowed():
cfg = VllmConfig(
scheduler_config=SchedulerConfig(
max_model_len=8192,
is_encoder_decoder=False,
async_scheduling=True,
),
parallel_config=ParallelConfig(
pipeline_parallel_size=2,
distributed_executor_backend="mp",
nnodes=2,
),
)
assert cfg.scheduler_config.async_scheduling is True
def test_data_parallel_rpc_port_has_fixed_default():
assert ParallelConfig().data_parallel_rpc_port == 29550
@pytest.mark.parametrize("port", [1, 29550, 65535])
def test_data_parallel_rpc_port_accepts_valid_ports(port: int):
assert ParallelConfig(data_parallel_rpc_port=port).data_parallel_rpc_port == port
@pytest.mark.parametrize("port", [-1, 0, 65536])
def test_data_parallel_rpc_port_rejects_invalid_ports(port: int):
with pytest.raises(ValidationError):
ParallelConfig(data_parallel_rpc_port=port)
def test_reconfigure_for_independent_dp_rank_on_multinode_dense_model():
parallel_config = ParallelConfig(
tensor_parallel_size=8,
data_parallel_size=2,
data_parallel_size_local=1,
data_parallel_rank=1,
distributed_executor_backend="mp",
nnodes=2,
node_rank=1,
)
assert parallel_config.nnodes_within_dp == 1
assert parallel_config.node_rank_within_dp == 0
parallel_config.reconfigure_for_independent_dp_rank()
assert parallel_config.data_parallel_size == 1
assert parallel_config.data_parallel_size_local == 1
assert parallel_config.data_parallel_rank == 0
assert parallel_config.data_parallel_index == 1
assert parallel_config.nnodes == 1
assert parallel_config.node_rank == 0
assert parallel_config.world_size == 8
def test_draft_model_enables_async_scheduling_by_default():
parallel_config = ParallelConfig(distributed_executor_backend="uni")
model_config = ModelConfig("Qwen/Qwen3-0.6B", max_model_len=2048)
speculative_config = SpeculativeConfig(
method="draft_model",
model="Qwen/Qwen3-0.6B",
num_speculative_tokens=3,
target_model_config=model_config,
target_parallel_config=parallel_config,
)
cfg = VllmConfig(
model_config=model_config,
scheduler_config=SchedulerConfig(
max_model_len=2048,
is_encoder_decoder=False,
),
parallel_config=parallel_config,
speculative_config=speculative_config,
)
assert cfg.scheduler_config.async_scheduling is True
@pytest.mark.parametrize(
("method", "parallel_drafting", "expected_slots"),
[
pytest.param("eagle3", False, 0, id="eagle3"),
pytest.param("eagle3", True, 7, id="p-eagle"),
pytest.param("dflash", True, 8, id="dflash"),
pytest.param("dspark", True, 7, id="dspark"),
pytest.param("mtp", False, 0, id="mtp"),
pytest.param("ngram", False, 0, id="ngram"),
pytest.param("draft_model", False, 1, id="draft-model"),
pytest.param("draft_model", True, 8, id="pard"),
],
)
def test_max_num_new_slots_for_drafting(method, parallel_drafting, expected_slots):
speculative_config = SpeculativeConfig(
model="ngram",
num_speculative_tokens=8,
)
speculative_config.method = method
speculative_config.parallel_drafting = parallel_drafting
assert speculative_config.max_num_new_slots_for_drafting == expected_slots
@dataclass
class _TestConfigFields:
a: int
b: dict = field(default_factory=dict)
c: str = "default"
def test_get_field():
b = get_field(_TestConfigFields, "b")
assert isinstance(b, Field)
assert b.default is MISSING
assert b.default_factory is dict
c = get_field(_TestConfigFields, "c")
assert isinstance(c, Field)
assert c.default == "default"
assert c.default_factory is MISSING
@dataclass
class _TestNestedConfig:
a: _TestConfigFields = field(default_factory=lambda: _TestConfigFields(a=0))
@dataclass
class _TestDerivedConfigFields(_TestConfigFields):
pass
def test_update_config():
# Simple update
config1 = _TestConfigFields(a=0)
new_config1 = update_config(config1, {"a": 42})
assert new_config1.a == 42
# Nonexistent field
with pytest.raises(ValueError, match=r"_TestConfigFields\.nonexistent"):
new_config1 = update_config(config1, {"nonexistent": 1})
# Nested update with dataclass
config2 = _TestNestedConfig()
new_inner_config = _TestConfigFields(a=1, c="new_value")
new_config2 = update_config(config2, {"a": new_inner_config})
assert new_config2.a == new_inner_config
# Declared field type, not the live value's subtype, defines valid overrides
config_with_derived = _TestNestedConfig(a=_TestDerivedConfigFields(a=0))
new_config2 = update_config(config_with_derived, {"a": new_inner_config})
assert new_config2.a is new_inner_config
# Nested update with unrelated dataclass
with pytest.raises(ValueError, match=r"_TestNestedConfig\.a"):
update_config(config2, {"a": _TestNestedConfig()})
# Nested update with dict
config3 = _TestNestedConfig()
new_config3 = update_config(config3, {"a": {"c": "new_value"}})
assert new_config3.a.c == "new_value"
# Nested update with invalid type
with pytest.raises(ValueError, match=r"_TestNestedConfig\.a"):
update_config(config3, {"a": "new_value"})
# Invalid nested field preserves its full path
with pytest.raises(ValueError, match=r"_TestNestedConfig\.a\.nonexistent"):
update_config(config3, {"a": {"nonexistent": 1}})
@pytest.mark.parametrize(
("model_id", "expected_runner_type", "expected_convert_type"),
[
("distilbert/distilgpt2", "generate", "none"),
("intfloat/multilingual-e5-small", "pooling", "none"),
("jason9693/Qwen2.5-1.5B-apeach", "pooling", "classify"),
("cross-encoder/ms-marco-MiniLM-L-6-v2", "pooling", "none"),
("Qwen/Qwen2.5-Math-RM-72B", "pooling", "none"),
("openai/whisper-small", "generate", "none"),
],
)
def test_auto_runner(model_id, expected_runner_type, expected_convert_type):
config = ModelConfig(model_id, runner="auto")
assert config.runner_type == expected_runner_type
assert config.convert_type == expected_convert_type
@pytest.mark.parametrize(
("model_id", "expected_runner_type", "expected_convert_type"),
[
("distilbert/distilgpt2", "pooling", "embed"),
("intfloat/multilingual-e5-small", "pooling", "none"),
("jason9693/Qwen2.5-1.5B-apeach", "pooling", "classify"),
("cross-encoder/ms-marco-MiniLM-L-6-v2", "pooling", "none"),
("Qwen/Qwen2.5-Math-RM-72B", "pooling", "none"),
("openai/whisper-small", "pooling", "embed"),
],
)
def test_pooling_runner(model_id, expected_runner_type, expected_convert_type):
config = ModelConfig(model_id, runner="pooling")
assert config.runner_type == expected_runner_type
assert config.convert_type == expected_convert_type
@pytest.mark.parametrize(
("model_id", "expected_runner_type", "expected_convert_type"),
[
("Qwen/Qwen2.5-1.5B-Instruct", "draft", "none"),
],
)
def test_draft_runner(model_id, expected_runner_type, expected_convert_type):
config = ModelConfig(model_id, runner="draft")
assert config.runner_type == expected_runner_type
assert config.convert_type == expected_convert_type
MODEL_IDS_EXPECTED = [
("Qwen/Qwen1.5-7B", 32768),
("mistralai/Mistral-7B-v0.1", 4096),
("mistralai/Mistral-7B-Instruct-v0.2", 32768),
]
@pytest.mark.parametrize("model_id_expected", MODEL_IDS_EXPECTED)
def test_disable_sliding_window(model_id_expected):
model_id, expected = model_id_expected
model_config = ModelConfig(model_id, disable_sliding_window=True)
assert model_config.max_model_len == expected
@pytest.mark.skipif(
current_platform.is_rocm(), reason="Xformers backend is not supported on ROCm."
)
def test_get_pooling_config():
model_id = "sentence-transformers/all-MiniLM-L12-v2"
model_config = ModelConfig(model_id)
assert model_config.pooler_config is not None
assert model_config.pooler_config.use_activation
assert model_config.pooler_config.seq_pooling_type == "MEAN"
assert model_config.pooler_config.tok_pooling_type == "ALL"
@pytest.mark.skipif(
current_platform.is_rocm(), reason="Xformers backend is not supported on ROCm."
)
def test_get_pooling_config_from_args():
model_id = "sentence-transformers/all-MiniLM-L12-v2"
pooler_config = PoolerConfig(seq_pooling_type="CLS", use_activation=False)
model_config = ModelConfig(model_id, pooler_config=pooler_config)
assert asdict(model_config.pooler_config) == asdict(pooler_config)
@pytest.mark.parametrize(
("model_id", "default_pooling_type", "pooling_type"),
[
("tomaarsen/Qwen3-Reranker-0.6B-seq-cls", "LAST", "LAST"), # LLM
("intfloat/e5-small", "CLS", "MEAN"), # BertModel
],
)
def test_default_seq_pooling_type(model_id, default_pooling_type, pooling_type):
model_config = ModelConfig(model_id)
assert model_config._model_info.default_seq_pooling_type == default_pooling_type
assert model_config.pooler_config.seq_pooling_type == pooling_type
@pytest.mark.parametrize(
("model_id", "default_pooling_type", "pooling_type"),
[
("Qwen/Qwen2.5-Math-RM-72B", "ALL", "ALL"), # reward
("Qwen/Qwen2.5-Math-PRM-7B", "STEP", "STEP"), # step reward
],
)
def test_default_tok_pooling_type(model_id, default_pooling_type, pooling_type):
model_config = ModelConfig(model_id)
assert model_config._model_info.default_tok_pooling_type == default_pooling_type
assert model_config.pooler_config.tok_pooling_type == pooling_type
@pytest.mark.parametrize(
("model_id", "expected_is_moe_model"),
[
("RedHatAI/Qwen3-8B-speculator.eagle3", False),
("RedHatAI/Llama-3.1-8B-Instruct-NVFP4", False),
("RedHatAI/Llama-3.2-1B-FP8", False),
("RedHatAI/Mistral-Small-24B-Instruct-2501-quantized.w8a8", False),
("RedHatAI/gpt-oss-20b", True),
("RedHatAI/DeepSeek-V2.5-1210-FP8", True),
("RedHatAI/Llama-4-Scout-17B-16E-Instruct", True),
("RedHatAI/Mixtral-8x7B-Instruct-v0.1", True),
],
)
def test_moe_model_detection(model_id, expected_is_moe_model):
model_config = ModelConfig(model_id)
# Just check that is_moe field exists and is a boolean
assert model_config.is_moe == expected_is_moe_model
@pytest.mark.parametrize(
("model_id", "quantized"),
[
("RedHatAI/Qwen3-8B-speculator.eagle3", False),
("RedHatAI/Llama-3.1-8B-Instruct-NVFP4", True),
("RedHatAI/Llama-3.2-1B-FP8", True),
("RedHatAI/Mistral-Small-24B-Instruct-2501-quantized.w8a8", True),
("RedHatAI/gpt-oss-20b", True),
("RedHatAI/DeepSeek-V2.5-1210-FP8", True),
("RedHatAI/Mixtral-8x7B-Instruct-v0.1", False),
],
)
def test_is_quantized(model_id, quantized):
model_config = ModelConfig(model_id)
# Just check that quantized field exists and is a boolean
assert model_config.is_quantized == quantized
@pytest.mark.skipif(
current_platform.is_rocm(), reason="Xformers backend is not supported on ROCm."
)
def test_get_bert_tokenization_sentence_transformer_config():
model_id = "BAAI/bge-base-en-v1.5"
bge_model_config = ModelConfig(model_id)
bert_bge_model_config = bge_model_config._get_encoder_config()
assert bert_bge_model_config["max_seq_length"] == 512
assert bert_bge_model_config["do_lower_case"]
def test_rope_customization():
TEST_ROPE_PARAMETERS = {
"rope_theta": 16_000_000.0,
"rope_type": "dynamic",
"factor": 2.0,
}
LLAMA_ROPE_PARAMETERS = {"rope_theta": 500000.0, "rope_type": "default"}
LONGCHAT_ROPE_PARAMETERS = {"rope_type": "linear", "factor": 8.0}
llama_model_config = ModelConfig("meta-llama/Meta-Llama-3-8B-Instruct")
assert (
getattr(llama_model_config.hf_config, "rope_parameters", None)
== LLAMA_ROPE_PARAMETERS
)
assert llama_model_config.max_model_len == 8192
llama_model_config = ModelConfig(
"meta-llama/Meta-Llama-3-8B-Instruct",
hf_overrides={"rope_parameters": TEST_ROPE_PARAMETERS},
)
assert (
getattr(llama_model_config.hf_config, "rope_parameters", None)
== TEST_ROPE_PARAMETERS
)
assert llama_model_config.max_model_len == 16384
longchat_model_config = ModelConfig("lmsys/longchat-13b-16k")
# Check if LONGCHAT_ROPE_PARAMETERS entries are in longchat_model_config
assert all(
longchat_model_config.hf_config.rope_parameters.get(key) == value
for key, value in LONGCHAT_ROPE_PARAMETERS.items()
)
assert longchat_model_config.max_model_len == 16384
longchat_model_config = ModelConfig(
"lmsys/longchat-13b-16k",
hf_overrides={
"rope_parameters": TEST_ROPE_PARAMETERS,
},
)
assert (
getattr(longchat_model_config.hf_config, "rope_parameters", None)
== TEST_ROPE_PARAMETERS
)
assert longchat_model_config.max_model_len == 4096
def test_nested_hf_overrides():
"""Test that nested hf_overrides work correctly."""
# Test with a model that has text_config
model_config = ModelConfig(
"Qwen/Qwen2-VL-2B-Instruct",
hf_overrides={
"text_config": {
"hidden_size": 1024,
},
},
)
assert model_config.hf_config.text_config.hidden_size == 1024
# Test with deeply nested overrides
model_config = ModelConfig(
"Qwen/Qwen2-VL-2B-Instruct",
hf_overrides={
"text_config": {
"hidden_size": 2048,
"num_attention_heads": 16,
},
"vision_config": {
"hidden_size": 512,
},
},
)
assert model_config.hf_config.text_config.hidden_size == 2048
assert model_config.hf_config.text_config.num_attention_heads == 16
assert model_config.hf_config.vision_config.hidden_size == 512
def test_model_class_overrides_registers_target():
"""`model_class_overrides` redirects an architecture to a custom class."""
from vllm.model_executor.models import ModelRegistry
arch = "_TestModelClassOverrideArch"
target = "vllm.model_executor.models.llama:LlamaForCausalLM"
assert arch not in ModelRegistry.models
model_config = ModelConfig(
"facebook/opt-125m",
model_class_overrides={arch: target},
)
try:
# Accessing `.registry` is the chokepoint that applies the overrides;
# it has already run during construction.
registered = model_config.registry.models[arch]
assert registered.module_name == "vllm.model_executor.models.llama"
assert registered.class_name == "LlamaForCausalLM"
# Idempotent: a second access does not re-register or error out.
assert model_config.registry.models[arch] is registered
finally:
ModelRegistry.models.pop(arch, None)
@pytest.mark.skipif(
current_platform.is_rocm(), reason="Encoder Decoder models not supported on ROCm."
)
@pytest.mark.parametrize(
("model_id", "is_encoder_decoder"),
[
("facebook/opt-125m", False),
("openai/whisper-tiny", True),
("meta-llama/Llama-3.2-1B-Instruct", False),
],
)
def test_is_encoder_decoder(model_id, is_encoder_decoder):
config = ModelConfig(model_id)
assert config.is_encoder_decoder == is_encoder_decoder
@pytest.mark.parametrize(
("model_id", "uses_mrope"),
[
("facebook/opt-125m", False),
("Qwen/Qwen2-VL-2B-Instruct", True),
],
)
def test_uses_mrope(model_id, uses_mrope):
config = ModelConfig(model_id)
assert config.uses_mrope == uses_mrope
def test_generation_config_loading():
model_id = "Qwen/Qwen2.5-1.5B-Instruct"
# When set generation_config to "vllm", the default generation config
# will not be loaded.
model_config = ModelConfig(model_id, generation_config="vllm")
assert model_config.get_diff_sampling_param() == {}
# When set generation_config to "auto", the default generation config
# should be loaded.
model_config = ModelConfig(model_id, generation_config="auto")
correct_generation_config = {
"repetition_penalty": 1.1,
"temperature": 0.7,
"top_p": 0.8,
"top_k": 20,
}
assert model_config.get_diff_sampling_param() == correct_generation_config
# The generation config could be overridden by the user.
override_generation_config = {"temperature": 0.5, "top_k": 5}
model_config = ModelConfig(
model_id,
generation_config="auto",
override_generation_config=override_generation_config,
)
override_result = correct_generation_config.copy()
override_result.update(override_generation_config)
assert model_config.get_diff_sampling_param() == override_result
# When generation_config is set to "vllm" and override_generation_config
# is set, the override_generation_config should be used directly.
model_config = ModelConfig(
model_id,
generation_config="vllm",
override_generation_config=override_generation_config,
)
assert model_config.get_diff_sampling_param() == override_generation_config
@pytest.mark.parametrize(
"pt_load_map_location",
[
DEVICE_TYPE,
{"": DEVICE_TYPE},
],
)
def test_load_config_pt_load_map_location(pt_load_map_location):
load_config = LoadConfig(pt_load_map_location=pt_load_map_location)
config = VllmConfig(load_config=load_config)
assert config.load_config.pt_load_map_location == pt_load_map_location
@pytest.mark.parametrize(
("model_id", "max_model_len", "expected_max_len", "should_raise"),
[
("BAAI/bge-reranker-base", None, 512, False),
("BAAI/bge-reranker-base", 256, 256, False),
("BAAI/bge-reranker-base", 513, 512, True),
("deepseek-ai/DeepSeek-R1-Distill-Qwen-7B", None, 131072, False),
("deepseek-ai/DeepSeek-R1-Distill-Qwen-7B", 131073, 131072, True),
],
)
def test_get_and_verify_max_len(
model_id, max_model_len, expected_max_len, should_raise
):
"""Test get_and_verify_max_len with different configurations."""
model_config = ModelConfig(model_id)
if should_raise:
with pytest.raises(ValueError):
model_config.get_and_verify_max_len(max_model_len)
else:
actual_max_len = model_config.get_and_verify_max_len(max_model_len)
assert actual_max_len == expected_max_len
class MockConfig:
"""Simple mock object for testing maybe_pull_model_tokenizer_for_runai"""
def __init__(self, model: str, tokenizer: str):
self.model = model
self.tokenizer = tokenizer
self.model_weights = None
@pytest.mark.parametrize(
"s3_url",
[
"s3://example-bucket-1/model/",
"s3://example-bucket-2/model/",
],
)
@patch("vllm.transformers_utils.runai_utils.ObjectStorageModel.pull_files")
def test_s3_url_model_tokenizer_paths(mock_pull_files, s3_url):
"""Test that S3 URLs create deterministic local directories for model and
tokenizer."""
# Mock pull_files to avoid actually downloading files during tests
mock_pull_files.return_value = None
# Create first mock and run the method
config1 = MockConfig(model=s3_url, tokenizer=s3_url)
ModelConfig.maybe_pull_model_tokenizer_for_runai(config1, s3_url, s3_url)
# Check that model and tokenizer point to existing directories
assert os.path.exists(config1.model), (
f"Model directory does not exist: {config1.model}"
)
assert os.path.isdir(config1.model), (
f"Model path is not a directory: {config1.model}"
)
assert os.path.exists(config1.tokenizer), (
f"Tokenizer directory does not exist: {config1.tokenizer}"
)
assert os.path.isdir(config1.tokenizer), (
f"Tokenizer path is not a directory: {config1.tokenizer}"
)
# Verify that the paths are different from the original S3 URL
assert config1.model != s3_url, "Model path should be converted to local directory"
assert config1.tokenizer != s3_url, (
"Tokenizer path should be converted to local directory"
)
# Store the original paths
created_model_dir = config1.model
create_tokenizer_dir = config1.tokenizer
# Create a new mock and run the method with the same S3 URL
config2 = MockConfig(model=s3_url, tokenizer=s3_url)
ModelConfig.maybe_pull_model_tokenizer_for_runai(config2, s3_url, s3_url)
# Check that the new directories exist
assert os.path.exists(config2.model), (
f"Model directory does not exist: {config2.model}"
)
assert os.path.isdir(config2.model), (
f"Model path is not a directory: {config2.model}"
)
assert os.path.exists(config2.tokenizer), (
f"Tokenizer directory does not exist: {config2.tokenizer}"
)
assert os.path.isdir(config2.tokenizer), (
f"Tokenizer path is not a directory: {config2.tokenizer}"
)
# Verify that the paths are deterministic (same as before)
assert config2.model == created_model_dir, (
f"Model paths are not deterministic. "
f"Original: {created_model_dir}, New: {config2.model}"
)
assert config2.tokenizer == create_tokenizer_dir, (
f"Tokenizer paths are not deterministic. "
f"Original: {create_tokenizer_dir}, New: {config2.tokenizer}"
)
@patch("vllm.transformers_utils.runai_utils.ObjectStorageModel.pull_files")
def test_s3_url_different_models_create_different_directories(mock_pull_files):
"""Test that different S3 URLs create different local directories."""
# Mock pull_files to avoid actually downloading files during tests
mock_pull_files.return_value = None
s3_url1 = "s3://example-bucket-1/model/"
s3_url2 = "s3://example-bucket-2/model/"
# Create mocks with different S3 URLs and run the method
config1 = MockConfig(model=s3_url1, tokenizer=s3_url1)