|
| 1 | +"""V7-M0.3: MLX-vs-CUDA forward-parity harness. |
| 2 | +
|
| 3 | +The CUDA reference logits artefact must be generated on GB10 (the |
| 4 | +canonical reference hardware). This script produces the MLX side |
| 5 | +and either (a) compares to bench/baselines/m03_cuda_logits_ref.npy |
| 6 | +when present, or (b) writes bench/baselines/m03_mlx_logits.npy as |
| 7 | +the MLX reference so a GB10 run can later cross-check. |
| 8 | +
|
| 9 | +Usage: |
| 10 | + PYTHONPATH=. python bench/m03_cuda_logits_parity_harness.py \\ |
| 11 | + --H 128 --S 32 --seed 7 |
| 12 | +
|
| 13 | +When the CUDA reference is missing (Mac-only dev), the script writes |
| 14 | +m03_mlx_logits.npy + a status JSON noting the gap; the bd ticket |
| 15 | +(cppmega-mlx-uwhj) tracks the GB10 generation that closes the loop. |
| 16 | +""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +import argparse |
| 21 | +import json |
| 22 | +from pathlib import Path |
| 23 | + |
| 24 | +import numpy as np |
| 25 | + |
| 26 | +from cppmega_v4.jsonrpc.schema import VerifyParams |
| 27 | +from cppmega_v4.runner import Pipeline, run_pipeline |
| 28 | + |
| 29 | + |
| 30 | +def _spec(H: int, S: int) -> VerifyParams: |
| 31 | + return VerifyParams.model_validate({ |
| 32 | + "graph": { |
| 33 | + "nodes": [ |
| 34 | + {"id": "attn", "kind": "attention", |
| 35 | + "params": {"num_heads": max(2, H // 64), "head_dim": 64}}, |
| 36 | + {"id": "mlp", "kind": "mlp", "params": {}}, |
| 37 | + ], |
| 38 | + "edges": [{"src": "attn", "dst": "mlp"}], |
| 39 | + }, |
| 40 | + "dim_env": {"B": 1, "S": S, "H": H, |
| 41 | + "nh": max(2, H // 64), "nkv": 1, "head_dim": 64}, |
| 42 | + "loss": {"kind": "cross_entropy", "head_outputs": ["mlp"]}, |
| 43 | + "optim": {"kind": "adamw", |
| 44 | + "groups": [{"matcher": "all", "lr": 1e-3, |
| 45 | + "weight_decay": 0.01, |
| 46 | + "betas": [0.9, 0.95]}]}, |
| 47 | + }) |
| 48 | + |
| 49 | + |
| 50 | +def main() -> None: |
| 51 | + p = argparse.ArgumentParser() |
| 52 | + p.add_argument("--H", type=int, default=128) |
| 53 | + p.add_argument("--S", type=int, default=32) |
| 54 | + p.add_argument("--seed", type=int, default=7) |
| 55 | + p.add_argument("--mlx-out", |
| 56 | + default="bench/baselines/m03_mlx_logits.npy") |
| 57 | + p.add_argument("--cuda-ref", |
| 58 | + default="bench/baselines/m03_cuda_logits_ref.npy") |
| 59 | + p.add_argument("--status-out", |
| 60 | + default="bench/baselines/m03_parity_status.json") |
| 61 | + args = p.parse_args() |
| 62 | + |
| 63 | + spec = _spec(args.H, args.S) |
| 64 | + rep = run_pipeline(spec, Pipeline.from_dict({ |
| 65 | + "stages": ["parse", "verify_build_spec", "build_model", |
| 66 | + "dry_forward"], |
| 67 | + "stage_options": {"dry_forward": {"seed": args.seed, |
| 68 | + "capture_logits": True}}, |
| 69 | + })) |
| 70 | + dry = next(s for s in rep.stages if s.name == "dry_forward") |
| 71 | + |
| 72 | + out_dir = Path("bench/baselines") |
| 73 | + out_dir.mkdir(parents=True, exist_ok=True) |
| 74 | + status = { |
| 75 | + "preset": "llama3_8b_unit", |
| 76 | + "dim_env": {"B": 1, "S": args.S, "H": args.H, |
| 77 | + "seed": args.seed}, |
| 78 | + "stage_status": dry.status, |
| 79 | + } |
| 80 | + |
| 81 | + # Capture whatever logits-shaped tensor the dry_forward stage |
| 82 | + # surfaced. Backend may not have wired capture_logits yet — in |
| 83 | + # that case status flags 'mlx_unavailable' so the GB10 reference |
| 84 | + # generation can stay pending. |
| 85 | + extras = getattr(dry, "extras", {}) or {} |
| 86 | + logits = extras.get("output_logits") or extras.get("logits") |
| 87 | + if logits is None: |
| 88 | + status["status"] = "mlx_unavailable" |
| 89 | + status["detail"] = ( |
| 90 | + "dry_forward did not surface output_logits; capture_logits " |
| 91 | + "wiring is the M0.3 follow-up.") |
| 92 | + else: |
| 93 | + arr = np.asarray(logits, dtype=np.float32) |
| 94 | + np.save(Path(args.mlx_out), arr) |
| 95 | + status["mlx_logits_path"] = args.mlx_out |
| 96 | + status["mlx_shape"] = list(arr.shape) |
| 97 | + cuda_ref = Path(args.cuda_ref) |
| 98 | + if cuda_ref.is_file(): |
| 99 | + ref = np.load(cuda_ref).astype(np.float32) |
| 100 | + if ref.shape != arr.shape: |
| 101 | + status["status"] = "shape_mismatch" |
| 102 | + status["cuda_shape"] = list(ref.shape) |
| 103 | + else: |
| 104 | + delta = float(np.abs(arr - ref).max()) |
| 105 | + status["status"] = "parity_checked" |
| 106 | + status["max_abs_delta"] = delta |
| 107 | + status["passing"] = delta < 1e-3 |
| 108 | + else: |
| 109 | + status["status"] = "awaiting_cuda_ref" |
| 110 | + status["detail"] = ( |
| 111 | + f"CUDA reference {args.cuda_ref} missing; regenerate " |
| 112 | + "on GB10 (bd cppmega-mlx-uwhj).") |
| 113 | + |
| 114 | + Path(args.status_out).write_text(json.dumps(status, indent=2)) |
| 115 | + print(json.dumps(status, indent=2)) |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + main() |
0 commit comments