test_offline_mode.py 3.3 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
Joe Runde's avatar
Joe Runde committed
3
4
5
"""Tests for HF_HUB_OFFLINE mode"""
import importlib
import sys
6
import os
Joe Runde's avatar
Joe Runde committed
7
8

import pytest
9
import urllib3
Joe Runde's avatar
Joe Runde committed
10
11

from vllm import LLM
12
from vllm.distributed import cleanup_dist_env_and_memory
13
from ...utils import models_path_prefix
Joe Runde's avatar
Joe Runde committed
14
15


16
17
MODEL_CONFIGS = [
    {
zhuwenwen's avatar
zhuwenwen committed
18
        "model": os.path.join(models_path_prefix, "facebook/opt-125m"),
19
20
21
22
23
24
25
26
        "enforce_eager": True,
        "gpu_memory_utilization": 0.20,
        "max_model_len": 64,
        "max_num_batched_tokens": 64,
        "max_num_seqs": 64,
        "tensor_parallel_size": 1,
    },
    {
zhuwenwen's avatar
zhuwenwen committed
27
        "model":  os.path.join(models_path_prefix, "mistralai/Mistral-7B-Instruct-v0.1"),
28
29
30
31
32
33
34
35
        "enforce_eager": True,
        "gpu_memory_utilization": 0.95,
        "max_model_len": 64,
        "max_num_batched_tokens": 64,
        "max_num_seqs": 64,
        "tensor_parallel_size": 1,
        "tokenizer_mode": "mistral",
    },
36
37
38
39
40
41
42
43
44
    {
        "model": "sentence-transformers/all-MiniLM-L12-v2",
        "enforce_eager": True,
        "gpu_memory_utilization": 0.20,
        "max_model_len": 64,
        "max_num_batched_tokens": 64,
        "max_num_seqs": 64,
        "tensor_parallel_size": 1,
    },
45
]
Joe Runde's avatar
Joe Runde committed
46
47
48


@pytest.fixture(scope="module")
49
50
51
52
53
def cache_models():
    # Cache model files first
    for model_config in MODEL_CONFIGS:
        LLM(**model_config)
        cleanup_dist_env_and_memory()
Joe Runde's avatar
Joe Runde committed
54

55
    yield
Joe Runde's avatar
Joe Runde committed
56
57
58


@pytest.mark.skip_global_cleanup
59
@pytest.mark.usefixtures("cache_models")
60
def test_offline_mode(monkeypatch: pytest.MonkeyPatch):
Joe Runde's avatar
Joe Runde committed
61
    # Set HF to offline mode and ensure we can still construct an LLM
62
63
64
65
    with monkeypatch.context() as m:
        try:
            m.setenv("HF_HUB_OFFLINE", "1")
            m.setenv("VLLM_NO_USAGE_STATS", "1")
66

67
68
            def disable_connect(*args, **kwargs):
                raise RuntimeError("No http calls allowed")
69

70
71
72
73
74
75
76
77
78
79
            m.setattr(
                urllib3.connection.HTTPConnection,
                "connect",
                disable_connect,
            )
            m.setattr(
                urllib3.connection.HTTPSConnection,
                "connect",
                disable_connect,
            )
80

81
82
83
84
85
86
87
88
89
90
            # Need to re-import huggingface_hub
            # and friends to setup offline mode
            _re_import_modules()
            # Cached model files should be used in offline mode
            for model_config in MODEL_CONFIGS:
                LLM(**model_config)
        finally:
            # Reset the environment after the test
            # NB: Assuming tests are run in online mode
            _re_import_modules()
Joe Runde's avatar
Joe Runde committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113


def _re_import_modules():
    hf_hub_module_names = [
        k for k in sys.modules if k.startswith("huggingface_hub")
    ]
    transformers_module_names = [
        k for k in sys.modules if k.startswith("transformers")
        and not k.startswith("transformers_modules")
    ]

    reload_exception = None
    for module_name in hf_hub_module_names + transformers_module_names:
        try:
            importlib.reload(sys.modules[module_name])
        except Exception as e:
            reload_exception = e
            # Try to continue clean up so that other tests are less likely to
            # be affected

    # Error this test if reloading a module failed
    if reload_exception is not None:
        raise reload_exception