#!/usr/bin/env python3 """Export an NVFP4 draft (W4A16: quantized routed experts, BF16 the rest) from the BF16 GLM-5.3-Flash mtp/ folder. Uses ModelOpt's own NVFP4QTensor.quantize (same math as the target checkpoint; packing: byte k = (even nibble | odd nibble<<4), one-level e4m3 block scales, scale_2 = 1.0). Declares input_activations: null so vLLM routes the experts through its W4A16 NVFP4 path (no input_scale needed, mirroring the target's expert behavior). Output: ~/github/GLM5.3-Flash-E224-DGX-Spark/mtp-nvfp4/ """ import json import shutil import struct import sys import time from pathlib import Path import torch from modelopt.torch.quantization.qtensor.nvfp4_tensor import NVFP4QTensor from safetensors.torch import save_file torch.set_grad_enabled(False) # Usage: python3 export_mtp_nvfp4.py /path/to/GLM5.3-Flash-E224-DGX-Spark ROOT = (Path(sys.argv[1]).expanduser() if len(sys.argv) > 1 else Path.home() / "github/GLM5.3-Flash-E224-DGX-Spark") SRC = ROOT / "mtp" DST = ROOT / "mtp-nvfp4" def read_shard(p: Path): with open(p, "rb") as f: n = struct.unpack("