File size: 1,590 Bytes
1289298
1fc7206
 
 
d6aa7f4
1fc7206
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6aa7f4
 
1fc7206
 
 
 
 
 
 
 
 
 
 
 
 
 
1289298
 
 
1fc7206
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
41
42
#!/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()