MoDA: Mixture-of-Depths Attention — drop-in attention replacement that lets transformers attend across depth (arXiv:2603.15619)
Project description
MoDA — Mixture-of-Depths Attention
A pip-installable PyTorch library implementing Mixture-of-Depths Attention from arXiv:2603.15619.
MoDA lets each attention head attend to both sequence KV (standard causal attention) and depth KV (representations from all preceding layers at the same token position), fused into a single softmax so they naturally compete for attention mass.
Install
pip install -e .
# With Triton kernel support (CUDA required):
pip install -e ".[triton]"
Quick Start
import torch
from moda import MoDAConfig, MoDAModel
config = MoDAConfig(
d_model=512,
num_heads=8,
num_kv_heads=4, # GQA: 2 query heads per KV head
num_layers=6,
chunk_size=64, # chunk-aware depth optimization
)
model = MoDAModel(config)
x = torch.randn(2, 128, config.d_model)
out = model(x) # [2, 128, 512]
Using MoDAAttention Standalone
from moda import MoDAConfig, MoDAAttention
config = MoDAConfig(d_model=256, num_heads=4, num_kv_heads=2, num_layers=8)
attn = MoDAAttention(config)
x = torch.randn(1, 32, 256)
K_depth = torch.randn(1, 2, 32 * 4, 32) # depth KV from 4 preceding layers
V_depth = torch.randn(1, 2, 32 * 4, 32)
output, k_write, v_write = attn(x, K_depth, V_depth)
Using the Kernel Directly
from moda.kernels import moda_attention_naive
Q = torch.randn(1, 4, 32, 64) # [B, H_q, T, d]
K = torch.randn(1, 2, 32, 64) # [B, H_k, T, d]
V = torch.randn(1, 2, 32, 64)
K_depth = torch.randn(1, 2, 32*8, 64) # [B, H_k, T*L, d]
V_depth = torch.randn(1, 2, 32*8, 64)
out = moda_attention_naive(Q, K, V, K_depth, V_depth, num_layers=8)
Architecture
MoDAConfig— Dataclass holding all hyperparametersDepthKVCache— Manages contiguous depth KV storage across layersMoDAAttention— Drop-in attention module with fused seq + depth softmaxMoDATransformerBlock— Attention + FFN, both writing to depth cacheMoDAModel— Full transformer stack with depth cache management
Kernels
moda_attention_naive— Reference PyTorch implementation (correct, not fast)moda_attention_triton— Fused Triton kernel with online softmax (requires CUDA + Triton)
Key Design Choices
- Combined softmax: Sequence and depth attention scores are concatenated and softmaxed together, so they compete for attention mass
- Chunk-aware optimization: Queries split into chunks of size C; each chunk accesses only C×L depth KV entries
- GQA support: G query heads share each KV head;
repeat_interleavehandles expansion - Post-norm default: Paper finds post-norm outperforms pre-norm with MoDA
- FFN writes to depth cache: Both attention and FFN layers produce depth KV entries
Testing
pip install -e ".[dev]"
pytest
License
Apache 2.0
Citation
@article{zhu2026moda,
title={Mixture-of-Depths Attention},
author={Zhu, Yongqi and others},
journal={arXiv preprint arXiv:2603.15619},
year={2026}
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file moda_attention-0.1.0.tar.gz.
File metadata
- Download URL: moda_attention-0.1.0.tar.gz
- Upload date:
- Size: 19.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1d881cb3b1c7bd5b6a83e7265546c1c46cd76340ffff1c13a3d0047c9a689723
|
|
| MD5 |
322620390c3e90f71b1d1f173a267c4d
|
|
| BLAKE2b-256 |
02738081f4568c787b039bf88739f23b641ef309dedf9c55a7a928f210b67c0b
|
File details
Details for the file moda_attention-0.1.0-py3-none-any.whl.
File metadata
- Download URL: moda_attention-0.1.0-py3-none-any.whl
- Upload date:
- Size: 17.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f77fb33df7cd6c4a2bbbb4a85f2208d06c7712412089bc7b5b9ca27559c12e8
|
|
| MD5 |
99f18b4769042378e1a06ac546201aac
|
|
| BLAKE2b-256 |
cfefe482b3101986d2e7a0b5f04ed77f700ce4dd3bdccf19ab4a975f318cb56b
|