Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
34 changes: 32 additions & 2 deletions tests/v1/attention/test_indexer_tp_row_shard.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ def all_gatherv(local, dim, sizes):
assert torch.all(buffer[decode + sum(sizes) :] == -7), "padding untouched"


@pytest.mark.parametrize("tp_size", [2, 4])
@pytest.mark.parametrize("tp_size", [2, 4, 8])
def test_balanced_row_shard_partitions_and_declines(tp_size: int) -> None:
"""The split covers every row once with >= 1 row per rank and declines
below the activation floor (or on a single rank)."""
n = indexer.MIN_TP_SHARD_ROWS_PER_RANK * tp_size
n = indexer.MIN_TP_SHARD_TOTAL_ROWS
shapes = [
([4 * n], [4 * n]), # fresh prompt: the full causal cost ramp
# uneven requests at different context depths
Expand All @@ -105,6 +105,36 @@ def test_balanced_row_shard_partitions_and_declines(tp_size: int) -> None:
assert indexer.balanced_prefill_row_shard(lens, lens, ratio, 1) is None


def test_row_shard_threshold_is_total_not_per_rank() -> None:
"""The activation floor counts TOTAL rows regardless of tp_size: a
per-rank floor would need 128K rows at TP8, i.e. an unschedulable
--max-num-batched-tokens of 128K on 80 GB Hopper nodes."""
n = indexer.MIN_TP_SHARD_TOTAL_ROWS
lens = torch.tensor([n], dtype=torch.int32)
for tp_size in (2, 4, 8):
assert indexer.balanced_prefill_row_shard(lens, lens, 4, tp_size) is not None


def test_tp2_old_per_rank_window_stays_replicated() -> None:
"""32K-64K total rows engaged on TP2 under the old per-rank floor
(16K rows/rank); the total floor now keeps them replicated — the
all_gatherv latency term is largest at small tp."""
window = torch.tensor([3 * indexer.MIN_TP_SHARD_TOTAL_ROWS // 4], dtype=torch.int32)
assert indexer.balanced_prefill_row_shard(window, window, 4, 2) is None


def test_row_shard_threshold_env_override(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""VLLM_INDEXER_TP_SHARD_MIN_ROWS overrides the built-in floor."""
monkeypatch.setattr(indexer.envs, "VLLM_INDEXER_TP_SHARD_MIN_ROWS", 2_000_000)
lens = torch.tensor([indexer.MIN_TP_SHARD_TOTAL_ROWS], dtype=torch.int32)
assert indexer.balanced_prefill_row_shard(lens, lens, 4, 8) is None
monkeypatch.setattr(indexer.envs, "VLLM_INDEXER_TP_SHARD_MIN_ROWS", 1)
small = torch.tensor([8], dtype=torch.int32)
assert indexer.balanced_prefill_row_shard(small, small, 4, 8) is not None


def test_row_sharding_gate_rejects_unsupported_configurations(
monkeypatch: pytest.MonkeyPatch,
) -> None:
Expand Down
6 changes: 6 additions & 0 deletions vllm/envs.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
VLLM_ENABLE_FLA_PACKED_RECURRENT_DECODE: bool = True
VLLM_GDN_DECODE_KERNEL: Literal["cuda", "triton"] = "cuda"
VLLM_DISABLE_PYNCCL: bool = False
VLLM_INDEXER_TP_SHARD_MIN_ROWS: int = 0
VLLM_USE_OINK_OPS: bool = False
VLLM_MXFP4_EMULATION_DEQUANT_AT_LOAD: bool = False
VLLM_MXFP8_EMULATION_DEQUANT_AT_LOAD: bool = True
Expand Down Expand Up @@ -1224,6 +1225,11 @@ def _resolve_rust_cli_path() -> str | None:
"VLLM_DISABLE_PYNCCL": lambda: (
os.getenv("VLLM_DISABLE_PYNCCL", "False").lower() in ("true", "1")
),
# Total scheduled prefill rows required before the sparse-indexer TP row
# sharding engages; 0 uses the built-in default (see mla/indexer.py).
"VLLM_INDEXER_TP_SHARD_MIN_ROWS": lambda: int(
os.getenv("VLLM_INDEXER_TP_SHARD_MIN_ROWS", "0")
),
# Optional: enable external Oink custom ops (e.g., Blackwell RMSNorm).
# Disabled by default.
"VLLM_USE_OINK_OPS": lambda: (
Expand Down
18 changes: 15 additions & 3 deletions vllm/v1/attention/backends/mla/indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,19 @@ def __call__(
)


# Below this many rows per rank, stay replicated (TP4 engages at 64K rows).
MIN_TP_SHARD_ROWS_PER_RANK = 16_384
# Below this many TOTAL prefill rows, stay replicated (each rank scores every
Comment thread
ZJY0516 marked this conversation as resolved.
Resolved
# row). The engagement crossover was calibrated on H200/TP4, where 64K total
# rows was the first measured win. Per rank, the saved replicated indexer work
# (~ (1 - 1/tp) * rows) and the all_gatherv index traffic
# (~ (tp - 1)/tp * rows * topk * 4B) scale identically with tp, so the
# crossover in TOTAL rows is tp-independent to first order. A per-rank floor
# would instead grow the total requirement linearly with tp: at TP8 it takes
# 128K scheduled rows, i.e. --max-num-batched-tokens >= 128K, which 80 GB
# Hopper nodes cannot fit (the KV-cache budget goes negative during memory
# profiling). The floor was calibrated on NVSwitch; on slower interconnects
# (e.g. PCIe) the all_gatherv costs more per row, so VLLM_INDEXER_TP_SHARD_MIN_ROWS
# can raise, lower, or effectively disable it.
MIN_TP_SHARD_TOTAL_ROWS = 65_536


def balanced_prefill_row_shard(
Expand All @@ -216,7 +227,8 @@ def balanced_prefill_row_shard(
) -> list[int] | None:
"""Return contiguous TP row counts balanced by indexer MQA work."""
num_rows = int(query_lens_cpu.sum())
if tp_size < 2 or num_rows < MIN_TP_SHARD_ROWS_PER_RANK * tp_size:
min_rows = envs.VLLM_INDEXER_TP_SHARD_MIN_ROWS or MIN_TP_SHARD_TOTAL_ROWS
if tp_size < 2 or num_rows < min_rows:
return None

query_lens = query_lens_cpu.to(torch.int64)
Expand Down
Loading