""" The competition contract: one signature, any architecture behind it. Entrants build whatever they like -- diffusion, GAN, autoregressive strokes, a hand-rolled NumPy transformer, PyTorch or JAX or TensorFlow -- and export it to a single ONNX file. Nothing about the interior is constrained. Why ONNX rather than a framework checkpoint. An ONNX file is not code; it is a serialized computation graph, and onnxruntime walks it executing a fixed set of standard operators. So "run any student's model" stops implying "run any student's Python". A `.pt` executes arbitrary code the instant it is unpickled, and even a safetensors file is only inert because something *else* supplies the architecture -- which is what forced an architecture whitelist and defeated the point. Here the architecture travels inside the graph, and the graph is data. The second thing ONNX buys is that the I/O contract stops being a convention and becomes the file's own declared signature. Input names, shapes and dtypes are readable off the model before a single operator runs, so a mismatched entry is rejected in milliseconds with an exact reason rather than crashing mid-run. inputs prompt_embed float32 [1, T, 3584] frozen encoder, we provide noise float32 [1, 16, 64, 64] the only entropy source output image float32 [1, 512, 512] in [0,1], 0 = ink Noise is passed in rather than drawn inside the graph. That makes determinism checkable -- identical inputs must give an identical image -- which is what lets a leaderboard entry be reproduced and audited later. Sampling loops belong inside the graph. A diffusion entrant bakes its own denoising loop in (ONNX has Loop/Scan, and the starter kit shows an unrolled export, which is simpler and usually faster). That is deliberate: it keeps the contract to one call for every architecture, instead of privileging diffusion by making the harness a denoiser loop. Inference budget is then levelled by a wall-clock limit rather than by forcing everyone into the same sampler. """ from __future__ import annotations from dataclasses import dataclass # ---------------------------------------------------------------- the canvas # Identical to the game's. An entry that rendered at another size or polarity # would stand out in a quiz lineup by its format rather than its drawing, which # destroys the only measurement the competition is scored on. IMAGE_SIZE = 512 INK, PAPER = 0.0, 1.0 # 0 = black ink, 1 = white paper # ------------------------------------------------------------- the conditioner # Every entrant gets the SAME frozen embedding from the same encoder the game # uses. Students neither tokenise nor ship a text encoder: it removes a large # confound (whose tokeniser was better) and keeps submissions small. TEXT_ENCODER = "Tongyi-MAI/Z-Image-Turbo" # subfolder: text_encoder EMBED_DIM = 3584 # Shape of the entropy the graph is handed. Latent-shaped by convention only -- # a model with no VAE may reshape or ignore it. NOISE_SHAPE = (1, 16, 64, 64) IN_EMBED, IN_NOISE, OUT_IMAGE = "prompt_embed", "noise", "image" # ------------------------------------------------------------------- budgets # Enforced by the worker, generous, and stated up front rather than discovered. MAX_FILE_BYTES = 4 * 1024**3 MAX_SECONDS_PER_IMAGE = 120.0 MAX_OPSET = 21 # refuse graphs from an opset our runtime predates DETERMINISM_TOL = 1e-4 # Operators that reach outside the graph. onnxruntime is a parser written in # C++, not a sandbox, so the worker still runs containerised -- but refusing # these closes the documented escapes rather than relying on that alone. FORBIDDEN_OPS = { "custom", # any unregistered custom operator "PythonOp", # torch autograd bridge: executes Python "Cast_Python", } @dataclass(frozen=True) class Problem: """One reason an entry cannot be scored, in language a student can act on.""" field: str detail: str def __str__(self) -> str: return f"{self.field}: {self.detail}" def _dim(d) -> int | str | None: """ONNX dims are either an int, a symbolic name, or unset.""" if d is None: return None return d if isinstance(d, int) else str(d) def validate_signature(sess) -> list[Problem]: """Check an onnxruntime session's declared I/O against the contract. Runs before any inference: a wrong signature costs milliseconds, not a model's worth of compute. """ problems: list[Problem] = [] ins = {i.name: i for i in sess.get_inputs()} outs = {o.name: o for o in sess.get_outputs()} for want in (IN_EMBED, IN_NOISE): if want not in ins: hint = "" # Exporters prune inputs the forward pass never reads, so a missing # input usually means "declared but unused" rather than "misnamed". # A model that ignores the noise also emits one fixed image per # prompt, which is its own reason to reject. if want == IN_NOISE: hint = (". If your forward() never reads it, the exporter drops " "it from the graph — the model must actually use the noise") elif want == IN_EMBED: hint = (". If your forward() never reads it, the exporter drops " "it from the graph — the model must be conditioned on the caption") problems.append(Problem( "inputs", f"no input named {want!r}; found {sorted(ins)}{hint}")) if OUT_IMAGE not in outs: problems.append(Problem( "outputs", f"no output named {OUT_IMAGE!r}; found {sorted(outs)}")) if problems: return problems # shape checks would be noise now e = ins[IN_EMBED] shape = [_dim(d) for d in e.shape] if e.type != "tensor(float)": problems.append(Problem(IN_EMBED, f"must be float32, got {e.type}")) if len(shape) != 3: problems.append(Problem(IN_EMBED, f"expected rank 3 [1, T, {EMBED_DIM}], got {shape}")) elif shape[2] != EMBED_DIM: # the token axis may stay symbolic; the feature axis may not problems.append(Problem( IN_EMBED, f"last dim must be {EMBED_DIM} to accept the provided " f"embedding, got {shape[2]}")) n = ins[IN_NOISE] shape = [_dim(d) for d in n.shape] if n.type != "tensor(float)": problems.append(Problem(IN_NOISE, f"must be float32, got {n.type}")) if len(shape) != 4 or any( isinstance(g, int) and g != w for g, w in zip(shape, NOISE_SHAPE)): problems.append(Problem(IN_NOISE, f"expected {list(NOISE_SHAPE)}, got {shape}")) o = outs[OUT_IMAGE] shape = [_dim(d) for d in o.shape] if o.type != "tensor(float)": problems.append(Problem(OUT_IMAGE, f"must be float32, got {o.type}")) want = [1, IMAGE_SIZE, IMAGE_SIZE] if len(shape) != 3 or any( isinstance(g, int) and g != w for g, w in zip(shape, want)): problems.append(Problem( OUT_IMAGE, f"expected {want} (one greyscale canvas), got {shape}")) return problems def validate_graph(model) -> list[Problem]: """Static checks on the ONNX graph itself, before it is ever executed.""" problems: list[Problem] = [] opsets = {i.domain or "ai.onnx": i.version for i in model.opset_import} std = opsets.get("ai.onnx") if std is None: problems.append(Problem("opset", "graph declares no standard ai.onnx opset")) elif std > MAX_OPSET: problems.append(Problem( "opset", f"opset {std} is newer than the runtime's {MAX_OPSET}; " f"re-export with opset_version={MAX_OPSET}")) # A non-empty custom domain means operators our runtime has no definition # for -- which is the one way a graph could reach outside the operator set. custom = {d for d in opsets if d not in ("ai.onnx", "ai.onnx.ml", "")} if custom: problems.append(Problem( "operators", f"custom operator domain(s) {sorted(custom)} are not " "executable here; export using standard ONNX ops only")) for node in model.graph.node: if node.op_type in FORBIDDEN_OPS: problems.append(Problem("operators", f"{node.op_type} is not permitted")) break return problems def check_output(arr) -> list[Problem]: """Check one generated canvas against the contract. The degenerate cases matter as much as the malformed ones: a blank page scores *perfectly* on every sparsity metric the project uses, so without an explicit floor the empty model looks statistically excellent. """ import numpy as np problems: list[Problem] = [] a = np.asarray(arr) if a.ndim == 3 and a.shape[0] == 1: a = a[0] if a.shape != (IMAGE_SIZE, IMAGE_SIZE): return [Problem("shape", f"expected ({IMAGE_SIZE}, {IMAGE_SIZE}), got {a.shape}")] if not np.isfinite(a).all(): return [Problem("values", "output contains NaN or infinity")] if a.min() < INK - 1e-3 or a.max() > PAPER + 1e-3: problems.append(Problem( "range", f"must lie in [{INK}, {PAPER}], got [{a.min():.3f}, {a.max():.3f}]")) ink = float((a < 0.5).mean()) if ink < 0.002: problems.append(Problem("content", f"effectively blank (ink fraction {ink:.4f})")) elif ink > 0.60: problems.append(Problem("content", f"effectively solid (ink fraction {ink:.4f})")) return problems