Add faster KDA decode kernel (kdn_decode) - #35
Open
endrix wants to merge 2 commits into
Open
Conversation
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}).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_decodein #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:
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_decodecompiles the kernel with megagdn's own bisheng flags (-xcce,--cce-aicore-arch=dav-c220) againstthird_party/pto-isa;tests/test_kda_decode.pybuilds it on demand and validates on-device. Verified on Ascend 910B2: compiles clean and passes 8/8 (worst rel-err 2.2e-7).Files:
kernels/pto/kdn_decode.cpp,megagdn_pto/compile.py(addscompile_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) andcompile.pycompiles it againstthird_party/pto-isa.