-
Notifications
You must be signed in to change notification settings - Fork 979
[models] Upgrade Qwen3.5 implementation to Qwen3.8, supports text-only path #4355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e01d7fd
[models] Upgrade Qwen3.5 implementation to Qwen3.8
wwwjn b2c3374
[models] Keep Qwen3.5 registry alongside Qwen3.8
wwwjn d3610c9
[models] Restore Qwen3.5 implementation ownership
wwwjn b6af2a1
[models] Remove Qwen3.8 numerical script
wwwjn 6bba537
[docs] Trim Qwen3.8 model README
wwwjn 1a82af9
[models] Revert Qwen3.5 cosmetic changes
wwwjn 20c91df
[models] Explain Qwen checkpoint prefix selection
wwwjn c510981
[models] Refine checkpoint prefix comment
wwwjn 19e369d
[rl] Restore Qwen3.5 RL configurations
wwwjn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| from typing import cast | ||
|
|
||
| import pytest | ||
|
|
||
| pytest.importorskip("fla") | ||
|
|
||
| from torchtitan.models.qwen3_5 import model_registry, Qwen35Model, qwen3_5_configs | ||
| from torchtitan.models.qwen3_5.config_registry import qwen35_0_8b, qwen35_27b | ||
| from torchtitan.models.qwen3_8 import model_registry as qwen3_8_model_registry | ||
|
|
||
|
|
||
| def test_qwen35_registry_keeps_released_flavors() -> None: | ||
| assert set(qwen3_5_configs) == { | ||
| "debugmodel", | ||
| "debugmodel_moe", | ||
| "0.8B", | ||
| "2B", | ||
| "4B", | ||
| "9B", | ||
| "27B", | ||
| "35B-A3B", | ||
| "122B-A10B", | ||
| "397B-A17B", | ||
| } | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("flavor", sorted(qwen3_5_configs)) | ||
| def test_qwen35_registry_builds_every_flavor(flavor: str) -> None: | ||
| model_spec = model_registry( | ||
| flavor, | ||
| moe_comm_backend=( | ||
| "standard" if flavor == "debugmodel_moe" or "-A" in flavor else None | ||
| ), | ||
| ) | ||
|
|
||
| assert model_spec.name == "qwen3_5" | ||
| assert model_spec.flavor == flavor | ||
|
|
||
|
|
||
| def test_qwen35_is_the_shared_model_implementation() -> None: | ||
| model_spec = model_registry("0.8B") | ||
| config = cast(Qwen35Model.Config, model_spec.model) | ||
| qwen38_config = qwen3_8_model_registry("27B").model | ||
|
|
||
| assert model_spec.name == "qwen3_5" | ||
| assert model_spec.flavor == "0.8B" | ||
| assert config.dim == 1024 | ||
| assert len(config.layers) == 24 | ||
| assert isinstance(qwen38_config, Qwen35Model.Config) | ||
|
|
||
|
|
||
| def test_qwen35_keeps_small_dense_and_moe_models() -> None: | ||
| dense_config = cast(Qwen35Model.Config, model_registry("0.8B").model) | ||
| moe_config = cast( | ||
| Qwen35Model.Config, | ||
| model_registry("35B-A3B", moe_comm_backend="standard").model, | ||
| ) | ||
|
|
||
| assert dense_config.dim == 1024 | ||
| assert moe_config.dim == 2048 | ||
| assert moe_config.layers[0].moe is not None | ||
| assert moe_config.layers[0].moe.router.num_experts == 256 | ||
| assert moe_config.layers[0].moe.router.top_k == 8 | ||
|
|
||
|
|
||
| def test_qwen35_recipes_keep_versioned_hugging_face_paths() -> None: | ||
| small_config = qwen35_0_8b() | ||
| large_config = qwen35_27b() | ||
|
|
||
| assert small_config.hf_assets_path.endswith("Qwen3.5-0.8B") | ||
| assert small_config.model_spec is not None | ||
| assert small_config.model_spec.name == "qwen3_5" | ||
| assert large_config.hf_assets_path.endswith("Qwen3.5-27B") | ||
| assert large_config.model_spec is not None | ||
| assert large_config.model_spec.name == "qwen3_5" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| from dataclasses import replace | ||
| from typing import cast | ||
|
|
||
| import pytest | ||
| import torch | ||
|
|
||
| pytest.importorskip("fla") | ||
|
|
||
| from torchtitan.models.qwen3_5 import Qwen35Model, Qwen35StateDictAdapter | ||
| from torchtitan.models.qwen3_5.sharding import set_qwen35_sharding_config | ||
| from torchtitan.models.qwen3_8 import model_registry, qwen3_8_configs | ||
| from torchtitan.models.qwen3_8.config_registry import qwen38_27b, qwen38_2_4t_a95b | ||
|
|
||
|
|
||
| def test_qwen38_registry_exposes_only_qwen38_flavors() -> None: | ||
| assert set(qwen3_8_configs) == { | ||
| "debugmodel", | ||
| "debugmodel_moe", | ||
| "27B", | ||
| "2.4T-A95B", | ||
| } | ||
| for legacy_flavor in ( | ||
| "0.8B", | ||
| "2B", | ||
| "4B", | ||
| "9B", | ||
| "35B-A3B", | ||
| "122B-A10B", | ||
| "397B-A17B", | ||
| ): | ||
| with pytest.raises(KeyError): | ||
| model_registry(legacy_flavor) | ||
|
|
||
|
|
||
| def test_qwen38_27b_reuses_qwen35_multimodal_architecture() -> None: | ||
| model_spec = model_registry("27B") | ||
| config = cast(Qwen35Model.Config, model_spec.model) | ||
|
|
||
| assert model_spec.name == "qwen3_8" | ||
| assert config.dim == 5120 | ||
| assert len(config.layers) == 64 | ||
| assert config.vision_encoder is not None | ||
| assert config.vision_encoder.merger.fc2.out_features == 5120 | ||
|
|
||
|
|
||
| def test_qwen38_recipes_use_released_hugging_face_paths() -> None: | ||
| dense_config = qwen38_27b() | ||
| moe_config = qwen38_2_4t_a95b() | ||
|
|
||
| assert dense_config.hf_assets_path.endswith("Qwen3.8-27B") | ||
| assert dense_config.model_spec is not None | ||
| assert dense_config.model_spec.name == "qwen3_8" | ||
| assert moe_config.hf_assets_path.endswith("Qwen3.8-2.4T-A95B") | ||
| assert moe_config.model_spec is not None | ||
| assert moe_config.model_spec.name == "qwen3_8" | ||
|
|
||
|
|
||
| def test_qwen38_2_4t_a95b_matches_hugging_face_config() -> None: | ||
| config = qwen3_8_configs["2.4T-A95B"]( | ||
| attn_backend="flex", | ||
| moe_comm_backend="standard", | ||
| ) | ||
|
|
||
| assert config.dim == 8192 | ||
| assert len(config.layers) == 92 | ||
| assert config.vision_encoder is None | ||
|
|
||
| linear_layer = config.layers[0] | ||
| assert linear_layer.delta_net is not None | ||
| assert linear_layer.delta_net.in_proj_q.out_features == 16 * 128 | ||
| assert linear_layer.delta_net.in_proj_v.out_features == 128 * 128 | ||
|
|
||
| full_attention_layer = config.layers[3] | ||
| assert full_attention_layer.attention is not None | ||
| assert full_attention_layer.attention.n_heads == 64 | ||
| assert full_attention_layer.attention.n_kv_heads == 4 | ||
|
|
||
| assert linear_layer.moe is not None | ||
| assert linear_layer.moe.router.num_experts == 512 | ||
| assert linear_layer.moe.router.top_k == 10 | ||
|
|
||
|
|
||
| def test_text_only_qwen38_sharding_does_not_require_vision() -> None: | ||
| config = qwen3_8_configs["2.4T-A95B"]( | ||
| attn_backend="flex", | ||
| moe_comm_backend="standard", | ||
| ) | ||
|
|
||
| set_qwen35_sharding_config(config, enable_sp=True, enable_ep=True) | ||
|
|
||
| assert config.tok_embeddings.sharding_config is not None | ||
| assert config.layers[0].sharding_config is not None | ||
|
|
||
|
|
||
| def test_shared_model_builds_without_vision_encoder() -> None: | ||
| config = qwen3_8_configs["debugmodel"](attn_backend="flex") | ||
| config = replace( | ||
| config, | ||
| vocab_size=128, | ||
| tok_embeddings=replace(config.tok_embeddings, num_embeddings=128), | ||
| lm_head=replace(config.lm_head, out_features=128), | ||
| vision_encoder=None, | ||
| ) | ||
|
|
||
| model = config.build() | ||
|
|
||
| assert model.vision_encoder is None | ||
|
|
||
|
|
||
| def test_text_only_checkpoint_adapter_uses_model_prefix() -> None: | ||
| config = qwen3_8_configs["2.4T-A95B"]( | ||
| attn_backend="flex", | ||
| moe_comm_backend="standard", | ||
| ) | ||
| adapter = Qwen35StateDictAdapter(config, hf_assets_path=None) | ||
| embedding = torch.randn(2, 3) | ||
| lm_head = torch.randn(2, 3) | ||
|
|
||
| converted = adapter.from_hf( | ||
| { | ||
| "model.embed_tokens.weight": embedding, | ||
| "lm_head.weight": lm_head, | ||
| } | ||
| ) | ||
| assert set(converted) == {"tok_embeddings.weight", "lm_head.weight"} | ||
| torch.testing.assert_close(converted["tok_embeddings.weight"], embedding) | ||
| torch.testing.assert_close(converted["lm_head.weight"], lm_head) | ||
|
|
||
| restored = adapter.to_hf(converted) | ||
| assert set(restored) == {"model.embed_tokens.weight", "lm_head.weight"} | ||
| torch.testing.assert_close(restored["model.embed_tokens.weight"], embedding) | ||
| torch.testing.assert_close(restored["lm_head.weight"], lm_head) | ||
|
|
||
|
|
||
| def test_multimodal_checkpoint_adapter_keeps_language_model_prefix() -> None: | ||
| config = qwen3_8_configs["27B"](attn_backend="flex") | ||
| adapter = Qwen35StateDictAdapter(config, hf_assets_path=None) | ||
| embedding = torch.randn(2, 3) | ||
| lm_head = torch.randn(2, 3) | ||
|
|
||
| converted = adapter.from_hf( | ||
| { | ||
| "model.language_model.embed_tokens.weight": embedding, | ||
| "lm_head.weight": lm_head, | ||
| } | ||
| ) | ||
| restored = adapter.to_hf(converted) | ||
|
|
||
| assert "model.language_model.embed_tokens.weight" in restored | ||
| assert "model.embed_tokens.weight" not in restored | ||
|
|
||
|
|
||
| def test_text_only_checkpoint_adapter_converts_fused_deltanet_qkv() -> None: | ||
| config = qwen3_8_configs["2.4T-A95B"]( | ||
| attn_backend="flex", | ||
| moe_comm_backend="standard", | ||
| ) | ||
| adapter = Qwen35StateDictAdapter(config, hf_assets_path=None) | ||
| delta_net = config.layers[0].delta_net | ||
| assert delta_net is not None | ||
| key_dim = delta_net.in_proj_q.out_features | ||
| value_dim = delta_net.in_proj_v.out_features | ||
| fused_qkv = torch.randn(key_dim * 2 + value_dim, 1) | ||
|
|
||
| converted = adapter.from_hf( | ||
| { | ||
| "model.layers.0.linear_attn.in_proj_qkv.weight": fused_qkv, | ||
| "lm_head.weight": torch.randn(2, 3), | ||
| } | ||
| ) | ||
| assert set(converted) == { | ||
| "layers.0.attn.in_proj_q.weight", | ||
| "layers.0.attn.in_proj_k.weight", | ||
| "layers.0.attn.in_proj_v.weight", | ||
| "lm_head.weight", | ||
| } | ||
|
|
||
| restored = adapter.to_hf(converted) | ||
| torch.testing.assert_close( | ||
| restored["model.layers.0.linear_attn.in_proj_qkv.weight"], | ||
| fused_qkv, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,5 +15,6 @@ | |
| "muse_glimmer", | ||
| "qwen3", | ||
| "qwen3_5", | ||
| "qwen3_8", | ||
| ] | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Qwen3.8-2.4T-A95B model is text only so we want to allow None vision_encoder