Skip to content

Add KV cache for paged/non-paged attention - #1355

Merged
cyanguwa merged 131 commits into
NVIDIA:mainfrom
cyanguwa:paged_attention
Mar 18, 2025
Merged

Add KV cache for paged/non-paged attention#1355
cyanguwa merged 131 commits into
NVIDIA:mainfrom
cyanguwa:paged_attention

Conversation

@cyanguwa

@cyanguwacyanguwa commented Dec 4, 2024

Copy link
Copy Markdown
Collaborator

Description

This PR adds KV cache support for FusedAttention, FlashAttention, and UnfusedDotProductAttention backends in TE-PyTorch.

backend | precision | KV cache | architecture | qkv_format | page_size
---------------------------------------------------------------------------------------
Fused | FP16/BF16 | non-paged/paged | sm80+ | bshd,sbhd,thd | >= 1
Flash v2 | FP16/BF16 | non-paged/paged | sm80+ | bshd,sbhd,thd | >= 256
Flash v3 | FP16/BF16 | non-paged/paged | sm90 | bshd,sbhd,thd | >= 1
| FP8 | non-paged/paged | sm90 | thd | >= 1
Unfused | FP32/FP16/BF16 | non-paged/paged | all | bshd,sbhd,thd | >= 1
  • KV cache is in bshd format, and incoming tokens can be in bshd, sbhd, or thd
  • FusedAttention processes QKV in bshd, sbhd_2bshd, or thd_2bshd format directly
  • FlashAttention v2 converts Q to thd and uses flash_attn_varlen_func for attention
  • FlashAttention v3 (must be 39e7197 or after) converts Q to thd and uses flash_attn_with_kvcache for attention
  • UnfusedDotProductAttention converts Q to bshd for attention; for paged, it converts the cache tensors to non-paged first, based on the page table
  • All backends support pure context, pure generation, and mixed context/generation phases
  • FusedAttention and FlashAttention support CUDA graph, UnfusedDotProductAttention doesn't
  • K cache and V cache are expected to have the same page table
  • For FP8, only fp8_dpa=True is supported (and the KV cache is still in FP16/BF16), fp8_mha=True is not supported as of yet
  • Context parallelism is not supported
  • RoPE will be fixed for inference in RoPE enhancements #1478
  • This PR requires FE1.11 from Update FE to 1.11 #1580
  • Please be aware of the fixes in [PyTorch] Minor fixes for TE 2.2 #1589

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refractor

Changes

Please list the changes introduced in this PR:

  • Add KV caching support for FusedAttention, FlashAttention, and UnfusedDotProductAttention
  • Add mixed q/kv format support to FusedAttention for F16
  • Adapt to the new FA3 APIs from FA2.7.3+/hopper for CP and non-CP cases

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

cyanguwaand others added 10 commits December 3, 2024 17:01
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
@cyanguwa

Copy link
Copy Markdown
CollaboratorAuthor

/te-ci pytorch L0

Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
@cyanguwa

Copy link
Copy Markdown
CollaboratorAuthor

/te-ci pytorch L0

Comment threadtransformer_engine/pytorch/attention.py Outdated
Comment threadtests/pytorch/fused_attn/test_paged_attn.py Outdated
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
Comment threadtransformer_engine/pytorch/kv_cache_manager_non_paged.py Outdated
Comment threadtransformer_engine/pytorch/attention.py Outdated
Comment threadtransformer_engine/pytorch/kv_cache_manager_non_paged.py Outdated
Comment threadtransformer_engine/pytorch/kv_cache_manager_non_paged.py Outdated
cyanguwaand others added 7 commits January 29, 2025 07:47
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
Signed-off-by: Charlene Yang <charleney@nvidia.com>
Signed-off-by: Charlene Yang <charleney@nvidia.com>
Signed-off-by: Charlene Yang <charleney@nvidia.com>
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
cyanguwaand others added 7 commits March 14, 2025 18:30
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
@cyanguwa

Copy link
Copy Markdown
CollaboratorAuthor

/te-ci pytorch L0 L1 L3

@sudhakarsingh27sudhakarsingh27 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Summing up, most of my feedback turns out to be either asking for more comments or some renaming. I understand that we want to get this PR in this release so maybe you could go over the feedback once and we can discuss how much of it's reasonable to incorporate.

Comment threadtransformer_engine/pytorch/attention.py
Comment threadtransformer_engine/pytorch/attention.py
Comment threadtransformer_engine/pytorch/attention.py
Comment threadtransformer_engine/pytorch/attention.py
Comment threadtransformer_engine/pytorch/attention.py
Comment threadtransformer_engine/pytorch/dot_product_attention/inference.py Outdated
Comment threadtests/pytorch/test_numerics.py
Comment threadtransformer_engine/pytorch/dot_product_attention/inference.py Outdated
sample_kwargs = (sample_kwargs,)

# Check training/inference
is_training = all(c.training for c in callables)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is all the right function here or should it be any (or maybe we should return an error if there are some that training and some that are not)?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I added an assert for the mixed training/inference case, because we haven't tested it. We can remove the assert if it's proven that the mixed case is fine with make_graphed_callables.

cyanguwaand others added 7 commits March 17, 2025 12:57
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
@cyanguwa

cyanguwa commented Mar 17, 2025

Copy link
Copy Markdown
CollaboratorAuthor

Pipelines:

  • Jax: 25433649 (all pass)
  • PyTorch: 25585637 (in progress; regular CI is expected to have 14-15 test_paged_attn failures due to regression in cuDNN 9.8, e.g. 25446096; running with cuDNN 9.7 now)
    • previous pipeline 25531054 with 9.7 all passed, with expected failures in L0-cppunittest--B200_1GPU (time-out), L1_pytorch_distributed_unittest--H100_4GPU (unrelated to this PR), L1_pytorch_distributed_unittest--B200_8GPU (unrelated to this PR), L3_pytorch_FA_versions_tests--H100_1GPU (FA3 installation issue, is fixed in cb2d56e).

@sudhakarsingh27sudhakarsingh27 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Lgtm.

But I'll note that I'm not fully convinced with the naming convention especially in inference.py and would like it to be revisited in the upcoming refactor. @KshitijLakhani fyi

@cyanguwa

Copy link
Copy Markdown
CollaboratorAuthor

Pipeline 25585637: apart from the expected failures (unrelated to this PR), CI looks good. The L3_pytorch_FA_versions_tests--H100_1GPU didn't finish compiling for the last FA version, but local tests (both CP and non-CP) show that it's fine.

@cyanguwa
cyanguwa merged commit 4f33ece into NVIDIA:mainMar 18, 2025
lhb8125 pushed a commit to lhb8125/TransformerEngine that referenced this pull request Apr 8, 2025
* add paged attention; test_kv_cache_accuray and test_paged_attn pass
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* remove unnecessary change from last commit
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* test_fused_attn pass
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* remove unnecessary import in test_numerics
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* add license for test
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* fix lint
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* add to L0 test
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update license for test_paged_attn
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* update kv_cache_manager license
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* fix build issue from previous merge
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* WIP: minor fix/preparation for inference/cuda graph
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: non-paged
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: non-paged, bshd/sbhd
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: non-paged, thd, no CG
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* WIP: non-paged, thd, CG
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: non-paged, CG
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: non-paged, using paged kernel
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: restructure kernels
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: paged, CG
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: padding + BRCM
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: restructure IP, clean up
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: fix non-CG, fused
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: fix last commit
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: unfused, non-CG
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: flash-attn, non-CG
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: flash_attn_with_kvcache
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* commit two files missed by bcef6b3
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: thd_bshd_bshd
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: fix last commit
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: fix 1c31b68
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: add bshd_2sbhd, sbhd_2bshd
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* WIP: some cleanup
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: all qkv_format combinations and merge CM files
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* WIP: some lint fixes
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: add docstring for IP
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* fix sequences_pre
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* WIP: minor fixes for multi-layer
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: initial multi-layer test
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* WIP: minor clean up
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* WIP: clean up
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* WIP: switch to flash_attn_varlen_func
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: fix unfused for separate q/kv format
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: fix fused for separate q/kv formats
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: flash attn + TELayer + 2 layers
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: unfused + TL + 2layers
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: all modules/backend
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: minor cleanup
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* WIP: FlashAttention on Hopper with 2.7.3
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* WIP: FlashAttention + v3 from 39e7179
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* WIP: FlashAttention + v3 + FP8 + WIP
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* WIP: add backend support table
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* WIP: clean up
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* WIP: separate use_flash_attention_2 and _3
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* WIP: tweaks to paged attn script
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* WIP: enable/disable certain cases for fused attn
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: small fixes for lint and cg
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: minor fixes for attn/infer
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: fix CP
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* WIP: readd page info to FADescriptor_v1
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* minor tweak to test_numerics.py
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* fix 9.5/9.7 sq/skv + mask logic
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* clean up
Signed-off-by: Charlene Yang <charleney@nvidia.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* minor fix for FA3
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* more minor fixes for FA3
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* test page_size=1 for FA3
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* fix t3hd/th3d strides
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* fix ckpt recompute and fa3 k_scale
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* raise dynamo recompile limit for test
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* remove thunder test from L0
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix FA selection logic
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* fix FA3 q_descale shape
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* remove page_table from IP.step() returns
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* fix FP8 FlashAttn DPA fp8_dpa tests
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix CP
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* minor tweaks
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update FA3 note and L3 test
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* fix lint
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* remove redundant import in test
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* adopt new FA3 APIs from FA2.7.3+/hopper for CP and non-CP
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* fix lint
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* relax tols for TransformerLayers
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix merge
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* fix merge 2
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* fix FA import comments
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* relax tols for Ampere
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* fix fa3 version and reduce messaging
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update FA3 to its latest commit on main
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* add default values to IP and assertion to graph.py
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* add more comments in attention
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* use custom_cache_manager instead of cache_manager
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
---------
Signed-off-by: Charlene Yang <8636796+cyanguwa@users.noreply.github.com>
Signed-off-by: Charlene Yang <charleney@nvidia.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@cyanguwa@sudhakarsingh27@ptrendx