From f7449e1e2f7712e8a0fde4d63c8168e178d6940a Mon Sep 17 00:00:00 2001
From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com>
Date: Sat, 14 Nov 2026 04:58:40 -0500
Subject: [PATCH 1/3] Model L's own reconstruction on the D3D12 frame (runtime
option l_post=own)
Native D3D12, Model L: l_post=own runs l_post_recon_p12 (l_coef_pack12's images; l_coef + l_post_recon
where the pack12 kernels do not fit), then the unbanded analytic filter w64/post_filter_c1_s4, in place of
Model M's fused pack12 post. The default stays Model M's fused post (l_post=m); the kernels line names
the route (l_post=l_post_recon / m_pack12).
Direct-resource route (games): two D3D12-only kernels (dx12/kernels/l_post_direct.hlsl) stage the game's
colour into color_net with the staged route's values (native_direct_normalized_color) and store the
filter's FP32 output into the game's texture (native_direct_scale for pi, framebuffer_half bytes with
output_rne16, alpha 1). The direct route then gives the staged route's bytes.
Checks: CPU recorder (test_l_runtime_frame D3D12.test_own_post / test_own_post_refusals), and the GPU
check clean/l/runner/l_d3d12_own_post_check.py against the HIP frame and the reference.
Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com>
---
clean/dx12/kernels/l_post_direct.hlsl | 60 ++++++
clean/l/runner/l_d3d12_own_post_check.py | 171 ++++++++++++++++++
clean/l/runtime/tests/test_l_runtime_frame.py | 71 +++++++-
clean/runtime/runtime.cpp | 94 ++++++++--
clean/tests/runtime_frame_trace.cpp | 2 +
5 files changed, 381 insertions(+), 17 deletions(-)
create mode 100644 clean/dx12/kernels/l_post_direct.hlsl
create mode 100644 clean/l/runner/l_d3d12_own_post_check.py
diff --git a/clean/dx12/kernels/l_post_direct.hlsl b/clean/dx12/kernels/l_post_direct.hlsl
new file mode 100644
index 00000000..b52949ae
--- /dev/null
+++ b/clean/dx12/kernels/l_post_direct.hlsl
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: MIT
+// Model L's own post on the direct-resource route (runtime option l_post=own, native D3D12): the two D3D12-only steps
+// around l_post_recon_p12 -> w64/post_filter_c1_s4, which read and write buffers (no direct forms). With them the direct
+// frame gives the staged route's bytes: the staged route converts the game's textures into dense buffers (the
+// Converter's input pass, color_divide when pi != 1), runs the same kernels, then scale_rgb when pi != 1 and the
+// Converter's output pass into the game's texture.
+// l_direct_color: dense texel i < npix of the game's colour texture -> color_net (RGBA f32, 16 bytes):
+// native_direct_normalized_color(i), the staged colour buffer's value (alpha +0; divided by pi as color_divide).
+// l_direct_store: output pixel i < ow oh of the filter's FP32 RGB (out, 12 bytes a pixel) -> the game's output texture:
+// times pi as scale_rgb (native_direct_scale) when pi != 1, then the typed store with alpha 1 as the Converter's
+// output pass; with nd_output_rne16 (RGBA16F output-copy rows) the stored value is framebuffer_half's (+-65504 clamp,
+// round to nearest even), as post.hlsl's direct forms store it (pd_out16).
+// Grids: cdiv(npix, 256) and cdiv(ow oh, 256) workgroups of 256 lanes.
+//
+// @wg-kernel l_direct_color co=- block=256 wave=32 args=p,u rs=direct defines=WG_DIRECT_TEXTURES=1,LPD_COLOR=1
+// @wg-kernel l_direct_store co=- block=256 wave=32 args=p,u,u rs=direct defines=WG_DIRECT_TEXTURES=1,LPD_STORE=1
+#include "wg_platform.hlsli"
+#include "native_direct.hlsli"
+
+#if defined(LPD_COLOR)
+WG_ARGS_BEGIN
+ uint color_net; // pointer
+ int npix;
+WG_ARGS_END
+
+WG_KERNEL(l_direct_color)
+{
+ WG_SETUP();
+ const uint i = wg_block_id() * 256u + wg_lane_id();
+ if ((int)i >= npix) return;
+ wg_st128(color_net, i * 16u, asuint(native_direct_normalized_color(i)));
+}
+
+#elif defined(LPD_STORE)
+WG_ARGS_BEGIN
+ uint out_p; // pointer
+ int ow, oh;
+WG_ARGS_END
+
+// post.hip framebuffer_half's clamp (compares: a NaN passes), then wg_f2h (round to nearest even, NaN kept)
+float lpd_half(float v)
+{
+ const float c = select(v > 65504.0f, 65504.0f, select(v < -65504.0f, -65504.0f, v));
+ return f16tof32(wg_f2h(c));
+}
+
+WG_KERNEL(l_direct_store)
+{
+ WG_SETUP();
+ const uint i = wg_block_id() * 256u + wg_lane_id();
+ if ((int)i >= ow * oh) return;
+ const uint y = i / (uint)ow, x = i - y * (uint)ow;
+ float3 v = float3(wg_ldf(out_p, i * 12u), wg_ldf(out_p, i * 12u + 4u), wg_ldf(out_p, i * 12u + 8u));
+ if (nd_color_pi != 1.0f)
+ v = float3(native_direct_scale(v.x, nd_color_pi), native_direct_scale(v.y, nd_color_pi),
+ native_direct_scale(v.z, nd_color_pi));
+ if (nd_output_rne16 != 0u) v = float3(lpd_half(v.x), lpd_half(v.y), lpd_half(v.z));
+ native_direct_output[uint2(x, y)] = float4(v, 1.0f);
+}
+#endif
diff --git a/clean/l/runner/l_d3d12_own_post_check.py b/clean/l/runner/l_d3d12_own_post_check.py
new file mode 100644
index 00000000..be27551d
--- /dev/null
+++ b/clean/l/runner/l_d3d12_own_post_check.py
@@ -0,0 +1,171 @@
+# SPDX-License-Identifier: MIT
+"""Model L (issue #89): the native D3D12 frame with Model L's own reconstruction (runtime option l_post=own:
+l_post_recon_p12, then the analytic filter w64/post_filter_c1_s4) against the HIP frame and the CPU reference, on the
+frames, models and build of clean/l/runner/l_frame_check.py.
+
+ python clean/l/runner/l_frame_check.py --build
--rocm --prepare # CPU, once
+ python clean/l/runner/l_frame_check.py --build --rocm --run # GPU: the HIP run
+ python clean/l/runner/l_d3d12_own_post_check.py --build --run # GPU, inside a GPU slot
+ python clean/l/runner/l_d3d12_own_post_check.py --build --run --stages # GPU, inside a GPU slot
+ python clean/l/runner/l_d3d12_own_post_check.py --build --compare --json out.json # CPU
+
+/dx12 holds the D3D12 ports (python clean/dx12/build_dx12.py --out ). --run runs l_frame_check's frame
+sequence for Model L on D3D12 with l_post=own ("d3d12_own"); with --stages the D3D12 fusions are off
+(WG_D3D12_FUSION=none: every stage output written but b0 ... b4, "d3d12_own_stages"). --compare checks:
+- against the HIP run (l_frame_check's "default", which runs l_post_recon and the same filter): every output both
+ backends write, byte for byte, the post outputs (history, reduced history, display image) and the output included;
+ the pack12 images unpacked against HIP's A, B, C. Outputs all zero on D3D12 and not on HIP are reported as not
+ written there (the fusions keep some maps on chip), not as differing. Negative control: one output bit flipped in a
+ copy of a D3D12 frame must show as one differing value;
+- the "stages" run per stage against the reference on its own inputs, as l_frame_check --compare, with the post against
+ Model L's reconstruction (clean/l/reference/recon.py) under Model L's post acceptance (clean/l/runner/l_post_check.py,
+ per frame and over the run) and the filter against recon.output_filter; b5 as one unit (the bottleneck megakernel
+ writes only b5), and where that chained reference parts from the runtime, b5 per stage on HIP's six-launch run
+ (l_frame_check's "stages"), which must hold the same b5 bytes;
+- the default kernels against the "stages" run, byte for byte.
+Output: counts only, no timing.
+"""
+import argparse
+import copy
+import json
+import os
+import pickle
+import sys
+
+import numpy as np
+
+HERE = os.path.dirname(os.path.abspath(__file__))
+ROOT = os.path.dirname(os.path.dirname(os.path.dirname(HERE)))
+sys.path.insert(0, ROOT)
+
+from clean.l.runner import l_frame_check as F # noqa: E402
+from clean.l.runner import l_post_check as LP # noqa: E402
+
+OPTIONS = "model=L;l_post=own"
+
+
+def run(build, special=True, stages=False):
+ if stages:
+ os.environ["WG_D3D12_FUSION"] = "none" # before the runtime library is loaded
+ from clean.runner.pipeline import Pipeline
+ config = "d3d12_own_stages" if stages else "d3d12_own"
+ p = Pipeline(build, "", F.model_dir(build), F.MAX_RENDER, F.OUTPUT,
+ {"depth_inverted": False, "auto_exposure": True, "hdr": True}, backend="d3d12",
+ map_path=F.l_map(build), check_packing=False, kernels={"options": OPTIONS})
+ rng = np.random.default_rng(F.SEED)
+ frames = []
+ for (rw, rh), reset in F.SEQUENCE:
+ fr = F.frame_inputs(rng, rw, rh, reset, special)
+ out = p.frame(fr, debug=True)
+ raw = {n: p._get(n, np.uint8).copy() for n in ("dec0.A", "dec0.C")}
+ frames.append({"inputs": fr, "out": out, "raw": raw})
+ rec = {"model": "L", "config": config, "kernels": p.kernels(), "notes": p.notes(),
+ "model_source": p.model_source, "dw": p.dw, "dh": p.dh, "ow": p.ow, "oh": p.oh,
+ "taps": p.output_taps(), "frames": frames}
+ with open(os.path.join(build, f"lframe_L_{config}.pkl"), "wb") as f:
+ pickle.dump(rec, f)
+ print("L", config, "frames", len(frames), "post", rec["kernels"].get("post"), "l_post", rec["kernels"].get("l_post"),
+ "post_last", rec["kernels"].get("post_last"), "filter_last", rec["kernels"].get("filter_last"))
+
+
+def _load(build, config):
+ with open(os.path.join(build, f"lframe_L_{config}.pkl"), "rb") as f:
+ return pickle.load(f)
+
+
+def with_display(hip, dx, frames):
+ """compare_backends leaves the display image out (Model M's fused post writes none); both runs here write it:
+ values differing, per frame."""
+ for m, a, b in zip(frames, hip["frames"], dx["frames"]):
+ u, v = a["out"]["post"]["display"], b["out"]["post"]["display"]
+ n = -1 if u is None or v is None or u.shape != v.shape else int(np.sum(u.view(np.uint16) != v.view(np.uint16)))
+ m["differ"]["post.display"] = n
+ m["identical"] = m["identical"] and n == 0
+ return frames
+
+
+def compare(build, json_path):
+ with open(os.path.join(F.model_dir(build), "params.pkl"), "rb") as f:
+ params = pickle.load(f)["L"]
+ hip, own, st = _load(build, "default"), _load(build, "d3d12_own"), _load(build, "d3d12_own_stages")
+ res = {"kernels_d3d12": own["kernels"], "kernels_hip": hip["kernels"]}
+
+ vs_hip = with_display(hip, own, F.compare_backends(hip, own, same_post=True))
+ res["vs_hip"] = {"frames": vs_hip, "identical_frames": sum(f["identical"] for f in vs_hip),
+ "outputs_compared": sorted({n for f in vs_hip for n in f["differ"]}),
+ "differing": sorted({k for f in vs_hip for k, v in f["differ"].items() if v}),
+ "unwritten_d3d12": sorted({n for f in vs_hip for n in f["unwritten_d3d12"]})}
+ bad = copy.deepcopy(own)
+ o = bad["frames"][1]["out"]["out"]
+ o.view(np.uint32).reshape(-1)[o.size // 2] ^= 1
+ caught = with_display(hip, bad, F.compare_backends(hip, bad, same_post=True))[1]["differ"].get("out")
+ res["negative_control"] = {"flipped": "out, frame 1, one bit", "out_differing": caught, "caught": caught == 1}
+
+ st = F.fill_pack12(st) # dec0's A, B, C from the pack12 images (the debug view has none)
+ per_stage = F.compare_model(st, params, bneck_unit=True)
+ post_run = {k: LP.l_post_run([m["post"][k] for m in per_stage if k in m["post"]]) for k in ("history", "display")}
+ # b5 as one unit (b0 ... b5 chained in the reference from enc2.merged: the megakernel writes only b5) can part from
+ # the runtime where one FP8 rounding in the chain goes the other way. Such a frame's b5 is checked per stage
+ # instead: the HIP six-launch run ("stages", every block output written) has the same b5 bytes and its b5
+ # passes against the reference on its own b4.
+ hip_st = _load(build, "stages")
+ hip_ps = None
+ b5_via_hip = []
+ for k, m in enumerate(per_stage):
+ if m["stage_pass"].get("b5", True):
+ continue
+ hip_ps = hip_ps or F.compare_model(hip_st, params)
+ same = own["frames"][k]["out"]["b5"]["out"].tobytes() == hip_st["frames"][k]["out"]["b5"]["out"].tobytes()
+ if same and hip_ps[k]["stage_pass"]["b5"]:
+ m["stage_pass"]["b5"] = True
+ m["b5_checked"] = "per stage on HIP's six-launch run (same b5 bytes): pass"
+ m["pass"] = all(m["stage_pass"].values())
+ b5_via_hip.append(k)
+ res["d3d12_per_stage"] = {"frames": per_stage, "summary": F.summary(per_stage),
+ "passed": sum(m["pass"] for m in per_stage), "post_run": post_run,
+ "b5_unit_differs_checked_per_stage_on_hip": b5_via_hip}
+ fused = F.compare_backends(st, own)
+ res["d3d12_default_vs_stages"] = {"frames": fused, "identical_frames": sum(f["identical"] for f in fused),
+ "differing": sorted({k for f in fused for k, v in f["differ"].items() if v}),
+ "unwritten_default": sorted({n for f in fused for n in f["unwritten_d3d12"]})}
+ n = len(own["frames"])
+ res["pass"] = (res["vs_hip"]["identical_frames"] == n and res["negative_control"]["caught"] and
+ res["d3d12_per_stage"]["passed"] == n and all(r["pass"] for r in post_run.values()) and
+ res["d3d12_default_vs_stages"]["identical_frames"] == n and
+ own["kernels"].get("l_post") == "l_post_recon")
+ with open(json_path, "w") as f:
+ json.dump(res, f, indent=1, default=lambda x: x.item() if hasattr(x, "item") else str(x))
+ print("L d3d12 l_post=own:", "pass" if res["pass"] else "FAIL", "frames", n)
+ print(" against the HIP run: byte-identical", res["vs_hip"]["identical_frames"], "of", n, "differing",
+ res["vs_hip"]["differing"], "not written on D3D12", res["vs_hip"]["unwritten_d3d12"])
+ print(" negative control:", json.dumps(res["negative_control"]))
+ print(" per stage against the reference (Model L's post acceptance):", res["d3d12_per_stage"]["passed"], "of", n,
+ "post over the run", json.dumps({k: r["pass"] for k, r in post_run.items()}),
+ "(b5 as one unit off, checked per stage on HIP's six-launch run with the same bytes: frames",
+ res["d3d12_per_stage"]["b5_unit_differs_checked_per_stage_on_hip"], ")")
+ print(" default kernels against the stages run: byte-identical", res["d3d12_default_vs_stages"]["identical_frames"],
+ "of", n, "differing", res["d3d12_default_vs_stages"]["differing"])
+ for m in per_stage:
+ if not m["pass"]:
+ print(" frame", m["frame"], "failing:", [s for s, ok in m["stage_pass"].items() if not ok])
+ return 0 if res["pass"] else 1
+
+
+def main(argv=None):
+ ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
+ ap.add_argument("--build", required=True)
+ g = ap.add_mutually_exclusive_group(required=True)
+ g.add_argument("--run", action="store_true")
+ g.add_argument("--compare", action="store_true")
+ ap.add_argument("--stages", action="store_true", help="--run with the D3D12 fusions off")
+ ap.add_argument("--plain", action="store_true", help="--run: frames without special values (as l_frame_check)")
+ ap.add_argument("--json")
+ a = ap.parse_args(argv)
+ if a.run:
+ run(a.build, special=not a.plain, stages=a.stages)
+ return 0
+ return compare(a.build, a.json or os.path.join(a.build, "l_d3d12_own_post.json"))
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/clean/l/runtime/tests/test_l_runtime_frame.py b/clean/l/runtime/tests/test_l_runtime_frame.py
index 0c2e1250..41384290 100644
--- a/clean/l/runtime/tests/test_l_runtime_frame.py
+++ b/clean/l/runtime/tests/test_l_runtime_frame.py
@@ -167,7 +167,7 @@ def base(kernel):
"""A Model L launch's kernel without its clamp-free suffix; the pack12 coefficient store (native D3D12) as l_coef,
whose arguments it shares."""
k = kernel.replace("_nc", "")
- return "l_coef" if k == "l_coef_pack12" else k
+ return "l_coef" if k == "l_coef_pack12" else "l_post_recon" if k == "l_post_recon_p12" else k
def check_roles(t, f, digests, l_post=True):
@@ -356,8 +356,8 @@ def check_frame(self, t, f, roles=True, l_post=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 if l_post else 0) # (native D3D12: Model M's fused pack12 post for now)
+ post = [ln for ln in ls if base(ln.kernel) == "l_post_recon"]
+ self.assertEqual(len(post), 1 if l_post else 0) # (native D3D12: Model M's fused pack12 post by default)
if l_post:
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))
@@ -561,6 +561,71 @@ def test_hip_unchanged(self):
self.assertTrue(run("model=L", build_files=FT.SHIPPING, env={"WG_POST_PACK": "p12"})[0].startswith(
"ERR create: Model L: post_pack=p12"))
+ # runtime option l_post=own: Model L's own reconstruction on native D3D12 (its D3D12 ports, then the analytic filter)
+ OWN = [("l_post_recon_p12", "l_post.co"), ("l_post_recon", "l_post.co"), ("l_coef", "l_ops.co"),
+ ("w64/post_filter_c1_s4", "post_w64.co")]
+
+ def own_ports(self):
+ have = full_ports()
+ return have + [kc for kc in self.OWN if kc not in have]
+
+ def test_own_post(self):
+ """l_post=own: l_post_recon_p12 on the frames l_coef_pack12 stores, with post_recon's arguments (the #111 role
+ checks), then the unbanded filter w64/post_filter_c1_s4 from its display image into the output; no fused post.
+ A frame the pack12 kernels do not take (render 180 x 108 = D) runs l_coef and l_post_recon."""
+ sizes = "--sizes=96x64,96x64,180x108,80x48,77x45,180x108"
+ t = FT.Trace(run("model=L;l_post=own", frames=6, build_files=FT.SHIPPING, rw=180, rh=108,
+ flags=("--backend=3", sizes, "--reset=4"), manifest=self.own_ports()))
+ self.assertEqual(t.create, "create ok model L")
+ self.assertEqual(t.unknown(), [])
+ self.assertEqual(t.kernels.get("l_post"), "l_post_recon")
+ prev = None
+ for f in t.frames:
+ self.assertIsNone(f["error"])
+ info, ls = f["info"], f["launches"]
+ names = [ln.kernel for ln in ls]
+ p12 = (info["rw"], info["rh"]) != (180, 108)
+ self.assertIn("l_coef_pack12" if p12 else "l_coef", names)
+ self.assertNotIn("l_coef" if p12 else "l_coef_pack12", names)
+ self.assertFalse([n for n in names if "post_tf" in n or "post_recon2" in n or n.endswith("_band")], names)
+ post = [ln for ln in ls if ln.kernel == ("l_post_recon_p12" if p12 else "l_post_recon")]
+ self.assertEqual(len(post), 1, names)
+ lev, coef = Arguments.check_frame(self, t, f) # the roles of every argument, the filter after the post
+ flt = ls[ls.index(post[0]) + 1]
+ self.assertEqual(flt.kernel, "w64/post_filter_c1_s4")
+ self.assertEqual((flt.grid, flt.block), (cdiv(120, 40) * cdiv(cdiv(72, 2), 8), 64))
+ self.assertEqual(flt.args[:2], [f["names"]["post.display"], f["names"]["filter.out"]])
+ self.assertEqual(flt.args[2:6], [str((3 * 120 + 1) // 2), str((3 * 72 + 1) // 2), "120", "72"])
+ self.assertEqual(flt.args[7], f["names"]["exposure.pair"])
+ if p12:
+ self.assertGreaterEqual(t.extent(coef.args[1]), 8 * info["rw"] * info["rh"]) # pack12 image A
+ if prev:
+ self.assertEqual(lev.args[4], prev[1].args[4]) # the latent the last frame's store wrote
+ prev = (lev, coef)
+
+ def test_own_post_refusals(self):
+ def first(options, **kw):
+ return run(options, frames=1, build_files=FT.SHIPPING, **kw)[0]
+ d3d12 = dict(flags=("--backend=3",), manifest=self.own_ports())
+ self.assertEqual(first("model=L;l_post=yes", **d3d12),
+ "ERR create: l_post must be m or own (Model L on native D3D12)")
+ self.assertTrue(first("model=L;l_post=own", hdr=0, **d3d12).startswith(
+ "ERR create: Model L: l_post=own needs HDR colour"))
+ self.assertEqual(first("l_post=own", **d3d12), "ERR create: l_post=own is a Model L option (its own "
+ "reconstruction)")
+ for k, _ in self.OWN:
+ with self.subTest(port=k):
+ got = first("model=L;l_post=own", flags=("--backend=3",),
+ manifest=[kc for kc in self.own_ports() if kc[0] != k])
+ self.assertEqual(got, "ERR create: Model L on native D3D12 (l_post=own): not yet ported to D3D12: " + k)
+ # the default stays Model M's fused pack12 post (test_frame), named so; on HIP Model L runs its own post either way
+ self.assertEqual(FT.Trace(run("model=L", frames=1, build_files=FT.SHIPPING, **d3d12)).kernels.get("l_post"),
+ "m_pack12")
+
+ def hip(options):
+ return [re.sub(r"wg_lfb_\w+", "B", x) for x in run(options, frames=2, build_files=FT.SHIPPING)]
+ self.assertEqual(hip("model=L;l_post=own"), hip("model=L"))
+
class Refusals(unittest.TestCase):
def test_paths(self):
diff --git a/clean/runtime/runtime.cpp b/clean/runtime/runtime.cpp
index fe39bb6c..80bd8649 100644
--- a/clean/runtime/runtime.cpp
+++ b/clean/runtime/runtime.cpp
@@ -238,6 +238,11 @@ struct Runtime {
// Model L (runtime option model=L, builds with WG_MODEL_L): Model M's frame with Model L's encoder in step 2 and its
// decoder in step 4 (clean/l/FORMATS.md); the ten Swin stages, exposure and post are Model M's.
bool model_l = false;
+ // runtime option l_post=own (native D3D12): Model L's own reconstruction l_post_recon_p12 (l_post.co's D3D12 port),
+ // then the unbanded analytic filter w64/post_filter_c1_s4, in place of Model M's fused pack12 post (l_post=m, the
+ // default there for now). On the direct-resource route the colour is staged into color_net (l_direct_color) and the
+ // filter's FP32 output stored into the game's texture (l_direct_store; dx12/kernels/l_post_direct.hlsl).
+ bool l_own_post = false;
#ifdef WG_MODEL_L
// The maps of clean/l/FORMATS.md in three buffers (no kernel works in place):
// l_wide [fh + 4][fw + 4] x 64 bytes: q (FP8 halo, 32 bytes a pixel), then x2 (FP16 halo; q is dead after the
@@ -680,7 +685,11 @@ struct Runtime {
// Model L reads dec1's output map (dec1.out) in its decoder front: not the paths that keep it on chip
if (motion_outres == 1) return fail("Model L: motion_outres=original needs Model M's encoder (encoder_mo.co)");
d1d0_p12_opt = false;
+#ifdef WG_MODEL_L
+ if (l_own_post && c.backend == 3 && !l_own_post_setup(c)) return false;
+#endif
}
+ if (l_own_post && !model_l) return fail("l_post=own is a Model L option (its own reconstruction)");
if (!mid_setup(c)) return false;
if (!bneck_setup(c)) return false;
// D = O (fast mode, recon=output) runs without pack12, so dec1 + dec0 fused is the default there (bit-identical;
@@ -1074,6 +1083,10 @@ struct Runtime {
if (v != "auto" && v != "force") return fail("post_tf4 must be auto or force");
post_tf4_force = v == "force";
}
+ if (k == "l_post") {
+ if (v != "m" && v != "own") return fail("l_post must be m or own (Model L on native D3D12)");
+ l_own_post = v == "own";
+ }
if (k == "post_diet") post_diet = !(v == "0" || v == "off" || v == "false");
if (k == "tf_vdiet") tf_vdiet = !(v == "0" || v == "off" || v == "false");
if (k == "post_idx") post_idx = !(v == "0" || v == "off" || v == "false");
@@ -1143,10 +1156,14 @@ struct Runtime {
if (output_quality5 || output_quality9 || post_pre != "w64/" || (output_original && !has_taps))
return fail("native direct resources require the wave64 fused post: post_w64__orig.co with the model's "
"output-filter weights, or the analytic post_w64.co");
- if (recon_output || !p12 || !post_band || post_fused || out_f16 || post_wave != 64 ||
- post_kernel != "post_recon2_r_band_p12" || filter_kernel != "post_filter_c1_s4_band" ||
- !post_tf || !post_diet || !tf_vdiet || !post_sel || !post_idx || !post_trim)
- return fail("native direct resources support only post_tf_f32_p12_diet_vsit; alternate post/TF routes are unsupported");
+ // (Model L with l_post=own: l_post_recon_p12 and the unbanded filter on the staged colour, l_own_post_setup)
+ const bool own = model_l && l_own_post && !recon_output && p12 && !post_band && !post_fused && !out_f16 &&
+ post_wave == 64 && post_kernel == "post_recon2_r_p12" && filter_kernel == kDefaultFilter;
+ if (!own && (recon_output || !p12 || !post_band || post_fused || out_f16 || post_wave != 64 ||
+ post_kernel != "post_recon2_r_band_p12" || filter_kernel != "post_filter_c1_s4_band" ||
+ !post_tf || !post_diet || !tf_vdiet || !post_sel || !post_idx || !post_trim))
+ return fail("native direct resources support only post_tf_f32_p12_diet_vsit (Model L: or l_post=own); "
+ "alternate post/TF routes are unsupported");
if ((!d1d0_p12 && !model_l) || !dec_div.empty() || dec1_dec0 || bneck_variant != "bmegahg" ||
bneck_threads != 256 || bneck_groups != 128 || !mid_variant.empty() || swin_resident || force_clamp || fuse)
return fail("native direct resources require default pack12 decoder, bmegahg bottleneck and nonresident unclamped Swin routes");
@@ -1186,7 +1203,8 @@ struct Runtime {
return fail("native direct render size is outside the configured maximum");
if (ceil32(fr.rw) > 2 * fr.rw - 1 || ceil32(fr.rh) > 2 * fr.rh - 1)
return fail("native direct render size is too small for its padding (spec 03)");
- if (!tf_supported(fr.rw, fr.rh, dw, dh, kTfCap) && !tf4_supported(fr.rw, fr.rh, dw, dh))
+ // (l_post=own: l_post_recon_p12, or l_coef + l_post_recon where the pack12 kernels do not fit; any geometry)
+ if (!l_own_post && !tf_supported(fr.rw, fr.rh, dw, dh, kTfCap) && !tf4_supported(fr.rw, fr.rh, dw, dh))
return fail("native direct frame geometry is unsupported by post_tf_f32_p12_diet_vsit and post_tf4_dl_f32_p12_diet_vsit; "
"no fallback");
return true;
@@ -1374,7 +1392,10 @@ 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,l_post.co l_post=l_post_recon";
+ // (l_post: Model L's own reconstruction, or on native D3D12 by default Model M's fused pack12 post)
+ if (model_l)
+ kernels_text += std::string(" model=L l_kernels=l_enc_values.co,l_ops.co,l_swin.co,l_post.co l_post=") +
+ (d3d12_native && !l_own_post ? "m_pack12" : "l_post_recon");
return kernels_text.c_str();
}
@@ -1385,8 +1406,11 @@ struct Runtime {
const int ny = std::max(256, max_rh / 2);
// External buffer I/O keeps depth/motion/color_net for raw-input conversion. Direct textures need none of the
// five dense I/O buffers; their shader loads/stores use the caller's typed descriptors instead.
+ // (l_post=own on the direct route: the staged colour and the filter's FP32 output, l_direct_color / l_direct_store)
+ const bool own_direct = native_direct_io && l_own_post;
bool ok = (external_io || (alloc(&color, R * 16) && alloc(&out, On * 12))) &&
(native_direct_io || (alloc(&depth, R * 4) && alloc(&motion, R * 8) && alloc(&color_net, R * 16))) &&
+ (!own_direct || (alloc(&color_net, R * 16) && alloc(&out, On * 12))) &&
alloc(&expo, 32) &&
(!native_direct_io || (alloc(&f16lut, kF16LutBytes) && (dev->memset(f16lut, 0, 256) || dfail()))) &&
alloc(&expo_partials, 4 * (size_t)cdiv(ny, 4)) && alloc(&expo_computed, 4) && alloc(&expo_sync, 4) &&
@@ -1547,6 +1571,15 @@ struct Runtime {
// projections and pack12 coefficient store, and dec1's own block with its expand (Model M runs dec1 inside its fused
// decoder there; Model L's decoder reads dec1's map). Model M's other kernels are its native frame's.
std::string l_native_missing() const {
+ return manifest_missing({native_direct_io ? "lev/input_encoder_direct" : "lev/input_encoder",
+ "l_linear_32_32_relu", "l_linear_32_32_f16",
+ "l_swin_block_c32_enc_nc", "l_project_gather",
+ "swin_block_c64_add_ex_nc", "l_dec_project",
+ "l_swin_block_c32_dec_nc", "l_coef_pack12"});
+ }
+
+ // The kernels of `need` that /dx12/manifest.json lacks, comma-separated ("" = all there).
+ std::string manifest_missing(const std::vector& need) const {
std::string text, error, missing;
wgc::Json manifest;
std::vector have;
@@ -1558,15 +1591,35 @@ struct Runtime {
if (name && name->is_string()) have.push_back(name->text);
}
}
- const std::vector need = {native_direct_io ? "lev/input_encoder_direct" : "lev/input_encoder",
- "l_linear_32_32_relu", "l_linear_32_32_f16",
- "l_swin_block_c32_enc_nc", "l_project_gather",
- "swin_block_c64_add_ex_nc", "l_dec_project",
- "l_swin_block_c32_dec_nc", "l_coef_pack12"};
for (const std::string& k : need)
if (std::find(have.begin(), have.end(), k) == have.end()) missing += (missing.empty() ? "" : ", ") + k;
return missing;
}
+
+ // l_post=own on native D3D12 (after runtime_options): the unbanded pack12 route with Model L's reconstruction in
+ // place of Model M's fused post. p12 stays (l_coef_pack12 feeds l_post_recon_p12; frames the pack12 kernels do not
+ // take run l_coef and l_post_recon); post_recon_stage launches Model L's kernel, step 6 the unbanded default filter
+ // (w64/post_filter_c1_s4, the analytic build: Model L's file has no output-filter weights).
+ bool l_own_post_setup(const WgRtConfig& c) {
+ if (!c.hdr)
+ return fail("Model L: l_post=own needs HDR colour (WgRtConfig.hdr = 1): l_post_recon is HDR, its LDR form is "
+ "not written yet");
+ if (!p12 || post_kernel != "post_recon2_r_band_p12" || filter_kernel != "post_filter_c1_s4_band" || out_f16)
+ return fail("Model L: l_post=own replaces the default pack12 post route (post " + post_kernel + ", filter " +
+ filter_kernel + ")");
+ std::vector need = {"l_post_recon_p12", "l_post_recon", "l_coef", "w64/post_filter_c1_s4"};
+ if (native_direct_io) {
+ need.push_back("l_direct_color");
+ need.push_back("l_direct_store");
+ }
+ const std::string missing = manifest_missing(need);
+ if (!missing.empty()) return fail("Model L on native D3D12 (l_post=own): not yet ported to D3D12: " + missing);
+ post_kernel = "post_recon2_r_p12"; // unbanded pack12 route; the reconstruction itself is Model L's (post_recon_stage)
+ post_band = false;
+ filter_kernel = kDefaultFilter;
+ if (!filter_shape(filter_kernel, &filter_tile_x, &filter_s)) return fail("unknown output filter " + filter_kernel);
+ return true;
+ }
#endif
// The arguments of a kernel that runs the 5-tap output filter (post_filter*, post_tf*, post_fused*): with
@@ -1733,8 +1786,9 @@ struct Runtime {
"-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,
+ // (native D3D12, l_post=own: l_post_recon_p12 on the frames whose coefficients l_coef_pack12 stored)
+ post_last = p12_frame ? "l_post_recon_p12" : "l_post_recon";
+ return launch(post_last.c_str(), grid, 256,
{&cnet, &A, &B, &C, &hist[cur], &mnear[nxt], &mcenter, &mfar, &w, &hist[nxt], &reduced, &display,
&p, &expo});
}
@@ -2205,6 +2259,15 @@ struct Runtime {
}
}
if (!mark(3)) return false;
+ // l_post=own on the direct-resource route: Model L's reconstruction reads the colour from a buffer, so the game's
+ // texture is staged into color_net first, with the staged route's values (native_direct_normalized_color: the
+ // Converter's dense RGBA with alpha +0, divided by pi as color_divide when pi != 1)
+ const bool own_direct = native_direct_io && l_own_post;
+ if (own_direct) {
+ int npix = rw * rh;
+ if (!launch("l_direct_color", cdiv(npix, 256), 256, {&color_net, &npix})) return false;
+ cnet = color_net;
+ }
// 5. output reconstruction (spec 06, kernels/post.hip): history copy f -> copy 1 - f, reduced history, display.
// The exposures come from the GPU pair (expo); the values in the parameters are only the host-side default.
{
@@ -2363,7 +2426,10 @@ struct Runtime {
return false;
filter_last = fk;
}
- if (hp <= 0 && pi != 1.0f) { // spec 10 draft 2: the filter's result times pi
+ if (own_direct) { // the game's output texture: times pi (as scale_rgb) and the Converter's typed store
+ int ow_i = ow, oh_i = oh;
+ if (!launch("l_direct_store", cdiv(ow * oh, 256), 256, {&outp, &ow_i, &oh_i})) return false;
+ } else if (hp <= 0 && pi != 1.0f) { // spec 10 draft 2: the filter's result times pi
int n3 = 3 * ow * oh;
float p_ = pi;
if (!launch("scale_rgb", cdiv(n3, 256), 256, {&outp, &p_, &n3})) return false;
diff --git a/clean/tests/runtime_frame_trace.cpp b/clean/tests/runtime_frame_trace.cpp
index 0c0de8c2..58af8f1e 100644
--- a/clean/tests/runtime_frame_trace.cpp
+++ b/clean/tests/runtime_frame_trace.cpp
@@ -81,6 +81,8 @@ const Signature kSignatures[] = {
{"l_coef", "ppppppD"},
{"l_post_recon", "ppppppppppppPp"}, // Model M's post_recon arguments (clean/l/FORMATS.md step 5)
{"l_coef_pack12", "ppppppD"}, // the same arguments (B unused)
+ {"l_post_recon_p12", "ppppppppppppPp"}, // the same arguments (A, C: pack12 images; B unused)
+ {"post_filter_c1_s4", "ppiiiiip"}, // display, out, dw, dh, ow, oh, dec_scale (its bits), exposure pair
};
const char* kinds_of(const std::string& kernel) {
From f425e99c3480565ba59f4684032ceb91c79950a0 Mon Sep 17 00:00:00 2001
From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com>
Date: Sat, 14 Nov 2026 06:58:19 -0500
Subject: [PATCH 2/3] l_post=own: offline GPU checks of the fallback route and
the RGBA16F copy store; kernel sets
- clean/l/runner/l_d3d12_fallback_check.py: the fallback route (recon2_supported false: l_coef,
l_post_recon, w64/post_filter_c1_s4), chained as the frame runs it, against HIP byte for byte, with edge
tiles at sizes that are not tile multiples; sentinels, slack, negative control, Model M control.
- clean/dx12/tests/post_tf_d3d12_check.py --store: HIP's output-copy rows (post_filter_c1_s4_o16 /
post_filter_h) against w64/post_filter_c1_s4 then l_direct_store with output_rne16 = 1 into an RGBA16F
texture, with FP16 subnormals, values past 65504, infinities, NaN and rounding ties.
- kernel_replay: direct_params takes color_pi and output_rne16.
- kernel_sets.json regenerated: l_direct_color and l_direct_store in Model L's set (Model M: 46).
Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com>
---
clean/dx12/kernel_sets.json | 8 +
clean/dx12/tests/kernel_replay.cpp | 8 +-
clean/dx12/tests/post_tf_d3d12_check.py | 161 +++++++++++++++-
clean/l/runner/l_d3d12_fallback_check.py | 232 +++++++++++++++++++++++
4 files changed, 406 insertions(+), 3 deletions(-)
create mode 100644 clean/l/runner/l_d3d12_fallback_check.py
diff --git a/clean/dx12/kernel_sets.json b/clean/dx12/kernel_sets.json
index 9c62b5d9..a1d784b4 100644
--- a/clean/dx12/kernel_sets.json
+++ b/clean/dx12/kernel_sets.json
@@ -198,6 +198,14 @@
"l_dec_project",
"l_ops.co"
],
+ [
+ "l_direct_color",
+ "-"
+ ],
+ [
+ "l_direct_store",
+ "-"
+ ],
[
"l_linear_32_32_f16",
"l_ops.co"
diff --git a/clean/dx12/tests/kernel_replay.cpp b/clean/dx12/tests/kernel_replay.cpp
index e663d49b..2800c1f5 100644
--- a/clean/dx12/tests/kernel_replay.cpp
+++ b/clean/dx12/tests/kernel_replay.cpp
@@ -20,8 +20,8 @@
// by row (tightly packed), "-" for none (the output, written by no
// kernel replayed here). The case binds them as one direct frame
// (NativeD3D12Ext::bind_direct_frame) around its launch.
-// direct_params the frame's active render rectangle; motion scale 1, offset 0,
-// color_pi 1, no exposure texture
+// direct_params [pi [rne16]] the frame's active render rectangle; motion scale 1, offset 0,
+// no exposure texture; color_pi (default 1) and output_rne16 (default 0)
// direct_out after the launch, the output texture's texels are written to ,
// row by row (tightly packed); its initial texels come from the output
// slot's file (a sentinel fill, so that an unwritten texel shows)
@@ -374,6 +374,10 @@ int main(int argc, char** argv) {
c.direct.push_back(t);
} else if (w == "direct_params") {
s >> c.dparams.rx >> c.dparams.ry >> c.dparams.rw >> c.dparams.rh;
+ float pi = 1.0f;
+ unsigned rne16 = 0;
+ if (s >> pi) c.dparams.color_pi = pi; // optional: color_pi, then output_rne16
+ if (s >> rne16) c.dparams.output_rne16 = rne16;
} else if (w == "direct_out") {
s >> c.direct_out;
} else if (w == "arg") {
diff --git a/clean/dx12/tests/post_tf_d3d12_check.py b/clean/dx12/tests/post_tf_d3d12_check.py
index 610a137a..a1960d87 100644
--- a/clean/dx12/tests/post_tf_d3d12_check.py
+++ b/clean/dx12/tests/post_tf_d3d12_check.py
@@ -24,6 +24,10 @@
of post_w64.co: display image -> FP32 output, dec_scale from the argument or the exposure pair;
- control: w64/post_tf_f32_p12_diet_vsit (original-filter port, five FP16 weights as arguments) vs post_w64__orig.co.
The same name as the first: the D3D12 device runs the port of the code object the case loads under "w64".
+--direct: the direct forms (DIRECT). --store: Model L's own post on the direct route, its output-copy store
+(STORE_FRAMES): HIP's RGBA16F output-copy rows (post_filter_c1_s4_o16 / post_filter_h) against w64/post_filter_c1_s4
+then l_direct_store with output_rne16 = 1 into an RGBA16F texture (filled with 0x5A beforehand: every texel must be
+written), with FP16 subnormals, values past 65504, infinities, NaN and rounding ties among the stored values.
"""
import argparse
import json
@@ -260,6 +264,159 @@ def judge(name, case, want, ow, oh, prefix=""):
return res
+# ---- Model L's own post on the direct route: the output-copy store (l_direct_store, output_rne16 = 1) --------------
+# HIP's output copy of the same display image: post_filter_c1_s4_o16 (pi = 1) or post_filter_h (pi folded in, pitch 8 O)
+# -> RGBA16F rows, framebuffer_half((Dec dec_scale) pi). D3D12: w64/post_filter_c1_s4 (FP32), then l_direct_store
+# (dx12/kernels/l_post_direct.hlsl) on HIP's FP32 output into an RGBA16F output texture with color_pi and output_rne16 = 1.
+# dec_scale / pi choices drive the stored values into FP16 subnormals, past 65504 (clamp), to +-infinity (clamp) and
+# NaN (by the argument, with a payload, or from the exposure pair); ties of the FP16 rounding come from the many values.
+# (ow, oh, pi, dec_scale: a float, or "expo_nan" = the exposure pair's e is NaN)
+STORE_FRAMES = [(96, 54, 1.0, None), (99, 57, 1.375, None), (161, 83, 1.0, 2.0 ** -22), (161, 83, 1.375, 2.0 ** -22),
+ (1281, 140, 1.0, 2.0 ** -8), (1281, 140, 1.25, 2.0 ** -12), (40, 16, 1.0, float("inf")),
+ (40, 16, 1.375, float("-inf")), (7, 5, 1.0, "expo_nan"), (99, 57, 1.375, "nan_payload"),
+ (161, 83, 30000.0, None)]
+TEX_FILL16 = 0x5A5A
+
+
+def _s32(bits):
+ return bits - (1 << 32) if bits >= 1 << 31 else bits
+
+
+def store_cases(rng):
+ """[(HIP FP32 filter case, HIP output-copy case, D3D12 filter case, pi, ow, oh, label)]"""
+ out = []
+ for ow, oh, pi, ds in STORE_FRAMES:
+ dw, dh = MP.recon_size_for(ow), MP.recon_size_for(oh)
+ disp = _display(rng, dw, dh)
+ e = float(np.float32(rng.uniform(0.5, 2.0)))
+ expo = None
+ if ds is None:
+ bits = _f32_bits(np.float32(0.01 / e))
+ elif ds == "expo_nan":
+ bits, expo = _f32_bits(np.float32(0.01 / e)), np.array([np.nan, 1.0, 0, 0, 0], np.float32).tobytes()
+ elif ds == "nan_payload":
+ bits = 0xFFA00001 # negative signalling NaN with a payload
+ else:
+ bits = _f32_bits(np.float32(ds))
+ tail = [("u32", _s32(bits)), ("buf", expo) if expo is not None else ("null",)]
+ n32, n16 = ow * oh * 12, ow * oh * 8
+ base = [("buf", disp), None, ("u32", dw), ("u32", dh), ("u32", ow), ("u32", oh)] + tail
+ f32 = {"kernel": "w64/post_filter_c1_s4", "co": "post_w64.co", "tag": "w64", "hip_kernel": "post_filter_c1_s4",
+ "grid": PZ.PS._cdiv(ow, 40) * PZ.PS._cdiv(PZ.PS._cdiv(oh, 2), 8), "block": 64,
+ "args": [base[0], ("out", n32 + SLACK)] + base[2:],
+ "defined": [np.concatenate([np.ones(n32, bool), np.zeros(SLACK, bool)])]}
+ if pi == 1.0:
+ o16 = dict(f32, kernel="post_filter_c1_s4_o16", hip_kernel="post_filter_c1_s4_o16",
+ args=[base[0], ("out", n16 + SLACK)] + base[2:])
+ else:
+ o16 = dict(f32, kernel="post_filter_h", hip_kernel="post_filter_h",
+ grid=PZ.PS._cdiv(ow, 128) * PZ.PS._cdiv(PZ.PS._cdiv(oh, 2), 16),
+ args=[base[0], ("out", n16 + SLACK)] + base[2:] +
+ [("u32", _s32(_f32_bits(np.float32(pi)))), ("u32", 8 * ow)])
+ label = f"O {ow}x{oh} pi {pi} dec_scale {ds if ds is not None else '0.01/e'} ({o16['hip_kernel']})"
+ out.append((f32, o16, pi, ow, oh, label))
+ return out
+
+
+def _store_classes(v32, pi):
+ """Counts of the values stored (FP32 filter output times pi, numpy's FP32 product): NaN, beyond +-65504 (clamped;
+ infinities included), FP16 subnormal range, exact ties of the FP16 rounding (normal and subnormal range)."""
+ v = (np.asarray(v32, np.float32) * np.float32(pi)).astype(np.float32)
+ a = np.abs(v.astype(np.float64))
+ fin = np.isfinite(v)
+ bits = v.view(np.uint32)
+ normal = fin & (a >= 2.0 ** -14) & (a <= 65504.0)
+ sub = fin & (a > 0) & (a < 2.0 ** -14)
+ t = np.where(sub, a * 2.0 ** 24, 0.0)
+ return {"values": int(v.size), "nan": int(np.isnan(v).sum()), "beyond_65504": int(np.sum(~np.isnan(v) & (a > 65504))),
+ "infinite": int(np.isinf(v).sum()), "f16_subnormal_range": int(sub.sum()),
+ "ties": int(np.sum(normal & ((bits & 0x1FFF) == 0x1000)) + np.sum(sub & (t - np.floor(t) == 0.5)))}
+
+
+def store_run(build, rocm, seeds):
+ import subprocess
+ from clean.l.runner import l_c32_check as C32
+ g = C32.Gpu(build, rocm)
+ with open(os.path.join(build, "post_w64.co"), "rb") as f:
+ mod = g.hip.load_module(f.read())
+ cdir = os.path.join(build, "replay-store")
+ os.makedirs(cdir, exist_ok=True)
+ lines, todo, neg = [], [], None
+ for si, seed in enumerate(seeds):
+ rng = np.random.default_rng(seed)
+ for ci, (f32, o16, pi, ow, oh, label) in enumerate(store_cases(rng)):
+ name = f"store_s{si}_{ci}"
+ w32 = OPS.hip_run(g, mod, f32)[0]
+ w16 = OPS.hip_run(g, mod, o16)[0]
+ lines += OPS.replay_lines(f"{name}_filter", f32, cdir)
+ st = {"kernel": "l_direct_store", "co": "post_w64.co", "tag": "w64", "grid": PZ.PS._cdiv(ow * oh, 256),
+ "block": 256, "args": [("buf", w32[:ow * oh * 12]), ("u32", ow), ("u32", oh)],
+ "direct": [("color", OPS.DXGI_R32G32B32A32_FLOAT, 8, 8, bytes(8 * 8 * 16)),
+ ("depth", OPS.DXGI_R32_FLOAT, 8, 8, bytes(8 * 8 * 4)),
+ ("motion", OPS.DXGI_R32G32_FLOAT, 8, 8, bytes(8 * 8 * 8)),
+ ("output", OPS.DXGI_R16G16B16A16_FLOAT, ow, oh,
+ np.full(ow * oh * 4, TEX_FILL16, np.uint16).tobytes())],
+ "direct_params": (0, 0, 8, 8, repr(float(np.float32(pi))), 1)}
+ body = OPS.replay_lines(f"{name}_store", st, cdir)
+ lines += body[:-1] + [f"direct_out {name}_store.tex", ""]
+ if neg is None and st["grid"] >= 4:
+ neg = name
+ nl = OPS.replay_lines(f"{name}_store", st, cdir, grid=st["grid"] // 2, prefix="negative_")
+ lines += nl[:-1] + [f"direct_out negative_{name}_store.tex", ""]
+ todo.append((name, f32, w32, w16, pi, ow, oh, label))
+ with open(os.path.join(cdir, "cases.txt"), "w", newline="\n") as f:
+ f.write("\n".join(lines))
+ env = {k: v for k, v in os.environ.items()
+ if not any(s in k.upper() for s in ("HIP", "LLVM", "ROCM", "HSA")) or k.upper() in ("HIP_PLATFORM",)}
+ env["WG_D3D12_ARENA_MB"] = "512"
+ r = subprocess.run([os.path.join(build, "kernel_replay.exe"), build, os.path.join(cdir, "cases.txt")], env=env,
+ capture_output=True, text=True, timeout=150, creationflags=0x08000000)
+
+ def read(path):
+ return open(path, "rb").read() if os.path.exists(path) else b""
+
+ def tex_judge(path, w16, ow, oh):
+ n16 = ow * oh * 8
+ t = np.frombuffer(read(path), np.uint8)
+ h = np.frombuffer(w16, np.uint8)
+ o = {"bytes": n16, "hip_slack_bad": int(np.sum(h[n16:] != OPS.HIP_SENTINEL))}
+ if t.size != n16:
+ return False, dict(o, missing=True)
+ o["differ"] = int(np.sum(t != h[:n16]))
+ o["texels_sentinel"] = int(np.sum(np.all(t.reshape(-1, 8) == 0x5A, 1)))
+ return o["differ"] == 0 and o["hip_slack_bad"] == 0, o
+
+ res = {"replay_exit": r.returncode, "replay_tail": r.stdout[-600:] + r.stderr[-300:], "cases": []}
+ classes = {}
+ for name, f32, w32, w16, pi, ow, oh, label in todo:
+ got32 = np.frombuffer(read(os.path.join(cdir, f"{name}_filter.o0")), np.uint8)
+ b32 = np.frombuffer(w32, np.uint8)
+ m = f32["defined"][0]
+ filt = {"bytes": ow * oh * 12,
+ "differ": int(np.sum((got32 != b32) & m)) if got32.size == b32.size else -1,
+ "unwritten_bad": int(np.sum(~m & ((b32 != OPS.HIP_SENTINEL) | (got32 != OPS.D3D12_SENTINEL))))
+ if got32.size == b32.size else -1}
+ ok_t, tex = tex_judge(os.path.join(cdir, f"{name}_store.tex"), w16, ow, oh)
+ cls = _store_classes(np.frombuffer(w32[:ow * oh * 12], np.float32), pi)
+ for k, v in cls.items():
+ classes[k] = classes.get(k, 0) + v
+ ok = ok_t and filt["differ"] == 0 and filt["unwritten_bad"] == 0
+ res["cases"].append({"case": name, "label": label, "identical": ok, "filter_f32": filt, "store_rgba16f": tex,
+ "stored_values": cls})
+ if neg:
+ t = next(x for x in todo if x[0] == neg)
+ ok, o = tex_judge(os.path.join(cdir, f"negative_{neg}_store.tex"), t[3], t[5], t[6])
+ res["negative_control"] = {"case": neg, "half_grid": True, "caught": not ok, "differ": o.get("differ")}
+ res["stored_value_classes"] = classes
+ res["summary"] = {"cases": len(res["cases"]), "identical": sum(c["identical"] for c in res["cases"]),
+ "rgba16f_bytes": sum(c["store_rgba16f"]["bytes"] for c in res["cases"]),
+ "filter_f32_bytes": sum(c["filter_f32"]["bytes"] for c in res["cases"])}
+ res["pass"] = (r.returncode == 0 and bool(neg) and res["negative_control"]["caught"] and
+ res["summary"]["identical"] == res["summary"]["cases"] > 0 and
+ all(classes.get(k, 0) > 0 for k in ("nan", "beyond_65504", "infinite", "f16_subnormal_range", "ties")))
+ return res
+
+
def main(argv=None):
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("--build", required=True)
@@ -268,13 +425,15 @@ def main(argv=None):
ap.add_argument("--seeds", type=int, default=2)
ap.add_argument("--kernels", default=",".join(KINDS))
ap.add_argument("--direct", action="store_true", help="the direct forms instead (see DIRECT)")
+ ap.add_argument("--store", action="store_true", help="Model L's output-copy store instead (see STORE_FRAMES)")
ap.add_argument("--json")
a = ap.parse_args(argv)
if not a.run:
return 0
kernels = [k for k in a.kernels.split(",") if k]
seeds = tuple(range(89, 89 + a.seeds))
- res = direct_run(a.build, a.rocm, seeds) if a.direct else OPS.run(a.build, a.rocm, seeds, kernels, kinds=KINDS)
+ res = store_run(a.build, a.rocm, seeds) if a.store else direct_run(a.build, a.rocm, seeds) if a.direct else \
+ OPS.run(a.build, a.rocm, seeds, kernels, kinds=KINDS)
if a.json:
with open(a.json, "w") as f:
json.dump(res, f, indent=1)
diff --git a/clean/l/runner/l_d3d12_fallback_check.py b/clean/l/runner/l_d3d12_fallback_check.py
new file mode 100644
index 00000000..adfdb0d6
--- /dev/null
+++ b/clean/l/runner/l_d3d12_fallback_check.py
@@ -0,0 +1,232 @@
+# SPDX-License-Identifier: MIT
+"""Model L (issue #89): the fallback route of l_post=own on native D3D12 (frames whose render footprint the pack12 kernels
+do not take, runtime.cpp recon2_supported false: the unpacked coefficient store l_coef, l_post_recon, then the filter
+w64/post_filter_c1_s4) against HIP, byte for byte, chained as the frame runs it.
+
+ python clean/l/runner/l_d3d12_fallback_check.py --build --rocm "C:\\Program Files\\AMD\\ROCm\\7.2" --run
+ [--seeds N] --json out.json
+
+: l_ops.co, l_post.co, post_w64.co, swin.co (clean/l/runner/l_frame_check.py --prepare builds them all), the D3D12
+ports in /dx12 and kernel_replay.exe (clean/l/runner/l_d3d12_check.py prepare_replay). --run: one GPU slot.
+
+Per frame (FRAMES: recon2_supported false for each, asserted; D and O not multiples of the kernels' tiles, so every
+launch has edge tiles: D columns vs the 32-column reconstruction tile, D row pairs vs its 8, O columns vs the filter's 40,
+O row pairs vs its 8; render sizes not multiples of 32):
+1. l_coef (l_ops.co): a decoder map y2 [fh][fw][32] (FP16; values past the clamp, tanh's saturation and inside its
+ polynomial) -> A, B, latent (4 bytes a pixel), C (8 bytes a pixel);
+2. l_post_recon (l_post.co): HIP's A, B, C of step 1 with the frame's colour, history and motion surfaces (random, or
+ with NaN / infinity / 60000 colours, NaN / infinity / -1 history and far motion: "specials"), the exposures by value
+ or from the exposure pair -> full history, reduced codes, display image;
+3. w64/post_filter_c1_s4 (post_w64.co): HIP's display image of step 2 -> the FP32 output, dec_scale from the argument or
+ the exposure pair.
+Each step's D3D12 port gets the same inputs as HIP's kernel; since every step's outputs are compared, the chain is the
+route's. The rules are clean/l/runner/l_d3d12_ops_check.py's: raw downloaded bytes; every output buffer filled
+beforehand with its back end's own sentinel (HIP 0xA5, D3D12 0x5A) and followed by 64 slack bytes that no back end may
+write (the display's pitch padding too); a negative control (one case again with half its workgroups) must be caught;
+Model M's swin_block_c96_nc through the same engine as the control. Output: counts only, no timing.
+"""
+import argparse
+import json
+import os
+import subprocess
+import sys
+
+import numpy as np
+
+HERE = os.path.dirname(os.path.abspath(__file__))
+ROOT = os.path.dirname(os.path.dirname(os.path.dirname(HERE)))
+sys.path.insert(0, ROOT)
+
+from clean.l.runner import l_c32_check as C32 # noqa: E402
+from clean.l.runner import l_d3d12_ops_check as OPS # noqa: E402
+from clean.l.runner import l_post_check as LPC # noqa: E402
+from clean.runner import post as RP # noqa: E402
+
+SLACK = 64
+# (rw, rh, ow, oh, kind, expo, reset); D = (3 O + 1) / 2
+FRAMES = [(150, 90, 121, 73, "random", False, False), # D 182 x 110
+ (100, 60, 77, 45, "specials", True, False), # D 116 x 68
+ (96, 60, 64, 40, "random", False, True), # D 96 x 60 = R
+ (130, 70, 99, 57, "offset_0.1", True, False), # D 149 x 86
+ (11, 8, 7, 5, "specials", False, False)] # D 11 x 8
+
+
+def _cdiv(a, b):
+ return -(-a // b)
+
+
+def _f32_bits(v):
+ return int(np.array([v], np.float32).view(np.uint32)[0])
+
+
+def _defined(n, slack=SLACK):
+ return np.concatenate([np.ones(n, bool), np.zeros(slack, bool)])
+
+
+def coef_case(rng, rw, rh):
+ from clean.runner import dec0
+ fw, fh = _cdiv(rw, 32) * 32, _cdiv(rh, 32) * 32
+ npx = rw * rh
+ S, T = float(rng.uniform(0.5, 4.0)), float(rng.uniform(0.3, 3.0))
+ blob = dec0.pack({"W": np.zeros((68, 64), np.float32), "b": np.zeros(68, np.float32), "S": S, "T": T})
+ P = bytes(dec0.DecParams(rw=rw, rh=rh, fw=fw, fh=fh, reset=0, pad0=0, dw=2 * rw, dh=2 * rh, kx=0.5, ky=0.5,
+ jx=0.25, jy=-0.25, pjx=0.0, pjy=0.0))
+ sizes = (npx * 4, npx * 4, npx * 8, npx * 4) # A, B, C, latent
+ return {"kernel": "l_coef", "co": "l_ops.co", "grid": _cdiv((fh // 2) * (fw // 2), 128), "block": 256,
+ "args": [("buf", OPS._coef_map(rng, fw * fh))] + [("out", n + SLACK) for n in sizes] +
+ [("buf", blob), ("bytes", P)],
+ "defined": [_defined(n) for n in sizes], "sizes": sizes}
+
+
+def post_case(rng, x, coef_out, coef_sizes, expo):
+ prm = RP.post_params(x)
+ dw, dh = prm.dw, prm.dh
+ data = RP.post_device_inputs(x)
+ for k, o, n in zip(("A", "B", "C"), coef_out[:3], coef_sizes[:3]):
+ assert len(data[k]) == n
+ data[k] = o[:n]
+ ev = None
+ if expo:
+ ev = np.array([prm.e, 0.0 if rng.random() < 0.5 else prm.e_prev], np.float32)
+ prm.e, prm.e_prev = float(rng.uniform(0.5, 2.0)), float(rng.uniform(0.5, 2.0))
+ n_hist, n_red, n_disp = dw * dh * 8, max(4, (dw // 2) * (dh // 2) * 4), RP.display_bytes(dw, dh)
+ pitch = RP.display_pitch(dw)
+ disp_def = np.zeros((3, dh, pitch, 2), bool)
+ disp_def[:, :, :dw, :] = True
+ red_def = np.zeros(n_red, bool)
+ red_def[:(dw // 2) * (dh // 2) * 4] = True
+ args = [("buf", data[k]) for k in ("color", "A", "B", "C", "hist_prev", "m_near", "m_center", "m_far")]
+ args += [("buf", RP.pack({"s": np.array(LPC.S)})), ("out", n_hist + SLACK), ("out", n_red + SLACK),
+ ("out", n_disp + SLACK), ("bytes", bytes(prm))]
+ args += [("buf", ev.tobytes())] if expo else [("null",)]
+ return {"kernel": "l_post_recon", "co": "l_post.co", "grid": RP.recon_grid(dw, dh), "block": 256, "args": args,
+ "defined": [_defined(n_hist), np.concatenate([red_def, np.zeros(SLACK, bool)]),
+ np.concatenate([disp_def.reshape(-1), np.zeros(SLACK, bool)])],
+ "n_disp": n_disp, "e": float(ev[0]) if expo else float(prm.e)}
+
+
+def filter_case(rng, display, dw, dh, ow, oh, e, expo):
+ n_out = ow * oh * 12
+ args = [("buf", display), ("out", n_out + SLACK), ("u32", dw), ("u32", dh), ("u32", ow), ("u32", oh),
+ ("u32", _f32_bits(np.float32(0.01 / (e if e != 0.0 else 1.0))))]
+ args += [("buf", np.array([e, 1.0, 0.0, 0.0, 0.0], np.float32).tobytes())] if expo else [("null",)]
+ return {"kernel": "w64/post_filter_c1_s4", "co": "post_w64.co", "tag": "w64", "hip_kernel": "post_filter_c1_s4",
+ "grid": _cdiv(ow, 40) * _cdiv(_cdiv(oh, 2), 8), "block": 64, "args": args, "defined": [_defined(n_out)]}
+
+
+def compare(got, want, dmask):
+ a, b = np.frombuffer(got, np.uint8), np.frombuffer(want, np.uint8)
+ o = {"bytes": len(want)}
+ if a.size != b.size:
+ return dict(o, identical=False, differ=-1, unwritten_bad=-1)
+ o["defined_bytes"] = int(dmask.sum())
+ o["differ"] = int(np.sum((a != b) & dmask))
+ o["unwritten_bad"] = int(np.sum(~dmask & ((b != OPS.HIP_SENTINEL) | (a != OPS.D3D12_SENTINEL))))
+ o["identical"] = o["differ"] == 0 and o["unwritten_bad"] == 0
+ return o
+
+
+def run(build, rocm, seeds):
+ g = C32.Gpu(build, rocm)
+ mods = {}
+
+ def module(co):
+ if co not in mods:
+ with open(os.path.join(build, co), "rb") as f:
+ mods[co] = g.hip.load_module(f.read())
+ return mods[co]
+
+ cdir = os.path.join(build, "replay-fallback")
+ os.makedirs(cdir, exist_ok=True)
+ lines, todo, neg, frames = [], [], None, []
+
+ def add(name, case, step, label):
+ nonlocal lines, neg
+ want = OPS.hip_run(g, module(case["co"]), case)
+ lines += OPS.replay_lines(name, case, cdir)
+ todo.append((name, case, want, step, label))
+ if neg is None and step == "l_post_recon" and case["grid"] >= 4:
+ neg = (name, case, want)
+ lines += OPS.replay_lines(name, case, cdir, grid=case["grid"] // 2, prefix="negative_")
+ return want
+
+ for si, seed in enumerate(seeds):
+ rng = np.random.default_rng(seed)
+ for fi, (rw, rh, ow, oh, kind, expo, reset) in enumerate(FRAMES):
+ dw, dh = (3 * ow + 1) // 2, (3 * oh + 1) // 2
+ assert not RP.recon2_supported(rw, rh, dw, dh), (rw, rh, dw, dh)
+ label = f"R {rw}x{rh} O {ow}x{oh} D {dw}x{dh} {kind}" + (" expo" if expo else "") + (" reset" if reset else "")
+ frames.append({"label": label, "recon2_supported": False,
+ "edge": {"D_cols_mod_32": dw % 32, "D_rowpairs_mod_8": _cdiv(dh, 2) % 8,
+ "O_cols_mod_40": ow % 40, "O_rowpairs_mod_8": _cdiv(oh, 2) % 8,
+ "R_mod_32": [rw % 32, rh % 32]}})
+ base = f"s{si}_f{fi}"
+ coef = coef_case(rng, rw, rh)
+ coef_out = add(f"l_coef_{base}", coef, "l_coef", label)
+ x = LPC.make_case(rng, rw, rh, dw, dh, reset, "random" if kind == "specials" else kind)
+ if kind == "specials":
+ x = OPS._post_specials(rng, x)
+ post = post_case(rng, x, coef_out, coef["sizes"], expo)
+ post_out = add(f"l_post_recon_{base}", post, "l_post_recon", label)
+ filt = filter_case(rng, post_out[2][:post["n_disp"]], dw, dh, ow, oh, post["e"], expo)
+ add(f"w64_post_filter_c1_s4_{base}", filt, "w64/post_filter_c1_s4", label)
+ for ci, case in enumerate(OPS.control_cases("swin_block_c96_nc", rng)):
+ add(f"control_swin_block_c96_nc_s{si}_{ci}", case, "control swin_block_c96_nc", "Model M control")
+ with open(os.path.join(cdir, "cases.txt"), "w", newline="\n") as f:
+ f.write("\n".join(lines))
+ env = {k: v for k, v in os.environ.items()
+ if not any(s in k.upper() for s in ("HIP", "LLVM", "ROCM", "HSA")) or k.upper() in ("HIP_PLATFORM",)}
+ env["WG_D3D12_ARENA_MB"] = "512"
+ r = subprocess.run([os.path.join(build, "kernel_replay.exe"), build, os.path.join(cdir, "cases.txt")], env=env,
+ capture_output=True, text=True, timeout=150, creationflags=0x08000000)
+
+ def read(path):
+ return open(path, "rb").read() if os.path.exists(path) else b""
+
+ def judge(name, case, want, prefix=""):
+ defined = case.get("defined")
+ outs = [compare(read(os.path.join(cdir, f"{prefix}{name}.o{oi}")), w,
+ defined[oi] if defined else np.ones(len(w), bool)) for oi, w in enumerate(want)]
+ return all(o["identical"] for o in outs), outs
+
+ res = {"replay_exit": r.returncode, "replay_tail": r.stdout[-600:] + r.stderr[-300:], "frames": frames, "cases": []}
+ for name, case, want, step, label in todo:
+ ok, outs = judge(name, case, want)
+ res["cases"].append({"case": name, "step": step, "label": label, "identical": ok, "outputs": outs})
+ caught = None
+ if neg:
+ ok, outs = judge(neg[0], neg[1], neg[2], prefix="negative_")
+ caught = not ok and all(o["differ"] >= 0 for o in outs)
+ res["negative_control"] = {"case": neg[0], "half_grid": True, "caught": caught,
+ "differ": sum(max(o["differ"], 0) for o in outs)}
+ steps = sorted({c["step"] for c in res["cases"]})
+ res["per_step"] = {s: {"cases": sum(c["step"] == s for c in res["cases"]),
+ "identical": sum(c["identical"] for c in res["cases"] if c["step"] == s),
+ "bytes": sum(o["bytes"] for c in res["cases"] if c["step"] == s for o in c["outputs"]),
+ "differ": sum(max(o["differ"], 0) for c in res["cases"] if c["step"] == s
+ for o in c["outputs"])} for s in steps}
+ res["pass"] = (r.returncode == 0 and caught is True and
+ all(v["cases"] > 0 and v["identical"] == v["cases"] for v in res["per_step"].values()))
+ return res
+
+
+def main(argv=None):
+ ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
+ ap.add_argument("--build", required=True)
+ ap.add_argument("--rocm", required=True)
+ ap.add_argument("--run", action="store_true")
+ ap.add_argument("--seeds", type=int, default=2)
+ ap.add_argument("--json")
+ a = ap.parse_args(argv)
+ if not a.run:
+ return 0
+ res = run(a.build, a.rocm, tuple(range(89, 89 + a.seeds)))
+ if a.json:
+ with open(a.json, "w") as f:
+ json.dump(res, f, indent=1)
+ print({k: v for k, v in res.items() if k not in ("cases", "replay_tail", "frames")})
+ return 0 if res["pass"] else 1
+
+
+if __name__ == "__main__":
+ sys.exit(main())
From 0b89598c38165226f22a8fb621dcd7a0a8951ddf Mon Sep 17 00:00:00 2001
From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com>
Date: Sat, 14 Nov 2026 08:20:46 -0500
Subject: [PATCH 3/3] l_d3d12_own_post_check.py: l_frame_check's raw-byte API
(#165): sentinels, raw defined bytes, negative controls
Each run fills its outputs with its own sentinel before every frame and records every output's raw defined bytes; both raw comparisons (against HIP, defaults against stages) fail on a missing output, another size or any differing byte, and carry l_frame_check's record-level negative controls.
Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com>
---
clean/l/runner/l_d3d12_own_post_check.py | 108 ++++++++++++-----------
1 file changed, 56 insertions(+), 52 deletions(-)
diff --git a/clean/l/runner/l_d3d12_own_post_check.py b/clean/l/runner/l_d3d12_own_post_check.py
index be27551d..c491f0e4 100644
--- a/clean/l/runner/l_d3d12_own_post_check.py
+++ b/clean/l/runner/l_d3d12_own_post_check.py
@@ -11,22 +11,26 @@
/dx12 holds the D3D12 ports (python clean/dx12/build_dx12.py --out ). --run runs l_frame_check's frame
sequence for Model L on D3D12 with l_post=own ("d3d12_own"); with --stages the D3D12 fusions are off
-(WG_D3D12_FUSION=none: every stage output written but b0 ... b4, "d3d12_own_stages"). --compare checks:
-- against the HIP run (l_frame_check's "default", which runs l_post_recon and the same filter): every output both
- backends write, byte for byte, the post outputs (history, reduced history, display image) and the output included;
- the pack12 images unpacked against HIP's A, B, C. Outputs all zero on D3D12 and not on HIP are reported as not
- written there (the fusions keep some maps on chip), not as differing. Negative control: one output bit flipped in a
- copy of a D3D12 frame must show as one differing value;
+(WG_D3D12_FUSION=none: every stage output written but b0 ... b4, "d3d12_own_stages"). Before every frame each run fills
+its outputs with its own sentinel (l_frame_check fill_sentinels: d3d12_own 0x5A, d3d12_own_stages 0x3C; HIP's default
+run 0xA5) and records every output's raw defined bytes (raw_outputs). --compare checks, with l_frame_check's raw-byte
+comparison (raw_compare: a missing output, another size or any differing byte fails the frame; an output not written by
+design must still hold that run's sentinel in every byte):
+- against the HIP run (l_frame_check's "default", which runs l_post_recon and the same filter): every output byte for
+ byte, the post outputs (full history, reduced history, display image, output) included; the D3D12 pack12 images in
+ the base layout (pack12_to_base). Not written by design: b0 ... b4 on both (the bottleneck megakernel), enc1's merged
+ map on D3D12 (fusion pm), on the first frame the previous frame's state on both;
- the "stages" run per stage against the reference on its own inputs, as l_frame_check --compare, with the post against
Model L's reconstruction (clean/l/reference/recon.py) under Model L's post acceptance (clean/l/runner/l_post_check.py,
per frame and over the run) and the filter against recon.output_filter; b5 as one unit (the bottleneck megakernel
writes only b5), and where that chained reference parts from the runtime, b5 per stage on HIP's six-launch run
(l_frame_check's "stages"), which must hold the same b5 bytes;
-- the default kernels against the "stages" run, byte for byte.
+- the default kernels against the "stages" run, raw bytes;
+- l_frame_check's record-level negative controls on both raw comparisons (a dropped history write, a dropped reduced
+ write, a whole reduced write, a missing output): each must make the comparison fail.
Output: counts only, no timing.
"""
import argparse
-import copy
import json
import os
import pickle
@@ -42,6 +46,7 @@
from clean.l.runner import l_post_check as LP # noqa: E402
OPTIONS = "model=L;l_post=own"
+SENTINELS = {"d3d12_own": 0x5A, "d3d12_own_stages": 0x3C}
def run(build, special=True, stages=False):
@@ -54,12 +59,12 @@ def run(build, special=True, stages=False):
map_path=F.l_map(build), check_packing=False, kernels={"options": OPTIONS})
rng = np.random.default_rng(F.SEED)
frames = []
- for (rw, rh), reset in F.SEQUENCE:
+ for i, ((rw, rh), reset) in enumerate(F.SEQUENCE):
fr = F.frame_inputs(rng, rw, rh, reset, special)
+ F.fill_sentinels(p, SENTINELS[config], i == 0)
out = p.frame(fr, debug=True)
- raw = {n: p._get(n, np.uint8).copy() for n in ("dec0.A", "dec0.C")}
- frames.append({"inputs": fr, "out": out, "raw": raw})
- rec = {"model": "L", "config": config, "kernels": p.kernels(), "notes": p.notes(),
+ frames.append({"inputs": fr, "out": out, "raw": F.raw_outputs(p), "post_last": p.kernels().get("post_last")})
+ rec = {"model": "L", "config": config, "sentinel": SENTINELS[config], "kernels": p.kernels(), "notes": p.notes(),
"model_source": p.model_source, "dw": p.dw, "dh": p.dh, "ow": p.ow, "oh": p.oh,
"taps": p.output_taps(), "frames": frames}
with open(os.path.join(build, f"lframe_L_{config}.pkl"), "wb") as f:
@@ -73,15 +78,14 @@ def _load(build, config):
return pickle.load(f)
-def with_display(hip, dx, frames):
- """compare_backends leaves the display image out (Model M's fused post writes none); both runs here write it:
- values differing, per frame."""
- for m, a, b in zip(frames, hip["frames"], dx["frames"]):
- u, v = a["out"]["post"]["display"], b["out"]["post"]["display"]
- n = -1 if u is None or v is None or u.shape != v.shape else int(np.sum(u.view(np.uint16) != v.view(np.uint16)))
- m["differ"]["post.display"] = n
- m["identical"] = m["identical"] and n == 0
- return frames
+def unwritten(k, n):
+ """Outputs not written by design (raw_compare's sides): b0 ... b4 on both (the bottleneck megakernel), enc1's merged
+ map with the D3D12 default kernels (fusion pm keeps it on chip), on the first frame the previous frame's state."""
+ if n in F.BNECK_UNWRITTEN or (k == 0 and n in F.UNWRITTEN_ON_FIRST):
+ return "both"
+ if n == "enc1.merged":
+ return "b"
+ return None
def compare(build, json_path):
@@ -90,32 +94,28 @@ def compare(build, json_path):
hip, own, st = _load(build, "default"), _load(build, "d3d12_own"), _load(build, "d3d12_own_stages")
res = {"kernels_d3d12": own["kernels"], "kernels_hip": hip["kernels"]}
- vs_hip = with_display(hip, own, F.compare_backends(hip, own, same_post=True))
- res["vs_hip"] = {"frames": vs_hip, "identical_frames": sum(f["identical"] for f in vs_hip),
- "outputs_compared": sorted({n for f in vs_hip for n in f["differ"]}),
- "differing": sorted({k for f in vs_hip for k, v in f["differ"].items() if v}),
- "unwritten_d3d12": sorted({n for f in vs_hip for n in f["unwritten_d3d12"]})}
- bad = copy.deepcopy(own)
- o = bad["frames"][1]["out"]["out"]
- o.view(np.uint32).reshape(-1)[o.size // 2] ^= 1
- caught = with_display(hip, bad, F.compare_backends(hip, bad, same_post=True))[1]["differ"].get("out")
- res["negative_control"] = {"flipped": "out, frame 1, one bit", "out_differing": caught, "caught": caught == 1}
-
- st = F.fill_pack12(st) # dec0's A, B, C from the pack12 images (the debug view has none)
- per_stage = F.compare_model(st, params, bneck_unit=True)
+ vs_hip = F.raw_compare(hip, own, hip["sentinel"], own["sentinel"], unwritten, convert_b=True)
+ res["vs_hip"] = dict(F.raw_summary(vs_hip), frames=vs_hip)
+ res["vs_hip"]["negative_controls"] = F.negative_controls(hip, own, hip["sentinel"], own["sentinel"], unwritten,
+ convert_b=True)
+
+ st_dec = F.fill_pack12(st) # dec0's A, B, C from the pack12 images (the debug view has none)
+ per_stage = F.compare_model(st_dec, params, bneck_unit=True)
post_run = {k: LP.l_post_run([m["post"][k] for m in per_stage if k in m["post"]]) for k in ("history", "display")}
# b5 as one unit (b0 ... b5 chained in the reference from enc2.merged: the megakernel writes only b5) can part from
# the runtime where one FP8 rounding in the chain goes the other way. Such a frame's b5 is checked per stage
# instead: the HIP six-launch run ("stages", every block output written) has the same b5 bytes and its b5
# passes against the reference on its own b4.
- hip_st = _load(build, "stages")
- hip_ps = None
+ hip_st = None
b5_via_hip = []
for k, m in enumerate(per_stage):
if m["stage_pass"].get("b5", True):
continue
- hip_ps = hip_ps or F.compare_model(hip_st, params)
- same = own["frames"][k]["out"]["b5"]["out"].tobytes() == hip_st["frames"][k]["out"]["b5"]["out"].tobytes()
+ if hip_st is None:
+ with open(os.path.join(build, "lframe_L_stages.pkl"), "rb") as f:
+ hip_st = pickle.load(f)
+ hip_ps = F.compare_model(hip_st, params)
+ same = own["frames"][k]["raw"]["b5.out"].tobytes() == hip_st["frames"][k]["raw"]["b5.out"].tobytes()
if same and hip_ps[k]["stage_pass"]["b5"]:
m["stage_pass"]["b5"] = True
m["b5_checked"] = "per stage on HIP's six-launch run (same b5 bytes): pass"
@@ -124,27 +124,31 @@ def compare(build, json_path):
res["d3d12_per_stage"] = {"frames": per_stage, "summary": F.summary(per_stage),
"passed": sum(m["pass"] for m in per_stage), "post_run": post_run,
"b5_unit_differs_checked_per_stage_on_hip": b5_via_hip}
- fused = F.compare_backends(st, own)
- res["d3d12_default_vs_stages"] = {"frames": fused, "identical_frames": sum(f["identical"] for f in fused),
- "differing": sorted({k for f in fused for k, v in f["differ"].items() if v}),
- "unwritten_default": sorted({n for f in fused for n in f["unwritten_d3d12"]})}
+
+ fused = F.raw_compare(st, own, st["sentinel"], own["sentinel"], unwritten)
+ res["d3d12_default_vs_stages"] = dict(F.raw_summary(fused), frames=fused)
+ res["d3d12_default_vs_stages"]["negative_controls"] = F.negative_controls(st, own, st["sentinel"], own["sentinel"],
+ unwritten)
n = len(own["frames"])
- res["pass"] = (res["vs_hip"]["identical_frames"] == n and res["negative_control"]["caught"] and
- res["d3d12_per_stage"]["passed"] == n and all(r["pass"] for r in post_run.values()) and
- res["d3d12_default_vs_stages"]["identical_frames"] == n and
+ caught = all(v["caught"] for r in (res["vs_hip"], res["d3d12_default_vs_stages"])
+ for v in r["negative_controls"].values())
+ res["pass"] = (res["vs_hip"]["identical_frames"] == n and res["d3d12_per_stage"]["passed"] == n and
+ all(r["pass"] for r in post_run.values()) and
+ res["d3d12_default_vs_stages"]["identical_frames"] == n and caught and
own["kernels"].get("l_post") == "l_post_recon")
with open(json_path, "w") as f:
json.dump(res, f, indent=1, default=lambda x: x.item() if hasattr(x, "item") else str(x))
print("L d3d12 l_post=own:", "pass" if res["pass"] else "FAIL", "frames", n)
- print(" against the HIP run: byte-identical", res["vs_hip"]["identical_frames"], "of", n, "differing",
- res["vs_hip"]["differing"], "not written on D3D12", res["vs_hip"]["unwritten_d3d12"])
- print(" negative control:", json.dumps(res["negative_control"]))
+ print(" against the HIP run (raw bytes): identical", res["vs_hip"]["identical_frames"], "of", n,
+ "outputs compared", res["vs_hip"]["outputs_compared"], "differing", res["vs_hip"]["differing"])
+ print(" negative controls:", json.dumps({k: v["caught"] for k, v in res["vs_hip"]["negative_controls"].items()}))
print(" per stage against the reference (Model L's post acceptance):", res["d3d12_per_stage"]["passed"], "of", n,
"post over the run", json.dumps({k: r["pass"] for k, r in post_run.items()}),
- "(b5 as one unit off, checked per stage on HIP's six-launch run with the same bytes: frames",
- res["d3d12_per_stage"]["b5_unit_differs_checked_per_stage_on_hip"], ")")
- print(" default kernels against the stages run: byte-identical", res["d3d12_default_vs_stages"]["identical_frames"],
- "of", n, "differing", res["d3d12_default_vs_stages"]["differing"])
+ "(b5 as one unit off, checked per stage on HIP's six-launch run with the same bytes: frames", b5_via_hip, ")")
+ print(" default kernels against the stages run (raw bytes): identical",
+ res["d3d12_default_vs_stages"]["identical_frames"], "of", n, "differing",
+ res["d3d12_default_vs_stages"]["differing"], "negative controls",
+ json.dumps({k: v["caught"] for k, v in res["d3d12_default_vs_stages"]["negative_controls"].items()}))
for m in per_stage:
if not m["pass"]:
print(" frame", m["frame"], "failing:", [s for s, ok in m["stage_pass"].items() if not ok])