Skip to content

Point a 4-bit load that does not fit the GPU at offload_layers = "auto" - #13052

Merged
danielhanchen merged 7 commits into
mainfrom
fix-1629-cpu-spill-message
Oct 9, 2026
Merged

danielhanchen merged 7 commits into
mainfrom
fix-1629-cpu-spill-message

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

Fixes #1629

When a 4-bit (or 8-bit) bitsandbytes load does not fit on the GPU, transformers' automatic device map spills part of the model to the CPU and its bitsandbytes quantizer refuses with:

ValueError: Some modules are dispatched on the CPU or the disk. Make sure you have enough GPU RAM to fit the quantized model. If you want to dispatch the model on the CPU or the disk while keeping these modules in 32-bit, you need to set `llm_int8_enable_fp32_cpu_offload=True` and pass a custom `device_map` to `from_pretrained`. ...

That advice keeps the spilled weights unquantized in fp32 on the CPU, which does not get a user to training. Unsloth already has the right tool for this case, offload_layers = "auto", which keeps the decoder layers that do not fit in host RAM and streams them in during training. This PR re-raises the spill error (chained to the original) from both FastLanguageModel and FastModel with a message that names the model, the free GPU memory, and points at offload_layers = "auto":

ValueError: Unsloth: unsloth/Mistral-Small-3.2-24B-Instruct-2506-unsloth-bnb-4bit does not fit in GPU memory (cuda:0 has 11.96 GB free), so transformers placed part of it on the CPU, which bitsandbytes cannot quantize. Pass `offload_layers = "auto"` to from_pretrained to keep the decoder layers that do not fit in host RAM and stream them in during training (slower, but it trains), load a smaller model, or free GPU memory held by other programs.

(The latest wording says "slower, and it needs that much free system RAM" instead of "slower, but it trains".)

The hint adapts to what the caller passed: offload_layers = "auto" already set suggests a shorter max_seq_length or batch; a numeric offload_layers suggests more layers or "auto"; 8-bit loads suggest 4-bit; a caller's own quantization_config suggests load_in_4bit = True with offload_layers. A caller's own dict, "cpu" or disk device_map keeps the transformers error unchanged (on transformers 4.x an all-CPU map raises it too). The free-memory figure reads only the current GPU, and is skipped under a caller's max_memory. Every other error, and every load that fits, is untouched: the handler only matches a ValueError starting with the bitsandbytes quantizers' own prefix (a test pins that prefix against the installed transformers 4-bit and 8-bit quantizer sources).

Testing

Single GPU with free memory capped (both what accelerate reads and what the allocator allows), 4-bit, max_seq_length = 2048, LoRA r=16 + 2 training steps where it loads:

Case main this PR
Mistral-Small-3.2-24B, 23 GB (RTX 3090 report) loads, trains, peak 15.9 GB same, peak 15.9 GB
Qwen3-14B, 14.5 GB (T4 class) loads, trains, peak 12.0 GB same, peak 12.0 GB
Mistral-Small-3.2-24B, 12 GB transformers spill error Unsloth message above
Qwen3-14B, 7 GB transformers spill error Unsloth message
Mistral-Small-3.2-24B, 12 GB, offload_layers = "auto" loads, trains, peak 9.8 GB (the suggested fix works)

Real Colab T4 (14.9 GB free), Qwen3-32B 4-bit:

main this PR
default load transformers spill error Unsloth message, cuda:0 has 14.35 GB free

8-bit Qwen3-14B at 7 GB on this PR gets the load_in_4bit hint.

tests/test_bnb_cpu_spill_message.py: 14 passed, including an AST check that both loaders route load errors through the helper (fails when the wiring is reverted).

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 8, 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-08T13:49:31.030992Z b954e6a Manual request
🔒 Security Review ⚠️ Failed 2026-10-08T11:54:00.936983Z 9a358d5 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 danielhanchen self-assigned this Oct 8, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Something went wrong. Try again later by commenting “@codex review”.

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

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@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: 84011293bf

ℹ️ 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 unsloth/models/llama.py
_offload_layers_requested,
device_map = device_map,
load_in_8bit = load_in_8bit,
quantization_config = _quantization_config_requested,

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 Detect bundled 8-bit configs before suggesting offload

For a prequantized 8-bit checkpoint whose config.json supplies model_config.quantization_config (or a caller-supplied config containing that setting), load_in_8bit can remain false and _quantization_config_requested is None, even though Transformers performs an 8-bit load and can raise this spill error. The current version still passes only the original quantization_config kwarg here, so the helper incorrectly recommends offload_layers="auto", which is not supported for effective 8-bit loads; pass the resolved model configuration's quantization mode so this case receives the 4-bit/smaller-model remedy instead.

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.

Below the bar for this message: bundled 8-bit bnb checkpoints are rare, and if such a user follows the hint, the loader refuses offload_layers with its own printed reason (refuse_block_swap_load), so nothing silently misleads.

Comment thread unsloth/models/loader_utils.py Outdated
Comment on lines +269 to +270
'Pass `offload_layers = "auto"` to from_pretrained to keep the decoder layers the GPU '
"cannot hold in host RAM and stream them in during training (slower, but it trains), "

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 Avoid suggesting offload for ineligible load modes

For a 4-bit spill from a load using state_dict, use_safetensors=False, or a classification head via num_labels, this unconditional hint recommends offload_layers="auto", but the same loader explicitly refuses layer offload for those modes in llama.py lines 2753-2767 and 3026-3029. Following the advice therefore disables the requested offload and reaches the spill again; propagate whether block-swap loading is eligible and offer only the smaller-model/free-memory remedies when it is not.

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.

Below the bar for this message: state_dict / use_safetensors=False / num_labels spills are long-tail, and following the hint there hits refuse_block_swap_load, which prints exactly why offload_layers is declined for that mode.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 13fa11acbf

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

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 70c2cf1f01

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

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 8, 2026
@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. Delightful!

Reviewed commit: b954e6aeb0

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

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.

ValueError: Some modules are dispatched on the CPU or the disk

1 participant