Metadata-Version: 2.4
Name: mxflashattn
Version: 1.5.0
Summary: Observable FlashAttention-compatible decode integration for MetaX C500 and MXMACA
License-Expression: Apache-2.0
Classifier: Development Status :: 3 - Alpha
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: torch>=2.4
Requires-Dist: PyYAML<7,>=6.0
Provides-Extra: dev
Requires-Dist: pytest<10,>=8; extra == "dev"
Requires-Dist: ruff<1,>=0.6; extra == "dev"
Provides-Extra: vllm
Requires-Dist: vllm==0.17.0; extra == "vllm"
Provides-Extra: reports
Requires-Dist: matplotlib<4,>=3.8; extra == "reports"
Dynamic: license-file

# MXFlashAttn

**MXFlashAttn 1.5.0** provides a FlashAttention-compatible Python API and an
observable paged-decode integration for **MetaX C500 / MXMACA**.

The package combines:

- validated dense, varlen, and KV-cache forward entry points;
- explicit validation, dispatch information, and opt-in reference fallback;
- an M1 Split-KV decode path for C500 paged KV cache;
- an exact-profile MMA Split-KV path for calibrated FP16 decode shapes;
- vLLM EngineCore registration and per-dispatch evidence.

Native C500 extensions are compiled inside the target MetaX/MACA environment.
The PyPI artifact supplies the Python API, integration layer, profile handling,
and registration entry point.

## Install

```bash
python -m pip install mxflashattn==1.5.0
```

Base requirements:

- Python 3.10+
- PyTorch 2.4+
- PyYAML 6.x

For the vLLM 0.17 dependency contract:

```bash
python -m pip install "mxflashattn[vllm]==1.5.0"
```

MetaX deployments normally provide their own compatible PyTorch, vLLM, and
MXMACA wheels. vLLM 0.17.0 and 0.28.0 were validated on separate C500 software
stacks; they are not interchangeable environments.

## Python API

```python
import os
import torch

from mxflashattn import flash_attn_func, get_last_dispatch_info

# Explicitly enable the correctness/reference path when no native provider exists.
os.environ["MXFLASHATTN_ALLOW_FALLBACK"] = "1"

q = torch.randn(1, 32, 8, 64, dtype=torch.float16)
k = torch.randn(1, 128, 2, 64, dtype=q.dtype)
v = torch.randn_like(k)

out = flash_attn_func(q, k, v, causal=True)
print(out.shape)
print(get_last_dispatch_info())
```

The reference path is intended for API checks and correctness comparison. It
is not a C500 performance backend. Without
`MXFLASHATTN_ALLOW_FALLBACK=1`, a missing native provider raises an explicit
error instead of silently selecting a slow path.

Public entry points:

```python
from mxflashattn import (
    flash_attn_func,
    flash_attn_varlen_func,
    flash_attn_with_kvcache,
    get_last_dispatch_info,
)
```

## C500 Native Setup

Build native extensions in an environment matching the target MetaX PyTorch
and MACA installation:

```bash
git clone https://github.com/leo1852098393-blip/MXFlashAttn.git
cd MXFlashAttn
python -m pip install --no-build-isolation -e .

export MACA_PATH=/opt/maca
bash csrc_native/build_m1.sh
```

Then enable the guarded M1 route and collect dispatch evidence:

```bash
export MXFA_M1_DECODE=1
export MXFLASHATTN_DISPATCH_SAMPLE=1

python integrations/vllm/smoke_generate.py \
  --model /path/to/Qwen3-4B \
  --suite --prompt-count 4 --max-tokens 16 \
  --candidate --output artifacts/m1-smoke.json
```

A performance result is attributed to MXFlashAttn only when the associated
`dispatch.jsonl` contains a successful `m1_split_decode` or
`mma_split_decode` event.

## Verified Scope

| Area | v1.5.0 status |
| --- | --- |
| M1 decode | FP16 / BF16, head dimensions 64 / 128, GQA ratios 2 / 4 / 8 |
| vLLM 0.17.0 | Real Qwen3-4B/8B generation, M1 dispatch, fallback, and long-decode measurements |
| vLLM 0.28.0 | EngineCore plugin registration, real M1 dispatch, and a Qwen3-8B shape matrix |
| MMA decode | FP16 single-token decode, exact calibrated profiles only |
| Prefill | MetaX vendor production path; chunked prefill remains a correctness reference |
| Backward | Explicit MetaX provider integration; no in-house backward kernel |

In four vLLM 0.17.0 long-decode workloads, M1 delivered 2.67-3.54x the
throughput of the project's historical v7 path and reached 79.2%-86.5% of
MetaX vendor throughput. In one exact Qwen3-4B operator shape, MMA median
latency was `43.520 us` versus `46.592 us` for vendor over 100 rounds.

In the reported
[vLLM 0.28.0 Qwen3-8B matrix](https://github.com/leo1852098393-blip/MXFlashAttn/blob/main/docs/V1_5_028_MATRIX.md),
long-KV samples at batch 16 or larger remained 28%-46% below MetaX vendor. No
general M1-over-vendor crossover is claimed.

These results are bound to their recorded device, software stack, model,
shape, and baseline. The MMA comparison is operator-level, not an end-to-end
request-speedup claim.

## Runtime Boundaries

- Prefill and unprofiled decode shapes retain the MetaX vendor path.
- M1 requires its documented dtype, layout, head-dimension, and GQA contract.
- MMA requires an exact profile match, including KV heads, page size, binary
  variant, and shared-object identity.
- Short or low-concurrency requests may not benefit from M1.
- FP8 KV cache, multi-card execution, SGLang, GQ=7, and general TTFT gains are
  not v1.5.0 support claims.
- Unsupported API options fail explicitly or follow a documented fallback.

## Development and Audit

The project's own kernel implementations under `csrc_native/` were written by
AI coding agents. The project owner set the project goals, verification rules,
and release boundary, and made the final accept-or-reject decisions for code
changes and public claims.

Favorable measurements do not automatically become performance claims. The
short-KV `+15.2%` difference was the same order as repeated-run variance; the
long-KV `+22.7%` result was a single observation and does not establish a
general crossover. Neither is used for a general performance claim.

## Documentation

- [GitHub repository](https://github.com/leo1852098393-blip/MXFlashAttn)
- [GitLink mirror](https://gitlink.org.cn/Leo77/MXFlashAttn)
- [API guide](https://github.com/leo1852098393-blip/MXFlashAttn/blob/main/docs/API.md)
- [Support matrix](https://github.com/leo1852098393-blip/MXFlashAttn/blob/main/docs/support-matrix.md)
- [Evidence index](https://github.com/leo1852098393-blip/MXFlashAttn/blob/main/docs/EVIDENCE.md)
- [Release validation](https://github.com/leo1852098393-blip/MXFlashAttn/blob/main/docs/V1_5_RELEASE_VALIDATION.md)

## License

Apache License 2.0. MXMACA runtime, MetaX vendor packages, vLLM, model
weights, and datasets remain subject to their respective licenses.
