MambaDecoderBlock is a novel decoder architecture that replaces traditional self-attention mechanisms with Mamba state space models, augmented by Mixture of Experts (MoE) layers. This implementation provides an efficient alternative to Transformer-based decoders, offering linear complexity in sequence length while maintaining competitive performance through the integration of state space models and expert routing mechanisms.
The MambaDecoder architecture consists of several key components arranged in a hierarchical structure:
- Token Embedding Layer: Maps discrete tokens to continuous vector representations
- Post-Embedding Normalization (optional): Applies RMSNorm to stabilize training
- Mamba Decoder Blocks: Stacked layers combining Mamba state space models with MoE
- Post-Mamba MLA Block (optional): Multi-Head Latent Attention layer for enhanced context modeling
- Output Head: Final projection to vocabulary space
graph TB
Input[Token IDs<br/>Shape: batch × seq_len] --> Embed[Embedding Layer<br/>vocab_size → dim]
Embed --> PostNorm{Post Embed<br/>Norm?}
PostNorm -->|Yes| Norm1[RMSNorm]
PostNorm -->|No| Block1[MambaDecoderBlock 1]
Norm1 --> Block1
Block1 --> Block2[MambaDecoderBlock 2]
Block2 --> BlockN["..."]
BlockN --> BlockLast["MambaDecoderBlock N<br/>(× depth)"]
BlockLast --> PostMLA{Post Mamba<br/>MLA Block?}
PostMLA -->|Yes| MLABlock[MLABlock]
PostMLA -->|No| Norm2[RMSNorm]
MLABlock --> Norm2
Norm2 --> Linear[Linear Projection<br/>dim → vocab_size]
Linear --> Output[Logits<br/>Shape: batch × seq_len × vocab_size]
graph LR
Input[Input x] --> Norm1[RMSNorm]
Norm1 --> Mamba[Mamba<br/>State Space Model]
Mamba --> Add1[+]
Input --> Add1
Add1 --> Norm2[RMSNorm]
Norm2 --> MoE[MoE<br/>Mixture of Experts]
MoE --> Add2[+]
Add1 --> Add2
Add2 --> Output[Output]
The Mamba component leverages state space models (SSMs) to achieve linear complexity in sequence length. Unlike attention mechanisms that require quadratic computation, Mamba processes sequences efficiently through selective state space modeling, enabling effective long-range dependency modeling.
Key Features:
- Linear time complexity: O(n) vs O(n²) for attention
- Selective state space modeling for adaptive sequence processing
- Configurable depth through
n_layersparameter
The MoE layer implements a sparse expert routing mechanism, where only a subset of experts are activated for each input token. This design enables scaling model capacity without proportional increases in computational cost.
Configuration Parameters:
n_experts: Total number of expert networksn_activated: Number of experts activated per tokenexpert_inter_dim: Intermediate dimension for expert networks (optional)shared_expert_inter_dim: Dimension for shared expert (optional)use_adaptive_bias: Enable adaptive bias updatesbias_update_rate: Learning rate for bias adaptation
The optional MLA block provides an efficient attention mechanism that combines:
- LoRA-based Projections: Low-rank adaptations for query and key-value projections
- Rotary Positional Embeddings (RoPE): Position-aware attention computation
- KV Caching: Efficient incremental decoding support
- Latent Attention: "Absorb" mode for reduced memory footprint
Attention Modes:
naive: Standard attention with full KV cacheabsorb: Latent attention with compressed KV representations
The architecture employs RMSNorm (Root Mean Square Normalization) throughout, providing:
- Improved training stability
- Reduced computational overhead compared to LayerNorm
- Better performance on various tasks
pip install -r requirements.txtDependencies:
torch: PyTorch deep learning frameworkmambapy: Mamba state space model implementationopen_kimi: MoE implementation
import torch
from mamba_decoder.mamba_block import MambaDecoder, MambaConfig, MoEConfig
# Initialize model
model = MambaDecoder(
vocab_size=32000,
max_seq_len=128,
depth=6,
dim=512,
post_embed_norm=True,
mamba_config=MambaConfig(
d_model=512,
n_layers=1,
),
moe_config=MoEConfig(
dim=512,
n_experts=6,
n_activated=2,
expert_inter_dim=None,
shared_expert_inter_dim=None,
use_adaptive_bias=True,
bias_update_rate=0.01,
),
)
# Forward pass
x = torch.randint(0, 32000, (2, 128))
out = model(x)
print(out.shape) # (2, 128, 32000)model = MambaDecoder(
vocab_size=32000,
max_seq_len=128,
depth=6,
dim=512,
heads=64,
post_embed_norm=True,
mamba_config=MambaConfig(
d_model=512,
n_layers=1,
),
moe_config=MoEConfig(
dim=512,
n_experts=6,
n_activated=2,
expert_inter_dim=None,
shared_expert_inter_dim=None,
use_adaptive_bias=True,
bias_update_rate=0.01,
),
post_mamba_mla_block=True, # Enable MLA block
)
# Forward pass with caching support
out = model(x, start_pos=0, mask=None)The project includes a comprehensive benchmarking suite to compare MambaDecoderBlock performance against Regular Mamba and MLABlock implementations.
# Run benchmarks with default settings
python benchmark_suite.py# Custom model dimension and test ranges
python benchmark_suite.py --dim 1024 --seq-lens 256 512 1024 2048 --batch-sizes 1 2 4 8
# CPU-only mode
python benchmark_suite.py --no-cuda
# Custom output directory for plots
python benchmark_suite.py --output-dir my_benchmark_results--dim: Model dimension (default: 512)--seq-lens: Sequence lengths to test (default: [128, 256, 512, 1024, 2048])--batch-sizes: Batch sizes to test (default: [1, 2, 4, 8])--output-dir: Directory to save plots (default: 'benchmark_plots')--no-cuda: Disable CUDA (use CPU only)
- Speed (Throughput): Tokens processed per second
- Latency: Time per forward pass in milliseconds
- Scaling Behavior: Performance across different sequence lengths and batch sizes
- Parameter Efficiency: Throughput per million parameters
from benchmark_suite import run_scaling_benchmarks, plot_benchmark_results, print_summary_table
# Run benchmarks
results = run_scaling_benchmarks(
dim=512,
sequence_lengths=[128, 256, 512, 1024],
batch_sizes=[1, 2, 4],
use_cuda=True,
)
# Print summary
print_summary_table(results)
# Generate plots
plot_benchmark_results(results, output_dir='my_results')For detailed benchmarking documentation, see BENCHMARK_README.md.
| Parameter | Type | Description |
|---|---|---|
vocab_size |
int | Size of the vocabulary |
max_seq_len |
int | Maximum sequence length |
depth |
int | Number of MambaDecoderBlock layers |
dim |
int | Model dimension (hidden size) |
heads |
int | Number of attention heads (for MLA block) |
post_embed_norm |
bool | Apply RMSNorm after embedding |
mamba_config |
MambaConfig | Configuration for Mamba SSM |
moe_config |
MoEConfig | Configuration for MoE layer |
post_mamba_mla_block |
bool | Add MLA block after Mamba blocks |
| Parameter | Type | Description |
|---|---|---|
d_model |
int | Model dimension for Mamba |
n_layers |
int | Number of Mamba layers per block |
| Parameter | Type | Description |
|---|---|---|
dim |
int | Input/output dimension |
n_experts |
int | Total number of expert networks |
n_activated |
int | Number of experts activated per token |
expert_inter_dim |
int | None | Intermediate dimension for experts |
shared_expert_inter_dim |
int | None | Dimension for shared expert |
use_adaptive_bias |
bool | Enable adaptive bias updates |
bias_update_rate |
float | Learning rate for bias adaptation |
The MambaDecoder architecture offers significant computational advantages:
- Mamba Blocks: O(n) complexity per block, where n is sequence length
- MoE Layers: O(n × k) complexity, where k is the number of activated experts
- MLA Block (if enabled): O(n²) complexity, but with reduced memory footprint through latent attention
- Overall: Linear scaling with sequence length when MLA is disabled
- Scalability: Linear complexity enables processing longer sequences efficiently
- Selectivity: State space models can selectively process relevant information
- Memory Efficiency: Reduced memory requirements for long sequences
- Capacity Scaling: Increase model capacity without proportional compute increase
- Specialization: Different experts can specialize in different aspects of the data
- Efficiency: Only activate necessary experts for each token
- Hybrid Approach: Combines benefits of both state space models and attention
- Flexibility: Can be enabled for tasks requiring explicit attention mechanisms
- Efficiency: Latent attention mode reduces memory overhead
If you use this implementation in your research, please cite:
@software{mambadecoderblock,
title={MambaDecoderBlock: A State Space Model-Based Decoder Architecture},
author={Kye Gomez},
year={2024},
url={https://github.com/kyegomez/MambaDecoderBlock}
}See LICENSE file for details.
This implementation builds upon:
- Mamba state space models
- Mixture of Experts architectures
- Multi-Head Latent Attention mechanisms