Save and load quantized model checkpoints using the safetensors format.
Two functions:
save_quantized_model(model, output_dir, bits, group_size, method)
Walks every QuantLinear in the model and serializes:
{name}.weight_q— int8 integer codes{name}.scales— fp16 scale factors{name}.zero_points— uint8 zero-points (only if asymmetric and non-zero){name}.input_channel_scales— fp16 (AWQ only; the per-channelsvector)
Also writes quant_config.json with metadata: bits, group_size, method, skipped_modules.
load_quantized_model(model, checkpoint_dir)
Reads the .safetensors file and the quant_config.json, then:
- Reads
quant_config.jsonto getbits,group_size - Replaces each
nn.Linear(that has saved tensors) with a newQuantLinear - Loads
weight_q,scales,zero_points, andinput_channel_scalesinto the layer
- Safe: no code execution on load (unlike pickle)
- Fast: memory-mapped loading for large files
- Compatible: loadable by vLLM, HF Transformers, llama.cpp
output_dir/
├── quantized_model.safetensors # all quantized layer tensors
├── quant_config.json # bits, group_size, method, etc.
├── config.json # original HF model config (from tokenizer.save_pretrained)
├── tokenizer.json # tokenizer
└── tokenizer_config.json # tokenizer config
input_channel_scales must survive save/load — without it, the AWQ scaling trick
(W*s) @ (x/s) = W@x breaks silently (outputs wrong, no error).
The loader explicitly checks for and restores this buffer.