"""Probe projection components selected by MTPLX's Qwen implementation. This is an operator diagnostic, not a full-model or serving benchmark. Only the selected projections are materialized; no weights are downloaded. """ import argparse import hashlib import json import statistics import time from pathlib import Path import mlx.core as mx import numpy as np from mtplx.models import qwen4_exp def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("model", type=Path) parser.add_argument("--capture", type=Path, help="Capture only the 32-row router GPU operation") parser.add_argument("--check-split-k", action="store_true", help="Diagnose router rounding with explicitly batched K partitions") args = parser.parse_args() assert mx.__version__ == "0.32.2", "Use the pinned MTPLX MLX environment" index = json.loads((args.model / "model.safetensors.index.json").read_text())["weight_map"] config = json.loads((args.model / "config.json").read_text()) quantization = config["quantization"] model_args = qwen4_exp.TextArgs.from_dict(config["text_config"]) source = Path(qwen4_exp.__file__).resolve() print(json.dumps({"reference": "MTPLX Qwen projection components", "source": str(source), "source_sha256": hashlib.sha256(source.read_bytes()).hexdigest(), "mlx_version": mx.__version__}), flush=True) values = [] state = 0x12345678 for _ in range(2560): state = (state * 1664525 + 1013904223) & 0xFFFFFFFF values.append(((state >> 24) - 128) / 32) for suffix in ("linear_attn.in_proj_qkv", "mlp.gate", "linear_attn.in_proj_b", "mlp.shared_expert_gate"): name = "language_model.model.layers.0." + suffix weights = {} for part in ("weight", "scales", "biases"): key = name + "." + part weights[part] = mx.load(str(args.model / index[key]))[key] mx.eval(*weights.values()) layout = quantization[name] module = (qwen4_exp.GatedDeltaNet(model_args) if suffix.startswith("linear_attn.") else qwen4_exp.SparseMoeBlock(model_args)) attribute = suffix.split(".")[-1] projection = getattr(module, attribute).to_quantized(**layout) projection.load_weights(list(weights.items()), strict=True) setattr(module, attribute, projection) for rows in (1, 2, 3, 4, 31, 32, 63, 64, 2047, 2048): x = mx.array([[values] * rows], dtype=mx.bfloat16) mx.eval(x) capture = args.capture and suffix == "mlp.gate" and rows == 32 if capture: mx.metal.start_capture(str(args.capture)) try: result = getattr(module, attribute)(x) mx.eval(result) finally: if capture: mx.metal.stop_capture() raw = np.asarray(result.astype(mx.float32)).astype("