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
42 changes: 40 additions & 2 deletions clean/dx12/build_dx12.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"""Builds the D3D12 ports of the HIP kernels (clean/dx12/kernels/*.hlsl) for the hybrid device backend (runtime backend 2,
clean/runtime/device_dx12.cpp): <out>/dx12/<tag>__<kernel>.dxil (or <kernel>.dxil without a tag) + <out>/dx12/manifest.json.

python clean/dx12/build_dx12.py --out build/dx12-m/hip [--isa] [--only exposure_auto_diet,...] [-j N]
python clean/dx12/build_dx12.py --out build/dx12-m/hip [--isa] [--only exposure_auto_diet,...] [-j N] [--model-l]

Model L's kernels (#89: those that reproduce one of Model L's code objects, co=l_*.co, direct-route kernels named l_*, and MODEL_L_ONLY) are built only with --model-l
or when --only names them, so that a build without the option is Model M's kernel set unchanged.

Every kernel source declares its kernels in header lines (clean/dx12/README.md):

Expand Down Expand Up @@ -172,6 +175,29 @@ def kernel_decls(path):
return decls


# Kernels of Model M's code objects that only Model L's frame launches (#89): dec1 as a standalone block (#139;
# Model M runs dec1 fused with dec0).
MODEL_L_ONLY = {"swin_block_c64_add_ex_nc"}


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_*), or a kernel only Model L launches (MODEL_L_ONLY)."""
return decl["co"].startswith("l_") or decl["name"].startswith("l_") or decl["name"] in MODEL_L_ONLY


def kernel_set(model_l=False):
"""(name, code object) of every kernel a full build compiles (no --only), sorted: Model M's set, plus Model L's
with model_l. From the sources' declarations alone (nothing is compiled)."""
keys = set()
for fn in sorted(os.listdir(KERNEL_DIR)) if os.path.isdir(KERNEL_DIR) else []:
if fn.endswith(".hlsl"):
for d in kernel_decls(os.path.join(KERNEL_DIR, fn)):
if model_l or not is_model_l(d):
keys.add((d["name"], d["co"]))
return sorted(keys)


def arg_dwords(args, where):
n = 0
for a in args.split(","):
Expand Down Expand Up @@ -252,13 +278,23 @@ def vgpr_check(built):

def main(argv=None):
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("--out", required=True, help="build directory of the runtime (kernels go to <out>/dx12)")
ap.add_argument("--out", help="build directory of the runtime (kernels go to <out>/dx12); required unless --list")
ap.add_argument("--only", help="comma list of kernel names to build (default: all declared)")
ap.add_argument("--isa", action="store_true", help="also write the D3D12 ISA (RGA) to <out>/dx12/isa")
ap.add_argument("--listing", action="store_true", help="also keep the rewritten DXIL listing (<name>.ll)")
ap.add_argument("-j", "--jobs", type=int, default=int(os.environ.get("WG_DX12_JOBS", min(12, os.cpu_count() or 1))),
help="kernels compiled in parallel (default min(12, CPUs), env WG_DX12_JOBS; 1 = one at a time)")
ap.add_argument("--model-l", action="store_true",
help="also build Model L's kernels (co=l_*.co; off by default: Model M's kernel set unchanged)")
ap.add_argument("--list", action="store_true",
help="print the kernels a build with these options compiles (name and code object, JSON) and exit: "
"the cheap check before a build (kernel_sets.json, tests/test_kernel_sets.py)")
a = ap.parse_args(argv)
if a.list:
print(json.dumps([list(k) for k in kernel_set(a.model_l)], indent=1))
return 0
if not a.out:
ap.error("--out is required")
dst = os.path.join(a.out, "dx12")
os.makedirs(dst, exist_ok=True)
asm = build_asm_tool(a.out)
Expand All @@ -283,6 +319,8 @@ def main(argv=None):
for d in kernel_decls(src):
if only and d["name"] not in only:
continue
if not only and not a.model_l and is_model_l(d):
continue
key = (d["name"], d["co"])
if key in kernels and key not in old:
raise SystemExit(f"kernel {d['name']} ({d['co']}) declared twice")
Expand Down
254 changes: 254 additions & 0 deletions clean/dx12/kernel_sets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
{
"model_m": [
[
"bneck/swin_bneck6_mb_nc",
"swin__bmegahg.co"
],
[
"bneck/swin_bneck6_mbo_nc",
"swin__bmegahg.co"
],
[
"bneck/swin_bneck6_nc",
"swin__bmegahg.co"
],
[
"color_divide",
"frame.co"
],
[
"d1p12/swin_block_c64_add_dec0_nc",
"dec1_dec0__pack12.co"
],
[
"d1p12/swin_block_c64_add_dec0_xd_nc",
"dec1_dec0__pack12.co"
],
[
"emo/input_encoder_mo",
"encoder_mo.co"
],
[
"emo/input_encoder_mo_direct",
"encoder_mo.co"
],
[
"expand_c96",
"swin.co"
],
[
"exposure_auto_diet",
"exposure.co"
],
[
"exposure_auto_diet_direct",
"exposure.co"
],
[
"exposure_pair_diet",
"exposure.co"
],
[
"exposure_pair_diet_direct",
"exposure.co"
],
[
"f16lut_build_direct",
"-"
],
[
"input_encoder",
"encoder.co"
],
[
"input_encoder_direct",
"encoder.co"
],
[
"native_driver_probe",
"-"
],
[
"native_fill",
"-"
],
[
"patch_merge_c64",
"swin.co"
],
[
"patch_merge_c96",
"swin.co"
],
[
"pmo/post_tf4_dl_f32_p12_mo",
"post_mo_w64__orig.co"
],
[
"pmo/post_tf4_dl_f32_p12_mo_direct",
"post_mo_w64__orig.co"
],
[
"pmo/post_tf_f32_p12_mo",
"post_mo_w64__orig.co"
],
[
"pmo/post_tf_f32_p12_mo_direct",
"post_mo_w64__orig.co"
],
[
"scale_rgb",
"frame.co"
],
[
"swin_block_c64_nc",
"swin.co"
],
[
"swin_block_c96_add_ex_nc",
"swin.co"
],
[
"swin_block_c96_add_xe_nc",
"swin.co"
],
[
"swin_block_c96_add_xe_rev_nc",
"swin.co"
],
[
"swin_block_c96_add_xe_xd_nc",
"swin.co"
],
[
"swin_block_c96_add_xe_xo_nc",
"swin.co"
],
[
"swin_block_c96_nc",
"swin.co"
],
[
"swin_block_c96_pm_nc",
"swin.co"
],
[
"swin_block_c96_pm_rev_nc",
"swin.co"
],
[
"timing_barrier",
"frame.co"
],
[
"w64/post_filter_c1_s4",
"post_w64.co"
],
[
"w64/post_tf4_dl_f32_p12_diet_vsit",
"post_w64.co"
],
[
"w64/post_tf4_dl_f32_p12_diet_vsit",
"post_w64__orig.co"
],
[
"w64/post_tf4_dl_f32_p12_diet_vsit_direct",
"post_w64.co"
],
[
"w64/post_tf4_dl_f32_p12_diet_vsit_direct",
"post_w64__orig.co"
],
[
"w64/post_tf_f32_p12_diet_vsit",
"post_w64.co"
],
[
"w64/post_tf_f32_p12_diet_vsit",
"post_w64__orig.co"
],
[
"w64/post_tf_f32_p12_diet_vsit_direct",
"post_w64.co"
],
[
"w64/post_tf_f32_p12_diet_vsit_direct",
"post_w64__orig.co"
],
[
"w64/post_tf_p12_diet_vsit",
"post_w64__orig.co"
],
[
"w64/post_tf_p12_diet_vsit_tex",
"post_w64__orig.co"
]
],
"model_l": [
[
"l_coef",
"l_ops.co"
],
[
"l_coef_pack12",
"l_ops.co"
],
[
"l_dec_project",
"l_ops.co"
],
[
"l_linear_32_32_f16",
"l_ops.co"
],
[
"l_linear_32_32_fused",
"-"
],
[
"l_linear_32_32_relu",
"l_ops.co"
],
[
"l_post_recon",
"l_post.co"
],
[
"l_post_recon_p12",
"l_post.co"
],
[
"l_project_gather",
"l_ops.co"
],
[
"l_swin_block_c32_dec_coef12_nc",
"-"
],
[
"l_swin_block_c32_dec_nc",
"l_swin.co"
],
[
"l_swin_block_c32_enc_nc",
"l_swin.co"
],
[
"l_swin_block_c32_nc",
"l_swin.co"
],
[
"lev/input_encoder",
"l_enc_values.co"
],
[
"lev/input_encoder_direct",
"l_enc_values.co"
],
[
"swin_block_c64_add_ex_nc",
"swin.co"
]
]
}
58 changes: 58 additions & 0 deletions clean/dx12/tests/test_kernel_sets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# SPDX-License-Identifier: MIT
"""The D3D12 kernel sets, from the sources' declarations (clean/dx12/build_dx12.py kernel_set; nothing is compiled):
the cheap check before a build, in seconds. kernel_sets.json lists them: "model_m" = what a build without --model-l
compiles (Model M's kernel set; Model L's kernels never enter it), "model_l" = what --model-l adds (#89).

A kernel added, removed or moved between the sets changes kernel_sets.json in the same change; regenerate it with
python clean/dx12/tests/test_kernel_sets.py --write
and review the difference."""
import json
import os
import sys
import unittest

HERE = os.path.dirname(os.path.abspath(__file__))
DX12 = os.path.dirname(HERE)
sys.path.insert(0, DX12)
import build_dx12 as B # noqa: E402

SETS = os.path.join(DX12, "kernel_sets.json")


def current():
m = B.kernel_set(False)
full = B.kernel_set(True)
return {"model_m": [list(k) for k in m], "model_l": [list(k) for k in full if k not in set(m)]}


class TestKernelSets(unittest.TestCase):
def test_sets_match_the_list(self):
with open(SETS) as f:
want = json.load(f)
got = current()
for name in ("model_m", "model_l"):
w, g = set(map(tuple, want[name])), set(map(tuple, got[name]))
self.assertEqual(g, w, "%s: added %s, removed %s (update kernel_sets.json: python "
"clean/dx12/tests/test_kernel_sets.py --write)" % (name, sorted(g - w), sorted(w - g)))

def test_model_m_has_no_model_l_kernel(self):
m = B.kernel_set(False)
self.assertFalse([k for k in m if k[1].startswith("l_") or k[0].startswith("l_") or k[0] in B.MODEL_L_ONLY])
self.assertTrue(m)

def test_model_l_adds_only_model_l_kernels(self):
m, full = set(B.kernel_set(False)), set(B.kernel_set(True))
self.assertTrue(m <= full)
extra = full - m
self.assertTrue(extra)
self.assertFalse([k for k in extra if not (k[1].startswith("l_") or k[0].startswith("l_") or k[0] in B.MODEL_L_ONLY)])


if __name__ == "__main__":
if "--write" in sys.argv:
with open(SETS, "w", newline="\n") as f:
json.dump(current(), f, indent=1)
f.write("\n")
print("wrote", SETS)
else:
unittest.main()
2 changes: 1 addition & 1 deletion clean/l/runner/l_d3d12_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[--frames 100] [--warm 3]

<dir>: the build of clean/l/runner/l_frame_check.py --prepare (the synthetic models in <dir>/lframe_model) with the
D3D12 ports (clean/dx12/build_dx12.py --out <dir>). One model per process: the runtime library reads WG_D3D12_PROFILE
D3D12 ports (clean/dx12/build_dx12.py --model-l --out <dir>). One model per process: the runtime library reads WG_D3D12_PROFILE
when it is loaded. Run it in a GPU slot with the GPU otherwise idle. Profiled timestamps perturb scheduling: the step
sums are for attribution; the whole-frame span without profiling is measured too (wg_rt_frame_timed, a separate
Pipeline in a second process would be cleaner; here it is the same process after the profiled phase, reported apart).
Expand Down
Loading
Loading