fix: add Apple Silicon (MPS) device support across lessons#295
fix: add Apple Silicon (MPS) device support across lessons#295Jayantkhandebharad wants to merge 13 commits into
Conversation
verify.py probed only CUDA, so Apple Silicon machines reported no GPU despite a working MPS backend. Detect CUDA or MPS and label which is active. Refs rohitg00#294. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
gpu_check only detected CUDA and bailed on Macs. Detect MPS, run the benchmark on it, and note unified memory. Refs rohitg00#294. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
GPU memory and mixed-device demos were CUDA-only. Add an MPS branch using the torch.mps allocation APIs. Refs rohitg00#294. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Device selection ignored MPS, so the PyTorch intro ran on CPU on Apple Silicon. Add a cuda->mps->cpu helper. Refs rohitg00#294. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Training silently fell back to CPU on Apple Silicon. Add the cuda->mps->cpu device helper and use it. Refs rohitg00#294. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Training silently fell back to CPU on Apple Silicon. Add the cuda->mps->cpu device helper and use it. Refs rohitg00#294. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Training silently fell back to CPU on Apple Silicon. Add the cuda->mps->cpu device helper and use it. Refs rohitg00#294. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Training silently fell back to CPU on Apple Silicon. Add the cuda->mps->cpu device helper and use it. Refs rohitg00#294. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Training silently fell back to CPU on Apple Silicon. Add the cuda->mps->cpu device helper and use it. Refs rohitg00#294. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The demo skipped unless CUDA was present. Allow MPS for the pipeline placement, fp16 dtype, and generator. Refs rohitg00#294. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Only torch.cuda.synchronize was called, making MPS timings inaccurate. Add an MPS synchronize branch. Refs rohitg00#294. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Benchmark synchronized CUDA only. Add MPS synchronize for accurate per-stage timings. Refs rohitg00#294. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
AmpTrainState raised on any non cpu/cuda device, blocking MPS. Accept mps; GradScaler stays disabled since AMP loss scaling is CUDA-only. Refs rohitg00#294. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
📝 WalkthroughWalkthroughAdds Apple Silicon MPS backend support across 13 lesson script files. The changes introduce ChangesMPS Backend Support
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@phases/00-setup-and-tooling/12-debugging-and-profiling/code/debug_tools.py`:
- Line 238: Remove the f-string prefix from four print statements in
debug_tools.py that contain no variable interpolation, as these trigger Ruff
F541 lint errors. At line 238, convert the f-string in the print statement ("
After 10k x 10k tensor:") to a regular string by removing the f prefix. Apply
the same fix at lines 243, 252, and 257 where similar f-strings with no
interpolation exist. Each of these four locations should have the f prefix
removed from the string literal to satisfy the linter.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 33ae37c0-d44f-4e56-a2de-db2f6f8dc1dc
📒 Files selected for processing (13)
phases/00-setup-and-tooling/01-dev-environment/code/verify.pyphases/00-setup-and-tooling/03-gpu-setup-and-cloud/code/gpu_check.pyphases/00-setup-and-tooling/12-debugging-and-profiling/code/debug_tools.pyphases/03-deep-learning-core/11-intro-to-pytorch/code/pytorch_intro.pyphases/04-computer-vision/04-image-classification/code/main.pyphases/04-computer-vision/05-transfer-learning/code/main.pyphases/04-computer-vision/07-semantic-segmentation-unet/code/main.pyphases/04-computer-vision/09-image-generation-gans/code/main.pyphases/04-computer-vision/10-image-generation-diffusion/code/main.pyphases/04-computer-vision/11-stable-diffusion/code/main.pyphases/04-computer-vision/15-real-time-edge/code/main.pyphases/04-computer-vision/16-vision-pipeline-capstone/code/main.pyphases/19-capstone-projects/45-gradient-clipping-amp/code/main.py
| print(f" Cached: {torch.cuda.memory_reserved() / 1e6:.1f} MB") | ||
|
|
||
| large_tensor = torch.randn(10000, 10000, device="cuda") | ||
| print(f" After 10k x 10k tensor:") |
There was a problem hiding this comment.
Remove no-op f-strings to satisfy Ruff F541.
These four strings have no interpolation and trigger lint errors.
Suggested patch
- print(f" After 10k x 10k tensor:")
+ print(" After 10k x 10k tensor:")
...
- print(f" After cleanup:")
+ print(" After cleanup:")
...
- print(f" After 10k x 10k tensor:")
+ print(" After 10k x 10k tensor:")
...
- print(f" After cleanup:")
+ print(" After cleanup:")Also applies to: 243-243, 252-252, 257-257
🧰 Tools
🪛 Ruff (0.15.15)
[error] 238-238: f-string without any placeholders
Remove extraneous f prefix
(F541)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@phases/00-setup-and-tooling/12-debugging-and-profiling/code/debug_tools.py`
at line 238, Remove the f-string prefix from four print statements in
debug_tools.py that contain no variable interpolation, as these trigger Ruff
F541 lint errors. At line 238, convert the f-string in the print statement ("
After 10k x 10k tensor:") to a regular string by removing the f prefix. Apply
the same fix at lines 243, 252, and 257 where similar f-strings with no
interpolation exist. Each of these four locations should have the f prefix
removed from the string literal to satisfy the linter.
Source: Linters/SAST tools
Summary
Adds Apple Silicon (MPS) support to the GPU code across the curriculum. Device selection was CUDA-only (
"cuda" if torch.cuda.is_available() else "cpu"), so on M-series Macs lessons silently ran on CPU, reported "no GPU", or hard-errored — even thoughtorch.backends.mps.is_available()isTrue. Fixes the inconsistency reported in #294 (197 files already handlemps; these 13 didn't).What changed (13 lessons, one commit each)
get_device()helper (cuda → mps → cpu) and used it.verify.py,gpu_check.py,debug_tools.py): detect/benchmark/profile on MPS; the env check now passes on Apple Silicon.torch.mps.synchronize()for accurate timing.AmpTrainStatenow acceptsdevice_type="mps"(GradScaler stays disabled — AMP scaling is CUDA-only).Verification (on an Apple M4, torch 2.12.0)
gpu_check.pyandverify.pydetect and run on MPS;debug_tools.pyMPS memory demo works.45-gradient-clipping-ampunit tests: 17/17 pass.scripts/audit_lessons.py: 503 lessons, 0 issues.Closes #294.
🤖 Generated with Claude Code