Skip to content

fix(mlx): relax Metal context-store timeout by default - #7141

Merged
oobabooga merged 1 commit into
unslothai:mainfrom
Lyxot:fix/mlx-watchdog-timeout
Jul 17, 2026
Merged

oobabooga merged 1 commit into
unslothai:mainfrom
Lyxot:fix/mlx-watchdog-timeout

Conversation

@Lyxot

@Lyxot Lyxot commented Jul 15, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Default AGX_RELAX_CDM_CTXSTORE_TIMEOUT to 1 on Apple Silicon before MLX modules can initialize through the normal Python API or Studio workers.
  • Preserve every explicit user value, including 0 and an empty string, with os.environ.setdefault.

Why

Sustained MLX workloads can hit Metal's context-store/interactivity watchdog while a display is active, as reported in ml-explore/mlx#3267. The driver setting helped the reported M2 case, but it is private and does not guarantee protection on every chip or workload.

The setting must exist before MLX or Metal initializes. The unsloth package bootstrap covers normal Python API and export imports, while Studio's hardware bootstrap mirrors the default for training and inference workers that import MLX directly without first importing unsloth.

Behavior and risk

This is an Apple-Silicon-only, process-scoped default. It does not modify shell profiles or system configuration, and an explicitly supplied environment value always remains authoritative.

Because the variable is undocumented and has not protected every reported system, this PR treats it as a best-effort mitigation rather than a universal watchdog fix.

Validation

python -m ruff check unsloth/__init__.py studio/backend/utils/hardware/hardware.py
All checks passed

Direct import unsloth subprocess probes confirmed:

unset -> 1
explicit 0 -> 0
explicit empty string -> empty string

Copilot AI review requested due to automatic review settings July 15, 2026 13:17
@Lyxot
Lyxot requested a review from danielhanchen as a code owner July 15, 2026 13:17

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request configures the Apple Silicon Metal context-store timeout by setting the AGX_RELAX_CDM_CTXSTORE_TIMEOUT environment variable to "1" on Darwin arm64 systems, while preserving any existing user-defined values. It also introduces a comprehensive test suite to verify this behavior and ensure the environment variable is set before MLX initialization. There are no review comments, so I have no feedback to provide.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@Lyxot
Lyxot force-pushed the fix/mlx-watchdog-timeout branch from 9221fdd to 0874960 Compare July 15, 2026 13:21

Copilot AI left a comment

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.

Pull request overview

Note

Copilot couldn't run its full agentic review because no GitHub Actions runner was available. Make sure your repository has a runner available to run Copilot's review, or add a copilot-setup-steps.yml file specifying one with the runs-on attribute. See the docs for more details.

Sets a safer default for Metal’s context-store timeout on Apple Silicon so MLX workloads are less likely to hit the watchdog, while keeping any user-provided environment value authoritative.

Changes:

  • Default AGX_RELAX_CDM_CTXSTORE_TIMEOUT=1 on macOS arm64 via os.environ.setdefault.
  • Gate the behavior to Apple Silicon only, and keep explicit env overrides (including empty string / "0") intact.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@oobabooga

Copy link
Copy Markdown
Member

@codex review

@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: 0874960980

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@Lyxot
Lyxot force-pushed the fix/mlx-watchdog-timeout branch from 0874960 to 4aa3955 Compare July 16, 2026 11:25
@oobabooga

Copy link
Copy Markdown
Member

@codex review

@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: 4aa39558c9

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@Lyxot
Lyxot force-pushed the fix/mlx-watchdog-timeout branch from 4aa3955 to e69cb30 Compare July 17, 2026 07:05
@oobabooga

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: e69cb30048

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@oobabooga

Copy link
Copy Markdown
Member

Putting the default in both bootstraps is the right design. I verified it on an Apple Silicon Mac (M1, MLX 0.31.2). After the in-process setdefault, C-level getenv (what the AGX driver reads) returns "1", and a real Metal matmul runs cleanly. Importing this branch sets AGX_RELAX_CDM_CTXSTORE_TIMEOUT before the first mlx module lookup, while a pre-PR install leaves it unset.

Every MLX entry point (Python API, Studio workers, CLI train and export) passes through one of the two bootstraps before any MLX import.

The watchdog suppression itself rests on the upstream thread, where the MLX maintainer suggested this variable and the reporter confirmed it resolved the M2 case. The M5 report shows it is not universal, which your description already frames as best effort.

Thanks @Lyxot. Merging.

@oobabooga
oobabooga merged commit 57785f9 into unslothai:main Jul 17, 2026
50 of 51 checks passed
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.

3 participants