Skip to content

Commit bcb6fd0

Browse files
DarkLight1337lywa1998
authored andcommitted
[Misc] Move utils to avoid conflicts with stdlib, and move tests (vllm-project#27169)
Signed-off-by: DarkLight1337 <tlleungac@connect.ust.hk>
1 parent 34714df commit bcb6fd0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+246
-237
lines changed

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
from vllm.outputs import RequestOutput
6161
from vllm.sampling_params import BeamSearchParams
6262
from vllm.transformers_utils.utils import maybe_model_redirect
63-
from vllm.utils.collections import is_list_of
63+
from vllm.utils.collection_utils import is_list_of
6464
from vllm.utils.torch_utils import set_default_torch_num_threads
6565

6666
logger = init_logger(__name__)

tests/lora/test_add_lora.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from vllm.inputs import TextPrompt
1313
from vllm.lora.request import LoRARequest
1414
from vllm.sampling_params import SamplingParams
15-
from vllm.utils.asyncio import merge_async_iterators
15+
from vllm.utils.async_utils import merge_async_iterators
1616

1717
MODEL_PATH = "zai-org/chatglm3-6b"
1818
LORA_RANK = 64

tests/models/multimodal/generation/test_common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
)
1818

1919
from vllm.platforms import current_platform
20-
from vllm.utils.functools import identity
20+
from vllm.utils.func_utils import identity
2121

2222
from ....conftest import (
2323
IMAGE_ASSETS,

tests/models/multimodal/generation/vlm_utils/model_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from transformers.video_utils import VideoMetadata
2626

2727
from vllm.logprobs import SampleLogprobs
28-
from vllm.utils.collections import is_list_of
28+
from vllm.utils.collection_utils import is_list_of
2929

3030
from .....conftest import HfRunner, ImageAsset, ImageTestAssets
3131
from .types import RunnerOutput

tests/models/multimodal/processing/test_tensor_schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from vllm.multimodal.processing import BaseMultiModalProcessor, InputProcessingContext
3535
from vllm.multimodal.utils import group_mm_kwargs_by_modality
3636
from vllm.transformers_utils.tokenizer import cached_tokenizer_from_config
37-
from vllm.utils.collections import is_list_of
37+
from vllm.utils.collection_utils import is_list_of
3838
from vllm.utils.torch_utils import set_default_torch_dtype
3939

4040
from ...registry import _MULTIMODAL_EXAMPLE_MODELS, HF_EXAMPLE_MODELS

tests/utils_/test_async_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import pytest
77

8-
from vllm.utils.asyncio import merge_async_iterators
8+
from vllm.utils.async_utils import merge_async_iterators
99

1010

1111
async def _mock_async_iterator(idx: int):

tests/utils_/test_collections.py renamed to tests/utils_/test_collection_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
33
import pytest
44

5-
from vllm.utils.collections import swap_dict_values
5+
from vllm.utils.collection_utils import swap_dict_values
66

77

88
@pytest.mark.parametrize(

tests/utils_/test_func_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import pytest
66

7-
from vllm.utils.functools import deprecate_kwargs, supports_kw
7+
from vllm.utils.func_utils import deprecate_kwargs, supports_kw
88

99
from ..utils import error_on_warning
1010

tests/utils_/test_hashing.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3+
import hashlib
4+
import pickle
5+
6+
import pytest
7+
8+
from vllm.utils.hashing import sha256
9+
10+
11+
@pytest.mark.parametrize("input", [(), ("abc",), (None,), (None, bool, [1, 2, 3])])
12+
def test_sha256(input: tuple):
13+
digest = sha256(input)
14+
assert digest is not None
15+
assert isinstance(digest, bytes)
16+
assert digest != b""
17+
18+
input_bytes = pickle.dumps(input, protocol=pickle.HIGHEST_PROTOCOL)
19+
assert digest == hashlib.sha256(input_bytes).digest()
20+
21+
# hashing again, returns the same value
22+
assert digest == sha256(input)
23+
24+
# hashing different input, returns different value
25+
assert digest != sha256(input + (1,))

tests/utils_/test_mem_utils.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3+
import torch
4+
from vllm_test_utils.monitor import monitor
5+
6+
from vllm.utils.mem_utils import MemorySnapshot, memory_profiling
7+
8+
from ..utils import create_new_process_for_each_test
9+
10+
11+
@create_new_process_for_each_test()
12+
def test_memory_profiling():
13+
# Fake out some model loading + inference memory usage to test profiling
14+
# Memory used by other processes will show up as cuda usage outside of torch
15+
from vllm.distributed.device_communicators.cuda_wrapper import CudaRTLibrary
16+
17+
lib = CudaRTLibrary()
18+
# 512 MiB allocation outside of this instance
19+
handle1 = lib.cudaMalloc(512 * 1024 * 1024)
20+
21+
baseline_snapshot = MemorySnapshot()
22+
23+
# load weights
24+
25+
weights = torch.randn(128, 1024, 1024, device="cuda", dtype=torch.float32)
26+
27+
weights_memory = 128 * 1024 * 1024 * 4 # 512 MiB
28+
29+
def measure_current_non_torch():
30+
free, total = torch.cuda.mem_get_info()
31+
current_used = total - free
32+
current_torch = torch.cuda.memory_reserved()
33+
current_non_torch = current_used - current_torch
34+
return current_non_torch
35+
36+
with (
37+
memory_profiling(
38+
baseline_snapshot=baseline_snapshot, weights_memory=weights_memory
39+
) as result,
40+
monitor(measure_current_non_torch) as monitored_values,
41+
):
42+
# make a memory spike, 1 GiB
43+
spike = torch.randn(256, 1024, 1024, device="cuda", dtype=torch.float32)
44+
del spike
45+
46+
# Add some extra non-torch memory 256 MiB (simulate NCCL)
47+
handle2 = lib.cudaMalloc(256 * 1024 * 1024)
48+
49+
# this is an analytic value, it is exact,
50+
# we only have 256 MiB non-torch memory increase
51+
measured_diff = monitored_values.values[-1] - monitored_values.values[0]
52+
assert measured_diff == 256 * 1024 * 1024
53+
54+
# Check that the memory usage is within 5% of the expected values
55+
# 5% tolerance is caused by cuda runtime.
56+
# we cannot control cuda runtime in the granularity of bytes,
57+
# which causes a small error (<10 MiB in practice)
58+
non_torch_ratio = result.non_torch_increase / (256 * 1024 * 1024) # noqa
59+
assert abs(non_torch_ratio - 1) <= 0.05
60+
assert result.torch_peak_increase == 1024 * 1024 * 1024
61+
del weights
62+
lib.cudaFree(handle1)
63+
lib.cudaFree(handle2)

0 commit comments

Comments
 (0)