Buckets:

rhtse's picture
download
raw
2.84 kB
"""Parity debug — localize where the v6a artifact diverges: encoder
hidden states, then first decoder step logits, torch vs shipped ONNX,
on identical inputs.
"""
import tempfile
import zipfile
from pathlib import Path
import numpy as np
import onnxruntime as ort
import torch
RUN = "run-080-thai-lex-v6a"
CKPT = Path("/ckpt") / RUN / "best"
EXPORT = Path("/ckpt") / RUN / "export"
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer # noqa: E402
tok = AutoTokenizer.from_pretrained(CKPT)
model = AutoModelForSeq2SeqLM.from_pretrained(CKPT, torch_dtype=torch.float32).eval()
ids = tok("สวัสดี", return_tensors="pt").input_ids
print(f"[dbg] input ids: {ids.tolist()}", flush=True)
with torch.no_grad():
torch_hidden = model.get_encoder()(input_ids=ids)[0]
dec_start = torch.tensor([[0]])
torch_step1 = model.lm_head(
model.get_decoder()(input_ids=dec_start,
encoder_hidden_states=torch_hidden)[0])
torch_gen = model.generate(ids, max_new_tokens=32, num_beams=1)
torch_pred = tok.decode(torch_gen[0], skip_special_tokens=True)
print(f"[torch] step1 top5: {torch_step1[0, -1].topk(5).indices.tolist()}", flush=True)
print(f"[torch] greedy32: {torch_pred!r}", flush=True)
zips = sorted(EXPORT.glob("*.zip"))
_tmp = tempfile.mkdtemp()
with zipfile.ZipFile(zips[0]) as z:
for name in ("encoder.onnx", "decoder.onnx"):
(Path(_tmp) / name).write_bytes(z.read(name))
eso = ort.InferenceSession(str(Path(_tmp) / "encoder.onnx"),
providers=["CPUExecutionProvider"])
dso = ort.InferenceSession(str(Path(_tmp) / "decoder.onnx"),
providers=["CPUExecutionProvider"])
onnx_hidden = eso.run(None, {"input_ids": ids.numpy()})[0]
diff_h = np.abs(onnx_hidden.astype(np.float32)
- torch_hidden.numpy().astype(np.float32)).max()
print(f"[onnx] encoder hidden max|diff| = {diff_h:.6f}", flush=True)
onnx_step1 = dso.run(None, {"input_ids": np.array([[0]], dtype=np.int64),
"encoder_hidden_states": onnx_hidden.astype(np.float32)})[0]
print(f"[onnx] step1 top5: {np.argpartition(onnx_step1[0, -1], -5)[-5:][::-1].tolist()}", flush=True)
diff_l = np.abs(onnx_step1 - torch_step1.numpy()).max()
print(f"[onnx] step1 logits max|diff| = {diff_l:.4f}", flush=True)
# greedy 32 in ONNX with the torch hidden (isolates the decoder)
seq = np.array([[0]], dtype=np.int64)
for _ in range(32):
lg = dso.run(None, {"input_ids": seq,
"encoder_hidden_states": onnx_hidden.astype(np.float32)})[0]
seq = np.concatenate([seq, lg[:, -1].argmax(-1, keepdims=True)], axis=1)
if seq[0, -1] == 1:
break
body = seq[0, 1:-1] if seq[0, -1] == 1 else seq[0, 1:]
print(f"[onnx] greedy32 bytes: {bytes([b for b in body if b >= 3])!r}", flush=True)

Xet Storage Details

Size:
2.84 kB
·
Xet hash:
15ecbf67e4dd20ee2468648c54be3a9d7fe64fa5ee3b354b3ea78c95780cc34d

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.