Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions clean/dx12/kernels/l_swin.hlsl
Original file line number Diff line number Diff line change
@@ -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"
25 changes: 21 additions & 4 deletions clean/l/runner/l_d3d12_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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:
Expand Down
Loading