#!/usr/bin/env python3 """Preparation and setup script for Parvulus standalone native runtime assets.""" import argparse import os import subprocess import sys from pathlib import Path ROOT_DIR = Path(__file__).resolve().parent C_ENGINE_DIR = ROOT_DIR / "c_engine" def build_c_engine(): """Compiles C++/CUDA engine using make.""" lib_path = C_ENGINE_DIR / "libparvulus.so" cli_path = C_ENGINE_DIR / "parvulus_runtime" if lib_path.exists() and cli_path.exists(): print(f"[Prepare] Found existing {lib_path.name} and {cli_path.name}.") return print("\n[Prepare] Building C++/CUDA engine...") res = subprocess.run(["make", "-j", str(os.cpu_count() or 4)], cwd=str(C_ENGINE_DIR)) if res.returncode != 0: raise RuntimeError(f"C engine build failed with exit code {res.returncode}") print("[Prepare] C engine compiled successfully.") def export_binary_assets(): """Exports parvulus_model_weights.bin, action_scale.bin, and task_embeddings.bin from SafeTensors/JSON.""" out_bin = C_ENGINE_DIR / "parvulus_model_weights.bin" out_scale = C_ENGINE_DIR / "action_scale.bin" out_emb = C_ENGINE_DIR / "task_embeddings.bin" st_path = ROOT_DIR / "parvulus.safetensors" if not st_path.exists(): st_path = ROOT_DIR / "model.safetensors" if not st_path.exists(): raise FileNotFoundError(f"SafeTensors weights not found in {ROOT_DIR}") sys.path.insert(0, str(C_ENGINE_DIR)) from export_from_safetensors import export_binary_weights, export_action_scale, export_task_embeddings if not out_bin.exists(): print(f"\n[Prepare] Exporting binary model weights from {st_path.name}...") export_binary_weights(str(st_path), str(out_bin)) else: print(f"[Prepare] Found existing {out_bin.name} ({out_bin.stat().st_size / (1024*1024):.2f} MB).") if not out_scale.exists(): export_action_scale(str(ROOT_DIR / "action_scale.json"), str(out_scale)) else: print(f"[Prepare] Found existing {out_scale.name}.") if not out_emb.exists() and (C_ENGINE_DIR / "canonical_tasks.json").exists(): export_task_embeddings(str(C_ENGINE_DIR / "canonical_tasks.json"), str(st_path), str(out_emb)) elif out_emb.exists(): print(f"[Prepare] Found existing {out_emb.name}.") def obtain_text_encoder_onnx(hf_repo: str = None, force_convert: bool = False): """Obtains parvulus_text_encoder.onnx via Hugging Face download or local conversion.""" onnx_path = C_ENGINE_DIR / "parvulus_text_encoder.onnx" if onnx_path.exists() and not force_convert: print(f"[Prepare] Found existing {onnx_path.name} ({onnx_path.stat().st_size / (1024*1024):.1f} MB).") return repo_id = hf_repo or os.environ.get("PARVULUS_HF_REPO") if repo_id and not force_convert: print(f"\n[Prepare] Attempting to download {onnx_path.name} from Hugging Face Hub: '{repo_id}'...") try: from huggingface_hub import hf_hub_download downloaded_file = hf_hub_download( repo_id=repo_id, filename="parvulus_text_encoder.onnx", subfolder="c_engine", local_dir=str(ROOT_DIR), ) if Path(downloaded_file).exists(): print(f"[Prepare] Successfully downloaded ONNX model from Hugging Face Hub.") return except Exception as e: print(f"[Prepare] Hugging Face download from '{repo_id}' failed: {e}.") print("[Prepare] Falling back to local conversion...") print(f"\n[Prepare] Converting text encoder to ONNX locally (c_engine/export_text_encoder_onnx.py)...") sys.path.insert(0, str(C_ENGINE_DIR)) from export_text_encoder_onnx import export_onnx export_onnx( weights_path=str(ROOT_DIR / "parvulus.safetensors"), output_path=str(onnx_path), verify=True, ) def main(): parser = argparse.ArgumentParser(description="Prepare PARVULUS Standalone Native Runtime.") parser.add_argument( "--hf_repo", type=str, default=None, help="Optional Hugging Face repository ID (e.g. 'username/parvulus') to download pre-exported ONNX from", ) parser.add_argument( "--build_c", action="store_true", help="Force rebuild of C++/CUDA engine", ) parser.add_argument( "--convert_onnx", action="store_true", help="Force local ONNX conversion instead of downloading from HF", ) parser.add_argument( "--skip_onnx", action="store_true", help="Skip ONNX text encoder preparation (rely on pure vector conditioning or PyTorch fallback)", ) args = parser.parse_args() print("=" * 70) print(" PREPARING PARVULUS STANDALONE RUNTIME") print("=" * 70) if args.build_c: res = subprocess.run(["make", "clean"], cwd=str(C_ENGINE_DIR)) build_c_engine() export_binary_assets() if not args.skip_onnx: obtain_text_encoder_onnx(hf_repo=args.hf_repo, force_convert=args.convert_onnx) else: print("[Prepare] Skipping ONNX preparation (--skip_onnx set).") print("\n" + "=" * 70) print(" PARVULUS STANDALONE RUNTIME IS READY FOR DEPLOYMENT!") print("=" * 70) if __name__ == "__main__": main()