Skip to content

Bug: Windows - pre-quantized bnb-4bit checkpoints load with quant_state=None, forward fails with shape error #10017

Closed

Description

@heiqilin1985

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: Bnb4bitDeserialize via WeightConverter), 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_open path or the suffix-stripping branch for -bnb-4bit names).
  • Additionally, offload_embedding=True is 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 (accelerate AlignDevicesHook fails 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

  1. lonexreb commented on Sep 16, 2026

    @lonexreb
    Contributor

    This is the same defect as #9867 (CUDA, same model, same mat1 and mat2 signature) and the artifact issue filed for it, #10276: the published unsloth/Qwen3.8-27B-unsloth-bnb-4bit checkpoint's projections load with no quant_state, so the packed uint8 weight reaches F.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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions