CuTeDSL Flash-MSA training kernels for sparse attention.
Project description
Flash-MSA
Flash-MSA is written in CuTeDSL for Hopper and Blackwell GPUs (eg H100, B200) on CUDA 13.
These kernels implement training for the MiniMax Sparse Attention paper: https://arxiv.org/abs/2606.13392
Briefly, MSA is a style of sparse attention fitted to GQA that uses a small proxy attention layer to select blocks of keys to provide to the main attention layer. This offers a massive speedup to inference by slashing the memory-bandwidth bottleneck of loading the full KV cache from HBM.
The proxy heads are trained via a KL-divergence loss between the main attention layer's attention scores over the sparsely selected blocks. The proxy heads are assigned groups of main attention heads to select keys for & average scores over for KL-teaching.
This library also includes MSA warmup kernels, which run the main attention densely and train the proxy attention on the full sequence.
More information is included in the blog post.
Installation
flash-msa depends on FA3/4 from flash-attn. Try to configure your CUDA/Python/Torch versions to match one of the flash-attn wheels for a fast installation, but if you must build from source, set MAX_JOBS=<max jobs> to avoid pip install flash-msa[attn] bricking your CPU.
You will also need Python headers, e.g. apt-get install python3.12-dev, for whichever python version you are using.
uv pip install flash-msa
From source:
python setup.py install
or
uv pip install -e . --no-build-isolation
Usage
from flash_msa import flash_msa_func
attn_out, kl_loss = flash_msa_func(Q_proxy, K_proxy, Q, K, V, top_k, head_dim ** -0.5)
or
from flash_msa import flash_msa_warmup
attn_out, kl_loss = flash_msa_warmup_func(Q_proxy, K_proxy, Q, K, V, top_k, head_dim ** -0.5)
Note that kl_loss in the forward is just a torch.zeros placeholder, but after adding it to the main model loss, calling backward() will activate the on-the-fly gradient calcs equivalent to the actual proxy KL loss signal.
Caveats
- Flash-MSA only supports headdims 128, block size 128.
- Flash-MSA does not currently return fully materialized KL div. loss term in the fwd/bwd (see blog for explanation).
- No support for quantized training (fp8, nvfp4, mxfp4).
- No support for attn temps / oai-style softmax bias.
- Proxy Q is grouped by Main KV so Q_p <= KV heads for now.
These are not ridiculous to implement though so if there is demand or if someone makes a PR, I will update the repo to include these features.
Testing
Test sparse MSA correctness against an eager implementation of MSA: python tests/test_eager_match.py [args]
Test warmup MSA correctness against an eager implementation of MSA: python tests/test_warmup_eager_match.py [args]
Training
An MSA training example is implemented in this Megatron-LM fork.
Notably, you must add the kl_loss returned by MSA kernels to the model's main CE loss before backward to train the proxy attention. The kl_loss is currently treated as a torch.zeros` placeholder and calculated on-the-fly in the backward, so logging the kl_loss will not reflect how proxy training is actually going. Some solutions to get some signal on proxy training are logging grad/update norms of proxy weights, or patching the forward kernel to calculate and accumulate KL div, but only doing this once every n steps to amortize how slow this would make the forward.
In general if you are going to train with this it is highly recommended to follow tips from the paper, use MSA warmup before turning on MSA sparse training, and replicate any transformations to the main attention queries and keys (RoPE, QK norm, QK clip, etc) to the proxy queries and keys to improve proxy convergence.
Inference
See MiniMax's official repo for MSA inference kernels.
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 flash_msa-0.1.1.tar.gz.
File metadata
- Download URL: flash_msa-0.1.1.tar.gz
- Upload date:
- Size: 35.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
249e58cd288dc017e7295c121f1eeaa633ce2057c8419cbdc9d056a08f95d6d1
|
|
| MD5 |
0b91c2a36c750ce44330b2cf2f15a4eb
|
|
| BLAKE2b-256 |
08238d0c04807242206ef750fd208b4a8f2af99a0c7fa28b7def0f65cd92f2f4
|
File details
Details for the file flash_msa-0.1.1-py3-none-any.whl.
File metadata
- Download URL: flash_msa-0.1.1-py3-none-any.whl
- Upload date:
- Size: 39.6 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 |
ec1cc169b855272f84901d1bd10e5dd7fb4fe129f911255225feb0a28bd45c3d
|
|
| MD5 |
5cdc3d2572659412e9f2cedf91dc25c9
|
|
| BLAKE2b-256 |
5db101545061e9e2156f0f0298f903e3b1019926826b976a6f16a90e77d62c48
|