#!/usr/bin/env python3 """GPTQ W4A16 body for LFM3-1.7B with llm-compressor (compressed-tensors pack-quantized: int4, sym, group 128, static activation order = no runtime g_idx). Quantizes the same 142 decoder-block linears as round 4 (attn q/k/v/o, conv in/out, MLP w1/w2/w3); self_attn.gate_proj (BF16 in our vLLM plugin), the tied embedding / lm_head, the conv kernels (nn.Conv1d) and norms stay BF16. lm_head and the INT8 embedding are added afterwards by bake_head_embed.py + assemble.py. llm-compressor's sequential pipeline traces Lfm3Model.forward and feeds every decoder layer the RoPE table and mask of its own type, so round 4's per-layer replay patch (AutoRound) is not needed here; check_body.py verifies the traced pipeline against the plain model. python3 quantize_body.py MODEL_DIR CALIB.pt OUT_DIR [DAMP=0.05] """ import json, sys, time import torch from datasets import Dataset from llmcompressor import oneshot from llmcompressor.args.dataset_arguments import DatasetArguments from llmcompressor.modifiers.quantization import GPTQModifier from transformers import AutoModelForCausalLM, AutoTokenizer model_dir, calib, out_dir = sys.argv[1:4] damp = float(sys.argv[4]) if len(sys.argv) > 4 else 0.05 tok = AutoTokenizer.from_pretrained(model_dir) model = AutoModelForCausalLM.from_pretrained(model_dir, dtype=torch.bfloat16, trust_remote_code=True) samples = torch.load(calib) ds = Dataset.from_dict({"input_ids": [s[0].tolist() for s in samples]}) linears = [n for n, m in model.named_modules() if isinstance(m, torch.nn.Linear)] ignore = ["lm_head", "re:.*self_attn\\.gate_proj$"] to_quant = [n for n in linears if n.startswith("model.layers.") and not n.endswith("self_attn.gate_proj")] print(f"{len(linears)} nn.Linear total; quantizing {len(to_quant)}; ignore {ignore}", flush=True) recipe = GPTQModifier(targets="Linear", scheme="W4A16", ignore=ignore, block_size=128, dampening_frac=damp, actorder="static") t0 = time.time() oneshot(model=model, dataset=ds, recipe=recipe, max_seq_length=samples[0].shape[-1], num_calibration_samples=len(samples), pipeline="sequential", trust_remote_code_model=True, sequential_targets=["Lfm3DecoderLayer"], tracing_ignore=DatasetArguments().tracing_ignore + ["create_masks_by_layer_type"]) dt = time.time() - t0 model.save_pretrained(out_dir, save_compressed=True) tok.save_pretrained(out_dir) json.dump({"tool": "llm-compressor 0.15.0 (compressed-tensors 0.20.1)", "scheme": "W4A16", "bits": 4, "group_size": 128, "sym": True, "actorder": "static", "dampening_frac": damp, "block_size": 128, "nsamples": len(samples), "seqlen": int(samples[0].shape[-1]), "seconds": round(dt), "quantized_linears": len(to_quant), "ignore": ignore}, open(f"{out_dir}/quant_run.json", "w"), indent=1) print(f"done in {dt/60:.1f} min -> {out_dir}")