From 82aff2d9c2a84fb8782819bc25991a05b972b01c Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Sat, 14 Nov 2026 06:19:17 -0500 Subject: [PATCH] Model L on D3D12: the frame blocks' GELU table with the cheaper offsets for every tile l_swin_block_c32_enc_nc and l_swin_block_c32_dec_coef12_nc read q8(GELU) from the arena table with sw_gelu_fetch8_bn's offsets for every hidden tile (swin_block.hlsl SW_GELU 3, pattern 13333) instead of wg_gelu_lut_fetch8's (SW_GELU 1): the same entries, the same bytes (HIP's transcendental chain + pack), fewer instructions. 95 / 96 VGPRs (bound 96; patterns with a transcendental-chain tile need 108 .. 112). l_swin_block_c32_dec_nc (the fallback route) keeps SW_GELU 1. - clean/l/runner/l_d3d12_check.py: every other encoder / decoder block case with large inputs and special values (FP16 NaN, +-infinity, +-65504, +-0, subnormal; FP8 NaN, +-448, +-0, subnormals). Refs #89 Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> --- clean/dx12/kernels/l_swin.hlsl | 14 ++++++++------ clean/l/runner/l_d3d12_check.py | 25 +++++++++++++++++++++---- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/clean/dx12/kernels/l_swin.hlsl b/clean/dx12/kernels/l_swin.hlsl index fbd7e187..2242051b 100644 --- a/clean/dx12/kernels/l_swin.hlsl +++ b/clean/dx12/kernels/l_swin.hlsl @@ -1,20 +1,22 @@ // SPDX-License-Identifier: MIT // D3D12 ports of Model L's C = 32 Swin blocks (clean/l/kernels/l_swin.hip, l_swin.co; issue #89): swin_block.hlsl // instantiated at C = 32, 2 heads, MLP 128 (the same template as the HIP kernels'), with HIP's MLP unit cut (HPU 8 = all -// 8 hidden tiles in one unit). q8(GELU): the frame's blocks (_enc_nc, _dec_nc) read it from the 16 KB arena table -// (SW_GELU 1, bit-identical to HIP's transcendental chain + pack, swin_block.hlsl; measured -31 / -33 us per frame at -// 1080p Quality); the groupshared table (SW_GELU 2) takes them to 112 VGPRs, over their bound of 96. l_swin_block_c32_nc -// keeps HIP's transcendental chain (SW_GELU 0). +// 8 hidden tiles in one unit). q8(GELU): the frame's blocks read it from the 16 KB arena table, bit-identical to HIP's +// transcendental chain + pack (swin_block.hlsl): _dec_nc with SW_GELU 1 (measured -31 / -33 us per frame against the +// chain at 1080p Quality), _enc_nc and _dec_coef12_nc with the cheaper table offsets for every tile (SW_GELU 3, pattern +// 13333: sw_gelu_fetch8_bn; a further -7 .. -9 / -13 .. -15 us). The groupshared table (SW_GELU 2) and patterns with a +// transcendental-chain tile (10333) take them to 108 .. 112 VGPRs, over their bound of 96. l_swin_block_c32_nc keeps +// HIP's transcendental chain (SW_GELU 0). // l_swin_block_c32_nc FP8 map in, FP8 out, mirror border (#117) // l_swin_block_c32_enc_nc the encoder block: FP16 halo map [H + 4][W + 4][32] in (SW_SRC 1), FP8 out // l_swin_block_c32_dec_nc the decoder block: FP8 map in with the token-grid mirror (SW_SRC 2), FP16 out (SW_OUT_F16) // // @wg-kernel l_swin_block_c32_nc co=l_swin.co block=256 wave=32 args=p,p,p,p,u,u,u,u defines=SW_C=32,SW_HEADS=2,SW_MLP=128,SW_ADD=0,SW_SAFE=1,SW_ECL=0,SW_HPU=8,SW_GELU=0,SW_ENTRY_OVERRIDE=l_swin_block_c32_nc -// @wg-kernel l_swin_block_c32_enc_nc co=l_swin.co block=256 wave=32 args=p,p,p,p,u,u,u,u defines=SW_C=32,SW_HEADS=2,SW_MLP=128,SW_ADD=0,SW_SAFE=1,SW_ECL=0,SW_HPU=8,SW_GELU=1,SW_SRC=1,SW_ENTRY_OVERRIDE=l_swin_block_c32_enc_nc +// @wg-kernel l_swin_block_c32_enc_nc co=l_swin.co block=256 wave=32 args=p,p,p,p,u,u,u,u defines=SW_C=32,SW_HEADS=2,SW_MLP=128,SW_ADD=0,SW_SAFE=1,SW_ECL=0,SW_HPU=8,SW_GELU=3,SW_GELU_PAT=13333,SW_SRC=1,SW_ENTRY_OVERRIDE=l_swin_block_c32_enc_nc // @wg-kernel l_swin_block_c32_dec_nc co=l_swin.co block=256 wave=32 args=p,p,p,p,u,u,u,u defines=SW_C=32,SW_HEADS=2,SW_MLP=128,SW_ADD=0,SW_SAFE=1,SW_ECL=0,SW_HPU=8,SW_GELU=1,SW_SRC=2,SW_OUT_F16=1,SW_ENTRY_OVERRIDE=l_swin_block_c32_dec_nc // l_swin_block_c32_dec_coef12_nc D3D12 only: the decoder block with l_coef_pack12 as its epilogue (SW_OUT_COEF12; // byte for byte the pair _dec_nc -> l_coef_pack12, which stays the fallback route); // the FP16 map is not written. Arguments: _dec_nc's (out unused), then A, C, latent, // dec0.coef, rw, rh -// @wg-kernel l_swin_block_c32_dec_coef12_nc co=- block=256 wave=32 args=p,p,p,p,u,u,u,u,p,p,p,p,u,u defines=SW_C=32,SW_HEADS=2,SW_MLP=128,SW_ADD=0,SW_SAFE=1,SW_ECL=0,SW_HPU=8,SW_GELU=1,SW_SRC=2,SW_OUT_F16=1,SW_OUT_COEF12=1,COEF_CVT_HW=1,SW_ENTRY_OVERRIDE=l_swin_block_c32_dec_coef12_nc +// @wg-kernel l_swin_block_c32_dec_coef12_nc co=- block=256 wave=32 args=p,p,p,p,u,u,u,u,p,p,p,p,u,u defines=SW_C=32,SW_HEADS=2,SW_MLP=128,SW_ADD=0,SW_SAFE=1,SW_ECL=0,SW_HPU=8,SW_GELU=3,SW_GELU_PAT=13333,SW_SRC=2,SW_OUT_F16=1,SW_OUT_COEF12=1,COEF_CVT_HW=1,SW_ENTRY_OVERRIDE=l_swin_block_c32_dec_coef12_nc #include "swin_block.hlsl" diff --git a/clean/l/runner/l_d3d12_check.py b/clean/l/runner/l_d3d12_check.py index e004d1c5..03d18083 100644 --- a/clean/l/runner/l_d3d12_check.py +++ b/clean/l/runner/l_d3d12_check.py @@ -93,6 +93,12 @@ def _params_c96(rng): "l_swin_block_c32_dec_nc": ("l_swin.co", 32, _params_c32), "swin_block_c96_nc": ("swin.co", 96, _params_c96)} +# Special input values of the encoder block (FP16: NaN, +-infinity, +-65504, +-0, the smallest subnormal) and the decoder +# block (FP8 codes: both NaN codes, +-448, +-0, subnormals) +SPECIAL_F16 = np.array([np.nan, np.inf, -np.inf, 65504.0, -65504.0, 0.0, -0.0, 6e-8], np.float16) +SPECIAL_FP8 = np.array([0x7f, 0xff, 0x7e, 0xfe, 0x00, 0x80, 0x01, 0x81, 0x07, 0x87], np.uint8) + + def _nc(blob): return bool(np.frombuffer(blob[:4], np.uint32)[0] & 1) @@ -106,17 +112,28 @@ def block_cases(kernel, rng): kind = "first" if enc else "second" h = T.block_halo(T.TABLE, kind) assert not enc or h == 2, "the ports read a 2-pixel halo" - for (H, W) in L_BLOCK_GRIDS: + for gi, (H, W) in enumerate(L_BLOCK_GRIDS): for parity in (0, 1): blob = gen(rng) if not _nc(blob): continue off = T.block_offset(T.TABLE, kind, parity) + # every other case: large inputs (hidden values far past the GELU's range) with special values in a + # twentieth of them + special = (gi + parity) % 2 == 1 + scale = 60.0 if special else 1.5 if enc: - xb = rng.normal(0, 1.5, (H + 2 * h, W + 2 * h, 32)).astype(np.float16).tobytes() - yield H, W, off, xb, H * W * 32, blob + x = rng.normal(0, scale, (H + 2 * h, W + 2 * h, 32)).astype(np.float16) + if special: + pick = rng.random(x.shape) < 0.05 + x[pick] = rng.choice(SPECIAL_F16, int(pick.sum())) + yield H, W, off, x.tobytes(), H * W * 32, blob else: - yield H, W, off, fp8_encode(q8(rng.normal(0, 1.5, (H, W, 32)))).tobytes(), H * W * 64, blob + y = fp8_encode(q8(rng.normal(0, scale, (H, W, 32)))).astype(np.uint8) + if special: + pick = rng.random(y.shape) < 0.05 + y[pick] = rng.choice(SPECIAL_FP8, int(pick.sum())) + yield H, W, off, y.tobytes(), H * W * 64, blob return C = KINDS[kernel][1] for (H, W, off) in CASES: