ggml_ssm_scan: Selective State-Space Scan (Mamba / Mamba-2)

View source: R/operations.R

ggml_ssm_scanR Documentation

Selective State-Space Scan (Mamba / Mamba-2)

Description

The recurrent core of a Mamba block: a linear state-space recurrence whose transition is modulated per token by dt, B and C. ids selects which state row each sequence continues from, so several sequences can share one state buffer.

Usage

ggml_ssm_scan(ctx, s, x, dt, A, B, C, ids)

Arguments

ctx

GGML context

s

Initial states, [d_state, head_dim, n_head, n_seqs_total]

x

Inputs, [head_dim, n_head, n_seq_tokens, n_seqs]

dt

Per-token timestep, [n_head, n_seq_tokens, n_seqs]

A

State decay, [1 or d_state, n_head]

B

Input projection, [d_state, n_group, n_seq_tokens, n_seqs]

C

Output projection, same shape as B

ids

Sequence-to-state mapping, [n_seqs] I32, 0-based

Value

A flat tensor: outputs followed by final states

Shapes

With d_state the state width, head_dim the per-head channel count, n_head heads, n_seq_tokens tokens and n_seqs sequences:

  • s — [d_state, head_dim, n_head, n_seqs_total], contiguous

  • x — [head_dim, n_head, n_seq_tokens, n_seqs]

  • dt — [n_head, n_seq_tokens, n_seqs], contiguous

  • A — [1, n_head] (Mamba-2) or [d_state, n_head] (Mamba-1)

  • B, C — [d_state, n_group, n_seq_tokens, n_seqs], same shape

  • ids — [n_seqs], type GGML_TYPE_I32, 0-based

Packed result

The result is a flat tensor holding the outputs followed by the final states: ggml_nelements(x) output elements, then d_state * head_dim * n_head * n_seqs state elements. Use ggml_ssm_scan_output and ggml_ssm_scan_state to view either half rather than computing the offsets by hand – getting them wrong silently reads the state as output.

Inference only

GGML_OP_SSM_SCAN has no backward pass in ggml. A graph containing it runs forward on CPU and Vulkan, but cannot be trained: ggml_fit() and the autograd engine will not produce gradients for it.

See Also

ggml_ssm_scan_output, ggml_ssm_scan_state, ggml_ssm_conv

Other state-space: ggml_gated_linear_attn(), ggml_gated_linear_attn_back(), ggml_rwkv_output(), ggml_rwkv_wkv6(), ggml_rwkv_wkv6_back(), ggml_rwkv_wkv7(), ggml_rwkv_wkv7_back(), ggml_ssm_conv(), ggml_ssm_conv_back(), ggml_ssm_scan_back(), ggml_ssm_scan_output()


ggmlR documentation built on Oct. 23, 2026, 1:07 a.m.