Spaces:
Sleeping
Sleeping
Commit ·
f38a4b7
1
Parent(s): 940f9c7
Add sitecustomize.py to fix OMP_NUM_THREADS at Python startup
Browse files- post_install.py +39 -0
- requirements.txt +1 -0
- setup.py +24 -0
- sitecustomize.py +12 -0
post_install.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Post-install script to patch numexpr to handle invalid OMP_NUM_THREADS values
|
| 4 |
+
"""
|
| 5 |
+
import os
|
| 6 |
+
import site
|
| 7 |
+
|
| 8 |
+
def patch_numexpr():
|
| 9 |
+
"""Find and patch numexpr/utils.py to handle invalid OMP_NUM_THREADS"""
|
| 10 |
+
for site_dir in site.getsitepackages():
|
| 11 |
+
numexpr_utils = os.path.join(site_dir, 'numexpr', 'utils.py')
|
| 12 |
+
if os.path.exists(numexpr_utils):
|
| 13 |
+
print(f"Found numexpr at: {numexpr_utils}")
|
| 14 |
+
|
| 15 |
+
with open(numexpr_utils, 'r') as f:
|
| 16 |
+
content = f.read()
|
| 17 |
+
|
| 18 |
+
# Replace the problematic line
|
| 19 |
+
old_code = "requested_threads = int(os.environ['OMP_NUM_THREADS'])"
|
| 20 |
+
new_code = """try:
|
| 21 |
+
requested_threads = int(os.environ['OMP_NUM_THREADS'])
|
| 22 |
+
except (ValueError, TypeError):
|
| 23 |
+
requested_threads = 4 # Default for HF Spaces T4 GPU"""
|
| 24 |
+
|
| 25 |
+
if old_code in content and new_code not in content:
|
| 26 |
+
content = content.replace(old_code, new_code)
|
| 27 |
+
|
| 28 |
+
with open(numexpr_utils, 'w') as f:
|
| 29 |
+
f.write(content)
|
| 30 |
+
|
| 31 |
+
print("✅ Patched numexpr successfully!")
|
| 32 |
+
return True
|
| 33 |
+
else:
|
| 34 |
+
print("Already patched or couldn't find code to patch")
|
| 35 |
+
|
| 36 |
+
return False
|
| 37 |
+
|
| 38 |
+
if __name__ == '__main__':
|
| 39 |
+
patch_numexpr()
|
requirements.txt
CHANGED
|
@@ -93,3 +93,4 @@ validators==0.20.0
|
|
| 93 |
yarl==1.9.2
|
| 94 |
zipp==3.15.0
|
| 95 |
torch>=2.0.0
|
|
|
|
|
|
| 93 |
yarl==1.9.2
|
| 94 |
zipp==3.15.0
|
| 95 |
torch>=2.0.0
|
| 96 |
+
-e .
|
setup.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from setuptools import setup
|
| 2 |
+
from setuptools.command.install import install
|
| 3 |
+
import os
|
| 4 |
+
import shutil
|
| 5 |
+
import site
|
| 6 |
+
|
| 7 |
+
class PostInstallCommand(install):
|
| 8 |
+
def run(self):
|
| 9 |
+
install.run(self)
|
| 10 |
+
# Copy sitecustomize.py to site-packages
|
| 11 |
+
for site_dir in site.getsitepackages():
|
| 12 |
+
try:
|
| 13 |
+
dest = os.path.join(site_dir, 'sitecustomize.py')
|
| 14 |
+
shutil.copy('sitecustomize.py', dest)
|
| 15 |
+
print(f"✅ Installed sitecustomize.py to {dest}")
|
| 16 |
+
break
|
| 17 |
+
except:
|
| 18 |
+
continue
|
| 19 |
+
|
| 20 |
+
setup(
|
| 21 |
+
name='grdn-hf-fix',
|
| 22 |
+
version='0.0.1',
|
| 23 |
+
cmdclass={'install': PostInstallCommand},
|
| 24 |
+
)
|
sitecustomize.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
This file runs automatically when Python starts, before any imports.
|
| 3 |
+
Used to fix HuggingFace Spaces OMP_NUM_THREADS issue.
|
| 4 |
+
"""
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Fix OMP_NUM_THREADS before numexpr tries to use it
|
| 8 |
+
if 'OMP_NUM_THREADS' in os.environ:
|
| 9 |
+
value = str(os.environ['OMP_NUM_THREADS'])
|
| 10 |
+
if not value.isdigit():
|
| 11 |
+
os.environ['OMP_NUM_THREADS'] = '4'
|
| 12 |
+
print(f"Fixed OMP_NUM_THREADS from '{value}' to '4'")
|