Repository navigation
perf(kimi_k3): gfx1250 FlyDSL KDA backend without fla, fla fused KDA ops and [b, s] KDA projections - #1519
Draft
boyang-amd wants to merge 10 commits into
Draft
perf(kimi_k3): gfx1250 FlyDSL KDA backend without fla, fla fused KDA ops and [b, s] KDA projections#1519boyang-amd wants to merge 10 commits into
boyang-amd wants to merge 10 commits into
Conversation
|
Primary maintainers: @wenxie-amd @Xiaoming-AMD Reach out to the primary maintainers with any questions about this PR. Outside China Standard Time (UTC+8) working hours, for urgent issues you can also reach out to: vidushi8, lorri-rao, GeneDer, amd-fuyuajin, JohnQin-llm. |
…nk_kda safe_gate and disable_recompute After #1451 the KDA core is the largest non-GEMM block in the Kimi K3.1 Pro proxy on MI455X, about half of it eager fp32 chains that transcribe fla kernels the run already imports. Three config fields, all off by default: kda_fla_fused_ops puts the q / k L2 norm, the bounded decay gate and the gated output RMSNorm on fla's fused kernels (as models/hybrid/kimi_delta_attention.py does); kda_fla_safe_gate and kda_fla_disable_recompute are passed to fla's chunk_kda (fla backend only; safe_gate needs the gate bound in [-5, 0)). _resolve_fla_options resolves them once at construction. On one MI455X at the recipe's per-rank shape the KDA core forward + backward goes from 12.66 to 6.64 ms.
…ger path The fused L2 norm, gate and gated RMSNorm against the eager ops in the output and every input gradient; chunk_kda with safe_gate / disable_recompute against its defaults on K3-shaped inputs; a halved gate bound and a negated output gate fail the same checks; config defaults and resolution errors.
… safe_gate and disable_recompute kda_fla_fused_ops, kda_fla_safe_gate and kda_fla_disable_recompute on (PRIMUS_KDA_FLA_* override them), listed among the differences from the MI355X recipe.
…DSL without fla On gfx1250 the flydsl KDA backend becomes a standalone implementation (kda_kernels/_flydsl_gfx1250, no fla import): the chunked delta rule as fla's chunk_kda evaluates it with safe_gate and disable_recompute (step order adapted from fla, MIT, credited in chunk.py), every step a FlyDSL kernel (chunk-local cumsum, fused forward intra step, state recurrence, output; dAv, dhu, wy_dqkg and intra in backward, nothing recomputed), plus FlyDSL kernels for the q / k L2 norm, the bounded gate, the gated output RMSNorm and the short convs. gfx950 keeps _flydsl_v1; load_flydsl_kda_backend picks by architecture, as attn_res_backend flydsl does, and load_flydsl_kda_ops returns the gfx1250 ops. _resolve_kda_ops selects the L2 norm, gate, conv and chunk_kda arguments once at construction; the kda_fla_* fields apply to the fla backend only. Unsupported inputs (unbounded gate, head dim other than 128, T not a multiple of 64, initial / final state, conv bias, context parallelism) raise with a kda_backend: fla hint. One MI455X, recipe per-rank shape: KDA core forward + backward 6.64 ms (fla with the kda_fla_* fields) -> 4.17 ms.
…out fla imported The chunk kernel against eager_chunk_kda and fla's chunk_kda (safe_gate, disable_recompute); the L2 norm, gate, gated RMSNorm, cumsums and conv against their eager forms in the output and every input gradient; a negated beta, a halved gate bound, a negated output gate and a dropped SiLU fail the same checks; unsupported inputs raise; a fresh process runs every kernel with no fla module imported. The fla option tests follow _resolve_kda_ops (kda_fla_* ignored by non-fla backends).
…ackend kda_backend / use_kimi_k3_attention_backend default flydsl; PRIMUS_ATTN_BACKEND=fla switches to fla with the kda_fla_* fields, eager to the reference.
…failing collection models.kimi_k3 imports megatron.training, whose import chain needs a visible GPU (aiter reads the gfx arch from rocminfo), so on a CPU-only runner the module raised RuntimeError during collection. Skip it there, as test_model_registry.py does; with a GPU the error still fails the run.
Without sequence or context parallelism, KimiDeltaAttention transposes the hidden-wide input to [b, s, h] once and runs the q / k / v / f_a / b / g projections on it, so their outputs are already in the [b, s, ...] layout the kernels take; only o_proj's output is transposed back to [s, b, h]. Before, every projection output was transposed. With SP or CP the projections keep the [s, b, h] shard. 3-layer KDA proxy on one MI455X (kda_backend flydsl, EP 1, MBS 2, GBS 16, seq 4096, 12 iterations): 967.6 -> 944.9 ms per iteration, 0.9 GB less memory, loss within 6.3e-5 of the [s, b] path.
The chunk backward returned dq and dk cast to bf16 from kda_bwd_intra's fp32 outputs, which nothing else reads. The kernel now rounds them to bf16 itself (round to nearest even, as .to(torch.bfloat16)) and stores one 16-byte vector per 8 columns; dg stays fp32 for the reverse cumsum. On one MI455X at B 2, T 4096, 96 heads, K 128: bwd_intra 0.747 -> 0.556 ms, the chunk forward + backward 2.822 -> 2.564 ms. The 3-layer KDA proxy's losses are bit for bit unchanged over 12 iterations.
…ls; math.isnan in KDA tests Seven gfx1250 FlyDSL KDA kernel modules defined _MAX_VIEW_BYTES without reading it (the size checks are in chunk.py, conv_kernel.py and ops_kernel.py), and two KDA tests spelled the NaN check err != err. No behaviour change; flagged by code scanning on the PR.
boyang-amd
force-pushed
the
dev/boyang/kimi-k3.1-pro-mi455x-kda
branch
from
November 25, 2026 08:45
9cf24cd to
db75ffc
Compare
This branch has not been deployed
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.
Stacked on #1451 (FlyDSL situ): the branch starts at its head
1cd6baa5, and the PR targets #1451's branch until #1451 is merged, thenmain. No Primus-Turbo change is needed.Why
After #1451, the KDA core (short convs, q / k L2 norm, decay gate,
chunk_kda, gated output RMSNorm) is the largest non-GEMM block left in the Kimi K3.1 Pro 4-layer proxy on 4 x MI455X: 365 ms of GPU kernel time per step and rank in the #1413 profile. About half of it is eager fp32 elementwise chains (kda_l2norm,kda_gate,KimiGatedRMSNorm); the rest is fla's Tritonchunk_kdaand conv, which run well below the hardware on gfx1250. On one MI455X, at the recipe's per-rank shape (B 2, T 4096, 96 heads, K = V = 128), the KDA core forward + backward takes 11.2 ms, about 270 ms per step over 3 KDA layers x 8 micro-batches. Around the core, every q / k / v / gate projection output was also transposed from[s, b]to the[b, s]layout the kernels take.What
Two ways to run KDA faster, selected with the existing backend field (
kda_backend, oruse_kimi_k3_attention_backend, which supersedes it), and one layout change for every backend:kda_backend: flydslon gfx1250 (new;kda_kernels/_flydsl_gfx1250/, does not import fla). gfx950 keeps its existingflydslimplementation (_flydsl_v1);load_flydsl_kda_backendpicks one by architecture, asattn_res_backend: flydsldoes.chunk.py: the chunked delta rule as fla'schunk_kdaevaluates it withsafe_gateanddisable_recompute(the order of the steps and the tensors passed between them adapted from fla, MIT, credited in the file header), every step a FlyDSL kernel: the chunk-local gate cumsum, the fused forward intra step (Aqk,Akk = (I + L)^-1,w,u,qg,kgin one kernel), the forward state recurrence, the output, and in backwarddAqk / dv, the state-gradient recurrence,wy_dqkgand the intra step (which writesdq/dkin bf16 directly), then the reverse cumsum. Nothing is recomputed in backward.ops_kernel.py: the q / k L2 norm, the bounded gate and the gated output RMSNorm, forward and backward (16 lanes per 128-wide row,shuffle_xorrow sums; the parameter gradients summed per lane and once on the host);conv_kernel.py: the short causal conv + SiLU.kda_backend: flahint: the bounded gate (kda_gate_lower_boundin[-5, 0)), head dim 128, sequence lengths that are multiples of 64, no initial / final state, no conv bias, no context parallelism (packed sequences already go through the per-sample path). Tuning values are fixed at their measured best.kda_backend: flagets three opt-in fields (all defaultfalse):kda_fla_fused_opsputs the L2 norm, gate and gated RMSNorm on fla's fused kernels (asmodels/hybrid/kimi_delta_attention.pydoes);kda_fla_safe_gateandkda_fla_disable_recomputeare passed to fla'schunk_kda(fla backend only;safe_gateneeds the bounded gate). Theflydslbackend does not read them.[b, s]copy of the input (every backend): without sequence or context parallelism,KimiDeltaAttentiontransposes the hidden-wide input to[b, s, h]once and runs the q / k / v / f_a / b / g projections on it, so their outputs are already in the kernels' layout; onlyo_proj's output is transposed back to[s, b, h]. With SP or CP the projections keep the[s, b, h]shard.kimi_delta_attention.py:_resolve_kda_opsresolves the L2 norm, gate, conv andchunk_kdaarguments once at construction;KimiGatedRMSNormand_short_convtake the selected kernels.examples/megatron/configs/MI455X/kimi_k3.1_pro_4L-BF16-pretrain.yaml:kda_backend/use_kimi_k3_attention_backenddefaultflydsl(PRIMUS_ATTN_BACKEND=flaswitches to fla,eagerto the reference), and the threekda_fla_*fields on for the fla backend.patches/fla_kda_autotune_patches.py: comment only (chunk_kda_fwd_kernel_intra_sub_chunkis reachable withsafe_gate).test_kda_flydsl_gfx1250.pyandtest_kda_fla_options.py(new; the latter skips without a GPU, astest_model_registry.pydoes).Results
All on one MI455X (gfx1250), branch head
9cf24cdd, imageamdprimus/amdprimus:tas-20261109for the kernel benchmarks andtas-20261019for training. Baseline: #1451's KDA path, the first row of each table; one GPU, so tokens/s is tokens/s/GPU.KDA core forward + backward (B 2, T 4096, 96 heads, K = V = 128, bf16; the same inputs for every arm; relative error against the first row):
chunk_kdadefaults)flawith the threekda_fla_*fieldsflydsl(this PR's recipe)The
flydslchunk step by step (forward + backward 2.50 ms through autograd, ms): cumsum 0.078, forward intra 0.307, state recurrence 0.206, output 0.185; backwarddAqk / dv0.160, state gradient 0.268,wy_dqkg0.733, intra 0.555, reverse cumsum 0.079.Training: the MI455X recipe cut to 3 layers, all KDA + dense MLP (no MoE / MLA;
--num_layers 3 --linear_attention_freq '[1]*3' --moe_layer_freq '[0]*3' --routed_expert_hidden_size None), EP 1, MBS 2, GBS 16 (8 micro-batches, as per rank in the recipe), seq 4096, mock data, 12 iterations, arms interleaved twice, all on this branch (the[b, s]projections apply to every arm):hip mem usagefla,kda_fla_*off (#1451's KDA kernels)fla,kda_fla_*onflydsl(this PR's recipe)flydslis 178 ms per iteration faster than the first row (-16%), 7.4 ms per KDA layer and micro-batch. Losses over iterations 1-12 match the first row within 7.6e-5 relative (flawith the fields: 6.4e-5); the two runs of an arm are bitwise equal.hip mem usageat iteration 12.[b, s]projections alone (flydsl, the same proxy, interleaved with the commit before): 967.6 -> 944.9 ms per iteration, 0.9 GB less memory, losses within 6.3e-5. The bf16dq/dkin the intra step:bwd_intra0.747 -> 0.556 ms, losses bitwise unchanged.Test
tests/unit_tests/backends/megatron/kimi_k3/on gfx1250 (MI455X): 87 passed, 26 of them new.test_kda_flydsl_gfx1250.py: theflydslchunk kernel againsteager_chunk_kda(two shapes) and against fla'schunk_kdawithsafe_gate/disable_recompute; the L2 norm, gate (with theA_log/dt_biasgradients), gated RMSNorm (fp32 and bf16 weight), both cumsums and the conv against their eager forms, in the output and every input gradient; a negatedbeta, a halved gate bound, a negated output gate and a dropped SiLU fail the same checks; unsupported inputs raise with thekda_backend: flahint; a run of every kernel in a fresh process imports no fla module.test_kda_fla_options.py: fla's fused ops andchunk_kdaoptions against the eager path, the config defaults and the option resolution.test_grouped_mlp.py,test_fla_kda_autotune_patches.py,test_mla_rope_qk_norm_patches.py,test_kimi_k3_quantile_balancing_patches.py: 75 passed (162 with the above).tests/unit_tests/configs/test_example_configs.py -k kimi: 5 passed.thirdparty-lock.flaand thekda_fla_*fields off, and gfx950'sflydslimplementation is unchanged.Not in this PR
chunk_kda_bwd_intrawith a wider K block for theflabackend.