Skip to content

[qwen3.5] Tie embeddings for the 0.8B, 2B, and 4B flavors - #4916

Merged
wwwjn merged 2 commits into
pytorch:mainfrom
wwwjn:qwen35-tie-small-embeddings
Sep 28, 2026
Merged

wwwjn merged 2 commits into
pytorch:mainfrom
wwwjn:qwen35-tie-small-embeddings

Conversation

@wwwjn

@wwwjn wwwjn commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The Hugging Face Qwen3.5-0.8B, 2B, and 4B checkpoints set tie_word_embeddings=true and store no lm_head.weight. The torchtitan configs for these flavors kept a separate lm_head, so loading one of these checkpoints failed:

RuntimeError: Missing key in checkpoint state_dict: lm_head.weight.

This PR enables enable_weight_tying=True for the 0.8B, 2B, and 4B flavors, as the Qwen3 small flavors already do, and replaces the stale "torchtitan doesn't support tied embeddings yet" notes. No other code changes are needed: Decoder already ties tok_embeddings to lm_head, and the Qwen3.5 state dict adapter already restores lm_head from embed_tokens on load and drops it on save.

The 9B and larger HF checkpoints do not tie embeddings and are unchanged.

Why

The HF tied flavors could not be loaded through the HF checkpoint path (initial_load_in_hf, convert_from_hf.py). For example, the RL GDN full-cudagraph test (test_gdn_full_runner_matches_eager) builds a vLLM engine from a Qwen3.5-0.8B checkpoint. CI does not provide one and skips the test; pointed at the official checkpoint, it failed for this reason.

Affected configs

  • qwen3_5 model registry flavors 0.8B, 2B, 4B, and the recipes in torchtitan/models/qwen3_5/config_registry.py that use them.
  • None of these use pipeline parallelism, which weight tying does not support.
  • The Qwen3.5 RL recipes use debugmodel, 9B, and 27B and are unaffected.

Test plan

Qwen3.5-0.8B, official HF checkpoint (Qwen/Qwen3.5-0.8B):

  • scripts/checkpoint_conversion/convert_from_hf.py --model_name qwen3_5 --model_flavor 0.8B:
    • before: fails with Missing key in checkpoint state_dict: lm_head.weight;
    • after: succeeds.
  • Converted checkpoint vs. HF weights:
    • tok_embeddings.weight and lm_head.weight are both bitwise equal to HF model.language_model.embed_tokens.weight;
    • to_hf round trip reproduces the HF key set exactly (no extra lm_head.weight), with all 473 tensors bitwise equal. The 15 HF mtp.* (multi-token prediction) weights are not part of the torchtitan model, before or after this change.
  • tests/unit_tests/rl/test_vllm_wrapper.py::test_hf_adapter_restores_local_shards builds a partial Qwen3.5-0.8B state dict with lm_head.weight but no tok_embeddings.weight; with tying, to_hf drops lm_head and from_hf found neither key. The test checks TP shard restoration, not tying, so it now unties its config.
  • pytest tests/unit_tests/rl/: 439 passed, 0 failed.
  • pytest tests/unit_tests/cpu/test_qwen3_5.py tests/unit_tests/cpu/test_weight_tying.py: 27 passed. The same 4 test_qwen35_shared_expert_uses_explicit_tp_boundaries cases fail with and without this change in the local environment.

Note: scripts/checkpoint_conversion/numerical_tests_qwen3_5.py no longer runs against main (the model and collator moved to packed inputs), so a logit comparison against HF is not included here.

The Hugging Face Qwen3.5-0.8B, 2B, and 4B checkpoints set
tie_word_embeddings=true and store no lm_head.weight. Their torchtitan
configs kept a separate lm_head, so loading those checkpoints failed with
"Missing key in checkpoint state_dict: lm_head.weight". Enable weight tying
for these flavors, as the Qwen3 small flavors do; the Qwen3.5 state dict
adapter already restores lm_head from embed_tokens on load and drops it on
save. The 9B and larger checkpoints do not tie embeddings and are unchanged.
@pytorch-bot pytorch-bot Bot added ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl labels Sep 28, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 28, 2026
@wwwjn
wwwjn requested a review from shuhuayu September 28, 2026 00:27
test_hf_adapter_restores_local_shards builds a partial state dict that
carries lm_head.weight but not tok_embeddings.weight. With Qwen3.5-0.8B now
tying embeddings, to_hf drops lm_head and from_hf finds neither key. The test
checks TP shard restoration, not tying, so untie its config.
@wwwjn
wwwjn merged commit e8f5326 into pytorch:main Sep 28, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants