File size: 5,710 Bytes
67c4f10 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | #!/usr/bin/env python3
"""Export Parvulus text encoder (T5-small + projection head) to standalone ONNX format.
Enables zero-PyTorch dynamic text encoding and perturbation handling via ONNX Runtime.
"""
import argparse
import sys
from pathlib import Path
_ROOT = Path(__file__).resolve().parent.parent
if str(_ROOT) not in sys.path:
sys.path.insert(0, str(_ROOT))
import torch
import torch.nn as nn
from safetensors.torch import load_file
from text_encoder import ParvulusTextEncoder
class ParvulusTextEncoderONNX(nn.Module):
"""Wraps T5EncoderModel + mean pooling + trained 2-layer projection head."""
def __init__(self, text_encoder: ParvulusTextEncoder):
super().__init__()
self.t5 = text_encoder.t5
self.proj = text_encoder.proj
def forward(self, input_ids: torch.Tensor, attention_mask: torch.Tensor) -> torch.Tensor:
out = self.t5(input_ids=input_ids, attention_mask=attention_mask).last_hidden_state
mask = attention_mask.unsqueeze(-1).to(out.dtype)
summed = (out * mask).sum(dim=1)
count = mask.sum(dim=1).clamp(min=1e-6)
pooled = summed / count
return self.proj(pooled)
def export_onnx(
weights_path: str = "parvulus.safetensors",
output_path: str = "c_engine/parvulus_text_encoder.onnx",
model_name: str = "google-t5/t5-small",
revision: str = "df1b051c49625cf57a3d0d8d3863ed4d13564fe4",
opset_version: int = 14,
verify: bool = True,
):
"""Converts the Parvulus text encoder to standalone ONNX format."""
weights_p = Path(weights_path)
if not weights_p.is_absolute():
weights_p = _ROOT / weights_path
if not weights_p.exists():
fallback_p = _ROOT / "model.safetensors"
if fallback_p.exists():
weights_p = fallback_p
else:
raise FileNotFoundError(f"SafeTensors weights not found at {weights_p} or {fallback_p}")
out_p = Path(output_path)
if not out_p.is_absolute():
out_p = _ROOT / output_path
out_p.parent.mkdir(parents=True, exist_ok=True)
print(f"[Converter] Loading text encoder base ({model_name} @ {revision[:8]})...")
te = ParvulusTextEncoder(cond_dim=256, model_name=model_name, revision=revision).cpu().eval()
print(f"[Converter] Loading projection head weights from {weights_p.name}...")
weights = load_file(str(weights_p))
proj_dict = {
"0.weight": weights["text_encoder.proj.0.weight"],
"0.bias": weights["text_encoder.proj.0.bias"],
"2.weight": weights["text_encoder.proj.2.weight"],
"2.bias": weights["text_encoder.proj.2.bias"],
}
te.proj.load_state_dict(proj_dict)
model_onnx = ParvulusTextEncoderONNX(te).eval()
sample_prompt = "pick up the black bowl between the plate and the ramekin and place it on the plate"
tokens = te.tokenizer([sample_prompt], padding=True, truncation=True, return_tensors="pt")
print(f"[Converter] Exporting ONNX model to {out_p} (opset {opset_version})...")
torch.onnx.export(
model_onnx,
(tokens["input_ids"], tokens["attention_mask"]),
str(out_p),
input_names=["input_ids", "attention_mask"],
output_names=["text_emb"],
dynamic_axes={
"input_ids": {0: "batch", 1: "seq_len"},
"attention_mask": {0: "batch", 1: "seq_len"},
"text_emb": {0: "batch"},
},
opset_version=opset_version,
)
print(f"[Converter] Successfully exported ONNX model ({out_p.stat().st_size / (1024*1024):.1f} MB).")
if verify:
try:
import onnxruntime as ort
sess = ort.InferenceSession(str(out_p))
ort_out = sess.run(
None,
{
"input_ids": tokens["input_ids"].numpy(),
"attention_mask": tokens["attention_mask"].numpy(),
},
)[0]
ref_emb = te([sample_prompt]).detach().numpy()
max_diff = float(abs(ref_emb - ort_out).max())
print(f"[Converter] Numerical parity verified! Max absolute diff vs PyTorch: {max_diff:.3e}")
except ImportError:
print("[Converter] onnxruntime not installed; skipping verification check.")
def main():
parser = argparse.ArgumentParser(
description="Convert Parvulus text encoder to standalone ONNX format for zero-PyTorch dynamic text encoding."
)
parser.add_argument(
"--weights",
type=str,
default="parvulus.safetensors",
help="Path to parvulus.safetensors or model.safetensors",
)
parser.add_argument(
"--output",
type=str,
default="c_engine/parvulus_text_encoder.onnx",
help="Path to save output ONNX model",
)
parser.add_argument(
"--model_name",
type=str,
default="google-t5/t5-small",
help="Base T5 model name on Hugging Face Hub",
)
parser.add_argument(
"--revision",
type=str,
default="df1b051c49625cf57a3d0d8d3863ed4d13564fe4",
help="Pinned commit revision on Hugging Face Hub",
)
parser.add_argument(
"--opset",
type=int,
default=14,
help="ONNX opset version (default: 14)",
)
parser.add_argument(
"--no-verify",
action="store_true",
help="Skip numerical verification with onnxruntime",
)
args = parser.parse_args()
export_onnx(
weights_path=args.weights,
output_path=args.output,
model_name=args.model_name,
revision=args.revision,
opset_version=args.opset,
verify=not args.no_verify,
)
if __name__ == "__main__":
main()
|