0Pricing
Deep Learning Academy · บทเรียน

วิเคราะห์คอขวด

ค้นหาว่าเวลาและหน่วยความจำถูกใช้ไปที่ใด

วิเคราะห์คอขวด เป็นบทเรียน Deep Learning Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Deep Learning Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Deep Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

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 profile

Wrap 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. 🔍

คำถามที่พบบ่อย

บทเรียน “วิเคราะห์คอขวด” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “วิเคราะห์คอขวด” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Deep Learning Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Deep Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “วิเคราะห์คอขวด”

ค้นหาว่าเวลาและหน่วยความจำถูกใช้ไปที่ใด คุณปฏิบัติ Deep Learning Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Deep Learning Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Deep Learning Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “วิเคราะห์คอขวด” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Deep Learning Academy นี้ได้ไหม

ได้ บทเรียน Deep Learning Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ความแม่นยำผสมด้วย autocast และ GradScaler
  2. สะสมเกรเดียนต์สำหรับแบตช์ขนาดใหญ่
  3. วิเคราะห์คอขวด
  4. ลดการใช้หน่วยความจำของ GPU
← กลับไปที่ Deep Learning Academy