Skip to content

Resolve device auto to Apple Silicon MPS in instruction_tuning - #544

Open
davemaguire wants to merge 2 commits into
natolambert:mainfrom
davemaguire:mps-device-resolver
Open

davemaguire wants to merge 2 commits into
natolambert:mainfrom
davemaguire:mps-device-resolver

Conversation

@davemaguire

@davemaguire davemaguire commented Nov 13, 2026 •

Copy link
Copy Markdown

Thanks Nathan for the book. This is a great resource. I opened this pr adding the apple mps device to resolve_device() for ch 4. As is, the script runs on my mac's CPU. With this change it can actually finish. I didn't make the corresponding modification to the script for other chapters.

Claude Opus 5.5 — full change description

What changed

  • code/instruction_tuning/utils.py: resolve_device() now checks torch.backends.mps.is_available() after CUDA when device="auto", and accepts and validates an explicit device="mps". The error message for unsupported values lists mps.

Why

Running uv run python -m instruction_tuning.train --config instruction_tuning/configs/sft_olmo2_1b.yaml on a Mac trained on the CPU even though the GPU was available.

How to test

On an Apple Silicon Mac:

cd code
uv run python -c "from instruction_tuning.utils import resolve_device; print(resolve_device())"
# -> mps

I checked this on torch 2.15.0 on macOS, and a bf16 forward and backward pass also ran on MPS. I didn't run a full training job.

Notes / follow-ups

  • This PR changes only utils.py, as requested. The Config.device type hint (Literal["auto", "cuda", "cpu"]) and the --device CLI choices still leave out "mps", so --device mps is rejected by argparse. auto works. A follow-up could add "mps" to both.

🤖 Generated with Claude Code

Co-Authored-By: Claude noreply@anthropic.com

resolve_device() previously fell back straight to CPU on Macs. It now
tries CUDA, then MPS, then CPU, and validates an explicit "mps" request.

Co-Authored-By: Claude <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T20:04:07.114261Z d787b53 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d787b53f10

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +83 to +84
elif torch.backends.mps.is_available():
device = "mps"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Avoid auto-selecting MPS when default bf16 is unsupported

On Apple Silicon running macOS 12.3 or 13, this predicate can be true even though PyTorch's MPS backend only supports bfloat16 on macOS 14+. The checked-in config enables bf16 (code/instruction_tuning/configs/sft_olmo2_1b.yaml:22), and load_model constructs a bfloat16 model before .to(device) (utils.py:118-124), so the documented default command now fails while moving the model instead of retaining its previous CPU path. Gate MPS selection on bfloat16 support or choose a compatible dtype/fallback.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I remember doing this cuda-mps-cpu order in the first place and backed out for that ~same codex review (and simplicity) in #514 (review). Although no idea if MPS is more stable/better now

Comment thread code/instruction_tuning/utils.py Resolved
Co-Authored-By: Claude <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants