Skip to content

Model L D3D12: encoder block in reverse workgroup order (byte-identical, -13/-34 us at 1080p/1440p) - #168

Merged
phantoslayer21 merged 1 commit into
mainfrom
model-l/pix-order
Nov 14, 2026
Merged

phantoslayer21 merged 1 commit into
mainfrom
model-l/pix-order

Conversation

@phantoslayer21

Copy link
Copy Markdown
Owner

Refs #89

Model L's D3D12 encoder block l_swin_block_c32_enc_nc now runs its workgroups in reverse order. The bytes it writes are unchanged and so is the arithmetic: only the order in which the workgroups pick up their work changes.

  • wg_platform.hlsli: a kernel declared with WG_REV_ORDER=1 has workgroup g do the work of workgroup G - 1 - g, where G is the launch's whole grid, so split dispatches are covered. Without the define the code is exactly as before.
  • l_swin.hlsl: WG_REV_ORDER=1 on l_swin_block_c32_enc_nc only. That kernel is Model L-only; no Model M kernel changes.

Cause (measured before the fix)

The 1440p superlinearity in l_project_gather comes from cache order at two hand-offs:

  • x2: the fused linears → the encoder block;
  • x3: the encoder block → the gather.

The fused linears write x2 in forward order. The reversed block reads the last-written x2 rows first, and the x3 rows it writes last are the ones the gather reads first.

Order experiments (records order-<variant>-<shape>-ab.json, 2 rounds each):

  • Reversed encoder block (this PR): best of all variants.
  • Reversed gather: −26 µs at 1440p, smaller than this PR.
  • Reversed block and gather: the two reversals cancel at x3.
  • Reversed linears: worse, +23 µs at 1080p and +33 µs at 1440p.
  • Fold reversed on top of this PR: no effect.

The extra 1440p cost of l_linear_32_32_fused is not fixable by order. x2 is 107 MB at 1440p, more than the 64 MB last-level cache.

Timing

Synthetic models; main (A) against this branch (B), interleaved; 3 rounds; each run in its own GPU slot. Values are medians in ms. Records: encrev-1080p-quality-ab.json and encrev-1440p-quality-ab.json, each with its per-run records.

1080p Quality A 1080p Quality B 1440p Quality A 1440p Quality B
l_swin_block_c32_enc_nc 0.2722 0.2596 0.4766 0.4668
l_project_gather 0.0544 0.0582 0.1341 0.1073
encoder step (4 kernels) 0.5515 0.5385 1.0337 0.9993
frame span, unprofiled 2.0812 2.0749 3.5390 3.4892

So the encoder step is 13 µs faster at 1080p and 34 µs faster at 1440p. All three 1440p rounds of B beat all three of A, in both the encoder step and the frame span.

Tested

  • DXILs: full build_dx12.py --model-l build, all 62 compared against main's build. Only l_swin_block_c32_enc_nc.dxil differs, so every Model M DXIL is unchanged.
  • Kernel check: l_d3d12_check.py --kernels l_swin_block_c32_enc_nc against HIP. 28/28 cases byte-identical, specials included.
    • Negative control: half grid on l_swin_block_c32_enc_nc_s0_0, caught (768 bytes differ, 768 sentinel bytes).
    • Model M control swin_block_c96_nc: 16/16.
    • Record encrev-identity.json.
  • Frame check on main's raw machinery (l_frame_check: byte identity on raw defined bytes, per-run sentinels, missing outputs fail #165): l_frame_check.py --run --backend d3d12 / d3d12-stages, then --compare-d3d12, both models, 11 frames, pass. Record encrev-framecheck-raw-d3d12.json.
    • D3D12 per stage against the reference: 11/11.
    • Default kernels against that run (raw bytes): 11/11 identical.
    • Model M D3D12 against HIP (raw bytes): 11/11.
    • Negative controls (history, reduced, reduced_frame, missing): all caught.
    • Model L's D3D12-vs-HIP line is unchanged from main.
  • Frames against main: every recorded D3D12 array, both models, both configurations, 11 frames, compared byte for byte: 0 differing (638 arrays, 50 MB per run). Record encrev-frames-vs-main.json.
  • Unit test: test_kernel_sets.py.
  • Gates: public_gates.py passes; the LOOK lines are at baseline.

Not tested

  • HIP: unchanged, since the HIP kernels don't use this header.
  • Real models and real game frames.
  • Resolutions other than 1080p and 1440p Quality.

…_REV_ORDER; the same bytes)

wg_platform.hlsli: a kernel declared with WG_REV_ORDER=1 has workgroup g do the work of workgroup G - 1 - g. The
encoder block then reads first the rows of x2 the fused linears wrote last, and the rows of x3 it writes last are the
ones l_project_gather reads first: both hand-offs hit the caches more often. Only l_swin_block_c32_enc_nc's DXIL
changes; its outputs are byte-identical.

Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com>
@phantoslayer21
phantoslayer21 marked this pull request as ready for review November 14, 2026 13:48
@phantoslayer21
phantoslayer21 merged commit 18bbd98 into main Nov 14, 2026
9 checks passed
@phantoslayer21
phantoslayer21 deleted the model-l/pix-order branch November 14, 2026 14:01
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.

1 participant