vllm.v1.attention.ops.dcp_shardq.context
¶
ShardQ prefill steps under DCP: attention over a gathered context.
DCP shards the KV cache by token, so a DCP prefill step runs every query token with all heads against each rank's 1/W of the keys, merges the indexer's per-rank top-k across ranks, and combines the attention partials by LSE: per layer it moves gigabytes and repeats the attention work W times over.
When the TP group is the DCP group, a prefill step can instead run as a TP ShardQ step (vllm.v1.attention.ops.shardq): each rank gathers its requests' whole context into a symmetric-memory workspace, in token order and in the paged formats of the MLA and indexer caches, then scores and attends its slice of the query tokens with all heads over the workspace, exactly as TP ShardQ does over a replicated cache. No top-k merge and no LSE combine remain.
Per layer, the indexer pushes every rank's owned rows of the step's requests into all workspaces (prior-context MLA and indexer rows, plus the indexer rows of the new tokens); the MLA layer then writes the new tokens' MLA rows, which every rank computes for all tokens, into its own workspace.
A step runs this way when ShardQ splits its tokens (build_shardq_split) and
its prefill rows fit the workspace. Its leading
decode rows (rows of at most 1 + num_speculative_tokens tokens, which the
indexer scores on its decode path) keep the DCP path within the step; its
other rows are sharded, including the short prefill pieces that the sparse
MLA backend classifies as decodes. The workspace holds the prefill rows'
requests only. DcpShardQPlanner decides, and counts the prefill steps and
tokens of each path.
Scope: any sparse MLA layer with a DSA-style fp8 indexer cache, under DCP with the DCP group equal to the TP group; nothing here is model-specific.
Classes:
-
ContextLayout–Per-token bytes of a DSA layer's MLA and indexer caches.
-
DcpShardQContext–One step's gathered-context plan (see the module docstring).
-
DcpShardQPlanner–Plans the DCP ShardQ steps of one metadata builder (see the module
-
DcpShardQStats–Steps with prefill rows, and their prefill tokens, by path: ShardQ, or
Functions:
-
build_dcp_shardq_context–The context plan of
split's step on DCP rankrankofworld, -
capacity_pages–Workspace pages for
max_tokenstokens, each request rounded up. -
dcp_fallback_max_tokens–Most tokens of a step that keeps the DCP path under DCP ShardQ, given
-
register_warmup–Register the JIT kernels that only DCP ShardQ steps launch with the
-
reserve_workspace–Allocate the workspace in the memory profile; symmetric memory lives
-
scheduler_context_pages–The workspace pages the scheduler keeps each step's prefill rows
-
warm_up_dcp_fallback–Run
dcp_manager's query gather and output combine of a step of -
workspace_max_tokens–Tokens of context the workspace holds:
max_model_len, so any one
ContextLayout
dataclass
¶
Per-token bytes of a DSA layer's MLA and indexer caches.
Methods:
-
from_layer–The layout of a DSA layer that DCP ShardQ steps can gather, or None.
Source code in vllm/v1/attention/ops/dcp_shardq/context.py
from_layer(spec, indexer)
classmethod
¶
The layout of a DSA layer that DCP ShardQ steps can gather, or None.
The MLA cache must keep one-byte elements (per-tensor fp8,
nvfp4_ds_mla) in kernel blocks of whole 64-token pages (a packed
cache may use e.g. 1024-token blocks), and the indexer fp8 keys
with one 4-byte scale per quant_block_size values (or MXFP4 keys:
64 B of values and 4 B of scales per token).
Source code in vllm/v1/attention/ops/dcp_shardq/context.py
DcpShardQContext
dataclass
¶
One step's gathered-context plan (see the module docstring).
The workspace holds pages MLA pages, then as many indexer pages.
Methods:
-
gather–Push this rank's rows of one layer into every rank's workspace (see
-
index_view–The workspace's indexer pages, typed as
index_cache. -
mla_view–The workspace's MLA pages, typed as
mla_cache. -
push–Copy this rank's rows of the prefill requests of one layer's
Source code in vllm/v1/attention/ops/dcp_shardq/context.py
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 | |
_view(dtype, offset, row_bytes, device)
¶
The workspace pages at offset as a [pages, 64, row] cache.
Source code in vllm/v1/attention/ops/dcp_shardq/context.py
gather(mla_cache, mla_block_table, index_cache, index_block_table)
¶
Push this rank's rows of one layer into every rank's workspace (see
push) and return the workspace as the indexer's cache view, once
every rank's rows have landed in it.
Source code in vllm/v1/attention/ops/dcp_shardq/context.py
index_view(index_cache)
¶
The workspace's indexer pages, typed as index_cache.
Source code in vllm/v1/attention/ops/dcp_shardq/context.py
mla_view(mla_cache)
¶
The workspace's MLA pages, typed as mla_cache.
push(mla_cache, mla_block_table, index_cache, index_block_table, dst_addrs)
¶
Copy this rank's rows of the prefill requests of one layer's
DCP-local paged caches (with the step's kernel block tables) into the
workspaces at dst_addrs.
Source code in vllm/v1/attention/ops/dcp_shardq/context.py
DcpShardQPlanner
¶
Plans the DCP ShardQ steps of one metadata builder (see the module docstring); every rank reaches the same decisions from the same batch.
Methods:
-
plan–The context plan of a step that runs as a DCP ShardQ step with the
Source code in vllm/v1/attention/ops/dcp_shardq/context.py
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 | |
plan(common_attn_metadata, split)
¶
The context plan of a step that runs as a DCP ShardQ step with the
token split split (vllm.v1.attention.ops.shardq.build_shardq_split),
or None for the DCP path. A step without prefill rows is not counted.
The split's decode tokens (requests of at most max_decode_query_len
tokens, which the indexer scores on its decode path) keep the DCP path;
the plan covers the rest.
Source code in vllm/v1/attention/ops/dcp_shardq/context.py
DcpShardQStats
¶
Steps with prefill rows, and their prefill tokens, by path: ShardQ, or the DCP path and why. Rank 0 logs them at most every VLLM_LOG_STATS_INTERVAL seconds, for the steps since its previous report and in total, labeled with the layers of the metadata builder that plans them. Every step the builder plans counts, the runner's profile and warmup steps included.
Source code in vllm/v1/attention/ops/dcp_shardq/context.py
_GatheredContextIndexRemap
¶
Bases: ConvertReqIndexToGlobalIndexKernel
The sparse-MLA top-k remap as a DCP ShardQ step runs it (SparseMLACommonImpl.forward_mqa over kv_gathered metadata): onto the workspace's 64-token pages, without DCP, with valid counts, through a block table padded to TABLE_PAGES_ALIGN pages. Its Triton kernel is the one every ConvertReqIndexToGlobalIndexKernel launches, so warming these keys here warms the runtime launches.
Source code in vllm/v1/attention/ops/dcp_shardq/context.py
_workspace(layout, pages, device)
¶
The symmetric-memory workspace, grown to pages pages if needed.
Sizes come from the configuration, so every rank rendezvouses together.
Source code in vllm/v1/attention/ops/dcp_shardq/context.py
build_dcp_shardq_context(common_attn_metadata, split, layout, max_pages, max_indexer_keys, max_logits_bytes, world, rank, device)
¶
The context plan of split's step on DCP rank rank of world,
or None when its prefill rows (the requests after split's decode
tokens) do not fit the max_pages workspace pages.
Every rank derives the same plan; the push kernel finds each rank's rows from the step's device sequence lengths.
Source code in vllm/v1/attention/ops/dcp_shardq/context.py
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 | |
capacity_pages(max_tokens, max_reqs)
¶
Workspace pages for max_tokens tokens, each request rounded up.
dcp_fallback_max_tokens(vllm_config, decode_query_len)
¶
Most tokens of a step that keeps the DCP path under DCP ShardQ, given
the scheduler's context cap: its leading decode rows plus fewer than
sparse_mla_shard_q_min_tokens prefill tokens, or a padded step of at
most the DCP workspace's tokens.
Source code in vllm/v1/attention/ops/dcp_shardq/context.py
register_warmup(layout, world, num_topk_tokens)
¶
Register the JIT kernels that only DCP ShardQ steps launch with the
active warmup registry: the context push and the top-k remap onto the
workspace. (The prefill chunk metadata kernel warms its non-DCP form when
sparse_mla_shard_q is set under DCP; ShardQ's exchange kernel warms
with every ShardQ layer.)
Source code in vllm/v1/attention/ops/dcp_shardq/context.py
reserve_workspace(layout, max_tokens, max_reqs, device)
¶
Allocate the workspace in the memory profile; symmetric memory lives outside the torch caching allocator, so it counts as non-torch memory.
Source code in vllm/v1/attention/ops/dcp_shardq/context.py
scheduler_context_pages(vllm_config)
¶
The workspace pages the scheduler keeps each step's prefill rows
within (the sum over them of ceil(seq_len / 64)), or None when DCP
ShardQ steps cannot run under this configuration.
With the cap, no step finds the workspace full, so the DCP path only runs
a step's leading decode rows and steps of fewer than
sparse_mla_shard_q_min_tokens prefill tokens, and the memory profile
reserves those (dcp_fallback_max_tokens) instead of a whole
max_num_batched_tokens step. These are the configuration-level and
platform conditions of SparseMLAShardQ's DCP ShardQ; when its layer-level
ones fail the cap merely keeps a step's prefill context within
max_model_len.
Source code in vllm/v1/attention/ops/dcp_shardq/context.py
warm_up_dcp_fallback(dcp_manager, num_tokens)
¶
Run dcp_manager's query gather and output combine of a step of
num_tokens tokens, and the combine's Triton kernels for every batch
size, on dummy inputs (once per process and shape).
A DCP ShardQ instance takes most prefill steps off the DCP path, so the steps that keep it (decode rows, few tokens, a full workspace) may come only late, after the KV cache and the caching allocator took the memory. Beyond the direct workspaces the DCP path falls back to NCCL collectives, which connect their peers and allocate their buffers on first use, and its Triton kernels compile on first use; calling this in the memory profile run makes both happen there and accounts for them. Every rank of the group must call it.
Source code in vllm/v1/attention/ops/dcp_shardq/context.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | |
workspace_max_tokens(vllm_config)
¶
Tokens of context the workspace holds: max_model_len, so any one
request fits. It costs about mla_row_bytes + index_row_bytes (plus
the index scales) per token on each rank, ~0.7 GB per 1M tokens with a
per-tensor fp8 cache and ~0.5 GB with nvfp4_ds_mla. Call it
through this module so tests can patch it.