Skip to content

Add a tiny Qwen4 MoE model - #8774

Open
albertvillanova wants to merge 2 commits into
mainfrom
qwen4-tiny
Open

albertvillanova wants to merge 2 commits into
mainfrom
qwen4-tiny

Conversation

@albertvillanova

@albertvillanova albertvillanova commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

This PR adds a generation script for trl-internal-testing/tiny-Qwen4MoeForConditionalGeneration, based on Qwen/Qwen4-35B-A3B, and pins its revision.

Motivation

Qwen4 (qwen4_moe) has no tiny model, so no test runs it. It adds sparse attention with a QSA indexer, a per-layer embedding with a dilated short conv, and an n-gram embedding table to the Qwen3.5 MoE layout. The stacked PR runs the trainer tests on it.

Changes

  • Add qwen4_moe_for_conditional_generation.py, which generates tiny-Qwen4MoeForConditionalGeneration with the processor and generation config of Qwen/Qwen4-35B-A3B. Qwen4 was added in transformers 5.26.0, which the script pins.
  • The root tokenizer of Qwen/Qwen4-35B-A3B is remote code, so the script loads the transformers tokenizer and its chat template from the tokenizer-compat subfolder, and drops the auto_map of the image and video processors.
  • Pin the revision of the generated tiny-Qwen4MoeForConditionalGeneration in MODEL_REVISIONS.

Alignment with the reference

The config diff against Qwen/Qwen4-35B-A3B has 24 keys, all size reductions:

  • text: num_hidden_layers 40 → 4, so the reference pattern (3 linear attention layers, then a sparse attention one) appears once; hidden_size, attention and linear attention heads, experts, hc_lowrank, ple_embed_dim and ngram_vocab_size_base reduced
  • split_ngram_parts 128 → 2, which keeps the n-gram table split into shards
  • indexer_head_dim 128 → 64, the smallest that holds the attention's rotary dims; indexer_budget 8, smaller than the test sequences, so the sparse path drops tokens
  • vision: depth 2, hidden size 16, 2 heads (head size 8, as Flash Attention requires)

Every other key is the same as the reference. All tensors have the reference dtype, and the chat template and generation config are the reference's.

The tiny model was generated with this script at transformers 5.26.0:

python -m scripts.generate_tiny_models.for_conditional_generation.qwen4_moe_for_conditional_generation

Note

Low Risk
Test-only fixture generation and a new pinned Hub revision entry; no production training or runtime logic changes.

Overview
Adds a tiny Qwen4 MoE VLM test fixture by introducing scripts/generate_tiny_models/for_conditional_generation/qwen4_moe_for_conditional_generation.py, which builds trl-internal-testing/tiny-Qwen4MoeForConditionalGeneration from Qwen/Qwen4-35B-A3B with heavily shrunk text/vision configs while preserving Qwen4-specific structure (4 layers with 3 linear + 1 sparse attention, MoE experts, n-gram shards, indexer budget small enough to exercise sparse token dropping). The script requires transformers 5.26.0, loads tokenizer/chat template from tokenizer-compat (avoids remote-code root tokenizer), strips auto_map from image/video processors before assembling Qwen3VLProcessor, then runs the usual smoke/dtype/config checks and Hub push.

Registers the generated Hub artifact in MODEL_REVISIONS in tests/testing_utils.py so CI loads a pinned commit.

Reviewed by Cursor Bugbot for commit dec6999. Bugbot is set up for automated code reviews on this repo. Configure here.

@bot-ci-comment

bot-ci-comment Bot commented Dec 4, 2026

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.

1 participant