Pick a depth. Each prompt opens in your AI pre-loaded with the lesson. Click a row to preview the prompt.
Latency is how long one unit of work takes to finish; throughput is how much work finishes per unit of time across many units running together. Batching improves throughput because fixed overheads (kernel launches, memory reads that get reused across the batch) amortize over more work, but it necessarily raises the latency of any single item, because that item now waits for the rest of the batch to be ready. This tension is not a bug to be engineered away — it's a real dial you set deliberately. A training job cares almost entirely about throughput (bigger batches, more GPU-seconds of useful work per wall-clock second), while an interactive inference endpoint has a latency SLA it cannot violate no matter how good the throughput number looks. Learning to read this tradeoff as a curve, not a single number, is what lets you pick a batch size on purpose instead of copying whatever number a tutorial used.
The demo simulates a fixed per-batch overhead plus a per-item cost and sweeps batch size, printing both the average per-item latency and the aggregate throughput — the same curve shape you'll see again with real inference servers in Module 9.
fixed_overhead_ms to 20 (simulating a slower launch/dispatch path) and see how much larger a batch you need before amortized latency looks reasonable.max_latency_ms SLA parameter and find, by trial, the largest batch size that keeps worst_case_wait_ms under it.worst_case_wait_ms instead.def simulate(batch_size, fixed_overhead_ms=5.0, per_item_ms=1.2):
batch_time_ms = fixed_overhead_ms + per_item_ms * batch_size
per_item_latency_ms = batch_time_ms / batch_size # amortized, not wait time
# a request arriving right after the batch closes waits almost the full batch_time
worst_case_wait_ms = batch_time_ms
throughput_items_per_s = batch_size / (batch_time_ms / 1000)
return per_item_latency_ms, worst_case_wait_ms, throughput_items_per_s
for bs in (1, 2, 4, 8, 16, 32):
lat, worst, thr = simulate(bs)
print(f"batch={bs:3d} amortized_latency={lat:6.2f}ms worst_case_wait={worst:7.2f}ms throughput={thr:8.1f}/s")python3 main.py