vllm.models.glm5next.amd.ops.kpool_compress ¶
kpool (key-pooling) Triton kernels for the sparse-attention indexer.
The cache stores POOLS (1 entry per pool_size consecutive tokens) rather than individual tokens. compress_ratio == pool_size on the kv_cache_spec makes the metadata builder emit pool-granular slot_mapping / seq_lens / cu_seq_lens / page_table for free; this file supplies the compress-write kernel (replacing indexer_k_quant_and_cache) and the pool-level topk helpers (select pools -> expand to tokens -> append tail).
Functions:
-
append_tail_to_topk–Append non-pooled tail tokens after expanded history tokens.
-
build_pooled_page_table–Build a pool-granular page table by taking every
pool_size-th -
compute_pooled_write_locs–Map logical pooled-K ids to physical flat cache slots.
-
expand_pools_and_append_tail–Fuse
expand_pools_to_tokens+append_tail_to_topk(identity path). -
expand_pools_to_tokens–Expand selected full-pool ids to a strict-width token topk tensor.
-
history_group_budget_for_topk–Number of pools to select so that expanding yields
topktokens. -
kpool_compress_and_write_cache–Compress
pool_sizetokens into one fp8 K and write atloc. -
kpool_decode_update_and_maybe_write_cache_batched–Batched decode-step kpool update for spec verify (
next_n > 1). -
kpool_seed_tail_cache–Seed the paged tail cache from a prefill batch (see the kernel).
_kpool_decode_update_batched_kernel(buf_fp8_ptr, buf_fp32_ptr, tail_kv_ptr, tail_slot_mapping_ptr, key_ptr, key_stride_b, key_stride_t, slot_score_ptr, ss_stride_b, ss_stride_t, ape_ptr, ape_stride_0, slot_mapping_ptr, positions_ptr, NEXT_N, PAGE_SIZE, BUF_NUMEL_PER_PAGE, POOL_SIZE, TAIL_BLOCK_ELEMS, KPOOL_HEAD, HEAD_DIM, S_OFFSET_NBYTES_IN_PAGE, FP8_MAX, PRESHUFFLE, ROUND_SCALE, BLOCK_D) ¶
One program per request; iterates its NEXT_N verify tokens in order.
Replaces the caller's per-token sequential launch loop. The intra-request iteration MUST stay in position order: a pool-completion at token t reads the tail-ring slots that tokens t < t (same request) just stashed in this same invocation. tl.range iterates sequentially within the program, so those stashes are visible to the later completion read. Cross-request programs are independent (distinct tail blocks). With NEXT_N < POOL_SIZE (the spec-verify case: NEXT_N ~= num_spec+1, POOL_SIZE=16) at most one completion can occur per request per call, but the ordered loop is correct for any NEXT_N.
Source code in vllm/models/glm5next/amd/ops/kpool_compress.py
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 | |
_kpool_softmax_rotate_write_cache_kernel(buf_fp8_ptr, buf_fp32_ptr, slot_k_ptr, slot_score_ptr, ape_ptr, loc_ptr, write_mask_ptr, compressed_k_ptr, compressed_scale_ptr, slot_k_stride_0, slot_k_stride_1, slot_score_stride_0, slot_score_stride_1, ape_stride_0, PAGE_SIZE, BUF_NUMEL_PER_PAGE, POOL_SIZE, HEAD_DIM, S_OFFSET_NBYTES_IN_PAGE, FP8_MAX, PRESHUFFLE, ROUND_SCALE, HAS_WRITE_MASK, RETURN_COMPRESSED, WRITE_CACHE, BLOCK_D) ¶
One program per pool. softmax(slot_score+ape)-weighted sum of slot_k -> Hadamard-128 -> per-vector fp8 absmax quant -> write to cache at loc.
Source code in vllm/models/glm5next/amd/ops/kpool_compress.py
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 | |
_kpool_tail_seed_kernel(key_ptr, score_ptr, tslot_ptr, tail_ptr, n_tokens, TAIL_BLOCK_ELEMS, KPOOL_HEAD, HEAD_DIM, KPOOL, BLOCK_D) ¶
Copy token i's raw K + gate into its request's tail block.
Token i is among its request's last KPOOL tokens iff the token KPOOL ahead belongs to a different tail block (or is past the batch / padding, slot < 0). tslot = block * KPOOL + pos % KPOOL; the destination is tail[block, {0:K, 1:score}, pos % KPOOL, :].
Source code in vllm/models/glm5next/amd/ops/kpool_compress.py
append_tail_to_topk(topk_result, seq_lens, pool_lens, pool_size, page_table=None, topk_offsets=None) ¶
Append non-pooled tail tokens after expanded history tokens.
index_kpool_always_select_tail keeps the (incomplete) trailing pool so the most recent tokens are always attended to.
Source code in vllm/models/glm5next/amd/ops/kpool_compress.py
build_pooled_page_table(page_table, pool_size) ¶
Build a pool-granular page table by taking every pool_size-th token-page column (one pool maps to pool_size token pages).
Uses gather (not strided slicing) so the result is always a fresh row-major tensor — some downstream kernels require stride(-1) == 1.
Source code in vllm/models/glm5next/amd/ops/kpool_compress.py
compute_pooled_write_locs(page_table_64, pool_ids, pool_size) ¶
Map logical pooled-K ids to physical flat cache slots.
pool_size consecutive tokens share one pool slot that lives at the first token page of each page-group. page_table_64 maps token pages to physical block ids; we gather the block id of each pool's page-group and add the in-block pool offset.
Source code in vllm/models/glm5next/amd/ops/kpool_compress.py
expand_pools_and_append_tail(pool_ids, seq_lens, pool_size) ¶
Fuse expand_pools_to_tokens + append_tail_to_topk (identity path).
Produces the same [rows, topk + pool_size - 1] int32 output as calling the two functions in sequence when neither page_table nor topk_offsets is passed — the only path used by the GLM-5.3-Flash indexer. The kernel derives pool_len = seq_len // pool_size internally, so the caller no longer needs to precompute it. Replaces ~25 elementwise kernels with one Triton launch.
Source code in vllm/models/glm5next/amd/ops/kpool_compress.py
expand_pools_to_tokens(group_ids, group_valid, topk, pool_size, page_table=None, topk_offsets=None) ¶
Expand selected full-pool ids to a strict-width token topk tensor.
Source code in vllm/models/glm5next/amd/ops/kpool_compress.py
history_group_budget_for_topk(topk, pool_size) ¶
Number of pools to select so that expanding yields topk tokens.
kpool_compress_and_write_cache(kv_cache, slot_k, slot_score, ape, loc, pool_size, head_dim=INDEX_HEAD_DIM, write_mask=None, round_scale=True, return_compressed=False, write_cache=True) ¶
Compress pool_size tokens into one fp8 K and write at loc.
Parameters:
-
(kv_cache¶Tensor) –indexer K cache
[num_blocks, block_size, head_dim+4]uint8. -
(slot_k¶Tensor) –[n_pools, pool_size, head_dim]bf16 — raw per-token K. -
(slot_score¶Tensor) –[n_pools, pool_size, head_dim]— per-token gate score. -
(ape¶Tensor) –[pool_size, head_dim]fp32 — per-slot position bias. -
(loc¶Tensor) –[n_pools]int64 — flat physical slot per pool.
Source code in vllm/models/glm5next/amd/ops/kpool_compress.py
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 | |
kpool_decode_update_and_maybe_write_cache_batched(kv_cache, tail_kv_cache, tail_slot_mapping, key, slot_score, ape, slot_mapping, positions, pool_size, head_dim=INDEX_HEAD_DIM, round_scale=True) ¶
Batched decode-step kpool update for spec verify (next_n > 1).
One launch replaces the caller's per-token loop. Inputs are grouped per request: [num_requests, next_n, ...]. Each program handles one request's next_n tokens in position order (see the kernel docstring for why ordering is required for pool-completion correctness).
Plain decode (next_n == 1) is handled here too — the kernel collapses to a single-iteration loop.
Parameters:
-
(kv_cache¶Tensor) –indexer K cache
[num_blocks, block_size, head_dim+4]uint8. -
(tail_kv_cache¶Tensor) –paged tail cache
[num_blocks, 2, pool_size, head_dim]bf16 (K at half 0, gate score at half 1). -
(tail_slot_mapping¶Tensor) –[num_requests, next_n]int32. -
(key¶Tensor) –[num_requests, next_n, head_dim]bf16. -
(slot_score¶Tensor) –[num_requests, next_n, head_dim]bf16. -
(ape¶Tensor) –[pool_size, head_dim]fp32. -
(slot_mapping¶Tensor) –[num_requests, next_n]int32. -
(positions¶Tensor) –[num_requests, next_n]int32.
Source code in vllm/models/glm5next/amd/ops/kpool_compress.py
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 | |
kpool_seed_tail_cache(tail_kv_cache, key, gate_score, tslot, kpool, head_dim=INDEX_HEAD_DIM) ¶
Seed the paged tail cache from a prefill batch (see the kernel).