"""Real MTPLX module construction/strict lazy loading, no model inference. Only MTP's two small pre-FC norms are evaluated. Run under test-supervisor. """ import sys print("binding reference: importing pinned runtime", file=sys.stderr, flush=True) import hashlib import json import os from contextlib import redirect_stdout from pathlib import Path import mlx.core as mx import numpy as np from mlx.utils import tree_flatten from mlx_lm.utils import load_model from mtplx.models import qwen4_exp as qwen def main(root, mode=None): assert mx.__version__ == "0.32.2" name_in_bank = lambda name: "mtp." + name[len("language_model.mtp."):] if name.startswith("language_model.mtp.") else name for mask, mtp in ((0, False), (7, False), (0, True), (7, True)): print(f"binding reference: mask={mask} mtp={mtp}", file=sys.stderr, flush=True) os.environ.update(MTPLX_FUSED_GATE_UP=str(mask & 1), MTPLX_FUSED_GDN_INPROJ=str((mask >> 1) & 1), MTPLX_FUSED_QSA_QKV=str((mask >> 2) & 1)) with redirect_stdout(sys.stderr): model, _ = load_model(Path(root), lazy=True, strict=True, get_model_classes=lambda **_: (qwen.Model, qwen.ModelArgs)) if mtp: assert model.attach_mtp(Path(root)) if mode == "--parameter-order": names = [name_in_bank(name) for name, _ in tree_flatten(model.parameters())] print(json.dumps(dict(mask=mask, mtp=mtp, names=names),separators=(",", ":")),flush=True) del model, names continue if mode == "--post-weight-load": from mtplx.memory_plan import ngram_table_resident_policy assert not ngram_table_resident_policy(), "this test must not make the full table resident" with redirect_stdout(sys.stderr): model.post_weight_load(Path(root)) tables = [] for i, layer in enumerate(model.layers): if getattr(layer, "ple", None) is None: continue table = layer.ple.ple_embedding.ngram_embedding sidecar = table._sidecar out = table(mx.array([0, 1, 255, 1], dtype=mx.int64)) mx.eval(out) tables.append(dict(layer=i, resident=getattr(table, "_lazy_parts", None) is not None, hot_mb=sidecar._hot_cap_rows * sidecar._hot_row_bytes // 2**20, prefetch=sidecar._pool is not None, shape=list(out.shape), sha256=hashlib.sha256(np.asarray(out.astype(mx.float32)).astype('