vllm.v1.worker.mamba_utils ¶
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).
Source code in vllm/v1/worker/mamba_utils.py
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 | |
create classmethod ¶
create(
max_num_reqs: int,
kv_cache_config: KVCacheConfig,
num_state_types: int,
device: device,
make_buffer: Callable[..., CpuGpuBuffer],
) -> MambaSpecDecodeGPUContext
Create context with allocated buffers (metadata populated later).
Source code in vllm/v1/worker/mamba_utils.py
initialize_from_forward_context ¶
initialize_from_forward_context(
kv_cache_config: KVCacheConfig,
forward_context: dict[str, Any],
mamba_state_copy_funcs: tuple[MambaStateCopyFunc, ...],
block_tables: list[Tensor],
) -> None
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:
| Name | Type | Description | Default |
|---|---|---|---|
kv_cache_config | KVCacheConfig | Configuration containing KV cache group info and layer name mappings. | required |
forward_context | dict[str, Any] | Dictionary mapping layer names to attention objects, populated after the model is loaded. Each attention object must have a | required |
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. | required |
block_tables | list[Tensor] | per-mamba-group persistent block-table tensors, in the same order as | required |
Source code in vllm/v1/worker/mamba_utils.py
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 | |
run_fused_postprocess ¶
run_fused_postprocess(
num_reqs: int,
num_accepted_tokens_gpu: Tensor,
mamba_state_idx_gpu: Tensor,
num_scheduled_tokens_gpu: Tensor,
num_computed_tokens_gpu: Tensor,
num_draft_tokens_gpu: Tensor,
) -> None
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:
| Name | Type | Description | Default |
|---|---|---|---|
num_reqs | int | Number of active requests | required |
num_accepted_tokens_gpu | Tensor | [num_reqs] accepted token counts | required |
mamba_state_idx_gpu | Tensor | [num_reqs] source block indices | required |
num_scheduled_tokens_gpu | Tensor | [num_reqs] scheduled token counts | required |
num_computed_tokens_gpu | Tensor | [num_reqs] computed token counts | required |
num_draft_tokens_gpu | Tensor | [num_reqs] draft token counts | required |
Source code in vllm/v1/worker/mamba_utils.py
cleanup_mamba_state_idx ¶
cleanup_mamba_state_idx(
scheduler_output: SchedulerOutput,
mamba_state_idx: dict[str, int],
) -> None
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 ¶
postprocess_mamba_align_gpu(
*,
bufs: MambaBuffers,
num_reqs: int,
num_accepted_tokens_gpu: Tensor,
num_accepted_tokens_cpu_tensor: Tensor,
input_batch: GPUInputBatch,
kv_cache_config: KVCacheConfig,
forward_context: dict[str, Any],
mamba_state_copy_funcs: tuple[MambaStateCopyFunc, ...],
) -> None
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 ¶
postprocess_mamba_all(
scheduler_output: SchedulerOutput,
kv_cache_config: KVCacheConfig,
input_batch: GPUInputBatch,
requests: dict[str, CachedRequestState],
mamba_state_idx: dict[str, int],
num_spec_tokens: int,
num_reqs: int,
)
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 ¶
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: int64,
state_base_addrs_ptr,
state_block_strides_ptr,
state_elem_sizes_ptr,
state_inner_sizes_ptr,
state_conv_widths_ptr,
state_group_indices_ptr,
num_accepted_tokens_out_ptr,
num_reqs,
block_size: constexpr,
COPY_BLOCK_SIZE: constexpr,
)
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 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
25 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 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 | |
preprocess_mamba ¶
preprocess_mamba(
scheduler_output: SchedulerOutput,
kv_cache_config: KVCacheConfig,
cache_config: CacheConfig,
mamba_state_idx: dict[str, int],
input_batch: GPUInputBatch,
requests: dict[str, CachedRequestState],
forward_context: dict[str, Any],
mamba_state_copy_funcs: tuple[MambaStateCopyFunc, ...],
copy_bufs: MambaCopyBuffers,
)
Copy the mamba state of previous step to the last (1 + num_speculative_blocks) block.
Source code in vllm/v1/worker/mamba_utils.py
stage_mamba_state_idx_to_gpu ¶
stage_mamba_state_idx_to_gpu(
mamba_state_idx: dict[str, int],
req_ids: list[str],
num_reqs: int,
gpu_buf: CpuGpuBuffer,
) -> None
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 ¶
stage_postprocess_inputs_to_gpu(
ctx: MambaSpecDecodeGPUContext,
scheduler_output: SchedulerOutput,
req_ids: list[str],
num_reqs: int,
requests: dict[str, CachedRequestState],
mamba_state_idx: dict[str, int],
) -> None
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 ¶
stage_postprocess_metadata_to_gpu(
scheduler_output: SchedulerOutput,
req_ids: list[str],
num_reqs: int,
requests: dict[str, CachedRequestState],
num_scheduled_tokens_buf: CpuGpuBuffer,
num_computed_tokens_buf: CpuGpuBuffer,
num_draft_tokens_buf: CpuGpuBuffer,
) -> None
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.