diff --git a/clean/l/runner/l_frame_check.py b/clean/l/runner/l_frame_check.py index 3e335417..34aa605e 100644 --- a/clean/l/runner/l_frame_check.py +++ b/clean/l/runner/l_frame_check.py @@ -31,9 +31,12 @@ - the ten Swin stages (clean/reference/swin.py run_stage), each output under the per-stage rule; - dec0: Model L's decoder (oracle decode) or Model M's (clean/reference/decoder.py) on the runtime's dec1 output: the A, B and latent codes and the C values, each at least 99.5 % identical (l_c32_check's end-to-end bound); -- post (clean/reference/post.py reconstruct): history and display under test_post_emu.py's FP16 acceptance, the - reduced history codes within 1 and 99 % exact; the filter (when the post kernel writes a display image) as - test_post_emu.py; +- post: Model L's reconstruction (l_post_recon) against clean/l/reference/recon.py reconstruct, Model M's against + clean/reference/post.py reconstruct: history and display under test_post_emu.py's FP16 acceptance for Model M, and + for Model L under Model L's post acceptance (clean/l/runner/l_post_check.py, provisional: values more than one ulp + off at most 0.01 % over the run and 0.05 % per frame, for the hardware-log2 reason given there), the reduced + history codes within 1 and 99 % exact; the filter (when + the post kernel writes a display image) as test_post_emu.py, against recon.output_filter / post.output_filter; - exposure: the runtime's value and the reference's, reported (not a pass condition). Output: counts only, no timing. The summary gives per stage the values differing, the largest difference (FP8 steps, FP16 ulps or codes, as the stage's metric has it), the non-finite values on each side and the largest finite magnitude @@ -53,6 +56,8 @@ ROOT = os.path.dirname(os.path.dirname(os.path.dirname(HERE))) sys.path.insert(0, ROOT) +from clean.l.runner import l_post_check as LP # noqa: E402 (Model L's post acceptance) + L_SOURCES = [os.path.join("..", "l", "runtime", "l_model.cpp"), os.path.join("..", "l", "runtime", "l_pack.cpp")] SEED = 89 MAX_RENDER = (96, 64) @@ -147,30 +152,33 @@ def prepare(build, rocm, model=None, map_path=None): # ---- --run (GPU) ---------------------------------------------------------------------------------------------------- -def frame_inputs(rng, rw, rh, reset): +def frame_inputs(rng, rw, rh, reset, special=True): from clean.reference import encoder as En - x = En.random_inputs(rng, rw=rw, rh=rh, fw=-(-rw // 32) * 32, fh=-(-rh // 32) * 32) + x = En.random_inputs(rng, rw=rw, rh=rh, fw=-(-rw // 32) * 32, fh=-(-rh // 32) * 32, special=special) return {"color": x["color"], "depth": x["depth"], "motion": x["motion"], "mv_scale": x["mv_scale"], "jitter": x["jitter"], "exposure": 1.0, "pre_exposure": PRE_EXPOSURE, "reset": reset} -def run(build, rocm): +def run(build, rocm, special=True): from clean.runner.hip import clean_process_environment clean_process_environment(rocm) from clean.runner.pipeline import Pipeline d = model_dir(build) + # Model L: HDR colour (its reconstruction kernel l_post_recon is HDR; the runtime refuses Model L without it) setups = {"L": dict(map_path=l_map(build), check_packing=False, kernels={"options": "model=L"}), "M": dict(check_packing=True, kernels={"pack": "none"})} + flags = {"L": {"depth_inverted": False, "auto_exposure": True, "hdr": True}, + "M": {"depth_inverted": False, "auto_exposure": True}} for model, kw in setups.items(): # "stages": the bottleneck as six block launches, so that b0 ... b4 are written (the default megakernel writes # only b5's output); "default": the runtime's default kernels, which must give the same bytes for config in ("stages", "default"): k = dict(kw, kernels=dict(kw["kernels"], **({"bneck": "none"} if config == "stages" else {}))) - p = Pipeline(build, rocm, d, MAX_RENDER, OUTPUT, {"depth_inverted": False, "auto_exposure": True}, **k) + p = Pipeline(build, rocm, d, MAX_RENDER, OUTPUT, flags[model], **k) rng = np.random.default_rng(SEED) frames = [] for (rw, rh), reset in SEQUENCE: - fr = frame_inputs(rng, rw, rh, reset) + fr = frame_inputs(rng, rw, rh, reset, special) out = p.frame(fr, debug=True) frames.append({"inputs": fr, "out": out}) rec = {"model": model, "config": config, "kernels": p.kernels(), "notes": p.notes(), @@ -230,16 +238,22 @@ def same_values(got, ref): "max_f16_steps": int(_f16_steps(g[dif], r[dif]).max()) if dif.any() else 0} -def post_fp16(got, ref): +def post_fp16(got, ref, model="M"): finite = np.isfinite(got) s = _f16_steps(got, ref) d = np.abs(np.where(finite, got, 0.0).astype(np.float64) - ref) m = {"values": int(s.size), "differ": int(np.sum(s > 0)), "max_f16_steps": int(s.max()) if s.size else 0, - "nonfinite": int(np.sum(~finite)), "frac_over_1": float(np.mean(s > 1)), "frac_diff": float(np.mean(s > 0)), + "nonfinite": int(np.sum(~finite)), "frac_over_1": float(np.mean(s > 1)), "over_1": int(np.sum(s > 1)), + "frac_diff": float(np.mean(s > 0)), "mean_rel_rms": float(d.mean() / max(np.sqrt(np.mean(np.square(ref, dtype=np.float64))), 1e-30))} m.update(_magnitudes(got, ref)) - m["pass"] = (m["nonfinite"] == 0 and m["frac_over_1"] <= 1e-4 and m["mean_rel_rms"] <= 1e-3 - and m["frac_diff"] <= 0.01) + if model == "L": + # Model L's post acceptance (clean/l/runner/l_post_check.py, provisional): per frame at most 0.05 % more than + # one ulp off; the run-level 0.01 % is checked in compare() + m["pass"] = LP.l_post_rule(m["values"], m["nonfinite"], m["over_1"], m["differ"], m["mean_rel_rms"]) + else: + m["pass"] = (m["nonfinite"] == 0 and m["frac_over_1"] <= 1e-4 and m["mean_rel_rms"] <= 1e-3 + and m["frac_diff"] <= 0.01) return m @@ -249,6 +263,7 @@ def compare_model(rec, params): from clean.reference import decoder as MD from clean.reference import encoder as ME from clean.reference import exposure as Ex + from clean.l.reference import recon as R from clean.reference import post as Po from clean.reference import swin as S model = rec["model"] @@ -332,10 +347,11 @@ def compare_model(rec, params): "history": out["state"]["history_prev"] if not eff_reset else np.zeros((D[1], D[0], 3), np.float32), "m_near": out["enc0"]["m_near"], "m_center": out["enc0"]["m_center"], "m_far": out["enc0"]["m_far"], "jitter": fr["jitter"], "reset": eff_reset, "recon_size": D} - ref = Po.reconstruct(xp, float(np.asarray(params["post"]["s"]).reshape(-1)[0])) - m["post"] = {"history": post_fp16(out["post"]["history"], ref["history"])} + s_post = float(np.asarray(params["post"]["s"]).reshape(-1)[0]) + ref = R.reconstruct(xp, s_post) if model == "L" else Po.reconstruct(xp, s_post) + m["post"] = {"history": post_fp16(out["post"]["history"], ref["history"], model)} if out["post"]["display"] is not None: - m["post"]["display"] = post_fp16(out["post"]["display"], ref["display"]) + m["post"]["display"] = post_fp16(out["post"]["display"], ref["display"], model) dc = codes(out["post"]["reduced"], ref["reduced"], 1023.0) dc["pass"] = dc["max_code_diff"] <= 1 and dc["differ"] <= 0.01 * dc["values"] m["post"]["reduced"] = dc @@ -343,6 +359,8 @@ def compare_model(rec, params): disp, e = out["post"]["display"], out["exposure"]["e"] if rec["taps"] is not None: fo = Po.output_filter_original(disp, e, (rec["ow"], rec["oh"]), rec["taps"]) + elif model == "L": + fo = R.output_filter(disp, e, (rec["ow"], rec["oh"])) else: fo = Po.output_filter(disp, e, (rec["ow"], rec["oh"])) fo = (np.asarray(fo, np.float32) * pi).astype(np.float32) @@ -434,6 +452,10 @@ def compare(build, json_path): "default_identical": {"frames": len(same), "identical": sum(not d for d in same), "differing_outputs": sorted({k for d in same for k in d})}, "pass": all(m["pass"] for m in frames) and not any(same)} + if model == "L": # Model L's run-level limit on post values more than one ulp off (provisional) + res[model]["post_run"] = {k: LP.l_post_run([m["post"][k] for m in frames if k in m["post"]]) + for k in ("history", "display")} + res[model]["pass"] = res[model]["pass"] and all(r["pass"] for r in res[model]["post_run"].values()) res["pass"] = res["L"]["pass"] and res["M"]["pass"] src = os.path.join(model_dir(build), "source.json") if os.path.exists(src): @@ -460,6 +482,9 @@ def main(argv=None): g.add_argument("--run", action="store_true") g.add_argument("--compare", action="store_true") ap.add_argument("--json") + ap.add_argument("--plain", action="store_true", + help="--run: frames without random_inputs' special values (NaN / huge / negative colours, NaN " + "depths, motion leaving the frame)") ap.add_argument("--model", help="--prepare: a converter-written Model L model.wgcm in place of the synthetic model") ap.add_argument("--map", help="--prepare with --model: the map it was converted with (default: the shipped " "data/preset-L.v1.map.json)") @@ -472,7 +497,7 @@ def main(argv=None): prepare(a.build, a.rocm, a.model, a.map) return 0 if a.run: - run(a.build, a.rocm) + run(a.build, a.rocm, special=not a.plain) return 0 return compare(a.build, a.json or os.path.join(a.build, "lframe_check.json")) diff --git a/clean/l/runner/l_post_check.py b/clean/l/runner/l_post_check.py index df1a037b..fbd7b2e7 100644 --- a/clean/l/runner/l_post_check.py +++ b/clean/l/runner/l_post_check.py @@ -13,11 +13,17 @@ the reduced codes' three 10-bit fields and top 2 bits (must be 0); no byte of any slack may change. A negative control (the same case with the jitter moved by half a render pixel) must fail the criteria. -Criteria. Values: Model M's acceptance for its own reconstruction kernel (clean/tests/test_post_emu.py -test_matches_reference): no non-finite value, at most 0.01 % of FP16 values more than one ulp off, at most 1 % differ, -mean absolute error at most 0.1 % of the reference RMS; reduced codes at most 1 off, at most 1 % differ. These are -Model M's thresholds (float32 kernel against a float64 oracle); they are to be tightened when a kernel-order model of -the reconstruction exists. The oracle's colour gather runs in the model's precision (confirmed by the spec side), so +Criteria (Model L's post acceptance, l_post_rule; also used by clean/l/runner/l_frame_check.py for Model L). Values: +no non-finite value, at most 1 % differ, mean absolute error at most 0.1 % of the reference RMS; reduced codes at most +1 off, at most 1 % differ (Model M's acceptance for its own reconstruction kernel, clean/tests/test_post_emu.py +test_matches_reference). FP16 values more than one ulp off: at most 0.01 % over the whole run (all cases of all seeds) +and at most 0.05 % in any one case. PROVISIONAL until a kernel-order model of the reconstruction exists (approved by +the coordinator, 2026-11-14; Model M's checks keep their per-case 0.01 %). Why: the kernel's hardware log2 +(v_log_f32, not correctly rounded) can put one input feature h(E_in(e c)) on the other side of an FP16 rounding +boundary than the oracle's correctly rounded log; the gather's half accumulators carry that flip forward, to several +FP16 steps where the window mixes a clamped negative colour with positive ones. Forcing that one feature to the +kernel's value in the oracle leaves 0 values more than one ulp off (the failing special case of #137's record, and +frames 8 and 9 of l_frame_check's record). With about 5 such values per case, a per-case 0.01 % turns on one flip. The oracle's colour gather runs in the model's precision (confirmed by the spec side), so the kernel and the oracle agree on which weights underflow: no pixel is excluded. Special colours (the encoder's random_inputs special values: NaN, 1e3 ... 1e6, small negatives, +inf): the same criteria; the ill-conditioned pixels (a large fallback colour cancelled in the blend) are also reported apart. @@ -121,6 +127,24 @@ def decode(raw, sizes, dims): "slack_untouched": all(bool((raw[k][sizes[k]:] == SENTINEL).all()) for k in sizes)} +# Model L's post acceptance (see the module docstring; provisional): FP16 values more than one ulp off, at most this +# fraction of all values of a run, and at most L_OVER1_CASE in any one case or frame. +L_OVER1_RUN = 1e-4 +L_OVER1_CASE = 5e-4 + + +def l_post_rule(values, nonfinite, over_1, differ, mean_rel_rms): + """One case's (or frame's) FP16 output under Model L's post acceptance, the run-level limit aside.""" + return (nonfinite == 0 and over_1 <= L_OVER1_CASE * values and differ <= 0.01 * values and mean_rel_rms <= 1e-3) + + +def l_post_run(items): + """The run-level limit: items = per-case dicts with "values" and "over_1". Returns {values, over_1, frac, pass}.""" + v = sum(int(i["values"]) for i in items) + o = sum(int(i["over_1"]) for i in items) + return {"values": v, "over_1": o, "frac_over_1": o / max(v, 1), "pass": o <= L_OVER1_RUN * v} + + def m_rule(got, ref): res = {} for k in ("history", "display"): @@ -129,10 +153,11 @@ def m_rule(got, ref): s = C32.f16_steps(np.where(fin, g, 0), r) d = np.abs(np.where(fin, g, 0.0).astype(np.float64) - r) res[k] = {"values": int(s.size), "nonfinite": int(np.sum(~fin)), "frac_over_1": float(np.mean(s > 1)), - "differ": int(np.sum(s > 0)), "frac_diff": float(np.mean(s > 0)), "max_steps": int(s.max()), + "over_1": int(np.sum(s > 1)), "differ": int(np.sum(s > 0)), "frac_diff": float(np.mean(s > 0)), + "max_steps": int(s.max()), "mean_rel_rms": float(d.mean() / max(np.sqrt(np.mean(np.square(r, dtype=np.float64))), 1e-30))} - res[k]["pass"] = (res[k]["nonfinite"] == 0 and res[k]["frac_over_1"] <= 1e-4 and res[k]["frac_diff"] <= 0.01 - and res[k]["mean_rel_rms"] <= 1e-3) + res[k]["pass"] = l_post_rule(res[k]["values"], res[k]["nonfinite"], res[k]["over_1"], res[k]["differ"], + res[k]["mean_rel_rms"]) rc = np.rint(np.asarray(ref["reduced"], np.float64) * 1023.0).astype(np.int64) dc = np.abs(got["reduced_codes"] - rc) res["reduced"] = {"values": int(dc.size), "differ": int(np.sum(dc > 0)), "max": int(dc.max(initial=0))} @@ -197,7 +222,9 @@ def run(build, rocm, seeds=(89,)): raw, sizes, dims = run_kernel(hip, mod, dict(x, jitter=np.asarray(x["jitter"], np.float32) + 0.5), S) neg = m_rule(decode(raw, sizes, dims), R.reconstruct(x, S)) res["negative_control_fails"] = not neg["pass"] - res["pass"] = all(c["pass"] for c in res["cases"]) and res["negative_control_fails"] + res["run"] = {k: l_post_run([c[k] for c in res["cases"]]) for k in ("history", "display")} + res["pass"] = (all(c["pass"] for c in res["cases"]) and res["negative_control_fails"] + and all(r["pass"] for r in res["run"].values())) return res @@ -214,7 +241,7 @@ def main(argv=None): for c in res["cases"]: print(json.dumps({k: c[k] for k in c if k not in ("history", "display", "reduced")}), {k: (c[k]["differ"], c[k].get("max_steps", c[k].get("max"))) for k in ("history", "display", "reduced")}) - print({"negative_control_fails": res["negative_control_fails"], "pass": res["pass"]}) + print({"run": res["run"], "negative_control_fails": res["negative_control_fails"], "pass": res["pass"]}) return 0 if res["pass"] else 1 diff --git a/clean/l/runtime/tests/test_l_runtime_abi.py b/clean/l/runtime/tests/test_l_runtime_abi.py index 730f8c38..f74f4b34 100644 --- a/clean/l/runtime/tests/test_l_runtime_abi.py +++ b/clean/l/runtime/tests/test_l_runtime_abi.py @@ -1,10 +1,11 @@ # SPDX-License-Identifier: MIT -"""Model L's code objects against what the runtime launches (#89), without running them: l_enc_values.co, l_ops.co -and l_swin.co are compiled (clean/l/runner/l_c32_check.py compile_kernels; HIP SDK clang, compile only), and each -kernel the runtime frame launches (clean/runtime/runtime.cpp, clean/l/FORMATS.md) must be exported with the arguments -the runtime passes: their number and sizes (a device address 8 bytes, an int 4, the encoder parameters WgEncParams -116, the decoder parameters WgDecParams 56). The kernels' own metadata (the AMDGPU metadata note of each code object, -amdhsa.kernels) is read here; nothing is loaded or executed. The argument kinds are the frame trace's table +"""Model L's code objects against what the runtime launches (#89), without running them: l_enc_values.co, l_ops.co, +l_swin.co and l_post.co are compiled (clean/l/runner/l_c32_check.py compile_kernels; HIP SDK clang, compile only), +and each kernel the runtime frame launches (clean/runtime/runtime.cpp, clean/l/FORMATS.md) must be exported with the +arguments the runtime passes: their number and sizes (a device address 8 bytes, an int 4, the encoder parameters +WgEncParams 116, the decoder parameters WgDecParams 56, the reconstruction parameters WgPostParams 64). The kernels' +own metadata (the AMDGPU metadata note of each code object, amdhsa.kernels) is read here; nothing is loaded or +executed. The argument kinds are the frame trace's table (clean/tests/runtime_frame_trace.cpp), so the recorder that checks the runtime's arguments and the kernels agree. Skipped without the HIP SDK (ROCM_PATH or C:\\Program Files\\AMD\\ROCm\\7.2). @@ -21,12 +22,13 @@ import unittest ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -SIZES = {"p": 8, "i": 4, "E": 116, "D": 56} +SIZES = {"p": 8, "i": 4, "E": 116, "D": 56, "P": 64} # the kernels the Model L frame launches, by code object (the lev tag of l_enc_values.co is the runtime's) LAUNCHED = {"l_enc_values.co": ["input_encoder"], "l_ops.co": ["l_linear_32_32_relu", "l_linear_32_32_f16", "l_project_gather", "l_dec_project", "l_coef"], "l_swin.co": ["l_swin_block_c32_enc", "l_swin_block_c32_enc_nc", "l_swin_block_c32_dec", - "l_swin_block_c32_dec_nc"]} + "l_swin_block_c32_dec_nc"], + "l_post.co": ["l_post_recon"]} _BUILD = None @@ -55,7 +57,7 @@ def kinds_table(): """{kernel name or prefix*: kinds} from the frame trace program's table.""" with open(os.path.join(ROOT, "clean", "tests", "runtime_frame_trace.cpp"), encoding="utf-8") as f: text = f.read() - return dict(re.findall(r'\{"([A-Za-z0-9_*]+)", "([piED]+)"\}', text)) + return dict(re.findall(r'\{"([A-Za-z0-9_*]+)", "([piEDP]+)"\}', text)) def kinds_of(table, kernel): diff --git a/clean/l/runtime/tests/test_l_runtime_frame.py b/clean/l/runtime/tests/test_l_runtime_frame.py index 9c5f4909..33def5aa 100644 --- a/clean/l/runtime/tests/test_l_runtime_frame.py +++ b/clean/l/runtime/tests/test_l_runtime_frame.py @@ -30,6 +30,7 @@ import hashlib import os import shutil +import struct import subprocess import tempfile import unittest @@ -83,8 +84,9 @@ def map_path(): return os.path.join(model_dir(), "maps", "preset-L.v1.map.json") -def run(options="", frames=4, build_files=(), env=None, model=None, exe=None, flags=(), rw=RW, rh=RH): - """The trace program's output lines (WG_* removed from the environment, then env applied).""" +def run(options="", frames=4, build_files=(), env=None, model=None, exe=None, flags=(), rw=RW, rh=RH, hdr=1): + """The trace program's output lines (WG_* removed from the environment, then env applied). hdr: WgRtConfig.hdr + (Model L's reconstruction kernel is HDR; Model M does not read the flag).""" build = tempfile.mkdtemp(prefix="wg_lfb_") for n in build_files: open(os.path.join(build, n), "wb").close() @@ -92,7 +94,8 @@ def run(options="", frames=4, build_files=(), env=None, model=None, exe=None, fl e.update(env or {}) try: r = subprocess.run([exe or trace_exe(), build, model or model_dir(), str(rw), str(rh), "120", "72", str(frames), - options, "--format=0", "--map=" + map_path(), *flags], capture_output=True, text=True, + options, "--format=0", "--map=" + map_path(), "--hdr=%d" % hdr, *flags], + capture_output=True, text=True, env=e, timeout=600) finally: shutil.rmtree(build, ignore_errors=True) @@ -105,17 +108,20 @@ def blob_digests(): """{blob name: SHA-256 hex} of Model L's blobs packed in Python (clean/l/runner/l_pack.py) from the test model's parameters: the independent names of the weight buffers (the recorder logs what create uploads, with its hash).""" blobs = LP.pack_model_l(L.random_params(np.random.default_rng(SEED))) + # the post blob: s as float32, zero padding to 16 bytes (Model M's layout; the test model's s, LL.raw_model) + blobs["post"] = struct.pack(" y "l_swin_block_c32_dec": [n["l.narrow"], "0", n["l.wide"], w["dec0_block"], str(fh), str(fw)] + do, # -> y2 "l_coef": [n["l.wide"], n["dec0.A"], n["dec0.B"], n["dec0.C"], n["dec0.latent"], w["dec0.coef"], None], + # the reconstruction (Model M's post_recon arguments): colour, A, B, C, the previous history, this frame's + # motion surfaces, the post blob, the new history, the reduced history, the display image, P, exposure pair + "l_post_recon": [n["input.color"], n["dec0.A"], n["dec0.B"], n["dec0.C"], n["state.history_prev"], + n["enc0.m_near"], n["enc0.m_center"], n["enc0.m_far"], w["post"], n["post.history"], + n["post.reduced"], n["post.display"], None, n["exposure.pair"]], } @@ -173,8 +184,8 @@ def nc_flags(): def expected_l(m_frame, parity, fh, fw): - """Model M's launches of one frame with Model L's steps 2 and 4 (clean/l/FORMATS.md, "Kernels" and "Launch order - of one frame"): kernel, grid, block.""" + """Model M's launches of one frame (on the unbanded post_recon route) with Model L's steps 2, 4 and 5 + (clean/l/FORMATS.md, "Kernels" and "Launch order of one frame"): kernel, grid, block.""" nc = nc_flags() npix, tokens = (fh + 4) * (fw + 4), (fh // 2) * (fw // 2) @@ -192,6 +203,8 @@ def block(kind, offsets, i): elif k == "coef_decoder": out += [("l_dec_project", cdiv(tokens, 128), 256), block("dec", "second", 1), ("l_coef", cdiv(tokens, 128), 256)] + elif k.split("/")[-1] == "post_recon": # Model L's reconstruction, same arguments and grid + out.append(("l_post_recon", g, b)) else: out.append((k, g, b)) return out @@ -199,8 +212,9 @@ def block(kind, offsets, i): class Frame(unittest.TestCase): def check(self, build_files=(), m_env=None): + # Model M on the route Model L's reconstruction takes: the unbanded post_recon, then the unbanded filter fh, fw = cdiv(RH, 32) * 32, cdiv(RW, 32) * 32 - cm, lm, fm, km = FT.parse(run(build_files=build_files, env=m_env)) + cm, lm, fm, km = FT.parse(run(build_files=build_files, env=dict(m_env or {}, WG_POST_KERNEL="post_recon"))) cl, ll, fl, kl = FT.parse(run("model=L", build_files=build_files)) self.assertEqual(cm, "create ok model M") self.assertEqual(cl, "create ok model L") @@ -209,8 +223,9 @@ def check(self, build_files=(), m_env=None): names = [k for k, _, _ in m] self.assertIn("input_encoder", names) self.assertIn("coef_decoder", names) # Model M's base decoder: the one Model L's l_coef stands in for + self.assertEqual(sum(k.split("/")[-1] == "post_recon" for k in names), 1) self.assertEqual(l_, expected_l(m, i & 1, fh, fw), "frame %d" % i) - l_mods = ["l_enc_values.co as lev", "l_ops.co", "l_swin.co"] + l_mods = ["l_enc_values.co as lev", "l_ops.co", "l_swin.co", "l_post.co"] self.assertEqual([x for x in ll if x not in l_mods], [x for x in lm if x not in (km["enc0"], km["dec0"])]) for want in l_mods: self.assertIn(want, ll) @@ -289,6 +304,11 @@ def check_frame(self, t, f, roles=True): self.assertEqual(e[:4], [rw, rh, fw, fh]) self.assertEqual(e[5], info["reset"]) self.assertEqual(d, [rw, rh, fw, fh, info["reset"]]) + post = [ln for ln in ls if ln.kernel == "l_post_recon"] + self.assertEqual(len(post), 1) + self.assertEqual(post[0].params(12), [rw, rh, (3 * 120 + 1) // 2, (3 * 72 + 1) // 2, info["reset"]]) + self.assertEqual(post[0].grid, cdiv((3 * 120 + 1) // 2, 32) * cdiv(cdiv((3 * 72 + 1) // 2, 2), 8)) + self.assertTrue(ls[ls.index(post[0]) + 1].kernel.endswith("post_filter_c1_s4")) # Model M's filter next self.assertTrue(lev.args[11].startswith("b")) # the values blob: an allocation if roles: # every argument against the named buffers and blobs check_roles(t, f, blob_digests()) @@ -348,6 +368,9 @@ class Roles(unittest.TestCase): ("lev/input_encoder", 4, "lev/input_encoder", 5), # previous latent <-> motion history ("lev/input_encoder", 7, "lev/input_encoder", 8), # m_near <-> m_far ("l_linear_32_32_relu", 0, "l_linear_32_32_relu", 1), # q's map <-> x1's map + ("l_post_recon", 4, "l_post_recon", 9), # history read <-> history written + ("l_post_recon", 8, "l_coef", 5), # post blob <-> dec0.coef + ("l_post_recon", 1, "l_post_recon", 3), # A <-> C ) @classmethod @@ -358,7 +381,7 @@ def test_every_argument_named(self): t, digests = self.trace, blob_digests() addrs = [t.uploaded(d) for d in digests.values()] self.assertNotIn(None, addrs) - self.assertEqual(len(set(addrs)), len(addrs)) # eight distinct blobs, each in its own allocation + self.assertEqual(len(set(addrs)), len(addrs)) # nine distinct blobs, each in its own allocation for f in t.frames: self.assertEqual(len(f["names"]) >= 30, True, sorted(f["names"])) check_roles(t, f, digests) @@ -413,6 +436,12 @@ def test_paths(self): .startswith("ERR create: Model L: motion_outres=original")) self.assertTrue(run("model=L", build_files=FT.SHIPPING, env={"WG_POST_PACK": "p12"})[0].startswith( "ERR create: Model L: post_pack=p12")) + # Model L's reconstruction is its own kernel: Model M's post kernels are not chosen for it + self.assertTrue(run("model=L", env={"WG_POST_KERNEL": "post_recon2_r"})[0].startswith( + "ERR create: Model L: its reconstruction is l_post_recon")) + # LDR colour: Model L's LDR reconstruction kernel is not written yet (refused, never Model M's post instead) + self.assertTrue(run("model=L", hdr=0)[0].startswith( + "ERR create: Model L: LDR colour (WgRtConfig.hdr = 0) needs Model L's LDR reconstruction kernel")) def test_model_data(self): d = tempfile.mkdtemp(prefix="wg_lf_") diff --git a/clean/runtime/runtime.cpp b/clean/runtime/runtime.cpp index d8b7d8ea..a70080e0 100644 --- a/clean/runtime/runtime.cpp +++ b/clean/runtime/runtime.cpp @@ -541,6 +541,10 @@ struct Runtime { if (c.wpw128) wpw[2] = c.wpw128; if (!choose_kernels(c)) return false; if (model_l && recon_output) return fail("Model L: recon=output (D = O) is Model M's experimental mode"); + // Model L's reconstruction kernel l_post_recon is HDR; its LDR form (11/11/10-bit display codes) is not written yet + if (model_l && !c.hdr) + return fail("Model L: LDR colour (WgRtConfig.hdr = 0) needs Model L's LDR reconstruction kernel, which is not " + "written yet (l_post_recon is HDR)"); if (model_l && enc0_co != "encoder.co") return fail("Model L: enc0 variants are Model M's encoder; Model L's runs from l_enc_values.co"); // Model L's coefficient kernel (l_coef) writes Model M's coef_decoder formats, not pack12: the _p12 post kernels @@ -649,8 +653,9 @@ struct Runtime { // Model L's code objects (clean/l/FORMATS.md): the encoder's values under the tag lev (lev/input_encoder, next to // Model M's input_encoder), the linears / projection / channel copy and the width-32 block untagged if (model_l && (!dev->load_module_tagged(build_dir + "/l_enc_values.co", "lev") || - !dev->load_module(build_dir + "/l_ops.co") || !dev->load_module(build_dir + "/l_swin.co"))) - return fail(dev->err() + " (Model L's code objects l_enc_values.co, l_ops.co, l_swin.co)"); + !dev->load_module(build_dir + "/l_ops.co") || !dev->load_module(build_dir + "/l_swin.co") || + !dev->load_module(build_dir + "/l_post.co"))) + return fail(dev->err() + " (Model L's code objects l_enc_values.co, l_ops.co, l_swin.co, l_post.co)"); if (!stage_variants(c)) return false; if (!runtime_options(c)) return false; @@ -864,6 +869,20 @@ struct Runtime { const bool filter_unbanded = fk != kDefaultFilter && fk.find("_band") == std::string::npos; if (!post_given && !filter_unbanded && !recon_output) post_kernel = kDefaultBandPost; } +#ifdef WG_MODEL_L + // Model L reconstructs with its own kernel, l_post_recon (l_post.co; clean/l/FORMATS.md step 5), which takes Model + // M's post_recon arguments and buffers: the unbanded post_recon route (8-byte history, base coefficient format, + // no pack12), then Model M's output filter (unbanded). Model M's post kernels are not for Model L. + if (model_l) { + if ((c.post_kernel && *c.post_kernel) || choose(nullptr, "WG_POST_KERNEL", "") != "") + return fail("Model L: its reconstruction is l_post_recon; a post kernel (post_kernel / WG_POST_KERNEL) " + "chooses among Model M's"); + if (choose(c.post_pack, "WG_POST_PACK", "none") == "p12") + return fail("Model L: post_pack=p12 (the pack12 decoder) is Model M's; Model L's l_coef writes the base " + "coefficient format"); + post_kernel = "post_recon"; + } +#endif // post_pack / WG_POST_PACK=p12: the _p12 form of the post kernel (post_recon2_r[_hp][_band] -> ..._p12). Default // p12 (measured in game with the banded path: -0.04 ms, exact) when nothing else is chosen for post / dec0, not // with recon=output or the fused dec1 + dec0, and only when the pack12 decoder is there (build.py builds it). @@ -1332,7 +1351,7 @@ struct Runtime { kernels_text += std::string(" model_map=") + (model_source == "raw" ? "none" : model_other_map ? "other" : "same"); if (native_direct_io) kernels_text += " native_io=direct_textures"; // Model L: its encoder and decoder (enc0= / dec0= above name Model M's, not loaded for Model L) - if (model_l) kernels_text += " model=L l_kernels=l_enc_values.co,l_ops.co,l_swin.co"; + if (model_l) kernels_text += " model=L l_kernels=l_enc_values.co,l_ops.co,l_swin.co,l_post.co l_post=l_post_recon"; return kernels_text.c_str(); } @@ -1661,6 +1680,14 @@ struct Runtime { if (post_last != pk && hist_bytes != 8) return fail(pk + " (" + std::to_string(hist_bytes) + "-byte history) cannot fall back to post_recon here"); +#ifdef WG_MODEL_L + if (model_l) { // Model L: l_post_recon (l_post.co, untagged) with post_recon's arguments + post_last = "l_post_recon"; + return launch("l_post_recon", grid, 256, + {&cnet, &A, &B, &C, &hist[cur], &mnear[nxt], &mcenter, &mfar, &w, &hist[nxt], &reduced, &display, + &p, &expo}); + } +#endif if (uses_prep(post_last)) { // pre-pass (one record per render pixel), then post_recon5* / 6* if (!launch((post_pre + "post_prep").c_str(), cdiv((long long)rw * rh, 256), 256, {&cnet, &A, &B, &C, &prep, &p, &expo}) || diff --git a/clean/tests/runtime_frame_trace.cpp b/clean/tests/runtime_frame_trace.cpp index 8f299a55..071cf224 100644 --- a/clean/tests/runtime_frame_trace.cpp +++ b/clean/tests/runtime_frame_trace.cpp @@ -11,6 +11,7 @@ // --sizes=WxH,... the render size of frame 0, 1, ... (repeated; default: the maximum every frame) // --reset=f,... frames the game asks for a reset (WgRtFrame.reset = 1) // --fail=@ that kernel's launch fails in that frame (the device returns false) +// --hdr=<0|1> WgRtConfig.hdr (default 0) // // Prints the create result and the allocations ("alloc b "), what create uploads ("upload b+ // ": the weight blobs, so a test can tell which blob an address holds), the code objects @@ -18,7 +19,8 @@ // (wg_rt_info) and one line per // launch: "launch ", followed for the kernels of the table below by " | " and the decoded // arguments: a device address as b+ (allocation k) or 0, an int as its value, the encoder and decoder -// parameters as E{rw,rh,fw,fh,frame,reset} / D{rw,rh,fw,fh,reset}. A failed frame prints what was launched up to the +// parameters as E{rw,rh,fw,fh,frame,reset} / D{rw,rh,fw,fh,reset}, the reconstruction's as P{rw,rh,dw,dh,reset}. +// A failed frame prints what was launched up to the // failure and "ERR frame : "; the next frame runs. After each frame, "name b+ " // for every buffer name the runtime knows (wg_rt_buffer; kNames below), so the launches' addresses can be checked // against the buffers' names. "ERR " at create. The code object files are not @@ -49,6 +51,11 @@ struct EncParams { float f[19]; }; static_assert(sizeof(EncParams) == 116, "EncParams"); +struct PostParams { // kernels/post.hip WgPostParams + int rw, rh, dw, dh, reset, pad0, pad1, pad2; + float f[8]; +}; +static_assert(sizeof(PostParams) == 64, "PostParams"); struct DecParams { int rw, rh, fw, fh, reset, pad0; float f[8]; @@ -71,6 +78,7 @@ const Signature kSignatures[] = { {"l_project_gather", "pppii"}, {"l_dec_project", "pppii"}, {"l_coef", "ppppppD"}, + {"l_post_recon", "ppppppppppppPp"}, // Model M's post_recon arguments (clean/l/FORMATS.md step 5) }; const char* kinds_of(const std::string& kernel) { @@ -176,6 +184,12 @@ class RecordingDevice : public Device { std::to_string(e.fh) + "," + std::to_string(e.frame) + "," + std::to_string(e.reset) + "}"; break; } + case 'P': { + const PostParams& q = *static_cast(args[i]); + line += "P{" + std::to_string(q.rw) + "," + std::to_string(q.rh) + "," + std::to_string(q.dw) + "," + + std::to_string(q.dh) + "," + std::to_string(q.reset) + "}"; + break; + } case 'D': { const DecParams& d = *static_cast(args[i]); line += "D{" + std::to_string(d.rw) + "," + std::to_string(d.rh) + "," + std::to_string(d.fw) + "," + @@ -262,6 +276,8 @@ int main(int argc, char** argv) { } } else if (s.compare(0, 8, "--reset=") == 0) { for (const std::string& f : split(s.substr(8), ',')) resets.insert(std::atoi(f.c_str())); + } else if (s.compare(0, 6, "--hdr=") == 0) { + cfg.hdr = std::atoi(s.c_str() + 6); } else if (s.compare(0, 7, "--fail=") == 0) { fail = s.substr(7); } else if (s.compare(0, 2, "--") == 0) {