-
Notifications
You must be signed in to change notification settings - Fork 28
Fix circular dependence #205
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
+212
−111
Merged
Changes from all commits
Commits
Show all changes
5 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| """ | ||
| Process group management for distributed training. | ||
|
|
||
| This module provides singleton-based process group management for distributed training, | ||
| including support for CFG parallelism, sequence parallelism (Ulysses + Ring), and tensor parallelism. | ||
| """ | ||
|
|
||
| import torch.distributed as dist | ||
| from typing import Optional, List | ||
|
|
||
|
|
||
| class Singleton: | ||
| _instance = None | ||
|
|
||
| def __new__(cls, *args, **kwargs): | ||
| if not cls._instance: | ||
| cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs) | ||
| return cls._instance | ||
|
|
||
|
|
||
| class ProcessGroupSingleton(Singleton): | ||
| def __init__(self): | ||
| if not hasattr(self, 'initialized'): | ||
| self.CFG_GROUP: Optional[dist.ProcessGroup] = None | ||
| self.SP_GROUP: Optional[dist.ProcessGroup] = None | ||
| self.SP_ULYSSUES_GROUP: Optional[dist.ProcessGroup] = None | ||
| self.SP_RING_GROUP: Optional[dist.ProcessGroup] = None | ||
| self.TP_GROUP: Optional[dist.ProcessGroup] = None | ||
|
|
||
| self.CFG_RANKS: List[int] = [] | ||
| self.SP_RANKS: List[int] = [] | ||
| self.SP_ULYSSUES_RANKS: List[int] = [] | ||
| self.SP_RING_RANKS: List[int] = [] | ||
| self.TP_RANKS: List[int] = [] | ||
|
|
||
| self.initialized = True | ||
|
|
||
|
|
||
| PROCESS_GROUP = ProcessGroupSingleton() | ||
|
|
||
|
|
||
| # CFG parallel group functions | ||
| def get_cfg_group(): | ||
| return PROCESS_GROUP.CFG_GROUP | ||
|
|
||
|
|
||
| def get_cfg_world_size(): | ||
| return PROCESS_GROUP.CFG_GROUP.size() if PROCESS_GROUP.CFG_GROUP is not None else 1 | ||
|
|
||
|
|
||
| def get_cfg_rank(): | ||
| return PROCESS_GROUP.CFG_GROUP.rank() if PROCESS_GROUP.CFG_GROUP is not None else 0 | ||
|
|
||
|
|
||
| def get_cfg_ranks(): | ||
| return PROCESS_GROUP.CFG_RANKS | ||
|
|
||
|
|
||
| # Sequence parallel group functions | ||
| def get_sp_group(): | ||
| return PROCESS_GROUP.SP_GROUP | ||
|
|
||
|
|
||
| def get_sp_world_size(): | ||
| return PROCESS_GROUP.SP_GROUP.size() if PROCESS_GROUP.SP_GROUP is not None else 1 | ||
|
|
||
|
|
||
| def get_sp_rank(): | ||
| return PROCESS_GROUP.SP_GROUP.rank() if PROCESS_GROUP.SP_GROUP is not None else 0 | ||
|
|
||
|
|
||
| def get_sp_ranks(): | ||
| return PROCESS_GROUP.SP_RANKS | ||
|
|
||
|
|
||
| # Sequence parallel Ulysses group functions | ||
| def get_sp_ulysses_group(): | ||
| return PROCESS_GROUP.SP_ULYSSUES_GROUP | ||
|
|
||
|
|
||
| def get_sp_ulysses_world_size(): | ||
| return PROCESS_GROUP.SP_ULYSSUES_GROUP.size() if PROCESS_GROUP.SP_ULYSSUES_GROUP is not None else 1 | ||
|
|
||
|
|
||
| def get_sp_ulysses_rank(): | ||
| return PROCESS_GROUP.SP_ULYSSUES_GROUP.rank() if PROCESS_GROUP.SP_ULYSSUES_GROUP is not None else 0 | ||
|
|
||
|
|
||
| def get_sp_ulysses_ranks(): | ||
| return PROCESS_GROUP.SP_ULYSSUES_RANKS | ||
|
|
||
|
|
||
| # Sequence parallel Ring group functions | ||
| def get_sp_ring_group(): | ||
| return PROCESS_GROUP.SP_RING_GROUP | ||
|
|
||
|
|
||
| def get_sp_ring_world_size(): | ||
| return PROCESS_GROUP.SP_RING_GROUP.size() if PROCESS_GROUP.SP_RING_GROUP is not None else 1 | ||
|
|
||
|
|
||
| def get_sp_ring_rank(): | ||
| return PROCESS_GROUP.SP_RING_GROUP.rank() if PROCESS_GROUP.SP_RING_GROUP is not None else 0 | ||
|
|
||
|
|
||
| def get_sp_ring_ranks(): | ||
| return PROCESS_GROUP.SP_RING_RANKS | ||
|
|
||
|
|
||
| # Tensor parallel group functions | ||
| def get_tp_group(): | ||
| return PROCESS_GROUP.TP_GROUP | ||
|
|
||
|
|
||
| def get_tp_world_size(): | ||
| return PROCESS_GROUP.TP_GROUP.size() if PROCESS_GROUP.TP_GROUP is not None else 1 | ||
|
|
||
|
|
||
| def get_tp_rank(): | ||
| return PROCESS_GROUP.TP_GROUP.rank() if PROCESS_GROUP.TP_GROUP is not None else 0 | ||
|
|
||
|
|
||
| def get_tp_ranks(): | ||
| return PROCESS_GROUP.TP_RANKS | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "PROCESS_GROUP", | ||
| "get_cfg_group", | ||
| "get_cfg_world_size", | ||
| "get_cfg_rank", | ||
| "get_cfg_ranks", | ||
| "get_sp_group", | ||
| "get_sp_world_size", | ||
| "get_sp_rank", | ||
| "get_sp_ranks", | ||
| "get_sp_ulysses_group", | ||
| "get_sp_ulysses_world_size", | ||
| "get_sp_ulysses_rank", | ||
| "get_sp_ulysses_ranks", | ||
| "get_sp_ring_group", | ||
| "get_sp_ring_world_size", | ||
| "get_sp_ring_rank", | ||
| "get_sp_ring_ranks", | ||
| "get_tp_group", | ||
| "get_tp_world_size", | ||
| "get_tp_rank", | ||
| "get_tp_ranks", | ||
| ] |
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.
Uh oh!
There was an error while loading. Please reload this page.