2025-02-02 14:58:18 -05:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2025-06-03 11:20:17 -07:00
|
|
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
2025-02-02 14:58:18 -05:00
|
|
|
|
2024-11-07 05:42:40 -03:00
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
2026-01-09 19:02:14 +08:00
|
|
|
from vllm.model_executor.layers.pooler import DispatchPooler
|
|
|
|
|
from vllm.model_executor.layers.pooler.seqwise import CLSPool, MeanPool
|
2024-11-07 05:42:40 -03:00
|
|
|
from vllm.model_executor.models.bert import BertEmbeddingModel
|
2024-11-14 18:23:29 -03:00
|
|
|
from vllm.model_executor.models.roberta import RobertaEmbeddingModel
|
2024-11-07 05:42:40 -03:00
|
|
|
from vllm.platforms import current_platform
|
|
|
|
|
|
|
|
|
|
MAX_MODEL_LEN = 128
|
|
|
|
|
MODEL_NAME = os.environ.get("MODEL_NAME", "BAAI/bge-base-en-v1.5")
|
|
|
|
|
REVISION = os.environ.get("REVISION", "main")
|
|
|
|
|
|
2024-11-14 18:23:29 -03:00
|
|
|
MODEL_NAME_ROBERTA = os.environ.get("MODEL_NAME", "intfloat/multilingual-e5-base")
|
|
|
|
|
REVISION_ROBERTA = os.environ.get("REVISION", "main")
|
|
|
|
|
|
2024-11-07 05:42:40 -03:00
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
|
current_platform.is_rocm(), reason="Xformers backend is not supported on ROCm."
|
|
|
|
|
)
|
2025-07-26 10:09:52 -03:00
|
|
|
def test_model_loading_with_params(vllm_runner, monkeypatch):
|
2024-11-07 05:42:40 -03:00
|
|
|
"""
|
|
|
|
|
Test parameter weight loading with tp>1.
|
|
|
|
|
"""
|
2025-07-26 10:09:52 -03:00
|
|
|
# to use apply_model
|
|
|
|
|
monkeypatch.setenv("VLLM_ALLOW_INSECURE_SERIALIZATION", "1")
|
2024-11-07 05:42:40 -03:00
|
|
|
with vllm_runner(
|
|
|
|
|
model_name=MODEL_NAME,
|
|
|
|
|
revision=REVISION,
|
|
|
|
|
dtype="float16",
|
2025-01-20 15:00:59 +08:00
|
|
|
max_model_len=MAX_MODEL_LEN,
|
|
|
|
|
) as vllm_model:
|
2025-06-24 02:38:06 +08:00
|
|
|
output = vllm_model.embed(
|
|
|
|
|
"Write a short story about a robot that dreams for the first time.\n"
|
|
|
|
|
)
|
2024-11-07 05:42:40 -03:00
|
|
|
|
2025-12-07 16:00:22 +08:00
|
|
|
model_config = vllm_model.llm.llm_engine.model_config
|
|
|
|
|
model_tokenizer = vllm_model.llm.llm_engine.tokenizer
|
2024-11-07 05:42:40 -03:00
|
|
|
|
|
|
|
|
# asserts on the bert model config file
|
|
|
|
|
assert model_config.encoder_config["max_seq_length"] == 512
|
|
|
|
|
assert model_config.encoder_config["do_lower_case"]
|
|
|
|
|
|
|
|
|
|
# asserts on the pooling config files
|
2026-01-10 12:53:24 +08:00
|
|
|
assert model_config.pooler_config.seq_pooling_type == "CLS"
|
|
|
|
|
assert model_config.pooler_config.tok_pooling_type == "ALL"
|
2025-05-28 11:12:54 +05:30
|
|
|
assert model_config.pooler_config.normalize
|
2024-11-07 05:42:40 -03:00
|
|
|
|
|
|
|
|
# asserts on the tokenizer loaded
|
2025-12-07 16:00:22 +08:00
|
|
|
assert model_config.tokenizer == "BAAI/bge-base-en-v1.5"
|
|
|
|
|
assert model_tokenizer.model_max_length == 512
|
2024-11-07 05:42:40 -03:00
|
|
|
|
2025-01-20 15:00:59 +08:00
|
|
|
def check_model(model):
|
|
|
|
|
assert isinstance(model, BertEmbeddingModel)
|
2025-07-23 11:25:37 +08:00
|
|
|
assert isinstance(pooler := model.pooler, DispatchPooler)
|
|
|
|
|
assert isinstance(pooler.poolers_by_task["embed"].pooling, CLSPool)
|
2025-01-20 15:00:59 +08:00
|
|
|
|
|
|
|
|
vllm_model.apply_model(check_model)
|
|
|
|
|
|
2024-11-07 05:42:40 -03:00
|
|
|
assert output
|
2024-11-14 18:23:29 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
|
current_platform.is_rocm(), reason="Xformers backend is not supported on ROCm."
|
|
|
|
|
)
|
2025-07-26 10:09:52 -03:00
|
|
|
def test_roberta_model_loading_with_params(vllm_runner, monkeypatch):
|
2024-11-14 18:23:29 -03:00
|
|
|
"""
|
|
|
|
|
Test parameter weight loading with tp>1.
|
|
|
|
|
"""
|
2025-07-26 10:09:52 -03:00
|
|
|
# to use apply_model
|
|
|
|
|
monkeypatch.setenv("VLLM_ALLOW_INSECURE_SERIALIZATION", "1")
|
2024-11-14 18:23:29 -03:00
|
|
|
with vllm_runner(
|
|
|
|
|
model_name=MODEL_NAME_ROBERTA,
|
|
|
|
|
revision=REVISION_ROBERTA,
|
|
|
|
|
dtype="float16",
|
2025-01-20 15:00:59 +08:00
|
|
|
max_model_len=MAX_MODEL_LEN,
|
|
|
|
|
) as vllm_model:
|
2025-06-24 02:38:06 +08:00
|
|
|
output = vllm_model.embed(
|
|
|
|
|
"Write a short story about a robot that dreams for the first time.\n"
|
|
|
|
|
)
|
2024-11-14 18:23:29 -03:00
|
|
|
|
2025-12-07 16:00:22 +08:00
|
|
|
model_config = vllm_model.llm.llm_engine.model_config
|
|
|
|
|
model_tokenizer = vllm_model.llm.llm_engine.tokenizer
|
2024-11-14 18:23:29 -03:00
|
|
|
|
|
|
|
|
# asserts on the bert model config file
|
|
|
|
|
assert model_config.encoder_config["max_seq_length"] == 512
|
|
|
|
|
assert not model_config.encoder_config["do_lower_case"]
|
|
|
|
|
|
|
|
|
|
# asserts on the pooling config files
|
2026-01-10 12:53:24 +08:00
|
|
|
assert model_config.pooler_config.seq_pooling_type == "MEAN"
|
|
|
|
|
assert model_config.pooler_config.tok_pooling_type == "ALL"
|
2025-05-28 11:12:54 +05:30
|
|
|
assert model_config.pooler_config.normalize
|
2024-11-14 18:23:29 -03:00
|
|
|
|
|
|
|
|
# asserts on the tokenizer loaded
|
2025-12-07 16:00:22 +08:00
|
|
|
assert model_config.tokenizer == "intfloat/multilingual-e5-base"
|
|
|
|
|
assert model_tokenizer.model_max_length == 512
|
2024-11-14 18:23:29 -03:00
|
|
|
|
2025-01-20 15:00:59 +08:00
|
|
|
def check_model(model):
|
|
|
|
|
assert isinstance(model, RobertaEmbeddingModel)
|
2025-07-23 11:25:37 +08:00
|
|
|
assert isinstance(pooler := model.pooler, DispatchPooler)
|
|
|
|
|
assert isinstance(pooler.poolers_by_task["embed"].pooling, MeanPool)
|
2025-01-20 15:00:59 +08:00
|
|
|
|
|
|
|
|
vllm_model.apply_model(check_model)
|
2024-11-14 18:23:29 -03:00
|
|
|
|
|
|
|
|
assert output
|
2025-01-11 15:05:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
|
current_platform.is_rocm(), reason="Xformers backend is not supported on ROCm."
|
|
|
|
|
)
|
2025-07-26 10:09:52 -03:00
|
|
|
def test_facebook_roberta_model_loading_with_params(vllm_runner, monkeypatch):
|
2025-01-11 15:05:09 +01:00
|
|
|
"""
|
|
|
|
|
Test loading roberta-base model with no lm_head.
|
|
|
|
|
"""
|
2025-07-26 10:09:52 -03:00
|
|
|
# to use apply_model
|
|
|
|
|
monkeypatch.setenv("VLLM_ALLOW_INSECURE_SERIALIZATION", "1")
|
2025-01-11 15:05:09 +01:00
|
|
|
model_name = "FacebookAI/roberta-base"
|
|
|
|
|
with vllm_runner(
|
2025-01-20 15:00:59 +08:00
|
|
|
model_name=model_name, dtype="float16", max_model_len=MAX_MODEL_LEN
|
|
|
|
|
) as vllm_model:
|
2025-06-24 02:38:06 +08:00
|
|
|
output = vllm_model.embed(
|
|
|
|
|
"Write a short story about a robot that dreams for the first time.\n"
|
|
|
|
|
)
|
2025-01-11 15:05:09 +01:00
|
|
|
|
2025-12-07 16:00:22 +08:00
|
|
|
assert vllm_model.llm.llm_engine.model_config.tokenizer == model_name
|
2025-01-11 15:05:09 +01:00
|
|
|
|
2025-01-20 15:00:59 +08:00
|
|
|
def check_model(model):
|
|
|
|
|
assert isinstance(model, RobertaEmbeddingModel)
|
|
|
|
|
assert not hasattr(model, "lm_head")
|
2025-07-23 11:25:37 +08:00
|
|
|
assert isinstance(pooler := model.pooler, DispatchPooler)
|
|
|
|
|
assert isinstance(pooler.poolers_by_task["embed"].pooling, CLSPool)
|
2025-01-20 15:00:59 +08:00
|
|
|
|
|
|
|
|
vllm_model.apply_model(check_model)
|
2025-01-11 15:05:09 +01:00
|
|
|
|
|
|
|
|
assert output
|