Repository navigation
Add use_exact_model_name option to prevent automatic model name modification - #1339
Merged
Merged
Conversation
niryuu
force-pushed
the
use-exact-model-name
branch
from
November 26, 2024 11:20
6d2ea6d to
12f97c8
Compare
Member
|
So the goal is instead of loading |
Author
|
Yes. Unsloth has a mechanism for rewriting the given model name according to |
Member
|
@niryuu Apologies on the delay - also do you know why you added |
Author
|
The reason for copying |
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_nameparameter that allows users to bypass this automatic modification and load the exact model specified.Example: