CUDA advanced
Your code runs on the GPU. Is it fast, and can you find out why not? With Server AI Hub, you watch GPU use, power, temperature and clock while your job runs.
Chapters
- 0:00Introduction
- 0:06With Server AI Hub: Why it's slow
- 0:14What you gain: The benefits
- 0:21The alternative: Inside the code
- 0:29Where this fits: The AI stack
- 0:33First: Measure it
- 0:43Look inside: Run a profile
- 0:58Waste less: Fewer copies
- 1:09Overlap: Streams
- 1:20Smaller numbers: Mixed precision
- 1:28Memory: Who's holding it?
- 1:41When it breaks: Find the real line
- 1:54Recap: Faster, sturdier
- 1:59Wrap up
Read the transcript
Introduction
Your code runs on the GPU. Is it fast, and can you find out why not?
With Server AI Hub: Why it's slow
With Server AI Hub, you watch GPU use, power, temperature and clock while your job runs.
What you gain: The benefits
Throttle events show when the chip slowed itself down. And with two Sparks, you compare them side by side.
The alternative: Inside the code
The other way is by hand: timers, a profiler and a Terminal. That's also how you see inside your own code.
Where this fits: The AI stack
Near the bottom of the stack again.
First: Measure it
Measure before you change anything. GPU work runs on after the launch returns, so synchronize before you start the clock, and again before you stop it.
torch.cuda.synchronize()
start = time.time()
y = model(x)
torch.cuda.synchronize()
print(time.time() - start)Look inside: Run a profile
NVIDIA's Nsight Systems can profile a run. In a Terminal, put N sys profile in front of your command. Its report shows kernels and memory copies over time. job.py stands for your own script.
nsys profile --stats=true python3 job.pyWaste less: Fewer copies
Keep data on the GPU between steps. Don't pull a result back to the processor unless you need it. Pinned memory can let a copy run without making the program wait.
Overlap: Streams
A stream is a queue of GPU work that runs in order. Work in different streams may overlap. If one needs another's result, you must synchronize them.
Smaller numbers: Mixed precision
Mixed precision runs suitable steps with smaller numbers. It can save time and memory. Check your results still hold up.
with torch.autocast("cuda",
dtype=torch.bfloat16):
y = model(x)Memory: Who's holding it?
PyTorch keeps memory it has freed, ready to use again. Allocated is what your tensors use. Reserved is what PyTorch holds. On a Spark that memory is shared, so leave room for the system.
torch.cuda.memory_allocated()
torch.cuda.memory_reserved()When it breaks: Find the real line
A GPU error can be reported late, pointing at the wrong line. Run once with CUDA LAUNCH BLOCKING set to one, so each launch waits. It slows things down, so use it only to debug.
CUDA_LAUNCH_BLOCKING=1 python3 job.pyRecap: Faster, sturdier
Measure, profile, then change one thing at a time.
Wrap up
Tune your code by hand, and let Server AI Hub keep watch on the hardware: use, power, heat and throttle events, on both of your Sparks.
Sources
- docs.pytorch.org/docs/2.15/notes/cuda.html
- docs.pytorch.org/docs/2.15/amp.html
- docs.nvidia.com/nsight-systems/UserGuide/index.html
- docs.nvidia.com/dgx/dgx-spark/known-issues.html
More lessons
- How a GPU works 2:31
- CUDA and the software stack 1:36
- CUDA intermediate 2:04
- Docker basics 3:24
- Docker intermediate 2:26
- Docker advanced 2:44