Skip to content

Add Apple M-series (MPS) device support to Python scripts #977

@brianholland

Description

@brianholland

Summary

Most standalone Python scripts (.py) only detect CUDA or fall back to CPU, ignoring Apple Silicon's MPS backend. This means users with M1/M2/M3/M4 Macs always fall back to CPU, missing significant GPU acceleration.

Problem

~29 Python files use this pattern:

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

This should be updated to also detect MPS:

if torch.cuda.is_available():
    device = torch.device("cuda")
elif torch.backends.mps.is_available():
    device = torch.device("mps")
else:
    device = torch.device("cpu")

Additional considerations

  • pin_memory=True in DataLoaders should be conditional on CUDA availability
  • torch.compile should be guarded (limited MPS support)
  • DDP scripts are inherently CUDA-only and should be left as-is

Scope

  • All standalone .py training/inference scripts in ch04-ch07, appendix, and pkg/tests
  • Excludes DDP-specific scripts (appendix-A DDP scripts, ch05/10 multi-GPU)
  • Excludes notebooks (many already have MPS support)

🤖 Generated with Claude Code

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions