Skip to content

Experts4bit: fused MoE weights in 4-bit NF4/FP4#1984

Open
ZAID646 wants to merge 4 commits into
bitsandbytes-foundation:mainfrom
ZAID646:feat/experts4bit
Open

Experts4bit: fused MoE weights in 4-bit NF4/FP4#1984
ZAID646 wants to merge 4 commits into
bitsandbytes-foundation:mainfrom
ZAID646:feat/experts4bit

Conversation

@ZAID646

@ZAID646 ZAID646 commented Jul 4, 2026

Copy link
Copy Markdown

Summary

Adds bitsandbytes.nn.Experts4bit — a module for storing fused Mixture-of-Experts weights (gate_up_proj + down_proj) in 4-bit NF4/FP4 precision. Each expert is quantized independently with per-expert absmax, and dequantized on-the-fly during the forward pass, keeping the runtime memory footprint small.

Closes #1965

Motivation

transformers v5 stores MoE expert weights as 3D tensors (e.g. [num\_experts, intermediate, hidden]). Linear4bit cannot wrap these because it expects 2D nn.Linear weights. Experts4bit fills this gap.

Design

  • Plain nn.Parameter for packed uint8 weights
  • Per-expert float32 absmax buffers
  • from_float() classmethod for easy construction
  • Per-expert dequant loop
  • Gated activation (SiLU/SwiGLU) support
  • Standard state_dict serialization

Benchmark Results (RTX 3090)

Memory Savings

4-bit uses ~28% of fp16 memory (72% reduction, near the 75% theoretical max).

Quantization Error (NF4)

Metric Value
MAE 0.073
RMSE 0.092

Throughput

Experts Tokens/sec
8 ~5000
64 ~4000

Testing

12 tests covering quant round-trip, shape validation, error cases, forward vs reference, and state_dict round-trip. All pass. No regressions in existing test suites.

ZAID646 and others added 4 commits July 5, 2026 02:06
Implements per-expert block-wise 4-bit quantization for fused MoE
layers (gate_up_proj + down_proj). Each expert is quantized,
stored, and dequantized independently during the forward pass.

- Experts4bit class in bitsandbytes/nn/modules.py
- from_float() classmethod for easy construction from fp16/bf16/fp32
- Per-expert dequant loop with SiLU gated activation support
- Standard state_dict serialization
- Exported via bitsandbytes.nn.Experts4bit
- Tests: quant round-trip, shape validation, error cases,
  forward vs reference, state_dict round-trip (12 tests, all pass)
- Benchmarks: memory, accuracy, throughput on RTX 3090
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.

1 participant