Triton-based Flash MLM attention kernels for PyTorch
Project description
flash-mlm
Triton-based attention kernels optimized for MLM-based models, in PyTorch.
Highlights
- ✅ Packed variable-length query/key/value path
- ✅ Optional layer-scoped KV cache (
InferenceCache) for both dense + packed paths - ✅ Dense (
flash_attn_mlm), compressed (flash_attn_mlm_compressed), and precompressed (flash_attn_mlm_precompressed) host launchers - ✅ Inline prefill support (
prefill=True) for iterative inference flows - ✅ Reusable packing workspace + metadata cache (
PackingCache) - ✅ Optional cumulative query attention in packed mode (
causal_query_seq_attn=True) - ✅ MLA + non-MLA support (no RoPE)
- ✅ Benchmarks for context scaling, batch scaling, and sequence-length scaling
[B,H,N,D] padded tensors
-> pack metadata
-> fused Triton MLM kernel
-> packed output
-> unpack to [B,H,N,D]
On the Roadmap
-
Backpropagation support Prefer using
flash_attn_varlen_funcfor training today; native backward support here is still on the roadmap. -
Faster tile scheduling Current packing removes padding tokens, but host-side scheduling/packing overhead is still an optimization target for larger models.
-
Variable context batch sizes Today we support either a single shared context or full-batch context. Intermediate/index-mapped context batching is planned.
-
RoPE compatibility with MLA RoPE is not currently supported with MLA.
-
Paged attention for KV cache
Contributions are most welcome!
Requirements
- Linux
- Python
>=3.11,<3.12 - GPU runtime compatible with your installed
torch+triton
Install
# latest main
pip install "flash-mlm @ git+https://github.com/vymao/flash-mlm.git"
# pinned tag
pip install "flash-mlm @ git+https://github.com/vymao/flash-mlm.git@v0.1.0"
Quick usage
1) Compressed path (packed queries)
import torch
from flash_mlm import (
InferenceCache,
flash_attn_mlm_compressed,
build_pack_metadata,
unpack_from_kernel,
)
B, H, N, D = 2, 8, 128, 64
q = torch.randn(B, H, N, D, device="cuda", dtype=torch.float16)
k = torch.randn_like(q)
v = torch.randn_like(q)
lengths_q = torch.tensor([N, N - 16], device="cuda", dtype=torch.int32)
q_meta = build_pack_metadata(lengths_q, N=N, block_n=64)
cache = InferenceCache()
layer_id = 0
packed_out = flash_attn_mlm_compressed(
q,
k,
v,
num_heads=H,
q_meta=q_meta,
scale=1.0 / (D ** 0.5),
inference_cache=cache,
layer_id=layer_id,
# prefill=True stores this call's packed K/V as next-step context.
prefill=True,
# Optional: each query subsequence can attend previous subsequences.
causal_query_seq_attn=True,
)
out = unpack_from_kernel(packed_out, q_meta, H=H)
2) Dense path (non-packed)
import torch
from flash_mlm import InferenceCache, flash_attn_mlm
B, H, N, D = 2, 8, 128, 64
q = torch.randn(B, H, N, D, device="cuda", dtype=torch.float16)
k = torch.randn_like(q)
v = torch.randn_like(q)
cache = InferenceCache()
layer_id = 0
out = flash_attn_mlm(
q,
k,
v,
scale=1.0 / (D ** 0.5),
inference_cache=cache,
layer_id=layer_id,
prefill=True,
)
Cache + prefill semantics
inference_cacheandlayer_idmust be provided together (or both omitted).prefill=Truerequiresinference_cache.- On cache miss, launchers run with empty context.
- On prefill, current call K/V are stored for the provided
layer_id. InferenceCache.prefill_kv_cache(...)supports both:- dense-style prefill (omit
cu_seqlens_kv) - packed/varlen prefill (provide
cu_seqlens_kv)
- dense-style prefill (omit
Cumulative query attention (packed path)
flash_attn_mlm_compressed supports cumulative query attention via
causal_query_seq_attn=True.
When enabled, each query subsequence can attend to packed main tokens from previous query subsequences in the same batch (in addition to any cached context).
Public API
flash_attn_mlmflash_attn_mlm_compressedflash_attn_mlm_precompressedInferenceCachePackingCachePackMetadatabuild_pack_metadataunpack_from_kernel
Benchmarking
python src/benchmark/benchmark_mlm_nctx.py
python src/benchmark/benchmark_mlm_batch.py
python src/benchmark/standard_kernel/benchmark_mlm_vs_torch.py
python src/benchmark/standard_kernel/benchmark_mlm_vs_torch_sequence_length.py
Outputs are written to plots/.
The standard-kernel comparison benchmarks cover non-MLA and MLA configurations, with and without cache, across both context-length and sequence-length sweeps.
Four-way latency comparison
Generated by benchmark_mlm_vs_torch.py:
Development
poetry install
poetry run pytest -q src/flash_mlm/test_host_utils.py
poetry run pytest -q src/flash_mlm/test.py -k "mlm_dense or mlm_compressed"
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_mlm-1.2.3.tar.gz.
File metadata
- Download URL: flash_mlm-1.2.3.tar.gz
- Upload date:
- Size: 27.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4d181442d06bd41d99ada48b0e2dfb94c7ba09eb2d48159c6317fb0534eb92ac
|
|
| MD5 |
1078c8cf01de5875021dc9a4fe81c0e8
|
|
| BLAKE2b-256 |
b0edc633a67e78c8984b0244f8241e5b599575c1616e253fa7d560d1179b8f0f
|
Provenance
The following attestation bundles were made for flash_mlm-1.2.3.tar.gz:
Publisher:
python-package.yml on vymao/flash-mlm
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
flash_mlm-1.2.3.tar.gz -
Subject digest:
4d181442d06bd41d99ada48b0e2dfb94c7ba09eb2d48159c6317fb0534eb92ac - Sigstore transparency entry: 1243516701
- Sigstore integration time:
-
Permalink:
vymao/flash-mlm@2c2f8d28423a756bfacac148fa986c4daa3c9bc3 -
Branch / Tag:
refs/tags/v1.2.3 - Owner: https://github.com/vymao
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-package.yml@2c2f8d28423a756bfacac148fa986c4daa3c9bc3 -
Trigger Event:
push
-
Statement type:
File details
Details for the file flash_mlm-1.2.3-py3-none-any.whl.
File metadata
- Download URL: flash_mlm-1.2.3-py3-none-any.whl
- Upload date:
- Size: 31.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6dc44c495a4818277cc37fb92505afde3b92bde57184d8031d454fa1b291cddb
|
|
| MD5 |
1f049341c1a1e80f7228a6681dcd96a0
|
|
| BLAKE2b-256 |
5085cc4628017b27b9fe7a08c5ed1328593f0b7abe9c216fe209505d1748b916
|
Provenance
The following attestation bundles were made for flash_mlm-1.2.3-py3-none-any.whl:
Publisher:
python-package.yml on vymao/flash-mlm
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
flash_mlm-1.2.3-py3-none-any.whl -
Subject digest:
6dc44c495a4818277cc37fb92505afde3b92bde57184d8031d454fa1b291cddb - Sigstore transparency entry: 1243516707
- Sigstore integration time:
-
Permalink:
vymao/flash-mlm@2c2f8d28423a756bfacac148fa986c4daa3c9bc3 -
Branch / Tag:
refs/tags/v1.2.3 - Owner: https://github.com/vymao
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-package.yml@2c2f8d28423a756bfacac148fa986c4daa3c9bc3 -
Trigger Event:
push
-
Statement type: