[Bugfix] Make DSV4 sparse MLA work end-to-end for plain decode, MTP, and DSpark (#51538)
This commit is contained in:
@@ -7,6 +7,9 @@ from types import SimpleNamespace
|
||||
import torch
|
||||
|
||||
from vllm.config import set_current_vllm_config
|
||||
from vllm.models.deepseek_v4.nvidia.flashinfer_sparse import (
|
||||
_required_sm120_sparse_topk,
|
||||
)
|
||||
from vllm.platforms.interface import DeviceCapability
|
||||
from vllm.utils import flashinfer as fi_utils
|
||||
from vllm.v1.attention.backends.mla.flashinfer_mla_sparse import (
|
||||
@@ -52,3 +55,33 @@ def test_v32_glm_sm120_backend_accepts_glm_block_size(
|
||||
)
|
||||
|
||||
assert invalid_reasons == []
|
||||
|
||||
|
||||
def test_sm120_dsv4_capability_checks_exact_dispatch_shape(monkeypatch) -> None:
|
||||
fake_module = SimpleNamespace(
|
||||
_DECODE_DSV4_DISPATCH=frozenset({(32, 128), (32, 192)})
|
||||
)
|
||||
monkeypatch.setattr(fi_utils, "has_flashinfer_sparse_mla_sm120", lambda: True)
|
||||
monkeypatch.setattr(fi_utils, "_get_submodule", lambda _name: fake_module)
|
||||
fi_utils.has_flashinfer_sparse_mla_sm120_config.cache_clear()
|
||||
|
||||
assert fi_utils.has_flashinfer_sparse_mla_sm120_config(32, 128)
|
||||
assert fi_utils.has_flashinfer_sparse_mla_sm120_config(32, 192)
|
||||
assert not fi_utils.has_flashinfer_sparse_mla_sm120_config(32, 256)
|
||||
assert not fi_utils.has_flashinfer_sparse_mla_sm120_config(16, 192)
|
||||
|
||||
fi_utils.has_flashinfer_sparse_mla_sm120_config.cache_clear()
|
||||
|
||||
|
||||
def test_sm120_dsv4_required_topk_tracks_dspark_width() -> None:
|
||||
causal = SimpleNamespace(
|
||||
attention_config=SimpleNamespace(use_non_causal=False),
|
||||
speculative_config=SimpleNamespace(num_speculative_tokens=5),
|
||||
)
|
||||
dspark = SimpleNamespace(
|
||||
attention_config=SimpleNamespace(use_non_causal=True),
|
||||
speculative_config=SimpleNamespace(num_speculative_tokens=5),
|
||||
)
|
||||
|
||||
assert _required_sm120_sparse_topk(causal, 128) == 128
|
||||
assert _required_sm120_sparse_topk(dspark, 128) == 192
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
from types import SimpleNamespace
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
import torch
|
||||
|
||||
from vllm.v1.attention.backends.utils import PAD_SLOT_ID
|
||||
from vllm.v1.worker.gpu.spec_decode.dflash.speculator import (
|
||||
prepare_dflash_inputs,
|
||||
)
|
||||
|
||||
pytestmark = pytest.mark.skipif(
|
||||
not torch.cuda.is_available(), reason="requires a CUDA device"
|
||||
)
|
||||
|
||||
|
||||
def _run_prepare(*, target_positions: list[int], block_table_values: list[int]):
|
||||
device = torch.device("cuda")
|
||||
max_num_reqs = 4
|
||||
max_num_tokens = 16
|
||||
num_speculative_steps = 3
|
||||
|
||||
input_buffers = SimpleNamespace(
|
||||
input_ids=torch.full((max_num_tokens,), -1, dtype=torch.int32, device=device),
|
||||
positions=torch.full((max_num_tokens,), -1, dtype=torch.int64, device=device),
|
||||
query_start_loc=torch.full(
|
||||
(max_num_reqs + 1,), -1, dtype=torch.int32, device=device
|
||||
),
|
||||
seq_lens=torch.full((max_num_reqs,), -1, dtype=torch.int32, device=device),
|
||||
)
|
||||
input_batch = SimpleNamespace(
|
||||
num_reqs=1,
|
||||
num_scheduled_tokens=np.array([4], dtype=np.int32),
|
||||
positions=torch.tensor(target_positions, dtype=torch.int64, device=device),
|
||||
query_start_loc=torch.tensor([0, 4], dtype=torch.int32, device=device),
|
||||
idx_mapping=torch.tensor([2], dtype=torch.int32, device=device),
|
||||
)
|
||||
query_slot_mapping = torch.full(
|
||||
(max_num_tokens,), -2, dtype=torch.int64, device=device
|
||||
)
|
||||
context_positions = torch.full(
|
||||
(max_num_tokens,), -1, dtype=torch.int64, device=device
|
||||
)
|
||||
context_slot_mapping = torch.full(
|
||||
(max_num_tokens,), -2, dtype=torch.int64, device=device
|
||||
)
|
||||
sample_indices = torch.full(
|
||||
(max_num_reqs * num_speculative_steps,),
|
||||
-1,
|
||||
dtype=torch.int64,
|
||||
device=device,
|
||||
)
|
||||
sample_pos = torch.full_like(sample_indices, -1)
|
||||
sample_idx_mapping = torch.full(
|
||||
sample_indices.shape, -1, dtype=torch.int32, device=device
|
||||
)
|
||||
temperature = torch.zeros(max_num_reqs, dtype=torch.float32, device=device)
|
||||
seeds = torch.zeros(max_num_reqs, dtype=torch.int64, device=device)
|
||||
input_temperature = torch.tensor(
|
||||
[0.0, 0.0, 1.0, 0.0], dtype=torch.float32, device=device
|
||||
)
|
||||
input_seeds = torch.tensor([0, 0, 17, 0], dtype=torch.int64, device=device)
|
||||
last_sampled = torch.tensor([0, 0, 99, 0], dtype=torch.int64, device=device)
|
||||
next_prefill_tokens = torch.zeros_like(last_sampled)
|
||||
block_table = torch.tensor([block_table_values], dtype=torch.int32, device=device)
|
||||
|
||||
prepare_dflash_inputs(
|
||||
input_buffers,
|
||||
query_slot_mapping,
|
||||
context_positions,
|
||||
context_slot_mapping,
|
||||
sample_indices,
|
||||
sample_pos,
|
||||
sample_idx_mapping,
|
||||
temperature,
|
||||
seeds,
|
||||
input_batch,
|
||||
torch.tensor([1], dtype=torch.int32, device=device),
|
||||
torch.tensor([2], dtype=torch.int32, device=device),
|
||||
last_sampled,
|
||||
next_prefill_tokens,
|
||||
input_temperature,
|
||||
input_seeds,
|
||||
block_table,
|
||||
4,
|
||||
123,
|
||||
num_speculative_steps,
|
||||
num_speculative_steps,
|
||||
max_num_reqs,
|
||||
max_num_tokens,
|
||||
128,
|
||||
sample_from_anchor=True,
|
||||
)
|
||||
torch.accelerator.synchronize()
|
||||
return SimpleNamespace(
|
||||
input_buffers=input_buffers,
|
||||
query_slot_mapping=query_slot_mapping.cpu(),
|
||||
context_positions=context_positions.cpu(),
|
||||
context_slot_mapping=context_slot_mapping.cpu(),
|
||||
sample_indices=sample_indices.cpu(),
|
||||
sample_pos=sample_pos.cpu(),
|
||||
sample_idx_mapping=sample_idx_mapping.cpu(),
|
||||
temperature=temperature.cpu(),
|
||||
seeds=seeds.cpu(),
|
||||
)
|
||||
|
||||
|
||||
def test_prepare_dflash_inputs_excludes_rejected_context_suffix():
|
||||
# Positions 10/11 use physical block 7. Rejected positions 12/13 would use
|
||||
# block 8, but must be PAD context rather than contaminating draft KV.
|
||||
out = _run_prepare(
|
||||
target_positions=[10, 11, 12, 13],
|
||||
block_table_values=[0, 0, 7, 8, 9, 10, 11, 12],
|
||||
)
|
||||
|
||||
assert out.context_positions[:4].tolist() == [10, 11, 0, 0]
|
||||
assert out.context_slot_mapping[:4].tolist() == [30, 31, PAD_SLOT_ID, PAD_SLOT_ID]
|
||||
|
||||
# The replacement query starts immediately after the two valid rows and
|
||||
# advances from the last accepted position (11).
|
||||
assert out.input_buffers.input_ids[:3].cpu().tolist() == [99, 123, 123]
|
||||
assert out.input_buffers.positions[:3].cpu().tolist() == [12, 13, 14]
|
||||
assert out.query_slot_mapping[:3].tolist() == [32, 33, 34]
|
||||
assert out.sample_indices[:3].tolist() == [0, 1, 2]
|
||||
assert out.sample_pos[:3].tolist() == [13, 14, 15]
|
||||
assert out.sample_idx_mapping[:3].tolist() == [2, 2, 2]
|
||||
assert out.temperature[2].item() == 1.0
|
||||
assert out.seeds[2].item() == 17
|
||||
|
||||
|
||||
def test_prepare_dflash_inputs_never_writes_the_null_block():
|
||||
# The valid context uses logical block 0 and the replacement query uses
|
||||
# logical block 1. Both map to the null block and must remain unwritable.
|
||||
out = _run_prepare(
|
||||
target_positions=[2, 3, 4, 5],
|
||||
block_table_values=[0, 0, 7, 8, 9, 10, 11, 12],
|
||||
)
|
||||
|
||||
assert out.context_slot_mapping[:4].tolist() == [
|
||||
PAD_SLOT_ID,
|
||||
PAD_SLOT_ID,
|
||||
PAD_SLOT_ID,
|
||||
PAD_SLOT_ID,
|
||||
]
|
||||
assert out.query_slot_mapping[:3].tolist() == [
|
||||
PAD_SLOT_ID,
|
||||
PAD_SLOT_ID,
|
||||
PAD_SLOT_ID,
|
||||
]
|
||||
@@ -0,0 +1,102 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
from typing import cast
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
|
||||
import vllm.v1.worker.workspace as workspace
|
||||
from vllm.config import VllmConfig
|
||||
from vllm.v1.worker.gpu_worker import _num_workspace_lanes
|
||||
|
||||
|
||||
class _SpecConfig:
|
||||
def __init__(self, dspark: bool) -> None:
|
||||
self._dspark = dspark
|
||||
|
||||
def use_dspark(self) -> bool:
|
||||
return self._dspark
|
||||
|
||||
|
||||
class _VllmConfig:
|
||||
def __init__(self, spec_config: _SpecConfig | None) -> None:
|
||||
self.speculative_config = spec_config
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("use_v2_model_runner", "spec_config", "expected"),
|
||||
[
|
||||
(True, _SpecConfig(True), 2),
|
||||
(False, _SpecConfig(True), 1),
|
||||
(True, _SpecConfig(False), 1),
|
||||
(True, None, 1),
|
||||
],
|
||||
)
|
||||
def test_workspace_lane_count_is_dspark_only(
|
||||
use_v2_model_runner: bool,
|
||||
spec_config: _SpecConfig | None,
|
||||
expected: int,
|
||||
) -> None:
|
||||
config = cast(VllmConfig, _VllmConfig(spec_config))
|
||||
assert _num_workspace_lanes(config, use_v2_model_runner) == expected
|
||||
|
||||
|
||||
def test_workspace_lanes_do_not_alias_and_restore_context(monkeypatch) -> None:
|
||||
monkeypatch.setattr(workspace, "dbo_current_ubatch_id", lambda: 0)
|
||||
manager = workspace.WorkspaceManager(
|
||||
torch.device("cpu"), num_ubatches=2, num_lanes=2
|
||||
)
|
||||
|
||||
assert manager._current_workspaces == [None, None, None, None]
|
||||
|
||||
(target,) = manager.get_simultaneous(((512,), torch.uint8))
|
||||
with workspace.use_workspace_lane(1):
|
||||
(draft,) = manager.get_simultaneous(((256,), torch.uint8))
|
||||
(draft_reused,) = manager.get_simultaneous(((8,), torch.uint8))
|
||||
(target_reused,) = manager.get_simultaneous(((8,), torch.uint8))
|
||||
|
||||
assert manager._current_workspaces[0].numel() == 512 # type: ignore[union-attr]
|
||||
assert manager._current_workspaces[1].numel() == 256 # type: ignore[union-attr]
|
||||
assert manager._current_workspaces[2:] == [None, None]
|
||||
assert target.data_ptr() != draft.data_ptr()
|
||||
assert draft.data_ptr() == draft_reused.data_ptr()
|
||||
assert target.data_ptr() == target_reused.data_ptr()
|
||||
|
||||
|
||||
def test_workspace_lanes_compose_with_ubatches(monkeypatch) -> None:
|
||||
active_ubatch = [0]
|
||||
monkeypatch.setattr(workspace, "dbo_current_ubatch_id", lambda: active_ubatch[0])
|
||||
manager = workspace.WorkspaceManager(
|
||||
torch.device("cpu"), num_ubatches=2, num_lanes=2
|
||||
)
|
||||
|
||||
pointers = set()
|
||||
for ubatch_id in range(2):
|
||||
active_ubatch[0] = ubatch_id
|
||||
for lane in range(2):
|
||||
with workspace.use_workspace_lane(lane):
|
||||
(buffer,) = manager.get_simultaneous(((16,), torch.uint8))
|
||||
pointers.add(buffer.data_ptr())
|
||||
|
||||
assert len(pointers) == 4
|
||||
|
||||
|
||||
def test_workspace_lane_validation(monkeypatch) -> None:
|
||||
monkeypatch.setattr(workspace, "dbo_current_ubatch_id", lambda: 0)
|
||||
manager = workspace.WorkspaceManager(torch.device("cpu"), num_lanes=1)
|
||||
|
||||
with (
|
||||
pytest.raises(ValueError, match="non-negative"),
|
||||
workspace.use_workspace_lane(-1),
|
||||
):
|
||||
pass
|
||||
|
||||
with (
|
||||
workspace.use_workspace_lane(1),
|
||||
pytest.raises(RuntimeError, match="is not configured"),
|
||||
):
|
||||
manager.get_simultaneous(((1,), torch.uint8))
|
||||
|
||||
with pytest.raises(ValueError, match="at least one"):
|
||||
workspace.WorkspaceManager(torch.device("cpu"), num_lanes=0)
|
||||
Reference in New Issue
Block a user