Skip to content

Studio: make the install-time bytecode precompile opt-in - #15923

Merged
danielhanchen merged 5 commits into
mainfrom
perf/r9-no-install-precompile-windows
Oct 29, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
perf/r9-no-install-precompile-windows

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

Summary

v0.1.912-beta shipped the post-install bytecode precompile from #15646. It made unsloth studio update and Desktop updates slower on every desktop OS: every update re-runs setup, which compiles a .pyc for every module in the Studio venv (about 25,000 files, including packages whose versions did not change). On Windows, Defender real-time protection also scans each newly written file. Python already compiles each module lazily the first time it is imported and caches it in __pycache__, which spreads the same work over only the modules a launch actually loads.

This turns the precompile off by default on every OS and keeps it as an opt-in.

What changes

  • studio/setup.ps1 and studio/setup.sh: the "Precompile bytecode" step runs only when UNSLOTH_STUDIO_COMPILE_BYTECODE (or UV_COMPILE_BYTECODE) is true (1, true, yes, on, case and whitespace insensitive). A false value on either name still wins. Without the opt-in the step prints nothing and starts no process.
  • studio/precompile_bytecode.py: the helper itself also defaults to off with the same opt-in, so a direct call behaves the same way as setup.

Opt-in

UNSLOTH_STUDIO_COMPILE_BYTECODE=1 unsloth studio update
$env:UNSLOTH_STUDIO_COMPILE_BYTECODE = "1"
unsloth studio update

Results: 911 to 912 Desktop update, precompile on (main) vs off (this PR)

Same update command and environment the 911 Desktop app runs, on the staging runners (CPU torch). "Net saved" is the update time saved minus the extra time of the first backend import main afterwards, which now compiles on demand.

runner update time saved first import main, precompiled / lazy net saved
windows-latest 53.0 s 4.3 / 14.9 s 42.4 s
windows-11-arm 93.5 s 7.4 / 18.4 s 82.5 s
macos-15 31.1 s 4.2 / 7.3 s 27.9 s
ubuntu-latest 20.4 s 2.7 / 4.5 s 18.7 s
ubuntu-24.04-arm 14.4 s 3.1 / 6.3 s 11.1 s

The precompile phase was 41 to 52% of the whole update on every runner, and 20 to 30% of a fresh install. Modules imported later in a session also compile on first use and are not counted in the import column.

What is unchanged

Tests

tests/python/test_installer_compile_bytecode.py:

  • the setup.ps1 block (under PowerShell) and the setup.sh block (under bash) do not call the helper with no switch set or after no dependency pass, and do call it for UNSLOTH_STUDIO_COMPILE_BYTECODE=1 / On and UV_COMPILE_BYTECODE=true / 1; a false on either name wins over a true;
  • the helper writes nothing without the opt-in and compiles with it; the existing compile, idempotence and stop-signal tests run with the opt-in set.

The new default-off cases fail on main; on this branch test_installer_compile_bytecode.py, test_installer_interactive_prompts.py (143 passed together) and tests/python/test_cross_platform_parity.py (119 passed) pass.

Windows detail: Defender exclusions removed (911 to 912 Desktop update)

Same update command and environment the 911 Desktop app runs, then the 912 installer with /S /UPDATE; two runs per arm. "Defender on" = the runner's drive exclusions removed and real-time protection enabled before the update. import main timed twice from the installed backend afterwards. Runner speed varies between jobs, so compare within a row pair.

Defender real-time precompile update total (s) precompile phase (s, share) first / second import main (s)
off on (main) 159.6 / 94.2 79.6 / 43.1 (50% / 46%) 3.8 / 3.4 and 3.2 / 3.2
off off (this PR) 70.5 / 66.7 none 10.7 / 11.4 and 4.4 / 4.5
on on (main) 237.8 / 274.2 130.4 / 150.5 (55% / 55%) 4.6 / 6.1 and 4.1 / 5.5
on off (this PR) 124.3 / 122.4 none 23.6 / 20.7 and 5.4 / 5.4

The precompile writes about 25,000 .pyc files for the whole venv on every update, including packages whose versions did not change. Skipping it moves about 7 s (Defender off) or 16 to 18 s (Defender on) to the first backend import after the update, for a net saving of about 36 to 73 s (off) and 60 to 130 s (on). Modules imported later in a session also compile on first use and are not counted here.

Windows Defender scans every .pyc written during an update, so the
post-install precompile from #15646 made Windows updates slower. Python
already compiles each module lazily on first import, which spreads that
cost over the imports a launch actually makes.

setup.ps1 now runs studio/precompile_bytecode.py only when
UNSLOTH_STUDIO_COMPILE_BYTECODE (or UV_COMPILE_BYTECODE) is true, and the
helper itself defaults to off on Windows with the same opt-in. A false
value still wins. Linux, macOS and WSL are unchanged.
@danielhanchen danielhanchen self-assigned this Oct 29, 2026
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 29, 2026
@danielhanchen danielhanchen changed the title Studio: skip the install-time bytecode precompile on Windows unless opted in Studio: make the install-time bytecode precompile opt-in Oct 29, 2026
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 29, 2026
@danielhanchen
danielhanchen marked this pull request as ready for review October 29, 2026 09:25
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-29T10:26:47.568751Z a026e88 Manual request
ℹ️ 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 010407ffb6

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

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: a026e88b5f

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

@danielhanchen
danielhanchen merged commit 867aa46 into main Oct 29, 2026
11 of 78 checks passed
@danielhanchen
danielhanchen deleted the perf/r9-no-install-precompile-windows branch October 29, 2026 10:38
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.

1 participant