🚨 Make the causal conv1d, Mamba scan and Mamba2 SSD of linear attention layers kernelizable (#49186)
* Add a linear attention interface, starting with Mamba2's SSD core
Linear attention layers now dispatch only their stateful core (causal
convolution, recurrence and recurrent state handling) through a
per-mechanism interface selected by `config._linear_attn_implementation`.
Projections, gating and the output projection stay in the modeling file.
This lets runtimes that manage recurrent state themselves (e.g. vLLM's
Transformers modeling backend) swap the core without re-implementing the
layer.
- Add `LinearAttentionInterface` and `ALL_SSD_FUNCTIONS` for Mamba2
- Add `linear_attn_implementation` to configs and `from_pretrained`
- Move `Mamba2Mixer`'s conv + scan into `eager_ssd_forward`
- Propagate to Bamba, Zamba2, GraniteMoeHybrid, NemotronH and FalconH1
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
* small cleanup
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
* fix double attr
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
* Use a single linear attention registry keyed by mechanism
Replace the per-mechanism interface classes with one
`ALL_LINEAR_ATTENTION_FUNCTIONS`, whose keys are
`"<implementation>|<mechanism>"` (e.g. `"vllm|ssd"`). Backends register
in one place, validation reads one registry and the mechanism is a
string literal that modular conversion cannot rename.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
* Revert to one linear attention registry per mechanism
This reverts commit d09dc863bb. One registry per contract (e.g.
`ALL_SSD_FUNCTIONS`) matches `AttentionInterface` and
`ExpertsInterface` and keeps each contract's docstring next to its
registry, instead of introducing a two-part key convention.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
* Split the Mamba2 SSD interface into conv and scan interfaces
Dispatch the two stateful pieces of the Mamba2 mixer separately:
`CausalConv1dInterface` owns the conv state and `SSDInterface` owns the
recurrent state. Projections, masking, gated norm and output projection
stay in the modeling file. `use_precomputed_states` is computed once
before the convolution, since the convolution marks the cache as having
a previous state.
Document that layers must read their sizes from module attributes at
forward time, so runtimes can shard them for tensor parallelism.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Share the causal conv1d of linear attention layers in modeling_linear_attn_utils
Every linear attention model had the same copy of the causal_conv1d kernels and of the
code that runs them against the conv state in the cache. Move both to
modeling_linear_attn_utils.causal_conv1d_forward and use it in all 23 models. Kernel widths
the causal_conv1d kernels don't support fall back to PyTorch, which replaces LFM3's
PyTorch-only override.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
* Share the Mamba2 SSD and make the shared steps kernelizable
Move the SSD step of the Mamba2 family (recurrent vs chunk scan and the recurrent state
handling) and its kernels to modeling_linear_attn_utils.ssd_forward. Decorate
causal_conv1d_forward and ssd_forward with use_kernel_forward_from_hub and list them in
each layer's use_kernelized_func, so kernelize can replace the whole stateful step.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
* Inline the shared linear attention steps with modular
Move the causal conv1d into modeling_mamba and the SSD into modeling_mamba2,
and have the other models import them in their modular files, so that each
modeling file contains its own copy. Add modular files for Mamba2 and Zamba,
which reuse the Mamba conv.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
* Make the Mamba selective scan kernelizable
Move the scan of the Mamba mixer, which reads and updates the recurrent
state in the cache, into selective_scan_forward, which kernelize can
replace. FalconMamba and Jamba inherit it with modular. Zamba scans each
of its Mamba heads separately, so it keeps calling the kernels directly.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
* Drop the per-width torch fallback of the shared causal conv1d
Restore the torch code into the decorated causal_conv1d_update and
causal_conv1d_fn, as on main, and keep LFM3's width 7 torch-only override in
its modular file. Lfm3ShortConv still lists causal_conv1d_forward in
use_kernelized_func so kernelize can replace it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
* Serve LFM3's conv with mamba-ssm v5 and hide runtime-only mapping warnings
Version 5 of the kernels-community/mamba-ssm causal conv1d serves widths up
to 8 in half precision, so LFM3 no longer needs its torch-only override.
Its kernels test is skipped, as the kernels still serve only widths 2 to 4
in float32.
causal_conv1d_forward, ssd_forward and selective_scan_forward are for
runtimes that bring their own kernels, so hide the warnings kernelize logs
for having no Hub mapping for them.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
* Fix CI after merging main
Kernelize LaraMini's causal_conv1d_forward, which its gated DeltaNet
inherits, drop the _skip_keys_device_placement that main removed from the
Zamba modular file, and shorten the GPT-2 init comment in the Mamba2
modular file.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
* Address review: shared kernels log suppression, LFM3 kernels test, OlmoHybrid style
- Hide the runtime-only mapping warnings with `suppress_kernels_logging`, copied
from #47773
- Unskip LFM3's kernels test, main now serves its conv with the Hub kernels
- Use if/else for OlmoHybrid's recurrent state, like the other models
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
* Address review: keep `mamba_`/`mamba2_` names and reuse Mamba via modular
- Revert the `ssd_*` renames to the original `mamba2_selective_state_update`
and `mamba2_chunk_scan`, and name the new seams `mamba_scan_forward` and
`mamba2_scan_forward`, which the converter never renames
- Mamba2 (derived from Mamba) inherits its norm, block, pretrained model,
model and causal LM classes from Mamba, overriding only what differs
- Zamba (a Mamba backbone with shared attention) takes its norm and selective
scan helpers from Mamba and `eager_attention_forward` from Llama
`apply_mask_to_padding_states` stays local, as modular would rewrite the
`state-spaces/mamba` link in its docstring.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
* Use the generic sequence classification head for Zamba and Zamba2
Zamba's head was a hand-copied version of `GenericForSequenceClassification`,
so it now inherits it via Mistral. Zamba2 keeps its own `forward` and drops the
`__init__` that only swapped in `Zamba2Model`, which `AutoModel` now resolves.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
* Take Zamba's sequence classification head from Llama
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
* Address review: forward kwargs to causal_conv1d_forward, simplify suppress_kernels_logging
- Inkling, Inkling2 and Zamba pass `**kwargs` to `causal_conv1d_forward` like every other caller
- `suppress_kernels_logging` takes `layers` as required and uses a callable filter
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Address review: read the recurrent state in the scans, modernise Mamba2Model
- `mamba_scan_forward` and `mamba2_scan_forward` read the recurrent state from `cache` themselves, like
`causal_conv1d_forward` does for the conv state
- Restore `suppress_kernels_logging` as in #47773
- Rename `MambaRMSNormGated` to `Mamba2RMSNormGated`; the gated norm is part of the final projection step
- `Mamba2Model` uses `merge_with_config_defaults` and `capture_outputs`, `Mamba2ForCausalLM` uses `can_return_tuple`
- Seam docstrings: "The mixer", no `seq_idx` example
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
* Address review: inherit more of Zamba
- `ZambaAttention` inherits from `JambaAttention`
- `ZambaForCausalLM` inherits from `LlamaForCausalLM`, without a TP plan as before (its head is tied to the
unsharded embedding)
- Import `apply_mask_to_padding_states` from Mamba2
- `ZambaHybridLayer` concatenates the embeddings before calling the shared attention block
- Support flex attention and the attention backend interface
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
* Drop FalconMamba's unused A after upstream removed its fused path
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
* Mark Zamba's per-call layer_idx as a diff and drop its TP plan via AttributeError
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
* Regenerate Zamba2 and NemotronH after dropping Zamba's TP plan
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
---------
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>