MogensR's picture
Update app.py
1fc7206
raw
history blame
1.59 kB
#!/usr/bin/env python3
# app.py — process entrypoint
# --- MUST come before any other imports (especially numpy/torch/opencv) ---
import os
import sys
# Sanitize OpenMP/BLAS thread envs *before* heavy libs initialize
omp = os.environ.get("OMP_NUM_THREADS", "")
if not omp.strip().isdigit():
# Spaces sometimes sets OMP_NUM_THREADS to an invalid string → crash in libgomp
os.environ["OMP_NUM_THREADS"] = "2" # safe default
# Reasonable defaults; adjust if you profile differently
os.environ.setdefault("MKL_NUM_THREADS", "2")
os.environ.setdefault("OPENBLAS_NUM_THREADS", "2")
os.environ.setdefault("NUMEXPR_NUM_THREADS", "2")
# (Optional) If you’ve seen duplicate OpenMP issues on certain bases, uncomment:
# os.environ.setdefault("KMP_DUPLICATE_LIB_OK", "TRUE")
# If you want to prepend repo root to sys.path (your original behavior)
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
# Optionally set PyTorch intra-op threads early (must be before parallel work starts).
# We do it right after importing torch but still prior to importing the rest of the app.
try:
import torch
# Only set if not already configured by the environment
if os.environ.get("TORCH_NUM_THREADS") is None:
torch.set_num_threads(2)
if os.environ.get("TORCH_NUM_INTEROP_THREADS") is None:
torch.set_num_interop_threads(2)
except Exception:
# If torch isn't installed here or fails to import, ignore and continue.
pass
# Now it’s safe to import the rest of the application.
from core.app import main
if __name__ == "__main__":
main()