Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion tests/v1/core/test_kv_cache_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import torch

import vllm.v1.core.kv_cache_utils as kv_cache_utils
from vllm.config import ModelConfig, SchedulerConfig, VllmConfig
from vllm.config import DeviceConfig, ModelConfig, SchedulerConfig, VllmConfig
from vllm.config.kv_events import KVEventsConfig
from vllm.lora.request import LoRARequest
from vllm.multimodal.inputs import (
Expand Down Expand Up @@ -2462,6 +2462,30 @@ def test_get_kv_cache_configs_attention_free():
]


def test_warns_when_num_gpu_blocks_override_exceeds_profiled_capacity(caplog):
model_config = ModelConfig(max_model_len=16)
vllm_config = VllmConfig(
model_config=model_config, device_config=DeviceConfig(device="cpu")
)
vllm_config.cache_config.num_gpu_blocks_override = 33
mem_per_block_per_layer = 16 * 2 * 64 * 4 * 2
kv_cache_specs = {
"layer_1": new_kv_cache_spec(),
"layer_2": new_kv_cache_spec(),
}

with caplog.at_level("WARNING"):
get_kv_cache_configs(
vllm_config,
[kv_cache_specs],
[mem_per_block_per_layer * 2 * 32],
)

assert "exceeds the capacity of 32 blocks implied by available KV memory" in (
caplog.text
)


def test_generate_uniform_type_kv_cache_specs():
# All layers are full attention, can be merged
kv_cache_specs = {
Expand Down
218 changes: 218 additions & 0 deletions tests/v1/worker/test_prompt_logprobs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project

from types import SimpleNamespace
from unittest.mock import Mock

import numpy as np
import pytest
import torch

from vllm.v1.worker.gpu.model_runner import GPUModelRunner
from vllm.v1.worker.gpu.sample import prompt_logprob
from vllm.v1.worker.gpu.sample.prompt_logprob import (
PromptLogprobsWorker,
compute_prompt_logprobs_with_chunking,
)


@pytest.mark.parametrize("chunk_size", [0, -1])
def test_prompt_logprobs_worker_rejects_invalid_chunk_size(
monkeypatch: pytest.MonkeyPatch, chunk_size: int
):
monkeypatch.setenv("VLLM_PROMPT_LOGPROBS_CHUNK_SIZE", str(chunk_size))

with pytest.raises(ValueError, match="must be greater than zero"):
PromptLogprobsWorker(max_num_reqs=1)


def test_prompt_logprobs_chunk_size_bounds_logits_rows(
monkeypatch: pytest.MonkeyPatch,
):
chunk_rows: list[int] = []

def logits_fn(hidden_states: torch.Tensor) -> torch.Tensor:
chunk_rows.append(hidden_states.shape[0])
return torch.zeros((hidden_states.shape[0], 8))

def fake_compute_topk_logprobs(
logits: torch.Tensor,
num_logprobs: int,
sampled_token_ids: torch.Tensor,
) -> SimpleNamespace:
del num_logprobs
return SimpleNamespace(
logprob_token_ids=sampled_token_ids.unsqueeze(-1),
logprobs=torch.zeros((logits.shape[0], 1)),
selected_token_ranks=torch.ones(logits.shape[0], dtype=torch.int64),
)

monkeypatch.setattr(
prompt_logprob, "compute_topk_logprobs", fake_compute_topk_logprobs
)
prompt_token_ids = torch.arange(5)
prompt_hidden_states = torch.zeros((5, 4))

token_ids, logprobs, ranks = compute_prompt_logprobs_with_chunking(
prompt_token_ids,
prompt_hidden_states,
logits_fn,
num_prompt_logprobs=1,
chunk_size=2,
)

assert chunk_rows == [2, 2, 1]
torch.testing.assert_close(token_ids.squeeze(-1), prompt_token_ids)
assert logprobs.shape == (5, 1)
torch.testing.assert_close(ranks, torch.ones(5, dtype=torch.int64))


def test_prompt_logprobs_profile_uses_full_batch(
monkeypatch: pytest.MonkeyPatch,
):
monkeypatch.setenv("VLLM_PROMPT_LOGPROBS_CHUNK_SIZE", "2")
worker = PromptLogprobsWorker(max_num_reqs=1)
helper = Mock()
monkeypatch.setattr(prompt_logprob, "compute_prompt_logprobs_with_chunking", helper)
hidden_states = torch.zeros((5, 4))
logits_fn = Mock()

worker.profile_run(logits_fn, hidden_states, max_num_logprobs=-1)

prompt_token_ids, profiled_hidden_states, fn, max_logprobs, chunk_size = (
helper.call_args.args
)
assert prompt_token_ids.shape == (5,)
assert profiled_hidden_states is hidden_states
assert fn is logits_fn
assert max_logprobs == -1
assert chunk_size == 2


def test_prompt_logprobs_accumulates_chunks_on_cpu(
monkeypatch: pytest.MonkeyPatch,
):
worker = PromptLogprobsWorker(max_num_reqs=1)
worker.uses_prompt_logprobs[0] = True
worker.num_prompt_logprobs[0] = 1
worker.in_progress_prompt_logprobs["req"] = None

monkeypatch.setattr(
prompt_logprob,
"get_prompt_logprobs_token_ids",
lambda *args, **kwargs: torch.arange(args[0]),
)
chunks = iter(
[
(
torch.tensor([[10, 11], [20, 21]]),
torch.tensor([[1.0, 1.1], [2.0, 2.1]]),
torch.tensor([1, 2]),
),
(
torch.tensor([[30, 31], [40, 41], [50, 51]]),
torch.tensor([[3.0, 3.1], [4.0, 4.1], [5.0, 5.1]]),
torch.tensor([3, 4, 5]),
),
]
)
monkeypatch.setattr(
prompt_logprob,
"compute_prompt_logprobs_with_chunking",
lambda *args, **kwargs: next(chunks),
)
synchronize = Mock()
monkeypatch.setattr(torch.accelerator, "synchronize", synchronize)

def make_batch(computed: int, scheduled: int) -> SimpleNamespace:
return SimpleNamespace(
idx_mapping_np=np.array([0]),
idx_mapping=torch.tensor([0]),
num_computed_prefill_tokens_np=np.array([computed]),
prefill_len_np=np.array([5]),
num_scheduled_tokens=np.array([scheduled]),
num_tokens=scheduled,
query_start_loc=torch.tensor([0, scheduled]),
query_start_loc_np=np.array([0, scheduled]),
req_ids=["req"],
)

output = worker.compute_prompt_logprobs(
Mock(),
torch.zeros((2, 4)),
make_batch(computed=0, scheduled=2),
torch.zeros((1, 5), dtype=torch.int64),
torch.zeros(1, dtype=torch.int64),
np.array([5]),
)
assert output == {}
in_progress = worker.in_progress_prompt_logprobs["req"]
assert in_progress is not None
assert in_progress.logprobs.device.type == "cpu"
synchronize.assert_not_called()

output = worker.compute_prompt_logprobs(
Mock(),
torch.zeros((3, 4)),
make_batch(computed=2, scheduled=3),
torch.zeros((1, 5), dtype=torch.int64),
torch.full((1,), 2, dtype=torch.int64),
np.array([5]),
)
result = output["req"]
torch.testing.assert_close(
result.logprob_token_ids,
torch.tensor([[10, 11], [20, 21], [30, 31], [40, 41]], dtype=torch.int32),
)
torch.testing.assert_close(
result.logprobs,
torch.tensor([[1.0, 1.1], [2.0, 2.1], [3.0, 3.1], [4.0, 4.1]]),
)
torch.testing.assert_close(
result.selected_token_ranks, torch.tensor([1, 2, 3, 4], dtype=torch.int32)
)
assert worker.in_progress_prompt_logprobs["req"] is None
synchronize.assert_not_called()


def test_model_runner_profiles_prompt_logprobs(
monkeypatch: pytest.MonkeyPatch,
):
hidden_states = torch.zeros((8, 4))
sample_hidden_states = hidden_states[:2]
logits_fn = Mock()
runner = object.__new__(GPUModelRunner)
runner.max_num_tokens = 8
runner.is_last_pp_rank = True
runner.pooling_runner = None
runner.model = SimpleNamespace(compute_logits=logits_fn)
runner.model_config = SimpleNamespace(max_logprobs=20)
runner.prompt_logprobs_worker = Mock(chunk_size=256)
runner._dummy_run = Mock(return_value=(hidden_states, sample_hidden_states))
runner._dummy_sampler_run = Mock()
monkeypatch.setattr(torch.accelerator, "synchronize", Mock())

GPUModelRunner.profile_run(runner)

runner._dummy_run.assert_called_once_with(8, skip_attn=True, is_profile=True)
runner._dummy_sampler_run.assert_called_once_with(sample_hidden_states)
runner.prompt_logprobs_worker.profile_run.assert_called_once_with(
logits_fn, hidden_states, 20
)


def test_non_last_pp_rank_does_not_profile_prompt_logprobs(
monkeypatch: pytest.MonkeyPatch,
):
hidden_states = torch.zeros((8, 4))
runner = object.__new__(GPUModelRunner)
runner.max_num_tokens = 8
runner.is_last_pp_rank = False
runner.pooling_runner = None
runner.prompt_logprobs_worker = Mock()
runner._dummy_run = Mock(return_value=(hidden_states, None))
monkeypatch.setattr(torch.accelerator, "synchronize", Mock())

GPUModelRunner.profile_run(runner)

runner.prompt_logprobs_worker.profile_run.assert_not_called()
8 changes: 7 additions & 1 deletion vllm/envs.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@
VLLM_RUST_FRONTEND_PATH: str | None = "auto"
VLLM_SERVER_DEV_MODE: bool = False
VLLM_V1_OUTPUT_PROC_CHUNK_SIZE: int = 128
VLLM_PROMPT_LOGPROBS_CHUNK_SIZE: int = 1024
VLLM_MLA_DISABLE: bool = False
VLLM_DSPARK_DYNAMIC_DRAFT_DEPTH: bool = False
VLLM_DSPARK_DYNAMIC_DRAFT_DEPTH_WINDOW: int = 8
Expand Down Expand Up @@ -1551,6 +1552,11 @@ def _resolve_rust_frontend_path() -> str | None:
"VLLM_V1_OUTPUT_PROC_CHUNK_SIZE": lambda: int(
os.getenv("VLLM_V1_OUTPUT_PROC_CHUNK_SIZE", "128")
),
# Maximum prompt-token rows materialized in the full-vocabulary logits
# tensor at once while computing V1 prompt logprobs.
"VLLM_PROMPT_LOGPROBS_CHUNK_SIZE": lambda: int(
os.getenv("VLLM_PROMPT_LOGPROBS_CHUNK_SIZE", "1024")
),
# If set, vLLM will disable the MLA attention optimizations.
"VLLM_MLA_DISABLE": lambda: bool(int(os.getenv("VLLM_MLA_DISABLE", "0"))),
# Physically shorten DSpark's next draft block from the historical
Expand Down Expand Up @@ -2292,7 +2298,6 @@ def _resolve_rust_frontend_path() -> str | None:
# Prebuilt exllamav3 extension location and torch-ABI compatibility shim.
"VLLM_EXL3_EXT_PATH": lambda: os.getenv("VLLM_EXL3_EXT_PATH"),
"VLLM_EXL3_ABI_SHIM": lambda: os.getenv("VLLM_EXL3_ABI_SHIM"),

}


Expand Down Expand Up @@ -2438,6 +2443,7 @@ def compile_factors() -> dict[str, object]:
"VLLM_WORKER_MULTIPROC_METHOD",
"VLLM_ENABLE_V1_MULTIPROCESSING",
"VLLM_V1_OUTPUT_PROC_CHUNK_SIZE",
"VLLM_PROMPT_LOGPROBS_CHUNK_SIZE",
"VLLM_CPU_KVCACHE_SPACE",
"VLLM_CPU_MOE_PREPACK",
"VLLM_ZENTORCH_WEIGHT_PREPACK",
Expand Down
11 changes: 10 additions & 1 deletion vllm/v1/core/kv_cache_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2363,9 +2363,18 @@ def get_kv_cache_configs(
adjusted_memory.append(avail_mem)
continue
bytes_per_block = _pool_bytes_per_block(vllm_config, groups)
profiled_num_blocks = avail_mem // bytes_per_block
if override > profiled_num_blocks:
logger.warning(
"num_gpu_blocks_override=%d exceeds the capacity of %d "
"blocks implied by available KV memory. The override "
"bypasses memory accounting and can cause runtime CUDA OOMs.",
override,
profiled_num_blocks,
)
logger.info(
"Overriding num_gpu_blocks=%d with num_gpu_blocks_override=%d",
avail_mem // bytes_per_block,
profiled_num_blocks,
override,
)
adjusted_memory.append(override * bytes_per_block)
Expand Down
11 changes: 11 additions & 0 deletions vllm/v1/worker/gpu/model_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,17 @@ def profile_run(self) -> None:
assert sample_hidden_states is not None
if self.pooling_runner is None:
self._dummy_sampler_run(sample_hidden_states)
assert hidden_states is not None
assert self.prompt_logprobs_worker is not None
self.prompt_logprobs_worker.profile_run(
self.model.compute_logits,
hidden_states,
self.model_config.max_logprobs,
)
logger.info_once(
"Profiled V1 prompt-logprobs workspace with chunk size %d",
self.prompt_logprobs_worker.chunk_size,
)
else:
self._dummy_pooler_run(hidden_states)

Expand Down
Loading