Skip to content

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

Open
Lyxot wants to merge 1 commit into
unslothai:mainfrom
Lyxot:fix/mlx-watchdog-timeout
Open

fix(mlx): relax Metal context-store timeout by default#7141
Lyxot wants to merge 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 can initialize in training and export paths.
  • 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 shared hardware module covers Studio and CLI training hardware detection, and the export backend now imports that hardware bootstrap before importing unsloth, covering both subprocess and direct export-backend imports.

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 studio/backend/utils/hardware/hardware.py studio/backend/core/export/export.py
All checks passed

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".

Comment thread studio/backend/utils/hardware/hardware.py
@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".

Comment on lines +54 to +55
if platform.system() == "Darwin" and platform.machine() == "arm64":
os.environ.setdefault("AGX_RELAX_CDM_CTXSTORE_TIMEOUT", "1")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Put the Metal default in the Unsloth bootstrap

On Apple Silicon users who run the normal Python API (from unsloth import FastLanguageModel) never import studio.backend.utils.hardware before unsloth/__init__.py calls _is_mlx_available() and imports unsloth_zoo.mlx (unsloth/init.py:57-76), so this setdefault is not applied before MLX can initialize. That leaves regular non-Studio MLX training/inference with the old watchdog behavior; the default needs to live in a bootstrap that the unsloth package imports before MLX detection, or be shared by both.

Useful? React with 👍 / 👎.

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