Skip to content

feat(cambricon): add flash attention KV-cache provider - #969

Merged
voltjia merged 1 commit into
masterfrom
feat/cambricon-flash-attn-with-kvcache
Sep 4, 2026
Merged

feat(cambricon): add flash attention KV-cache provider#969
voltjia merged 1 commit into
masterfrom
feat/cambricon-flash-attn-with-kvcache

Conversation

@baominghelly

@baominghelly baominghelly commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add the Cambricon linked provider at implementation slot 16 for flash_attn_with_kvcache.
  • Load the Cambricon flash-attn extension (flash_attn_2_bang*.so) and call its mha_varlen_fwd entry point.
  • Support dense and paged KV caches, optional KV append, scalar or tensor cache lengths, cache-batch remapping, causal/window masking, ALiBi forwarding, softmax LSE output, and non-default MLU streams.
  • Extend the operator tests with independent MLU references for FP16 and BF16.

Motivation

Cambricon model inference needs FlashAttention decoding against an existing KV cache. The existing linked implementation covered NVIDIA only, so Cambricon could not select the linked slot used by the model operator manifest.

Type of Change

  • feat — new feature / new operator / new platform
  • fix — bug fix
  • perf — performance improvement (no behavioral change)
  • refactor — code restructuring without behavior change
  • test — adding or fixing tests only
  • docs — documentation only
  • build / ci — build system or CI configuration
  • chore — tooling, formatting, or other non-code changes
  • Breaking change (requires a ! in the Conventional Commits prefix or a BREAKING CHANGE: footer)

Platforms Affected

  • CPU (WITH_CPU)
  • NVIDIA (WITH_NVIDIA)
  • Iluvatar (WITH_ILUVATAR)
  • MetaX (WITH_METAX)
  • Cambricon (WITH_CAMBRICON)
  • Moore (WITH_MOORE)
  • Ascend (WITH_ASCEND)
  • PyTorch C++ bindings (WITH_TORCH)
  • Build system / CMake / CI
  • Python bindings / user-facing API

Smoke Test Result

Not run — the repository's official Cambricon smoke suite was not run. A fresh slot-specific Cambricon linked build and operator test were run instead.

export INFINI_OPS_DISPATCH_BATCH_SIZE=64
cmake -S <source> -B <build> \
  -DAUTO_DETECT_DEVICES=OFF \
  -DAUTO_DETECT_BACKENDS=OFF \
  -DWITH_CPU=OFF \
  -DWITH_CAMBRICON=ON \
  -DWITH_NVIDIA=OFF \
  -DWITH_TORCH=OFF \
  -DWITH_LINKED=ON \
  -DGENERATE_PYTHON_BINDINGS=ON \
  -DINFINI_OPS_OPS=flash_attn_with_kvcache \
  -DINFINI_RT_ROOT=/workspace/install/infinirt-master-test
cmake --build <build> --target ops --parallel "$(nproc)"
python -m pytest -q tests/test_flash_attn_with_kvcache.py \
  --devices cambricon --maxfail=1

12 passed in 0.28s

Test Results on Supported Platforms

Platform Affected Build / Smoke Result Full Result / Notes
NVIDIA No N/A - not affected N/A - not affected
Iluvatar No N/A - not affected N/A - not affected
MetaX No N/A - not affected N/A - not affected
Cambricon Yes Slot-specific ops target passed; official smoke not run 12 passed for test_flash_attn_with_kvcache.py
Moore No N/A - not affected N/A - not affected
Ascend No N/A - not affected N/A - not affected
Targeted pytest output
............                                                             [100%]
12 passed in 0.28s

Benchmark / Performance Impact

N/A - no benchmark was run.

Notes for Reviewers

  • This is a linked provider and requires the Cambricon flash-attn Python distribution and its flash_attn_2_bang*.so library.
  • The provider currently rejects rotary tables, cache left padding, and non-zero softcap.
  • Paged cache mode does not support cache_batch_idx; dense cache mode does.
  • Shared paged KV-cache helpers live in src/common/op_utils/paged_kv_cache.h. The header intentionally depends on ATen because its current consumers are linked Torch FlashAttention providers; this scope is documented inline.
  • clang-format --dry-run --Werror and git diff --check passed.
  • Building the unrestricted default all target also attempts repository examples; those examples failed to link against the installed InfiniRT because it lacks TensorView::ToString(). The tested infiniops and ops targets built successfully.

@baominghelly
baominghelly force-pushed the feat/cambricon-flash-attn-with-kvcache branch from 6e71e4b to c7c7836 Compare September 3, 2026 02:10
@baominghelly
baominghelly marked this pull request as ready for review September 3, 2026 02:27
@baominghelly
baominghelly requested a review from a team September 3, 2026 02:27

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

这个文件和所在文件夹先不引入了,有需要的话先放入 src/linked/torch/cambricon/ops/flash_attn_with_kvcache/flash_attn.ccnamespace detail 里面吧。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

已修改,放入common文件夹中

@baominghelly
baominghelly force-pushed the feat/cambricon-flash-attn-with-kvcache branch from c7c7836 to 4a750ac Compare September 3, 2026 07:14
@voltjia
voltjia merged commit e9a2430 into master Sep 4, 2026
12 of 20 checks passed
@voltjia
voltjia deleted the feat/cambricon-flash-attn-with-kvcache branch September 4, 2026 01:59
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.

2 participants