Skip to content

[Bug] Qwen3.8-27B bnb-4bit training crashes with a shape error (R9700 on Windows, also NVIDIA) #9867

Closed

Description

@Awardin

Summary

Training a Qwen3.5-family hybrid-attention model (unsloth/Qwen3.8-27B-unsloth-bnb-4bit) crashes on the first forward pass: the compatibility patch in unsloth_zoo/temporary_patches/bitsandbytes.py passes the packed 4-bit weight undequantized to torch.nn.functional.linear inside the GatedDeltaNet (linear-attention) projections, producing a mat1/mat2 shape error. Loading works fine; only the hybrid-attention projection path fails. Non-hybrid architectures train correctly on the same stack, so this is isolated to the Qwen3.5 GatedDeltaNet patch path — not to bitsandbytes or the ROCm backend.

Environment

  • GPU: AMD Radeon AI PRO R9700 (gfx1201, 32 GB), native Windows 11
  • Driver: AMD Software PRO Edition 26.Q3 (unchanged; installed via the official Unsloth Windows installer)
  • unsloth 2026.8.21 · unsloth_zoo 2026.8.15 · torch 2.11.0+rocm7.13.0 (from repo.amd.com gfx120X wheels) · bitsandbytes 0.50.2.dev0 (ROCm build) · transformers 5.5.0 · peft 0.18.1 · trl 0.23.1 · triton 3.7.1 · Python 3.11

Reproduction

from unsloth import FastLanguageModel

model, tokenizer = FastLanguageModel.from_pretrained(
    "unsloth/Qwen3.8-27B-unsloth-bnb-4bit",   # 22.36 GB, nf4, compute dtype float16
    max_seq_length=512, load_in_4bit=True,
)
model = FastLanguageModel.get_peft_model(model, r=16)
# SFTTrainer(...).train()  -> crashes on the first forward pass

Loading succeeds (20.57 GB VRAM after load, 79.69 M trainable params, architecture Qwen3_5ForConditionalGeneration). The first training step then raises:

RuntimeError: mat1 and mat2 shapes cannot be multiplied (82x5120 and 1x15728640)
  File "unsloth_zoo/temporary_patches/bitsandbytes.py", line 78, in ...
    torch.nn.functional.linear(x, weight, bias)
  called from Qwen3_5GatedDeltaNet_forward -> self.in_proj_z(hidden_states)

Analysis

15,728,640 = 6144 x 5120 / 2 — i.e. exactly the packed nf4 shape of a (6144, 5120) weight. The patched code hands this packed tensor straight to F.linear without dequantizing it, so the matmul sees a (1, 15728640) matrix. The failure site is the GatedDeltaNet input projection (in_proj_z) of the Qwen3.5 hybrid-attention blocks.

Isolation evidence (same machine, same stack, same session)

Test Result
Qwen2.5-3B, load_in_4bit=True, LoRA r=16, 20 steps trains fine, 2.98 GB peak, loss 5.06 -> 1.08
unsloth/gemma-2-27b-it-bnb-4bit (non-hybrid Gemma2ForCausalLM), LoRA r=16, 25 steps trains fine, 16.66 GB peak, loss monotonic, exit 0
unsloth/Qwen3.8-27B-unsloth-bnb-4bit (hybrid GatedDeltaNet) crashes as above

So bitsandbytes 4-bit training itself works on gfx1201/Windows — only the Qwen3.5 hybrid path routes a packed weight into a dense F.linear.

Expected behavior

The GatedDeltaNet projections should either dequantize the 4-bit weight before the F.linear fallback (as the regular attention/MLP paths do), or route through bitsandbytes.matmul_4bit.

Checked for duplicates: #4160 (Qwen3.5 packing/grads) and #6002 (MLX/Studio attn call) describe different failures.

Happy to provide full logs or run patched builds on this hardware.

Activity

  1. rockerBOO commented on Aug 28, 2026

    @rockerBOO

    Hit this independently on CUDA (NVIDIA NGC container, not ROCm) with Qwen/Qwen3.8-27B + FastModel + LoRA — same crash signature (... and 1x15728640).

    Root cause on our end: FastModel.from_pretrained() silently remaps the model name to the prequantized unsloth/Qwen3.8-27B-unsloth-bnb-4bit mirror. That mirror's checkpoint loses quant_state for linear_attn.in_proj_z during loading, so the still-packed 4-bit weight reaches F.linear undequantized on the first forward pass.

  2. lonexreb commented on Aug 29, 2026

    @lonexreb
    Contributor

    Proposed fix: unslothai/unsloth-zoo#1132 — in the patched Linear4bit.forward, a uint8 weight that reaches the quant_state-None fallback now re-derives its quant state from the module (then routes through matmul_4bit like the regular attention/MLP paths), and fails with an error naming the layer if the state is genuinely unrecoverable — instead of running F.linear on the packed buffer.

    Your analysis was exactly right: 15728640 = 6144 × 5120 / 2, and the control flow is pinned by CPU tests using that projection's shape. Since you offered — could you run a patched build from that branch on your gfx1201 box? pip install git+https://github.com/lonexreb/unsloth-zoo@fix/gated-deltanet-packed-4bit-fallback over your existing install should be enough to rerun your repro. That end-to-end confirmation on real hardware is the one piece I can't produce.

  3. lonexreb commented on Aug 31, 2026

    @lonexreb
    Contributor

    @Awardin @rockerBOO — Daniel has confirmed the root cause on unslothai/unsloth-zoo#1132 and the last step before it can merge is an end-to-end run on real hardware, which neither of us can do (I'm on a Mac; the control flow is pinned by CPU tests but the matmul_4bit path needs a real GPU). If either of you can spare a few minutes:

    pip install --force-reinstall --no-deps git+https://github.com/lonexreb/unsloth-zoo.git@fix/gated-deltanet-packed-4bit-fallback

    then re-run exactly what crashed for you before (Awardin: the Qwen3.5 GatedDeltaNet 4-bit load on gfx1201; rockerBOO: Qwen/Qwen3.8-27B + FastModel + LoRA in your NGC container). Before the patch it dies with the mat1 and mat2 shapes cannot be multiplied (…x… and 1x15728640) error; with it, the projections should route through matmul_4bit and the forward pass complete. A quick 👍/👎 with any traceback on the PR is all that's needed — thanks!

  4. changed the title [-][Bug] Qwen3.5 GatedDeltaNet + bnb-4bit: packed 4-bit weight passed undequantized to F.linear (mat1/mat2 shape error) — ROCm gfx1201, native Windows[/-] [+][Bug] Qwen3.8-27B bnb-4bit training crashes with a shape error (R9700 on Windows, also NVIDIA)[/+] on Sep 25, 2026
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