#!/usr/bin/env python3 """Install an overlay only; never replace the verified CANN/TorchNPU stack.""" from __future__ import annotations import argparse import json import os from pathlib import Path import subprocess import sys import time PACKAGES = [ "transformers==4.57.0", "huggingface-hub==0.36.0", "safetensors==0.6.2", "soundfile==0.13.1", "pyarrow==20.0.0", "einops==0.8.1", "sacrebleu==2.5.1", "sentencepiece==0.2.1", "pytest==8.4.2", "numpy==2.2.6", ] def main(): p = argparse.ArgumentParser() p.add_argument("--root", type=Path, required=True) a = p.parse_args() root = a.root.resolve() overlay = root / "env" overlay.mkdir(exist_ok=False) os.environ.update(TMPDIR=str(root / "tmp"), PIP_CACHE_DIR=str(root / "pip-cache"), HF_HOME=str(root / "hf-cache"), HF_HUB_DISABLE_TELEMETRY="1") for name in ("tmp", "pip-cache", "hf-cache"): (root / name).mkdir(exist_ok=True) status = {"state": "running", "started_at": time.time(), "overlay": str(overlay)} def publish(): path = root / "control" / "setup.json" tmp = path.with_suffix(".tmp") tmp.write_text(json.dumps(status, indent=2)) os.replace(tmp, path) publish() try: subprocess.run([sys.executable, "-I", "-m", "pip", "install", "--target", str(overlay), "--only-binary=:all:", *PACKAGES], check=True, timeout=1200) # Official CPU wheel; dependencies disabled so it cannot upgrade Torch. subprocess.run([sys.executable, "-I", "-m", "pip", "install", "--target", str(overlay), "--no-deps", "--only-binary=:all:", "torchaudio==2.7.1", "--index-url", "https://download.pytorch.org/whl/cpu"], check=True, timeout=600) probe = "import sys,json;sys.path.insert(0," + repr(str(overlay)) + ");" + ( "import torch,torch_npu,torchaudio,transformers,pyarrow,soundfile,safetensors;" "assert torch.__version__.split('+')[0]=='2.7.1';" "assert torch_npu.__version__=='2.7.1.post4';" "assert torchaudio.__version__.split('+')[0]=='2.7.1';" "assert transformers.__version__=='4.57.0';" "print(json.dumps({'torch':torch.__version__,'torch_npu':torch_npu.__version__," "'torchaudio':torchaudio.__version__,'transformers':transformers.__version__," "'pyarrow':pyarrow.__version__,'soundfile':soundfile.__version__}))" ) result = subprocess.run([sys.executable, "-I", "-c", probe], check=True, capture_output=True, text=True, timeout=120) status.update(state="succeeded", versions=json.loads(result.stdout), finished_at=time.time()) print("NPU_SETUP_TERMINAL " + json.dumps(status), flush=True) except Exception as exc: status.update(state="failed", error_type=type(exc).__name__, finished_at=time.time()) print("NPU_SETUP_TERMINAL " + json.dumps(status), flush=True) raise finally: publish() if __name__ == "__main__": main()