Pream912 commited on
Commit
1ce9771
Β·
verified Β·
1 Parent(s): aed5eeb

Update wall_pipeline.py

Browse files
Files changed (1) hide show
  1. wall_pipeline.py +48 -15
wall_pipeline.py CHANGED
@@ -42,38 +42,71 @@ warnings.filterwarnings("ignore", category=UserWarning)
42
  # ── PyTorch / CUDA ────────────────────────────────────────────────────────────
43
  try:
44
  import torch
45
- _TORCH = True
46
- _TORCH_CUDA = torch.cuda.is_available()
47
- _DEVICE = torch.device("cuda" if _TORCH_CUDA else "cpu")
48
- if _TORCH_CUDA:
49
- print(f"[GPU] PyTorch CUDA OK device={torch.cuda.get_device_name(0)}")
50
- else:
51
- print("[GPU] PyTorch: CUDA not found β€” CPU tensors")
 
 
 
 
 
 
 
52
  except ImportError:
53
  _TORCH = _TORCH_CUDA = False
54
  _DEVICE = None
55
- print("[GPU] PyTorch not installed")
56
 
57
  # ── CuPy ─────────────────────────────────────────────────────────────────────
 
 
 
58
  try:
59
- import cupy as cp
 
 
 
 
 
 
60
  import cupyx.scipy.ndimage as cpnd
61
  _CUPY = True
62
  print(f"[GPU] CuPy OK version={cp.__version__}")
63
  except ImportError:
64
- cp = np # type: ignore[assignment]
65
- cpnd = None # type: ignore[assignment]
66
  _CUPY = False
67
  print("[GPU] CuPy not installed β€” NumPy fallback")
 
 
 
 
 
 
 
68
 
69
  # ── OpenCV CUDA ───────────────────────────────────────────────────────────────
70
  _CV_CUDA = False
71
  try:
72
- _CV_CUDA = cv2.cuda.getCudaEnabledDeviceCount() > 0
73
- print(f"[GPU] OpenCV CUDA {'OK' if _CV_CUDA else 'NO'}"
74
- f" devices={cv2.cuda.getCudaEnabledDeviceCount()}")
 
 
 
 
 
 
 
75
  except AttributeError:
76
- print("[GPU] OpenCV CUDA module absent")
 
 
 
77
 
78
  # ── scikit-image skeleton ─────────────────────────────────────────────────────
79
  try:
 
42
  # ── PyTorch / CUDA ────────────────────────────────────────────────────────────
43
  try:
44
  import torch
45
+ _TORCH = True
46
+ try:
47
+ # cuda.is_available() can itself raise if the driver is too old
48
+ _TORCH_CUDA = torch.cuda.is_available()
49
+ if _TORCH_CUDA:
50
+ # Force an actual CUDA context to catch driver-version mismatches
51
+ torch.zeros(1, device="cuda")
52
+ print(f"[GPU] PyTorch CUDA OK device={torch.cuda.get_device_name(0)}")
53
+ else:
54
+ print("[GPU] PyTorch: CUDA not available β€” CPU tensors")
55
+ except Exception as _te:
56
+ _TORCH_CUDA = False
57
+ print(f"[GPU] PyTorch CUDA DISABLED ({type(_te).__name__}: {_te})")
58
+ _DEVICE = torch.device("cuda" if _TORCH_CUDA else "cpu")
59
  except ImportError:
60
  _TORCH = _TORCH_CUDA = False
61
  _DEVICE = None
62
+ print("[GPU] PyTorch not installed β€” CPU fallback")
63
 
64
  # ── CuPy ─────────────────────────────────────────────────────────────────────
65
+ # Import alone is not enough β€” cupy is lazily linked to libcuda.so and will
66
+ # only raise CUDARuntimeError when the *first allocation* is attempted.
67
+ # We probe with a tiny 1-element array and catch every possible CUDA error.
68
  try:
69
+ import cupy as _cp_test
70
+ import cupyx.scipy.ndimage as _cpnd_test
71
+ # Probe: force an actual CUDA context + allocation
72
+ _probe = _cp_test.zeros(1, dtype=_cp_test.uint8)
73
+ del _probe
74
+ # If we get here the driver is compatible
75
+ import cupy as cp # re-bind to public name
76
  import cupyx.scipy.ndimage as cpnd
77
  _CUPY = True
78
  print(f"[GPU] CuPy OK version={cp.__version__}")
79
  except ImportError:
80
+ cp = np # type: ignore[assignment]
81
+ cpnd = None # type: ignore[assignment]
82
  _CUPY = False
83
  print("[GPU] CuPy not installed β€” NumPy fallback")
84
+ except Exception as _ce:
85
+ # Catches CUDARuntimeError (driver too old), CUDADriverError, etc.
86
+ cp = np # type: ignore[assignment]
87
+ cpnd = None # type: ignore[assignment]
88
+ _CUPY = False
89
+ print(f"[GPU] CuPy DISABLED ({type(_ce).__name__}: {_ce})")
90
+ print("[GPU] CuPy β†’ NumPy fallback (all operations remain correct)")
91
 
92
  # ── OpenCV CUDA ───────────────────────────────────────────────────────────────
93
  _CV_CUDA = False
94
  try:
95
+ n_dev = cv2.cuda.getCudaEnabledDeviceCount()
96
+ if n_dev > 0:
97
+ # Probe with a tiny upload to catch driver mismatches
98
+ _probe_mat = cv2.cuda_GpuMat()
99
+ _probe_mat.upload(np.zeros((2, 2), np.uint8))
100
+ del _probe_mat
101
+ _CV_CUDA = True
102
+ print(f"[GPU] OpenCV CUDA OK devices={n_dev}")
103
+ else:
104
+ print("[GPU] OpenCV CUDA NO (no CUDA-enabled devices)")
105
  except AttributeError:
106
+ print("[GPU] OpenCV CUDA module absent β€” CPU morphology")
107
+ except Exception as _oce:
108
+ _CV_CUDA = False
109
+ print(f"[GPU] OpenCV CUDA DISABLED ({type(_oce).__name__}: {_oce})")
110
 
111
  # ── scikit-image skeleton ─────────────────────────────────────────────────────
112
  try: