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
py 3.11 OK OK OK OK OK OK OK
py 3.12 OK OK OK OK OK OK OK OK
py 3.13 OK OK OK OK OK OK
py 3.14 OK OK OK

Empty cells are combinations PyTorch itself does not ship wheels for, or that have not been run through tests/wheel_matrix yet.

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

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.2
File Actions
mps_flash_attn-0.6.2-cp314-cp314-macosx_11_0_arm64.whl CPython 3.14 CPython 3.14 macOS 11.0+ ARM64 Details for mps_flash_attn-0.6.2-cp314-cp314-macosx_11_0_arm64.whl
mps_flash_attn-0.6.2-cp313-cp313-macosx_11_0_arm64.whl CPython 3.13 CPython 3.13 macOS 11.0+ ARM64 Details for mps_flash_attn-0.6.2-cp313-cp313-macosx_11_0_arm64.whl
mps_flash_attn-0.6.2-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details for mps_flash_attn-0.6.2-cp312-cp312-macosx_11_0_arm64.whl
mps_flash_attn-0.6.2-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details for mps_flash_attn-0.6.2-cp311-cp311-macosx_11_0_arm64.whl
mps_flash_attn-0.6.2-cp310-cp310-macosx_11_0_arm64.whl CPython 3.10 CPython 3.10 macOS 11.0+ ARM64 Details for mps_flash_attn-0.6.2-cp310-cp310-macosx_11_0_arm64.whl

Total release size: 5.5 MB

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

Download URL mps_flash_attn-0.6.2-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
f043f7ae0875438f41e09724b2c9eadf8b2242619fb3519c5392778320620c60
BLAKE2b-256 checksum
How to use checksums
4511c526dc709aec81771be074b5c2d9fb6c227aa19824daedf67eec02ee369a
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.2-cp313-cp313-macosx_11_0_arm64.whl

Download URL mps_flash_attn-0.6.2-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
d27c965ab9576fc1ed893583e23cecdcdffd69ebf1a36a9323741923b5707a3b
BLAKE2b-256 checksum
How to use checksums
c84156b119fa160c8738d831e3abc903871e5d761fb059739977a61a8cab1308
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.2-cp312-cp312-macosx_11_0_arm64.whl

Download URL mps_flash_attn-0.6.2-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
9b3d52146e2128c35e62e1ef6627c40d843031088a924b0005c6c42f11db3f23
BLAKE2b-256 checksum
How to use checksums
0d81cc7c659de54fc8c51f7e9806f3009962f9d7fc1e4a1033a8b3c69d83a6d1
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.2-cp311-cp311-macosx_11_0_arm64.whl

Download URL mps_flash_attn-0.6.2-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
41aa3cb1e6bf8671be661bc51a1f6db4f054847742a72e5459878967be5aff92
BLAKE2b-256 checksum
How to use checksums
f52dd9dd1e7633b62c6c588abac75f412cb64f077bb6008b80f536166935ebdc
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.2-cp310-cp310-macosx_11_0_arm64.whl

Download URL mps_flash_attn-0.6.2-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
76b2a75a319af290b277b0c4a6e2f58f0d69db1273f4da9618b241bc626cfe38
BLAKE2b-256 checksum
How to use checksums
23a98531022aecfe624a5b4703ccf5521b4dacceff5e6dc057a198ea15088682
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.2

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