Skip to content

Model L & M (D3D12): hybrid Swin/bneck/post/project speedups (-0.320 ms Model L at 1440p Q, byte-identical) and m_mode presets - #298

Open
phantoslayer21 wants to merge 9 commits into
mainfrom
perf/model-l-hybrid-speed
Open

phantoslayer21 wants to merge 9 commits into
mainfrom
perf/model-l-hybrid-speed

Conversation

@phantoslayer21

Copy link
Copy Markdown
Owner

Summary

Bit-exact D3D12 hybrid kernel speedups for Model L on RDNA 4 (gfx1201) plus Model M m_mode (fast|hybrid|exact) and native D3D12 q7c support, and updated clean/PERF.md hardware overlap notes.

Key Optimizations (all bit-exact, 0 byte diffs)

  • l_post.hlsl (-63.5 us, 0.9012 -> 0.8377 ms): exact reconstruction/history-encode optimizations.
  • Hybrid Swin blocks (swin_block.hlsl, fused_swin.hlsl, hyb_swin.hlsl, hyb_block.hlsli, wg_hyb.hlsli):
    • swin_block_c64_add_hyb (-61.7 us), swin_block_c64_hyb (-54.0 us), l_swin_block_c32_dec_coef12_hyb (-48.8 us), l_swin_block_c32_enc_lin_hyb (-37.6 us), swin_block_c96_hyb (-8.5 us), swin_block_c96_add_hyb (-4.3 us).
    • Tightened vgpr_limits.json caps (256 -> 192 VGPRs) for swin_block_c96_hyb, swin_block_c128_hyb, swin_bneck6_hyb, and *_q7c_hyb.
  • hyb_bneck.hlsl: enabled WG_LD_A16=1 on swin_bneck6_hyb (192/192 VGPRs, 0 spills, hyb_isa_scan pass, scanmut fails as required).
  • l_ops_project.hlsl: paired 2 output tiles (mt += 2u), coalesced wg_ld128/wg_st128 + WG_LD_A16=1 (l_dec_project -8.3 us, l_project_gather -3.9 us).
  • Model M m_mode / q7c (runtime.cpp, model.cpp, pipeline.py, equiv/model_m.py): adds m_mode=fast|hybrid|exact and SW_Q7_CHUNK (~-154 us faster Model M exact, 0 byte diffs across 78 dumps).
  • clean/PERF.md: documents cycle-verified gfx1201 WMMA vs VALU serialization and genuine SALU/VMEM/LDS/occupancy overlap.

Verification (RX 9070 XT gfx1201, 1440p Quality 1706x960 -> 2560x1440)

  • Model L paired A/B vs v0.6.5:
    • v0.6.5: 4.7426 ms median span (4.5465 min, 4.9566 profsum)
    • This PR: 4.4228 ms median span (4.2988 min, 4.6563 profsum) — -0.3198 ms span / -0.3003 ms profsum vs v0.6.5
  • Bit-exactness: 0 byte diffs across 6 frames (HDR + LDR + special).
  • Checks: hyb_isa_scan.py PASS (0 fused squares, 0 FP8 bad), *_scanmut FAIL as required, WG_DX12_VGPR_CHECK=1 PASS, test_equiv_model_m + test_model_select (21 tests) PASS, tools/ci/check_line_endings.py PASS.

…ms Model L at 1440p Q, byte-identical) and m_mode presets
…switch (default off)

Hybrid decoder blocks with their expand fused in (swin_block.hlsl SW_HYB with SW_ECL; hyb_swin.hlsl):
swin_block_c96_add_ex_hyb (dec2, SW_ECL 128) and swin_block_c64_add_ex_hyb (dec1, SW_ECL 96). The expanded input E is
computed per window into LDS with the hybrid FP8 point (hyb_q8_of_chain) and read through the coarse mirror (the
sw_coarse_src source of the unfused route), so expand_c128_hyb + swin_block_c96_add_hyb and expand_c96_hyb +
swin_block_c64_add_hyb become one launch each and the expanded map is never stored. 0 spills, 118 / 158 VGPRs, LDS
unchanged, hyb_isa_scan rules 1 and 2 pass, both *_scanmut mutants fail them. Runtime: WG_L_EXFUSE (default on when the
manifest has both kernels; 0 = the unfused route) launches them through the M-form fused names and the hyb_map twins.
test_hyb_fused_expand_addr.py checks the window / record addressing exhaustively (n = 16 .. 4100, offsets 0 and 2).

l_post.hlsl: LP_W64 entries w64_lc12/l_post_recon[_p12][_ldr] (64-lane waves, FP16 code table from f32(k) f32(1/255)
exact for all 256 codes, r_prev from the first lane, QuadReadAcrossX). The driver's matrix conversions are wave32-only,
so the wave64 form converts in software: RGA gfx1201 7605 -> 12922 instructions. Runtime WG_L_POST_W64=1 (default off,
a measurement switch). The lc12 entries are byte-identical DXIL to before.

Every kernel that includes a changed source was rebuilt: the 41 pre-existing ones have the same DXIL sha256 as before.
kernel_sets.json regenerated, vgpr_limits.json has the two new bounds, test_l_runtime_frame covers the fused route and
the toggle.
@j0nsh1n

j0nsh1n commented Nov 25, 2026

Copy link
Copy Markdown
Contributor
Build Whole-frame GPU time hipbench TOTAL Final output checksum
Before: main 0bf6aa4 4.811 ms 4.550 ms a424359bb2b40089
After: PR d58a6fa 4.425 ms 4.262 ms 70281b5c75b0f54f

Model L, 1440p Quality (1706x960 -> 2560x1440), RX 9070 XT on Linux. The PR is faster in both measurements, but its output is not byte-identical on RADV.

Timing

Our whole-frame host improved by 0.384 ms per frame and was faster in 9/9 interleaved rounds.

Your test_interop_d3d12 hipbench (executor=dx12, 1500 frames per round) improved by 0.289 ms, also in 9/9 rounds. Section changes were enc0 -0.122 ms, Swin -0.159 ms, dec0 +0.025 ms (slower in 9/9), and post -0.033 ms.

Profiled kernel changes were:

  • l_swin_block_c32_enc_lin_hyb -0.132 ms; swin_bneck6_hyb -0.053 ms.
  • swin_block_c64_add_hyb -0.052 ms; swin_block_c64_hyb -0.050 ms.
  • lc12/l_post_recon_p12 -0.045 ms; l_swin_block_c32_dec_coef12_hyb -0.043 ms.
  • swin_block_c96_hyb and swin_block_c96_add_hyb -0.020 ms each.

Exactness on RADV

Each build is deterministic run to run, but main and the PR differ. Across 31 stage buffers plus the output for three frames, at 64x40 -> 96x60, 77x45 -> 113x67, and 1440p Quality, only 38 of 93 dumped files matched main. The output image differs, as do the hipbench checksums above.

The first differing buffer is the c32 encoder block's map, written by l_swin_block_c32_enc_lin_hyb: 9.2% of its FP16 values differ, with a median difference of about 255 FP16 steps. That is more than one rounding.

Single-kernel and single-source-change diagnostic builds at 64x40 isolated the scope:

  • PR runtime C++ with all main kernels matched main 93/93. The runtime changes are byte-neutral.
  • PR with main's 15 hybrid network kernels, retaining the PR post/reconstruction kernels, also matched main 93/93. Thus the post and reconstruction kernels are exact on RADV, including l_post_recon_p12, whose ACO instruction count doubled.
  • Within the hybrid Swin kernels (12 rebuilt per test), one build without WG_LD_A16=1 and another without the four knobs SW_HEAD_UNROLL, SW_NORM1_EARLY, SW_COMMIT_EARLY and SW_MLP_STEP both left PR bytes unchanged. The 128-bit loads and those scheduling changes are byte-neutral.
  • Substituting main's hyb_norm for packed hyb_norm_h also left PR bytes unchanged.
  • Substituting main's float32 softmax-weight chain for the new FP16 hyb_softmax_pre (hyb_block.hlsli) changed the bytes, so hyb_softmax_pre changes results on RADV. It still did not reproduce main, so at least one more change in those kernels also differs. We stopped bisecting there.

A CPU check in IEEE arithmetic gives main's e from the new hyb_softmax_pre for all 63,488 finite FP16 scores. Our guess, not proven, is that RADV/ACO fuses the FP16 mad (uh * th + p0) into v_fmaak_f16 with one rounding, or that packed FP16 multiply/clamp behavior differs from the CPU model.

ACO registers

On gfx1201, RADV statistics show swin_block_c96_hyb, swin_block_c96_add_hyb, swin_bneck6_hyb, and swin_block_c128_hyb at 256 VGPRs with spills in both builds. Spilled VGPRs from main to PR are 2 -> 4, 2 -> 4, 26 -> 25, and 19 -> 13 respectively. The new 192 cap in vgpr_limits.json therefore does not hold under ACO, although the PR cuts these kernels' instruction counts by 5-9%. swin_block_c64_hyb and swin_block_c64_add_hyb remain at 192 VGPRs with no spills.

Setup: RX 9070 XT (gfx1201), D3D12 kernels through Wine + vkd3d-proton 5b1089c8 with dxil-spirv patches including the FMad NoContraction fix, RADV/ACO from Mesa git 1c3a936. Tests used synthetic Model L weights and synthetic frames.

Not tested: Windows, real model weights, RDNA 3, or Model M's new m_mode / q7c paths.

Two questions:

  1. On Windows, does hyb_softmax_pre's FP16 mad compile to a fused FMA too, or to multiply then add? Which result is intended to be exact?
  2. Should we keep bisecting on Linux for the second source, or do you want to look first? We can share the diagnostic builds' source patches.

…re-exposure division folded, wave64 hardware FP16 conversion

L-W2.1. One wave64 kernel of 512 lanes (kernels/l_post_tf.hlsl, tags w64_lc12/l_post_tf_p12 and _pf) replaces l_post_recon_p12 +
w64/post_filter_c1_s4 + scale_rgb (the display image is never written): Model L's per-pixel reconstruction (the new shared
l_post_core.hlsli, moved from l_post.hlsl unchanged: the wave32 DXILs / ISA of every l_post kernel are identical) inside Model M's
post_tf walk (ring of display rows in LDS, analytic Lanczos passes, Dec, store times dec_scale with scale_rgb's bits folded in).
With pi != 1 the staged route's division is folded into lev/input_encoder_q20_pf and the _pf post (wg_pidiv.hlsli, as Model M's
_pf kernels): no color_divide launch. Wave64 FP16 conversions are the driver channel's single-value hardware conversion (op 0x1C,
one v_cvt_f16_f32, as rr_d0r.hlsl): standalone w64_lc12/l_post_recon_p12 12916 -> 8443 instructions.
RGA gfx1201: l_post_tf_p12 4289 inst / 84 VGPR / 26752 B LDS / 0 spills; _pf 4916 / 84.
Runtime: WG_L_POST_TF (default on when the kernels are in the manifest; 0 = the separate launches), HDR staged route only.
Tests: FusedPost in test_l_runtime_frame.py; the other tests run the separate route (WG_L_POST_TF=0).
…locks, arithmetic GELU, wave64 projections (WG_L_W3)

Every lever is its own set of kernels beside the wave 2 ones (WG_L_W3 mask, default 0x17, 0 = wave 2), selected only when
the manifest has them; same arguments, grids and bytes.

- SW_DIRECT_W: the C = 32 / 64 hybrid blocks read their weight fragments straight from global memory (no LDS staging, no
  barriers; the C = 64 fused decoder block keeps only its 8 KB of expand records and 2 barriers).
- HYB_GELU_POLY: q8(GELU16(h)) as packed FP16 arithmetic (hyb_gelu_poly8) instead of the 128 KB table's gathers; exact for
  all 65,536 inputs (test_hyb_gelu_poly.py), both multiplies feeding an add behind the contraction barrier, pinned by FMA
  counts in isa_checks.json. Applies to C = 32, 64, 96 and the bottleneck.
- l_project_gather_w64 / l_dec_project_w64: wave64 design D, weights shared by both token tiles.
- l_swin / hyb_swin / hyb_bneck: *_w3, *_gp (ablation), *_w3p (c96 with the MLP-pairs knobs) and their _scanmut mutants.
- vgpr_limits.json, kernel_sets.json, build_dx12.py (is_model_l), fused_swin_gen.py anchor, runtime trace and frame tests.
…0 encoder at 96 VGPRs (WG_L_W4)

Each lever is its own set of kernels beside the wave 3 ones (WG_L_W4 mask, default 0x3, 0 = wave 3 frame), selected only when the
manifest has them; same arguments, grids and bytes. Every wave 2 / 3 kernel's DXIL is unchanged (new defines LP_W4 / ENC_W4).

- l_post_tf_p12[_pf]_w4 (bit 0): history_cr's six span loads in one burst (LP_HSEQ 0): RGA 86 VGPRs (84 before), inside the 96 that keep
  the LDS-capped 2 workgroups per CU. _w4m (bit 2): the motion loads issued ahead of the last gather column (96 VGPRs).
  Ablations that RGA showed as no-ops are knobs, off: uint2 staging (LP_W4_LDS2), h(uu kA) shared by both rows, half2 sums, unrolled
  column loop (the driver already merges the dwords and shares the product).
- lev/input_encoder_q20[_pf]_w4 (bit 1): 96 VGPRs (104 / 106 before: the 96-register tier): the fourth history position's loads after
  the first three's values, the uniform loads through readfirstlane, slot 0's stores before slot 1.
- vgpr_limits.json / build_dx12.py (vgpr_checked), kernel_sets.json, runtime WG_L_W4, trace signatures and frame tests.
… wins (WG_L_W3 = 0, WG_L_W4 = 0x1)

Every lever stays selectable by its mask bit; only the defaults change. Byte-exact in every configuration: 10080 buffers
(small, 1705x960 -> 2560x1440, 1706x960 -> 2560x1440; pre-exposure 1, 1.25, 2, 1e38; HDR and LDR; reset and non-reset
frames) identical to WG_L_W3=0 WG_L_W4=0 for masks 0x17/0x03, 0x37/0x07 and 0x08/0x00, and 3360 buffers again with the
rebuilt default library.

Paired ABBA, 8 rounds, 1440p Quality, pre-exposure 1.25, unprofiled frame span (us vs W3=0 W4=0, 4486 us):
  W3 0x01 c32 direct weights + poly GELU  +111    W3 0x08 c32 poly GELU only  +96
  W3 0x02 c64 direct weights + poly GELU   +51    W3 0x04 c96 + bneck6 poly   +56
  W3 0x24 c96 _w3p + bneck6                +47    W3 0x10 wave64 projections  +18
  W4 0x01 post (history burst)              -8    W4 0x05 post + motion       +17
  W4 0x02 q20 encoder at 96 VGPRs           +6    default 0x17 / 0x03        +244
The per-dispatch GPU profile confirms the routed kernels ran; the poly-GELU and direct-weight forms cost more than the
LUT/LDS forms they replace on gfx1201 (c32 blocks 746.7 -> 818.6 and 706.9 -> 801.8 us). Only W4 0x01 is faster
(w64_lc12/l_post_tf_p12_pf 896.4 -> 885.8 us); 0x02 is neutral (190.3 -> 190.2 us).

Confirmation, 10 rounds, W4 0x1 vs 0: -10.4 us at pre-exposure 1.25, -8.2 us at 1.0 (W4 0x3: -11.0 / -2.7, no better).
Scaling at pre-exposure 1.25, 6 rounds, paired vs W3=0 W4=0 (final default; the old 0x17/0x03 default):
  1080p Q 2644.5 -> 2642.4 (-1.3; +135.5)   1440p Perf 3007.0 -> 2999.7 (-8.6; +133.8)   1440p Q 4483.6 -> 4477.0 (-4.8; +244.8)
  1440p DLAA 9382.1 -> 9374.5 (-7.3; +560.1)   4K Q 10060.2 -> 10052.1 (-8.2; +532.6)

Tests follow the new defaults (WG_L_W3 unset = the wave 2 kernels, WG_L_W4 unset = the post only).
…n comments, prune Wave 3/4 dead variants

Fixes two RADV/vkd3d-proton/ACO bit-exactness divergences in the hybrid Swin blocks (l_swin_block_c32_enc_lin_hyb and siblings) while keeping VMEM/LUT latency hiding intact:
- hyb_block.hlsli (hyb_softmax_pre): restore compiler-proof FP32 hyb_softmax_e8(hyb_round16(s)) instead of FP16 mad(half2) (which splits under vkd3d-proton FMad NoContraction) and FP16 score scaling/clamping, keeping the HybSmaxPre / hyb_softmax_fin split to hide hyb_rcp16 LUT latency behind Wv.
- swin_block.hlsl (norm2 b2 add, plain hybrid and SW_Q7_CHUNK): restore FP32 add + AGS RN16 (hyb_add16 / hyb_rn16x8) instead of fptrunc + fadd half.
- Clean stale Matrix || Vector overlap comments across swin_block.hlsl, fused_swin.hlsl, and l_ops_project.hlsl (RDNA 4 has 0% WMMA+VALU overlap; the gain is VMEM/LUT latency hiding).
- Prune regressive Wave 3 levers (WG_L_W3, _hyb_w3, _hyb_w3p, _hyb_gp, l_ops_project_w64.hlsl, SW_DIRECT_W, HYB_GELU_POLY) and neutral/regressive Wave 4 variants (ENC_W4, LP_W4_MOT, LP_W4_LDS2, LP_W4_H2), making the winning LP_HSEQ=0 burst the default in w64_lc12/l_post_tf_p12[_pf].
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.

2 participants