Skip to content

vllm.model_executor.models.zamba2

PyTorch Zamba2 model implementation for vLLM.

This module implements the Zamba2 architecture from https://arxiv.org/abs/2411.15242, which combines Mamba and Transformer architectures in a hybrid model optimized for efficient sequence modeling. The model alternates between state space model layers and attention-based layers.

Classes:

Zamba2Attention

Bases: Module

Multi-head attention mechanism for the Zamba2 model.

Implements attention with parallel computation, QKV projections, optional adapters and rotary position embeddings. The attention is computed across distributed blocks for efficient processing.

Methods:

  • __init__ –

    Initialize the attention layer.

  • forward –

    Forward pass through the attention layer.

__init__(config, bare_block_idx, num_hybrid_layers, cache_config=None, quant_config=None, prefix='')

Initialize the attention layer.

Parameters:

  • config

    (Zamba2Config) –

    The Zamba2 model configuration

  • bare_block_idx

    (int) –

    Index of the bare attention block

  • num_hybrid_layers

    (int) –

    Total number of hybrid layers

  • cache_config

    (CacheConfig | None, default: None ) –

    Configuration for key-value caching

  • quant_config

    (QuantizationConfig | None, default: None ) –

    Configuration for model quantization

  • prefix

    (str, default: '' ) –

    Optional prefix for parameter names

forward(hidden_states, block_idx, position_ids)

Forward pass through the attention layer.

Parameters:

  • hidden_states

    (Tensor) –

    Input tensor [batch_size, seq_len, hidden_size]

  • position_ids

    (Tensor) –

    Position IDs for positional embeddings

  • block_idx

    (int) –

    Current shared transformer block index

Returns:

  • Tensor –

    Output tensor [batch_size, seq_len, hidden_size]

Zamba2AttentionDecoderLayer

Bases: Module

Single decoder layer combining attention and feed-forward networks.

This layer implements a standard transformer block with: - Input layer normalization - Multi-head self-attention - Pre-feed-forward layer normalization - Feed-forward network (MLP)

Methods:

  • __init__ –

    Initialize the decoder layer.

  • forward –

    Forward pass through the decoder layer.

__init__(config, bare_block_idx, num_hybrid_layers, cache_config=None, quant_config=None, prefix='')

Initialize the decoder layer.

Parameters:

  • config

    (Zamba2Config) –

    The Zamba2 model configuration

  • bare_block_idx

    (int) –

    Index of the bare block

  • num_hybrid_layers

    (int) –

    Total number of hybrid layers

  • cache_config

    (CacheConfig | None, default: None ) –

    Configuration for key-value caching

  • quant_config

    (QuantizationConfig | None, default: None ) –

    Configuration for model quantization

  • prefix

    (str, default: '' ) –

    Optional prefix for parameter names

forward(hidden_states, original_hidden_states, block_idx, positions)

Forward pass through the decoder layer.

Parameters:

  • hidden_states

    (Tensor) –

    Input tensor from previous layer

  • original_hidden_states

    (Tensor) –

    Original input tensor for residual connection

  • block_idx

    (int) –

    Current shared transformer block index

  • positions

    (Tensor) –

    IDs for positional embeddings

Returns:

  • Tensor –

    Transformed hidden states after attention and feed-forward

Zamba2ForCausalLM

Bases: Module, HasInnerState, IsHybrid

Zamba2 model with causal language modeling head.

This class wraps the core Zamba2 model and adds: - A language modeling head for next token prediction - Mamba state caching functionality - Support for model parallelism and quantization - Sampling capabilities for text generation

Methods:

__init__(*, vllm_config, prefix='')

Initialize the Zamba2 model for causal language modeling.

Parameters:

  • vllm_config

    (VllmConfig) –

    Configuration containing model, cache, quantization, LoRA and scheduler settings

  • prefix

    (str, default: '' ) –

    Optional prefix for parameter names

compute_logits(hidden_states)

Compute logits for next token prediction.

Parameters:

  • hidden_states

    (Tensor) –

    Hidden states from model forward pass

Returns:

  • Tensor | None –

    Logits for next token prediction

embed_input_ids(input_ids)

Convert input token IDs to embeddings.

Parameters:

  • input_ids

    (Tensor) –

    Tensor of input token IDs

Returns: Embedded representation of the input tokens

forward(input_ids, positions, inputs_embeds=None, **kwargs)

Forward pass through the model.

Parameters:

  • input_ids

    (Tensor | None) –

    Input token IDs

  • positions

    (Tensor) –

    Position IDs for embeddings

  • inputs_embeds

    (Tensor | None, default: None ) –

    Optional pre-computed input embeddings

  • **kwargs

    (Any, default: {} ) –

    Additional arguments passed to cache manager

Returns:

  • Tensor –

    Output hidden states

get_mamba_state_shape_from_config(vllm_config) classmethod

Calculate shapes for Mamba's convolutional and state caches.

Parameters:

Returns:

Zamba2HybridLayer

Bases: Module

Hybrid layer combining Transformer and Mamba architectures.

This layer implements the hybrid architecture described in the Zamba paper, where a shared transformer pathway processes input in parallel with a Mamba pathway. The transformer output is projected and added to the Mamba input for enhanced representation learning.

Methods:

  • __init__ –

    Initialize the hybrid layer.

  • forward –

    Forward pass through the hybrid layer.

__init__(shared_transformer, config, block_idx, model_config=None, cache_config=None, quant_config=None, prefix='')

Initialize the hybrid layer.

Parameters:

  • shared_transformer

    (Zamba2AttentionDecoderLayer) –

    Transformer decoder layer for attention pathway

  • config

    (Zamba2Config) –

    The Zamba2 model configuration

  • block_idx

    (int) –

    Index of this hybrid block in the model

  • model_config

    (ModelConfig | None, default: None ) –

    The model config, when available

  • cache_config

    (CacheConfig | None, default: None ) –

    The KV cache config, when available

  • quant_config

    (QuantizationConfig | None, default: None ) –

    Configuration for model quantization

  • prefix

    (str, default: '' ) –

    Module prefix used for quantization config lookup

forward(hidden_states, original_hidden_states, positions)

Forward pass through the hybrid layer.

Processes input through parallel transformer and Mamba paths: 1. Transformer path processes input with attention 2. Transformer output is projected to match hidden size 3. Projected output is added to Mamba path input 4. Final output combines both paths' representations

Parameters:

  • hidden_states

    (Tensor) –

    Input tensor [batch_size, seq_len, hidden_size]

  • original_hidden_states

    (Tensor) –

    Original input for transformer residual connection

  • positions

    (Tensor) –

    Position IDs for positional embeddings

Returns:

  • Tensor –

    Output tensor combining transformer and Mamba representations

Zamba2LoRA

Bases: Module

LoRA layer for the Zamba2 model.

Implements a LoRA layer that is used in shared attention and gated MLP blocks.

Methods:

  • __init__ –

    Initialize the attention layer.

__init__(input_dim, rank, output_dim, quant_config=None, prefix='')

Initialize the attention layer.

Parameters:

  • input_dim

    (int) –

    input dimension

  • rank

    (int) –

    LoRA rank

  • output_dim

    (int | list[int]) –

    output dimension

  • quant_config

    (QuantizationConfig | None, default: None ) –

    Configuration for model quantization

  • prefix

    (str, default: '' ) –

    Module prefix used for quantization config lookup

Zamba2MLP

Bases: Module

Feed-forward MLP layer for the Zamba2 model.

Implements a gated feed-forward network that projects inputs to a larger intermediate size, applies GELU activation with gating, then projects back to the original size. Includes optional adapter layers for model adaptation.

Methods:

  • __init__ –

    Initialize the MLP layer.

  • forward –

    Forward pass through the MLP layer.

__init__(config, bare_block_idx, num_hybrid_layers, quant_config=None, prefix='')

Initialize the MLP layer.

Parameters:

  • config

    (Zamba2Config) –

    The Zamba2 model configuration

  • bare_block_idx

    (int) –

    Index of the bare block in the model

  • num_hybrid_layers

    (int) –

    Total number of hybrid layers

  • quant_config

    (QuantizationConfig | None, default: None ) –

    Configuration for model quantization

  • prefix

    (str, default: '' ) –

    Module prefix used for quantization config lookup

forward(hidden_states, block_idx)

Forward pass through the MLP layer.

Parameters:

  • hidden_states

    (Tensor) –

    Input tensor [batch_size, seq_len, hidden_size]

  • block_idx

    (int) –

    Current shared transformer block index

Returns:

  • Tensor –

    Output tensor [batch_size, seq_len, hidden_size] after applying

  • Tensor –

    gated feed-forward transformation

Zamba2MambaDecoderLayer

Bases: Module

Single Mamba decoder layer with normalization.

This implements a Mamba block. It includes input normalization and can process sequences using either chunked or full computation depending on configuration.

Methods:

  • __init__ –

    Initialize the Mamba decoder layer.

  • forward –

    Forward pass through the Mamba decoder layer.

__init__(config, model_config=None, cache_config=None, quant_config=None, prefix='')

Initialize the Mamba decoder layer.

Parameters:

  • config

    (Zamba2Config) –

    The Zamba2 model configuration

  • model_config

    (ModelConfig | None, default: None ) –

    The model config, when available

  • cache_config

    (CacheConfig | None, default: None ) –

    The KV cache config, when available

  • quant_config

    (QuantizationConfig | None, default: None ) –

    Configuration for model quantization

  • prefix

    (str, default: '' ) –

    Module prefix used for quantization config lookup

forward(hidden_states, transformer_hidden_states=None, positions=None, original_hidden_states=None)

Forward pass through the Mamba decoder layer.

Parameters:

  • hidden_states

    (Tensor) –

    Input tensor [batch_size, seq_len, hidden_size]

  • transformer_hidden_states

    (Tensor | None, default: None ) –

    Optional output from transformer path Added to input if provided (used in hybrid architecture)

  • positions

    (Tensor | None, default: None ) –

    Optional position IDs (unused in Mamba)

  • original_hidden_states

    (Tensor | None, default: None ) –

    Optional original inputs (unused in Mamba)

Returns:

  • Tensor –

    Transformed hidden states with residual connection applied

Zamba2Model

Bases: Module

Core Zamba2 model combining transformer and Mamba architectures.

The model processes input through a sequence of hybrid and Mamba-only layers, using token embeddings and final layer normalization.

Methods:

__init__(*, vllm_config, prefix='')

Initialize the Zamba2 model.

Parameters:

  • vllm_config

    (VllmConfig) –

    Configuration object containing model, cache, quantization and LoRA settings

  • prefix

    (str, default: '' ) –

    Optional prefix for parameter names in state dict

embed_input_ids(input_ids)

Convert input token IDs to embeddings.

Parameters:

  • input_ids

    (Tensor) –

    Tensor of input token IDs

Returns:

  • Tensor –

    Embedded representation of the input tokens

forward(input_ids, positions, inputs_embeds=None)

Forward pass through the model.

Parameters:

  • input_ids

    (Tensor | None) –

    Input token IDs

  • positions

    (Tensor) –

    Position IDs for embeddings

  • inputs_embeds

    (Tensor | None, default: None ) –

    Optional pre-computed input embeddings

Returns: