Skip to content

Add use_exact_model_name option to prevent automatic model name modification - #1339

Merged
danielhanchen merged 2 commits into
unslothai:nightlyfrom
niryuu:use-exact-model-name
Jan 5, 2025
Merged

danielhanchen merged 2 commits into
unslothai:nightlyfrom
niryuu:use-exact-model-name

Conversation

@niryuu

@niryuu niryuu commented Nov 26, 2024

Copy link
Copy Markdown

When loading quantized models, Unsloth automatically modifies the model name to load optimized versions. While this is helpful in most cases, it can lead to duplicate model caching when users specifically want to load both original and quantized ways.

This PR adds a new use_exact_model_name parameter that allows users to bypass this automatic modification and load the exact model specified.

Example:

model, tokenizer = FastLanguageModel.from_pretrained(
    "google/gemma-2-9b",
    load_in_4bit=True,
    use_exact_model_name=True  # Will load exactly this model without modification
)

@niryuu
niryuu force-pushed the use-exact-model-name branch from 6d2ea6d to 12f97c8 Compare November 26, 2024 11:20
@danielhanchen

Copy link
Copy Markdown
Member

So the goal is instead of loading google/gemma-2-9b-bnb-4bit, it should load directly from the cache of google/gemma-2-9b?

@niryuu

niryuu commented Nov 26, 2024 •

Copy link
Copy Markdown
Author

Yes. Unsloth has a mechanism for rewriting the given model name according to unsloth/models/mapper.py for efficiency. However, there are cases where we want to use the exact model name, such as cache control. The purpose of this PR is to provide an option for this case.

@danielhanchen

Copy link
Copy Markdown
Member

@niryuu Apologies on the delay - also do you know why you added kwargs.copy() - is copying it necessary?

@niryuu

niryuu commented Dec 5, 2024

Copy link
Copy Markdown
Author

The reason for copying kwargs is to address the problem that use_exact_model_name is a proprietary parameter that causes errors when passed to dispatched models.
However, I just tried it and it works without it.

@danielhanchen
danielhanchen changed the base branch from main to nightly January 5, 2025 05:53
@danielhanchen
danielhanchen merged commit 3f251f5 into unslothai:nightly Jan 5, 2025
danielhanchen added a commit that referenced this pull request Sep 25, 2026
)

* Dequantize fp8 weights left in plain Linears by a text_only FP8 load

unsloth/Qwen3.8-27B-FP8 lists `model.language_model.layers.N.mlp.gate` in
modules_to_not_convert. transformers' should_convert_module prefix-matches that
entry against `mlp.gate_proj`, so gate_proj stays a plain nn.Linear and its
weight_scale_inv is dropped as an unexpected key.

On a full load the raw fp8 values are cast to bf16 and
_restore_dropped_fp8_scales rescales them (#6200). With text_only=True the key is
renamed (`model.language_model.*` -> `model.*`), and transformers 5 keeps the
checkpoint dtype for renamed keys on pre-quantized loads, so gate_proj keeps a
raw fp8 weight. The restore helper took any fp8 weight for a converted FP8Linear
and skipped it, so the eval forward failed with
`expected mat1 and mat2 to have the same dtype, but got: BFloat16 != Float8_e4m3fn`.

A module holding an fp8 weight without its own weight_scale_inv / weight_scale
is now dequantized into the load dtype (passed from the three loader call sites,
falling back to config dtype, the embedding dtype, then bfloat16). The result is
bit-identical to the unrenamed load. Real FP8Linear / FbgemmFp8Linear modules
still carry their scale and are skipped as before.

* Restore dropped FP8 scales on offloaded layers through accelerate's weights map

When a device map offloads part of an FP8 checkpoint (a text_only
Qwen3.8-27B-FP8 load on a GPU that is partly in use already), the orphaned
gate_proj weights of the offloaded layers are meta placeholders, and the restore
helper skipped them with a warning. The text_only forward then still hit a raw
fp8 weight on those layers (`BFloat16 != Float8_e4m3fn`), and an unrenamed load
ran them unscaled.

For a meta weight, find the store accelerate materializes it from (the
OffloadedWeightsLoader behind the hooks' PrefixedDataset), dequantize a copy of
the stored tensor and write it into the loader's in-memory state_dict, which it
reads before the disk index, then recast the placeholder so accelerate's cast on
materialize keeps the dequantized dtype. Converted fp8 modules that carry their
own scale are now skipped before the offload check, so they no longer inflate
the "offloaded" warning. The in-place scaling is moved into
_apply_fp8_block_scale unchanged.

* Only scale an orphaned FP8 weight that still holds raw fp8 values

_restore_dropped_fp8_scales treated every 16/32-bit weight that has a checkpoint
weight_scale_inv as raw values transformers cast without their scale. That is
true on a plain load, but not once a load-time dequantize has already folded
the scale in: with unslothai/unsloth-zoo#1339 (dequantize fp8 weights that have
no FP8 container) the orphaned mlp.gate_proj of Qwen3.8-27B-FP8 was scaled a
second time, leaving it at a relative error of 1.0 against the bf16 weights,
on device and, since the offload restore, on offloaded layers too.

A 16/32-bit weight is now scaled only while every element lies exactly on the
e4m3 or e5m2 grid, which raw widened fp8 values always do and scale-multiplied
values practically never do. The offload path decides from the stored tensor
accelerate materializes. Checking the dtype alone is not enough: an unrenamed
load hands the raw values over as bf16, and that is the case the helper exists
for (#6200).

Tiny qwen3_5 FP8 on transformers 5.17: with #1339, gate_proj goes from 0.9998
back to 0.0267 relative error and the logits match the reference exactly, on
full, text_only and offloaded loads; without it every cell is unchanged. The
fixtures that stood in for raw weights with arbitrary bf16 values now use
fp8-grid values, as a real load produces.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Trim comments in FP8 restore helpers

* Drop narration comments in FP8 restore

* Keep restored disk-offloaded FP8 tensors on disk instead of in RAM

* Tighten disk-offload test docstring

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
…lothai#11841)

* Dequantize fp8 weights left in plain Linears by a text_only FP8 load

unsloth/Qwen3.8-27B-FP8 lists `model.language_model.layers.N.mlp.gate` in
modules_to_not_convert. transformers' should_convert_module prefix-matches that
entry against `mlp.gate_proj`, so gate_proj stays a plain nn.Linear and its
weight_scale_inv is dropped as an unexpected key.

On a full load the raw fp8 values are cast to bf16 and
_restore_dropped_fp8_scales rescales them (unslothai#6200). With text_only=True the key is
renamed (`model.language_model.*` -> `model.*`), and transformers 5 keeps the
checkpoint dtype for renamed keys on pre-quantized loads, so gate_proj keeps a
raw fp8 weight. The restore helper took any fp8 weight for a converted FP8Linear
and skipped it, so the eval forward failed with
`expected mat1 and mat2 to have the same dtype, but got: BFloat16 != Float8_e4m3fn`.

A module holding an fp8 weight without its own weight_scale_inv / weight_scale
is now dequantized into the load dtype (passed from the three loader call sites,
falling back to config dtype, the embedding dtype, then bfloat16). The result is
bit-identical to the unrenamed load. Real FP8Linear / FbgemmFp8Linear modules
still carry their scale and are skipped as before.

* Restore dropped FP8 scales on offloaded layers through accelerate's weights map

When a device map offloads part of an FP8 checkpoint (a text_only
Qwen3.8-27B-FP8 load on a GPU that is partly in use already), the orphaned
gate_proj weights of the offloaded layers are meta placeholders, and the restore
helper skipped them with a warning. The text_only forward then still hit a raw
fp8 weight on those layers (`BFloat16 != Float8_e4m3fn`), and an unrenamed load
ran them unscaled.

For a meta weight, find the store accelerate materializes it from (the
OffloadedWeightsLoader behind the hooks' PrefixedDataset), dequantize a copy of
the stored tensor and write it into the loader's in-memory state_dict, which it
reads before the disk index, then recast the placeholder so accelerate's cast on
materialize keeps the dequantized dtype. Converted fp8 modules that carry their
own scale are now skipped before the offload check, so they no longer inflate
the "offloaded" warning. The in-place scaling is moved into
_apply_fp8_block_scale unchanged.

* Only scale an orphaned FP8 weight that still holds raw fp8 values

_restore_dropped_fp8_scales treated every 16/32-bit weight that has a checkpoint
weight_scale_inv as raw values transformers cast without their scale. That is
true on a plain load, but not once a load-time dequantize has already folded
the scale in: with unslothai/unsloth-zoo#1339 (dequantize fp8 weights that have
no FP8 container) the orphaned mlp.gate_proj of Qwen3.8-27B-FP8 was scaled a
second time, leaving it at a relative error of 1.0 against the bf16 weights,
on device and, since the offload restore, on offloaded layers too.

A 16/32-bit weight is now scaled only while every element lies exactly on the
e4m3 or e5m2 grid, which raw widened fp8 values always do and scale-multiplied
values practically never do. The offload path decides from the stored tensor
accelerate materializes. Checking the dtype alone is not enough: an unrenamed
load hands the raw values over as bf16, and that is the case the helper exists
for (unslothai#6200).

Tiny qwen3_5 FP8 on transformers 5.17: with unslothai#1339, gate_proj goes from 0.9998
back to 0.0267 relative error and the logits match the reference exactly, on
full, text_only and offloaded loads; without it every cell is unchanged. The
fixtures that stood in for raw weights with arbitrary bf16 values now use
fp8-grid values, as a real load produces.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Trim comments in FP8 restore helpers

* Drop narration comments in FP8 restore

* Keep restored disk-offloaded FP8 tensors on disk instead of in RAM

* Tighten disk-offload test docstring

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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.

3 participants