Skip to content

Model K on D3D12: the narrow network stages, byte-identical to HIP - #204

Merged
phantoslayer21 merged 3 commits into
mainfrom
k/d3d12-narrow
Nov 14, 2026
Merged

phantoslayer21 merged 3 commits into
mainfrom
k/d3d12-narrow

Conversation

@phantoslayer21

@phantoslayer21 phantoslayer21 commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Refs #15. Stacked on #201 (and through it #198, #194): the diff is against that branch.

Preset K's narrow network stages on D3D12, byte-identical to their HIP kernels on the exact FP32 path. Not ported, on purpose: the FP16-accumulating and FP8 forms of the stage kernels (the approximation modes); they stay HIP-only and opt-in.

What changes

  • clean/k/dx12/kernels/k_block.hlsl and headers hlsl/k_block.hlsli, k_common.hlsli, k_blkparams.hlsli, k_norm.hlsli: the k_block template with the instantiations of k_block.hip:
    • one window per workgroup: k_stage_e2, k_stage_e0, k_stage_e1, k_stage_d2, k_stage_d1;
    • two windows per workgroup: k_stage2_e0, k_stage2_e1, k_stage2_d1;
    • the network's d0 forms with the hidden history (spec 43 6.8) and, for _hp, the compact pixel head: k_stage2_d0_hp, k_stage2_d0_hidden.
      Argument KBlkParams by value as s184:p19.b32 (the struct argument of Model K on D3D12: --model-k, struct arguments, the port check, k_expo_scalars and k_warm #201); the d0 kernels take the hidden pointer too.
  • kernel_sets.json: ten kernels in model_k.
  • k_port_check.py family narrow, k_plan_bridge.py (new): the stage runner's launch plans (k_stages.prepare, the same plans the HIP tests use) are run on HIP and replayed on D3D12 on the same input bytes. kernel_replay chains take pointers into a buffer (@id+bytes) and null arguments.
  • clean/k/tests/test_k_plan_bridge.py: CPU test of the bridge (pointer fields, buffer and offset, chain steps).

Tested

k_port_check.py --run --family narrow --seeds 2, one GPU slot, random stage weights (random_stage_params) and inputs, six block-level grids per kernel (8 x 8 to 40 x 24, odd sizes, fewer windows than a workgroup's among them) with window offsets from {0, 2, 4, 6}:

  • 120 of 120 cases identical to HIP over the defined bytes, 12 per kernel for all ten kernels; every output buffer has 64 slack bytes that kept their run's sentinel (HIP 0xA5, D3D12 0x5A); the part of a buffer beyond what the kernel defines (the head buffer of _hp) kept it too.
  • Special values: 40 cases carry infinity, +-65504, zero, -0, subnormals in the inputs (raw bytes identical in all 40); 40 more carry NaN as well: raw bytes identical in 31 of them, and in 9 cases (decoder stages) 21 elements are NaN on both devices but with a different sign. IEEE 754 does not define a NaN's sign; every other byte is identical. The check reports both counts.
  • Negative control (half grid: the stage launched with half its workgroups) caught, 22528 bytes differ.
  • Control, Model M scale_rgb through the same replay: identical.
  • Record: pr6-narrow.json (counts and verdicts only).
  • CPU: test_k_plan_bridge (4), test_kernel_sets, test_struct_args pass; line endings clean.
  • DXILs: no Model M or Model L source or build code changed in this PR (the build part is Model K on D3D12: --model-k, struct arguments, the port check, k_expo_scalars and k_warm #201's, whose 12-kernel sample was identical to main's).
  • public_gates.py: must-pass gates pass; the LOOK counts equal main's.

Not tested

  • The full Model M plus Model L DXIL set.
  • Whole network frames on D3D12 (the runtime does not select these ports yet; a later PR).
  • Real model weights.

Accepted exceptions (review decision, 2026-11-14)

  • NaN sign: with NaN in the inputs, the decoder stages produce NaN at exactly the same elements on D3D12 and HIP, but the NaN sign can differ. Every non-NaN byte is identical. A NaN sign carries no value (NaN propagates), so this is accepted as a documented exception to raw-byte identity; the port check keeps counting these elements separately.
  • FP32 denormals: D3D12 flushes FP32 denormals where HIP keeps them, the same accepted behaviour as the Model M ports. The check inputs avoid denormals.

…cal to HIP

Refs #15. Ports of k_stage_e2, k_stage_e0, k_stage_e1, k_stage_d2, k_stage_d1 and the two-window forms k_stage2_e0, k_stage2_e1, k_stage2_d1, plus the network's d0 forms k_stage2_d0_hp and k_stage2_d0_hidden (hidden history and compact pixel head): the exact FP32 path only; the FP16-accumulating and FP8 forms are not ported. The port check runs the stage runner's launch plans on HIP and replays them on D3D12 (k_plan_bridge.py); kernel_replay chains take offset pointers and null arguments.

Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com>
A replay output of an earlier run could stand in for one that a failed replay did not write; each run now has its own folder, and a failing replay fails the run.

Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com>
@phantoslayer21
phantoslayer21 marked this pull request as ready for review November 14, 2026 22:08
Base automatically changed from k/d3d12-base to main November 14, 2026 22:35
@phantoslayer21
phantoslayer21 merged commit 0eb6eac into main Nov 14, 2026
9 checks passed
@phantoslayer21
phantoslayer21 deleted the k/d3d12-narrow branch November 14, 2026 22:35
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