Files
vllm-omni/benchmarks/edge_harness/experiments/quantize_int8.py
T
tzhouamandClaude Opus 5 3834de3105 Rename the project to edge-infer, and complete the packaging audit
Renamed from vllm-embedding: nothing here is about embeddings, and the name had
come from the parent working directory. edge-infer says what it does. The
GitHub repository is renamed to match; nothing had been pushed yet.

The audit found the packaging was incomplete in two ways.

Scripts: 56 of them produced results that are published in this repo but were
not included, so the numbers could be read and not checked. Added under
benchmarks/edge_harness/experiments/ (one-off drivers: the AI Hub on-device
runs, the 4-bit GEMM microbenchmark, fidelity and parity checks, checkpoint
build and round-trip, activation capture, and the load generator used to prove
the contention guard actually refuses) and benchmarks/edge_harness/runs/ (the
24 shell drivers behind published grids). Both directories carry a README
saying plainly that they are a record of what was run rather than a supported
interface -- most hardcode absolute paths from the machine they ran on. The
supported entry points remain the tools one level up.

One broken relative link fixed (comparison.md is under docs/engines/, not
docs/analysis/). All other links and every script reference in the docs now
resolve inside the repo.

Secret scan: the token in ~/.qai_hub/client.ini is absent from the worktree and
from all 61 commits. Worth noting that an earlier version of my own check
reported a false positive, because `grep -rl ... | head` takes its exit status
from head, which always succeeds -- the same class of mistake as the measurement
errors this project keeps recording. The AI Hub scripts authenticate by reading
that file at runtime and contain no credential.

README reframed for the new name and the wider scope: the repository is the
engine changes, the harness, the raw results, the baselines and the analyses,
not just a patch set.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L1ax5wYLA6XbqEL1i8L9gZ
2026-09-14 14:21:07 +08:00

104 lines
3.5 KiB
Python

"""Write a compressed-tensors W8A8-int8 checkpoint for Spark-X2.5.
Done directly from the safetensors rather than through llmcompressor: that
package pulls transformers 5.x, on which Spark's remote modelling code does
not import, and symmetric per-channel round-to-nearest needs no calibration
data anyway.
The head-wise gate (g_proj) is deliberately left in full precision. It is a
2048x8 projection whose eight outputs each scale a whole attention head, so
quantisation error there is multiplied across 256 dims -- and it is 0.001% of
the weights, so leaving it out costs nothing.
"""
import argparse, json, shutil
from pathlib import Path
import torch
from safetensors.torch import load_file, save_file
QUANT_SUFFIXES = (
"self_attn.q_k_v_proj.weight",
"self_attn.out_proj.weight",
"mlp.gate_proj.weight",
"mlp.up_proj.weight",
"mlp.down_proj.weight",
)
def quantize_channelwise(w: torch.Tensor):
"""Symmetric per-output-channel int8."""
w = w.to(torch.float32)
scale = w.abs().amax(dim=1, keepdim=True) / 127.0
scale = torch.clamp(scale, min=1e-12)
q = torch.clamp(torch.round(w / scale), -127, 127).to(torch.int8)
return q, scale.to(torch.float32)
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--src", required=True)
ap.add_argument("--dst", required=True)
a = ap.parse_args()
src, dst = Path(a.src), Path(a.dst)
dst.mkdir(parents=True, exist_ok=True)
state = {}
for shard in sorted(src.glob("*.safetensors")):
state.update(load_file(str(shard)))
out, n_q, err = {}, 0, []
for name, t in state.items():
if name.endswith(QUANT_SUFFIXES):
q, s = quantize_channelwise(t)
out[name] = q
out[name + "_scale"] = s
deq = q.to(torch.float32) * s
ref = t.to(torch.float32)
err.append(float(
20 * torch.log10(ref.norm() / (ref - deq).norm().clamp(min=1e-12))
))
n_q += 1
else:
out[name] = t
save_file(out, str(dst / "model.safetensors"), metadata={"format": "pt"})
for f in ("config.json", "tokenizer.json", "tokenizer_config.json",
"special_tokens_map.json", "vocab.json", "merges.txt",
"generation_config.json", "chat_template.jinja"):
if (src / f).exists():
shutil.copy(src / f, dst / f)
cfg = json.loads((dst / "config.json").read_text())
cfg.pop("auto_map", None) # the vendored vLLM config/model handle this arch
cfg["quantization_config"] = {
"quant_method": "compressed-tensors",
"format": "int-quantized",
"quantization_status": "compressed",
"ignore": ["lm_head", "re:.*g_proj.*"],
"config_groups": {
"group_0": {
"targets": ["Linear"],
"weights": {
"num_bits": 8, "type": "int", "symmetric": True,
"strategy": "channel", "dynamic": False,
"observer": "minmax",
},
"input_activations": {
"num_bits": 8, "type": "int", "symmetric": True,
"strategy": "token", "dynamic": True, "observer": None,
},
}
},
}
(dst / "config.json").write_text(json.dumps(cfg, indent=2))
print(json.dumps({
"quantized_tensors": n_q,
"weight_snr_db_min": round(min(err), 2),
"weight_snr_db_mean": round(sum(err) / len(err), 2),
"dst": str(dst),
}, indent=2))
main()