Spaces:
Running on Zero

File size: 1,850 Bytes
5e0b6f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env bash
# macOS local development only (NOT used in Docker / on the Space).
#
# torch, faiss-cpu and scikit-learn each bundle their own libomp.dylib and the
# xgboost wheel loads Homebrew's. Four OpenMP runtimes in one process segfault
# (exit 139) as soon as models load in a worker thread. Fix: point every copy at
# torch's libomp and make the dynamic loader resolve `@rpath/libomp.dylib` there.
#
#   source deploy/macos_dev_env.sh      # after: python3.12 -m venv .venv && pip install -r deploy/requirements.txt
#   python start.py
#
# Launch python DIRECTLY from the shell where you sourced this. macOS strips
# DYLD_LIBRARY_PATH when a protected system binary runs (nohup, sudo, env, open,
# /bin/sh -c ...), so `nohup python start.py` loads Homebrew's libomp again and
# segfaults in the warm-up thread.
# works when sourced from bash OR zsh (the macOS default shell)
if [ -n "${BASH_SOURCE:-}" ]; then
  _cp_here="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
elif [ -n "${ZSH_VERSION:-}" ]; then
  _cp_here="$(cd "$(dirname "$(eval 'echo "${(%):-%x}"')")/.." && pwd)"
else
  _cp_here="$PWD"
fi
VENV="${VENV:-$_cp_here/.venv}"
SP="$VENV/lib/python3.12/site-packages"
if [ ! -f "$SP/torch/lib/libomp.dylib" ]; then
  echo "torch not found in $VENV - create the venv and install deploy/requirements.txt first" >&2
  return 1 2>/dev/null || exit 1
fi
for d in faiss/.dylibs sklearn/.dylibs; do
  if [ -f "$SP/$d/libomp.dylib" ] && [ ! -L "$SP/$d/libomp.dylib" ]; then
    mv "$SP/$d/libomp.dylib" "$SP/$d/libomp.dylib.orig"
    ln -s "$SP/torch/lib/libomp.dylib" "$SP/$d/libomp.dylib"
    echo "unified $d/libomp.dylib -> torch/lib/libomp.dylib"
  fi
done
export DYLD_LIBRARY_PATH="$SP/torch/lib${DYLD_LIBRARY_PATH:+:$DYLD_LIBRARY_PATH}"
export PATH="$VENV/bin:$PATH"
echo "macOS dev env ready (DYLD_LIBRARY_PATH -> torch/lib)"