Journaliser les replis CPU du GPU et retirer une copie VRAM inutile

This commit is contained in:
Antoine Jacquin
2026-09-18 21:23:39 +02:00
parent 93d1656d9f
commit f3708f8449
2 changed files with 14 additions and 6 deletions

View File

@ -355,7 +355,10 @@ def to_gpu(arr):
if _gpu_available(): if _gpu_available():
try: try:
return _cp.asarray(arr.astype(np.float32)) return _cp.asarray(arr.astype(np.float32))
except Exception: except Exception as e:
# L'erreur d'origine (souvent OOM) ne doit pas être avalée :
# sans elle, un repli CPU est indissociable d'un simple bug.
logger.warning(f"Transfert GPU échoué ({e}) — repli CPU pour ce calcul")
disable_gpu() disable_gpu()
return arr.astype(np.float32) return arr.astype(np.float32)
@ -386,7 +389,8 @@ def xp_gaussian_filter(arr, sigma):
if _cp is not None and isinstance(arr, _cp.ndarray): if _cp is not None and isinstance(arr, _cp.ndarray):
try: try:
return _cp_ndimage.gaussian_filter(arr, sigma) return _cp_ndimage.gaussian_filter(arr, sigma)
except Exception: except Exception as e:
logger.warning(f"Filtre gaussien GPU échoué ({e}) — repli CPU")
arr = to_cpu(arr) arr = to_cpu(arr)
return ndimage.gaussian_filter(arr, sigma) return ndimage.gaussian_filter(arr, sigma)
@ -395,7 +399,8 @@ def xp_uniform_filter(arr, size):
if _cp is not None and isinstance(arr, _cp.ndarray): if _cp is not None and isinstance(arr, _cp.ndarray):
try: try:
return _cp_ndimage.uniform_filter(arr, size) return _cp_ndimage.uniform_filter(arr, size)
except Exception: except Exception as e:
logger.warning(f"Filtre uniform GPU échoué ({e}) — repli CPU")
arr = to_cpu(arr) arr = to_cpu(arr)
return ndimage.uniform_filter(arr, size) return ndimage.uniform_filter(arr, size)
@ -404,7 +409,8 @@ def xp_minimum_filter(arr, footprint=None, size=None):
if _cp is not None and isinstance(arr, _cp.ndarray): if _cp is not None and isinstance(arr, _cp.ndarray):
try: try:
return _cp_ndimage.minimum_filter(arr, footprint=footprint, size=size) return _cp_ndimage.minimum_filter(arr, footprint=footprint, size=size)
except Exception: except Exception as e:
logger.warning(f"Filtre minimum GPU échoué ({e}) — repli CPU")
arr = to_cpu(arr) arr = to_cpu(arr)
return ndimage.minimum_filter(arr, footprint=footprint, size=size) return ndimage.minimum_filter(arr, footprint=footprint, size=size)
@ -413,7 +419,8 @@ def xp_maximum_filter(arr, footprint=None, size=None):
if _cp is not None and isinstance(arr, _cp.ndarray): if _cp is not None and isinstance(arr, _cp.ndarray):
try: try:
return _cp_ndimage.maximum_filter(arr, footprint=footprint, size=size) return _cp_ndimage.maximum_filter(arr, footprint=footprint, size=size)
except Exception: except Exception as e:
logger.warning(f"Filtre maximum GPU échoué ({e}) — repli CPU")
arr = to_cpu(arr) arr = to_cpu(arr)
return ndimage.maximum_filter(arr, footprint=footprint, size=size) return ndimage.maximum_filter(arr, footprint=footprint, size=size)

View File

@ -460,7 +460,8 @@ def generate_hillshade(dem_file, basename, vis_dir, resolution, shared=None):
if shared: if shared:
transform = shared.transform transform = shared.transform
crs = shared.crs crs = shared.crs
dem = to_gpu(shared.dem_np) # Pas de copie GPU du DEM brut ici : seuls gradient/pente/aspect
# servent (déjà partagés) — ~100 Mo de VRAM économisés par dalle
dy = to_gpu(shared.dy) if _gpu_mod.HAS_GPU else shared.dy dy = to_gpu(shared.dy) if _gpu_mod.HAS_GPU else shared.dy
dx = to_gpu(shared.dx) if _gpu_mod.HAS_GPU else shared.dx dx = to_gpu(shared.dx) if _gpu_mod.HAS_GPU else shared.dx
slope = to_gpu(shared.slope_rad) if _gpu_mod.HAS_GPU else shared.slope_rad slope = to_gpu(shared.slope_rad) if _gpu_mod.HAS_GPU else shared.slope_rad