0Pricing
Deep Learning Academy · Lektion

GPU-Speicherverbrauch senken

Checkpointing und ein intelligenterer Umgang mit Tensoren.

GPU-Speicherverbrauch senken ist eine kostenlose Deep Learning Academy-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Deep Learning Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Deep Learning Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

The Dreaded OOM

Run out of GPU memory and training crashes with an out-of-memory error. The good news is several simple tactics free up space fast.

Where Memory Goes

Your GPU holds the model weights, the gradients, the optimizer state, and the activations saved for backward. Activations are often the biggest.

Shrink the Batch

The fastest fix is a smaller batch size. Fewer samples per step means fewer activations to store, and accumulation can recover the effective size.

No Grad for Inference

During evaluation you do not need gradients. Wrapping inference in torch.no_grad skips storing activations and saves a lot of memory.

with torch.no_grad():
    preds = model(x)

Mixed Precision Helps Here Too

Half-precision tensors are simply smaller. Turning on autocast cuts activation and weight memory roughly in half during training.

with torch.autocast(device_type='cuda'):
    out = model(x)

Gradient Checkpointing

Checkpointing trades compute for memory: it drops most activations and recomputes them during backward instead of keeping them all.

from torch.utils.checkpoint import checkpoint

Apply Checkpointing

Wrap a heavy block in checkpoint so its activations are rebuilt on the backward pass. You save memory at the cost of extra recompute.

out = checkpoint(heavy_block, x)

Detach What You Log

Keeping a loss tensor around holds its whole graph in memory. Call .item() to log just the number and let the graph be freed.

running_loss += loss.item()

Use set_to_none

Pass set_to_none to zero_grad so gradient tensors are released instead of merely filled with zeros, freeing their memory between steps.

optimizer.zero_grad(set_to_none=True)

Clear the Cache

PyTorch caches freed blocks for reuse. When you truly need space back, empty_cache returns it to the GPU, though it rarely fixes real leaks.

torch.cuda.empty_cache()

Inspect Your Usage

Check how much you hold with memory_allocated. Watching this number while you tune confirms which change actually freed space.

print(torch.cuda.memory_allocated())

Quick Check

Which technique saves memory by recomputing activations during backward?

Recap

Beat out-of-memory by shrinking batches, using no_grad for inference, autocast, gradient checkpointing, and set_to_none. Measure with memory_allocated. 🧹

Häufig gestellte Fragen

Ist die Lektion „GPU-Speicherverbrauch senken“ kostenlos?

Ja — der vollständige Text von „GPU-Speicherverbrauch senken“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Deep Learning Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Deep Learning Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „GPU-Speicherverbrauch senken“?

Checkpointing und ein intelligenterer Umgang mit Tensoren. Du übst Deep Learning Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Deep Learning Academy zu starten?

Keine Vorkenntnisse erforderlich. Deep Learning Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „GPU-Speicherverbrauch senken“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Deep Learning Academy-Lektion Code schreiben und ausführen?

Ja. Jede Deep Learning Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Gemischte Präzision mit autocast und GradScaler
  2. Gradientenakkumulation für große Batches
  3. Den Flaschenhals profilieren
  4. GPU-Speicherverbrauch senken
← Zurück zu Deep Learning Academy