vllm.distributed.kv_transfer.kv_checksum.worker
¶
Worker-side KV checksum computation.
Each worker computes its part of a block's checksum: the slices (layer, KV
head) it holds, each weighed by its global layer and head (see
checksum.py). Parts from all TP ranks and PP stages add up to the block's
checksum. Drafter (speculative decoding) layers are not checksummed.
The results stay on the device until the model runner copies them to host
memory with the step's other outputs, on its output copy stream (see
PendingKVChecksums and vllm/v1/worker/gpu/async_utils.py).
Classes:
-
KVChecksumWorker–Computes the KV checksums the scheduler asks a worker for.
-
PendingKVChecksums–A worker's KV checksums of one step, until they reach host memory.
Functions:
-
create_kv_checksum_worker–This worker's KV checksum worker, or None if KV checksums are off.
KVChecksumWorker
¶
Computes the KV checksums the scheduler asks a worker for.
Requests to send are checksummed in the step that finishes their KV.
Requests to receive are checksummed in the step whose transfer results
report their load finished, before any later step can write their blocks.
A connector that offloads KV and loads it back can offload checksums with
it (init_block_checksums), which the loads are checked against.
Methods:
-
bind–Take the requests the scheduler set for checksumming this step.
-
init_block_checksums–Set up the per-block checksums a connector offloads with the KV of
-
post_forward–Queue the checksums of this step's sends and finished loads.
-
save_block_checksums–Write the checksums of full blocks a connector is about to offload
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 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 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 | |
_checksum(batches, plans)
¶
Checksum the rows of each group's batch, as plans say.
Returns:
-
list[tuple[int, _GroupBatch, Tensor, Tensor]]–Per group: its id, its batch, the batch's block ids on the device
-
list[tuple[int, _GroupBatch, Tensor, Tensor]]–and the
[rows, len(replica keys)]int64 checksums.
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
_compute(reqs, output, plans, stored=False)
¶
Checksum the blocks of reqs into output, as plans say,
with the checksums offloaded with them if stored.
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
bind(scheduled)
¶
Take the requests the scheduler set for checksumming this step.
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
init_block_checksums(group_ids)
¶
Set up the per-block checksums a connector offloads with the KV of
group_ids, so that loading the KV back can be checked.
The connector moves a block's entry wherever it moves the block's KV:
when offloading, after save_block_checksums wrote it (on the
current stream); when loading back, before it reports the load
finished.
Returns:
-
Tensor–A
(num_blocks,)int64 device tensor holding, per block, the -
Tensor–sum of this worker's partial checksums. Repeated calls return the
-
Tensor–same tensor.
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
post_forward(transfer_results, finished_req_ids)
¶
Queue the checksums of this step's sends and finished loads.
Parameters:
-
(transfer_results¶KVConnectorTransferResults) –The connector's transfer results of this step: the loads it finished (failed ones are not checked) and the producer checksums it received, which the first worker passes on to the scheduler.
-
(finished_req_ids¶set[str]) –Requests that ended, e.g. aborted while loading.
Returns:
-
PendingKVChecksums | None–The step's checksums, still on the device, or None if there is
-
PendingKVChecksums | None–nothing to report.
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
save_block_checksums(runs)
¶
Write the checksums of full blocks a connector is about to offload
into the init_block_checksums tensor, each XORed with its
block_checksum_tags.
Queued on the current stream: call it after the forward that wrote the blocks and before the connector reads them.
Parameters:
-
(runs¶Iterable[tuple[int, int, Sequence[int], Sequence[bytes]]]) –(group id, position of the first block, block ids at consecutive positions from there, their block hashes). A block's hash is the request's block hash at its last token.
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
PendingKVChecksums
¶
A worker's KV checksums of one step, until they reach host memory.
post_forward queues the checksum kernels on the main stream. The model
runner then calls enqueue_async_cpu_copy on its output copy stream, ordered
after them, and reads the record it returns once that copy is done.
Methods:
-
enqueue_async_cpu_copy–Enqueue the asynchronous D2H copy; call on the output copy stream,
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
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 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 | |
enqueue_async_cpu_copy()
¶
Enqueue the asynchronous D2H copy; call on the output copy stream, after it waits for the main stream.
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
_FusedLayers
¶
Layers of a group whose caches share one layout (shape, strides and dtype), each adding into its column of the group's checksums in one Triton launch.
Methods:
-
accumulate–Add each layer's weighted sum of each block row into its column of
out. -
warm_up–Compile the Triton kernel now rather than in a serving step.
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
accumulate(block_ids, first_tokens, valid_tokens, out)
¶
Add each layer's weighted sum of each block row into its column ofout.
Parameters:
-
(block_ids¶Tensor) –[n]logical block ids on the device. -
(first_tokens¶Tensor) –[n]position in the request of each block's first token. -
(valid_tokens¶Tensor) –[n]tokens to include from the start of each block. -
(out¶Tensor) –[n, num_columns]int64 accumulator, wrapping around (mod 2**64).
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
warm_up(num_columns)
¶
Compile the Triton kernel now rather than in a serving step.
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
_GroupBatch
dataclass
¶
The rows of one KV cache group to checksum in one pass.
Methods:
-
rows–The
[block_ids, first_tokens, valid_tokens]of the rows.
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
rows(block_size)
¶
The [block_ids, first_tokens, valid_tokens] of the rows.
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
_GroupPlan
dataclass
¶
How a worker checksums a group's layers: one launch per cache layout, adding into one column per replica key.
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
_LayerCache
dataclass
¶
One distinct KV cache of a group, or segment of a Mamba state, under the
lowest layer number using it. Rank r holds shard r // replicas:
the heads slices from first_head + shard * heads in the layer.
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
_LayerView
dataclass
¶
One layer's KV cache as integer pieces
[num_blocks, kernel_blocks, heads, tokens, pieces], read as unsigned.
A piece j of slice (layer, head) weighs m * (2 * j + 1), with
m = 2 * (layer * H + head) + 1 for the global head (see
checksum.py).
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
_UnsupportedGroupError
¶
_as_pieces(cache, num_blocks)
¶
A [blocks, kernel_blocks, heads, tokens, pieces] 8-byte piece view.
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
_attention_pieces(spec, cache, num_blocks, tp_size, uniform_kv_heads)
¶
An attention layer's 8-byte piece view, TP replicas and heads per rank.
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
_build_layer_views(group, kv_caches, num_blocks, tp_rank, tp_size, uniform_kv_heads)
¶
The views of a group's layers, each KV cache counted once.
Raises:
-
_UnsupportedGroupError–If the group cannot be checksummed.
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
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 442 443 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 | |
_draft_layer_names(vllm_config)
¶
The drafter's KV cache layers, marked when the drafter was loaded.
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
_global_state_shapes(spec, tp_size)
¶
Per split state: its global segment sizes and its other dimensions.
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
_group_fingerprint(group, tp_size)
¶
The sum of a hash of each of the group's layers on this worker.
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
_layer_module(layer_name)
¶
The model-wide layer number in a layer name, the same on every rank, and the name of its layer module (up to the number).
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
_mamba_pieces(spec, cache, num_blocks, tp_size)
¶
Like _attention_pieces, for a Mamba layer: each block's leading
_MAMBA_CHECKSUMMED_STATES states are one slice of one head and token.
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
_mamba_rows(spec, cache, num_blocks, number, tp_size)
¶
A Mamba layer's TP-split states, each global row (the TP-split
dimension, see MambaStateSplit) one slice of its elements, read as
unsigned 2- or 4-byte pieces. Slices are numbered across the layer's
states in their global order, so a rank holds those of its shard of each
segment whatever the TP size.
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
_tp_replicas(spec, tp_size, uniform_kv_heads)
¶
Consecutive TP ranks holding the same KV heads of a layer.
Specs that declare max_tp_shards say how they shard, and MLA-type
caches that do not are held whole by every rank, as NIXL transfers them.
Other attention layers shard their KV heads across TP and, once TP exceeds them,
replicate one head on consecutive ranks. More than one head per rank thus
means no replicas, while one head per rank is ambiguous and is resolved
with the model's KV head count when every layer shares it.
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
_uniform_kv_heads(vllm_config)
¶
The model's KV head count if every attention layer has it, else None.
Only needed to tell sharded from replicated heads at one head per rank.
Source code in vllm/distributed/kv_transfer/kv_checksum/worker.py
create_kv_checksum_worker(vllm_config, kv_cache_config, kv_caches)
¶
This worker's KV checksum worker, or None if KV checksums are off.