Skip to content

Rename block_swap_layers to offload_layers - #12705

Merged
danielhanchen merged 2 commits into
mainfrom
rename-offload-layers
Oct 5, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
rename-offload-layers

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Renames the block_swap_layers argument (added in #11832) to offload_layers, which says what it does: decoder layers are offloaded to CPU RAM and streamed back to the GPU for each step.

model, tokenizer = FastLanguageModel.from_pretrained(
    "unsloth/Llama-3.1-8B-Instruct",
    max_seq_length = 2048,
    load_in_4bit = True,
    offload_layers = "auto",   # or a count; 0 (default) = off
)
model = FastLanguageModel.get_peft_model(
    model, r = 16, lora_alpha = 16,
    use_gradient_checkpointing = "unsloth",
    offload_layers = "auto",   # or here, after loading
)

Changes

  • offload_layers replaces block_swap_layers in FastLanguageModel / FastModel from_pretrained and get_peft_model, and in every message Unsloth prints about it.
  • block_swap_layers still works as an alias through a small legacy_offload_layers helper, so code written against main keeps working. If both are passed, offload_layers wins.
  • attach_block_swap_layers is renamed attach_offload_layers (internal, only llama.py calls it). Internal names like BlockSwap, plan_block_swap and the unsloth_zoo.block_swap module are unchanged.
  • The other line changes are ruff reflowing lines that now fit on one line.

Companion zoo PR: unslothai/unsloth-zoo#1559 renames the two messages zoo prints and makes the MLX loader drop either name. The two PRs do not depend on each other.

Testing

Qwen3-0.6B on one GPU, 3 SFT steps, 4 layers offloaded each way:

Argument Where Layers offloaded Losses
offload_layers = 4 from_pretrained 24, 25, 26, 27 0.6535, 0.6535, 0.6427
offload_layers = 4 get_peft_model 6, 13, 20, 27 0.6535, 0.6535, 0.6427
block_swap_layers = 4 from_pretrained 24, 25, 26, 27 0.6535, 0.6535, 0.6427
block_swap_layers = 4 get_peft_model 6, 13, 20, 27 0.6535, 0.6535, 0.6427

FastModel with gemma-3-270m: offload_layers = 4 at load and block_swap_layers = 4 in get_peft_model both offload layers 3, 8, 12, 17 with the same loss.

tests/test_block_swap_install.py and tests/test_offload_tied_autodisable.py pass (82), including a new test for the alias.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 4, 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-05T02:07:23.264686Z e5d0d7b 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.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 5, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security 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: 632ab65d24

ℹ️ 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/_utils.py Outdated
def legacy_offload_layers(kwargs, offload_layers = 0):
"""`offload_layers`, accepting its original name `block_swap_layers` from `kwargs`."""
legacy = kwargs.pop("block_swap_layers", None)
return legacy if legacy is not None and not offload_layers else offload_layers

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 Honor an explicit zero for offload_layers

When both names are supplied and the new value is 0 or False, this truthiness check lets the legacy value win; for example, offload_layers=0, block_swap_layers=4 resolves to 4 and unexpectedly enables CPU layer streaming. This can occur when a new default/config is merged with a persisted legacy setting, and contradicts the stated precedence that offload_layers wins whenever both are passed; presence rather than truthiness must determine precedence.

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.

Fixed in e5d0d7b: offload_layers now defaults to None in from_pretrained and both get_peft_model signatures, and block_swap_layers is used only when offload_layers was not given, so offload_layers = 0 with block_swap_layers = 4 resolves to 0. Checked on Qwen3-0.6B: no layers offloaded with that pair or with neither name, layers 6, 13, 20, 27 with block_swap_layers = 4 alone.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 5, 2026
@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: e5d0d7bf88

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

1 participant