Skip to content

Quantize fused MoE experts with bitsandbytes 4-bit - #51201

Draft
qgallouedec wants to merge 1 commit into
mainfrom
bnb-4bit-fused-experts
Draft

qgallouedec wants to merge 1 commit into
mainfrom
bnb-4bit-fused-experts

Conversation

@qgallouedec

@qgallouedec qgallouedec commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

🤗 Transformers CI

Fixes the transformers side of bitsandbytes-foundation/bitsandbytes#1849. In v5 the routed experts are 3D parameters, which load_in_4bit leaves in bf16, so most of an MoE stays 16-bit. This quantizes them on load and dequantizes them on access with bitsandbytes' parametrization, the approach of #39859 ported to v5.

  • Qwen3-30B-A3B: 55.7 → 16.8 GiB loaded, greedy decode 8.6 → 8.2 tokens/s (H100)
  • Inkling 2 Mini QLoRA SFT: peak 58.4 → 25.5 GiB, 6.54 → 7.21 s/step, same loss

Draft: save_pretrained fails on the packed experts, and LoRA on them needs huggingface/peft#4214.

import torch
from transformers import AutoModelForCausalLM, BitsAndBytesConfig

model = AutoModelForCausalLM.from_pretrained(
    "Qwen/Qwen3-30B-A3B-Instruct-2507",
    dtype=torch.bfloat16,
    device_map="cuda",
    quantization_config=BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16),
)
print(f"{torch.cuda.memory_allocated() / 2**30:.1f} GiB")  # 55.7 on main, 16.8 with this PR

transformers v5 stores the routed experts of MoE models as 3D parameters, which bitsandbytes 4-bit left in 16-bit.
Quantize them on load and dequantize them on access through bitsandbytes' parametrization.
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

This branch has not been deployed

No deployments
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.

2 participants