Skip to content
Draft
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
7 changes: 7 additions & 0 deletions clean/dx12/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ WG_KERNEL(exposure_auto_diet) { WG_SETUP(); ... } // entry name = HIP kernel n
* `args` = the HIP kernel's arguments in order: `p` pointer (DWORD: arena byte offset), `u` any 32-bit scalar,
`b<N>` struct by value (N bytes = N/4 DWORDs, copied verbatim). At most 57 DWORDs. The `WG_ARGS_BEGIN` block must
declare exactly these DWORDs in order, **32-bit scalars only** (no vectors / arrays / structs: cbuffer packing).
* A kernel named `<kernel>_fast` is Model L's `l_mode=fast` twin of `<kernel>` (clean/l/SPEED.md "Fast mode"): the runtime
launches it in place of the base kernel with the same arguments when the build has it, so it declares the same `args`; it
is built with `--model-l` only (`is_model_l`), has no HIP twin (`co=-`), keeps the base kernel's DXIL untouched, and the
hybrid route never launches it. Its arithmetic is not bit-exact to the reference by design (the hybrid route is).
`<kernel>_fast3` (the 3 x 3 post) and `<stem>_w2p_fast` / `<stem>_w2t_fast` (two windows per wave, in place of
`<stem>_nc`; `group=128 gscale=1`, the device's launch of the same grid) are fast-mode kernels too (`build_dx12.py
fast_base` names the kernel each one replaces).
* Output: `<build>/dx12/<tag>__<kernel>.dxil` (`<kernel>.dxil` without a tag) and a `manifest.json` line
`{"name", "file", "co", "entry", "block", "wave", "args", "arg_dwords", "source", "sha256"}`. Build all kernels at
once (no `--only`) for a release: `--only` rewrites the shared manifest read-modify-write.
Expand Down
24 changes: 18 additions & 6 deletions clean/dx12/build_dx12.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
dxv. Any other float division ('/', which would be an IEEE division) or float remainder is refused: use wg_div.

VGPR check: every Swin / bottleneck kernel built (a name with a swin_ or l_swin_ part, and Model L's fused post
w64_lc12/l_post_tf_p12[_pf]) is run through RGA (gfx1201) and its
w64_lc12/l_post_tf_p12[_pf][_fast|_fast3]) is run through RGA (gfx1201) and its
USED_VGPRs compared with its bound in clean/dx12/vgpr_limits.json (the count that keeps its present workgroups per CU);
over the bound, or no bound listed, fails the build. WG_DX12_VGPR_CHECK=0 skips it.
ISA rules of the hybrid kernels: every hybrid kernel built (a name with a _hyb part) is checked in the same RGA run by
Expand Down Expand Up @@ -227,12 +227,24 @@ def kernel_decls(path):
MODEL_L_ONLY = {"swin_block_c64_add_ex_nc", "lev/input_encoder_q20", "lev/input_encoder_direct_q20"}


def fast_base(name):
"""The kernel a fast-mode kernel of Model L's l_mode=fast is launched in place of (same grid, block and arguments), or
None: "<kernel>_fast" and "<kernel>_fast3" (the 3 x 3 post) replace <kernel>; "<stem>_w2p_fast" / "<stem>_w2t_fast" (the
two-windows-per-wave Swin blocks, l_swin_c32_fast.hlsl) replace "<stem>_nc"."""
m = re.fullmatch(r"(.+)_w2[pt]_fast", name)
if m:
return m.group(1) + "_nc"
m = re.fullmatch(r"(.+)_fast3?", name)
return m.group(1) if m else None


def is_model_l(decl):
"""A port of one of Model L's code objects (#89: l_swin.co, l_ops.co, l_post.co, l_enc_values.co), a Model L kernel
of the direct route with no code object (co "-", named l_*), a kernel only Model L launches (MODEL_L_ONLY), or a
kernel of the hybrid form (named *_hyb: WG_L_FORM = hybrid, Model L's route only so far)."""
of the direct route with no code object (co "-", named l_*), a kernel only Model L launches (MODEL_L_ONLY), a kernel
of the hybrid form (named *_hyb: WG_L_FORM = hybrid, Model L's route only so far), or a fast-mode kernel (fast_base:
Model L's l_mode=fast, launched in place of the kernel it names)."""
return (decl["co"].startswith("l_") or decl["name"].startswith("l_") or decl["name"] in MODEL_L_ONLY
or decl["name"].endswith("_hyb"))
or decl["name"].endswith("_hyb") or fast_base(decl["name"]) is not None)


def is_frame_gen(decl):
Expand Down Expand Up @@ -331,8 +343,8 @@ def rga_isa(dxil, isa_dir, stem):


def vgpr_checked(name):
# l_swin_*: Model L's blocks (#89); l_post_tf_p12[_pf]: Model L's fused post (bound = 96-register tier)
return re.search(r"(^|/|l_)swin_|/l_post_tf_p12(_pf)?$", name) is not None
# l_swin_*: Model L's blocks (#89); l_post_tf_p12[_pf][_fast|_fast3]: Model L's fused post (bound = 96-register tier)
return re.search(r"(^|/|l_)swin_|/l_post_tf_p12(_pf)?(_fast3?)?$", name) is not None


def rga_vgprs(dxil, isa=False):
Expand Down
68 changes: 68 additions & 0 deletions clean/dx12/kernel_sets.json
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@
"l_dec_project",
"l_ops.co"
],
[
"l_dec_project_fast",
"-"
],
[
"l_direct_color",
"-"
Expand Down Expand Up @@ -286,6 +290,10 @@
"l_project_gather",
"l_ops.co"
],
[
"l_project_gather_fast",
"-"
],
[
"l_q7_block_c128",
"l_q7w.co"
Expand Down Expand Up @@ -330,6 +338,22 @@
"l_q7_merge_c96",
"l_q7w.co"
],
[
"l_swin_block_c32_dec_add_nc",
"-"
],
[
"l_swin_block_c32_dec_coef12_add_nc",
"-"
],
[
"l_swin_block_c32_dec_coef12_add_w2p_fast",
"-"
],
[
"l_swin_block_c32_dec_coef12_add_w2t_fast",
"-"
],
[
"l_swin_block_c32_dec_coef12_hyb",
"-"
Expand Down Expand Up @@ -358,6 +382,18 @@
"l_swin_block_c32_enc_lin_nc",
"-"
],
[
"l_swin_block_c32_enc_lin_nc_fast",
"-"
],
[
"l_swin_block_c32_enc_lin_w2p_fast",
"-"
],
[
"l_swin_block_c32_enc_lin_w2t_fast",
"-"
],
[
"l_swin_block_c32_enc_nc",
"l_swin.co"
Expand Down Expand Up @@ -438,6 +474,10 @@
"swin_block_c64_add_ex_nc",
"swin.co"
],
[
"swin_block_c64_add_ex_nc_fast",
"-"
],
[
"swin_block_c64_add_hyb",
"-"
Expand All @@ -450,6 +490,10 @@
"swin_block_c64_hyb",
"-"
],
[
"swin_block_c64_nc_fast",
"-"
],
[
"swin_block_c64_q7c_hyb",
"-"
Expand All @@ -458,6 +502,10 @@
"swin_block_c96_add_ex_hyb",
"-"
],
[
"swin_block_c96_add_ex_nc_fast",
"-"
],
[
"swin_block_c96_add_hyb",
"-"
Expand All @@ -470,6 +518,10 @@
"swin_block_c96_hyb",
"-"
],
[
"swin_block_c96_pm_rev_nc_fast",
"-"
],
[
"swin_block_c96_q7c_hyb",
"-"
Expand Down Expand Up @@ -497,6 +549,22 @@
[
"w64_lc12/l_post_recon_p12_ldr",
"l_post__lc12.co"
],
[
"w64_lc12/l_post_tf_p12_fast",
"-"
],
[
"w64_lc12/l_post_tf_p12_fast3",
"-"
],
[
"w64_lc12/l_post_tf_p12_pf_fast",
"-"
],
[
"w64_lc12/l_post_tf_p12_pf_fast3",
"-"
]
],
"model_k": [
Expand Down
6 changes: 5 additions & 1 deletion clean/dx12/kernels/fused_swin.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,11 @@ void load_frags_g(uint base, uint off, out uint2 f[KC])
#ifndef SW_OUT_F16
#define SW_OUT_F16 0
#endif
#if (SW_SRC != 0 || (SW_OUT_F16 && !SW_HYB)) && (SW_ADD || SW_ECL > 0)
// The one exception outside the hybrid decoder: Model L's fast-mode decoder block (l_mode=fast, l_swin_fast.hlsl): the
// M-form chain (SW_HYB 0) with the skip, SW_SRC 2 (x by the token-grid mirror) + SW_ADD (xadd, the skip, by the fine one)
// + SW_OUT_F16, without a fused expand.
#define SW_L_FAST_DEC (SW_SRC == 2 && SW_ADD && SW_OUT_F16 && !SW_HYB && SW_ECL == 0)
#if (SW_SRC != 0 || (SW_OUT_F16 && !SW_HYB)) && (SW_ADD || SW_ECL > 0) && !SW_L_FAST_DEC
#error "SW_SRC / SW_OUT_F16: plain blocks only (the hybrid C32 decoder: SW_OUT_F16 with SW_ADD, the skip)"
#endif
#ifndef SW_OUT_COEF12
Expand Down
15 changes: 15 additions & 0 deletions clean/dx12/kernels/l_ops_project.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,23 @@
// this kernel has no branch): RN16 = v_cvt_f16_f32 then the exact widening, the FP8 pack v_cvt_pk_fp8_f32 after the
// clamp. The software forms were most of the kernels' instructions.
//
// Fast mode (l_mode=fast, PJ_FAST 1: l_project_gather_fast, l_dec_project_fast; native D3D12 only, no HIP twin): the FP32
// accumulation runs through all KB K blocks and rounds once, in the FP8 pack, instead of rounding to FP16 after every group of
// 32 inputs (the 128 conversions per lane of pj_rn16 were the kernel's longest chain). The result is the FP32 sum of the
// block's products: it differs from the reference's stepwise FP16 sum (which the hybrid route reproduces bit for bit, and
// l_ops.co's bytes follow) by the FP16 roundings of that sum, at most 3 ulp of FP16 at the largest value; the clamp and the
// pack are unchanged.
//
// @wg-kernel l_project_gather co=l_ops.co block=256 wave=32 args=p,p,p,u,u defines=PJ_DEC=0,WG_LD_A16=1
// @wg-kernel l_dec_project co=l_ops.co block=256 wave=32 args=p,p,p,u,u defines=PJ_DEC=1,WG_LD_A16=1
// @wg-kernel l_project_gather_fast co=- entry=l_project_gather block=256 wave=32 args=p,p,p,u,u defines=PJ_DEC=0,WG_LD_A16=1,PJ_FAST=1
// @wg-kernel l_dec_project_fast co=- entry=l_dec_project block=256 wave=32 args=p,p,p,u,u defines=PJ_DEC=1,WG_LD_A16=1,PJ_FAST=1
#include "wg_platform.hlsli"

#ifndef PJ_FAST
#define PJ_FAST 0
#endif

WG_ARGS_BEGIN
uint src, dst, w; // gather: x (pixel map [2 th][2 tw][32] FP8), features [th][tw][64]; dec: z [th][tw][64], y pixel map
int tw, th;
Expand Down Expand Up @@ -83,8 +96,10 @@ WG_KERNEL(l_project_gather)
acc1 = wg_wmma_fp8(fr1[2u * g], zf[2u * g], acc1);
acc0 = wg_wmma_fp8(fr0[2u * g + 1u], zf[2u * g + 1u], acc0);
acc1 = wg_wmma_fp8(fr1[2u * g + 1u], zf[2u * g + 1u], acc1);
#if !PJ_FAST
acc0 = pj_rn16(acc0);
acc1 = pj_rn16(acc1);
#endif
}
#if PJ_DEC
const int s = (int)(mt >> 1);
Expand Down
Loading
Loading