"""Quantize Qwen3-30B-A3B-Instruct-2507 one part at a time, W4A16 (INT4, group 128) with llm-compressor 0.14. Used for content/standalone/05-quantization/12-quantizing-moe (2026-10-27). python moe_variants.py --out Every GPTQ variant: 512 perfectblend samples of 2,048 tokens (llm-compressor's MoE example), lm_head in BF16. - experts: routed experts only (attention and router stay BF16) - attn: attention projections only (experts and router stay BF16) - all: experts + attention, router BF16 (the published recipes) - all-routed: as "all", but each expert calibrated only on the tokens routed to it (moe_calibrate_all_experts=False) - rtn: as "all", round to nearest, no calibration data - nvfp4: NVFP4 W4A4 (weights and activations), not GPTQ: llm-compressor's examples/quantization_w4a4_fp4/ qwen_30b_a3b.py recipe (20 perfectblend samples of 2,048 tokens, all experts calibrated), for content/standalone/05-quantization/15-humming-quantized-kernels (arm C0, 2026-10-29); --calib-samples N changes the sample count (the 2026-10-31 static-scale check used 256) The router (mlp.gate) is a Qwen3MoeTopKRouter, not an nn.Linear, in transformers 5: targets="Linear" never reaches it, so the router ignore entry is kept only as the published recipes write it. moe_router_int4.py makes the router-quantized variant from "all". Writes /run-meta.json: seconds, peak CUDA memory. """ import argparse import json import time import torch from transformers import AutoModelForCausalLM, AutoTokenizer from llmcompressor import oneshot from llmcompressor.modifiers.gptq import GPTQModifier from llmcompressor.modifiers.quantization import QuantizationModifier from llmcompressor.utils import load_context MODEL = "Qwen/Qwen3-30B-A3B-Instruct-2507" ROUTER, ATTN, EXPERTS = "re:.*mlp.gate$", "re:.*self_attn.*", "re:.*mlp.experts.*" IGNORE = { "experts": ["lm_head", ROUTER, ATTN], "attn": ["lm_head", ROUTER, EXPERTS], "all": ["lm_head", ROUTER], "all-routed": ["lm_head", ROUTER], "rtn": ["lm_head", ROUTER], "nvfp4": ["lm_head", ROUTER], } def main(): ap = argparse.ArgumentParser() ap.add_argument("variant", choices=list(IGNORE)) ap.add_argument("--out", required=True) ap.add_argument("--calib-samples", type=int, help="nvfp4 only: calibration samples (default 20, the example's)") a = ap.parse_args() t0 = time.time() with load_context(): model = AutoModelForCausalLM.from_pretrained(MODEL, dtype=torch.bfloat16) tok = AutoTokenizer.from_pretrained(MODEL) if a.variant == "nvfp4": oneshot(model=model, dataset="perfectblend", splits="train[:512]", recipe=QuantizationModifier(targets="Linear", scheme="NVFP4", ignore=IGNORE["nvfp4"]), max_seq_length=2048, num_calibration_samples=a.calib_samples or 20, moe_calibrate_all_experts=True) elif a.variant == "rtn": oneshot(model=model, recipe=QuantizationModifier(targets="Linear", scheme="W4A16", ignore=IGNORE["rtn"])) else: oneshot(model=model, dataset="perfectblend", splits="train[:512]", recipe=GPTQModifier(targets="Linear", scheme="W4A16", ignore=IGNORE[a.variant]), max_seq_length=2048, num_calibration_samples=512, moe_calibrate_all_experts=(a.variant != "all-routed")) model.save_pretrained(a.out, save_compressed=True) tok.save_pretrained(a.out) meta = {"variant": a.variant, "ignore": IGNORE[a.variant], "calib_samples": a.calib_samples, "seconds": round(time.time() - t0), "peak_cuda_gib": round(torch.cuda.max_memory_allocated() / 2**30, 1)} json.dump(meta, open(f"{a.out}/run-meta.json", "w"), indent=1) print("saved", a.out, meta) if __name__ == "__main__": main()