Mixed Precision with autocast & GradScaler
Half-precision math for big speedups.
Mixed Precision with autocast & GradScaler is a free Deep Learning Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Deep Learning Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Mixed Precision Means
By default PyTorch does math in 32-bit floats. Mixed precision runs many operations in 16-bit instead, which is faster and uses far less memory.
Why Half Precision Is Faster
Modern GPUs have special tensor cores tuned for 16-bit math. Feeding them half-precision data can speed up training two or three times with little accuracy loss.
The Catch With 16-Bit
Half precision has a tiny range, so very small gradient values can round down to zero. That silent underflow stalls learning if you do nothing about it.
Meet autocast
Wrap your forward pass in autocast and PyTorch picks a safe precision per operation automatically. You never cast tensors by hand.
with torch.autocast(device_type='cuda'):
out = model(x)What autocast Wraps
Put only the forward pass and loss inside autocast. The backward call stays outside the block, where PyTorch handles precision for you.
with torch.autocast(device_type='cuda'):
out = model(x)
loss = loss_fn(out, y)Meet GradScaler
A GradScaler fixes underflow by multiplying the loss by a large factor before backward, so small gradients stay big enough to survive in 16-bit.
scaler = torch.cuda.amp.GradScaler()Scale the Loss
Instead of loss.backward(), call scaler.scale(loss).backward(). The scaler inflates the loss first so the resulting gradients do not vanish.
scaler.scale(loss).backward()Step Through the Scaler
Run the optimizer with scaler.step, which quietly unscales the gradients back to normal size before applying the update.
scaler.step(optimizer)Update the Scale Factor
Finish each step with scaler.update(). It grows the scale when things are stable and shrinks it if it ever spots an overflow.
scaler.update()The Full AMP Step
Together these calls form one clean AMP iteration: zero grads, autocast forward, scaled backward, scaler step, then update.
optimizer.zero_grad()
with torch.autocast(device_type='cuda'):
loss = loss_fn(model(x), y)
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()When To Reach For It
Mixed precision shines on a recent NVIDIA GPU with large batches. On a plain CPU it gives little benefit, so save it for real training runs.
Quick Check
You enabled autocast but tiny gradients keep vanishing. What tool fixes that?
Recap
Wrap the forward in autocast for fast 16-bit math, then use a GradScaler to scale, step, and update so tiny gradients survive. ⚡
Frequently asked questions
Is the “Mixed Precision with autocast & GradScaler” lesson free?
Yes — the full text of “Mixed Precision with autocast & GradScaler” is free to read here on the web, and the Deep Learning Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Deep Learning Academy course, upgrade to CoddyKit PRO.
What will I learn in “Mixed Precision with autocast & GradScaler”?
Half-precision math for big speedups. You practise Deep Learning Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Deep Learning Academy?
No prior experience is required. Deep Learning Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Mixed Precision with autocast & GradScaler” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Deep Learning Academy lesson?
Yes. Every Deep Learning Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Mixed Precision with autocast & GradScaler
- Gradient Accumulation for Big Batches
- Profile the Bottleneck
- Cut GPU Memory Usage