-
Notifications
You must be signed in to change notification settings - Fork 284
Add fake balance for EP mode #962
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
Open
STwangyingrui
wants to merge
3
commits into
main
Choose a base branch
from
yr/ep_balance
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,71 @@ | ||
| import torch | ||
| import os | ||
|
|
||
| import threading | ||
|
|
||
| from lightllm.utils.log_utils import init_logger | ||
|
|
||
| logger = init_logger(__name__) | ||
|
|
||
|
|
||
| def singleton_threadsafe(cls): | ||
| instances = {} | ||
| lock = threading.Lock() | ||
|
|
||
| def get_instance(*args, **kwargs): | ||
| # A key that includes the arguments is needed for parameter-dependent singletons. | ||
| # Using a tuple of args and a frozenset of kwargs items makes it hashable. | ||
| key = (cls, args, frozenset(kwargs.items())) | ||
| with lock: | ||
| if key not in instances: | ||
| instances[key] = cls(*args, **kwargs) | ||
| return instances[key] | ||
|
|
||
| return get_instance | ||
|
|
||
|
|
||
| @singleton_threadsafe | ||
| class BalancedTensor: | ||
| def __init__(self, num_experts=256, num_selected=8): | ||
| self.balanced_tensors = {} | ||
| self.num_experts = num_experts | ||
| self.num_selected = num_selected | ||
|
|
||
| def generate_balanced_tensor(self, num_tokens): | ||
| # Evenly distribute num_tokens to num_selected experts out of num_experts. | ||
| # Note that the num_selected experts activated by a token cannot be repeated. | ||
| # Performance is not that important, as it is only activated in special scenarios. | ||
| tensor = torch.zeros((num_tokens, self.num_selected), dtype=torch.int, device="cuda") | ||
| expert_load = torch.zeros(self.num_experts, dtype=torch.int, device="cuda") | ||
|
|
||
| for i in range(num_tokens): | ||
| available_experts = torch.arange(self.num_experts, device="cuda") | ||
| selected = [] | ||
| for _ in range(self.num_selected): | ||
| current_load = expert_load[available_experts] | ||
| min_load_indices = torch.where(current_load == current_load.min())[0] | ||
| if len(min_load_indices) > 1: | ||
| # If there are multiple least-loaded experts, select one randomly | ||
| chosen_index = torch.randint(0, len(min_load_indices), (1,), device="cuda").item() | ||
| chosen_expert_index = min_load_indices[chosen_index] | ||
| else: | ||
| chosen_expert_index = min_load_indices[0] | ||
| chosen_expert = available_experts[chosen_expert_index] | ||
| selected.append(chosen_expert) | ||
| # Remove the selected expert from the list of available experts | ||
| available_experts = torch.cat( | ||
| [available_experts[:chosen_expert_index], available_experts[chosen_expert_index + 1 :]] | ||
| ) | ||
| expert_load[chosen_expert] += 1 | ||
|
|
||
| tensor[i] = torch.tensor(selected, dtype=torch.int, device="cuda") | ||
|
|
||
| return tensor | ||
|
|
||
| def get_balance_topk_ids(self, num_tokens): | ||
| if num_tokens in self.balanced_tensors: | ||
| return self.balanced_tensors[num_tokens] | ||
|
|
||
| tensor = self.generate_balanced_tensor(num_tokens) | ||
| self.balanced_tensors[num_tokens] = tensor | ||
| return tensor | ||
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.
The current implementation of
generate_balanced_tensoris inefficient due to the use oftorch.catinside a loop. This creates a new tensor and copies data in every iteration, which can be slow for largenum_tokensornum_experts. A more performant approach would be to use a boolean mask to keep track of selected experts, avoiding the expensivetorch.catoperation. This can significantly reduce the overhead.