Skip to content

[Perf] Parallelize KDA cache updates across layers - #1092

Merged
Todobe merged 6 commits into
sgl-project:mainfrom
wanerlee:codex/kda-cache-layer-parallel-main
Nov 28, 2026
Merged

Todobe merged 6 commits into
sgl-project:mainfrom
wanerlee:codex/kda-cache-layer-parallel-main

Conversation

@wanerlee

@wanerlee wanerlee commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

At small batch sizes, the KDA cache update kernels expose only one program per
request while iterating over layers serially. This change launches
conv_state_rollback and move_intermediate_cache_kda with a
(requests, layers) grid. Each program uses tl.program_id(1) to update one
layer, replacing the serial layer loop.

  • Preserve in-place storage, destination strides, skipped steps and empty inputs.
  • Retain reverse window traversal for overlapping convolution-state copies and
    keep the KDA mover's head/V tiling and tail masks unchanged.
  • Add Kimi K3.1 cases to the existing test_mamba_state_update.py, using its
    data generators, public API and comparison helpers. Existing test cases,
    shared helpers and accuracy standards are unchanged.
  • The PR changes only the canonical Triton implementation and the existing
    Mamba test file. Public signatures, the legacy module and the generic
    non-KDA mover are unchanged.

Validation

python -m pytest tests/python/operators/mamba/test_mamba_state_update.py --impl=triton --impl-ref=torch_native --npu-device=0 --enable-perf=false -v -ra
  • The file contains 26 cases: the original 14 plus 12 Kimi K3.1-derived
    cases. Six cover both operators at batch 1, 8 and 32. Shapes use 69 layers,
    TP=16 (six local heads), head dimensions 128 and four draft tokens. Move uses
    FP32 with transposed destination storage; rollback uses BF16. Each cache pool
    has batch + 3 slots. The single-request rollback case uses step zero so it
    performs a rollback instead of taking the negative-step skip path.
    Six additional Move cases cover mixed valid/negative steps at batch 2/8/32
    and all-negative steps at batch 1/8/32. They check skipped destination
    slots exactly against their initial values across every layer.
  • The added cases use the existing correctness checks and disable optional
    profiling by default. Use -k kimi_k31 for the 12 added cases, or
    -k skipped_steps for the six new skipped-step cases.
  • A3: 51 passed on 60b6672, before the standalone test file was removed.
    This is historical NPU validation, not a fresh run of the consolidated suite.
  • A3 and A5 A/B benchmarks: all 18 measured cases passed accuracy checks on
    each machine.
    These measurements use the separate timing flow described
    below; the optimized kernel code is unchanged by this test consolidation.
  • All 26 cases collected locally. The six original Kimi-derived call paths
    passed reduced-shape CPU Triton-interpreter checks against torch_native,
    preserving batch sizes 1/8/32. The six skipped-step cases also passed
    reduced-shape interpreter checks; injected corruption of a skipped slot
    in the last layer was detected for both patterns. Hardware initialization
    was mocked for these local checks. The full-shape pytest suite has not
    been rerun on NPU.
  • The separate skipped-step benchmark passed all 17 full-shape scenarios
    on Ascend950DT_9582 for both implementations, including unchanged skipped
    slots across all layers. This is NPU benchmark correctness validation,
    not an NPU pytest result; see the second-Move results below.
  • Full pre-commit run --all-files --show-diff-on-failure previously passed
    on an isolated Windows checkout, including the same six skipped-step cases
    from validation commit b53dc695. A rerun for this follow-up was blocked by
    Windows application control (WinError 4551) while launching a hook; no
    new full pre-commit pass is claimed. git diff --check passed.

Performance

The baseline is the serial-layer implementation from upstream commit
458e72caf6cdfb5b9c06711e6f84d134d15e3f97. Both machines' recorded source hashes
match the same measured baseline and optimized source. The final test-file
cleanup does not change the optimized kernels' executable structure.

Kimi K3.1-derived inputs use 69 KDA layers, six local heads, head dimensions 128,
four draft tokens and a pool of 257 slots. TP=16 is a synthetic sharding choice;
each run uses one NPU, logical device 0, without a model server.

Operator Tensors dtype / destination layout
Conv [69, 257, 6, 2304] BF16 / contiguous
Move source [69, B, 4, 6, 128, 128]; destination [69, 257, 6, 128, 128] FP32 / transposed V/K

The original A3/A5 measurements below use step zero for all requests. Timings are median microseconds from three
warmups and 20 alternating-order NPU event samples; speedup is old/new.
Destination resets, compilation and reference checks are outside the measured
interval. A separate A/B timing flow is used because rollback mutates its
input. Event elapsed time can include host enqueue gaps and is not
profiler-summed kernel time or end-to-end serving throughput.

A3

Chip: Ascend910_9362. PyTorch 2.10.0+cpu, torch-npu 2.10.0.post6,
Triton 3.2.0, Triton-Ascend 3.2.2. CANN: 9.1.0.
Recorded at 2026-11-26 02:36:51 UTC.

Batch Conv old (us) Conv new (us) Speedup Move old (us) Move new (us) Speedup
1 171.75 102.09 1.682x 798.37 194.52 4.104x
2 174.48 103.47 1.686x 785.72 251.91 3.119x
4 172.89 101.24 1.708x 788.75 385.26 2.047x
8 178.77 104.90 1.704x 800.88 657.94 1.217x
16 178.36 123.62 1.443x 1216.69 1237.36 0.983x
32 186.69 166.24 1.123x 2283.31 2285.32 0.999x
64 299.36 261.61 1.144x 4446.26 4496.87 0.989x
128 510.84 431.08 1.185x 8948.43 8784.25 1.019x
256 850.06 788.51 1.078x 17434.14 17363.85 1.004x

A5

Chip: Ascend950PR_9589. PyTorch 2.13.0+cpu, torch-npu 2.13.0,
Triton 3.6.0, Triton-Ascend 3.6.0+dev20261105160244.
CANN: 9.2.0. Recorded at 2026-11-26 02:59:04 UTC.

Batch Conv old (us) Conv new (us) Speedup Move old (us) Move new (us) Speedup
1 156.01 29.94 5.212x 4440.56 155.46 28.564x
2 153.03 31.94 4.791x 4447.29 222.05 20.028x
4 155.53 33.18 4.688x 4451.92 352.29 12.637x
8 171.55 59.64 2.876x 4450.19 614.01 7.248x
16 150.37 56.74 2.650x 4450.08 1202.82 3.700x
32 162.94 98.69 1.651x 4445.75 2307.57 1.927x
64 172.29 171.60 1.004x 4466.56 4532.44 0.985x
128 327.89 329.89 0.994x 8892.50 9017.28 0.986x
256 638.04 638.30 1.000x 17733.72 17964.65 0.987x

Large-batch behavior

Small batches improve on both machines, but the improvement is not uniform.
A3 move latency increases by 1.70% at batch 16 and 1.14% at batch 64. A5 move
latency increases by 1.30%-1.47% at batches 64-256; A5 conv is approximately
flat at batches 64-256, with a 0.61% increase at batch 128.

This implementation uses layer parallelism at every batch size. These small
regressions are disclosed rather than treated as noise; their repeatability
has not yet been established across independent runs. No batch-dependent
fallback or end-to-end serving speedup is claimed.

Second KDA Move with skipped steps

Device: Ascend950DT_9582, logical NPU 0. This is a separate device from
the Ascend950PR_9589 run above; its software versions were not supplied in
the new log. Run directory: 20261127T063244.613239Z (2026-11-27 UTC).

The benchmark on the validation branch
compares the same upstream serial baseline against the unchanged optimized
kernel. It uses 69 layers, six local heads, 128x128 state, four drafts, FP32,
transposed destination storage, and a pool of 2 * batch + 3 slots.
Current-state and checkpoint destination slots are disjoint. Before each
sample, the destination is reset and the first Move runs outside timing;
only the second Move is timed. Mixed batches alternate a valid step cycling
through 0..3 with -1; all-skip batches use -1 throughout and still launch
the second Move. Batch 1 has only the all-skip scenario.

All 17 scenarios passed correctness checks for both implementations,
including all layers and unchanged skipped/untouched slots. Timings below
are median microseconds after three warmups and 20 alternating-order samples,
with the same NPU-event wrapper-call metric described above. These shapes and
the first-Move setup differ from the original step-zero benchmark, so absolute
latencies should be compared within each A/B pair.

Batch Mixed old (us) Mixed new (us) Speedup All-skip old (us) All-skip new (us) Speedup
1 — — — 55.904 56.392 0.991x
2 4450.144 233.738 19.039x 57.609 54.737 1.052x
4 4454.345 361.263 12.330x 53.560 52.799 1.014x
8 4455.063 621.127 7.173x 53.888 53.110 1.015x
16 4457.398 1203.355 3.704x 53.212 52.545 1.013x
32 4457.252 2308.066 1.931x 52.706 52.463 1.005x
64 4464.458 4515.592 0.989x 53.911 54.121 0.996x
128 8895.941 8995.697 0.989x 58.841 57.059 1.031x
256 17759.228 17966.352 0.988x 55.087 53.864 1.023x

Mixed batches improve by 1.931x-19.039x at batch 2-32. At batch 64-256,
latency increases by approximately 1.1%-1.2%; this regression is disclosed,
and the sampled old/new ranges do not overlap in this run. Repeatability
across independent runs has not yet been established. All-skip medians remain
approximately 52-59 us, with new-minus-old differences from -2.872 to +0.488 us;
there is no clear skip-only latency amplification in this wrapper-call metric.
No kernel implementation or batch-dependent dispatch change is included in
this follow-up. The benchmark stays on the validation branch; only the six
regression cases are added to this PR's existing test file.

@yuejiat0416

Copy link
Copy Markdown
Collaborator

/pr-review

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

AI-generated; not an approval. Run.

yuejiat0416
yuejiat0416 previously approved these changes Nov 26, 2026
@Todobe
Todobe merged commit ae205b2 into sgl-project:main Nov 28, 2026
53 checks passed
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