From 0eeaf8ffc23687ca6d3be0028f6a6f484f6303f3 Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Fri, 13 Nov 2026 16:44:35 -0500 Subject: [PATCH] build.py: optional models as a table (the --no-model-k switch, model M and K unchanged) The runtime library's optional models are one table, OPTIONAL_MODELS: each row names the model's runtime sources, built-in files, map, the define and stand-in source of a build without it, its kernel build and its option text. --no-model- options, the -DWG_NO_MODEL_ defines, the copied maps, the kernel builds and the extra-architecture child commands come from it. Model K's row is the former --no-model-k code; set_model_k and MODEL_K keep working. test_build_models checks model K's row against the old switch and plugs in a synthetic second optional model. Refs #89 Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> --- clean/runner/build.py | 68 +++++++++++++++++-------- clean/tests/test_build_models.py | 87 ++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 20 deletions(-) create mode 100644 clean/tests/test_build_models.py diff --git a/clean/runner/build.py b/clean/runner/build.py index 2fc0a101..ee274b60 100644 --- a/clean/runner/build.py +++ b/clean/runner/build.py @@ -61,19 +61,45 @@ K_EMBED_FILES = ["k_block.co", "k_block_wide.co", "k_io.co", os.path.basename(K_MAP_FILE)] NO_MODEL_K_SOURCE = "no_model_k.cpp" - -def set_model_k(on): - """Model K in (default) or out of the runtime library; changes RUNTIME_SOURCES and EMBED_FILES in place.""" +# Optional models of the runtime library (#89). Model M is always built. Each model below is in by default and can be +# left out with --no-model- (set_model(name, False)): its runtime sources and built-in files are dropped, its map +# is not copied, its kernels are not built, and its stand-in source is compiled with its define instead (the stand-in +# refuses model= at create). Model K's row is the former --no-model-k code, unchanged; a model adds its row here. +OPTIONAL_MODELS = { + "K": {"sources": K_RUNTIME_SOURCES, "embed": K_EMBED_FILES, "map": K_MAP_FILE, "define": "WG_NO_MODEL_K", + "stand_in": NO_MODEL_K_SOURCE, "build_device": k_build.build_device, + "help": "a runtime library without model K: its kernels and map are not built or built in, its runtime is " + "not compiled, model=K fails at create (release builds that ship model M only)"}, +} +MODELS_BUILT = {name: True for name in OPTIONAL_MODELS} + + +def model_built(name): + """An optional model is part of this build (OPTIONAL_MODELS).""" + return MODELS_BUILT[name] + + +def set_model(name, on): + """Optional model `name` in (default) or out of the runtime library; changes RUNTIME_SOURCES and EMBED_FILES in + place.""" global MODEL_K - if bool(on) == MODEL_K: + m = OPTIONAL_MODELS[name] + if bool(on) == MODELS_BUILT[name]: return - MODEL_K = bool(on) - if MODEL_K: - RUNTIME_SOURCES[:] = [s for s in RUNTIME_SOURCES if s != NO_MODEL_K_SOURCE] + K_RUNTIME_SOURCES - EMBED_FILES[:] = EMBED_FILES + K_EMBED_FILES + MODELS_BUILT[name] = bool(on) + if name == "K": + MODEL_K = bool(on) # the name the scripts and tests used before the table + if on: + RUNTIME_SOURCES[:] = [s for s in RUNTIME_SOURCES if s != m["stand_in"]] + m["sources"] + EMBED_FILES[:] = EMBED_FILES + m["embed"] else: - RUNTIME_SOURCES[:] = [s for s in RUNTIME_SOURCES if s not in K_RUNTIME_SOURCES] + [NO_MODEL_K_SOURCE] - EMBED_FILES[:] = [n for n in EMBED_FILES if n not in K_EMBED_FILES] + RUNTIME_SOURCES[:] = [s for s in RUNTIME_SOURCES if s not in m["sources"]] + [m["stand_in"]] + EMBED_FILES[:] = [n for n in EMBED_FILES if n not in m["embed"]] + + +def set_model_k(on): + """Model K in (default) or out of the runtime library; changes RUNTIME_SOURCES and EMBED_FILES in place.""" + set_model("K", on) EMBED_KEY = 0x5747454D42454444 # the same in clean/runtime/embedded.cpp @@ -231,8 +257,9 @@ def runtime_command(cxx, out, embedded=None): if embedded: # the generated table of built-in files srcs.append(embedded) cmd += ["-DWG_EMBEDDED_TABLE"] - if not MODEL_K: - cmd += ["-DWG_NO_MODEL_K"] + for name, m in OPTIONAL_MODELS.items(): + if not MODELS_BUILT[name]: + cmd += ["-D" + m["define"]] cmd += srcs if RR_OBJECTS and sys.platform == "win32": # release 0.6.0 (owner 2026-11-02: one runtime DLL, "just like before"): the RR runtime (clean/rr_f/runtime, built by @@ -253,7 +280,7 @@ def runtime_command(cxx, out, embedded=None): def build_runtime(cxx, out_dir, embed=True): - for m in (MAP_FILE, K_MAP_FILE) if MODEL_K else (MAP_FILE,): + for m in [MAP_FILE] + [o["map"] for name, o in OPTIONAL_MODELS.items() if MODELS_BUILT[name]]: shutil.copyfile(m, os.path.join(out_dir, os.path.basename(m))) embedded = write_embedded(out_dir) if embed else None run(runtime_command(cxx, os.path.join(out_dir, runtime_library_name()), embedded)) @@ -387,11 +414,11 @@ def main(argv=None): ap.add_argument("--with-rr", metavar="OBJ_DIR", help="link the RR runtime's object files (clean/rr_f/runtime/tools/build.py /obj) into the runtime " "library: one wronggpu_runtime.dll with the Model M and the RR interfaces (release 0.6.0)") - ap.add_argument("--no-model-k", action="store_true", - help="a runtime library without model K: its kernels and map are not built or built in, its " - "runtime is not compiled, model=K fails at create (release builds that ship model M only)") + for name, m in OPTIONAL_MODELS.items(): # --no-model-k, ... + ap.add_argument("--no-model-" + name.lower(), action="store_true", help=m["help"]) args = ap.parse_args(argv) - set_model_k(not args.no_model_k) + for name in OPTIONAL_MODELS: + set_model(name, not getattr(args, "no_model_" + name.lower())) global RR_OBJECTS RR_OBJECTS = args.with_rr if args.payload: @@ -433,8 +460,9 @@ def main(argv=None): run(device_command(clang, args.rocm, os.path.join(KERNEL_DIR, src), out, device_libs=not args.no_device_libs, report=args.report, soft_fp8=args.soft_fp8, defines=dict(d.split('=', 1) for d in args.define))) - if MODEL_K: - k_build.build_device(clang, args.rocm, args.out, device_libs=not args.no_device_libs, report=args.report) + for name, m in OPTIONAL_MODELS.items(): # the optional models' kernels (model K: clean/k) + if MODELS_BUILT[name]: + m["build_device"](clang, args.rocm, args.out, device_libs=not args.no_device_libs, report=args.report) # the post kernels in wave64 as well (the runtime's default post code object; post.co for WG_POST_WAVE=32) # (always with the resource report: POST_W64_GATE; skipped for experiment builds with --define) run_post_w64(device_command(clang, args.rocm, os.path.join(KERNEL_DIR, "post.hip"), @@ -531,7 +559,7 @@ def main(argv=None): sub = [sys.executable, os.path.abspath(__file__), "--out", os.path.join(args.out, arch), "--no-runtime"] sub += (["--rocm", args.rocm] if args.rocm else []) + (["--clang", args.clang] if args.clang else []) sub += (["--no-device-libs"] if args.no_device_libs else []) + (["--soft-fp8"] if args.soft_fp8 else []) - sub += ["--no-model-k"] if args.no_model_k else [] + sub += ["--no-model-" + n.lower() for n in OPTIONAL_MODELS if getattr(args, "no_model_" + n.lower())] for d in args.define: sub += ["--define", d] print(f"-- code objects for {arch}", flush=True) diff --git a/clean/tests/test_build_models.py b/clean/tests/test_build_models.py new file mode 100644 index 00000000..7f59f1b0 --- /dev/null +++ b/clean/tests/test_build_models.py @@ -0,0 +1,87 @@ +# SPDX-License-Identifier: MIT +"""The build script's optional models (clean/runner/build.py OPTIONAL_MODELS, #89): model K's row is the former +--no-model-k switch, and a further optional model plugs in with its own row, its own --no-model- option and its +own define, without changing what model M or model K get. No GPU, no compiler. + +Run: python -m unittest clean.tests.test_build_models +""" +import io +import os +import unittest +from contextlib import redirect_stdout +from unittest.mock import patch + +from clean.runner import build + + +class OptionalModels(unittest.TestCase): + def setUp(self): + self.saved = (list(build.RUNTIME_SOURCES), list(build.EMBED_FILES), dict(build.MODELS_BUILT), build.MODEL_K) + + def tearDown(self): # the module's lists exactly as before the test (other tests share them) + build.OPTIONAL_MODELS.pop("T", None) + build.RUNTIME_SOURCES[:], build.EMBED_FILES[:] = self.saved[0], self.saved[1] + build.MODELS_BUILT.clear() + build.MODELS_BUILT.update(self.saved[2]) + build.MODEL_K = self.saved[3] + + def test_k_row_is_the_old_switch(self): + k = build.OPTIONAL_MODELS["K"] + self.assertIs(k["sources"], build.K_RUNTIME_SOURCES) + self.assertIs(k["embed"], build.K_EMBED_FILES) + self.assertEqual((k["map"], k["define"], k["stand_in"]), (build.K_MAP_FILE, "WG_NO_MODEL_K", + build.NO_MODEL_K_SOURCE)) + self.assertEqual(list(build.OPTIONAL_MODELS), ["K"]) + on = (list(build.RUNTIME_SOURCES), list(build.EMBED_FILES)) + build.set_model("K", False) + self.assertFalse(build.model_built("K") or build.MODEL_K) + off = (list(build.RUNTIME_SOURCES), list(build.EMBED_FILES)) + build.set_model_k(True) + self.assertTrue(build.model_built("K") and build.MODEL_K) + self.assertEqual((build.RUNTIME_SOURCES, build.EMBED_FILES), on) + build.set_model_k(False) + self.assertEqual((build.RUNTIME_SOURCES, build.EMBED_FILES), off) # the same lists by either name + + def test_second_optional_model(self): + t_src = [os.path.join("..", "t", "runtime", "model_t.cpp")] + build.OPTIONAL_MODELS["T"] = {"sources": t_src, "embed": ["t_block.co"], "map": build.MAP_FILE, + "define": "WG_NO_MODEL_T", "stand_in": "no_model_t.cpp", + "build_device": lambda *a, **k: None, "help": "a library without model T"} + build.MODELS_BUILT["T"] = False + build.set_model("T", True) + self.assertEqual(build.RUNTIME_SOURCES[-1:], t_src) + self.assertIn("t_block.co", build.EMBED_FILES) + k_on = list(build.K_RUNTIME_SOURCES) + build.set_model("T", False) + self.assertNotIn(t_src[0], build.RUNTIME_SOURCES) + self.assertIn("no_model_t.cpp", build.RUNTIME_SOURCES) + self.assertNotIn("t_block.co", build.EMBED_FILES) + cmd = build.runtime_command("c++", "x") + self.assertIn("-DWG_NO_MODEL_T", cmd) + self.assertNotIn("-DWG_NO_MODEL_K", cmd) # model K untouched + self.assertTrue(all(s in build.RUNTIME_SOURCES for s in k_on)) + build.set_model("K", False) + cmd = build.runtime_command("c++", "x") + self.assertIn("-DWG_NO_MODEL_K", cmd) + self.assertIn("-DWG_NO_MODEL_T", cmd) + # its option is generated from the table + buf = io.StringIO() + with redirect_stdout(buf), self.assertRaises(SystemExit): + build.main(["--help"]) + self.assertIn("--no-model-t", buf.getvalue()) + self.assertIn("--no-model-k", buf.getvalue()) + + def test_maps_copied(self): + copied = [] + with patch.object(build.shutil, "copyfile", lambda a, b: copied.append(os.path.basename(a))), \ + patch.object(build, "write_embedded", lambda d: None), patch.object(build, "run", lambda c: None): + build.build_runtime("c++", "out", embed=False) + self.assertEqual(copied, ["preset-M.v1.map.json", "preset-K.v1.map.json"]) + copied.clear() + build.set_model("K", False) + build.build_runtime("c++", "out", embed=False) + self.assertEqual(copied, ["preset-M.v1.map.json"]) + + +if __name__ == "__main__": + unittest.main()