Repository navigation
Description
Bug: Windows — pre-quantized bnb-4bit checkpoints load with quant_state=None, forward fails with shape error
Environment
- Windows 10 Pro, single RTX 3090 (24GB), 48GB RAM
- unsloth 2026.8.22, unsloth_zoo 2026.8.16 (both latest), transformers 5.5.0 (unsloth_zoo upper pin), bitsandbytes 0.50.2, torch 2.11.0+cu128
- Model:
unsloth/Qwen3.8-27B-unsloth-bnb-4bit(downloaded from the ModelScope mirror, byte-identical size 22,344,833,801)
Summary
Loading the official pre-quantized 4-bit checkpoint via FastModel.from_pretrained(..., load_in_4bit=True) on Windows leaves the attention layers' Params4bit.quant_state as None (the quant metadata in the checkpoint is stored as sibling keys: weight.absmax, weight.quant_map, weight.nested_absmax, weight.nested_quant_map, weight.quant_state.bitsandbytes__nf4, and these are never re-attached on this load path).
The first training/inference forward then crashes in unsloth_zoo/temporary_patches/bitsandbytes.py (line ~78) with:
RuntimeError: mat1 and mat2 shapes cannot be multiplied (87x5120 and 1x15728640)
(15728640 is exactly the packed-uint8 byte length of a 5120→6144 q_proj — i.e. the packed bytes are used directly as a weight matrix.)
Notes:
- transformers 5.5 has the correct deserialization logic (
transformers/integrations/bitsandbytes.py: Bnb4bitDeserializeviaWeightConverter), but on this path it is not applied to these tensors. - On Linux/Colab the same recipe from https://unsloth.ai/docs/models/qwen3.8/train.md reportedly works, so this appears Windows-load-path specific (possibly the
_uma_safe_openpath or the suffix-stripping branch for-bnb-4bitnames). - Additionally,
offload_embedding=Trueis silently ignored on Windows ("Unsloth: Not offloading embeddings; offloading is unsupported on Windows."), after which the default device map dispatches quantized layers to CPU as meta tensors, compounding into further errors (accelerateAlignDevicesHookfails moving meta weights).
Reproduction sketch
from unsloth import FastModel
model, tokenizer = FastModel.from_pretrained(
"unsloth/Qwen3.8-27B-unsloth-bnb-4bit",
max_seq_length=512, load_in_4bit=True, full_finetuning=False,
device_map={"": 0, "model.visual": "cpu"}, # text-only fine-tune; vision tower unused
)
# any forward -> RuntimeError: mat1 and mat2 shapes cannot be multiplied (87x5120 and 1x15728640)Working workaround (post-load quant_state repair)
import bitsandbytes as bnb
from safetensors.torch import safe_open
suffixes = {
".weight.absmax": "absmax", ".weight.quant_map": "quant_map",
".weight.nested_absmax": "nested_absmax", ".weight.nested_quant_map": "nested_quant_map",
".weight.quant_state.bitsandbytes__nf4": "quant_state.bitsandbytes__nf4",
}
stats = {}
with safe_open(checkpoint_path, framework="pt") as f:
for k in f.keys():
for suf, short in suffixes.items():
if k.endswith(suf):
stats.setdefault(k[: -len(suf)], {})[short] = f.get_tensor(k)
break
for name, mod in model.named_modules():
if isinstance(mod, bnb.nn.Linear4bit):
w = mod.weight
if w.is_meta:
continue # never touch meta (CPU-offloaded) weights
if not isinstance(w, bnb.nn.Params4bit) or getattr(w, "quant_state", None) is None:
if name in stats:
mod.weight = bnb.nn.Params4bit.from_prequantized(
data=w.data, quantized_stats=stats[name],
requires_grad=False, device=w.device, module=mod)With this repair + device_map={"": 0, "model.visual": "cpu"}, QLoRA fine-tuning of Qwen3.8-27B completed successfully on a single 24GB RTX 3090 (peak 20.65 GiB, r=4, adafactor).
It would be great if the Windows load path could run the standard bnb deserialization for pre-quantized checkpoints (and/or warn when quant_state ends up None on a non-meta weight).
Activity
Lift the transformers ceiling to 5.17.0 and align the torch mirror
lonexreb commentedon Sep 16, 2026
This is the same defect as #9867 (CUDA, same model, same
mat1 and mat2signature) and the artifact issue filed for it, #10276: the publishedunsloth/Qwen3.8-27B-unsloth-bnb-4bitcheckpoint's projections load with noquant_state, so the packed uint8 weight reachesF.linear. Two independent hardware runs (ROCm gfx1201 on Windows, and CUDA) pinned it to the checkpoint rather than the platform: quantizing the base model at load time trains end to end in the same environment. Your Windows/RTX 3090 report is the third, and the sibling-key layout you list (weight.absmax,weight.quant_map,weight.quant_state.bitsandbytes__nf4) is exactly what is not being re-attached.Working recovery today: load the base model instead of the pre-quantized one,
FastModel.from_pretrained("Qwen/Qwen3.8-27B", load_in_4bit = True, use_exact_model_name = True), so bitsandbytes creates the state at load. The runtime guard that turns the shape error into a named one (and now says the above) is unslothai/unsloth-zoo#1132; the real fix is regenerating the checkpoint, tracked in #10276.Warn when the installed transformers drops a pre-quantized checkpoint…
Load prequantized multimodal checkpoints correctly on transformers 5.…
Warn when the installed transformers drops a pre-quantized checkpoint…
Load prequantized multimodal checkpoints correctly on transformers 5.…
Lift the transformers ceiling to 5.17.0 and align the mirrored CI caps (
Lift the transformers ceiling to 5.17.0 and align the mirrored CI caps (