Skip to content

fast_inference for Qwen3.5 / 3.6 MoE and Gemma-4 MoE with LoRA on the experts - #12742

Merged
danielhanchen merged 12 commits into
mainfrom
moe-vllm-qwen36
Oct 6, 2026
Merged

danielhanchen merged 12 commits into
mainfrom
moe-vllm-qwen36

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Lets fast_inference = True (vLLM) load Qwen3.5 / 3.6 MoE (qwen3_5_moe) and Gemma-4 MoE (gemma4, e.g. gemma-4-26B-A4B) with LoRA on the expert layers. Main refuses both at the allowlist:

RuntimeError: Unsloth: Fast inference is only supported for Language models and these vision model types:
qwen2_5_vl, gemma3, mistral3, qwen3_vl, qwen3_vl_moe, qwen3_5, idefics3. Found architectures: siglip, qwen3_5_moe!

The weight sharing itself (vLLM and the training model holding ONE copy of the experts) is in unslothai/unsloth-zoo#1573, which this needs first. Evidence for the whole feature, including both models sharing every expert tensor with vLLM, is in that PR.

What changes

  1. Allowlist qwen3_5_moe, gemma4 and gemma4_text. The gate is exact membership, so the existing qwen3_5 entry does not cover the MoE checkpoints.

  2. Dense Gemma-4 stays refused. gemma4 is also the model type of the dense checkpoints (E2B, E4B, 31B), which main refuses cleanly and which are not served yet. Measured on gemma-4-E2B-it, vLLM 0.29.0:

    • 16-bit: vLLM aborts the whole Python process profiling the audio encoder, terminate called ... Cannot call numel() on tensor with symbolic sizes/strides, exit 250, with no exception to catch.
    • 4-bit with vllm-bnb-plugin: ModuleNotFoundError: No module named 'vllm.model_executor.model_loader.bitsandbytes_loader'.

    So gemma4 passes the gate only when its text config declares experts (num_experts / enable_moe_block). Dense Gemma-4 now gets, in about 3 seconds and before vLLM loads anything: fast_inference = True is only supported for the MoE gemma4 checkpoints (such as gemma-4-26B-A4B), not the dense ones yet. Please set fast_inference = False.

  3. Older unsloth_zoo is refused clearly, "needs a newer unsloth_zoo", the same pattern main uses for Idefics3. Without it the experts would be 1-wide placeholders in the training model.

  4. 4-bit is refused before vLLM loads for these MoE models. vLLM packs bitsandbytes experts into a blob the training model cannot alias and does not serve LoRA on bitsandbytes experts; before this, Qwen3.6 with the plugin crashed inside vLLM's loader with output with shape [512, 1] doesn't match the broadcast shape [512, 2048]. The same _is_sparse_moe_config helper drives both this refusal and the dense Gemma-4 one, so the two cannot drift apart.

Evidence

B200, vLLM 0.29.0, transformers 5.17.0. Base = main, head = this branch with unslothai/unsloth-zoo#1573.

Model base head
Qwen3.6-35B-A3B, 16-bit refused at the allowlist loads; 280 / 0 expert tensors shared with vLLM; 3 GRPO steps
gemma-4-26B-A4B-it, 16-bit refused at the allowlist loads; 150 / 0 shared; 3 GRPO steps
gemma-4-26B-A4B-it, after the dense refusal commit still passes; 150 / 0 shared; one-step loss and expert grad norm identical to before it (8.62079, 7.099221)
gemma-4-E2B-it (dense), 16-bit refused at the allowlist refused at the gate, clear message
gemma-4-E2B-it (dense), 4-bit with plugin refused at the allowlist refused at the gate, clear message
Qwen3.6 / Gemma-4 MoE, 4-bit, with and without plugin refused at the allowlist clear NotImplementedError before vLLM loads
Qwen3.5-2B (dense, already supported) works identical sharing census and check results

Tests: new tests/test_moe_fast_inference_gate.py. With the Idefics3 registration and compile-decode selection suites, 39 passed. Removing the dense Gemma-4 refusal makes test_dense_gemma4_is_refused_at_the_gate fail.

Not verified

vLLM versions other than 0.29.0; GPUs other than B200; dense Gemma-4 support itself, which this deliberately keeps refused rather than fixes.

shimmyshimmer and others added 5 commits October 5, 2026 05:43
The MoE checkpoints report model_type qwen3_5_moe, and the gate is exact membership against
VLLM_SUPPORTED_VLM, so the existing qwen3_5 entry never matched them and fast_inference = True
was refused before any of the MoE work could run.
Gemma-4 ships as Gemma4ForConditionalGeneration with a vision_config, so even the text
MoE checkpoints hit the vision gate and were refused before vLLM ever saw them. The gate
is exact membership, so gemma4_text does not come along with gemma4.

With the matching unsloth_zoo changes, gemma-4-26B-A4B-it loads under fast_inference and
shares all 150 of its MoE parameters with vLLM by storage identity.
… experts

qwen3_5_moe and gemma4 pass the vLLM allowlist, but only an unsloth_zoo that ships
extract_moe_layers can alias their experts into the training model. An older one builds
the model with 1-wide placeholder experts and fails much later, or not at all, so say
which package to upgrade instead, as Idefics3 already does.
…ading vLLM

vLLM packs bitsandbytes MoE experts into a blob the training model cannot alias, and does
not serve LoRA on bitsandbytes MoE experts. With the vllm-bnb-plugin installed,
Qwen3.6-35B-A3B load_in_4bit then died inside vLLM's loader with "output with shape
[512, 1] doesn't match the broadcast shape [512, 2048]", after quantizing the whole
checkpoint. Say what is unsupported and what to use instead, before any weights load.
Adding gemma4 / gemma4_text to the allowlist also admitted the dense Gemma-4 checkpoints
(E2B, E4B, 31B), which share the model type and which main refused cleanly. They are not
served yet, and measured on gemma-4-E2B-it with vLLM 0.29.0:

- 16-bit: vLLM aborts the whole process while profiling the audio encoder,
  `terminate called ... Cannot call numel() on tensor with symbolic sizes/strides`, exit 250,
  with no Python exception to catch.
- 4-bit with vllm-bnb-plugin: `ModuleNotFoundError: No module named
  'vllm.model_executor.model_loader.bitsandbytes_loader'` from
  patch_gemma4_vllm_k_eq_v_support, which imports a loader vLLM >= 0.28 moved out of tree.

So gemma4 now passes the gate only when its text config declares experts
(num_experts / enable_moe_block). _is_sparse_moe_config is shared with the 4-bit refusal
below so the two tests cannot drift apart. Qwen3.5 MoE has its own model type and is
unaffected; dense Qwen3.5 keeps its existing entry.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-06T02:34:07.865929Z 93059d7 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 5, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bdeb1fe193

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread unsloth/models/vision.py Outdated
"checkpoints (such as gemma-4-26B-A4B), not the dense ones yet. "
"Please set fast_inference = False."
)
if any(arch in VLLM_ZOO_MOE_VLM for arch in model_types) and not _zoo_supports_moe_fast_inference():

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Apply the zoo version gate to text-only MoE loads

When text_only=True selects the standalone decoder, the code deliberately sets is_vlm_config to false, so execution never reaches this check even though model_types still identifies qwen3_5_moe or gemma4_text. A 16-bit text-only load with an older unsloth_zoo therefore proceeds with the one-wide placeholder experts this guard is meant to reject, potentially failing later or producing an invalid training model. Move this compatibility check outside the vision-only block so it also covers text-only MoE decoders.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, fixed in 7543229. The unsloth_zoo version check now sits outside the is_vlm_config block, keyed on fast_inference, the MoE model types and _is_sparse_moe_config(auto_config), so a text_only = True load of a Qwen3.5 / Qwen3.6 MoE or Gemma-4 MoE decoder is refused with an older unsloth_zoo as well. The sparse check keeps dense Gemma-4 text_only loads out of it. test_the_zoo_gate_covers_text_only_moe_loads evaluates the gate's condition for the text config and fails on the previous commit.

Comment thread unsloth/models/vision.py
Comment on lines +3662 to +3665
if (
load_in_4bit
and any(arch in VLLM_ZOO_MOE_VLM for arch in model_types)
and _is_sparse_moe_config(model_config)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Reject prequantized BnB MoE checkpoints too

This refusal only examines the caller's effective load_in_4bit flag. If a sparse MoE checkpoint already has a bitsandbytes quantization config and the caller passes load_in_4bit=False (for example alongside load_in_16bit=True), load_vllm still selects its bitsandbytes loader from model_config, but this condition is skipped. That sends the unsupported packed experts into the exact late vLLM failure this guard intends to prevent; include the config's quantization method when deciding whether the load is BnB.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, fixed in 7543229. The refusal now uses the same test load_vllm uses to pick its bitsandbytes loader: load_in_4bit, a -bnb-4bit model name, or quant_method == "bitsandbytes" in the config. A prequantized bnb MoE checkpoint loaded with load_in_4bit = False is refused before vLLM starts, while 16-bit, FP8 and dense checkpoints are unaffected. Covered by test_prequantized_bnb_moe_checkpoints_are_refused_without_load_in_4bit, which fails on the previous commit.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: af77138c65

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread unsloth/models/vision.py Outdated
Comment on lines +2937 to +2939
if any(arch in VLLM_MOE_ONLY_VLM for arch in model_types) and not _is_sparse_moe_config(
auto_config
):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Apply the dense Gemma-4 guard to text-only loads

When a dense Gemma-4 checkpoint is loaded with text_only=True, the loader replaces the parent config with gemma4_text and sets is_vlm_config to false, so this guard is skipped even though model_types still matches VLLM_MOE_ONLY_VLM. The unsupported dense decoder then proceeds into the vLLM path instead of receiving the intended early refusal. Move this MoE-only check outside the is_vlm_config block, while retaining the fast_inference condition, so dense text-only loads are rejected too.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, fixed in e92d52d. The dense Gemma-4 check now sits outside the is_vlm_config block next to the unsloth_zoo version check, keyed on fast_inference, so a text_only = True load of a dense Gemma-4 is refused up front as well; MoE text_only loads still pass. For reference, on main that load already failed, just later and less clearly: gemma-4-E2B-it with text_only = True, fast_inference = True (vLLM 0.29.0, transformers 5.17) dies with AmbiguousGlobalPerLayerAttributeError: 'head_dim' is a per-layer attribute. The test now evaluates the gate for the text config and asserts it is not nested under is_vlm_config; it fails on the previous commit.

Comment thread unsloth/models/vision.py
# Mirrors load_vllm's bnb loader test, so prequantized bnb-4bit is refused too.
if (
(
load_in_4bit

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Reject 8-bit BnB MoE loads before invoking vLLM

When one of the newly admitted sparse MoE models is requested with load_in_8bit=True, that flag survives validation but this refusal only tests load_in_4bit. The subsequent load_vllm call has no 8-bit argument and passes use_bitsandbytes=load_in_4bit, so the checkpoint is loaded at full precision rather than in the requested 8-bit mode; for these 26B/35B models this can unexpectedly double memory use and OOM. Reject load_in_8bit here as well unless the vLLM path actually implements BnB 8-bit loading.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, fixed in e92d52d. load_vllm has no 8-bit mode, so load_in_8bit = True is now refused for the sparse MoE models in the same check as the bitsandbytes 4-bit cases, with the message pointing to a 16-bit load. Covered by a new case in test_bnb_moe_loads_are_refused_without_load_in_4bit, which fails with the load_in_8bit term removed. Dense models are unchanged here; load_in_8bit with fast_inference on them is outside this PR.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: e92d52de1b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 93059d782b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants