asimfayaz commited on
Commit
a1c03ad
·
1 Parent(s): 5ec9af2

Add comprehensive OpenMP fork() prevention

Browse files

- Set extensive environment variables in both gradio_app.py and rembg.py
- Added KMP_INIT_AT_FORK=FALSE to prevent OpenMP initialization at fork
- Added OMP_WAIT_POLICY=PASSIVE for better thread management
- Added TOKENIZERS_PARALLELISM=false to prevent tokenizer conflicts
- Enhanced error detection for OpenMP fork() errors in rembg library
- Provides clear diagnostic messages when rembg internal processes fail

Files changed (2) hide show
  1. gradio_app.py +9 -4
  2. hy3dshape/hy3dshape/rembg.py +33 -7
gradio_app.py CHANGED
@@ -1,15 +1,20 @@
1
- # Set OpenMP environment variables to prevent fork() errors
 
2
  import os
3
  os.environ["OMP_NUM_THREADS"] = "1" # Limit OpenMP to single thread
4
- os.environ["PYTHONFAULTHANDLER"] = "1" # Better error reporting
5
- os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE" # Avoid Intel MKL errors
6
- os.environ["OMP_THREAD_LIMIT"] = "1" # Limit total number of OpenMP threads
7
  os.environ["MKL_NUM_THREADS"] = "1" # Intel MKL threading
8
  os.environ["NUMEXPR_NUM_THREADS"] = "1" # NumExpr threading
9
  os.environ["OPENBLAS_NUM_THREADS"] = "1" # OpenBLAS threading
10
  os.environ["VECLIB_MAXIMUM_THREADS"] = "1" # Apple vecLib threading
 
 
11
  os.environ["OMP_DISPLAY_ENV"] = "FALSE" # Suppress OpenMP environment display
12
  os.environ["KMP_WARNINGS"] = "FALSE" # Suppress KMP warnings
 
 
 
 
 
13
 
14
  # Hunyuan 3D is licensed under the TENCENT HUNYUAN NON-COMMERCIAL LICENSE AGREEMENT
15
  # except for the third-party components listed below.
 
1
+ # Set comprehensive OpenMP environment variables to prevent ANY fork() errors
2
+ # This must be done BEFORE importing any libraries that use OpenMP (torch, numpy, rembg, etc.)
3
  import os
4
  os.environ["OMP_NUM_THREADS"] = "1" # Limit OpenMP to single thread
 
 
 
5
  os.environ["MKL_NUM_THREADS"] = "1" # Intel MKL threading
6
  os.environ["NUMEXPR_NUM_THREADS"] = "1" # NumExpr threading
7
  os.environ["OPENBLAS_NUM_THREADS"] = "1" # OpenBLAS threading
8
  os.environ["VECLIB_MAXIMUM_THREADS"] = "1" # Apple vecLib threading
9
+ os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE" # Avoid Intel MKL errors
10
+ os.environ["OMP_THREAD_LIMIT"] = "1" # Limit total number of OpenMP threads
11
  os.environ["OMP_DISPLAY_ENV"] = "FALSE" # Suppress OpenMP environment display
12
  os.environ["KMP_WARNINGS"] = "FALSE" # Suppress KMP warnings
13
+ os.environ["PYTHONFAULTHANDLER"] = "1" # Better error reporting
14
+ # Additional settings to prevent subprocess/multiprocessing issues in rembg and other libraries
15
+ os.environ["TOKENIZERS_PARALLELISM"] = "false" # Disable tokenizer parallelism
16
+ os.environ["OMP_WAIT_POLICY"] = "PASSIVE" # Use passive waiting
17
+ os.environ["KMP_INIT_AT_FORK"] = "FALSE" # Don't initialize OpenMP at fork
18
 
19
  # Hunyuan 3D is licensed under the TENCENT HUNYUAN NON-COMMERCIAL LICENSE AGREEMENT
20
  # except for the third-party components listed below.
hy3dshape/hy3dshape/rembg.py CHANGED
@@ -17,12 +17,22 @@ import io
17
  import traceback
18
  import numpy as np
19
 
20
- # Set OpenMP environment variables to prevent threading conflicts
21
- # This allows rembg to run safely in the main thread without multiprocessing
22
  os.environ["OMP_NUM_THREADS"] = "1" # Limit OpenMP to single thread
23
- os.environ["PYTHONFAULTHANDLER"] = "1" # Better error reporting
 
 
 
24
  os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE" # Avoid Intel MKL errors
25
  os.environ["OMP_THREAD_LIMIT"] = "1" # Limit total number of OpenMP threads
 
 
 
 
 
 
 
26
 
27
  from PIL import Image
28
  from rembg import remove, new_session
@@ -113,9 +123,17 @@ class BackgroundRemover():
113
  print("Alpha matting successful!")
114
  return output
115
  except Exception as e:
 
116
  print(f"Alpha matting failed: {e}")
117
- import traceback
118
- traceback.print_exc()
 
 
 
 
 
 
 
119
 
120
  # If alpha matting fails, try standard background removal
121
  print("Alpha matting failed, falling back to standard background removal")
@@ -130,9 +148,17 @@ class BackgroundRemover():
130
  print("Standard background removal successful!")
131
  return output
132
  except Exception as e:
 
133
  print(f"Standard background removal failed: {e}")
134
- import traceback
135
- traceback.print_exc()
 
 
 
 
 
 
 
136
 
137
  # If all else fails, return the original image
138
  print("Background removal completely failed, returning original image")
 
17
  import traceback
18
  import numpy as np
19
 
20
+ # Set comprehensive OpenMP environment variables to prevent ANY threading/fork conflicts
21
+ # This prevents both our code and the rembg library from causing OpenMP fork() errors
22
  os.environ["OMP_NUM_THREADS"] = "1" # Limit OpenMP to single thread
23
+ os.environ["MKL_NUM_THREADS"] = "1" # Intel MKL threading
24
+ os.environ["NUMEXPR_NUM_THREADS"] = "1" # NumExpr threading
25
+ os.environ["OPENBLAS_NUM_THREADS"] = "1" # OpenBLAS threading
26
+ os.environ["VECLIB_MAXIMUM_THREADS"] = "1" # Apple Accelerate threading
27
  os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE" # Avoid Intel MKL errors
28
  os.environ["OMP_THREAD_LIMIT"] = "1" # Limit total number of OpenMP threads
29
+ os.environ["OMP_DISPLAY_ENV"] = "FALSE" # Don't display OpenMP environment
30
+ os.environ["KMP_WARNINGS"] = "FALSE" # Suppress KMP warnings
31
+ os.environ["PYTHONFAULTHANDLER"] = "1" # Better error reporting
32
+ # Additional settings to prevent subprocess/multiprocessing issues in rembg
33
+ os.environ["TOKENIZERS_PARALLELISM"] = "false" # Disable tokenizer parallelism
34
+ os.environ["OMP_WAIT_POLICY"] = "PASSIVE" # Use passive waiting
35
+ os.environ["KMP_INIT_AT_FORK"] = "FALSE" # Don't initialize OpenMP at fork
36
 
37
  from PIL import Image
38
  from rembg import remove, new_session
 
123
  print("Alpha matting successful!")
124
  return output
125
  except Exception as e:
126
+ error_msg = str(e)
127
  print(f"Alpha matting failed: {e}")
128
+
129
+ # Check for specific OpenMP fork errors
130
+ if "fork()" in error_msg and "OpenMP" in error_msg:
131
+ print("DETECTED: OpenMP fork() error in rembg library itself!")
132
+ print("This is an internal rembg issue, not our code.")
133
+ print("Falling back to standard background removal...")
134
+ else:
135
+ import traceback
136
+ traceback.print_exc()
137
 
138
  # If alpha matting fails, try standard background removal
139
  print("Alpha matting failed, falling back to standard background removal")
 
148
  print("Standard background removal successful!")
149
  return output
150
  except Exception as e:
151
+ error_msg = str(e)
152
  print(f"Standard background removal failed: {e}")
153
+
154
+ # Check for specific OpenMP fork errors
155
+ if "fork()" in error_msg and "OpenMP" in error_msg:
156
+ print("DETECTED: OpenMP fork() error in rembg library (standard mode)!")
157
+ print("The rembg library itself has internal multiprocessing that conflicts with OpenMP.")
158
+ print("This is a known issue with the rembg library in daemon environments.")
159
+ else:
160
+ import traceback
161
+ traceback.print_exc()
162
 
163
  # If all else fails, return the original image
164
  print("Background removal completely failed, returning original image")