feat: add macOS (MPS/CPU) support to training script#240
Open
Ryo722 wants to merge 1 commit into
Open
Conversation
Enable training on macOS with Apple Silicon (MPS) and CPU fallback, while maintaining full CUDA compatibility. Changes: - Add device detection: CUDA > MPS > CPU, used throughout the script - Guard CUDA-specific settings (TF32, SDP, flash attention) behind `torch.cuda.is_available()` check - Add `torch.cuda.amp` import fallback to `torch.amp` for PyTorch 2.6+ - Use gloo DDP backend on macOS (Darwin) in addition to Windows - Pass `device_ids` to DDP only on CUDA (MPS does not support it) - Replace all `.cuda(local_rank)` with `.to(_DEVICE)` for portability - Preserve `non_blocking=True` for CUDA transfers (no-op on CPU/MPS) - Guard `torch.cuda.set_device()` and `torch.cuda.empty_cache()` behind availability checks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Enable
train_ms_jp_extra.pyto run on macOS with Apple Silicon (MPS) and CPU, while maintaining full CUDA compatibility.Motivation
The current training script assumes CUDA is always available, which causes crashes on macOS. This PR adds proper device detection and conditional execution so the same script works across CUDA, MPS, and CPU environments.
Changes
CUDA > MPS > CPUselection via_DEVICEvariabletorch.cuda.is_available()checktorch.cuda.amp→torch.ampfor PyTorch 2.6+ compatibilityglooon macOS (Darwin) in addition to Windowsdevice_ids).cuda(local_rank)→.to(_DEVICE)withnon_blockingpreserved for CUDAtorch.cuda.set_device()andtorch.cuda.empty_cache()guardedBackward Compatibility
non_blocking=Truepreserved.Testing
Tested on macOS 14 (Apple Silicon M4) with PyTorch 2.3 (CPU mode).