Skip to content

Studio: lazy-import matplotlib so the server starts when the wheel is blocked - #6596

Merged
danielhanchen merged 5 commits into
unslothai:mainfrom
LeoBorcherding:fix/studio-matplotlib-lazy-import
Jun 23, 2026
Merged

danielhanchen merged 5 commits into
unslothai:mainfrom
LeoBorcherding:fix/studio-matplotlib-lazy-import

Conversation

@LeoBorcherding

Copy link
Copy Markdown
Collaborator

Issue

On Windows with Smart App Control enabled, Studio fails to start. matplotlib.pyplot was imported at the top of studio/backend/core/training/training.py, which is on the server boot path (main -> routes -> core.training -> training). When matplotlib's native extension cannot load, that import raises and takes down the whole server.

A common trigger is Smart App Control blocking matplotlib's unsigned _c_internal_utils wheel, but any environment where matplotlib's native libs are missing or broken hits the same crash. matplotlib is also unpinned, so a fresh release can reintroduce it at any time. See #6588 for the full write-up.

Fix

Make matplotlib lazy so it never loads during server startup:

  • Move the import into a _load_pyplot() helper called on first plot, using the headless Agg backend.
  • Return None when matplotlib is unavailable, so loss plotting degrades gracefully instead of crashing. _create_loss_plot already returned an optional figure, so callers need no changes.
  • Keep the type-only import under TYPE_CHECKING and quote the annotations.

Testing

  • Module imports cleanly with matplotlib forcibly blocked; _load_pyplot() returns None and logs a warning.
  • With matplotlib present, _load_pyplot() returns pyplot on the Agg backend.
  • Full --local install on Windows + AMD ROCm (gfx1200): server now reaches "Application startup complete"; training, chat, and export all work.

Fixes #6588

… blocked

matplotlib.pyplot was imported at the top of core/training/training.py, on the
server boot path. When matplotlib's native extension fails to load (e.g. an
unsigned wheel blocked by Windows Smart App Control), that import crashed the
whole Studio server at startup instead of just disabling loss plots.

Move it into a lazy _load_pyplot() helper called from _create_loss_plot, using
the headless Agg backend, and return None when matplotlib is unavailable so
plotting degrades gracefully. The plot return was already Optional, so callers
need no changes. Keep the type-only import under TYPE_CHECKING and quote the
annotations.

Fixes unslothai#6588
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@LeoBorcherding

LeoBorcherding commented Jun 23, 2026 •

Copy link
Copy Markdown
Collaborator Author

One thing worth flagging for awareness: matplotlib is unpinned in studio/backend/requirements/studio.txt, so every new matplotlib release is a fresh unsigned wheel with no reputation yet, which is what reintroduces the Smart App Control block on Windows. This PR makes that non-fatal, the server boots and just skips plotting, so it isn't blocking. pinning matplotlib to a known-good version would stop new releases from re-triggering it. Happy to add that as well.

@LeoBorcherding

Copy link
Copy Markdown
Collaborator Author

The failing check "Core / llama.cpp build + smoke" looks unrelated to this PR.
It is missing a binary from the prebuilt bundle, not a build break:

AssertionError: llama-cli not found at '/home/runner/.unsloth/llama.cpp/llama-cli'
Build root contents: ['llama-diffusion-cli', 'llama-diffusion-gemma-visual-server',
'llama-quantize', 'llama-server']

The job pulled the prebuilt app-b9739-mix-2d6bd50-linux-x64-cpu bundle and it
does not contain llama-cli (also llama-mtmd-cli and llama-gguf-split), so the
smoke test assertion fails. This PR only touches studio/backend/core/training/
training.py and never goes near llama.cpp, so it should reproduce on main too.
Looks like the linux-x64-cpu prebuilt asset needs those binaries included. Flagging
in case it is affecting other PRs.

Pin matplotlib to the current latest so a new unsigned release does not
reintroduce the Smart App Control block on Windows. Belt-and-suspenders on
top of the lazy import. Pinned in both studio.txt and extras.txt.
@danielhanchen

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: 3d467d2c5d

ℹ️ 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 thread studio/backend/requirements/studio.txt Outdated
pydantic
packaging
matplotlib
matplotlib==3.11.0

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 Keep Studio installable on Python 3.10

This pin makes pip install -r studio/backend/requirements/studio.txt unsatisfiable on Python 3.10 because matplotlib 3.11.0 declares Requires-Python >=3.11; the backend CI matrix still includes Python 3.10 and installs this exact file (.github/workflows/studio-backend-ci.yml:53,68), and pyproject.toml still advertises support below 3.11. Users on supported 3.10 will fail during dependency install before the lazy-import fallback can help; use a 3.10-compatible upper bound or a Python-version marker instead.

Useful? React with 👍 / 👎.

matplotlib 3.11.0 requires Python >=3.11, so the pin had no installable wheel on
Python 3.10 (still supported) and pip install failed there. 3.10.9 is the latest
3.10.x (requires-python >=3.10) and covers Python 3.10 through 3.13. Also tighten
the lazy-import docstrings.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@danielhanchen

Copy link
Copy Markdown
Member

Pushed a small fix on top: pinned matplotlib==3.10.9 instead of 3.11.0.

matplotlib==3.11.0 requires Python >=3.11, so it has no wheel on Python 3.10 (still supported), and the Python 3.10 job failed with No matching distribution found for matplotlib==3.11.0. 3.10.9 is the latest 3.10.x (requires-python >=3.10) and installs cleanly on Python 3.10 through 3.13. It is also an older, aged wheel, so it is less likely to trip Smart App Control than a brand new release.

Verified on Windows and Ubuntu across Python 3.10 and 3.13: the module still imports with matplotlib blocked, _load_pyplot() returns None, and loss plotting degrades gracefully.

The other red checks look unrelated to this change:

  • llama.cpp build + smoke: prebuilt bundle is missing llama-cli; it passes on main and this PR does not touch llama.cpp.
  • pip scan-packages :: extras / hf-stack / studio: fail on unrelated specs (langid, descript-audio-codec) and scanner findings, not on matplotlib.

Also tightened the lazy-import docstrings while here.

@danielhanchen
danielhanchen merged commit 69d8a57 into unslothai:main Jun 23, 2026
42 of 45 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.

Windows install + launch broken on Smart App Control machines (unsigned ROCm/matplotlib wheels blocked)

2 participants