Skip to content

[FlyDSL] [Perf] Fuse Engram TP gate/residual and tune WKV GEMMs on gfx950 - #6994

Closed
junhaha666 wants to merge 6 commits into
mainfrom
jun/dsv41_wkv
Closed

junhaha666 wants to merge 6 commits into
mainfrom
jun/dsv41_wkv

Conversation

@junhaha666

@junhaha666 junhaha666 commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

DeepSeek-V4.1 Engram's sharded WKV can spend more time launching separate gate, all-gather and residual kernels than it saves in the GEMM at small M. Add a gfx950 FlyDSL kernel and HIP IPC workspace that combine normalized weighted dot, signed sqrt, sigmoid, gate exchange and the complete residual update in one launch. The companion ATOM integration selects it for 13..128 token rows.

FusedEngramPost(group) supports TP2/TP4/TP8 with BF16 H/KV and FP32 gate weights. It uses two sequence-tagged IPC message slots, bounds peer polling and exposes .check() for tests/debug outside graph capture. Initialize eagerly, warm each shape before capture, keep the workspace alive with its graphs, and invoke matching shapes in the same serialized order on all ranks. TP8 first combines half-Key statistics across paired ranks. The wrapper supports at most 128 rows; larger shapes use the companion composed path.

The tuning CSV retains the existing sparse TP2/TP4 and full-WKV configurations:

  • KV prefix: N=12800/6400, K=6144; M=1,2,4,8,16. M=9..12 resolves to the tuned M=16 entry through padded-M lookup.
  • Local Key/complete V: N=15360/10240, K=6144; 39 M points per shape: 1,2,4,8,16; 32..256 by 16; 288..512 by 32; then 768,1024,1536,2048,4096,6144,8192,10240,12288,14336,16384.
  • Full WKV: N=25600, K=6144; adds M=3072 and updates 12 configurations between M=512 and 16384. Overall tuning changes retain 89 added shapes and 12 updated configurations versus the original PR base, with no baseline shape removed.
  • The 14 experimental TP8 N=7680/3200 entries have been removed. The final CSV is byte-identical to the preceding PR head; TP8 functional support remains without dedicated tuning.

Companion ATOM integration: ROCm/ATOM#2748

All ranks must hold complete H. ATOM consults mono's token-parallel policy before selecting the weight layout: TP2 mono keeps replicated H, while a deployment configured for TP4 mono token partitioning retains full WKV. The TP4 results below use replicated H.

Two weight-sharding routes, one stored WKV weight/scale pair

M counts all physical token rows, including graph padding. The diagrams show TP2/TP4 (P=2 or 4) with replicated input on every rank: embedding X=[M,6144], residual H=[M,4,5120], both BF16. The FP32 gate weight is [4,5120].

1. M <= 12: shard Keys and Value, then gather KV

flowchart TD
    X["X: BF16 [M,6144]"] --> Q["Input quantization: FP8 1x32"]
    W["Stored WKV: FP8 [20480/P+5120,6144]<br/>local Keys + rotated complete V"] --> PREFIX["Prefix view: [25600/P,6144]<br/>same weight and scale storage"]
    Q --> GEMM["Tuned FP8 GEMM"]
    PREFIX --> GEMM
    GEMM --> KV["Local KV: BF16 [M,25600/P]"]
    KV --> AG["aiter custom all-gather, dim=1"]
    AG --> FULL["Gathered KV: BF16 [M,25600]<br/>rank-grouped K0,V0,K1,V1,..."]
    FULL --> POST["Triton post: normalized weighted dot<br/>signed sqrt, sigmoid, residual update"]
    H["Replicated H: BF16 [M,4,5120]"] --> POST
    POST --> O["Complete H_new on every rank<br/>BF16 [M,4,5120]"]
Loading

2. M > 12: shard Keys, compute complete V locally, exchange gates

flowchart TD
    X["X: BF16 [M,6144]"] --> Q["Input quantization: FP8 1x32"]
    W["Same stored WKV: FP8 [20480/P+5120,6144]"] --> GEMM["Tuned FP8 GEMM"]
    Q --> GEMM
    GEMM --> KV["Local KV: BF16 [M,20480/P+5120]<br/>Keys [M,4/P,5120], complete V [M,5120]"]
    KV --> SELECT{"Physical token rows M"}
    SELECT -->|13..128| FUSED["One aiter FlyDSL kernel<br/>local normalized dot, signed sqrt, sigmoid<br/>IPC exchange of FP32 gates, then H + gate * V<br/>logical gates [M,4/P] to [M,4]"]
    SELECT -->|greater than 128| GATE["Local gate kernel<br/>FP32 [M,4/P]"]
    GATE --> AG["aiter custom all-gather, dim=0<br/>physical [P*M,4/P], logical [P,M,4/P]"]
    AG --> RES["Residual kernel: H + gate * V<br/>read rotated V columns directly"]
    KV --> RES
    H["Replicated H: BF16 [M,4,5120]"] --> FUSED
    H --> GATE
    H --> RES
    FUSED --> O["Complete H_new on every rank<br/>BF16 [M,4,5120]"]
    RES --> O
Loading

Key selection, V rotation, scale selection and preshuffling happen at load time. The short prefix is a view of the only stored weight/scale pair. Neither route adds a runtime V packing/copy or V all-gather in the complete-V path.

TP Stored WKV weight Small-M prefix FP8 weight storage per rank/layer
2 [15360,6144] [12800,6144] 90 MiB
4 [10240,6144] [6400,6144] 60 MiB
8 [7680,6144] [3200,6144] 45 MiB

Full WKV is [25600,6144], or 150 MiB per rank/layer, excluding scales. For TP2/TP4, the small-M KV contribution is 51200/P bytes per token/rank; the gate contribution is 16/P bytes. These are logical payload sizes, not measured link traffic.

TP8 functionality is retained, but no dedicated TP8 GEMM tuning entries or TP8 performance claim are included. Each TP8 rank owns half a Key (2560 elements). The fused kernel first exchanges FP32 sum(H^2), sum(K^2) and weighted-dot statistics with rank ^ 1, then exchanges the four completed gates within the same-parity ranks. The composed TP8 fallback gathers [M,3] statistics per rank before the residual update. TP8 GEMMs use the existing fallback selection after removing the experimental N=7680/3200 configurations.

The unified upper fusion cutoff is 128. Boundary measurements found TP2's benefit ended at 128, while TP4 could extend to 256; the shared rule keeps dispatch simple and avoids the measured TP2 regression above 128. M<=12 keeps the KV-prefix route. If the FlyDSL path is unavailable, the composed post path remains in use.

Performance: full WKV, previous PR remeasured, and current dispatch

The following 2026-10-28 measurements replace the previous published tables. All three columns use the same AITER graph/profiler method; the previous PR column is a fresh remeasurement, not its historical event-timed value. These are combined ATOM + aiter WKV results, not the isolated gain from GEMM tuning.

  • gfx950, 256 CUs, container jun_atom2. TP2 used GPUs 4/5, then TP4 used GPUs 0-3 sequentially. Host KFD monitoring at 0.5-second intervals found no foreign GPU allocation during either run (351/415 samples).
  • Real DeepSeek-V4.1-Flash layer 1/14 weights, random BF16 X/H, with H complete and replicated on all ranks. Sixteen weight allocations rotate at every M, including 16384. Full, previous and current use tuned GEMMs; previous/current share the same prepared weight/scale/gate tensors and addresses.
  • Unmodified aiter.test_common.run_perftest(testGraph=True), five rounds and 129 calls per round; CUDA-event timing is disabled. Choose the slowest rank in each round, then the median round. Per-op values use the same selected rank/round as their total.
  • Times are AITER-filtered GPU kernel-time sums per complete WKV call: input quantization + GEMM + actual collective + gate + residual through full H_new. Kernel gaps, embedding lookup, surrounding mHC and other model layers are excluded. This is not model E2E latency or throughput.
  • Current calls production EngramOp.forward: M<=12 KV gather, 13..128 fused post, M>128 composed gate/gather/residual. The relevant 157 TP2/TP4 tuning rows are byte-identical between previous and current, and actual GEMM configuration matches at every measured shape.
  • Reduction is 100 * (reference - current) / reference; positive means less GPU time. Times below are microseconds.

The fused interval M=13..128 reduces time versus the previous PR by 8.33%-16.50% at TP2 and 4.66%-14.62% at TP4. Outside that interval the route is unchanged; differences remain within +/-0.94% in this run and are not attributed to a new algorithmic improvement.

TP2

M Current route Full WKV Previous PR (remeasured) Current Reduction vs full Reduction vs previous PR
1 KV gather 34.542 28.443 28.429 +17.70% +0.05%
4 KV gather 34.590 29.569 29.562 +14.54% +0.02%
8 KV gather 34.669 31.469 31.473 +9.22% -0.01%
12 KV gather 34.806 33.338 33.353 +4.17% -0.05%
13 FlyDSL fused 34.890 35.350 29.517 +15.40% +16.50%
16 FlyDSL fused 34.938 35.497 29.660 +15.11% +16.44%
32 FlyDSL fused 36.037 37.404 31.637 +12.21% +15.42%
64 FlyDSL fused 40.279 39.582 34.354 +14.71% +13.21%
128 FlyDSL fused 49.956 46.137 42.293 +15.34% +8.33%
256 gate gather 75.801 62.629 62.847 +17.09% -0.35%
512 gate gather 104.574 90.051 90.044 +13.89% +0.01%
1024 gate gather 190.472 126.641 127.517 +33.05% -0.69%
2048 gate gather 356.156 238.857 237.236 +33.39% +0.68%
4096 gate gather 639.489 446.074 448.881 +29.81% -0.63%
8192 gate gather 1220.040 889.001 883.030 +27.62% +0.67%
16384 gate gather 2394.451 1698.203 1684.194 +29.66% +0.82%

TP4

M Current route Full WKV Previous PR (remeasured) Current Reduction vs full Reduction vs previous PR
1 KV gather 34.339 23.204 23.130 +32.64% +0.32%
4 KV gather 34.517 23.746 23.739 +31.23% +0.03%
8 KV gather 34.624 25.105 24.912 +28.05% +0.77%
12 KV gather 34.769 26.101 26.081 +24.99% +0.08%
13 FlyDSL fused 34.946 30.610 26.135 +25.21% +14.62%
16 FlyDSL fused 34.896 30.701 26.271 +24.72% +14.43%
32 FlyDSL fused 36.449 32.493 28.159 +22.74% +13.34%
64 FlyDSL fused 39.913 36.502 32.044 +19.72% +12.21%
128 FlyDSL fused 50.144 41.780 39.834 +20.56% +4.66%
256 gate gather 75.700 56.419 56.176 +25.79% +0.43%
512 gate gather 105.286 77.461 77.035 +26.83% +0.55%
1024 gate gather 190.930 111.837 112.610 +41.02% -0.69%
2048 gate gather 359.973 205.019 204.241 +43.26% +0.38%
4096 gate gather 647.557 346.937 347.870 +46.28% -0.27%
8192 gate gather 1226.632 635.431 634.114 +48.30% +0.21%
16384 gate gather 2395.903 1269.801 1281.702 +46.50% -0.94%
Per-op GPU times for the current dispatch (microseconds)

Gate/post is the full post-WKV kernel for the KV-gather route, and the local gate kernel for the composed route. The FlyDSL column includes gate computation, peer exchange and residual update. A zero means no separate kernel is launched. Rounding can affect the sum of displayed values.

TP M Quant GEMM Gate/post Gather Residual FlyDSL post Total
2 1 4.201 15.215 4.410 4.604 0.000 0.000 28.429
2 4 4.194 15.327 4.257 5.784 0.000 0.000 29.562
2 8 4.181 15.481 4.313 7.498 0.000 0.000 31.473
2 12 4.238 15.446 4.402 9.267 0.000 0.000 33.353
2 13 4.197 18.218 0.000 0.000 0.000 7.103 29.517
2 16 4.205 18.239 0.000 0.000 0.000 7.216 29.660
2 32 4.464 18.511 0.000 0.000 0.000 8.662 31.637
2 64 4.403 21.412 0.000 0.000 0.000 8.540 34.354
2 128 4.769 27.131 0.000 0.000 0.000 10.394 42.293
2 256 4.962 41.052 4.927 5.085 6.821 0.000 62.847
2 512 4.982 61.706 5.819 5.772 11.765 0.000 90.044
2 1024 7.400 82.546 8.756 7.158 21.657 0.000 127.517
2 2048 14.074 158.536 16.974 5.052 42.600 0.000 237.236
2 4096 25.411 297.413 33.867 5.594 86.597 0.000 448.881
2 8192 45.118 575.841 76.790 19.932 165.348 0.000 883.030
2 16384 90.441 1105.719 140.361 17.475 330.198 0.000 1684.194
4 1 4.228 9.550 4.341 5.010 0.000 0.000 23.130
4 4 4.148 9.508 4.266 5.817 0.000 0.000 23.739
4 8 4.199 9.635 4.175 6.903 0.000 0.000 24.912
4 12 4.268 9.866 4.391 7.556 0.000 0.000 26.081
4 13 4.203 13.285 0.000 0.000 0.000 8.646 26.135
4 16 4.168 13.391 0.000 0.000 0.000 8.712 26.271
4 32 4.226 14.869 0.000 0.000 0.000 9.065 28.159
4 64 4.304 18.024 0.000 0.000 0.000 9.716 32.044
4 128 4.400 22.798 0.000 0.000 0.000 12.636 39.834
4 256 4.613 35.283 4.384 5.428 6.468 0.000 56.176
4 512 4.495 51.277 4.366 5.598 11.299 0.000 77.035
4 1024 7.145 70.796 5.642 7.777 21.250 0.000 112.610
4 2048 13.846 132.073 8.661 6.828 42.832 0.000 204.241
4 4096 23.877 209.130 18.045 10.823 85.995 0.000 347.870
4 8192 45.514 371.931 37.059 11.748 167.862 0.000 634.114
4 16384 85.795 743.951 82.682 40.811 328.462 0.000 1281.702

Validation

  • Fresh ATOM checks: 255 passed across test_wkv_tp.py, test_gate.py, test_import_boundary.py, mono dispatch and V4.1 compilation tests. These cover loading, single-weight storage, prefix dispatch, masks, fallbacks and choosing the mono-compatible layout before loading. Native TP8 projection checks also passed after removing the TP8 tuning entries.
  • The performance sweep completed 15,360 previous/current-vs-full output comparisons, all within tolerance. TP2: 5,120 comparisons, 4,320 bitwise equal; TP4: 10,240 comparisons, 8,640 bitwise equal. Maximum NRMSE 1.8203815e-5, maximum absolute difference 0.015625, zero outliers beyond the per-element rtol=atol=0.01 criterion. An additional 7,680 full-baseline self-checks are excluded from that comparison count.
  • Standalone aiter multi-GPU fusion validation passed TP2/TP4/TP8 with 384/768/1,536 comparisons against an independent FP32 reference. It covers changing graph inputs and masks, four calls per captured graph, a delayed rank, and signed-int32/uint32 epoch wrap. Maximum NRMSE is 1.53193e-5 (TP2/4) and 1.41677e-5 (TP8); no IPC timeout flags. Kernel source is unchanged since those runs.
  • Ruff, Black and git diff --check passed for the changed code. The tested production source hashes match the committed implementation.
  • These results validate the WKV subgraph. Full-model GSM8K and E2E serving were not rerun for this fusion. In particular, the TP4 table assumes replicated H and does not represent mono token-partitioned execution.

The aiter multi-GPU regression can be run on an idle gfx950 node with torchrun --standalone --nproc_per_node=2 op_tests/multigpu_tests/test_flydsl_engram_post_wkv.py; use 4 or 8 processes for the other TP sizes.

Source provenance

  • Previous PR baseline: ATOM 05efed0f180c3edae5455d0267048f506eefcffd, aiter 6f5897e4ff44b13bbc6d18f9ecf835bacd58185c. The Engram layer/gate were loaded from those ATOM git objects; only the test-local gate import was redirected. Both versions use the same common linear/GEMM/communication backend.
  • Current committed implementation: ATOM e26bcd28b0a213437aa6bf6d496fe2e90967f00b, aiter 67a0f379dbabb32b2b379637e068d50ffd5c8497.
  • Current tuning CSV SHA256: 7dc75b0672b81fd126ce235a65f6a97a4697bc8c34ebf9189e175d88db2ea9c7, byte-identical to the previous aiter PR head. Removing 14 experimental TP8 configurations does not alter any TP2/TP4 measurement above.

Add gfx950 K6144 native FP8 32x32 configurations for N10240 and N15360
used by TP4 and TP2 gate-only all-gather. Include tuned full N25600
baseline configurations at larger token counts through M16384.
@junhaha666
junhaha666 requested a review from a team October 27, 2026 06:35
@github-actions github-actions Bot changed the title [Perf] Tune DeepSeek V4.1 Engram WKV GEMMs on gfx950 [Config] [Perf] Tune DeepSeek V4.1 Engram WKV GEMMs on gfx950 Oct 27, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
ci:windows Windows JIT op smoke test on the gfx1201 ROCm runner
ci:multi-gpu Aiter multi-GPU tests on the 8-GPU runner
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 6994 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to stop the title rewrites; labels stay in sync either way.

@github-actions github-actions Bot changed the title [Config] [Perf] Tune DeepSeek V4.1 Engram WKV GEMMs on gfx950 [FlyDSL] [Perf] Tune DeepSeek V4.1 Engram WKV GEMMs on gfx950 Oct 28, 2026
@github-actions github-actions Bot added FlyDSL and removed Config labels Oct 28, 2026
@junhaha666 junhaha666 changed the title [FlyDSL] [Perf] Tune DeepSeek V4.1 Engram WKV GEMMs on gfx950 [Perf] Fuse Engram TP gate/residual and tune WKV GEMMs on gfx950 Oct 28, 2026
@github-actions github-actions Bot changed the title [Perf] Fuse Engram TP gate/residual and tune WKV GEMMs on gfx950 [FlyDSL] [Perf] Fuse Engram TP gate/residual and tune WKV GEMMs on gfx950 Oct 28, 2026
@junhaha666 junhaha666 closed this Oct 28, 2026
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