Files
xpu-llm-ports/lfm3/tests/test_embed_int8.py
T
Pascal LinxweilerandClaude Opus 5.5 38b0e9a345 lfm3: round 6 - GPTQ INT4 baked (INT4 lm_head + INT8 embedding)
- quant/gptq_baked/: llm-compressor GPTQ W4A16 body (sequential pipeline, verified
  equal to the plain forward), B65-style GPTQ INT4 lm_head bake with a small
  damp/act-order/clipping grid, INT8 per-row embedding, assembly into one
  compressed-tensors checkpoint (1.155 GB vs 3.46 GB BF16)
- plugin: pass quant_config to the token embedding so vLLM's compressed-tensors
  INT8 embedding path is used; BF16/FP8 greedy output unchanged
- tests: INT8 embedding kernel check, quant method dump, head timing, round-6 driver
- NOTES: round 6 results (PPL +8.1%, decode 217 t/s = FP8 pc +14%, head bake
  gives no measurable decode gain on this stack), llama-swap snippet (not applied)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 14:48:48 +01:00

55 lines
2.7 KiB
Python

#!/usr/bin/env python3
"""INT8 embedding (compressed-tensors, per-row scale) on XPU: vLLM 0.32's own dequant-gather kernel
(CompressedTensorsEmbeddingWNA16Int, used by our Lfm3Model.embed_tokens for INT8 checkpoints) vs a
BF16 lookup. Two checks:
1. kernel == F.embedding(dequantized weight) for the gathered rows (bit-exact expected: same
int * scale product, rounded to BF16 once);
2. error vs the original BF16 embedding (quantization error only).
Run inside the image: python3 tests/test_embed_int8.py [EMBED_INT8.safetensors] [MODEL_DIR]
Without arguments it uses a random 128000 x 2048 weight.
"""
import sys
import torch
import torch.nn.functional as F
from compressed_tensors.compressors.pack_quantized.helpers import pack_to_int32
from vllm.model_executor.layers.quantization.compressed_tensors.compressed_tensors_embedding import (
_dequant_gather_triton,
)
dev = "xpu"
torch.manual_seed(0)
if len(sys.argv) > 2:
from safetensors import safe_open
with safe_open(sys.argv[1], "pt") as f:
packed = f.get_tensor("model.embed_tokens.weight_packed").to(dev)
scale = f.get_tensor("model.embed_tokens.weight_scale").to(dev)
with safe_open(f"{sys.argv[2]}/model.safetensors", "pt") as f:
W = f.get_tensor("model.embed_tokens.weight").to(dev)
codes = None
else:
W = (torch.randn(128000, 2048) * 0.02).to(torch.bfloat16).to(dev)
s = (W.float().abs().amax(1) / 127).clamp_min(1e-8).to(torch.bfloat16)
codes = torch.clamp(torch.round(W.float() / s.float()[:, None]), -128, 127).to(torch.int8)
packed, scale = pack_to_int32(codes.cpu(), 8, packed_dim=1).to(dev), s[:, None].to(dev)
V, K = W.shape
# unpack all rows once (reference path): 4 int8 per int32, LSB first, offset 128
u = packed.view(torch.uint8).view(V, K).to(torch.int16) - 128
if codes is not None:
assert torch.equal(u.to(torch.int8), codes), "pack/unpack mismatch"
deq = (u.float() * scale.float()).to(torch.bfloat16)
ids = torch.cat([torch.randint(0, V, (4096,)), torch.tensor([0, 1, V - 2, V - 1, 124893, 124900])]).to(dev)
out = _dequant_gather_triton(ids, packed, scale, K, 8)
ref_deq = F.embedding(ids, deq)
ref_bf16 = F.embedding(ids, W)
exact = torch.equal(out, ref_deq)
max_d = (out.float() - ref_deq.float()).abs().max().item()
rel = ((out.float() - ref_bf16.float()).norm() / ref_bf16.float().norm()).item()
cos = F.cosine_similarity(out.float(), ref_bf16.float(), dim=1).min().item()
print(f"kernel vs dequantized lookup: {'bit-exact' if exact else f'max |d| {max_d:.3g}'}")
print(f"vs BF16 embedding: relative L2 {rel:.4%}, min row cosine {cos:.6f} ({len(ids)} rows, dtype {out.dtype})")
assert out.dtype == torch.bfloat16 and max_d <= 2 ** -7 * ref_deq.float().abs().max().item()
assert rel < 0.02
print("PASS")