test_jina.py 3.8 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4
from functools import partial

5
6
import pytest

7
from vllm import PoolingParams
8

9
10
from ...utils import EmbedModelInfo, RerankModelInfo
from .embed_utils import (check_embeddings_close,
11
                          correctness_test_embed_models, matryoshka_fy)
12
from .mteb_utils import mteb_test_embed_models, mteb_test_rerank_models
13

14
EMBEDDING_MODELS = [
15
16
    EmbedModelInfo("jinaai/jina-embeddings-v3",
                   architecture="XLMRobertaModel",
17
                   is_matryoshka=True)
18
19
]

20
21
22
23
24
25
26
RERANK_MODELS = [
    RerankModelInfo(
        "jinaai/jina-reranker-v2-base-multilingual",
        architecture="XLMRobertaForSequenceClassification",
        dtype="float32",
    )
]
27
28


29
30
31
@pytest.mark.parametrize("model_info", EMBEDDING_MODELS)
def test_embed_models_mteb(hf_runner, vllm_runner,
                           model_info: EmbedModelInfo) -> None:
32

33
34
    def hf_model_callback(model):
        model.encode = partial(model.encode, task="text-matching")
35

36
37
38
39
    mteb_test_embed_models(hf_runner,
                           vllm_runner,
                           model_info,
                           hf_model_callback=hf_model_callback)
40
41


42
43
44
45
@pytest.mark.parametrize("model_info", EMBEDDING_MODELS)
def test_embed_models_correctness(hf_runner, vllm_runner,
                                  model_info: EmbedModelInfo,
                                  example_prompts) -> None:
46

47
48
    def hf_model_callback(model):
        model.encode = partial(model.encode, task="text-matching")
49

50
51
52
53
54
    correctness_test_embed_models(hf_runner,
                                  vllm_runner,
                                  model_info,
                                  example_prompts,
                                  hf_model_callback=hf_model_callback)
55
56


57
58
59
60
61
62
@pytest.mark.parametrize("model_info", RERANK_MODELS)
def test_rerank_models_mteb(hf_runner, vllm_runner,
                            model_info: RerankModelInfo) -> None:
    mteb_test_rerank_models(hf_runner, vllm_runner, model_info)


63
@pytest.mark.parametrize("model_info", EMBEDDING_MODELS)
64
65
66
67
68
@pytest.mark.parametrize("dtype", ["half"])
@pytest.mark.parametrize("dimensions", [16, 32])
def test_matryoshka(
    hf_runner,
    vllm_runner,
69
    model_info,
70
71
    dtype: str,
    dimensions: int,
72
    example_prompts,
73
74
    monkeypatch,
) -> None:
75
76
    if not model_info.is_matryoshka:
        pytest.skip("Model is not matryoshka")
77

78
79
    # ST will strip the input texts, see test_embedding.py
    example_prompts = [str(s).strip() for s in example_prompts]
80
81

    with hf_runner(
82
            model_info.name,
83
84
85
86
87
88
            dtype=dtype,
            is_sentence_transformer=True,
    ) as hf_model:
        hf_outputs = hf_model.encode(example_prompts, task="text-matching")
        hf_outputs = matryoshka_fy(hf_outputs, dimensions)

89
90
91
    with vllm_runner(model_info.name,
                     task="embed",
                     dtype=dtype,
92
                     max_model_len=None) as vllm_model:
93
94
        assert vllm_model.model.llm_engine.model_config.is_matryoshka

95
96
97
98
99
100
        matryoshka_dimensions = (
            vllm_model.model.llm_engine.model_config.matryoshka_dimensions)
        assert matryoshka_dimensions is not None

        if dimensions not in matryoshka_dimensions:
            with pytest.raises(ValueError):
101
                vllm_model.embed(
102
103
104
                    example_prompts,
                    pooling_params=PoolingParams(dimensions=dimensions))
        else:
105
            vllm_outputs = vllm_model.embed(
106
107
108
109
110
111
112
113
114
115
                example_prompts,
                pooling_params=PoolingParams(dimensions=dimensions))

            check_embeddings_close(
                embeddings_0_lst=hf_outputs,
                embeddings_1_lst=vllm_outputs,
                name_0="hf",
                name_1="vllm",
                tol=1e-2,
            )