SSM-As-VLM-Bridge: State Space Models as Vision-Language Bridge Layers
📖 Abstract
This repository presents an exploration into leveraging State Space Models (SSMs) as bridge/adapter layers for Vision-Language Models (VLMs). The project introduces a novel architecture that uses SSMs to facilitate cross-modal understanding between visual and textual representations, potentially offering more efficient and interpretable vision-language fusion compared to traditional attention-based approaches.
🏗️ Model Architecture
Overview
The SSM-As-VLM-Bridge architecture consists of three main components:
- Vision Encoder: Enhanced Vision Transformer (ViT) for image feature extraction
- SSM Bridge: Multi-layer State Space Model for cross-modal fusion
- Language Model: Transformer-based decoder for text generation
Detailed Architecture
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Vision Input │ │ Text Input │ │ Generated │
│ (224×224×3) │ │ (Tokens) │ │ Text Output │
└─────────┬───────┘ └─────────┬───────┘ └─────────┬───────┘
│ │ │
▼ ▼ ▲
┌─────────────────┐ ┌─────────────────┐ │
│ Vision Encoder │ │ Token Embedding│ │
│ (Enhanced ViT) │ │ + Positional │ │
└─────────┬───────┘ └─────────┬───────┘ │
│ │ │
▼ │ │
┌─────────────────┐ │ │
│ SSM Bridge │ │ │
│ (Multi-layer │ │ │
│ State Space │ │ │
│ Model) │ │ │
└─────────┬───────┘ │ │
│ │ │
└──────────┬───────────┘ │
│ │
▼ │
┌─────────────────┐ │
│ Cross-Modal │ │
│ Attention │ │
└─────────┬───────┘ │
│ │
▼ │
┌─────────────────┐ │
│ Language │ │
│ Transformer │ │
│ (Decoder) │ │
└─────────┬───────┘ │
│ │
└─────────────────────────────────┘
Key Components
1. Enhanced Vision Transformer (ViT)
- Patch Size: 16×16 pixels
- Embedding Dimension: 768
- Number of Layers: 12
- Number of Heads: 12
- Input Resolution: 224×224×3
2. SSM Bridge Layer
- State Dimension: 64
- Hidden Dimension: 256
- Number of Layers: 4
- Dropout: 0.1
- Activation: Enhanced Swish with numerical stability
3. Cross-Modal Attention
- Number of Layers: 2
- Number of Heads: 8
- Multi-Query Attention: Efficient attention with shared key-value heads
4. Language Model
- Vocabulary Size: 32,000
- Embedding Dimension: 768
- Number of Layers: 12
- Number of Heads: 12
- KV Heads: 1 (Multi-Query)
- Max Sequence Length: 2,048
🚀 Installation
Prerequisites
- Python 3.10+
- PyTorch 2.0+
- CUDA (optional, for GPU acceleration)
Install from PyPI (Recommended)
pip install ssm-bridge
Install from Source
# Clone the repository
git clone https://github.com/kyegomez/SSM-As-VLM-Bridge.git
cd SSM-As-VLM-Bridge
# Install in development mode
pip install -e .
Install Dependencies
pip install -r requirements.txt
💻 Usage
Basic Usage
import torch
from ssm_bridge.model import EnhancedVLM, VLMConfig
# Create configuration
config = VLMConfig(
img_size=224,
patch_size=16,
vision_embed_dim=768,
vision_num_layers=12,
vision_num_heads=12,
vocab_size=32000,
text_embed_dim=768,
text_num_layers=12,
text_num_heads=12,
text_kv_heads=1,
max_seq_length=2048,
ssm_state_dim=64,
ssm_hidden_dim=256,
ssm_num_layers=4,
ssm_dropout=0.1,
cross_attn_layers=2,
cross_attn_heads=8,
dropout=0.1,
layer_norm_eps=1e-5,
)
# Initialize model
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = EnhancedVLM(config).to(device)
# Prepare input data
images = torch.randn(2, 3, 224, 224).to(device) # Batch of images
tokens = torch.randint(0, 32000, (2, 10)).to(device) # Text tokens
targets = torch.randint(0, 32000, (2, 10)).to(device) # Target tokens
# Forward pass
logits, loss = model(images, tokens, targets)
print(f"Logits shape: {logits.shape}, Loss: {loss.item():.4f}")
# Text generation
generated_tokens = model.generate(
images,
max_length=50,
temperature=0.8,
top_k=50,
top_p=0.9
)
print(f"Generated tokens shape: {generated_tokens.shape}")
Advanced Configuration
# Custom SSM configuration for different use cases
config = VLMConfig(
# Vision settings
img_size=384, # Higher resolution
patch_size=16,
vision_embed_dim=1024, # Larger embeddings
vision_num_layers=24, # Deeper vision encoder
# SSM Bridge settings
ssm_state_dim=128, # Larger state dimension
ssm_hidden_dim=512, # Larger hidden dimension
ssm_num_layers=6, # More SSM layers
# Language settings
text_embed_dim=1024, # Match vision embedding
text_num_layers=24, # Deeper language model
max_seq_length=4096, # Longer sequences
)
🔬 Research Contributions
Novel SSM Bridge Design
The SSM bridge introduces several innovations:
- Multi-layer SSM Architecture: Stacked SSM layers with residual connections
- Enhanced Numerical Stability: Clamped activations and proper initialization
- Gating Mechanisms: Adaptive gating for better information flow
- Cross-modal State Management: Efficient state transitions between modalities
Key Features
- Efficient Attention: Multi-Query Attention reduces computational complexity
- Numerical Stability: Enhanced Swish activation and proper gradient flow
- Modular Design: Easy to modify and extend components
- Memory Efficient: Optimized for large-scale training
📊 Model Specifications
| Component | Parameter Count | Memory Usage | FLOPs |
|---|---|---|---|
| Vision Encoder | ~86M | ~2GB | ~15G |
| SSM Bridge | ~2M | ~0.5GB | ~5G |
| Language Model | ~86M | ~2GB | ~20G |
| Total | ~174M | ~4.5GB | ~40G |
Estimates for batch size 1, sequence length 2048, image size 224×224
🎯 TODO & Roadmap
Immediate Tasks (Next 2-4 weeks)
- Train Base Model: Implement training pipeline with COCO/CC3M datasets
- Benchmark Performance: Compare against CLIP, LLaVA, and other VLMs
- Ablation Studies: Analyze SSM bridge effectiveness
- Memory Optimization: Implement gradient checkpointing and mixed precision
Medium-term Goals (1-3 months)
- All-SSM VLM: Explore replacing attention with SSMs throughout
- Multi-modal SSM: Extend SSM to handle multiple modalities
- Efficient Inference: Optimize for real-time applications
- Pre-trained Models: Release checkpoints for various scales
Long-term Vision (3-6 months)
- Large-scale Training: Train on web-scale datasets
- Zero-shot Evaluation: Comprehensive evaluation on VQA, captioning, etc.
- Deployment Pipeline: Easy deployment for production use
- Community Models: Open-source ecosystem for SSM-based VLMs
🧪 Experiments & Benchmarks
Planned Evaluations
-
Image Captioning
- COCO Captions
- Flickr30k
- NoCaps
-
Visual Question Answering
- VQA v2.0
- GQA
- OK-VQA
-
Zero-shot Classification
- ImageNet-1k
- ImageNet-21k
- CIFAR-100
-
Cross-modal Retrieval
- MS-COCO retrieval
- Flickr30k retrieval
Baseline Comparisons
- CLIP: OpenAI's contrastive learning approach
- LLaVA: Large Language and Vision Assistant
- Flamingo: DeepMind's few-shot learning model
- BLIP-2: Bootstrapping Language-Image Pre-training
🤝 Contributing
We welcome contributions! Please see our Contributing Guidelines for details.
Development Setup
# Install development dependencies
pip install -e ".[dev]"
# Run tests
# Code formatting
black .
ruff check . --fix
📚 Citation
If you use this code in your research, please cite:
@misc{gomez2024ssmvlm,
title={SSM-As-VLM-Bridge: State Space Models as Vision-Language Bridge Layers},
author={Kye Gomez},
year={2024},
howpublished={\url{https://github.com/kyegomez/SSM-As-VLM-Bridge}},
note={An exploration into leveraging SSMs as bridge/adapter layers for VLMs}
}
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- Kye Gomez - Original research and implementation
- PyTorch Team - Excellent deep learning framework
- Open Source Community - Inspiration and collaboration
📞 Contact
- Author: Kye Gomez
- Email: kye@swarms.world
- GitHub: @kyegomez
- Discord: Join our community
🔗 Related Work
- Mamba: Linear-Time Sequence Modeling
- Vision Mamba: Efficient Visual Representation Learning
- CLIP: Learning Transferable Visual Representations
- LLaVA: Large Language and Vision Assistant
Note: This is a research project. The model architecture and implementation are subject to ongoing development and improvement. Please check the Issues page for known limitations and planned features.
Metadata
Release files for ssm-bridge 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ssm_bridge-0.0.2.tar.gz | 14.4 kB | Without provenance | Details for ssm_bridge-0.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ssm_bridge-0.0.2-py3-none-any.whl | Python 3 | none | any | Details for ssm_bridge-0.0.2-py3-none-any.whl |
Total release size: 25.9 kB
Release files / ssm_bridge-0.0.2.tar.gz
| Download URL | ssm_bridge-0.0.2.tar.gz |
|---|---|
| Size | 14.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0b701230813a571bc4639879ac30c4f1e913f9620be8709742fed4ecb1059266
|
|
BLAKE2b-256 checksum How to use checksums |
5f120c29132d7354f3f68df009c7489d78cf821d933b1e253cb094a923879efd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.3 CPython/3.12.3 Darwin/24.5.0
|
Release files / ssm_bridge-0.0.2-py3-none-any.whl
| Download URL | ssm_bridge-0.0.2-py3-none-any.whl |
|---|---|
| Size | 11.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cc86e4560ff97b078fb5bc289cb5d0637fb7f1f5d6c359a3bbf23fa0cc27b307
|
|
BLAKE2b-256 checksum How to use checksums |
bd2aaea09b58db7ad11b2da854fd49bb372a0c4a44823e3bced365ec2da70e5f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.3 CPython/3.12.3 Darwin/24.5.0
|
Security for ssm-bridge 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.