vllm.model_executor.layers.mamba.ops.mamba_ssm ¶
_canonical_cache_dtype ¶
Canonical key for config lookup. bf16 and fp16 share the same tuned configs because the kernel only sees bit width when accessing state.
Source code in vllm/model_executor/layers/mamba/ops/mamba_ssm.py
_get_default_ssm_launch_config ¶
Hard-coded fallback heuristic used when no tuned config is available.
Source code in vllm/model_executor/layers/mamba/ops/mamba_ssm.py
_try_get_optimal_ssm_config_cached cached ¶
_try_get_optimal_ssm_config_cached(
headdim: int,
dstate: int,
batch: int,
nheads: int,
cache_dtype: str,
is_blackwell: bool,
) -> tuple[int, int]
Cached resolution. See :func:try_get_optimal_ssm_config.
Source code in vllm/model_executor/layers/mamba/ops/mamba_ssm.py
get_ssm_config_file_name ¶
Return the JSON filename for the given kernel shape.
Layout: configs/selective_state_update/ headdim=<H>,dstate=<D>,device_name=<dev>,cache_dtype=<dt>.json.
Source code in vllm/model_executor/layers/mamba/ops/mamba_ssm.py
get_ssm_configs cached ¶
Return tuned (BLOCK_SIZE_M, num_warps) configs for selective_state_update keyed by effective_batch = batch * nheads, or None if no config file is found for the (headdim, dstate, cache_dtype, device) combination.
They can be generated with
benchmarks/kernels/benchmark_selective_state_update.py --save-configs
Source code in vllm/model_executor/layers/mamba/ops/mamba_ssm.py
override_ssm_config ¶
Pin try_get_optimal_ssm_config to config for the duration of the context. Used by the tuning benchmark to time specific configs.
Source code in vllm/model_executor/layers/mamba/ops/mamba_ssm.py
selective_scan_fn ¶
selective_scan_fn(
u,
ssm_states,
delta,
A,
B,
C,
D=None,
z=None,
delta_bias=None,
delta_softplus=False,
query_start_loc=None,
cache_indices=None,
has_initial_state=None,
null_block_id=NULL_BLOCK_ID,
block_size=1024,
block_idx_first_scheduled_token=None,
block_idx_last_scheduled_token=None,
initial_state_idx=None,
cu_chunk_seqlen=None,
last_chunk_indices=None,
) -> Tensor
(dim, total_length) for varlen or (batch, dim, seqlen)
applies changes in place.
ssm_states: (batch, dim, dstate) or (batch, nheads, dim, dstate) applies changes in place. delta: (dim, total_length) for varlen or (batch, dim, seqlen) A: (dim, dstate) B: (ngroups, dstate, total_length) for varlen or (batch,ngroups,dstate,seqlen) C: (ngroups, dstate, total_length) for varlen or (batch,ngroups,dstate,seqlen) D: (dim,) z: (dim, total_length) for varlen or (batch, dim, seqlen) dt_bias: (dim,) or (dim) query_start_loc: (batch + 1) int32 The cumulative sequence lengths of the sequences in the batch, used to index into sequence. prepended with 0. for example: query_start_loc = torch.Tensor([0,10,16,17]), x.shape=(dim,17) cache_indices: (batch) int32 A tensor with each cell is a correspondent input and output ssm_state indices - Without APC: (batch,) - single state index per batch item - With APC: (batch, max_positions) - cache block indices for read/write Each non-zero value indicates a cache block to load from and/or write to. has_initial_state: (batch) bool A tensor populated with ones and zeros, indicate if the ssm_state at the corresponding index should be used as initial state. Not providing argument assumes there's no initial state null_block_id: int if cache_indices is passed, lets the kernel identify padding entries that will not be processed, for example: cache_indices = [null_block_id, 1 ,20 ,null_block_id] in this case, the kernel will not process entries at indices 0 and 3 block_size: int The block size to align the cached states to block_idx_first_scheduled_token: (batch,), dtype int32 The pointer into cache_indices, where the first cache block to be filled is located. block_idx_last_scheduled_token: (batch,), dtype int32 The pointer into cache_indices, where the last cache block to be filled is located. initial_state_idx: (batch,), dtype int32 The pointer into cache_indices, where the cache block containing the initial state is located. returns output: (dim, total_length) for varlen or (batch, dim, seqlen) supports inplace replacement
Source code in vllm/model_executor/layers/mamba/ops/mamba_ssm.py
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 | |
selective_state_update ¶
selective_state_update(
state,
x,
dt,
A,
B,
C,
D,
dt_bias,
z=None,
dt_softplus=False,
state_batch_indices=None,
dst_state_batch_indices=None,
null_block_id=NULL_BLOCK_ID,
out=None,
num_accepted_tokens=None,
cu_seqlens=None,
is_blackwell=False,
enable_stochastic_rounding=False,
cache_philox_rounds=0,
)
Argument
state: (batch, dim, dstate) or (batch, nheads, dim, dstate) x: (batch, dim) or (batch, nheads, dim) dt: (batch, dim) or (batch, nheads, dim) A: (dim, dstate) or (nheads, dim, dstate) B: (batch, dstate) or (batch, ngroups, dstate) C: (batch, dstate) or (batch, ngroups, dstate) D: (dim,) or (nheads, dim) z: (batch, dim) or (batch, nheads, dim) dt_bias: (dim,) or (nheads, dim) null_block_id: int if state_batch_indices is passed, lets the kernel identify padded entries that will not be processed, for example: state_batch_indices = [null_block_id, 1, 20, null_block_id] in this case, the kernel will not process entries at indices 0 and 3 out: Preallocated ssm output tensor. Assume same shape as x. In-place updated. num_accepted_tokens: (batch,) number of accepted tokens from previous verification step, tells the kernel which initial state to use cu_seqlens: (batch,) length per sequence, for variable length in speculative decoding cases
Source code in vllm/model_executor/layers/mamba/ops/mamba_ssm.py
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 | |
try_get_optimal_ssm_config ¶
try_get_optimal_ssm_config(
headdim: int,
dstate: int,
batch: int,
nheads: int,
cache_dtype: str,
is_blackwell: bool,
) -> tuple[int, int]
Return (BLOCK_SIZE_M, num_warps) for the given kernel shape.
Tuning is keyed on effective_batch = batch * nheads (the kernel grid scales with the product), so configs transfer across (model, TP) combos sharing (headdim, dstate, cache_dtype).