#!/usr/bin/env python3 """Check that llm-compressor's traced sequential pipeline (what quantize_body.py calibrates with) is the same function as the plain LFM3 forward: trace the BF16 model with the same sequential targets and tracing_ignore, run the 25 subgraphs one after another on a 1000-token calibration sample (as quant/check_patch.py in round 4) and compare the logits with model(x).logits. python3 check_body.py MODEL_DIR CALIB.pt [DEVICE=xpu] """ import sys import torch from llmcompressor.args.dataset_arguments import DatasetArguments from llmcompressor.pipelines.sequential.helpers import trace_subgraphs from transformers import AutoModelForCausalLM model_dir, calib = sys.argv[1:3] dev = sys.argv[3] if len(sys.argv) > 3 else "xpu" model = AutoModelForCausalLM.from_pretrained(model_dir, dtype=torch.bfloat16, trust_remote_code=True).to(dev).eval() x = torch.load(calib)[3][:, :1000].to(dev) inputs = {"input_ids": x} with torch.no_grad(): ref = model(**inputs).logits.float() subgraphs = trace_subgraphs(model, inputs, ["Lfm3DecoderLayer"], DatasetArguments().tracing_ignore + ["create_masks_by_layer_type"]) acts = dict(inputs) for sg in subgraphs: acts.update(sg.forward(model, **{k: acts[k] for k in sg.input_names if k in acts})) out = next(v for v in acts.values() if torch.is_tensor(v) and v.shape[-1] == ref.shape[-1]).float() print(f"{len(subgraphs)} subgraphs; max |dlogit| {(out - ref).abs().max().item()}, " f"top1 agree {(out.argmax(-1) == ref.argmax(-1)).float().mean().item()}")