Download c_engine/export_text_encoder_onnx.py from aasivas/Parvulus: direct link, hf CLI and curl.
- Browser
- Download file 5.71 kB
-
https://huggingface.co/aasivas/Parvulus/resolve/main/c_engine/export_text_encoder_onnx.py
- Command line
-
hf download hf://aasivas/Parvulus/c_engine/export_text_encoder_onnx.py
-
curl -L -o export_text_encoder_onnx.py https://huggingface.co/aasivas/Parvulus/resolve/main/c_engine/export_text_encoder_onnx.py
5.71 kB
| #!/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() | |