Skip to main content

MPS Flash Attention

Flash Attention for PyTorch on Apple Silicon (M1/M2/M3/M4/M5).

O(N) memory instead of O(N²), enabling 100K+ sequence lengths on unified memory.

Performance

Benchmarked on Apple Silicon (M1/M2/M3/M4/M5):

Seq Length vs PyTorch SDPA Notes
1024 1.1-2.0x faster Crossover point
2048 1.7-3.7x faster Sweet spot
4096 2.0-3.9x faster Peak performance
8192+ 3-4x faster SDPA often OOMs

Average speedup: 1.8x across all configurations.

Installation

pip install mps-flash-attn

Build from source

git clone --recursive https://github.com/mpsops/mps-flash-attention.git
cd mps-flash-attention

# Build Swift bridge
cd swift-bridge && swift build -c release && cd ..

# Install (uses the torch already in your environment)
pip install -e . --no-build-isolation

To build the full PyPI matrix (5 Python versions, dual torch ABIs), use scripts/build_all_wheels.sh. The wheels land in dist/. Run bash tests/wheel_matrix/test_wheel_matrix.sh to verify each wheel against the torch versions it claims to support.

Usage

Basic Attention

from mps_flash_attn import flash_attention

# (B, H, N, D) format
q = torch.randn(2, 8, 4096, 64, device='mps', dtype=torch.float16)
k = torch.randn(2, 8, 4096, 64, device='mps', dtype=torch.float16)
v = torch.randn(2, 8, 4096, 64, device='mps', dtype=torch.float16)

out = flash_attention(q, k, v)

Causal Masking

out = flash_attention(q, k, v, is_causal=True)

Sliding Window (Mistral/Llama 3.2)

# Only attend to last 4096 tokens
out = flash_attention(q, k, v, is_causal=True, window_size=4096)

Quantized KV Cache (2-4x memory savings)

from mps_flash_attn import flash_attention_fp8, quantize_kv_fp8

# Quantize K/V to FP8
k_quant, k_scale = quantize_kv_fp8(k)
v_quant, v_scale = quantize_kv_fp8(v)

# Run attention with quantized KV
out = flash_attention_fp8(q, k_quant, v_quant, k_scale, v_scale)

100K+ Long Sequences

from mps_flash_attn import flash_attention_chunked

# Process 100K tokens without OOM
q = torch.randn(1, 8, 100000, 64, device='mps', dtype=torch.float16)
k = torch.randn(1, 8, 100000, 64, device='mps', dtype=torch.float16)
v = torch.randn(1, 8, 100000, 64, device='mps', dtype=torch.float16)

out = flash_attention_chunked(q, k, v, chunk_size=8192)

Drop-in SDPA Replacement

from mps_flash_attn import replace_sdpa

replace_sdpa()  # Patches F.scaled_dot_product_attention

# Now all PyTorch attention uses Flash Attention on MPS

torch.compile() Support

from mps_flash_attn import register_custom_op

register_custom_op()

@torch.compile
def my_attention(q, k, v):
    return torch.ops.mfa.flash_attention(q, k, v, False, None, None)

Training with BF16 Backward

out = flash_attention(q, k, v, bf16_backward=True)  # 2x faster backward
loss = out.sum()
loss.backward()

Benchmarking

# Quick benchmark
python -m mps_flash_attn.benchmark --suite quick

# Full suite with report
python -m mps_flash_attn.benchmark --suite full --output report.html
from mps_flash_attn.benchmark import run_suite, compare_vs_sdpa

results = run_suite(seq_lengths=[1024, 2048, 4096])
compare_vs_sdpa()

Features

Feature Supported Notes
Forward pass yes FP16/BF16/FP32
Backward pass yes Full gradient support
Causal masking yes Native kernel support
Attention masks yes Boolean masks
Sliding window yes For local attention models
GQA/MQA yes Grouped-query attention
Quantized KV yes FP8, INT8, NF4
Chunked attention yes 100K+ tokens
torch.compile() yes Custom op backend
Dropout no Not supported

Architecture

Python API (mps_flash_attn)
         |
    C++ Extension (_C_legacy.so or _C_modern.so, picked at import)
         | dlopen
    Swift Bridge (MFABridge.swift)
         |
    Metal Flash Attention (kernel generation)
         |
    Metal GPU Shaders

Requirements

  • macOS 14+ (Sonoma, Sequoia, or Tahoe)
  • Apple Silicon (M1/M2/M3/M4/M5)
  • Python 3.10, 3.11, 3.12, 3.13, or 3.14
  • PyTorch 2.5 through 2.13

Tested torch + Python combinations

torch 2.5 2.6 2.7 2.8 2.9 2.10 2.11 2.12 2.13
py 3.10 OK OK OK OK OK OK OK OK OK
py 3.11 OK OK OK OK OK OK OK OK OK
py 3.12 OK OK OK OK OK OK OK OK OK
py 3.13 OK OK OK OK OK OK OK OK
py 3.14 OK OK OK OK OK

Empty cells are combinations PyTorch itself does not ship wheels for. Each wheel carries three builds of the C++ extension — _C_legacy (torch 2.5–2.9), _C_modern (2.10–2.12) and _C_next (2.13+) — because torch's C++ ABI changed at 2.10 and again at 2.13; the right one is picked at import.

The dependencies field in the wheel pins torch>=2.5,<2.14, so pip install mps-flash-attn will refuse to install on a torch outside this range rather than installing and crashing at runtime. When a new torch minor is released we cut a release that bumps the upper bound.

Credits

License

MIT

Metadata

Release files for mps-flash-attn 0.6.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for mps-flash-attn 0.6.3
File Actions
mps_flash_attn-0.6.3-cp314-cp314-macosx_11_0_arm64.whl CPython 3.14 CPython 3.14 macOS 11.0+ ARM64 Details for mps_flash_attn-0.6.3-cp314-cp314-macosx_11_0_arm64.whl
mps_flash_attn-0.6.3-cp313-cp313-macosx_11_0_arm64.whl CPython 3.13 CPython 3.13 macOS 11.0+ ARM64 Details for mps_flash_attn-0.6.3-cp313-cp313-macosx_11_0_arm64.whl
mps_flash_attn-0.6.3-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details for mps_flash_attn-0.6.3-cp312-cp312-macosx_11_0_arm64.whl
mps_flash_attn-0.6.3-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details for mps_flash_attn-0.6.3-cp311-cp311-macosx_11_0_arm64.whl
mps_flash_attn-0.6.3-cp310-cp310-macosx_11_0_arm64.whl CPython 3.10 CPython 3.10 macOS 11.0+ ARM64 Details for mps_flash_attn-0.6.3-cp310-cp310-macosx_11_0_arm64.whl

Total release size: 5.5 MB

Release files / mps_flash_attn-0.6.3-cp314-cp314-macosx_11_0_arm64.whl

Download URL mps_flash_attn-0.6.3-cp314-cp314-macosx_11_0_arm64.whl
Size 1.1 MB
Tags CPython 3.14 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
13953e35544dd5a8958b169e950154b1402f37c3303fa56f91b17a9430cc31c7
BLAKE2b-256 checksum
How to use checksums
c65bc3ca8d2ea12b0fdbc8556b954cbf6239b1499dcd742cf303eae1a966bb3b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.12

Release files / mps_flash_attn-0.6.3-cp313-cp313-macosx_11_0_arm64.whl

Download URL mps_flash_attn-0.6.3-cp313-cp313-macosx_11_0_arm64.whl
Size 1.1 MB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
c54416d82197e98d3cead379dd303786f41f0d691196d00c9cdc4f3d19f51a06
BLAKE2b-256 checksum
How to use checksums
332fdf1e1fd4c7b1147dd3d9cc742f6887552fd1ac2f9d3d0fab6973b7fe56ac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.12

Release files / mps_flash_attn-0.6.3-cp312-cp312-macosx_11_0_arm64.whl

Download URL mps_flash_attn-0.6.3-cp312-cp312-macosx_11_0_arm64.whl
Size 1.1 MB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
486696f09d00b643b2634811bc8bf445d099a899e74a2e44830c2983678b119e
BLAKE2b-256 checksum
How to use checksums
6cd43271382fe2afc375af5f9fae47c3e3f335e2aff11c4dbf1a73d9fc2111b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.12

Release files / mps_flash_attn-0.6.3-cp311-cp311-macosx_11_0_arm64.whl

Download URL mps_flash_attn-0.6.3-cp311-cp311-macosx_11_0_arm64.whl
Size 1.1 MB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
43f9c9f29150877a3978b7bc99cfca5f5dad8f75ddd20df7d1825069816e626e
BLAKE2b-256 checksum
How to use checksums
cb2d67e7b9d183adcc2c11df1bf8038b594af2525e7dfeed4e770bb938a266a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.12

Release files / mps_flash_attn-0.6.3-cp310-cp310-macosx_11_0_arm64.whl

Download URL mps_flash_attn-0.6.3-cp310-cp310-macosx_11_0_arm64.whl
Size 1.1 MB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
2ff06ad69f8f7f920dcf5cecb0ea534b511876c8ffdb29f732297a9071635fbe
BLAKE2b-256 checksum
How to use checksums
5a486101f601370a12b98bbf7841e6a4fa7ba258a8391d884bc954613dee1ffe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.12

Security for mps-flash-attn 0.6.3

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.

Release history Release notifications | RSS feed

This release

0.6.3 This release

5 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page