painter3000 commited on
Commit
b58a8a1
·
verified ·
1 Parent(s): c81908d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -22
app.py CHANGED
@@ -1,8 +1,10 @@
 
 
 
 
1
  import spaces
2
  import sys
3
  import torch
4
- import os
5
- os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
6
  import shutil
7
  import tempfile
8
  import gradio as gr
@@ -18,20 +20,17 @@ from pathlib import Path
18
 
19
 
20
  def _get_subprocess_env() -> dict:
21
- """Baut die Subprocess-Umgebung mit korrekten CUDA- und Library-Pfaden.
22
 
23
- Wichtige Fixes:
24
- - LD_LIBRARY_PATH: libcudart.so via nvidia pip-Pakete findbar machen
25
- - PYTORCH_CUDA_ALLOC_CONF: expandable_segments:True nutzt NVML, das im
26
- Subprocess auf ZeroGPU Blackwell + torch 2.11.0 nicht stabil ist.
27
- Im Subprocess max_split_size_mb:512 verwenden (NVML-frei, stabil).
28
  """
29
  import site
30
  import glob as _glob
31
 
32
  env = os.environ.copy()
33
 
34
- # 1. CUDA-Library-Pfade für libcudart
35
  nvidia_paths: list[str] = []
36
  all_site = site.getsitepackages()
37
  try:
@@ -53,16 +52,47 @@ def _get_subprocess_env() -> dict:
53
  nvidia_paths + [torch_lib] + extra + ([existing_ld] if existing_ld else [])
54
  )
55
 
56
- # 2. CUDA-Allocator: expandable_segments:True braucht NVML, das im
57
- # ZeroGPU-Subprocess mit torch 2.11.0 zu CUDACachingAllocator-Assertions führt.
58
- # Im Subprocess stabilen Fallback verwenden.
59
- env["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:512"
60
 
61
  return env
62
 
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  def _log_cuda_diagnostics():
65
- """Loggt torch-Version, CUDA, libcudart-Pfad und pytorch3d beim Start."""
66
  import glob as _glob
67
  import diffusers
68
  print(f"[DIAG] diffusers version : {diffusers.__version__}", flush=True)
@@ -71,17 +101,13 @@ def _log_cuda_diagnostics():
71
  print(f"[DIAG] CUDA available : {torch.cuda.is_available()}", flush=True)
72
  print(f"[DIAG] sys.executable : {sys.executable}", flush=True)
73
  env = _get_subprocess_env()
74
- ld_path = env.get("LD_LIBRARY_PATH", "")
75
- print(f"[DIAG] LD_LIBRARY_PATH : {ld_path}", flush=True)
76
- found = []
77
- for d in ld_path.split(":"):
78
- if d:
79
- found.extend(_glob.glob(os.path.join(d, "libcudart.so*")))
80
  print(f"[DIAG] libcudart found : {found}", flush=True)
81
  print(f"[DIAG] subprocess ALLOC : {env.get('PYTORCH_CUDA_ALLOC_CONF')}", flush=True)
 
82
  import importlib.util
83
- p3d = importlib.util.find_spec("pytorch3d")
84
- print(f"[DIAG] pytorch3d spec : {p3d}", flush=True)
85
 
86
  _log_cuda_diagnostics()
87
 
 
1
+ import os
2
+ # PYTORCH_CUDA_ALLOC_CONF MUSS vor torch-Import stehen (GPT-Fix)
3
+ os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
4
+
5
  import spaces
6
  import sys
7
  import torch
 
 
8
  import shutil
9
  import tempfile
10
  import gradio as gr
 
20
 
21
 
22
  def _get_subprocess_env() -> dict:
23
+ """Subprocess-Umgebung mit CUDA-Library-Pfaden und NVML-freiem Allocator.
24
 
25
+ backend:cudaMallocAsync ersetzt CUDACachingAllocator komplett –
26
+ kein NVML nötig, stabil auf ZeroGPU Blackwell + torch 2.11.0 + CUDA 13.
 
 
 
27
  """
28
  import site
29
  import glob as _glob
30
 
31
  env = os.environ.copy()
32
 
33
+ # CUDA-Library-Pfade für libcudart
34
  nvidia_paths: list[str] = []
35
  all_site = site.getsitepackages()
36
  try:
 
52
  nvidia_paths + [torch_lib] + extra + ([existing_ld] if existing_ld else [])
53
  )
54
 
55
+ # NVML-freier Allocator für Subprocess (CUDACachingAllocator-Assertions vermeiden)
56
+ env["PYTORCH_CUDA_ALLOC_CONF"] = "backend:cudaMallocAsync"
 
 
57
 
58
  return env
59
 
60
 
61
+ def _validate_xformers() -> bool:
62
+ """Prüft ob xformers.ops.memory_efficient_attention wirklich funktioniert.
63
+ Gibt True zurück wenn ein Mini-Test mit echten CUDA-Tensoren besteht.
64
+ """
65
+ try:
66
+ import xformers
67
+ import xformers.ops
68
+ print(f"[DIAG] xformers version : {xformers.__version__}", flush=True)
69
+
70
+ has_mea = hasattr(xformers.ops, 'memory_efficient_attention')
71
+ print(f"[DIAG] xformers MEA attr : {has_mea}", flush=True)
72
+
73
+ if not has_mea:
74
+ print("[DIAG] xformers MEA test : SKIP (attr fehlt)", flush=True)
75
+ return False
76
+
77
+ if not torch.cuda.is_available():
78
+ print("[DIAG] xformers MEA test : SKIP (kein CUDA)", flush=True)
79
+ return False
80
+
81
+ # Mini-Test mit kleinen CUDA-Tensoren
82
+ q = torch.randn(2, 16, 64, device="cuda", dtype=torch.float16)
83
+ k = torch.randn(2, 16, 64, device="cuda", dtype=torch.float16)
84
+ v = torch.randn(2, 16, 64, device="cuda", dtype=torch.float16)
85
+ _ = xformers.ops.memory_efficient_attention(q, k, v)
86
+ torch.cuda.synchronize()
87
+ print("[DIAG] xformers MEA test : PASS ✅", flush=True)
88
+ return True
89
+
90
+ except Exception as e:
91
+ print(f"[DIAG] xformers MEA test : FAIL – {e}", flush=True)
92
+ return False
93
+
94
+
95
  def _log_cuda_diagnostics():
 
96
  import glob as _glob
97
  import diffusers
98
  print(f"[DIAG] diffusers version : {diffusers.__version__}", flush=True)
 
101
  print(f"[DIAG] CUDA available : {torch.cuda.is_available()}", flush=True)
102
  print(f"[DIAG] sys.executable : {sys.executable}", flush=True)
103
  env = _get_subprocess_env()
104
+ ld = env.get("LD_LIBRARY_PATH", "")
105
+ found = [h for d in ld.split(":") if d for h in _glob.glob(os.path.join(d, "libcudart.so*"))]
 
 
 
 
106
  print(f"[DIAG] libcudart found : {found}", flush=True)
107
  print(f"[DIAG] subprocess ALLOC : {env.get('PYTORCH_CUDA_ALLOC_CONF')}", flush=True)
108
+ _validate_xformers()
109
  import importlib.util
110
+ print(f"[DIAG] pytorch3d spec : {importlib.util.find_spec('pytorch3d')}", flush=True)
 
111
 
112
  _log_cuda_diagnostics()
113