Skip to content

Opt concat cache mla - #1239

Merged
valarLip merged 6 commits into
mainfrom
opt_concat_cache_mla
Oct 23, 2025
Merged

valarLip merged 6 commits into
mainfrom
opt_concat_cache_mla

Conversation

@yzhou103

@yzhou103 yzhou103 commented Oct 22, 2025 •

Copy link
Copy Markdown
Contributor

Motivation

Technical Details

use buffer_load_dwordx4

Test Plan

test_concat_and_cache_mla.py

[aiter] summary:

<style> </style>
kv_lora_rank qk_rope_head_dim num_tokens block_size num_blocks dtype device kv_cache_dtype aiter_us vllm_us aiter_bw(TB/s)
512 64 128 64 2 torch.bfloat16 cuda auto 4.13597 3.769417 0.071304192
512 64 128 64 2 torch.bfloat16 cuda fp8 4.201828 3.91702 0.070186595
512 64 256 64 4 torch.bfloat16 cuda auto 3.973454 4.578602 0.14844113
512 64 256 64 4 torch.bfloat16 cuda fp8 4.267344 4.758616 0.138218058
512 64 512 64 8 torch.bfloat16 cuda auto 4.374758 6.242732 0.269648744
512 64 512 64 8 torch.bfloat16 cuda fp8 4.6094 6.520232 0.255922246
512 64 1024 64 16 torch.bfloat16 cuda auto 5.335172 9.29344 0.442215546
512 64 1024 64 16 torch.bfloat16 cuda fp8 5.537771 9.97466 0.426037119
512 64 2048 64 32 torch.bfloat16 cuda auto 7.11025 16.76472 0.663632362
512 64 2048 64 32 torch.bfloat16 cuda fp8 7.370323 17.54682 0.64021509
512 64 4096 64 64 torch.bfloat16 cuda auto 10.91779 30.93031 0.864385766
512 64 4096 64 64 torch.bfloat16 cuda fp8 10.96783 32.50468 0.860442221
512 64 8192 64 128 torch.bfloat16 cuda auto 17.14558 58.70323 1.100830208
512 64 8192 64 128 torch.bfloat16 cuda fp8 17.45699 61.93986 1.081192639
512 64 16384 64 256 torch.bfloat16 cuda auto 30.19321 113.3199 1.250239201
512 64 16384 64 256 torch.bfloat16 cuda fp8 30.66454 120.3454 1.231022372

Submission Checklist

Copilot AI review requested due to automatic review settings October 22, 2025 10:56

Copilot AI left a comment

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.

Pull Request Overview

This PR optimizes the concat_and_cache_mla kernel for Multi-head Latent Attention (MLA) by implementing a vectorized memory loading approach using buffer_load_dwordx4. The optimization shows significant performance improvements, particularly for larger token counts (up to ~4x speedup for 16K tokens).

Key Changes:

  • Introduced optimized kernel variant concat_and_cache_mla_opt_kernel with double-buffered vectorized loads
  • Added conditional dispatch logic to select between original and optimized kernels based on alignment
  • Cleaned up commented-out code and test parameter constraints

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
csrc/kernels/cache_kernels.cu Added optimized MLA cache kernel with vectorized memory operations and updated dispatch logic to use it when dimensions are 8-byte aligned
op_tests/test_concat_cache_mla.py Removed restrictive parameter constraints on dtype argument to allow more flexible test configurations

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread csrc/kernels/cache_kernels.cu Resolved
Comment thread csrc/kernels/cache_kernels.cu Resolved
Comment thread csrc/kernels/cache_kernels.cu Resolved
Comment thread csrc/kernels/cache_kernels.cu Resolved
Comment thread csrc/kernels/cache_kernels.cu Resolved
@valarLip
valarLip merged commit a6652a7 into main Oct 23, 2025
@valarLip
valarLip deleted the opt_concat_cache_mla branch October 23, 2025 09:01
valarLip pushed a commit that referenced this pull request Mar 18, 2026
* opt concat_and_cache_mla

* opt concat_and_cache_mla

* fix rebase conflicts

* opt to use buffer_load_dwordx4

* add bandwidth(TB/s) in test result

* fix lint error
zufayu added a commit that referenced this pull request Oct 12, 2026
The Windows ROCm torch wheels are built without Kineto. torch.profiler still
runs there, but records no device events, so get_trace_perf finds an empty
device frame and returns 0. Every op test that reports a duration prints
0.00 us, and the ones that divide to show an uplift print nan -- visible in
the Windows CI run of test_rmsnorm2d.py as

    [perf] torch avg: 0.00 us, ck avg: 0.00 us, uplift: nan%
    test_rmsnorm2d.py:93: RuntimeWarning: invalid value encountered in scalar divide

122 of the 166 files under op_tests/ go through perftest or run_perftest, so
this is most of the suite. The same gap was hit independently in ROCm/FlyDSL
PR #1239, whose author reports the pytest performance wrapper not completing
"because Torch Kineto's DataFrame field is unavailable".

perftest already has a use_cuda_event path that times with torch.cuda.Event
and never touches the profiler; five call sites pass it explicitly. Probe for
Kineto once and take that path automatically when it is missing, rather than
handing the caller a 0. Builds that ship Kineto -- every Linux ROCm wheel --
resolve to the profiler path exactly as before.

The Windows smoke suite added in #6273 measures no performance, so nothing
depends on this today. It matters as soon as any existing op test is pointed
at Windows, which is the next phase rather than this one.

Verified on MI308X, ROCm 7.2.4, torch 2.10.0, a 4096x4096 fp16 mm:

    profiler path      785.4 us    (unchanged)
    cuda.Event path    799.9 us
    forced fallback    798.1 us

The two timing methods agree to within 2%, so the fallback is not just
nonzero but comparable.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
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