Reverb commited on
Commit
aead907
·
1 Parent(s): c901117

Preload libcudart.so.13 via ctypes.CDLL before nvdiffrast import

Browse files
Files changed (1) hide show
  1. app.py +23 -8
app.py CHANGED
@@ -25,14 +25,29 @@ import sys
25
  import time
26
  from pathlib import Path
27
 
28
- # nvdiffrast is compiled against the machine's CUDA 13.0 toolkit, so its .so
29
- # links to libcudart.so.13. Add the CUDA 13.0 lib dir before any import so
30
- # the dynamic linker can find it. Must happen before `import nvdiffrast`.
31
- _cuda_lib = "/cuda-image/usr/local/cuda-13.0/lib64"
32
- if os.path.isdir(_cuda_lib):
33
- os.environ["LD_LIBRARY_PATH"] = (
34
- _cuda_lib + ":" + os.environ.get("LD_LIBRARY_PATH", "")
35
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  import gradio as gr
38
  import spaces # HF ZeroGPU
 
25
  import time
26
  from pathlib import Path
27
 
28
+ # nvdiffrast is compiled against CUDA 13.0 and its .so links to libcudart.so.13.
29
+ # os.environ["LD_LIBRARY_PATH"] only affects child processes, not the running
30
+ # Python interpreter. Use ctypes.CDLL with RTLD_GLOBAL to load libcudart.so.13
31
+ # into the current process before nvdiffrast is imported.
32
+ import ctypes, glob as _glob
33
+
34
+ def _preload_cudart() -> None:
35
+ cuda_lib_dir = "/cuda-image/usr/local/cuda-13.0/lib64"
36
+ # Try versioned name first, then unversioned fallback.
37
+ for pattern in (
38
+ f"{cuda_lib_dir}/libcudart.so.13*",
39
+ f"{cuda_lib_dir}/libcudart.so",
40
+ ):
41
+ matches = _glob.glob(pattern)
42
+ if matches:
43
+ try:
44
+ ctypes.CDLL(matches[0], mode=ctypes.RTLD_GLOBAL)
45
+ print(f"Preloaded {matches[0]}", flush=True)
46
+ except OSError as e:
47
+ print(f"Preload failed for {matches[0]}: {e}", flush=True)
48
+ return
49
+
50
+ _preload_cudart()
51
 
52
  import gradio as gr
53
  import spaces # HF ZeroGPU