From a383ff68637b527096dac65447ef959a884cd7ab Mon Sep 17 00:00:00 2001 From: aimaaaimaa Date: Thu, 16 Apr 2026 18:46:40 +1000 Subject: [PATCH] fix: torch.from_numpy crash with numpy 2.x in frozen binary (#361) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit torch is compiled against numpy 1.x. numpy 2.x changed the ABI version returned by PyArray_GetNDArrayCVersion() (0x01000009 → 0x02000000), so torch's is_numpy_available() always returns False and torch.from_numpy() raises RuntimeError. This causes TTS generation to fail with: ValueError: Unable to create tensor, you should probably activate padding with 'padding=True' Two fixes: 1. Pin numpy<2.0 in requirements.txt so new builds bundle a compatible numpy version. (The existing comment already flagged this intention but the upper bound was never added.) 2. Add a PyInstaller runtime hook (pyi_rth_numpy_compat.py) that installs a ctypes memmove fallback for torch.from_numpy() at startup. Runtime hooks run after FrozenImporter is registered so frozen torch is importable. The fallback catches RuntimeError from the C-level ABI check and copies the numpy array into a new tensor via raw memory copy, bypassing the check entirely. This is a belt-and-suspenders fix that works regardless of the bundled numpy version. Co-authored-by: aimaaaimaa Co-authored-by: Claude Sonnet 4.6 --- backend/build_binary.py | 10 +++++ backend/pyi_rth_numpy_compat.py | 80 +++++++++++++++++++++++++++++++++ backend/requirements.txt | 2 +- 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 backend/pyi_rth_numpy_compat.py diff --git a/backend/build_binary.py b/backend/build_binary.py index 43ad0719..84c019ab 100644 --- a/backend/build_binary.py +++ b/backend/build_binary.py @@ -52,6 +52,16 @@ def build_server(cuda=False): if platform.system() == "Windows": args.append("--noconsole") + # numpy 2.x / torch ABI mismatch fix: install memmove fallback for + # torch.from_numpy() before the app starts. Runtime hooks run after + # FrozenImporter is registered so frozen torch/numpy are importable. + args.extend( + [ + "--runtime-hook", + str(backend_dir / "pyi_rth_numpy_compat.py"), + ] + ) + # Add local qwen_tts path if specified (for editable installs) qwen_tts_path = os.getenv("QWEN_TTS_PATH") if qwen_tts_path and Path(qwen_tts_path).exists(): diff --git a/backend/pyi_rth_numpy_compat.py b/backend/pyi_rth_numpy_compat.py new file mode 100644 index 00000000..2e3b5468 --- /dev/null +++ b/backend/pyi_rth_numpy_compat.py @@ -0,0 +1,80 @@ +""" +PyInstaller runtime hook: numpy 2.x / torch ABI mismatch fix. + +Problem +------- +torch is compiled against numpy 1.x headers. numpy 2.x changed the version +number returned by PyArray_GetNDArrayCVersion() (0x01000009 → 0x02000000), +so torch's is_numpy_available() returns False and every torch.from_numpy() +call raises: + + RuntimeError: Numpy is not available + +This surfaces as: + + ValueError: Unable to create tensor, you should probably activate + padding with 'padding=True' + +during TTS generation (EncodecFeatureExtractor → BatchFeature.convert_to_tensors). + +Fix +--- +Runtime hooks execute after PyInstaller's FrozenImporter is registered, so +frozen torch/numpy are importable here. We start a background thread that +waits for torch to finish loading then wraps torch.from_numpy with a ctypes +memmove fallback that bypasses the C-level numpy ABI check entirely. + +This approach works with any numpy version and is safer than binary-patching +libtorch_python.dylib (which risks PyArray_Descr struct layout mismatches). +""" + +import sys +import threading + + +def _patch_torch_from_numpy(): + import time + + for _ in range(7200): # poll up to 360 s at 50 ms intervals + time.sleep(0.05) + torch = sys.modules.get("torch") + if torch is None or not hasattr(torch, "from_numpy"): + continue + if getattr(torch, "_vb_from_numpy_patched", False): + return + try: + import ctypes + import numpy as np + + _orig = torch.from_numpy + + def _safe_from_numpy(arr, _orig=_orig, _c=ctypes, _np=np, _t=torch): + try: + return _orig(arr) + except RuntimeError: + a = _np.ascontiguousarray(arr) + dtype_map = { + "float32": _t.float32, + "float64": _t.float64, + "int32": _t.int32, + "int64": _t.int64, + "int16": _t.int16, + "int8": _t.int8, + "uint8": _t.uint8, + "bool": _t.bool, + } + out = _t.empty( + list(a.shape), + dtype=dtype_map.get(str(a.dtype), _t.float32), + ) + _c.memmove(out.data_ptr(), a.ctypes.data, a.nbytes) + return out + + torch.from_numpy = _safe_from_numpy + torch._vb_from_numpy_patched = True + except Exception: + pass + return + + +threading.Thread(target=_patch_torch_from_numpy, daemon=True).start() diff --git a/backend/requirements.txt b/backend/requirements.txt index e916b1d2..c8aa1877 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -50,7 +50,7 @@ en_core_web_sm @ https://github.com/explosion/spacy-models/releases/download/en_ # Audio processing librosa>=0.10.0 soundfile>=0.12.0 -numpy>=1.24.0 +numpy>=1.24.0,<2.0 numba>=0.60.0,<0.61.0 pedalboard>=0.9.0