Den Flaschenhals profilieren
Finden Sie heraus, wo Zeit und Speicher verbraucht werden.
Den Flaschenhals profilieren ist eine kostenlose Deep Learning Academy-Lektion auf CoddyKit. Dies ist Lektion 3 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.
Why Profile First
Before optimizing, find out where time actually goes. Guessing wastes effort, while a quick profile shows you the real slow spots.
Two Common Bottlenecks
Training usually stalls in one of two places: the GPU compute doing math, or the data pipeline feeding it. Knowing which one matters.
Time It Crudely First
Start simple by timing a loop section with the clock. A rough perf_counter reading often points you to the right area in seconds.
import time
t = time.perf_counter()
# run one batch
print(time.perf_counter() - t)GPU Work Is Async
CUDA runs in the background, so naive timers lie. Call synchronize first to make sure the GPU has truly finished before you read the clock.
torch.cuda.synchronize()The Built-In Profiler
For real detail, use the torch.profiler context manager. It records how long every operation takes on both CPU and GPU.
from torch.profiler import profileWrap the Code to Profile
Run the part you care about inside a profile block. Choosing both CPU and CUDA activities captures the whole picture.
with profile(activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA]) as prof:
model(x)Read the Table
Print results sorted by cost to see the heaviest ops at the top. The key_averages table groups identical operations together.
print(prof.key_averages().table(sort_by='cuda_time_total'))Spot a Data Bottleneck
If the GPU often sits idle waiting, your DataLoader is too slow. More workers or cached data usually fixes that gap.
Spot a Compute Bottleneck
If one matmul or conv dominates the table, the limit is raw compute. Mixed precision or a smaller model is the lever to pull.
Watch Memory Too
The profiler can also report peak memory. Tracking profile_memory reveals which layers eat the most, guiding what to trim.
with profile(profile_memory=True) as prof:
model(x)Measure, Change, Re-Measure
Optimization is a loop: profile, make one change, then profile again. Trust numbers, not hunches, to confirm a fix actually helped.
Quick Check
Your GPU often sits idle between batches. What is the likely bottleneck?
Recap
Profile before you tune: synchronize for honest timings, use torch.profiler to find the heaviest ops, then fix data or compute and measure again. 🔍
Häufig gestellte Fragen
Ist die Lektion „Den Flaschenhals profilieren“ kostenlos?
Ja — der vollständige Text von „Den Flaschenhals profilieren“ 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 „Den Flaschenhals profilieren“?
Finden Sie heraus, wo Zeit und Speicher verbraucht werden. 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 3 von 4.
Wie lange dauert die Lektion „Den Flaschenhals profilieren“?
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
- Gemischte Präzision mit autocast und GradScaler
- Gradientenakkumulation für große Batches
- Den Flaschenhals profilieren
- GPU-Speicherverbrauch senken