-
Notifications
You must be signed in to change notification settings - Fork 999
[Feat] Support T5 Tensor Parallelism #1881
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
+570
−6
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3353237
add tp T5
yuanheng-zhao f6d6210
apply in pipelien flux
yuanheng-zhao b8b75e5
upd
yuanheng-zhao e6f5f30
upd T5 model
yuanheng-zhao db4d3ec
upd usage
yuanheng-zhao 96d9739
add unit tests for T5 tp
yuanheng-zhao a224b96
revert glm-image T5-tp usage
yuanheng-zhao 1bf1912
apply model level load_weights
yuanheng-zhao 49dfcce
use vllm RMSNorm
yuanheng-zhao 955fbb3
Merge main
yuanheng-zhao e5400f6
trim L1 tests
yuanheng-zhao 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
161 changes: 161 additions & 0 deletions
161
tests/diffusion/models/t5_encoder/test_t5_encoder_tp.py
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,161 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
| import pytest | ||
| import torch | ||
| from transformers import T5Config | ||
| from vllm.config import DeviceConfig, VllmConfig, set_current_vllm_config | ||
|
|
||
| from vllm_omni.diffusion.models.t5_encoder.t5_encoder import ( | ||
| T5EncoderModel, | ||
| T5SelfAttention, | ||
| ) | ||
|
|
||
| pytestmark = [pytest.mark.core_model, pytest.mark.cpu] | ||
|
|
||
| _T5_MODULE = "vllm_omni.diffusion.models.t5_encoder.t5_encoder" | ||
|
|
||
| SMALL_T5_CONFIG = dict( | ||
| d_model=64, | ||
| d_kv=8, | ||
| d_ff=128, | ||
| num_heads=8, | ||
| num_layers=2, | ||
| vocab_size=256, | ||
| relative_attention_num_buckets=32, | ||
| relative_attention_max_distance=128, | ||
| is_gated_act=True, | ||
| dense_act_fn="gelu_new", | ||
| layer_norm_epsilon=1e-6, | ||
| feed_forward_proj="gated-gelu", | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def t5_config() -> T5Config: | ||
| return T5Config(**SMALL_T5_CONFIG) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="function", autouse=True) | ||
| def setup_tp_group(monkeypatch, mocker): | ||
| """Set up TP=2, rank=0, VllmConfig, and mock activation for all tests.""" | ||
| device_config = DeviceConfig(device="cpu") | ||
|
|
||
| # TP world size | ||
| monkeypatch.setattr("vllm.model_executor.layers.linear.get_tensor_model_parallel_world_size", lambda: 2) | ||
| monkeypatch.setattr(f"{_T5_MODULE}.get_tensor_model_parallel_world_size", lambda: 2) | ||
| monkeypatch.setattr( | ||
| "vllm.model_executor.layers.vocab_parallel_embedding.get_tensor_model_parallel_world_size", | ||
| lambda: 2, | ||
| ) | ||
|
|
||
| monkeypatch.setattr(f"{_T5_MODULE}.get_tensor_model_parallel_rank", lambda: 0) | ||
| monkeypatch.setattr( | ||
| "vllm.model_executor.layers.vocab_parallel_embedding.get_tensor_model_parallel_rank", | ||
| lambda: 0, | ||
| ) | ||
|
|
||
| # TP group | ||
| mock_tp_group = mocker.MagicMock() | ||
| mock_tp_group.world_size = 2 | ||
| mocker.patch("vllm.distributed.parallel_state.get_tp_group", return_value=mock_tp_group) | ||
|
|
||
| monkeypatch.setattr(f"{_T5_MODULE}.get_act_fn", lambda _: torch.nn.GELU()) | ||
|
|
||
| with set_current_vllm_config(VllmConfig(device_config=device_config)): | ||
| yield | ||
|
|
||
|
|
||
| class TestRelativePositionBiasTPSlicing: | ||
| """Verify compute_bias slices heads correctly per TP rank.""" | ||
|
|
||
| def test_compute_bias_shape(self, t5_config): | ||
| attn = T5SelfAttention(t5_config, has_relative_attention_bias=True) | ||
|
|
||
| seq_len = 6 | ||
| bias = attn.compute_bias(seq_len, seq_len, device=torch.device("cpu")) | ||
|
|
||
| local_heads = t5_config.num_heads // 2 | ||
| assert bias.shape == (1, local_heads, seq_len, seq_len) | ||
|
|
||
| def test_all_ranks_cover_all_heads(self, t5_config, monkeypatch): | ||
| seq_len = 4 | ||
|
|
||
| biases = [] | ||
| ref_weight = None | ||
| for rank in range(2): | ||
| monkeypatch.setattr(f"{_T5_MODULE}.get_tensor_model_parallel_rank", lambda r=rank: r) | ||
| attn = T5SelfAttention(t5_config, has_relative_attention_bias=True) | ||
| if rank > 0: | ||
| attn.relative_attention_bias.weight.data.copy_(ref_weight) | ||
| else: | ||
| ref_weight = attn.relative_attention_bias.weight.data.clone() | ||
| biases.append(attn.compute_bias(seq_len, seq_len, device=torch.device("cpu"))) | ||
|
|
||
| full_bias = torch.cat(biases, dim=1) | ||
| assert full_bias.shape == (1, t5_config.num_heads, seq_len, seq_len) | ||
|
|
||
|
|
||
| class TestT5EncoderModelWeightLoading: | ||
| """Test weight loading at the top-level T5EncoderModel.""" | ||
|
|
||
| def test_model_instantiation(self, t5_config): | ||
| model = T5EncoderModel(t5_config, prefix="text_encoder") | ||
|
|
||
| assert model.config is t5_config | ||
| assert model.encoder is not None | ||
| assert len(model.encoder.block) == t5_config.num_layers | ||
|
|
||
| def test_embedding_shape(self, t5_config): | ||
| model = T5EncoderModel(t5_config, prefix="text_encoder") | ||
|
|
||
| assert model.shared.embedding_dim == t5_config.d_model | ||
|
|
||
| def test_embed_input_ids(self, t5_config, monkeypatch): | ||
| # Verify method and output shape | ||
| model = T5EncoderModel(t5_config, prefix="text_encoder") | ||
|
|
||
| # Mock all-reduce to be identity (no actual TP communication) | ||
| monkeypatch.setattr( | ||
| "vllm.model_executor.layers.vocab_parallel_embedding.tensor_model_parallel_all_reduce", | ||
| lambda x: x, | ||
| ) | ||
|
|
||
| input_ids = torch.randint(0, t5_config.vocab_size, (2, 8)) | ||
| embeddings = model.embed_input_ids(input_ids) | ||
|
|
||
| assert embeddings.shape == (2, 8, t5_config.d_model) | ||
|
|
||
| def test_qkv_weights_loaded_through_blocks(self): | ||
| # Verify that HF-style separate Q/K/V weights can be loaded through | ||
| # the block hierarchy | ||
| config = T5Config(**{**SMALL_T5_CONFIG, "num_layers": 1}) | ||
| model = T5EncoderModel(config, prefix="text_encoder") | ||
|
|
||
| inner_dim = config.num_heads * config.d_kv | ||
| prefix = "encoder.block.0.layer.0.SelfAttention." | ||
|
|
||
| loaded = model.load_weights( | ||
| [ | ||
| (prefix + "q.weight", torch.randn(inner_dim, config.d_model)), | ||
| (prefix + "k.weight", torch.randn(inner_dim, config.d_model)), | ||
| (prefix + "v.weight", torch.randn(inner_dim, config.d_model)), | ||
| ] | ||
| ) | ||
|
|
||
| assert len(loaded) > 0 | ||
| attn = model.encoder.block[0].layer[0].SelfAttention | ||
| expected_qkv_dim = 3 * (config.num_heads // 2) * config.d_kv | ||
| assert attn.qkv_proj.weight.shape == (expected_qkv_dim, config.d_model) | ||
|
|
||
|
|
||
| class TestTPConstraints: | ||
| """Verify that invalid TP configurations raise clear errors.""" | ||
|
|
||
| def test_num_heads_not_divisible_by_tp(self): | ||
| config = T5Config(**{**SMALL_T5_CONFIG, "num_heads": 7}) | ||
| with pytest.raises(AssertionError, match=r"num_heads.*must be divisible by tp_size"): | ||
| T5SelfAttention(config) | ||
|
|
||
| def test_num_heads_divisible_by_tp(self, t5_config): | ||
| attn = T5SelfAttention(t5_config) | ||
| assert attn.n_heads_per_partition == 4 |
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,7 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
| """Tensor-parallel T5 encoder model.""" | ||
|
|
||
| from vllm_omni.diffusion.models.t5_encoder.t5_encoder import T5EncoderModel | ||
|
|
||
| __all__ = ["T5EncoderModel"] |
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.
@congw729 I'm not sure whether we need unit test for this module? I remember in vLLM, we don't have unit test for some specific module like
siglip.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.
I think it's okay to have this test.
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.
Thanks, could you please double-check to make sure this unit test meets the specifications?
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.
LGTM. The test cases cover most scenarios, the marks are correctly labeled, and the time cost is also within tolerance.