Skip to content

Migrate cpu sdpa from NS flash attn - #2092

Open
jijiaz wants to merge 64 commits into
intel:mainfrom
jijiaz:copilot/migrate-cpu-flash-attention
Open

Migrate cpu sdpa from NS flash attn#2092
jijiaz wants to merge 64 commits into
intel:mainfrom
jijiaz:copilot/migrate-cpu-flash-attention

Conversation

@jijiaz

@jijiaz jijiaz commented Jul 27, 2026

Copy link
Copy Markdown

Description

Migrate CPU SDPA backend from Neural Speed to auto_round_kernel, providing a drop-in CPU implementation with the same public API as the existing XPU sdpa(). The implementation leverages BestLA kernels for mixed-precision (f32 Q + f16/bf16 KV) and homogeneous (fp16/bf16) dispatch, with a scalar fallback for unsupported configurations.

  • Public APIauto_round_kernel.sdpa() now accepts device="cpu" tensors. Supports HND/NHD layouts, causal masking, additive masks, GQA, and multi-tile K/V sequences. Mixed-dtype (f32 Q + f16/bf16 KV) is transparently accelerated via a hidden packed KV cache.
  • Dispatch Routes – 5 internal routes: Tier 0 scalar fallback (always active), Tier 1 mixed fp16 (AVX2) / mixed bf16 (AVX512F or AMX-BF16) via BestLA, Tier 2 homogeneous fp16 (AVX512-FP16) / homogeneous bf16 (AMX-BF16, no-GQA only). Routes 1/2 are promoted to default.
  • Packed KV Cache – Transparent per-forward KV reorder cache for decode workloads in the public sdpa() path. Explicit lifecycle helpers (packed_kv_alloc, update_packed_kv, bestla_sdpa_packed) under auto_round_kernel.internal.cpu for power users.
  • Internal Features – BestLA-only extensions (padding-right, ALiBi slopes, TANH30 activation, prefer_fp32) exposed through auto_round_kernel.internal.cpu for opt-in use; rejected by the public sdpa() contract.
  • Tests – 5 new Python test/benchmark files covering public API semantics, mixed-dtype correctness, internal feature parity, packed KV lifecycle, and performance benchmarks. ISA-aware skip guards for AVX2/AVX512F/AMX-BF16/AVX512-FP16.
  • C++ Unit TestsTestReorderKV, TestPersistentPackedKV, TestPackedForwardSetup, TestHomogeneousForwardSetup, TestMixedPaddingRight, TestMixedAlibiTanh, TestMixedNumericalFeatures.
  • CI – New GitHub Actions workflow (ark_cpu_sdpa.yml) with AVX2 (ubuntu-latest), AVX512F, and AMX-BF16 (self-hosted) runner tiers.

TODO / Follow-up

  • Debug route cleanup – remove debug_resolve_sdpa_route and debug_cpu_sdpa_route once route stability is confirmed in CI; tests should assert numerical parity rather than route integers.

Type of Change

Feature

Checklist Before Submitting

  • My code has been tested locally.
  • Documentation has been updated as needed.
  • New or updated tests are included where applicable.
  • The CUDA CI has passed. You can trigger it by commenting /azp run Unit-Test-CUDA-AutoRound.

@jijiaz
jijiaz force-pushed the copilot/migrate-cpu-flash-attention branch 14 times, most recently from 41e938e to 32356f9 Compare August 3, 2026 03:37
@jijiaz
jijiaz force-pushed the copilot/migrate-cpu-flash-attention branch 2 times, most recently from f90eab3 to d4a3493 Compare August 5, 2026 02:57
Copilot AI added 14 commits August 5, 2026 18:52
Signed-off-by: jijiaz <jijia.zhou@intel.com>
Signed-off-by: jijiaz <jijia.zhou@intel.com>
Signed-off-by: jijiaz <jijia.zhou@intel.com>
…dpa benchmark

Signed-off-by: jijiaz <jijia.zhou@intel.com>
…(phase 2 step 1)

Signed-off-by: jijiaz <jijia.zhou@intel.com>
…phase 2 step 2)

Signed-off-by: jijiaz <jijia.zhou@intel.com>
…2 step 3)

Signed-off-by: jijiaz <jijia.zhou@intel.com>
Signed-off-by: jijiaz <jijia.zhou@intel.com>
…hase 4 step 1)

Signed-off-by: jijiaz <jijia.zhou@intel.com>
jijiaz and others added 28 commits August 5, 2026 18:52
Signed-off-by: jijiaz <jijia.zhou@intel.com>
Signed-off-by: jijiaz <jijia.zhou@intel.com>
Signed-off-by: jijiaz <jijia.zhou@intel.com>
Signed-off-by: jijiaz <jijia.zhou@intel.com>
Signed-off-by: jijiaz <jijia.zhou@intel.com>
Signed-off-by: jijiaz <jijia.zhou@intel.com>
Signed-off-by: jijiaz <jijia.zhou@intel.com>
Signed-off-by: jijiaz <jijia.zhou@intel.com>
Signed-off-by: jijiaz <jijia.zhou@intel.com>
Signed-off-by: jijiaz <jijia.zhou@intel.com>
Signed-off-by: jijiaz <jijia.zhou@intel.com>
Signed-off-by: jijiaz <jijia.zhou@intel.com>
Signed-off-by: jijiaz <jijia.zhou@intel.com>
Signed-off-by: jijiaz <jijia.zhou@intel.com>
Signed-off-by: jijiaz <jijia.zhou@intel.com>
Signed-off-by: jijiaz <jijia.zhou@intel.com>
Signed-off-by: jijiaz <jijia.zhou@intel.com>
Signed-off-by: jijiaz <jijia.zhou@intel.com>
Signed-off-by: jijiaz <jijia.zhou@intel.com>
Signed-off-by: jijiaz <jijia.zhou@intel.com>
Signed-off-by: jijiaz <jijia.zhou@intel.com>
Signed-off-by: jijiaz <jijia.zhou@intel.com>
Signed-off-by: jijiaz <jijia.zhou@intel.com>
@a32543254
a32543254 self-requested a review August 7, 2026 08:43
@pytest.mark.parametrize("layout", ["HND", "NHD"])
def test_bestla_mixed_sdpa_matches_torch(kv_dtype, is_causal, layout):
torch.manual_seed(4003)
batch, heads_q, heads_kv, head_dim, seq = 1, 8, 2, 64, 64

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better also have medium and large size test for acc and performance
maybe take some shape from really LLMs

"auto_round_kernel", reason="compiled ARK extension not built in this environment"
)

_TOL = {torch.float16: (3e-2, 3e-2), torch.bfloat16: (8e-2, 8e-2)}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the TOL seem a little bit too high for float
maybe you could try to set a lower threshold

*.pyc No newline at end of file
*.pyc
*.csv.venv/
auto_round_extension/ark/auto_round_kernel/build_*/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don‘t think we need add more here
build
xbuild
*.csv
*.so
*.pyc

I believe the above already included what you add

| [test_matmul.py](test/test_matmul.py) | Low-level matmul |
| [test_packq.py](test/test_packq.py) | Weight packing utilities |
Notes:
* The patch only routes calls to ARK on XPU when the inputs match ARK kernel constraints; otherwise it falls back to the original torch SDPA.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“ The patch only routes calls to ARK on XPU when the inputs match ARK kernel constraints; otherwise it falls back to the original torch SDPA.”

why here mention xpu ? all the code should only focus on cpu right ?

Comment thread .gitignore
docs/plan/
.venv/
auto_round_extension/ark/auto_round_kernel/build_*/
auto_round_extension/ark/build-*/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants