Skip to content

vllm.v1.hisparse.coordinator

Classes:

  • HiSparseCoordinator –

    Own HiSparse host allocation, host publication, spills, and GPU residency.

Functions:

HiSparseCoordinator

Own HiSparse host allocation, host publication, spills, and GPU residency.

GPU-resident pages are a write-back cache of the host tier. Once a page's host copy is durable and its owner can read from host (it has, or has asked for, a hot region), the page's allocation reference is released with BlockPool.unpin_blocks: the block keeps being read, but the pool counts it as free and may hand it out, at which point the owner stops reading it. Residency changes reach the worker through take_residency_updates, not the block table, which stays append-only.

Methods:

Attributes:

Source code in vllm/v1/hisparse/coordinator.py
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 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
 326
 327
 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
 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
 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
 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
 588
 589
 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
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
class HiSparseCoordinator:
    """Own HiSparse host allocation, host publication, spills, and GPU residency.

    GPU-resident pages are a write-back cache of the host tier. Once a page's
    host copy is durable and its owner can read from host (it has, or has
    asked for, a hot region), the page's allocation reference is released with
    `BlockPool.unpin_blocks`: the block keeps being read, but the pool counts
    it as free and may hand it out, at which point the owner stops reading it.
    Residency changes reach the worker through `take_residency_updates`, not
    the block table, which stays append-only.
    """

    def __init__(
        self,
        kv_cache_config: KVCacheConfig,
        managers: tuple[SingleTypeKVCacheManager, ...],
        max_model_len: int,
        *,
        num_reprefillable_tokens: int,
        import_step_tokens: int = 0,
    ) -> None:
        # Spec decode step tokens an import needs beyond its sequence.
        self.import_step_tokens = import_step_tokens
        self.num_reprefillable_tokens = num_reprefillable_tokens
        groups = kv_cache_config.kv_cache_groups

        resident_managers: list[HiSparseResidentManager] = []
        hot_managers: list[HiSparseHotManager] = []
        resident_group_ids: list[int] = []
        for group_id, (group, manager) in enumerate(zip(groups, managers)):
            if isinstance(manager, HiSparseResidentManager):
                resident_managers.append(manager)
                resident_group_ids.append(group_id)
                assert not group.host_resident
            if isinstance(manager, HiSparseHotManager):
                hot_managers.append(manager)
                assert not group.host_resident
            if isinstance(manager, (HiSparseHotManager, HiSparseResidentManager)):
                manager.coordinator = self
        self.resident_managers = tuple(resident_managers)
        self.hot_managers = tuple(hot_managers)
        self.host_manager: HiSparseSourceManager | None = None
        self.host_group_id: int | None = None
        self.max_spill_pages = 0
        for group_id, manager in enumerate(managers):
            if isinstance(manager, HiSparseSourceManager):
                if self.host_manager is not None:
                    raise ValueError("Only one HiSparse host group is supported.")
                num_host_blocks = kv_cache_config.hisparse_host_num_blocks
                if num_host_blocks is None:
                    raise ValueError("HiSparse host group needs host capacity.")
                self.host_group_id = group_id
                self.host_manager = manager
                manager.coordinator = self
                manager.bind_host_pool(num_host_blocks)
        self.transition_watermark = 0
        if self.resident_managers:
            assert self.host_manager is not None
            assert self.host_group_id is not None
            if self.host_group_id > min(resident_group_ids):
                raise ValueError("HiSparse host group must precede resident groups.")
            resident_block_sizes = {
                manager.block_size for manager in self.resident_managers
            }
            if len(resident_block_sizes) != 1:
                raise ValueError(
                    "HiSparse resident cache groups must use one block size."
                )
            resident_block_size = resident_block_sizes.pop()
            if self.host_manager.block_size != resident_block_size:
                raise ValueError("HiSparse host and resident block sizes must match.")
            self.max_spill_pages = max_model_len // resident_block_size
            # Requests start reading from host once the shared pool runs this
            # low, so admissions find evictable pages rather than pinned ones.
            hot_cost = sum(manager.blocks_per_request for manager in hot_managers)
            self.transition_watermark = max(hot_cost, kv_cache_config.num_blocks // 10)

        # Host copies handed to the worker, by destination block id, until it
        # reports having run them.
        self._retained_copies: dict[int, tuple[KVCacheBlock, KVCacheBlock]] = {}
        # request -> prefix length an external load is still filling in.
        self._pending_imports: dict[str, int] = {}
        # request -> full-sequence host pages of an admitted import. A waiting
        # import holds host pages and is never preempted, so the pages it has
        # yet to allocate stay reserved against later admissions.
        self._import_pages: dict[str, int] = {}
        self._import_reservations: dict[str, int] = {}
        self._num_reserved_import_pages = 0
        # The import the admission gate last sized, kept for its allocation.
        self._sized_import: tuple[str, int] | None = None
        # Requests whose resident pages were all non-null when last scanned.
        # Pages only become null at admission or import (before the first
        # scan) and when the pool reuses an unpinned page (_lose_page).
        self._resident_requests: set[str] = set()
        self.spills_to_send: list[SparseKVPageTransfer] = []
        self.pending_spills: dict[int, _PendingSpill] = {}
        self.request_states: dict[str, _HiSparseRequestState] = {}
        self.next_spill_id = 0
        # Published host block hash -> GPU copies of that page, readable until
        # the pool reuses them. Lets a host prefix hit come back GPU-resident.
        self.copies: dict[BlockHashWithGroupId, tuple[KVCacheBlock, ...]] = {}
        self._copy_by_block: dict[int, BlockHashWithGroupId] = {}
        # Unpinned GPU block id -> (request, page) pairs still reading it.
        self._owners: dict[int, set[tuple[str, int]]] = {}

    @property
    def gpu_pool(self) -> BlockPool:
        return self.resident_managers[0].block_pool

    def _num_resident_pages(self, request_id: str) -> int:
        return min(
            len(manager.req_to_blocks.get(request_id, ()))
            for manager in self.resident_managers
        )

    def _get_request_state(self, request_id: str) -> _HiSparseRequestState:
        state = self.request_states.get(request_id)
        if state is None:
            state = _HiSparseRequestState()
            self.request_states[request_id] = state
        return state

    def commit_computed_blocks(self, request_id: str, num_host_pages: int) -> None:
        """Account for a prefix hit on host pages the request now references."""
        self._resident_requests.discard(request_id)
        if not self.resident_managers or self.host_manager is None:
            return
        host_blocks = self.host_manager.req_to_blocks.get(request_id, ())
        num_host_pages = min(num_host_pages, len(host_blocks))
        if num_host_pages == 0:
            return
        state = self._get_request_state(request_id)
        state.valid_pages.update(range(num_host_pages))
        state.ready_prefix_pages = max(num_host_pages, state.ready_prefix_pages)
        # Adopting pins copies out of the free pool, so it must wait until the
        # allocation admission counted on has run (see update_residency).
        state.pages_to_adopt = max(state.pages_to_adopt, num_host_pages)
        state.copies_recorded_blocks = max(state.copies_recorded_blocks, num_host_pages)

    def take_host_block_copies(self) -> tuple[KVCacheBlockCopy, ...]:
        """Drain host copy-on-write work, retaining both endpoints.

        The worker runs the copies in the step that carries them, so the
        endpoints are released when that step's output comes back.
        """
        if self.host_manager is None:
            return ()
        pairs = self.host_manager.take_host_cow_copies()
        for source_block, cow_block in pairs:
            self._retained_copies[cow_block.block_id] = (source_block, cow_block)
        return tuple(
            KVCacheBlockCopy(
                src_block_id=source_block.block_id,
                dst_block_id=cow_block.block_id,
            )
            for source_block, cow_block in pairs
        )

    def release_completed_host_copies(self, dst_block_ids: Iterable[int]) -> None:
        """Release the endpoints of host copies the worker reports having run."""
        blocks: list[KVCacheBlock] = []
        for dst_block_id in dst_block_ids:
            pair = self._retained_copies.pop(dst_block_id, None)
            if pair is not None:
                blocks.extend(pair)
        if not blocks:
            return
        assert self.host_manager is not None
        self.host_manager.block_pool.free_blocks(reversed(blocks))

    def get_host_block_pool(self) -> BlockPool | None:
        manager = self.host_manager
        return manager.block_pool if manager is not None else None

    # ------------------------------------------------------------------
    # GPU copies of published host pages
    # ------------------------------------------------------------------

    def _adopt_copies(
        self,
        request_id: str,
        state: _HiSparseRequestState,
        host_blocks: Sequence[KVCacheBlock],
    ) -> None:
        """Point null prefix pages at readable GPU copies of their contents.

        Runs after the hit length is fully reconciled, so copy hits and misses
        can be scattered per page without affecting the prefix hit.
        """
        if not self.copies:
            return
        copies: dict[int, tuple[KVCacheBlock, ...]] = {}
        for host_idx, host_block in enumerate(host_blocks):
            if host_block.is_null or host_block.block_hash is None:
                continue
            blocks = self.copies.get(host_block.block_hash)
            if blocks is not None:
                copies[host_idx] = blocks
        if not copies:
            return
        host_indices = list(copies)
        adopted: list[KVCacheBlock] = []
        for manager, column in zip(self.resident_managers, zip(*copies.values())):
            for host_idx, block in manager.adopt_resident_pages(
                request_id, host_indices, column
            ):
                adopted.append(block)
                state.pinned_clean.add(host_idx)
                if host_idx < state.synced_pages:
                    state.stale_pages.add(host_idx)
        self.gpu_pool.touch(adopted)

    def _adopt_pending_copies(
        self, request_id: str, state: _HiSparseRequestState
    ) -> None:
        if state.pages_to_adopt:
            assert self.host_manager is not None
            host_blocks = self.host_manager.req_to_blocks.get(request_id, ())
            self._adopt_copies(request_id, state, host_blocks[: state.pages_to_adopt])
            state.pages_to_adopt = 0

    def _record_copies(self, request_id: str, num_computed_tokens: int) -> None:
        """Index the GPU copies of just-published host blocks for later hits."""
        if not self.resident_managers:
            return
        assert self.host_manager is not None
        host_blocks = self.host_manager.req_to_blocks.get(request_id)
        if not host_blocks:
            return
        state = self._get_request_state(request_id)
        num_host_blocks = min(
            num_computed_tokens // self.host_manager.block_size, len(host_blocks)
        )
        if state.copies_recorded_blocks >= num_host_blocks:
            return
        num_pages = min(num_host_blocks, self._num_resident_pages(request_id))
        pages = [
            host_idx
            for host_idx in range(state.copies_recorded_blocks, num_pages)
            if host_idx not in state.pending_pages
        ]
        columns = [
            manager.get_residency_pages(request_id, pages)
            for manager in self.resident_managers
        ]
        for host_idx, blocks in zip(pages, zip(*columns)):
            self._store_copy(host_blocks[host_idx], blocks)
        state.copies_recorded_blocks = max(
            state.copies_recorded_blocks, num_host_blocks
        )

    def _record_copy(
        self, request_id: str, page_idx: int, host_block: KVCacheBlock
    ) -> None:
        if page_idx >= self._num_resident_pages(request_id):
            return
        blocks = tuple(
            manager.get_residency_pages(request_id, (page_idx,))[0]
            for manager in self.resident_managers
        )
        self._store_copy(host_block, blocks)

    def _store_copy(
        self, host_block: KVCacheBlock, blocks: tuple[KVCacheBlock, ...]
    ) -> None:
        if (
            host_block.is_null
            or host_block.block_hash is None
            or any(block.is_null for block in blocks)
        ):
            return
        self._drop_copy(host_block.block_hash)
        self.copies[host_block.block_hash] = blocks
        for block in blocks:
            self._copy_by_block[block.block_id] = host_block.block_hash

    def _drop_copy(self, host_hash: BlockHashWithGroupId) -> None:
        blocks = self.copies.pop(host_hash, None)
        if blocks is None:
            return
        for block in blocks:
            self._copy_by_block.pop(block.block_id, None)

    # ------------------------------------------------------------------
    # Residency: pinning, unpinning and losing pages
    # ------------------------------------------------------------------

    def _can_read_from_host(self, request_id: str) -> bool:
        """Whether resident pages may be dropped under the request."""
        return bool(self.hot_managers) and all(
            manager.has_hot(request_id) for manager in self.hot_managers
        )

    def _fills_admission_window(self, request_id: str) -> bool:
        """Whether the request holds the resident pages admission reserved.

        Admission caps a request's resident pages at the in-flight window,
        assuming older pages move to host; keeping them pinned past it lets a
        chunked prefill outgrow the pool it was admitted into.
        """
        return any(
            len(manager.req_to_blocks.get(request_id, ()))
            >= manager.max_admission_blocks_per_request
            for manager in self.resident_managers
        )

    def _unpin_pages(
        self,
        request_id: str,
        state: _HiSparseRequestState,
        page_indices: Iterable[int],
    ) -> None:
        """Release the pages' resident blocks to the pool, in the given order."""
        num_unpinnable = self._num_resident_pages(request_id) - ACTIVE_TAIL_PAGES
        pages = [
            page_idx
            for page_idx in page_indices
            if page_idx < num_unpinnable
            and page_idx not in state.pending_pages
            and page_idx in state.valid_pages
        ]
        if not pages:
            return
        columns = [
            manager.get_residency_pages(request_id, pages)
            for manager in self.resident_managers
        ]
        released: list[KVCacheBlock] = []
        for page_idx, blocks in zip(pages, zip(*columns)):
            if any(block.is_null for block in blocks):
                continue
            for block in blocks:
                self._owners.setdefault(block.block_id, set()).add(
                    (request_id, page_idx)
                )
            released.extend(blocks)
            state.pinned_clean.discard(page_idx)
            state.unpinned_pages.add(page_idx)
        if released:
            self.gpu_pool.unpin_blocks(released, self._on_block_reused)

    def _unpin_clean_pages(self, request_id: str, state: _HiSparseRequestState) -> None:
        # Tail first, so a prefix loses its tail pages before its head.
        if not state.pinned_clean:
            return
        self._unpin_pages(request_id, state, sorted(state.pinned_clean, reverse=True))

    def _on_block_reused(self, block: KVCacheBlock) -> None:
        host_hash = self._copy_by_block.get(block.block_id)
        if host_hash is not None:
            self._drop_copy(host_hash)
        for request_id, page_idx in self._owners.pop(block.block_id, ()):
            self._lose_page(request_id, page_idx)

    def _lose_page(self, request_id: str, page_idx: int) -> None:
        self._resident_requests.discard(request_id)
        state = self.request_states.get(request_id)
        if state is None:
            return
        for manager in self.resident_managers:
            block = manager.drop_resident_page(request_id, page_idx)
            if block is None:
                continue
            owners = self._owners.get(block.block_id)
            if owners is not None:
                owners.discard((request_id, page_idx))
        state.unpinned_pages.discard(page_idx)
        if page_idx < state.synced_pages:
            state.stale_pages.add(page_idx)
        for hot_manager in self.hot_managers:
            hot_manager.require_hot(request_id)

    def update_residency(self, request_id: str) -> None:
        """Per-step residency policy for a scheduled request.

        A request that can read from host releases every clean sealed page to
        the pool. One that cannot keeps its pages pinned until the shared pool
        runs low, or until it holds the resident pages admission reserved for
        it, then asks for a hot region. Pages remain pinned until that region is
        allocated on a subsequent scheduling pass.
        """
        if not self.resident_managers:
            return
        state = self._get_request_state(request_id)
        self._adopt_pending_copies(request_id, state)
        if not self._can_read_from_host(request_id):
            if (
                self.gpu_pool.get_num_free_blocks() >= self.transition_watermark
                and not self._fills_admission_window(request_id)
            ):
                return
            for manager in self.hot_managers:
                manager.require_hot(request_id)
            return
        self._unpin_clean_pages(request_id, state)

    # ------------------------------------------------------------------
    # Host publication and spills
    # ------------------------------------------------------------------

    def advance_scheduled(self, requests: Iterable[tuple[str, int]]) -> None:
        """Run the per-step residency work for each scheduled request.

        `requests` pairs a request id with the number of tokens computed
        once the step completes. As in `KVCacheCoordinator.cache_blocks`, the
        last `num_reprefillable_tokens` are not final yet and are not written
        back.
        """
        if not self.resident_managers:
            return
        for request_id, num_tokens in requests:
            self.plan_prefix_materialization(
                request_id, max(0, num_tokens - self.num_reprefillable_tokens)
            )
            self.update_residency(request_id)

    def plan_prefix_materialization(
        self, request_id: str, num_computed_tokens: int
    ) -> None:
        """Eagerly write back every sealed page that has no host copy yet."""
        if not self.resident_managers:
            return
        assert self.host_manager is not None
        host_block_size = self.host_manager.block_size
        num_pages = num_computed_tokens // host_block_size
        importing_pages = cdiv(
            self._pending_imports.get(request_id, 0), host_block_size
        )
        state = self._get_request_state(request_id)
        budget = max(self.max_spill_pages - len(self.spills_to_send), 0)
        first_page = max(state.ready_prefix_pages, importing_pages)
        if budget > 0 and first_page < num_pages:
            self._plan_page_transfers(
                request_id,
                (
                    page_idx
                    for page_idx in range(first_page, num_pages)
                    if page_idx not in state.valid_pages
                ),
                budget,
            )

    def publish_when_ready(
        self,
        request: Request,
        num_computed_tokens: int,
        retention_interval: int | None,
        *,
        replay_boundaries: Sequence[int],
    ) -> None:
        """Publish host-source hashes of the pages that are already durable."""
        manager = self.host_manager
        if manager is None:
            return
        # A lookahead hash arrives only once the tokens after its page are known.
        num_computed_tokens = min(
            num_computed_tokens,
            len(request.block_hashes) * manager.block_pool.hash_block_size,
        )
        request_id = request.request_id
        self._get_request_state(request_id).publication = _PendingPublication(
            request=request,
            num_computed_tokens=num_computed_tokens,
            num_pages=num_computed_tokens // manager.block_size,
            retention_interval=retention_interval,
            replay_boundaries=replay_boundaries,
        )
        self._publish_host_blocks_if_ready(request_id)

    def _publish_host_blocks_if_ready(self, request_id: str) -> None:
        """Publish the durable prefix of the pending publication.

        Host writes trail a long prefill by about a chunk, so waiting for the
        whole computed prefix would publish nothing until the prefill ends, and
        a prefill preempted before then would recompute from the start.
        """
        state = self.request_states.get(request_id)
        if state is None:
            return
        publication = state.publication
        if publication is None:
            return
        assert self.host_manager is not None
        num_tokens = min(
            publication.num_computed_tokens,
            state.ready_prefix_pages * self.host_manager.block_size,
        )
        self.host_manager.publish_blocks(
            publication.request,
            num_tokens,
            retention_interval=publication.retention_interval,
            replay_boundaries=publication.replay_boundaries,
        )
        self._record_copies(request_id, num_tokens)
        if state.ready_prefix_pages >= publication.num_pages:
            state.publication = None

    def record_pending_host_import(self, request_id: str, num_tokens: int) -> None:
        """Note a prefix an external load is populating in host pages."""
        self._resident_requests.discard(request_id)
        self._pending_imports[request_id] = num_tokens
        if self._sized_import is not None and self._sized_import[0] == request_id:
            self._import_pages[request_id] = self._sized_import[1]
        self._sized_import = None

    def size_import(self, request_id: str, num_pages: int) -> None:
        """Note the full-sequence host pages of an import being admitted.

        The admission gate runs in the same `allocate_slots` call as the
        allocation, so the next `record_pending_host_import` is this
        request's if it is admitted; a size left by a rejected admission is
        discarded.
        """
        self._sized_import = (request_id, num_pages)

    @property
    def reserved_import_pages(self) -> int:
        """Host pages admitted imports still need for their sequence."""
        return self._num_reserved_import_pages

    def update_import_reservation(self, request_id: str, num_held_pages: int) -> None:
        pages = self._import_pages.get(request_id)
        if pages is None:
            return
        self._set_import_reservation(request_id, max(pages - num_held_pages, 0))
        if num_held_pages >= pages:
            del self._import_pages[request_id]

    def _set_import_reservation(self, request_id: str, num_pages: int) -> None:
        self._num_reserved_import_pages += num_pages - self._import_reservations.pop(
            request_id, 0
        )
        if num_pages > 0:
            self._import_reservations[request_id] = num_pages

    def finish_host_import(self, request_id: str, *, failed: bool) -> None:
        num_tokens = self._pending_imports.pop(request_id, None)
        if num_tokens is not None and not failed:
            self.complete_host_import(request_id, num_tokens)

    def complete_host_import(self, request_id: str, num_computed_tokens: int) -> None:
        """Publish externally populated host pages after connector completion."""
        if not self.resident_managers:
            return
        block_size = self.resident_managers[0].block_size
        num_pages = num_computed_tokens // block_size
        state = self._get_request_state(request_id)
        state.valid_pages = set(range(num_pages))
        state.ready_prefix_pages = num_pages
        if num_computed_tokens:
            # A local prefix hit's surviving GPU copies cost no copy, so adopt
            # them before promoting the rest.
            self._adopt_pending_copies(request_id, state)
            tail_page = (num_computed_tokens - 1) // block_size
            pages = (tail_page, *self._promote_imported_pages(request_id, tail_page))
            self._plan_page_transfers(
                request_id, pages, budget=len(pages), restore=True
            )
        self._publish_host_blocks_if_ready(request_id)

    def _promote_imported_pages(self, request_id: str, num_pages: int) -> list[int]:
        """Back an import's first `num_pages` host pages with new GPU pages.

        Imports land in the host tier. While the GPU pool has room above the
        transition watermark, the imported pages are restored into resident
        pages too, so decode reads them like a locally computed prefix. They are
        clean copies of durable host pages: the residency policy unpins them,
        and the pool reclaims them only after every other free block.
        """
        assert self.host_manager is not None
        num_managers = len(self.resident_managers)
        spare = self.gpu_pool.get_num_free_blocks() - self.transition_watermark
        host_blocks = self.host_manager.req_to_blocks[request_id]
        num_pages = min(
            num_pages, len(host_blocks), self._num_resident_pages(request_id)
        )
        state = self._get_request_state(request_id)
        candidates = [
            page_idx
            for page_idx in range(num_pages)
            if not host_blocks[page_idx].is_null and page_idx not in state.pending_pages
        ]
        columns = [
            manager.get_residency_pages(request_id, candidates)
            for manager in self.resident_managers
        ]
        pages = [
            page_idx
            for page_idx, blocks in zip(candidates, zip(*columns))
            if all(block.is_null for block in blocks)
        ][: max(spare, 0) // num_managers]
        if not pages:
            return []
        blocks = self.gpu_pool.get_new_blocks(len(pages) * num_managers)
        for slot, manager in enumerate(self.resident_managers):
            adopted = manager.adopt_resident_pages(
                request_id, pages, blocks[slot::num_managers]
            )
            assert len(adopted) == len(pages), f"Cannot promote pages of {request_id}"
        state.stale_pages.update(
            page_idx for page_idx in pages if page_idx < state.synced_pages
        )
        return pages

    def _plan_page_transfers(
        self,
        request_id: str,
        page_indices: Iterable[int],
        budget: int,
        *,
        restore: bool = False,
    ) -> None:
        """Plan up to `budget` transfers for the pages, in the given order."""
        assert self.host_manager is not None
        host_blocks = self.host_manager.req_to_blocks.get(request_id)
        if host_blocks is None:
            return
        state = self._get_request_state(request_id)
        num_pages = min(len(host_blocks), self._num_resident_pages(request_id))
        candidates = (
            page_idx
            for page_idx in page_indices
            if page_idx < num_pages
            and page_idx not in state.pending_pages
            and not host_blocks[page_idx].is_null
        )
        pinned_host_blocks: list[KVCacheBlock] = []
        pinned_blocks: list[KVCacheBlock] = []
        while budget > 0 and (pages := list(itertools.islice(candidates, budget))):
            columns = [
                manager.get_residency_pages(request_id, pages)
                for manager in self.resident_managers
            ]
            for page_idx, blocks in zip(pages, zip(*columns)):
                if any(block.is_null for block in blocks):
                    continue
                host_block = host_blocks[page_idx]
                budget -= 1
                pinned_host_blocks.append(host_block)
                pinned_blocks.extend(blocks)
                spill_id = self.next_spill_id
                self.next_spill_id += 1
                plan = SparseKVPageTransfer(
                    transfer_id=spill_id,
                    host_block_id=host_block.block_id,
                    resident_block_ids=tuple(block.block_id for block in blocks),
                    restore=restore,
                )
                self.pending_spills[spill_id] = _PendingSpill(
                    transfer_id=spill_id,
                    page=(request_id, page_idx),
                    request_state=state,
                    host_block=host_block,
                    resident_blocks=blocks,
                    restore=restore,
                )
                state.pending_pages[page_idx] = spill_id
                self.spills_to_send.append(plan)
        if pinned_blocks:
            self.host_manager.block_pool.touch(pinned_host_blocks)
            self.gpu_pool.touch(pinned_blocks)

    # ------------------------------------------------------------------
    # Worker-facing views
    # ------------------------------------------------------------------

    def build_row_mirrors(
        self,
        requests: Iterable[tuple[str, int, int]],
    ) -> tuple[SparseKVRowMirror, ...]:
        """Map scheduled rows to scheduler-owned resident and host blocks."""
        if not self.resident_managers or self.host_manager is None:
            return ()
        block_size = self.resident_managers[0].block_size
        mirrors: list[SparseKVRowMirror] = []
        for request_id, num_computed_tokens, num_scheduled_tokens in requests:
            host_blocks = self.host_manager.req_to_blocks.get(request_id)
            if host_blocks is None or any(
                request_id not in manager.req_to_blocks
                for manager in self.resident_managers
            ):
                continue
            token_position = num_computed_tokens
            end_position = token_position + num_scheduled_tokens
            while token_position < end_position:
                page_idx, row_offset = divmod(token_position, block_size)
                num_rows = min(block_size - row_offset, end_position - token_position)
                # A page without a GPU copy has no mirror source, and a page
                # without a host block has no destination. Skip just that page:
                # its neighbours in the window are still mirrorable, and a
                # window only ever moves forward, so dropping them here would
                # leave their host rows permanently stale.
                source_starts = []
                for manager in self.resident_managers:
                    block = manager.get_resident_page(request_id, page_idx)
                    if block is None:
                        break
                    source_starts.append(block.block_id * block_size + row_offset)
                if len(source_starts) != len(self.resident_managers):
                    token_position += num_rows
                    continue
                host_block_idx = page_idx
                if host_block_idx >= len(host_blocks):
                    token_position += num_rows
                    continue
                host_block = host_blocks[host_block_idx]
                if host_block.is_null:
                    token_position += num_rows
                    continue
                mirrors.append(
                    SparseKVRowMirror(
                        source_starts=tuple(source_starts),
                        destination_start=(
                            host_block.block_id * block_size + row_offset
                        ),
                        num_rows=num_rows,
                    )
                )
                token_position += num_rows
        return tuple(mirrors)

    def all_context_pages_resident(
        self,
        requests: Iterable[tuple[str, int, int]],
    ) -> bool:
        """Return whether every scheduled request can read only resident KV."""
        if not self.resident_managers:
            return False
        first = self.resident_managers[0]
        for request_id, num_computed_tokens, num_scheduled_tokens in requests:
            num_pages = cdiv(
                num_computed_tokens + num_scheduled_tokens, first.block_size
            )
            if len(first.req_to_blocks.get(request_id, ())) < num_pages:
                return False
            if request_id in self._resident_requests:
                continue
            if any(
                block.is_null
                for manager in self.resident_managers
                for block in manager.get_residency_row(request_id)
            ):
                return False
            self._resident_requests.add(request_id)
        return True

    def take_residency_updates(
        self, request_ids: Iterable[str]
    ) -> dict[str, SparseKVResidencyUpdate]:
        """Resident block ids the worker lacks for the scheduled requests:
        stale pages plus pages appended since the last update.

        Requests that are not scheduled keep their changes until they are:
        only a scheduled request's residency is read.
        """
        updates: dict[str, SparseKVResidencyUpdate] = {}
        if not self.resident_managers:
            return updates
        first = self.resident_managers[0]
        for request_id in request_ids:
            state = self._get_request_state(request_id)
            num_pages = len(first.req_to_blocks.get(request_id, ()))
            start = state.synced_pages
            if not state.stale_pages and start >= num_pages:
                continue
            stale_pages = sorted(state.stale_pages)
            block_ids: list[list[int]] = []
            for manager in self.resident_managers:
                null_block_id = manager.block_pool.null_block.block_id
                group_block_ids = [
                    null_block_id if block is None else block.block_id
                    for block in (
                        manager.get_resident_page(request_id, page_idx)
                        for page_idx in stale_pages
                    )
                ]
                group_block_ids.extend(
                    block.block_id
                    for block in manager.get_residency_row(request_id, start)
                )
                block_ids.append(group_block_ids)
            updates[request_id] = SparseKVResidencyUpdate(
                pages=[*stale_pages, *range(start, num_pages)],
                block_ids=tuple(block_ids),
            )
            state.stale_pages.clear()
            state.synced_pages = num_pages
        return updates

    def build_offload_command(self) -> SparseKVOffloadCommand | None:
        """Package residency decisions for the worker connector."""
        if not self.resident_managers:
            return None
        command = SparseKVOffloadCommand(page_transfers=self.spills_to_send)
        self.spills_to_send = []
        return command

    def has_pending_reclamation(self) -> bool:
        """Whether an in-flight spill will free GPU or host blocks on completion."""
        return any(
            pending.host_block.ref_cnt == 1
            or (
                pending.page[0] in self.request_states
                and self._can_read_from_host(pending.page[0])
            )
            for pending in self.pending_spills.values()
        )

    def has_pending_work(self) -> bool:
        return bool(self.spills_to_send or self.pending_spills or self._retained_copies)

    def update_spills(
        self,
        enqueued_counts: Mapping[int, int],
        completed_counts: Mapping[int, int],
    ) -> None:
        for spill_id, count in enqueued_counts.items():
            pending = self.pending_spills.get(spill_id)
            if pending is not None and pending.expected_worker_completions == 0:
                pending.expected_worker_completions = count
        for spill_id, count in completed_counts.items():
            pending = self.pending_spills.get(spill_id)
            if pending is not None:
                pending.worker_completions += count

        self._apply_enqueued_spills()
        self._complete_host_writes()

    def _apply_enqueued_spills(self) -> None:
        """Drop the GPU pins once every worker has completed the transfer."""
        unpinned: list[KVCacheBlock] = []
        for pending in self.pending_spills.values():
            if (
                pending.enqueue_applied
                or not pending.expected_worker_completions
                or pending.worker_completions < pending.expected_worker_completions
            ):
                continue
            unpinned.extend(pending.resident_blocks)
            pending.enqueue_applied = True
        if unpinned:
            # Resident blocks are never hashed, so each one freed is prepended:
            # reversing keeps the queue order of freeing them one at a time.
            self.gpu_pool.free_blocks(reversed(unpinned))

    def _complete_host_writes(self) -> None:
        completed = [
            pending
            for pending in self.pending_spills.values()
            if pending.enqueue_applied
            and pending.expected_worker_completions
            and pending.worker_completions >= pending.expected_worker_completions
        ]
        completed_request_ids: dict[str, None] = {}
        clean_runs: list[tuple[str, list[int]]] = []
        for pending in completed:
            self.pending_spills.pop(pending.transfer_id, None)
            request_id, page_idx = pending.page
            pending.request_state.pending_pages.pop(page_idx, None)
            if self.request_states.get(request_id) is pending.request_state:
                if not pending.restore:
                    pending.request_state.valid_pages.add(page_idx)
                completed_request_ids[request_id] = None
                if page_idx in pending.request_state.valid_pages:
                    pending.request_state.pinned_clean.add(page_idx)
                    if clean_runs and clean_runs[-1][0] == request_id:
                        clean_runs[-1][1].append(page_idx)
                    else:
                        clean_runs.append((request_id, [page_idx]))
                    if pending.restore:
                        self._record_copy(request_id, page_idx, pending.host_block)
            elif pending.request_state.publication is not None:
                if not pending.restore:
                    pending.request_state.valid_pages.add(page_idx)
                self._publish_detached_blocks(pending.request_state)
            assert self.host_manager is not None
            self.host_manager.block_pool.free_blocks([pending.host_block])
        for request_id, pages in clean_runs:
            if self._can_read_from_host(request_id):
                self._unpin_pages(request_id, self.request_states[request_id], pages)
        for request_id in completed_request_ids:
            state = self.request_states[request_id]
            while state.ready_prefix_pages in state.valid_pages:
                state.ready_prefix_pages += 1
            self._publish_host_blocks_if_ready(request_id)

    def _publish_detached_blocks(self, state: _HiSparseRequestState) -> None:
        publication = state.publication
        if publication is None or state.pending_pages:
            return
        blocks = publication.detached_blocks
        if blocks is None:
            return
        assert self.host_manager is not None
        while state.ready_prefix_pages in state.valid_pages:
            state.ready_prefix_pages += 1
        # The host group uses full-attention retention. Only sealed pages with
        # completed copies can outlive the request, including on cancellation.
        self.host_manager.block_pool.cache_full_blocks(
            request=publication.request,
            blocks=blocks,
            num_cached_blocks=publication.num_cached_blocks,
            num_full_blocks=min(state.ready_prefix_pages, publication.num_pages),
            block_size=self.host_manager.block_size,
            kv_cache_group_id=self.host_manager.kv_cache_group_id,
        )
        self.host_manager.block_pool.free_blocks(reversed(blocks))
        state.publication = None

    def free(self, request_id: str) -> None:
        """Detach the request; its clean pages stay readable copies in the pool."""
        self._resident_requests.discard(request_id)
        self._pending_imports.pop(request_id, None)
        self._import_pages.pop(request_id, None)
        self._set_import_reservation(request_id, 0)
        state = self.request_states.pop(request_id, None)
        if state is None:
            return
        publication = state.publication
        if (
            publication is not None
            and publication.request.is_finished()
            and state.pending_pages
        ):
            assert self.host_manager is not None
            publication.detached_blocks = self.host_manager.req_to_blocks[request_id][
                : publication.num_pages
            ]
            publication.num_cached_blocks = self.host_manager.num_cached_block.get(
                request_id, 0
            )
            # Completed pages need leases too: another allocation may evict
            # them before the remaining copies finish and the prefix publishes.
            self.host_manager.block_pool.touch(publication.detached_blocks)
        else:
            # Preemption can cancel a scheduled forward after its spills were
            # planned, so its copies do not prove that the KV was computed.
            state.publication = None
        for manager in self.resident_managers:
            blocks = manager.get_residency_row(request_id)
            released: list[KVCacheBlock] = []
            dropped: list[int] = []
            # Tail first, so a prefix loses its tail pages before its head.
            for page_idx in reversed(range(len(blocks))):
                block = blocks[page_idx]
                if block.is_null:
                    continue
                if page_idx in state.unpinned_pages:
                    owners = self._owners.get(block.block_id)
                    if owners is not None:
                        owners.discard((request_id, page_idx))
                elif (
                    page_idx in state.valid_pages
                    and page_idx not in state.pending_pages
                ):
                    released.append(block)
                else:
                    continue
                dropped.append(page_idx)
            if released:
                self.gpu_pool.unpin_blocks(released, self._on_block_reused)
            manager.drop_resident_pages(request_id, dropped)

reserved_import_pages property

Host pages admitted imports still need for their sequence.

_adopt_copies(request_id, state, host_blocks)

Point null prefix pages at readable GPU copies of their contents.

Runs after the hit length is fully reconciled, so copy hits and misses can be scattered per page without affecting the prefix hit.

Source code in vllm/v1/hisparse/coordinator.py
def _adopt_copies(
    self,
    request_id: str,
    state: _HiSparseRequestState,
    host_blocks: Sequence[KVCacheBlock],
) -> None:
    """Point null prefix pages at readable GPU copies of their contents.

    Runs after the hit length is fully reconciled, so copy hits and misses
    can be scattered per page without affecting the prefix hit.
    """
    if not self.copies:
        return
    copies: dict[int, tuple[KVCacheBlock, ...]] = {}
    for host_idx, host_block in enumerate(host_blocks):
        if host_block.is_null or host_block.block_hash is None:
            continue
        blocks = self.copies.get(host_block.block_hash)
        if blocks is not None:
            copies[host_idx] = blocks
    if not copies:
        return
    host_indices = list(copies)
    adopted: list[KVCacheBlock] = []
    for manager, column in zip(self.resident_managers, zip(*copies.values())):
        for host_idx, block in manager.adopt_resident_pages(
            request_id, host_indices, column
        ):
            adopted.append(block)
            state.pinned_clean.add(host_idx)
            if host_idx < state.synced_pages:
                state.stale_pages.add(host_idx)
    self.gpu_pool.touch(adopted)

_apply_enqueued_spills()

Drop the GPU pins once every worker has completed the transfer.

Source code in vllm/v1/hisparse/coordinator.py
def _apply_enqueued_spills(self) -> None:
    """Drop the GPU pins once every worker has completed the transfer."""
    unpinned: list[KVCacheBlock] = []
    for pending in self.pending_spills.values():
        if (
            pending.enqueue_applied
            or not pending.expected_worker_completions
            or pending.worker_completions < pending.expected_worker_completions
        ):
            continue
        unpinned.extend(pending.resident_blocks)
        pending.enqueue_applied = True
    if unpinned:
        # Resident blocks are never hashed, so each one freed is prepended:
        # reversing keeps the queue order of freeing them one at a time.
        self.gpu_pool.free_blocks(reversed(unpinned))

_can_read_from_host(request_id)

Whether resident pages may be dropped under the request.

Source code in vllm/v1/hisparse/coordinator.py
def _can_read_from_host(self, request_id: str) -> bool:
    """Whether resident pages may be dropped under the request."""
    return bool(self.hot_managers) and all(
        manager.has_hot(request_id) for manager in self.hot_managers
    )

_fills_admission_window(request_id)

Whether the request holds the resident pages admission reserved.

Admission caps a request's resident pages at the in-flight window, assuming older pages move to host; keeping them pinned past it lets a chunked prefill outgrow the pool it was admitted into.

Source code in vllm/v1/hisparse/coordinator.py
def _fills_admission_window(self, request_id: str) -> bool:
    """Whether the request holds the resident pages admission reserved.

    Admission caps a request's resident pages at the in-flight window,
    assuming older pages move to host; keeping them pinned past it lets a
    chunked prefill outgrow the pool it was admitted into.
    """
    return any(
        len(manager.req_to_blocks.get(request_id, ()))
        >= manager.max_admission_blocks_per_request
        for manager in self.resident_managers
    )

_plan_page_transfers(request_id, page_indices, budget, *, restore=False)

Plan up to budget transfers for the pages, in the given order.

Source code in vllm/v1/hisparse/coordinator.py
def _plan_page_transfers(
    self,
    request_id: str,
    page_indices: Iterable[int],
    budget: int,
    *,
    restore: bool = False,
) -> None:
    """Plan up to `budget` transfers for the pages, in the given order."""
    assert self.host_manager is not None
    host_blocks = self.host_manager.req_to_blocks.get(request_id)
    if host_blocks is None:
        return
    state = self._get_request_state(request_id)
    num_pages = min(len(host_blocks), self._num_resident_pages(request_id))
    candidates = (
        page_idx
        for page_idx in page_indices
        if page_idx < num_pages
        and page_idx not in state.pending_pages
        and not host_blocks[page_idx].is_null
    )
    pinned_host_blocks: list[KVCacheBlock] = []
    pinned_blocks: list[KVCacheBlock] = []
    while budget > 0 and (pages := list(itertools.islice(candidates, budget))):
        columns = [
            manager.get_residency_pages(request_id, pages)
            for manager in self.resident_managers
        ]
        for page_idx, blocks in zip(pages, zip(*columns)):
            if any(block.is_null for block in blocks):
                continue
            host_block = host_blocks[page_idx]
            budget -= 1
            pinned_host_blocks.append(host_block)
            pinned_blocks.extend(blocks)
            spill_id = self.next_spill_id
            self.next_spill_id += 1
            plan = SparseKVPageTransfer(
                transfer_id=spill_id,
                host_block_id=host_block.block_id,
                resident_block_ids=tuple(block.block_id for block in blocks),
                restore=restore,
            )
            self.pending_spills[spill_id] = _PendingSpill(
                transfer_id=spill_id,
                page=(request_id, page_idx),
                request_state=state,
                host_block=host_block,
                resident_blocks=blocks,
                restore=restore,
            )
            state.pending_pages[page_idx] = spill_id
            self.spills_to_send.append(plan)
    if pinned_blocks:
        self.host_manager.block_pool.touch(pinned_host_blocks)
        self.gpu_pool.touch(pinned_blocks)

_promote_imported_pages(request_id, num_pages)

Back an import's first num_pages host pages with new GPU pages.

Imports land in the host tier. While the GPU pool has room above the transition watermark, the imported pages are restored into resident pages too, so decode reads them like a locally computed prefix. They are clean copies of durable host pages: the residency policy unpins them, and the pool reclaims them only after every other free block.

Source code in vllm/v1/hisparse/coordinator.py
def _promote_imported_pages(self, request_id: str, num_pages: int) -> list[int]:
    """Back an import's first `num_pages` host pages with new GPU pages.

    Imports land in the host tier. While the GPU pool has room above the
    transition watermark, the imported pages are restored into resident
    pages too, so decode reads them like a locally computed prefix. They are
    clean copies of durable host pages: the residency policy unpins them,
    and the pool reclaims them only after every other free block.
    """
    assert self.host_manager is not None
    num_managers = len(self.resident_managers)
    spare = self.gpu_pool.get_num_free_blocks() - self.transition_watermark
    host_blocks = self.host_manager.req_to_blocks[request_id]
    num_pages = min(
        num_pages, len(host_blocks), self._num_resident_pages(request_id)
    )
    state = self._get_request_state(request_id)
    candidates = [
        page_idx
        for page_idx in range(num_pages)
        if not host_blocks[page_idx].is_null and page_idx not in state.pending_pages
    ]
    columns = [
        manager.get_residency_pages(request_id, candidates)
        for manager in self.resident_managers
    ]
    pages = [
        page_idx
        for page_idx, blocks in zip(candidates, zip(*columns))
        if all(block.is_null for block in blocks)
    ][: max(spare, 0) // num_managers]
    if not pages:
        return []
    blocks = self.gpu_pool.get_new_blocks(len(pages) * num_managers)
    for slot, manager in enumerate(self.resident_managers):
        adopted = manager.adopt_resident_pages(
            request_id, pages, blocks[slot::num_managers]
        )
        assert len(adopted) == len(pages), f"Cannot promote pages of {request_id}"
    state.stale_pages.update(
        page_idx for page_idx in pages if page_idx < state.synced_pages
    )
    return pages

_publish_host_blocks_if_ready(request_id)

Publish the durable prefix of the pending publication.

Host writes trail a long prefill by about a chunk, so waiting for the whole computed prefix would publish nothing until the prefill ends, and a prefill preempted before then would recompute from the start.

Source code in vllm/v1/hisparse/coordinator.py
def _publish_host_blocks_if_ready(self, request_id: str) -> None:
    """Publish the durable prefix of the pending publication.

    Host writes trail a long prefill by about a chunk, so waiting for the
    whole computed prefix would publish nothing until the prefill ends, and
    a prefill preempted before then would recompute from the start.
    """
    state = self.request_states.get(request_id)
    if state is None:
        return
    publication = state.publication
    if publication is None:
        return
    assert self.host_manager is not None
    num_tokens = min(
        publication.num_computed_tokens,
        state.ready_prefix_pages * self.host_manager.block_size,
    )
    self.host_manager.publish_blocks(
        publication.request,
        num_tokens,
        retention_interval=publication.retention_interval,
        replay_boundaries=publication.replay_boundaries,
    )
    self._record_copies(request_id, num_tokens)
    if state.ready_prefix_pages >= publication.num_pages:
        state.publication = None

_record_copies(request_id, num_computed_tokens)

Index the GPU copies of just-published host blocks for later hits.

Source code in vllm/v1/hisparse/coordinator.py
def _record_copies(self, request_id: str, num_computed_tokens: int) -> None:
    """Index the GPU copies of just-published host blocks for later hits."""
    if not self.resident_managers:
        return
    assert self.host_manager is not None
    host_blocks = self.host_manager.req_to_blocks.get(request_id)
    if not host_blocks:
        return
    state = self._get_request_state(request_id)
    num_host_blocks = min(
        num_computed_tokens // self.host_manager.block_size, len(host_blocks)
    )
    if state.copies_recorded_blocks >= num_host_blocks:
        return
    num_pages = min(num_host_blocks, self._num_resident_pages(request_id))
    pages = [
        host_idx
        for host_idx in range(state.copies_recorded_blocks, num_pages)
        if host_idx not in state.pending_pages
    ]
    columns = [
        manager.get_residency_pages(request_id, pages)
        for manager in self.resident_managers
    ]
    for host_idx, blocks in zip(pages, zip(*columns)):
        self._store_copy(host_blocks[host_idx], blocks)
    state.copies_recorded_blocks = max(
        state.copies_recorded_blocks, num_host_blocks
    )

_unpin_pages(request_id, state, page_indices)

Release the pages' resident blocks to the pool, in the given order.

Source code in vllm/v1/hisparse/coordinator.py
def _unpin_pages(
    self,
    request_id: str,
    state: _HiSparseRequestState,
    page_indices: Iterable[int],
) -> None:
    """Release the pages' resident blocks to the pool, in the given order."""
    num_unpinnable = self._num_resident_pages(request_id) - ACTIVE_TAIL_PAGES
    pages = [
        page_idx
        for page_idx in page_indices
        if page_idx < num_unpinnable
        and page_idx not in state.pending_pages
        and page_idx in state.valid_pages
    ]
    if not pages:
        return
    columns = [
        manager.get_residency_pages(request_id, pages)
        for manager in self.resident_managers
    ]
    released: list[KVCacheBlock] = []
    for page_idx, blocks in zip(pages, zip(*columns)):
        if any(block.is_null for block in blocks):
            continue
        for block in blocks:
            self._owners.setdefault(block.block_id, set()).add(
                (request_id, page_idx)
            )
        released.extend(blocks)
        state.pinned_clean.discard(page_idx)
        state.unpinned_pages.add(page_idx)
    if released:
        self.gpu_pool.unpin_blocks(released, self._on_block_reused)

advance_scheduled(requests)

Run the per-step residency work for each scheduled request.

requests pairs a request id with the number of tokens computed once the step completes. As in KVCacheCoordinator.cache_blocks, the last num_reprefillable_tokens are not final yet and are not written back.

Source code in vllm/v1/hisparse/coordinator.py
def advance_scheduled(self, requests: Iterable[tuple[str, int]]) -> None:
    """Run the per-step residency work for each scheduled request.

    `requests` pairs a request id with the number of tokens computed
    once the step completes. As in `KVCacheCoordinator.cache_blocks`, the
    last `num_reprefillable_tokens` are not final yet and are not written
    back.
    """
    if not self.resident_managers:
        return
    for request_id, num_tokens in requests:
        self.plan_prefix_materialization(
            request_id, max(0, num_tokens - self.num_reprefillable_tokens)
        )
        self.update_residency(request_id)

all_context_pages_resident(requests)

Return whether every scheduled request can read only resident KV.

Source code in vllm/v1/hisparse/coordinator.py
def all_context_pages_resident(
    self,
    requests: Iterable[tuple[str, int, int]],
) -> bool:
    """Return whether every scheduled request can read only resident KV."""
    if not self.resident_managers:
        return False
    first = self.resident_managers[0]
    for request_id, num_computed_tokens, num_scheduled_tokens in requests:
        num_pages = cdiv(
            num_computed_tokens + num_scheduled_tokens, first.block_size
        )
        if len(first.req_to_blocks.get(request_id, ())) < num_pages:
            return False
        if request_id in self._resident_requests:
            continue
        if any(
            block.is_null
            for manager in self.resident_managers
            for block in manager.get_residency_row(request_id)
        ):
            return False
        self._resident_requests.add(request_id)
    return True

build_offload_command()

Package residency decisions for the worker connector.

Source code in vllm/v1/hisparse/coordinator.py
def build_offload_command(self) -> SparseKVOffloadCommand | None:
    """Package residency decisions for the worker connector."""
    if not self.resident_managers:
        return None
    command = SparseKVOffloadCommand(page_transfers=self.spills_to_send)
    self.spills_to_send = []
    return command

build_row_mirrors(requests)

Map scheduled rows to scheduler-owned resident and host blocks.

Source code in vllm/v1/hisparse/coordinator.py
def build_row_mirrors(
    self,
    requests: Iterable[tuple[str, int, int]],
) -> tuple[SparseKVRowMirror, ...]:
    """Map scheduled rows to scheduler-owned resident and host blocks."""
    if not self.resident_managers or self.host_manager is None:
        return ()
    block_size = self.resident_managers[0].block_size
    mirrors: list[SparseKVRowMirror] = []
    for request_id, num_computed_tokens, num_scheduled_tokens in requests:
        host_blocks = self.host_manager.req_to_blocks.get(request_id)
        if host_blocks is None or any(
            request_id not in manager.req_to_blocks
            for manager in self.resident_managers
        ):
            continue
        token_position = num_computed_tokens
        end_position = token_position + num_scheduled_tokens
        while token_position < end_position:
            page_idx, row_offset = divmod(token_position, block_size)
            num_rows = min(block_size - row_offset, end_position - token_position)
            # A page without a GPU copy has no mirror source, and a page
            # without a host block has no destination. Skip just that page:
            # its neighbours in the window are still mirrorable, and a
            # window only ever moves forward, so dropping them here would
            # leave their host rows permanently stale.
            source_starts = []
            for manager in self.resident_managers:
                block = manager.get_resident_page(request_id, page_idx)
                if block is None:
                    break
                source_starts.append(block.block_id * block_size + row_offset)
            if len(source_starts) != len(self.resident_managers):
                token_position += num_rows
                continue
            host_block_idx = page_idx
            if host_block_idx >= len(host_blocks):
                token_position += num_rows
                continue
            host_block = host_blocks[host_block_idx]
            if host_block.is_null:
                token_position += num_rows
                continue
            mirrors.append(
                SparseKVRowMirror(
                    source_starts=tuple(source_starts),
                    destination_start=(
                        host_block.block_id * block_size + row_offset
                    ),
                    num_rows=num_rows,
                )
            )
            token_position += num_rows
    return tuple(mirrors)

commit_computed_blocks(request_id, num_host_pages)

Account for a prefix hit on host pages the request now references.

Source code in vllm/v1/hisparse/coordinator.py
def commit_computed_blocks(self, request_id: str, num_host_pages: int) -> None:
    """Account for a prefix hit on host pages the request now references."""
    self._resident_requests.discard(request_id)
    if not self.resident_managers or self.host_manager is None:
        return
    host_blocks = self.host_manager.req_to_blocks.get(request_id, ())
    num_host_pages = min(num_host_pages, len(host_blocks))
    if num_host_pages == 0:
        return
    state = self._get_request_state(request_id)
    state.valid_pages.update(range(num_host_pages))
    state.ready_prefix_pages = max(num_host_pages, state.ready_prefix_pages)
    # Adopting pins copies out of the free pool, so it must wait until the
    # allocation admission counted on has run (see update_residency).
    state.pages_to_adopt = max(state.pages_to_adopt, num_host_pages)
    state.copies_recorded_blocks = max(state.copies_recorded_blocks, num_host_pages)

complete_host_import(request_id, num_computed_tokens)

Publish externally populated host pages after connector completion.

Source code in vllm/v1/hisparse/coordinator.py
def complete_host_import(self, request_id: str, num_computed_tokens: int) -> None:
    """Publish externally populated host pages after connector completion."""
    if not self.resident_managers:
        return
    block_size = self.resident_managers[0].block_size
    num_pages = num_computed_tokens // block_size
    state = self._get_request_state(request_id)
    state.valid_pages = set(range(num_pages))
    state.ready_prefix_pages = num_pages
    if num_computed_tokens:
        # A local prefix hit's surviving GPU copies cost no copy, so adopt
        # them before promoting the rest.
        self._adopt_pending_copies(request_id, state)
        tail_page = (num_computed_tokens - 1) // block_size
        pages = (tail_page, *self._promote_imported_pages(request_id, tail_page))
        self._plan_page_transfers(
            request_id, pages, budget=len(pages), restore=True
        )
    self._publish_host_blocks_if_ready(request_id)

free(request_id)

Detach the request; its clean pages stay readable copies in the pool.

Source code in vllm/v1/hisparse/coordinator.py
def free(self, request_id: str) -> None:
    """Detach the request; its clean pages stay readable copies in the pool."""
    self._resident_requests.discard(request_id)
    self._pending_imports.pop(request_id, None)
    self._import_pages.pop(request_id, None)
    self._set_import_reservation(request_id, 0)
    state = self.request_states.pop(request_id, None)
    if state is None:
        return
    publication = state.publication
    if (
        publication is not None
        and publication.request.is_finished()
        and state.pending_pages
    ):
        assert self.host_manager is not None
        publication.detached_blocks = self.host_manager.req_to_blocks[request_id][
            : publication.num_pages
        ]
        publication.num_cached_blocks = self.host_manager.num_cached_block.get(
            request_id, 0
        )
        # Completed pages need leases too: another allocation may evict
        # them before the remaining copies finish and the prefix publishes.
        self.host_manager.block_pool.touch(publication.detached_blocks)
    else:
        # Preemption can cancel a scheduled forward after its spills were
        # planned, so its copies do not prove that the KV was computed.
        state.publication = None
    for manager in self.resident_managers:
        blocks = manager.get_residency_row(request_id)
        released: list[KVCacheBlock] = []
        dropped: list[int] = []
        # Tail first, so a prefix loses its tail pages before its head.
        for page_idx in reversed(range(len(blocks))):
            block = blocks[page_idx]
            if block.is_null:
                continue
            if page_idx in state.unpinned_pages:
                owners = self._owners.get(block.block_id)
                if owners is not None:
                    owners.discard((request_id, page_idx))
            elif (
                page_idx in state.valid_pages
                and page_idx not in state.pending_pages
            ):
                released.append(block)
            else:
                continue
            dropped.append(page_idx)
        if released:
            self.gpu_pool.unpin_blocks(released, self._on_block_reused)
        manager.drop_resident_pages(request_id, dropped)

has_pending_reclamation()

Whether an in-flight spill will free GPU or host blocks on completion.

Source code in vllm/v1/hisparse/coordinator.py
def has_pending_reclamation(self) -> bool:
    """Whether an in-flight spill will free GPU or host blocks on completion."""
    return any(
        pending.host_block.ref_cnt == 1
        or (
            pending.page[0] in self.request_states
            and self._can_read_from_host(pending.page[0])
        )
        for pending in self.pending_spills.values()
    )

plan_prefix_materialization(request_id, num_computed_tokens)

Eagerly write back every sealed page that has no host copy yet.

Source code in vllm/v1/hisparse/coordinator.py
def plan_prefix_materialization(
    self, request_id: str, num_computed_tokens: int
) -> None:
    """Eagerly write back every sealed page that has no host copy yet."""
    if not self.resident_managers:
        return
    assert self.host_manager is not None
    host_block_size = self.host_manager.block_size
    num_pages = num_computed_tokens // host_block_size
    importing_pages = cdiv(
        self._pending_imports.get(request_id, 0), host_block_size
    )
    state = self._get_request_state(request_id)
    budget = max(self.max_spill_pages - len(self.spills_to_send), 0)
    first_page = max(state.ready_prefix_pages, importing_pages)
    if budget > 0 and first_page < num_pages:
        self._plan_page_transfers(
            request_id,
            (
                page_idx
                for page_idx in range(first_page, num_pages)
                if page_idx not in state.valid_pages
            ),
            budget,
        )

publish_when_ready(request, num_computed_tokens, retention_interval, *, replay_boundaries)

Publish host-source hashes of the pages that are already durable.

Source code in vllm/v1/hisparse/coordinator.py
def publish_when_ready(
    self,
    request: Request,
    num_computed_tokens: int,
    retention_interval: int | None,
    *,
    replay_boundaries: Sequence[int],
) -> None:
    """Publish host-source hashes of the pages that are already durable."""
    manager = self.host_manager
    if manager is None:
        return
    # A lookahead hash arrives only once the tokens after its page are known.
    num_computed_tokens = min(
        num_computed_tokens,
        len(request.block_hashes) * manager.block_pool.hash_block_size,
    )
    request_id = request.request_id
    self._get_request_state(request_id).publication = _PendingPublication(
        request=request,
        num_computed_tokens=num_computed_tokens,
        num_pages=num_computed_tokens // manager.block_size,
        retention_interval=retention_interval,
        replay_boundaries=replay_boundaries,
    )
    self._publish_host_blocks_if_ready(request_id)

record_pending_host_import(request_id, num_tokens)

Note a prefix an external load is populating in host pages.

Source code in vllm/v1/hisparse/coordinator.py
def record_pending_host_import(self, request_id: str, num_tokens: int) -> None:
    """Note a prefix an external load is populating in host pages."""
    self._resident_requests.discard(request_id)
    self._pending_imports[request_id] = num_tokens
    if self._sized_import is not None and self._sized_import[0] == request_id:
        self._import_pages[request_id] = self._sized_import[1]
    self._sized_import = None

release_completed_host_copies(dst_block_ids)

Release the endpoints of host copies the worker reports having run.

Source code in vllm/v1/hisparse/coordinator.py
def release_completed_host_copies(self, dst_block_ids: Iterable[int]) -> None:
    """Release the endpoints of host copies the worker reports having run."""
    blocks: list[KVCacheBlock] = []
    for dst_block_id in dst_block_ids:
        pair = self._retained_copies.pop(dst_block_id, None)
        if pair is not None:
            blocks.extend(pair)
    if not blocks:
        return
    assert self.host_manager is not None
    self.host_manager.block_pool.free_blocks(reversed(blocks))

size_import(request_id, num_pages)

Note the full-sequence host pages of an import being admitted.

The admission gate runs in the same allocate_slots call as the allocation, so the next record_pending_host_import is this request's if it is admitted; a size left by a rejected admission is discarded.

Source code in vllm/v1/hisparse/coordinator.py
def size_import(self, request_id: str, num_pages: int) -> None:
    """Note the full-sequence host pages of an import being admitted.

    The admission gate runs in the same `allocate_slots` call as the
    allocation, so the next `record_pending_host_import` is this
    request's if it is admitted; a size left by a rejected admission is
    discarded.
    """
    self._sized_import = (request_id, num_pages)

take_host_block_copies()

Drain host copy-on-write work, retaining both endpoints.

The worker runs the copies in the step that carries them, so the endpoints are released when that step's output comes back.

Source code in vllm/v1/hisparse/coordinator.py
def take_host_block_copies(self) -> tuple[KVCacheBlockCopy, ...]:
    """Drain host copy-on-write work, retaining both endpoints.

    The worker runs the copies in the step that carries them, so the
    endpoints are released when that step's output comes back.
    """
    if self.host_manager is None:
        return ()
    pairs = self.host_manager.take_host_cow_copies()
    for source_block, cow_block in pairs:
        self._retained_copies[cow_block.block_id] = (source_block, cow_block)
    return tuple(
        KVCacheBlockCopy(
            src_block_id=source_block.block_id,
            dst_block_id=cow_block.block_id,
        )
        for source_block, cow_block in pairs
    )

take_residency_updates(request_ids)

Resident block ids the worker lacks for the scheduled requests: stale pages plus pages appended since the last update.

Requests that are not scheduled keep their changes until they are: only a scheduled request's residency is read.

Source code in vllm/v1/hisparse/coordinator.py
def take_residency_updates(
    self, request_ids: Iterable[str]
) -> dict[str, SparseKVResidencyUpdate]:
    """Resident block ids the worker lacks for the scheduled requests:
    stale pages plus pages appended since the last update.

    Requests that are not scheduled keep their changes until they are:
    only a scheduled request's residency is read.
    """
    updates: dict[str, SparseKVResidencyUpdate] = {}
    if not self.resident_managers:
        return updates
    first = self.resident_managers[0]
    for request_id in request_ids:
        state = self._get_request_state(request_id)
        num_pages = len(first.req_to_blocks.get(request_id, ()))
        start = state.synced_pages
        if not state.stale_pages and start >= num_pages:
            continue
        stale_pages = sorted(state.stale_pages)
        block_ids: list[list[int]] = []
        for manager in self.resident_managers:
            null_block_id = manager.block_pool.null_block.block_id
            group_block_ids = [
                null_block_id if block is None else block.block_id
                for block in (
                    manager.get_resident_page(request_id, page_idx)
                    for page_idx in stale_pages
                )
            ]
            group_block_ids.extend(
                block.block_id
                for block in manager.get_residency_row(request_id, start)
            )
            block_ids.append(group_block_ids)
        updates[request_id] = SparseKVResidencyUpdate(
            pages=[*stale_pages, *range(start, num_pages)],
            block_ids=tuple(block_ids),
        )
        state.stale_pages.clear()
        state.synced_pages = num_pages
    return updates

update_residency(request_id)

Per-step residency policy for a scheduled request.

A request that can read from host releases every clean sealed page to the pool. One that cannot keeps its pages pinned until the shared pool runs low, or until it holds the resident pages admission reserved for it, then asks for a hot region. Pages remain pinned until that region is allocated on a subsequent scheduling pass.

Source code in vllm/v1/hisparse/coordinator.py
def update_residency(self, request_id: str) -> None:
    """Per-step residency policy for a scheduled request.

    A request that can read from host releases every clean sealed page to
    the pool. One that cannot keeps its pages pinned until the shared pool
    runs low, or until it holds the resident pages admission reserved for
    it, then asks for a hot region. Pages remain pinned until that region is
    allocated on a subsequent scheduling pass.
    """
    if not self.resident_managers:
        return
    state = self._get_request_state(request_id)
    self._adopt_pending_copies(request_id, state)
    if not self._can_read_from_host(request_id):
        if (
            self.gpu_pool.get_num_free_blocks() >= self.transition_watermark
            and not self._fills_admission_window(request_id)
        ):
            return
        for manager in self.hot_managers:
            manager.require_hot(request_id)
        return
    self._unpin_clean_pages(request_id, state)

_HiSparseRequestState dataclass

Residency bookkeeping for one request.

A resident page moves through three states: dirty (only the GPU copy exists), clean (valid_pages: the host copy is complete) and unpinned (unpinned_pages: clean, and its allocation reference was released so the pool may reuse it). pinned_clean tracks clean pages still holding their reference, which only happens before the request can read from host.

Source code in vllm/v1/hisparse/coordinator.py
@dataclass(slots=True)
class _HiSparseRequestState:
    """Residency bookkeeping for one request.

    A resident page moves through three states: dirty (only the GPU copy
    exists), clean (`valid_pages`: the host copy is complete) and unpinned
    (`unpinned_pages`: clean, and its allocation reference was released so
    the pool may reuse it). `pinned_clean` tracks clean pages still holding
    their reference, which only happens before the request can read from host.
    """

    valid_pages: set[int] = field(default_factory=set)
    ready_prefix_pages: int = 0
    pending_pages: dict[int, int] = field(default_factory=dict)
    publication: _PendingPublication | None = None
    copies_recorded_blocks: int = 0
    pinned_clean: set[int] = field(default_factory=set)
    unpinned_pages: set[int] = field(default_factory=set)
    # Prefix pages whose GPU copies are adopted after the admitting allocation.
    pages_to_adopt: int = 0
    # The worker holds this request's residency for every page below
    # synced_pages, except the stale pages changed since it was sent.
    synced_pages: int = 0
    stale_pages: set[int] = field(default_factory=set)

get_hisparse_coordinator(kv_cache_manager, *, import_step_tokens=None)

Return the coordinator shared by a KV cache manager's HiSparse groups.

Built on first call and memoised on the HiSparse managers it binds itself to. Must run before the first request is admitted.

Source code in vllm/v1/hisparse/coordinator.py
def get_hisparse_coordinator(
    kv_cache_manager: "KVCacheManager",
    *,
    import_step_tokens: int | None = None,
) -> HiSparseCoordinator:
    """Return the coordinator shared by a KV cache manager's HiSparse groups.

    Built on first call and memoised on the HiSparse managers it binds itself
    to. Must run before the first request is admitted.
    """
    managers = tuple(kv_cache_manager.coordinator.single_type_managers)
    for manager in managers:
        coordinator = getattr(manager, "coordinator", None)
        if coordinator is not None:
            assert isinstance(coordinator, HiSparseCoordinator)
            assert import_step_tokens in (None, coordinator.import_step_tokens)
            return coordinator
    coordinator = HiSparseCoordinator(
        kv_cache_manager.kv_cache_config,
        managers,
        kv_cache_manager.max_model_len,
        num_reprefillable_tokens=kv_cache_manager.coordinator.num_reprefillable_tokens,
        import_step_tokens=import_step_tokens or 0,
    )
    if coordinator.host_manager is None:
        raise ValueError("No HiSparse cache group is configured.")
    return coordinator