Skip to content

refactor(linked)!: migrate flash_attn_with_kvcache provider - #894

Merged
voltjia merged 1 commit into
feat/linked-cambricon-rms-normfrom
refactor/linked-flash-attn-with-kvcache
Aug 7, 2026
Merged

refactor(linked)!: migrate flash_attn_with_kvcache provider#894
voltjia merged 1 commit into
feat/linked-cambricon-rms-normfrom
refactor/linked-flash-attn-with-kvcache

Conversation

@voltjia

@voltjia voltjia commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Move the NVIDIA flash_attn_with_kvcache provider from the handwritten src/torch path to the generic src/linked/torch architecture.
  • Replace the operator-specific FlashAttention discovery, ABI check, link, and RPATH CMake block with linked-library YAML metadata and the shared resolver.
  • Preserve the existing public operator API and behavioral tests while assigning the linked implementation to slot 16.

Motivation

The existing integration already called the C++ ABI exported by flash_attn_2_cuda, but independently reimplemented package discovery, symbol inspection, library linking, RPATH setup, stream bridging, and ATen tensor adaptation in src/CMakeLists.txt and src/torch.

This migration makes the complex direct-symbol FlashAttention provider use the same linked-library mechanism as the simpler linked operators. It also validates that the architecture supports optional tensors, mutable caches, auxiliary outputs, scalar adaptation, and non-default streams without copying third-party kernels into InfiniOps.

This PR is stacked on #886. N/A - no linked issue.

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

Remote environment: ssh nvidia, accelerator-dev/nvidia:latest, NVIDIA A100, PyTorch 2.10.0a0+b4e4ee8, FlashAttention 2.7.4.post1+25.12.

cmake -S . -B build-linked-fa-smoke -G Ninja \
  -DINFINI_RT_ROOT=/opt/infinirt \
  -DWITH_CPU=ON -DWITH_NVIDIA=ON -DWITH_TORCH=ON -DWITH_LINKED=ON \
  -DAUTO_DETECT_DEVICES=OFF -DAUTO_DETECT_BACKENDS=OFF \
  -DGENERATE_PYTHON_BINDINGS=ON -DINFINI_OPS_SMOKE_BUILD=ON
cmake --build build-linked-fa-smoke --target ops -j 16
PYTHONPATH=build-linked-fa-smoke/install \
  python3 -m pytest tests -m smoke --devices nvidia -q

78 passed, 20 skipped, 7554 deselected in 20.60s

Test Results on Supported Platforms

Platform Affected Build / Smoke Result Full Result / Notes
NVIDIA Yes Focused linked build and default smoke passed tests/test_flash_attn_with_kvcache.py: 12 passed
Iluvatar No N/A - not affected N/A
MetaX No N/A - not affected N/A
Cambricon No N/A - not affected N/A
Moore No N/A - not affected N/A
Ascend No N/A - not affected N/A
Additional validation output
python3 scripts/resolve_linked_ops.py \
  --devices nvidia \
  --ops flash_attn_with_kvcache \
  --nm nm --readelf readelf --cxxfilt c++filt
exit 0

cmake -S . -B build-linked-fa -G Ninja \
  -DINFINI_RT_ROOT=/opt/infinirt \
  -DWITH_NVIDIA=ON -DWITH_LINKED=ON -DWITH_TORCH=OFF \
  -DGENERATE_PYTHON_BINDINGS=ON \
  -DINFINI_OPS_OPS=flash_attn_with_kvcache
cmake --build build-linked-fa --target ops -j 16

-- Resolved 1 linked operator source(s).
[build completed]

PYTHONPATH=build-linked-fa/install \
  python3 -m pytest tests/test_flash_attn_with_kvcache.py \
  --devices nvidia -q

12 passed in 2.12s

Benchmark / Performance Impact

N/A - this PR changes provider discovery and integration structure, not the called FlashAttention ABI or operator computation.

Notes for Reviewers

  • The provider still calls the exact flash::mha_fwd_kvcache C++ ABI exported by the installed flash_attn_2_cuda DSO. The full demangled signature is now verified by flash_attn.yaml; a package version is no longer treated as an ABI proxy.
  • The generic template owns ATen conversion, cache mutation, auxiliary LSE copy-back, scalar cache_seqlens adaptation, and external-stream guarding. The NVIDIA provider contains only the library ABI declaration/call and slot specialization.
  • No third-party kernel source or generated kernel is copied into the repository.
  • The current stack base predates master commit 1dc5014f (fix: copy optional tensor metadata #881). Manual validation applied that already-merged optional-metadata fix only to the remote test copy; it is not part of this PR. The branch should be rebased onto current master after its seven linked prerequisites merge.
  • The existing operator test file is unchanged and covers dense and paged caches, KV append, tensor/scalar sequence lengths, LSE output, defaults, and a non-default CUDA stream.

BREAKING CHANGE: enabling the NVIDIA flash_attn_with_kvcache provider now requires WITH_LINKED=ON instead of relying on WITH_TORCH=ON, and its implementation index moves from slot 8 to linked slot 16. The public operator signature and behavior are unchanged.

@voltjia
voltjia marked this pull request as ready for review August 7, 2026 03:35
@voltjia
voltjia requested a review from Ziminli August 7, 2026 03:35
@voltjia voltjia changed the title refactor(linked)!: migrate flash_attn_with_kvcache provider refactor(linked)!: migrate flash_attn_with_kvcache provider Aug 7, 2026
@voltjia
voltjia merged commit 69b4bfc into master Aug 7, 2026
6 checks passed
@voltjia
voltjia deleted the refactor/linked-flash-attn-with-kvcache branch August 7, 2026 11:00
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