File size: 819 Bytes
c8fa89c
 
094008d
 
c8fa89c
 
 
 
 
094008d
c8fa89c
 
094008d
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import os
import sys
import gc
import torch

# --- Centralized Debugging Control ---
DEBUG_ENABLED = os.environ.get("CMP_DEBUG", "0") == "1"

def dbg(*args, **kwargs):
    """A controlled debug print function."""
    if DEBUG_ENABLED:
        print("[DEBUG]", *args, **kwargs, file=sys.stderr, flush=True)

# --- NEU: Zentrale Funktion zur Speicherbereinigung ---
def cleanup_memory():
    """
    Eine zentrale, global verfügbare Funktion zum Aufräumen von CPU- und GPU-Speicher.
    Dies stellt sicher, dass die Speicherverwaltung konsistent und an einer einzigen Stelle erfolgt.
    """
    dbg("Cleaning up memory (centralized)...")
    # Python's garbage collector
    gc.collect()
    # PyTorch's CUDA cache
    if torch.cuda.is_available():
        torch.cuda.empty_cache()
    dbg("Memory cleanup complete.")