CPU vs GPU vs MPS: Pick a Device
Detect CUDA, Apple MPS, or fall back to CPU.
CPU vs GPU vs MPS: Pick a Device is a free Deep Learning Academy lesson on CoddyKit — lesson 2 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.
Where Math Happens
Every tensor lives on a device: the CPU, a GPU, or Apple's MPS. The device decides how fast your model trains. ⚡
The CPU Always Works
Your CPU is the safe default. It runs every PyTorch operation, just more slowly on the heavy matrix math that big networks demand.
The GPU Goes Fast
A GPU does thousands of multiplications in parallel, making it far faster for deep learning. PyTorch reaches NVIDIA GPUs through CUDA.
Apple Silicon Has MPS
On a Mac with Apple Silicon, MPS taps the built-in GPU. It is the fast path for M-series chips without any NVIDIA hardware.
Detect CUDA
Ask PyTorch whether an NVIDIA GPU is available before you use one. cuda.is_available returns True or False so you never assume.
torch.cuda.is_available()Detect MPS
On a Mac, check the Apple backend the same way. mps.is_available tells you if the M-series GPU is ready to use.
torch.backends.mps.is_available()Pick the Best Device
A clean pattern tries CUDA first, then MPS, then falls back to CPU. This device string adapts to whatever machine runs your code.
device = 'cuda' if torch.cuda.is_available() else 'cpu'Move a Tensor
Send data to the chosen device with .to(device). The tensor's numbers are unchanged, but its math now runs there.
x = torch.tensor([1.0, 2.0]).to(device)Keep Everything Together
Your model and your data must share one device. Mixing CPU and GPU tensors throws a device mismatch error during the forward pass.
Check Where a Tensor Lives
Not sure where data sits? Read the .device attribute. It prints cpu, cuda, or mps so you can confirm placement at a glance.
print(x.device)When to Use Which
Use the GPU for real training, and the CPU for quick tests or tiny data. The fastest device is the one your hardware actually has.
Quick Check
Pick the right call for a Mac with Apple Silicon.
Recap
You learned to detect CUDA, MPS, or CPU, pick the best one, and move tensors there. Same code, fastest hardware available. 🚀
Frequently asked questions
Is the “CPU vs GPU vs MPS: Pick a Device” lesson free?
Yes — the full text of “CPU vs GPU vs MPS: Pick a Device” 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 “CPU vs GPU vs MPS: Pick a Device”?
Detect CUDA, Apple MPS, or fall back to CPU. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “CPU vs GPU vs MPS: Pick a Device” 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
- Install PyTorch and Verify It Imports
- CPU vs GPU vs MPS: Pick a Device
- Notebooks, Scripts & Reproducible Seeds
- Your First torch.tensor