-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[Feat] flashcomm2+oshard Generalized #4723
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
56c288d
flashcomm2+oshard Generalized
90b2bf4
fix get vllm_config
d83c0e4
fix ci
352788a
extract flashcomm2_oshard_manager
851afb3
fix ci
4f8816f
fix review && fix Flashcomm2OshardQKVParallelOp to adapt not enable_sp()
0021b11
add ut
f9fedf1
adapt to refactor of oshard
6c20df9
fix comment
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,100 @@ | ||
| from typing import Any, Dict, Optional | ||
|
|
||
| from vllm.model_executor.models.utils import extract_layer_index | ||
|
|
||
| from vllm_ascend.distributed.parallel_state import get_shard_weight_group | ||
| from vllm_ascend.ops.layer_shard_linear import ( | ||
| is_hidden_layer, post_process_after_loading_for_shard_weight_series, | ||
| reach_layer_for_shard_weight_series, register_layer_to_shard_weight_series) | ||
| from vllm_ascend.utils import flashcomm2_enable, o_shard_enable | ||
|
|
||
|
|
||
| class Flashcomm2OShardManager: | ||
| """Manages sharded layers for the FlashComm2 O-Shard feature. | ||
|
|
||
| This class is implemented to centralize all logic related to Flashcomm2OShard layers. | ||
| Its main responsibilities are: | ||
| 1. Registering Attention `o_proj` layers that require O-Sharding. | ||
| 2. Storing and managing these layers in a dictionary mapping layer indices | ||
| to layer objects (`layer_index -> layer`). | ||
| 3. Providing a high-level API for external callers to use at key stages | ||
| like model initialization, computation, and weight loading. | ||
|
|
||
| Attributes: | ||
| _shard_layers: A dictionary to store the registered sharded layers, | ||
| mapping a layer index (int) to its corresponding layer object. | ||
| """ | ||
|
|
||
| def __init__(self): | ||
| self._shard_layers: Dict[int, Any] = {} | ||
|
|
||
| def flashcomm2_oshard_enable(self): | ||
| return flashcomm2_enable() and o_shard_enable() | ||
|
|
||
| def register_layer(self, layer: Any, prefetch_step: int = 1): | ||
| """Registers a layer for O-Sharding. | ||
|
|
||
| This method first checks if the O-Shard feature is enabled and if the | ||
| provided layer qualifies as a target (e.g., a hidden layer). If so, | ||
| it performs two actions: | ||
| 1. Caches the layer internally in the `_shard_layers` dictionary. | ||
| 2. Calls the underlying `register_layer_to_shared_weight_series` | ||
| function to register it for communication. | ||
|
|
||
| Args: | ||
| layer: The layer object to be registered. | ||
| prefetch_step: The prefetch step to be used when registering the | ||
| layer to the shared weight series. | ||
| """ | ||
| # Check if the layer is a target for sharding. | ||
| if is_hidden_layer(layer): | ||
| layer_idx = extract_layer_index(layer.prefix) | ||
| self._shard_layers[layer_idx] = layer | ||
|
|
||
| register_layer_to_shard_weight_series( | ||
| series_name="o_proj", | ||
| group=get_shard_weight_group(), | ||
| layer=layer, | ||
| prefetch_step=prefetch_step) | ||
|
|
||
| def get_layer(self, layer_idx: int) -> Optional[Any]: | ||
| """Safely retrieves a registered layer by its index. | ||
|
|
||
| Args: | ||
| layer_idx: The index of the layer to retrieve. | ||
|
|
||
| Returns: | ||
| The layer object if found, otherwise None. | ||
| """ | ||
| return self._shard_layers.get(layer_idx) | ||
|
|
||
| def trigger_broadcast_for_layer(self, layer_prefix: str): | ||
| """Triggers a broadcast for a specific layer during model computation. | ||
|
|
||
| This method is intended to be called within a layer's forward pass. | ||
| It extracts the layer index from the prefix, retrieves the corresponding | ||
| registered layer object, and then triggers the broadcast operation | ||
| if all conditions are met. | ||
|
|
||
| Args: | ||
| layer_prefix: The name prefix of the current layer being computed. | ||
| """ | ||
| layer_idx = extract_layer_index(layer_prefix) | ||
| target_layer = self.get_layer(layer_idx) | ||
|
|
||
| # Ensure the layer exists and meets the sharding criteria. | ||
| if target_layer and is_hidden_layer(target_layer): | ||
| reach_layer_for_shard_weight_series(target_layer) | ||
|
|
||
| def post_process_after_loading(self): | ||
| """Performs post-processing on all registered layers after weight loading. | ||
|
|
||
| This should be called once after the model weights have been fully loaded. | ||
| """ | ||
| if self._shard_layers: | ||
| # Pick any layer (e.g., the first one) to trigger the shard post-processing | ||
| any_layer = next(iter(self._shard_layers.values())) | ||
| post_process_after_loading_for_shard_weight_series(any_layer) | ||
|
|
||
|
|
||
| flashcomm2_oshard_manager = Flashcomm2OShardManager() | ||
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
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.
rename this file