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.
-
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_postprocess_inputs_to_gpu–Stage all per-request inputs the fused mamba postprocess kernel reads.
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
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 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 | |
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
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 while the kernel reads from a snapshot to avoid cross-program races when the accepted position stays in the running block and the count is reset to 1. 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 (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 | None) –optional [num_reqs] batch_idx -> req_state_idx. None means V1 batch order already equals request state order.
Source code in vllm/v1/worker/mamba_utils.py
_FusedPrecopy ¶
Bases: NamedTuple
Resolved fused align pre-copy resources (all non-None once resolved).
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, tile_idx, COPY_BLOCK_SIZE, CONV_STATE_DIM_FIRST, TEMPORAL_TILES) ¶
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.
tile_idx in [0, TEMPORAL_TILES) partitions the temporal state's u64 range into TEMPORAL_TILES contiguous, COPY_BLOCK_SIZE-aligned slices, giving more CTAs to fill the SMs at small batch (multi-MiB temporal copies otherwise leave the GPU under-filled). Conv states are small; only tile_idx == 0 copies them. TEMPORAL_TILES == 1 and tile_idx == 0 reproduces the untiled behavior.
Source code in vllm/v1/worker/mamba_utils.py
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 | |
_memcpy_u64_tiled(src_addr, dst_addr, copy_size, tile_idx, COPY_BLOCK_SIZE, NUM_TILES) ¶
Head/body/tail memcpy with the u64 body split across NUM_TILES CTAs.
Fast path (src and dst share sub-8B alignment): tile 0 owns the byte head that lifts dst to 8B and the 0-7 byte tail; body tiles vectorize as u64 over the aligned interior. NUM_TILES=1 collapses to a single- CTA memcpy. Production callers derive both addresses from the same state_base_addr + block_id * stride and always take this path.
Slow path (mismatched sub-8B alignment): byte-wide tiled copy. Some NVIDIA parts (e.g. GB200) reject misaligned u64 loads with cudaErrorMisalignedAddress instead of accepting the misaligned-sector cost, so we can't just let the fast path run with a misaligned src.
Source code in vllm/v1/worker/mamba_utils.py
_reinterpret_u64_as_i64(value) ¶
_resolve_fused_precopy(align_ctx) ¶
Bundle the fused-path buffers, or None for the scalar path.
Returning one non-None bundle lets callers narrow all four members with a single is not None check instead of re-asserting each buffer per use.
Source code in vllm/v1/worker/mamba_utils.py
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, TEMPORAL_TILES=1) ¶
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 [, TEMPORAL_TILES]) - program_id(0) = request/batch index - program_id(1) = state_idx (flattened index into layer/state_type metadata) - program_id(2) = temporal-copy tile index (0 when TEMPORAL_TILES == 1)
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
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 | |
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, HAS_IDX_MAPPING=True, TEMPORAL_TILES=1) ¶
Pre-copy mamba "align" state across block boundaries.
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 [, TEMPORAL_TILES]). V2 passes a batch-to-state idx_mapping; V1 already stores the staged arrays in batch order and uses HAS_IDX_MAPPING=False.
Source code in vllm/v1/worker/mamba_utils.py
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 | |
preprocess_mamba(scheduler_output, kv_cache_config, cache_config, mamba_state_idx, input_batch, requests, forward_context, mamba_state_copy_funcs, copy_bufs, align_ctx=None) ¶
Copy the mamba state of previous step to the last (1 + num_speculative_blocks) block.
Source code in vllm/v1/worker/mamba_utils.py
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 | |
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_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.
Walks req_ids[:num_reqs] once, writing each request's mamba block index and scheduled/computed/draft token counts into the matching pinned numpy views, then issues four non-blocking H→D copies. The fused kernel indexes the resulting GPU tensors by req_idx. Buffers live on ctx and only exist when the postprocess kernel is enabled.
Invariant: preprocess_mamba must have run first for the same batch so that every req_ids[i] has an entry in mamba_state_idx.