Skip to main content

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 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.

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

  1. Flash-MSA only supports headdims 128, block size 128.
  2. Flash-MSA does not currently return fully materialized KL div. loss term in the fwd/bwd (see blog for explanation).
  3. No support for quantized training (fp8, nvfp4, mxfp4).
  4. No support for attn temps / oai-style softmax bias.
  5. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flash_msa-0.1.0.tar.gz (33.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flash_msa-0.1.0-py3-none-any.whl (38.8 kB view details)

Uploaded Python 3

File details

Details for the file flash_msa-0.1.0.tar.gz.

File metadata

  • Download URL: flash_msa-0.1.0.tar.gz
  • Upload date:
  • Size: 33.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for flash_msa-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2b39c2cf0750f060cd933e796fc4df32a3f38603a6dd496c8cf93486e58f0237
MD5 047829b6b0862360b01e8afaa9a0aa18
BLAKE2b-256 5d92366dddf7917f27c77e26d4aeb4b06b85772ec2d0f12092256cc7d591027c

See more details on using hashes here.

File details

Details for the file flash_msa-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: flash_msa-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 38.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for flash_msa-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6646ac530156f512d227839cbf1412656a74a8b2aea030cd990f808449987c27
MD5 d3e958af860487031939cda9e59e7e29
BLAKE2b-256 856f5b0bc90e70c88baa87d045723798290803bd7e5832dfe466d116e2833917

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page