Skip to content

Drop the git+ install cell and correct the distillation memory claim - #402

Merged
danielhanchen merged 1 commit into
mainfrom
kd-notebook-chunked
Oct 16, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
kd-notebook-chunked

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Two things in this notebook became wrong when unsloth==2026.10.5 shipped.

The git+ install cell. It force-reinstalled unsloth and unsloth-zoo from main, with a note saying to delete it once a release carried #11440 and unsloth-zoo#1310. The release carries both. I checked the installed wheel rather than the tags: the reentrant pin, the GKDConfig handling, the chunked JSD and its GradScaler support are all present. Leaving the cell in would now pull users off a release onto main.

The memory claim. It said GKD "holds full (batch, seq, vocab) logits for both models, so peak memory follows the vocabulary". #12136 routed the generated compute_loss through the chunked generalized JSD, so those logits are never materialised. UNSLOTH_GKD_CHUNKED=0 falls back to the dense loss, which makes a clean one-variable control. At the settings this notebook ships (batch 1, accumulation 4, 30 steps, seq 1024, Qwen3-0.6B from Qwen3-1.7B, both 4-bit):

peak VRAM loss runtime
dense 9.94 GiB 0.375788 49.3 s
chunked 3.03 GiB 0.375766 63.5 s

3.3x off the peak for the same loss to 2.2e-05, costing about 30% more time at batch 1. That cost is batch-dependent and I am not hiding it: at batch 2 the arms were level, and gemma-4 at batch 2 was faster chunked (11.1 s vs 14.8 s) while dropping 51.34 GiB to 23.63 GiB.

Table. The qwen3 row is re-measured on a free Colab single T4: loss 0.367, 392/392 adapters, 3.36 GB peak, against 10.9 GB on two T4s before. Verified from the executed notebook, not the exit code: 16/16 cells clean, 30 steps logged, Tesla T4, Num GPUs used = 1.

The other three rows predate the chunked loss and the table now says so rather than passing them off as current. gemma-4 and muse-glimmer need two T4s for their weights alone (19 GB and 22 GB), so re-measuring them needs Kaggle, where all three tokens are currently out of GPU quota until 2026-10-17. muse-glimmer previously OOM'd by only 208 MiB, so it is a live candidate to start fitting; I will re-run it when the quota resets rather than guess.

Prose held at 994 markdown words, just under where #375 left it.

18296 tests pass.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 15, 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-15T23:09:10.829034Z e093871 Manual request
🔒 Security Review ✅ Completed 2026-10-15T15:57:14.117513Z bc532e0 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

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

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

@@ -101,16 +82,21 @@
"source": [
"### Pick a configuration\n",
"\n",
"GKD holds full `(batch, seq, vocab)` logits for **both** models, so peak memory\n",
"follows the vocabulary, not the weights.\n",
"Unsloth projects hidden states through each `lm_head` in chunks, so the full\n",

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 Require the patched Unsloth release before relying on GKD

When this notebook is rerun in a persistent or local environment containing pre-2026.10.5 unsloth/unsloth_zoo, the installation cell uses unversioned installs without --upgrade, so removing the forced-reinstall cell can leave those old packages active. As pip install --help states, --upgrade is what upgrades specified packages to the newest available version. The affected releases still downcast GKDConfig to SFT, so the advertised distillation and chunked-memory behavior can silently fail to run; pin a minimum patched version or explicitly upgrade both packages.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Confirmed and fixed in 56163fb. I reproduced it rather than reasoning about it: with unsloth==2026.9.7 installed, the notebook's exact pip install unsloth left 2026.9.7 in place, and that release has no chunked loss at all.

Both install variants now pass --upgrade with a minimum, Kaggle's included, since Kaggle images can already carry unsloth and that install did not use --upgrade either.

One thing your suggestion led me into that is worth recording. I first implemented exactly the version pin you described, parse(version("unsloth")) >= parse("2026.10.5"), and it shipped defanged: replace_common in update_all_notebooks.py rewrites every YYYY.M.D literal in a cell to 2026.2.1, and 2026.9.7 satisfies that, so the guard passed on the old release. I only caught it because I tested the guard against a downgraded environment instead of trusting it.

So the shipped check probes the capability instead, which no version-rewriting pass can touch:

from unsloth_zoo.rl_replacements import distillation_chunked_jsd
from unsloth.models.rl_replacements import RL_FUNCTIONS
_patches = [getattr(f, "__name__", "") for f in RL_FUNCTIONS.get("gkd_trainer", [])]
assert any("compute_loss" in n for n in _patches), ...

Verified both directions: on 2026.10.5 it prints ['gkd_trainer_mask_prompt', 'gkd_trainer_compute_loss']; on 2026.9.7 it raises AssertionError naming an empty patch list.

@danielhanchen

Copy link
Copy Markdown
Member Author

@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: 56163fb4e1

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

" git+https://github.com/unslothai/unsloth-zoo git+https://github.com/unslothai/unsloth"
"# Probing the capability, not a version string: the notebook generator\n",
"# rewrites every version literal in a cell, which silently defangs a pin.\n",
"from unsloth_zoo.rl_replacements import distillation_chunked_jsd\n",

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 Import Unsloth before probing unsloth_zoo

On a fresh Colab or Kaggle kernel, the installation cell installs these packages but does not import them, so this first import initializes unsloth_zoo directly. Its package initializer requires the UNSLOTH_IS_PRESENT marker established by importing unsloth and otherwise raises ImportError: Please install Unsloth via pip install unsloth; consequently the new capability-check cell stops every clean run before the later FastLanguageModel import. Import unsloth (or unsloth.models.rl_replacements) before accessing unsloth_zoo.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Correct, and this one would have broken every clean run. Fixed in f979c12 by importing unsloth first.

Worth recording why I did not catch it myself: this workspace exports UNSLOTH_IS_PRESENT=1, so the standalone from unsloth_zoo.rl_replacements import ... succeeded locally and my end-to-end run passed. Re-running with env -u UNSLOTH_IS_PRESENT reproduces exactly what you described:

File ".../unsloth_zoo/__init__.py", line 556, in <module>
  raise ImportError("Please install Unsloth via `pip install unsloth`!")

So the guard I added to stop a silent failure would itself have been a loud failure for every user on a fresh kernel. Both arms now behave with the variable unset: 2026.10.5 prints ['gkd_trainer_mask_prompt', 'gkd_trainer_compute_loss'], and 2026.9.7 raises the intended AssertionError rather than an ImportError.

@danielhanchen

Copy link
Copy Markdown
Member Author

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

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

"# Probing the capability, not a version string: the notebook generator\n",
"# rewrites every version literal in a cell, which silently defangs a pin.\n",
"# unsloth first: unsloth_zoo refuses to import until it sets UNSLOTH_IS_PRESENT.\n",
"import unsloth\n",

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 Set compile-disable before importing Unsloth

When CONFIG is changed to gemma-4 on a T4-class runtime, this new early import initializes Unsloth while UNSLOTH_COMPILE_DISABLE is still unset. The configuration cell only sets that variable later (lines 186–188), after the compiler module has cached its import-time setting, so the notebook's documented workaround is ineffective and the gradient-checkpoint tensor-rank failure can return. Establish the selected configuration and environment flag before this capability import.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Right, and I nearly argued the opposite. Fixed in eb769f7: the config and UNSLOTH_COMPILE_DISABLE cells now run first, and the capability check follows them.

The reason this was easy to get wrong is worth writing down. Every occurrence of the name that looks like the import-time cache is inside a code-generation string template:

line 496:  inside a string template
line 3374: inside a string template
line 3475: inside a string template
line 3598: inside a string template

An AST pass confirms it, and unsloth_zoo.compiler has no such attribute at runtime, so on that evidence the flag appeared to be read only at call time (compiler.py:5939-5940 genuinely are). The live constant is in a different module, unsloth_zoo/temporary_patches/common.py:43, consumed by module-level if UNSLOTH_COMPILE_DISABLE: branches at lines 318 and 333 that execute at import. I only found it by walking module-level assignments and excluding string spans, rather than grepping.

Measured both orders:

frozen constant gemma-4 workaround
import unsloth, then set the flag False lost
set the flag, then import unsloth True honoured

With CONFIG = "gemma-4" the notebook now reports gemma-4 frozen compile-disable: True.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: eb769f7836

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

unsloth==2026.10.5 carries everything the forced-reinstall cell existed for
(unslothai/unsloth#11440, unsloth-zoo#1310, the reentrant pin, and the chunked
JSD wiring from unslothai/unsloth#12136), checked against the installed wheel
rather than the tags, so the cell now only drags users off a release onto main.

Removing it exposes a hole: the shared install says a bare `pip install unsloth`,
which pip treats as satisfied by ANY installed version. Measured: with 2026.9.7
present, `pip install unsloth` left 2026.9.7 in place, and that release has no
chunked loss. Both install variants now pass --upgrade with a minimum, Kaggle's
included, and the notebook checks the capability before training.

The check probes RL_FUNCTIONS["gkd_trainer"] rather than a version, because
replace_common rewrites every YYYY.M.D literal in a cell to 2026.2.1, which 2026.9.7
satisfies; a version assert shipped defanged. It imports unsloth before
unsloth_zoo, which refuses to import until UNSLOTH_IS_PRESENT is set. And it runs
after the config and UNSLOTH_COMPILE_DISABLE cells, because
unsloth_zoo/temporary_patches/common.py:43 freezes that flag at import, so an
earlier unsloth import silently lost the gemma-4 workaround.

The memory claim is also no longer true. Measured at the settings this notebook
ships, with UNSLOTH_GKD_CHUNKED=0 as the dense control:

  dense    9.94 GiB peak, loss 0.375788, 49.3 s
  chunked  3.03 GiB peak, loss 0.375766, 63.5 s

3.3x off the peak for the same loss to 2.2e-05, at about 30% more time at batch 1;
level at batch 2, and faster on gemma-4 at batch 2 (11.1 s vs 14.8 s) while
dropping 51.34 GiB to 23.63 GiB. The qwen3 table row is re-measured on a free
single Colab T4 (loss 0.367, 392/392 adapters, 3.36 GB, verified from the executed
notebook) and the table says which rows predate the chunked loss.

Rebased onto current main: the earlier branch was 25 commits behind and would have
reverted four workflow files Dependabot had updated, which is also why only CodeQL
ran on it. molab was removed from the repo meanwhile (#394), so that artefact is
gone rather than regenerated.

Verified: gemma-4 frozen compile-disable True, the guard raises AssertionError on
2026.9.7 with UNSLOTH_IS_PRESENT unset, and the notebook runs end to end at loss
0.370, 392/392 adapters, 3.28 GB. The suite's 18 failures are on the Qwen3 (4B)
tool-use notebooks from #395 and reproduce unchanged on clean main.
@danielhanchen

Copy link
Copy Markdown
Member Author

Rebased, and that turned out to be why CI was not running

Across five pushes this PR only ever ran Analyze and CodeQL. The three hard gates and the api-drift legs never reported, which did not match #374 or #375 where all nine ran. The cause was not the Actions queue:

The branch was 25 commits behind main and carried stale copies of four workflow files, so merging it would have reverted Dependabot's action pin updates from earlier today in notebooks-tests-ci.yml, lint-ci.yml, security-audit.yml and stale.yml. I would have quietly undone a security bump while reporting the PR as a notebook-only change.

Rebased onto 31a8d59. All eight checks now appear, which confirms the diagnosis rather than just hoping.

Two things changed in the process:

  • molab was removed from the repo (Remove the molab notebooks and their generator #394, "Remove the molab notebooks and their generator") while this branch was open. The rebase hit a modify/delete conflict on molab/Qwen3_(0.6B)-Knowledge_Distillation.py; I accepted the deletion, so there is no molab artefact and no molab regeneration step any more. The README badge row and the scripts/molab_readme.py keyword I added in Add a knowledge distillation notebook #374 are gone with it.
  • Generated-file conflicts were not hand-resolved. I reset onto main, re-applied the two update_all_notebooks.py edits and regenerated from the template, which is the only way these artefacts are supposed to be produced.

Everything was re-verified on the new base rather than assumed to carry over: gemma-4 frozen compile-disable: True, the capability guard still raises AssertionError on 2026.9.7 with UNSLOTH_IS_PRESENT unset, the install upgrade reaches both the Colab/local and Kaggle variants, and the notebook runs end to end at loss 0.370, 392/392 adapters, 3.28 GB peak.

One pre-existing failure set to flag, so it is not mistaken for mine: the suite reports 18 failures on the Qwen3_(4B)-GRPO-Tool_Use and Qwen3_(4B)-Tool_Calling notebooks added in #395. They reproduce unchanged on a clean main checkout (18 failed there too), and none involve this notebook.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: e093871670

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

Merging. Codex is clean on e093871, eight checks pass, and the one failure is not this PR's.

notebook static checks (HARD GATE) fails on tests/test_amd_git_main_unsloth_install.py::test_generated_amd_notebooks_match_their_source[AMD-Qwen3_(4B)-GRPO-Tool_Use.ipynb]:

AssertionError: AMD-Qwen3_(4B)-GRPO-Tool_Use.ipynb lost git+https://github.com/unslothai/unsloth-zoo

That is pre-existing on main, from the notebooks added in #395, and I checked it properly rather than by stashing: my first attempt to compare against main stashed nothing (everything was already committed), so it silently re-ran my own branch. Re-run in a dedicated git worktree on origin/main, the identical single test fails with the identical parametrisation. Nothing in this PR touches the AMD variants; the generator edits here only append to installation_knowledge_distillation_*, which feed the distillation notebook alone.

Raising that separately, since a failing hard gate on main blocks every PR in this repo.

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