[cuda backend] optimized L_kv threshold for sdpa implementation selection. #20142
[cuda backend] optimized L_kv threshold for sdpa implementation selection. #20142Gasoonjia wants to merge 4 commits into
Conversation
…decode Coalesce int4 W4A8 decode-matvec scale/zero loads by baking the [N, n_groups] layout into the weight constant at pack time. Introduces CudaCoalescedInt4Tensor (an ExecuTorch-internal subclass) that owns the [n_groups, N] -> [N, n_groups] transpose, registers the int4_plain_mm dispatch on it by type, and adds the coalesced dp4a matvec kernel that reads scale/zero row-for-row with qdata (single coalesced load vs 32 stride-N cache lines). ~29.2 -> 37.4 tok/s on gemma group_size=32. Rebased onto main; INT8 dp4a decode op and the floor_div pass from this branch landed separately and now live in quantize_op_dispatch/.
…ied) + benchmark rework
Summary:
At decode (L_q==1) the standard pack-GQA SDPA kernel's grid collapses to
CTA = batch * n_kv_heads, which under-occupies the SMs; split-K flash-decoding
partitions the KV sequence across many more CTAs to fill the GPU. In
ReplaceEdgeOpWithTritonOpPass._pick_sdpa_kernel, route decode to split-K when
L_q==1 and L_kv >= 256 (power-of-2 head dim required; prefill and non-pow2 head
dims keep the standard kernel).
The 256 crossover was measured under CUDA-graph timing (capture+replay, faithful
to the deployed --cuda_graph runtime). The earlier 2048 boundary was overfit to
a plain (non-cuda-graph) microbenchmark, which charged split-K a ~140us per-call
partial-buffer alloc + extra-launch overhead that the graph runtime eliminates;
under faithful timing split-K wins ~1.2-20x from L_kv ~= 256 upward.
benchmark_sdpa.py reworked: deleted run_sweep and all CSV/sentinel machinery;
run_benchmark now compares all six backends (ET-standard, ET-split-K, PyTorch,
Flash, Efficient, Math) with the PyTorch correctness check, across several
decode configs (gemma D256/CTA16, qwen D256/CTA2, D128/CTA16) over the L_kv
range, with a cuda-graph on/off toggle (--mode {cudagraph,plain,both}) timing
every backend through a small self-contained cuda-graph primitive; terminal-only
output. Each reported cell is the mean+/-std over the last 6 of 10 runs (first 4
discarded as warmup; N_RUNS=10, N_WARMUP=4).
Test Plan:
Exercised against the repo (PYTHONPATH) since the conda env's installed
executorch is stale; a lib reinstall is required for the routing to take effect
in a real export.
backends/cuda/tests/test_sdpa_splitk_replacement.py
- L_kv=128 -> standard; L_kv=256 -> split-K; L_kv=4096 -> split-K;
non-pow2 D=96 -> standard.
backends/cuda/tests/test_triton_sdpa_splitk.py (14) and
backends/cuda/tests/test_triton_sdpa_nan.py (3) pass. 21 tests total.
gemma4_31b long-context decode (2401-tok prompt, 256 new tokens, temp 0,
--cuda_graph, 10 runs middle-6) with split-K routing: decode 37.91 -> 43.98
tok/s (+16.0%); prefill within noise.
python backends/cuda/benchmarks/benchmark_sdpa.py --mode cudagraph (gemma
D256/CTA16, mean+/-std us): L_kv=2048 ET-std 102.4+/-0.0 / ET-split-K 24.6+/-0.2 /
PyTorch 475.1+/-0.3 / Flash 56.5+/-0.0; L_kv=16384 ET-std 785.5+/-0.0 /
ET-split-K 179.8+/-0.1 / PyTorch 3447+/-2.6. Plain-timing mode shows split-K's
per-call overhead (the artifact behind the old 2048).
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/20142
Note: Links to docs will display an error until the docs builds have been completed. ⏳ 2 Pending, 2 Unrelated FailuresAs of commit ab3e466 with merge base 6ca98b3 ( FLAKY - The following job failed but was likely due to flakiness present on trunk:
BROKEN TRUNK - The following job failed but was present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
can you split other int4_mm changes from SDPA? |
This PR needs a
|
PR didn't stand on right branch. Rebase onto main solves the issue and now only sdpa related things are in this PR. |
Our
triton/replacement_pass.pyadopted a suboptimal threshold to choose betweensplited_sdpaandregular_sdpadue to misbenchmarking: the benchmark script was a. didn't take cuda graph optimization into account, and b. overfitted into qwen sdpa configuration.This PR updates benchmarking script to cover gemma4 case as well as other representative case. Based on the results shows as below we take L_kv == 256 as the threshold. With the update, the sliding-window decode attention in gemma4-31b can be benefit from split-sdpa performance gain and decode perf raised from 38.5 tok/s to 43.98 token/s, beats llama.cpp.
gemma sliding (D=256, CTA=16) [B=1 H_q=32 H_kv=16 D=256]
qwen (D=256, CTA=2) [B=1 H_q=16 H_kv=2 D=256]
head_dim=128 (D=128, CTA=16) [B=1 H_q=32 H_kv=16 D=128]
Next
Instead of hard coded the threshold, we should choose the operator base on the actual performance during export. Other refactor is on the way.