Skip to content

Add faster KDA decode kernel (kdn_decode) - #35

Open
endrix wants to merge 2 commits into
huawei-csl:mainfrom
endrix:kda-recurrent-decode
Open

Add faster KDA decode kernel (kdn_decode)#35
endrix wants to merge 2 commits into
huawei-csl:mainfrom
endrix:kda-recurrent-decode

Conversation

@endrix

@endrix endrix commented Jul 21, 2026

Copy link
Copy Markdown

Single-launch fused PTO kernel for the T=1 recurrent KDA decode (naive_recurrent_kda) — decay + key-readout, rank-1 state update, and query readout fused into one launch with the [K,V] state resident in UB.

Correctness: vs naive_recurrent_kda (fp64), worst rel-err 2.18e-07, B∈{1,4,16,32}×2 seeds, bit-identical across repeats.

Vs kdn_decode in #34: ~2.1× faster at the decode point (632 vs 293 GB/s), ~1.3× at 2 GB — after cross-checking #34 is numerically correct KDA (fp16). (#34's bf16 output path has a race: output intermittently all-zeros from a missing sync; the fp16 path is fine.)

738 GB/s at the decode point is a data-volume effect, not a kernel limit — the same kernel climbs to 1088 GB/s (1.47×) as B×HV grows, tracking the volume-matched ceiling:

bandwidth vs volume

Recommendation: standardize on the fp16-I/O + fp32-state path (low-precision I/O is a bandwidth no-op here — the fp32 state read+write dominates traffic).

Build & run (in-repo): megagdn_pto/compile.py::compile_kdn_decode compiles the kernel with megagdn's own bisheng flags (-xcce, --cce-aicore-arch=dav-c220) against third_party/pto-isa; tests/test_kda_decode.py builds it on demand and validates on-device. Verified on Ascend 910B2: compiles clean and passes 8/8 (worst rel-err 2.2e-7).

FLA_KDA_NAIVE=/path/to/flash-linear-attention/fla/ops/kda/naive.py \
  python tests/test_kda_decode.py

Files: kernels/pto/kdn_decode.cpp, megagdn_pto/compile.py (adds compile_kdn_decode), tests/test_kda_decode.py, benchmarks/kernel/bench_kda_decode.py.


This kernel was generated with npu-skillyard (PTO staged-kernel pipeline). It builds and runs directly under the megagdn-pto build — the includes use megagdn's convention (pto-inst.hpp/acl/rt_ffts) and compile.py compiles it against third_party/pto-isa.

endrix added 2 commits July 21, 2026 14:29
Single-launch fused PTO kernel for the T=1 recurrent KDA decode step
(naive_recurrent_kda): decay + key-readout, rank-1 state update, and query
readout compute-fused into one launch with the [K,V] state resident in UB.

- kernels/pto/kdn_decode.cpp    the kernel
- benchmarks/kernel/bench_kda_decode.py    bandwidth benchmark
- tests/test_kda_decode.py      correctness vs naive_recurrent_kda (fp64)

Validated worst rel-err 2.18e-07 (B in {1,4,16,32} x 2 seeds), bit-identical
across repeats. ~2.1x faster than the kdn_decode in huawei-csl#34 at the decode point
(and ~1.3x at 2 GB); see PR description.

Generated with npu-skillyard (https://github.com/huawei-csl/npu-skillyard).
Replace the out-of-repo "kernel_common.h" include with megagdn's direct
convention (<pto/pto-inst.hpp> + using namespace pto; "acl/acl.h";
<runtime/rt_ffts.h>; <cmath>/<cstdint>; guarded AICORE), mirroring
chunk_o_kda.cpp / chunk_cumsum.cpp. Add compile_kdn_decode() to compile.py
(vector dav-c220 flags, single .so for the K=V=128 decode contract) and have
tests/test_kda_decode.py build it on demand when compiled_lib/ is empty.

Verified on Ascend 910B2: compiles clean against megagdn third_party/pto-isa
and passes tests/test_kda_decode.py 8/8 (worst rel-err 2.2e-7 vs
naive_recurrent_kda, B in {1,4,16,32}).
Sign up for free to 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.

2 participants