vllm.v1.worker.mamba_utils ¶
Classes:
-
MambaBuffers–Single owner for all mamba-specific runner buffers.
-
MambaSpecDecodeGPUContext–Context for GPU-side Mamba state copy operations during the
Functions:
-
cleanup_mamba_state_idx–Pop stale
mamba_state_idxentries for finished/preempted/resumed reqs. -
postprocess_mamba_align_gpu–GPU-side mamba postprocess for spec decode + hybrid + align mode.
-
postprocess_mamba_all–All-mode postprocess (only meaningful with num_spec_tokens > 0):
-
postprocess_mamba_fused_kernel–Fused GPU kernel for postprocess_mamba that computes decisions AND performs
-
precopy_mamba_align_fused_kernel–Pre-copy mamba "align" state across block boundaries on the V2 runner.
-
preprocess_mamba–Copy the mamba state of previous step to the last
-
preprocess_mamba_align_fused_kernel–Fused align preprocess: emit the pre-copy src column/offset AND advance
-
stage_mamba_state_idx_to_gpu–Materialize
mamba_state_idxintogpu_bufand copy to GPU. -
stage_postprocess_inputs_to_gpu–Stage all per-request inputs the fused mamba postprocess kernel reads.
-
stage_postprocess_metadata_to_gpu–Stage per-request postprocess metadata into GPU buffers (non-blocking).
MambaBuffers dataclass ¶
Single owner for all mamba-specific runner buffers.
The two sub-objects have different gates: preprocess is needed whenever mamba_cache_mode == "align"; postprocess_align is needed only when align is combined with speculative decoding on a hybrid model, and is None otherwise.
Source code in vllm/v1/worker/mamba_utils.py
MambaSpecDecodeGPUContext dataclass ¶
Context for GPU-side Mamba state copy operations during the fused postprocess path.
Only used when speculative decoding is enabled on a hybrid model (and the mamba_cache_config is in align mode).
Precomputes memory layout metadata (base addresses, strides, element sizes) so the GPU kernel can perform state copies without CPU-GPU sync.
State types are distinguished by conv_width: >0 for conv states (sliding window with offset-based copies), 0 for temporal states (full block copies).
Methods:
-
create–Create context with allocated buffers (metadata populated later).
-
initialize_from_forward_context–Extract and cache memory layout metadata from Mamba state tensors.
-
run_fused_postprocess–Run the fused postprocess_mamba kernel on GPU.
-
run_fused_postprocess_align–V2 align postprocess: save the running state to the block-aligned
-
run_fused_precopy–Pre-copy each request's previous running block into its new window
Source code in vllm/v1/worker/mamba_utils.py
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 | |
create(max_num_reqs, kv_cache_config, num_state_types, device, make_buffer) classmethod ¶
Create context with allocated buffers (metadata populated later).
Source code in vllm/v1/worker/mamba_utils.py
initialize_from_forward_context(kv_cache_config, forward_context, mamba_state_copy_funcs, block_tables) ¶
Extract and cache memory layout metadata from Mamba state tensors.
This method populates the pre-allocated metadata tensors with information needed by postprocess_mamba_fused_kernel to perform state copies entirely on the GPU without CPU-GPU synchronization.
For each Mamba layer and state type, the following metadata is extracted: - state_base_addrs: GPU memory address (data_ptr) of the state tensor - state_block_strides: Bytes between consecutive blocks (stride * elem_size) - state_elem_sizes: Element size in bytes (e.g., 2 for float16) - state_inner_sizes: For conv states, elements per conv position (stride(1)), used to compute offset when slicing state[block, offset:]. For temporal states, this field is unused (set to 1). - state_conv_widths: Conv dimension size for conv states, 0 for temporal states
The conv vs temporal state type is detected by inspecting the copy function name: functions containing "conv" are treated as conv states.
This method is idempotent - it only executes once (guarded by is_initialized flag) since the metadata is static after model loading.
Parameters:
-
(kv_cache_config¶KVCacheConfig) –Configuration containing KV cache group info and layer name mappings.
-
(forward_context¶dict[str, Any]) –Dictionary mapping layer names to attention objects, populated after the model is loaded. Each attention object must have a
kv_cacheattribute containing the list of state tensors. -
(mamba_state_copy_funcs¶tuple[MambaStateCopyFunc, ...]) –Tuple of copy functions (one per state type) used to determine whether each state is a conv or temporal state.
-
(block_tables¶list[Tensor]) –per-mamba-group persistent block-table tensors, in the same order as
mamba_group_ids. Theirdata_ptr()/stride(0)are captured once for the kernel to index into.
Source code in vllm/v1/worker/mamba_utils.py
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 | |
run_fused_postprocess(num_reqs, num_accepted_tokens_gpu, mamba_state_idx_gpu, num_scheduled_tokens_gpu, num_computed_tokens_gpu, num_draft_tokens_gpu) ¶
Run the fused postprocess_mamba kernel on GPU.
This computes decisions and performs mamba state copies entirely on GPU, eliminating the CPU-GPU sync that was previously needed.
Parameters:
-
(num_reqs¶int) –Number of active requests
-
(num_accepted_tokens_gpu¶Tensor) –[num_reqs] accepted token counts
-
(mamba_state_idx_gpu¶Tensor) –[num_reqs] source block indices
-
(num_scheduled_tokens_gpu¶Tensor) –[num_reqs] scheduled token counts
-
(num_computed_tokens_gpu¶Tensor) –[num_reqs] computed token counts
-
(num_draft_tokens_gpu¶Tensor) –[num_reqs] draft token counts
Source code in vllm/v1/worker/mamba_utils.py
run_fused_postprocess_align(num_reqs, num_accepted_tokens_gpu, state_idx_gpu, new_num_computed_tokens_gpu, idx_mapping) ¶
V2 align postprocess: save the running state to the block-aligned position after spec-decode acceptance leaves the sequence non-aligned.
num_accepted_tokens_gpu is updated in place (reset to 1 when the accepted position stays in the running block); new_num_computed_tokens already holds the post-step computed count (PRECOMPUTED_NEW_COMPUTED). idx_mapping maps batch row -> req-state slot (HAS_IDX_MAPPING).
Source code in vllm/v1/worker/mamba_utils.py
run_fused_precopy(num_reqs, state_idx_gpu, src_col_gpu, token_bias_gpu, idx_mapping) ¶
Pre-copy each request's previous running block into its new window block before the forward pass (V2 align boundary migration).
Parameters:
-
(num_reqs¶int) –Number of active requests (batch order).
-
(state_idx_gpu¶Tensor) –[max_reqs] post-advance dst block column per req slot.
-
(src_col_gpu¶Tensor) –[max_reqs] pre-advance src block column (-1 = fresh).
-
(token_bias_gpu¶Tensor) –[max_reqs] accepted-token bias (num_accepted - 1).
-
(idx_mapping¶Tensor) –[num_reqs] batch_idx -> req_state_idx (-1 to skip).
Source code in vllm/v1/worker/mamba_utils.py
_copy_mamba_state_block(state_idx, bt_row_idx, src_col, dst_col, token_bias, block_table_ptrs_ptr, block_table_stride_req, state_base_addrs_ptr, state_block_strides_ptr, state_elem_sizes_ptr, state_inner_sizes_ptr, state_conv_widths_ptr, state_group_indices_ptr, state_dim_row_count_ptr, state_dim_row_stride_ptr, COPY_BLOCK_SIZE, CONV_STATE_DIM_FIRST) ¶
Copy one (layer, state-type) mamba state block between block columns.
Shared copy body of postprocess_mamba_fused_kernel and precopy_mamba_align_fused_kernel, mirroring the V1 copy specs (get_conv_copy_spec / get_temporal_copy_spec): - conv state (conv_width > 0): shift the window by token_bias tokens, state[bt[src_col], token_bias:] -> state[bt[dst_col], :conv_width - token_bias] - temporal state: token_bias selects the accepted speculative column, state[bt[src_col + token_bias]] -> state[bt[dst_col]]
The caller owns the decision logic (which columns, whether to copy); this device function only performs the byte copy for the given metadata slot.
Source code in vllm/v1/worker/mamba_utils.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 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 | |
cleanup_mamba_state_idx(scheduler_output, mamba_state_idx) ¶
Pop stale mamba_state_idx entries for finished/preempted/resumed reqs.
Force-preempted requests (e.g., during reset_prefix_cache / KV cache flush) appear in resumed_req_ids without a corresponding entry in preempted_req_ids, leaving stale entries that can point to block indices beyond the new (smaller) block allocation.
Source code in vllm/v1/worker/mamba_utils.py
postprocess_mamba_align_gpu(*, bufs, num_reqs, num_accepted_tokens_gpu, num_accepted_tokens_cpu_tensor, input_batch, kv_cache_config, forward_context, mamba_state_copy_funcs) ¶
GPU-side mamba postprocess for spec decode + hybrid + align mode.
Lazily binds the fused-kernel context to the persistent block tables and forward-context state pointers on the first call, runs the fused kernel, and async-copies the per-request accepted-token counts back to the input batch's CPU tensor for the next iteration's preprocess.
Source code in vllm/v1/worker/mamba_utils.py
postprocess_mamba_all(scheduler_output, kv_cache_config, input_batch, requests, mamba_state_idx, num_spec_tokens, num_reqs) ¶
All-mode postprocess (only meaningful with num_spec_tokens > 0): record per-request the block index of the last token scheduled this step, so the next step can anchor its in-place writes when accepted drafts leave the sequence at a non-block-aligned position.
Source code in vllm/v1/worker/mamba_utils.py
postprocess_mamba_fused_kernel(num_accepted_tokens_ptr, mamba_state_idx_ptr, num_scheduled_tokens_ptr, num_computed_tokens_ptr, num_draft_tokens_ptr, block_table_ptrs_ptr, block_table_stride_req, state_base_addrs_ptr, state_block_strides_ptr, state_elem_sizes_ptr, state_inner_sizes_ptr, state_conv_widths_ptr, state_group_indices_ptr, state_dim_row_count_ptr, state_dim_row_stride_ptr, num_accepted_tokens_out_ptr, idx_mapping_ptr, num_reqs, block_size, COPY_BLOCK_SIZE, CONV_STATE_DIM_FIRST, HAS_IDX_MAPPING=False, PRECOMPUTED_NEW_COMPUTED=False) ¶
Fused GPU kernel for postprocess_mamba that computes decisions AND performs mamba state copies without any CPU-GPU synchronization.
Grid: (num_reqs, num_layers * num_state_types) - program_id(0) = request/batch index - program_id(1) = state_idx (flattened index into layer/state_type metadata)
Note: num_layers and num_state_types are not passed as kernel parameters because the kernel indexes directly into pre-flattened metadata arrays using program_id(1). The grid dimensions encode the total state count.
Source code in vllm/v1/worker/mamba_utils.py
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 | |
precopy_mamba_align_fused_kernel(mamba_state_idx_ptr, src_col_ptr, token_bias_ptr, block_table_ptrs_ptr, block_table_stride_req, state_base_addrs_ptr, state_block_strides_ptr, state_elem_sizes_ptr, state_inner_sizes_ptr, state_conv_widths_ptr, state_group_indices_ptr, state_dim_row_count_ptr, state_dim_row_stride_ptr, idx_mapping_ptr, num_reqs, COPY_BLOCK_SIZE, CONV_STATE_DIM_FIRST) ¶
Pre-copy mamba "align" state across block boundaries on the V2 runner.
Before the forward pass, copy each request's last SSM/conv state from its previous block column into the new window block column, so the kernels read the initial state from the write-side block as usual (V1 align semantics). Same per-(layer, state) copy semantics as postprocess_mamba_fused_kernel (shared _copy_mamba_state_block body, i.e. the V1 preprocess_mamba copy specs), but driven by the GPU-resident src columns so it needs no CPU-GPU sync (async-scheduling safe).
Grid: (num_reqs, num_layers * num_state_types); block tables are indexed by batch row, per-request state by req_idx via idx_mapping (V2 layout).
Source code in vllm/v1/worker/mamba_utils.py
preprocess_mamba(scheduler_output, kv_cache_config, cache_config, mamba_state_idx, input_batch, requests, forward_context, mamba_state_copy_funcs, copy_bufs) ¶
Copy the mamba state of previous step to the last (1 + num_speculative_blocks) block.
Source code in vllm/v1/worker/mamba_utils.py
preprocess_mamba_align_fused_kernel(idx_mapping_ptr, state_idx_ptr, num_computed_tokens_ptr, query_start_loc_ptr, num_accepted_tokens_ptr, src_col_ptr, src_off_ptr, num_reqs, BLOCK_SIZE, MAMBA_BLOCK_SIZE) ¶
Fused align preprocess: emit the pre-copy src column/offset AND advance state_idx (with accepted-token reset) in a single launch (V2 align).
Per batch_idx (0..num_reqs-1), resolving req slot via idx_mapping: 1. Read pre-advance state_idx and num_accepted (last step's values). 2. Store the pre-copy src columns for precopy_mamba_align_fused_kernel: - src_col = state_idx (the previous running block column) - src_off = max(num_accepted - 1, 0) (the accepted-token bias) 3. Advance state_idx to the new running block, and reset num_accepted to 1 when a block boundary is crossed (so the migrated state, now at the start of the new block, is read with the neutral bias).
Source code in vllm/v1/worker/mamba_utils.py
stage_mamba_state_idx_to_gpu(mamba_state_idx, req_ids, num_reqs, gpu_buf) ¶
Materialize mamba_state_idx into gpu_buf and copy to GPU.
Walks req_ids[:num_reqs] in batch order, writing each request's block index into the buffer's pinned numpy view, then issues a non-blocking H→D copy. The fused kernel indexes the resulting GPU tensor by req_idx.
Invariant: preprocess_mamba must have run first for the same batch so that every req_ids[i] has an entry in mamba_state_idx.
Source code in vllm/v1/worker/mamba_utils.py
stage_postprocess_inputs_to_gpu(ctx, scheduler_output, req_ids, num_reqs, requests, mamba_state_idx) ¶
Stage all per-request inputs the fused mamba postprocess kernel reads.
Bundles stage_mamba_state_idx_to_gpu and stage_postprocess_metadata_to_gpu into a single call so the runner has one entry point for postprocess staging. Buffers live on ctx and only exist when the postprocess kernel is enabled.
Source code in vllm/v1/worker/mamba_utils.py
stage_postprocess_metadata_to_gpu(scheduler_output, req_ids, num_reqs, requests, num_scheduled_tokens_buf, num_computed_tokens_buf, num_draft_tokens_buf) ¶
Stage per-request postprocess metadata into GPU buffers (non-blocking).
Walks req_ids[:num_reqs] in batch order and writes each request's scheduled/computed/draft token counts into the matching pinned numpy views, then issues three non-blocking H→D copies. These values don't change between _prepare_inputs and _update_states_after_model_execute. The fused postprocess kernel indexes the resulting GPU tensors by req_idx.