-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[0.9.1] Add LMhead TP communication groups. #1956
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
10 commits
Select commit
Hold shift + click to select a range
ed2fa49
support lmhead tp
4434cc0
bugfix
1eccd75
fix mtp
1596ee1
mv back to ops
f4d68ef
rename
d9a4c9e
fix lint
ff59d08
modify assertion message
b13aeb1
avoid initialization of lmhead_tp for non-deepseek models
8adbdb1
fix lint
c339f42
fix
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # | ||
| # Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # This file is a part of the vllm-ascend project. | ||
| # | ||
|
|
||
| from vllm.distributed.parallel_state import ( | ||
| get_dp_group, get_tensor_model_parallel_world_size) | ||
|
|
||
| from vllm_ascend.distributed.parallel_state import get_lmhead_group | ||
|
|
||
|
|
||
| def is_lmhead_tp(): | ||
| # We only activate optimization of lmhead communication | ||
| # when tp_size == 1, dp_size > 1 and lmhead_tp_size > 1. | ||
|
|
||
| try: | ||
| get_lmhead_group() | ||
| except AssertionError: | ||
| return False | ||
|
|
||
| tp_size = get_tensor_model_parallel_world_size() | ||
| dp_size = get_dp_group().world_size | ||
| lmhead_tp_size = get_lmhead_group().world_size | ||
|
|
||
| return tp_size == 1 and dp_size > 1 and lmhead_tp_size > 1 |
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,150 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. | ||
| # Copyright 2023 The vLLM team. | ||
| # | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # Adapted from vllm/model_executor/layers/lmhead.py | ||
| # This file is a part of the vllm-ascend project. | ||
|
|
||
| from typing import Optional | ||
|
|
||
| import torch | ||
| from torch.nn.parameter import Parameter | ||
| from vllm.distributed import divide | ||
| from vllm.model_executor.layers.quantization.base_config import ( | ||
| QuantizationConfig, QuantizeMethodBase, method_has_implemented_embedding) | ||
| from vllm.model_executor.layers.vocab_parallel_embedding import ( | ||
| UnquantizedEmbeddingMethod, VocabParallelEmbedding, pad_vocab_size) | ||
| from vllm.model_executor.utils import set_weight_attrs | ||
|
|
||
| from vllm_ascend.distributed.parallel_state import get_lmhead_group | ||
|
|
||
| DEFAULT_VOCAB_PADDING_SIZE = 64 | ||
|
|
||
|
|
||
| class CustomParallelLMHead(VocabParallelEmbedding): | ||
| """Parallelized LM head. | ||
|
|
||
| Output logits weight matrices used in the Sampler. The weight and bias | ||
| tensors are padded to make sure they are divisible by the number of | ||
| model parallel GPUs. | ||
|
|
||
| Args: | ||
| num_embeddings: vocabulary size. | ||
| embedding_dim: size of hidden state. | ||
| bias: whether to use bias. | ||
| params_dtype: type of the parameters. | ||
| org_num_embeddings: original vocabulary size (without LoRA). | ||
| padding_size: padding size for the vocabulary. | ||
| """ | ||
|
|
||
| def __init__(self, | ||
| num_embeddings: int, | ||
| embedding_dim: int, | ||
| bias: bool = False, | ||
| params_dtype: Optional[torch.dtype] = None, | ||
| org_num_embeddings: Optional[int] = None, | ||
| padding_size: int = DEFAULT_VOCAB_PADDING_SIZE, | ||
| quant_config: Optional[QuantizationConfig] = None, | ||
| prefix: str = ""): | ||
| super().__init__(num_embeddings, embedding_dim, params_dtype, | ||
| org_num_embeddings, padding_size, quant_config, | ||
| prefix) | ||
| # Keep the input dimensions. | ||
| tp_rank = get_lmhead_group().rank_in_group | ||
| self.tp_size = get_lmhead_group().world_size | ||
| self.num_embeddings = num_embeddings | ||
| self.padding_size = padding_size | ||
| self.org_vocab_size = org_num_embeddings or num_embeddings | ||
| num_added_embeddings = num_embeddings - self.org_vocab_size | ||
| self.org_vocab_size_padded = pad_vocab_size(self.org_vocab_size, | ||
| self.padding_size) | ||
| self.num_embeddings_padded = pad_vocab_size( | ||
| self.org_vocab_size_padded + num_added_embeddings, | ||
| self.padding_size) | ||
| assert self.org_vocab_size_padded <= self.num_embeddings_padded | ||
|
|
||
| self.shard_indices = self._get_indices(self.num_embeddings_padded, | ||
| self.org_vocab_size_padded, | ||
| self.num_embeddings, | ||
| self.org_vocab_size, tp_rank, | ||
| self.tp_size) | ||
| self.embedding_dim = embedding_dim | ||
|
|
||
| quant_method = None | ||
| if quant_config is not None: | ||
| quant_method = quant_config.get_quant_method(self, prefix=prefix) | ||
| if quant_method is None: | ||
| quant_method = UnquantizedEmbeddingMethod() | ||
|
|
||
| # If we are making an embedding layer, then our quantization linear | ||
| # method must implement the embedding operation. If we are another | ||
| # layer type like ParallelLMHead, this is not important. | ||
| is_embedding_layer = type(self) is VocabParallelEmbedding | ||
| quant_method_implements_embedding = method_has_implemented_embedding( | ||
| type(quant_method)) | ||
| if is_embedding_layer and not quant_method_implements_embedding: | ||
| raise NotImplementedError( | ||
| f"The class {type(quant_method).__name__} must implement " | ||
| "the 'embedding' method, see UnquantizedEmbeddingMethod.") | ||
|
|
||
| self.quant_method: QuantizeMethodBase = quant_method | ||
|
|
||
| if params_dtype is None: | ||
| params_dtype = torch.get_default_dtype() | ||
| # Divide the weight matrix along the vocaburaly dimension. | ||
| self.num_added_embeddings = self.num_embeddings - self.org_vocab_size | ||
| self.num_embeddings_per_partition = divide(self.num_embeddings_padded, | ||
| self.tp_size) | ||
| assert (self.shard_indices.num_elements_padded == | ||
| self.num_embeddings_per_partition) | ||
| self.num_org_embeddings_per_partition = ( | ||
| self.shard_indices.org_vocab_end_index - | ||
| self.shard_indices.org_vocab_start_index) | ||
| self.num_added_embeddings_per_partition = ( | ||
| self.shard_indices.added_vocab_end_index - | ||
| self.shard_indices.added_vocab_start_index) | ||
|
|
||
| self.quant_method.create_weights(self, | ||
| self.embedding_dim, | ||
| [self.num_embeddings_per_partition], | ||
| self.embedding_dim, | ||
| self.num_embeddings_padded, | ||
| params_dtype=params_dtype, | ||
| weight_loader=self.weight_loader) | ||
|
|
||
| self.quant_config = quant_config | ||
| if bias: | ||
| self.bias = Parameter( | ||
| torch.empty(self.num_embeddings_per_partition, | ||
| dtype=params_dtype)) | ||
| set_weight_attrs(self.bias, { | ||
| "output_dim": 0, | ||
| "weight_loader": self.weight_loader, | ||
| }) | ||
| else: | ||
| self.register_parameter("bias", None) | ||
|
|
||
| def tie_weights(self, embed_tokens: VocabParallelEmbedding): | ||
| """Tie the weights with word embeddings.""" | ||
| # GGUF quantized embed_tokens. | ||
| if self.quant_config and self.quant_config.get_name() == "gguf": | ||
| return embed_tokens | ||
| else: | ||
| self.weight = embed_tokens.weight | ||
| return self | ||
|
|
||
| def forward(self, input_): | ||
| del input_ | ||
| raise RuntimeError("LMHead's weights should be used in the sampler.") |
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.
it's better that the default value is 1