Quantization Scipt

#1
by vgoklani - opened

Thanks for sharing! Could you please share your quantization script for ModelOpt?

Thanks!

saricles changed discussion status to closed
saricles changed discussion status to open

Sure give me a little time - I'll share that out later today

I dropped the script in this same repo: quantize-nvfp4-gb10.py.

It's the recipe that produced this exact quant — env-var configurable, with a documented header explaining the MoE-expert calibration gotcha you'll hit on M2 (where 64 calibration samples × top-K=2 routing leaves most of the 256 experts un-touched, so their weight_quantizer.amax stays unset and export_hf_checkpoint asserts). Phase 2.5 of the script handles that — mirrors _calibrate_weight_quantizer_if_needed from newer modelopt versions for folks on older releases that don't have that auto-fix yet.

If you adapt it for another architecture, the only model-specific bit is the ignore-list patterns near the bottom of Phase 2 — comments call out what to change. Hope it's useful!

Thank you for sharing! Why do you only use 64 samples for calibration?

Gonna be honest with you - I'm a bit of a newb, learning as I go. As such - I've been using my AI agents to facilitate my efforts. 😁
Here's the response my agent helped me to prepare:

64 is the standard for NVFP4 weight quantization — it's computing per-block scaling factors (amax), not fine-tuning, so the statistics converge fast. 64 samples × 2048 tokens = roughly 131K tokens of activation data, which gives a representative distribution of weight activation magnitudes.

More samples have diminishing returns on amax quality but linear cost — each sample is a full forward pass through a 230B model. On A100x8 that's roughly 25-30s per sample, so 64 = roughly 30 min. The quality delta between 64 and 256 samples is negligible for NVFP4 weight scales.

The real calibration challenge on MoE isn't sample count — it's expert coverage. With 256 experts and top-K=8 routing, 64 samples × 2048 tokens drive roughly 1M expert activations per layer (approximately 4K per expert on average). Most experts see plenty of calibration, but routing is heavily skewed — popular experts dominate while tail experts may be undersampled or never fire. That's why Phase 2.5 exists: it force-populates amax from weight statistics on the never-activated experts. The amax from weight stats is slightly less precise than activation-derived amax, but for experts that were never routed during calibration, any reasonable scale is fine.

NVIDIA's own ModelOpt examples use 64-128 samples. If you wanted to push quality, 128 would help expert coverage marginally, but the amax-populate fix handles the real gap.

Edit: corrected top-K value from 2 → 8 (actual MiniMax-M2.7 routing). Expert-activation math updated accordingly.

Could you also share a matching requirements.txt or pyproject deps block that satisifes the reqs for the quantization script?

I'm a huge fan of this, but I have struggled with some of my quant efforts because it has been tricky to get a reqs file with modern versions of vllm, llmcompressor, and compressed-tensors pip packages in the same venv, and these scripts are all very API sensitive.

Thank you for this, it's already a great reference for me!

Sign up or log in to comment