Skip to content

Stop the -bf16 notice claiming a request the caller never made - #9685

Merged
danielhanchen merged 3 commits into
mainfrom
fix/bf16-notice-default-4bit
Aug 25, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
fix/bf16-notice-default-4bit

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Follow-up to #9678, which merged while its last review round was still open.

The problem

load_in_4bit defaults to True in both loaders:

  • unsloth/models/loader.py:431 - FastLanguageModel.from_pretrained
  • unsloth/models/loader.py:1236 - FastModel.from_pretrained

Nothing between the signature and the -bf16 branch clears it on an ordinary call. The block at :462 that reads kwargs["quantization_config"] only ever turns flags on, and the bitsandbytes-unavailable branch at :577 only fires when bitsandbytes is missing.

So a bare FastLanguageModel.from_pretrained("org/model-bf16") reached the notice added in #9678 with the default flag still set, and was told:

Unsloth: `org/model-bf16` is a 16bit (-bf16) checkpoint, so the requested
4bit/8bit/fp8 loading is disabled and the model will load in 16bit. ...

when it had requested nothing. That is the most common call in the library.

The change

The notice now reports what happens rather than what was asked for, and names the 4bit default as the reason so the advice to point at the 4bit repo still lands:

Unsloth: `org/model-bf16` is a 16bit (-bf16) checkpoint, so 4bit/8bit/fp8
loading is disabled and the model will load in 16bit, which needs far more
VRAM. Unsloth loads 4bit by default; point at the 4bit repo instead if you
wanted that.

It is also suppressed when load_in_16bit is set. That flag defaults to False and nothing sets it True before the branch, so True there really is the caller's word, and telling someone who explicitly asked for 16bit that their quantization was dropped is noise.

Applied to all four copies of the branch, two in each loader. The quantization_config gate from #9678 is unchanged.

What is deliberately not changed

The signature default stays load_in_4bit = True rather than moving to a None sentinel. It is a documented public default and int(load_in_4bit) arithmetic exists downstream at unsloth/models/vision.py:1234, so a sentinel would be an API change for a message fix.

Tests

Two new source-level guards plus a behavioural one that runs a child interpreter with HF_HUB_OFFLINE=1 (the notice prints before any download) and asserts the bare call prints a notice making no claim about a request, while an explicit load_in_16bit = True or load_in_4bit = False prints nothing.

Against the unfixed loader that file reports 9 failed, 6 passed; with the fix, 15 passed. Full run:

tests/test_bf16_quant_disabled_message.py
tests/test_offloaded_parameter_hint.py
tests/test_kaggle_gguf_error_message.py
58 passed

One trap for anyone rewording the message later: the guard detector matches the substring load in 16bit through ast.get_source_segment, so wrapping the f-string such that the phrase spans two source lines makes all four guards invisible to the tests.

load_in_4bit defaults to True in both loaders (loader.py:431 for
FastLanguageModel.from_pretrained, :1236 for FastModel.from_pretrained), and
nothing between the signature and the -bf16 branch clears it on an ordinary
call. So a bare from_pretrained('org/model-bf16') reached the notice with the
default flag still set and was told its 'requested 4bit/8bit/fp8 loading is
disabled' when it had requested nothing. That is the most common call in the
library.

The notice now reports what happens rather than what was asked for, and names
the 4bit default as the reason, so the advice to point at the 4bit repo still
lands. It is suppressed when load_in_16bit is set, since that flag defaults to
False and nothing sets it True beforehand, so True there really is the
caller's word. The round 1 quantization_config gate is unchanged.

The signature default is deliberately left alone rather than moved to a None
sentinel: load_in_4bit=True is a documented public default and int(load_in_4bit)
arithmetic exists downstream in vision.py, so a sentinel would be an API change
for a message fix.

Two new source-level guards plus a behavioural one that runs a child
interpreter offline and asserts the bare call prints a notice making no claim
about a request, while an explicit load_in_16bit or load_in_4bit=False prints
nothing. Against the unfixed loader that file reports 9 failed, 6 passed.

One trap for anyone rewording this later: the guard detector matches the
substring 'load in 16bit' via ast.get_source_segment, so wrapping the f-string
so that phrase spans two lines makes all four guards invisible to the tests.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@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: 49311cb636

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

for cls in (FastLanguageModel, FastModel):
name = cls.__name__
bare = notice(cls)
assert bare, f"{name}: bare call printed no notice"

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 Account for the bitsandbytes fallback in the probe

On supported hosts where bitsandbytes is absent or its native kernels are unusable, both loaders clear the default load_in_4bit=True before reaching the -bf16 branch, so a bare call correctly emits no (-bf16) checkpoint notice and this assertion fails. The parent test specifically treats AssertionError as a real failure rather than an unavailable-environment skip, causing the suite to fail on supported bnb-less/AMD configurations; gate this expectation on ALLOW_BITSANDBYTES or otherwise force a mode that reaches the notice.

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.

Agreed. Without bitsandbytes, ALLOW_BITSANDBYTES is False, both loaders clear load_in_4bit and load_in_8bit at loader.py:606-607 and pop quantization_config at :609, so by the time the -bf16 branch runs there is no quant left to disable and the notice correctly stays silent. assert bare, "bare call printed no notice" would then have failed on a machine where the code was behaving exactly as intended.

9056f1d imports ALLOW_BITSANDBYTES and branches on it: with bitsandbytes the notice must appear and must not claim a request, without it the notice must NOT appear. That keeps the check meaningful in both environments instead of trading one wrong answer for a weaker assertion.

bare = notice(cls)
assert bare, f"{name}: bare call printed no notice"
assert "request" not in bare.lower(), f"{name}: bare call was told it requested 4bit: {bare}"
assert not notice(cls, load_in_16bit = True), f"{name}: explicit 16bit call got a notice"

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 Exercise the FastModel 16-bit branch before asserting

On a bitsandbytes-capable host, FastModel.from_pretrained(..., load_in_16bit=True) still has the default load_in_4bit=True, so its mutually exclusive mode check raises before reaching the changed -bf16 guard. Because notice() swallows that exception and returns an empty string, this assertion passes without testing the intended behavior; disable the default 4-bit mode and verify that execution actually reaches the branch rather than accepting any early failure.

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 I confirmed it by instrumenting the probe rather than reading it. Printing which calls reach the branch:

FastLanguageModel  {}                        notice=YES
FastLanguageModel  {'load_in_16bit': True}   notice=no    (reached the branch, gate suppressed it)
FastModel          {}                        notice=YES
FastModel          {'load_in_16bit': True}   notice=no    RuntimeError: Can only load in 4bit or 8bit or 16bit

The FastModel row is satisfied by the raise, not by the gate, so that assertion would have stayed green with the gate deleted. Fixed in 9056f1d: notice() now fails loudly if the mode-combination error appears, and the two loaders are asserted separately. FastLanguageModel accepts load_in_16bit = True with the 4bit default still on, which is the one combination where suppression comes from the caller's 16bit word rather than from there being no quant left to drop, so that is the half pinning the gate.

Falsified by restoring the old spelling: AssertionError: FastModel {'load_in_16bit': True}: never reached the branch.

Instrumented the probe and printed which calls actually reach the -bf16
branch. Two of its assertions were not measuring what they claimed.

FastModel.from_pretrained(name, load_in_16bit=True) raises 'Can only load in
4bit or 8bit or 16bit' before the branch is evaluated, because the default
load_in_4bit=True is still set alongside it. So 'no notice was printed' was
satisfied by an exception path, not by the gate under test, and the assertion
would have stayed green with the gate deleted. notice() now fails loudly on
that raise, and the two loaders are asserted separately: FastLanguageModel
accepts load_in_16bit=True with the 4bit default still on, which is the one
combination where suppression comes from the caller's 16bit word rather than
from there being no quant left to drop, so it is the half that pins the gate.

The bare-call assertion assumed bitsandbytes. Without it both loaders clear
load_in_4bit before the branch, no quant remains to disable, and the notice
correctly stays silent, so 'bare call printed no notice' would have failed on
a machine where the code is behaving properly. It is now conditioned on
ALLOW_BITSANDBYTES and asserts the opposite in the other case.

Falsified by restoring the old FastModel spelling: the new guard reports
'FastModel {load_in_16bit: True}: never reached the branch'.
@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. 🎉

Reviewed commit: 9056f1de60

ℹ️ 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 a182b18 into main Aug 25, 2026
27 of 38 checks passed
@danielhanchen
danielhanchen deleted the fix/bf16-notice-default-4bit branch August 25, 2026 10:51
danielhanchen added a commit that referenced this pull request Aug 25, 2026
* Stop the -bf16 notice claiming a request the caller never made

load_in_4bit defaults to True in both loaders (loader.py:431 for
FastLanguageModel.from_pretrained, :1236 for FastModel.from_pretrained), and
nothing between the signature and the -bf16 branch clears it on an ordinary
call. So a bare from_pretrained('org/model-bf16') reached the notice with the
default flag still set and was told its 'requested 4bit/8bit/fp8 loading is
disabled' when it had requested nothing. That is the most common call in the
library.

The notice now reports what happens rather than what was asked for, and names
the 4bit default as the reason, so the advice to point at the 4bit repo still
lands. It is suppressed when load_in_16bit is set, since that flag defaults to
False and nothing sets it True beforehand, so True there really is the
caller's word. The round 1 quantization_config gate is unchanged.

The signature default is deliberately left alone rather than moved to a None
sentinel: load_in_4bit=True is a documented public default and int(load_in_4bit)
arithmetic exists downstream in vision.py, so a sentinel would be an API change
for a message fix.

Two new source-level guards plus a behavioural one that runs a child
interpreter offline and asserts the bare call prints a notice making no claim
about a request, while an explicit load_in_16bit or load_in_4bit=False prints
nothing. Against the unfixed loader that file reports 9 failed, 6 passed.

One trap for anyone rewording this later: the guard detector matches the
substring 'load in 16bit' via ast.get_source_segment, so wrapping the f-string
so that phrase spans two lines makes all four guards invisible to the tests.

* Tighten the -bf16 notice comments

* Stop the behaviour probe passing for the wrong reason

Instrumented the probe and printed which calls actually reach the -bf16
branch. Two of its assertions were not measuring what they claimed.

FastModel.from_pretrained(name, load_in_16bit=True) raises 'Can only load in
4bit or 8bit or 16bit' before the branch is evaluated, because the default
load_in_4bit=True is still set alongside it. So 'no notice was printed' was
satisfied by an exception path, not by the gate under test, and the assertion
would have stayed green with the gate deleted. notice() now fails loudly on
that raise, and the two loaders are asserted separately: FastLanguageModel
accepts load_in_16bit=True with the 4bit default still on, which is the one
combination where suppression comes from the caller's 16bit word rather than
from there being no quant left to drop, so it is the half that pins the gate.

The bare-call assertion assumed bitsandbytes. Without it both loaders clear
load_in_4bit before the branch, no quant remains to disable, and the notice
correctly stays silent, so 'bare call printed no notice' would have failed on
a machine where the code is behaving properly. It is now conditioned on
ALLOW_BITSANDBYTES and asserts the opposite in the other case.

Falsified by restoring the old FastModel spelling: the new guard reports
'FastModel {load_in_16bit: True}: never reached the branch'.
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