What this video shows
About this walkthrough
The code uses the GPU, but it hands over one row at a time. This walkthrough replaces the loop with one batch transfer and a matrix multiplication, then reads the profile. In the judged Trace range, 128 transfers of 4 KiB become one 512 KiB transfer, and 128 kernel launches become 2. The unprofiled whole-harness time goes from 1.563 s to 1.473 s, about 5.8% lower, so fewer transfers and launches do not bring the same proportional speedup.
Read along
Transcript
Select a time to play the video from that chapter.
0:00Learn PyTorch Batching
Learn PyTorch batching with GPUFlight. We'll change a row-by-row loop, then compare the timeline, transfers, kernel launches, and elapsed time.
0:11On the GPU, one row at a time
This PyTorch code uses the GPU. But it sends one row at a time, then asks for one small matrix operation, over and over. Let's follow that loop into GPUFlight's timeline, and change how we deliver the work.
0:28The task: score a batch of inputs
Open Stage the Batch in GPUFlight Performance Lab. The task is to multiply input data by a weight matrix and return the same output scores. Each input row is one example. The weight matrix is already on the GPU, while the input batch starts on the CPU. This is the core calculation of a linear layer, simplified so we can focus on transfers and batching.
0:56Read the Python loop
The starter allocates an output tensor. Inside the loop, dot to moves one row to the GPU. The at sign performs matrix multiplication, and we store that row's output. A host-to-device transfer means CPU to GPU. A kernel launch asks the GPU to execute a function. A single Python expression can launch more than one kernel. We will count what actually happens.
1:25Run and inspect the baseline
Run the unchanged starter. The output is correct, but the lab reports 128 host-to-device transfers. Open View session details, then Timeline. In Ranges, select judged: the interval used to evaluate our function. This single Trace range contains 128 kernel launches. Setup and warmup also appear elsewhere in the full capture.
1:53Explore the baseline timeline
Switch to Tracks and zoom into the judged interval. Blue marks are input transfers, purple marks are device-to-device copies, and green marks show kernel execution. Select a blue mark. This individual transfer is 4 KiB: one input row. The repeated pattern reflects the Python loop. The trace shows small copies and matrix operations being issued repeatedly.
2:21Change one: transfer the batch
First, move the transfer before the loop. Data dot to, weight dot device, copies the complete input batch to the device holding the weights. Inside the loop, read each row from this device tensor. The data is already there, so we no longer request a new transfer for every row. This intermediate edit still performs matrix multiplication row by row. Next, we will batch that work too.
2:51Change two: multiply all rows together
Replace the output allocation and the loop with data GPU, at, weight. Matrix multiplication handles all the input rows together and produces the full output tensor. The input bytes and the mathematical task stay the same. We combine the transfers and give the matrix library a larger operation. Different execution paths can round floating-point values differently, so we still need the correctness check.
3:21Run and verify the batched version
Run the batched function on the same GPU. Correctness passes, and the input-transfer count falls from 128 to one. The result summary reports kernel launches falling from 256 to four. Keep that summary separate from the single Trace range, which we inspect next.
3:43Compare the execution pattern
The edited timeline now shows one larger input copy. Select it: 512 KiB, the same total input as 128 4 KiB rows. In Ranges, judged contains two kernels instead of 128. Here, the matrix library uses a matrix kernel and a second kernel to combine partial results. One matrix expression does not guarantee one kernel. That is why we inspect what actually ran.
4:16Compare elapsed time and scope
Back in the lab, click Compare. Unprofiled whole-harness runtime changes from 1.563 to 1.473 seconds, about six percent lower in this pair. The profiled judged range changes from 68.04 to 40.70 milliseconds. That range includes harness overhead; it is not isolated matrix-kernel time. These are different timing scopes. The clear result is fewer transfers and launches. Repeat timing trials before treating a small elapsed-time change as conclusive.
4:57Apply the pattern to your PyTorch code
Look for small device transfers and independent tensor operations inside Python loops. If the batch is already available and fits in memory, consider moving it together and using a batch operation. Larger batches can use more device memory. Waiting to assemble a batch can also increase request latency. Choose the batch size for your application, then measure.
5:24Follow Python into the GPU timeline
GPUFlight lets us connect a Python code change to the transfers and kernels it creates. We check the answer, inspect the timeline, and compare measurements with the same scope. Try Stage the Batch in GPUFlight Performance Lab. The link is in the description. Thanks for watching!
Measure it yourself
Run the same experiment on a real GPU
Edit the kernel in your browser, run it on managed hardware, and read the profile the video walks through.
Run the Stage the Batch lab