Repository navigation
[Dev] Changes to support multimodule pipelining #2092
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
Closed
yashaswikarnati
wants to merge
17
commits into
NVIDIA:dev
from
yashaswikarnati:yash/dev_1f1b_multimodule_pipelining
Closed
Changes from 12 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
fb5ff3d
add stage checkers to p2p comm
yashaswikarnati ccfc4a4
ensure 3d tensor in multimodule comm
yashaswikarnati 77bd063
changes to 1f1b schedule to support multimodule comm
yashaswikarnati 0ea94c3
remove unsued imports
yashaswikarnati d81d90d
remove extra debug log
yashaswikarnati 6340fb2
schedule changes to support 1F1B pipelineing
yashaswikarnati fb36fd2
wrap with DDP added to transformer block
yashaswikarnati 35d3d43
multimodule schedule tests
yashaswikarnati 7ef2a33
set batch p2p comm False for reliable pp communication
yashaswikarnati 2068929
precommit hooks
yashaswikarnati 00dd7b0
add copyright header
yashaswikarnati 1ff716f
fix multimodule test
yashaswikarnati 6c96ab1
add test cases for different dp
yashaswikarnati 6418dac
format
yashaswikarnati 113283f
format
yashaswikarnati 1efd80a
Merge branch 'dev' into yash/dev_1f1b_multimodule_pipelining
yashaswikarnati 4f8dff6
change back to pp_rank
yashaswikarnati 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,12 +16,38 @@ | |
| Shape = Union[List[int], torch.Size] | ||
|
|
||
|
|
||
| def _ensure_3d_tensor(tensor): | ||
| """Ensure tensor is 3D for P2P/bridge communication. | ||
|
|
||
| P2P and bridge communicators expect 3D tensors. | ||
| Handles both single tensors and lists of tensors (for VPP). | ||
| """ | ||
| if isinstance(tensor, list): | ||
| return [_ensure_3d_tensor(t) for t in tensor] | ||
| if isinstance(tensor, torch.Tensor) and tensor.ndim == 2: | ||
| return tensor.unsqueeze(-1) | ||
| return tensor | ||
|
|
||
|
|
||
| def _restore_tensor_shape(tensor): | ||
| """Restore original tensor shape after P2P/bridge communication. | ||
|
|
||
| Remove the extra dimension added by _ensure_3d_tensor if it was singleton. | ||
| Handles both single tensors and lists of tensors (for VPP). | ||
| """ | ||
| if isinstance(tensor, list): | ||
| return [_restore_tensor_shape(t) for t in tensor] | ||
| if isinstance(tensor, torch.Tensor) and tensor.ndim == 3 and tensor.shape[-1] == 1: | ||
| return tensor.squeeze(-1) | ||
| return tensor | ||
|
|
||
|
|
||
| @dataclass | ||
| class RankModuleInfo: | ||
| """Information about a rank in a module.""" | ||
|
|
||
| # the stage of the current rank in the current module's pipeline. | ||
| pp_rank: int # the stage of the current rank in the current module's pipeline | ||
| pp_stage: int # the stage of the current rank in the current module's pipeline | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dont need to change these. pp_rank is good.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done changed back to pp_rank |
||
| pp_size: int # the number of ranks in the current module's pipeline | ||
| p2p_communicator: Optional[P2PCommunicator] | ||
| # key is either the src or dst module name connected to the current module | ||
|
|
@@ -108,12 +134,12 @@ def is_pp_first_stage(self): | |
| """Return True if the current rank has the absolute first stage in the overall model. | ||
|
|
||
| The absolute first stage is defined as: | ||
| 1. The current rank must be in the first PP stage (pp_rank == 0) of some module | ||
| 1. The current rank must be in the first PP stage (pp_stage == 0) of some module | ||
| 2. That module must be a source module (no incoming connections in topology) | ||
| """ | ||
| for module_name, rank_module_info in self.rank_module_map.items(): | ||
| # Check if this rank is at the first PP stage of this module | ||
| if rank_module_info.pp_rank == 0: | ||
| if rank_module_info.pp_stage == 0: | ||
| # Check if this module is a source module (no incoming connections) | ||
| if self._is_source_module(module_name): | ||
| return True | ||
|
|
@@ -129,7 +155,7 @@ def is_pp_last_stage(self): | |
| """ | ||
| for module_name, rank_module_info in self.rank_module_map.items(): | ||
| # Check if this rank is at the last PP stage of this module | ||
| if rank_module_info.pp_rank == rank_module_info.pp_size - 1: | ||
| if rank_module_info.pp_stage == rank_module_info.pp_size - 1: | ||
| # Check if this module is a sink module (no outgoing connections) | ||
| if self._is_sink_module(module_name): | ||
| return True | ||
|
|
@@ -204,13 +230,13 @@ def _build_rank_module_info_map(self): | |
| p2p_comm = P2PCommunicator(pp_group, self.config) | ||
| pp_size = dist.get_world_size(pp_group) | ||
| rank_in_pp_group = dist.get_group_rank(pp_group, self.current_rank) | ||
| pp_rank = rank_in_pp_group % pp_size | ||
| pp_stage = rank_in_pp_group % pp_size | ||
|
|
||
| bridge_comms_as_dest_module = [] | ||
| bridge_comms_as_src_module = [] | ||
| # If first stage, check if the module has any incoming modules | ||
| # If so, initialize bridge communicator | ||
| if pp_rank == 0: | ||
| if pp_stage == 0: | ||
| for bridge_comm in self.bridge_comms: | ||
| if ( | ||
| bridge_comm.is_current_rank_in_grid(bridge_comm.dest_grid) | ||
|
|
@@ -219,7 +245,7 @@ def _build_rank_module_info_map(self): | |
| bridge_comms_as_dest_module.append(bridge_comm) | ||
| # If last stage, check if the module has any outgoing modules | ||
| # If so, initialize bridge communicator | ||
| if pp_rank == pp_size - 1: | ||
| if pp_stage == pp_size - 1: | ||
| for bridge_comm in self.bridge_comms: | ||
| if ( | ||
| bridge_comm.is_current_rank_in_grid(bridge_comm.src_grid) | ||
|
|
@@ -228,7 +254,7 @@ def _build_rank_module_info_map(self): | |
| bridge_comms_as_src_module.append(bridge_comm) | ||
| # Build RankModuleInfo for the module | ||
| rank_module_info = RankModuleInfo( | ||
| pp_rank=pp_rank, | ||
| pp_stage=pp_stage, | ||
| pp_size=pp_size, | ||
| p2p_communicator=p2p_comm, | ||
| bridge_comms_as_dest_module=bridge_comms_as_dest_module, | ||
|
|
@@ -254,7 +280,7 @@ def recv_forward( | |
| input_dict = {} | ||
| for module_name, rank_module_info in self.rank_module_map.items(): | ||
|
|
||
| if rank_module_info.pp_rank == 0: | ||
| if rank_module_info.pp_stage == 0: | ||
| # If first stage, and has incoming modules, receive forward activation | ||
| # from incoming modules. | ||
| for bridge_comm in rank_module_info.bridge_comms_as_dest_module: | ||
|
|
@@ -277,16 +303,16 @@ def send_forward(self, output_dict: Dict[str, torch.Tensor], is_last_stage: bool | |
| f"[send_forward] output_dict keys: {output_dict.keys()}, is_last_stage: {is_last_stage}" | ||
| ) | ||
| for module_name, rank_module_info in self.rank_module_map.items(): | ||
| if rank_module_info.pp_rank == rank_module_info.pp_size - 1: | ||
| if rank_module_info.pp_stage == rank_module_info.pp_size - 1: | ||
| # If last stage, and has outgoing modules, send forward activation | ||
| # by using bridge communicator. | ||
| for bridge_comm in rank_module_info.bridge_comms_as_src_module: | ||
| bridge_comm.send_forward(output_dict[module_name]) | ||
| tensor_to_send = _ensure_3d_tensor(output_dict[module_name]) | ||
| bridge_comm.send_forward(tensor_to_send) | ||
| else: | ||
| # If not last stage, send forward activation by using P2P communicator. | ||
| rank_module_info.p2p_communicator.send_forward( | ||
| output_dict[module_name], is_last_stage=False | ||
| ) | ||
| tensor_to_send = _ensure_3d_tensor(output_dict[module_name]) | ||
| rank_module_info.p2p_communicator.send_forward(tensor_to_send, is_last_stage=False) | ||
|
|
||
| def send_forward_recv_backward( | ||
| self, | ||
|
|
@@ -303,28 +329,23 @@ def send_forward_recv_backward( | |
| Returns: | ||
| A dictionary mapping module names to tensors. | ||
| """ | ||
| logging.debug( | ||
| f"[Rank {dist.get_rank()} ][MultiModulePipelineCommunicator] " | ||
| f"[send_forward_recv_backward] output_dict keys: {output_dict.keys()}, " | ||
| f"tensor_shape: {tensor_shape}, is_last_stage: {is_last_stage}" | ||
| ) | ||
| grad_dict = {} | ||
| for module_name, rank_module_info in self.rank_module_map.items(): | ||
| if rank_module_info.pp_rank == rank_module_info.pp_size - 1: | ||
| if rank_module_info.pp_stage == rank_module_info.pp_size - 1: | ||
| # If last stage, and has outgoing modules, send forward activation and | ||
| # receive backward gradient by using bridge communicator. | ||
| for bridge_comm in rank_module_info.bridge_comms_as_src_module: | ||
| grad_dict[bridge_comm.src_module_name] = bridge_comm.send_forward_recv_backward( | ||
| output_dict[module_name] | ||
| ) | ||
| tensor_to_send = _ensure_3d_tensor(output_dict[module_name]) | ||
| grad_tensor = bridge_comm.send_forward_recv_backward(tensor_to_send) | ||
| grad_dict[bridge_comm.src_module_name] = _restore_tensor_shape(grad_tensor) | ||
| else: | ||
| # If not last stage, send forward activation and receive backward gradient | ||
| # by using P2P communicator. | ||
| grad_dict[module_name] = ( | ||
| rank_module_info.p2p_communicator.send_forward_recv_backward( | ||
| output_dict[module_name], tensor_shapes=tensor_shape, is_last_stage=False | ||
| ) | ||
| tensor_to_send = _ensure_3d_tensor(output_dict[module_name]) | ||
| grad_tensor = rank_module_info.p2p_communicator.send_forward_recv_backward( | ||
| tensor_to_send, tensor_shapes=tensor_shape, is_last_stage=False | ||
| ) | ||
| grad_dict[module_name] = _restore_tensor_shape(grad_tensor) | ||
| return grad_dict | ||
|
|
||
| def send_backward_recv_forward( | ||
|
|
@@ -349,23 +370,23 @@ def send_backward_recv_forward( | |
| ) | ||
| input_dict = {} | ||
| for module_name, rank_module_info in self.rank_module_map.items(): | ||
| if rank_module_info.pp_rank == 0: | ||
| if rank_module_info.pp_stage == 0: | ||
| for bridge_comm in rank_module_info.bridge_comms_as_dest_module: | ||
| # If first stage, and has incoming modules, send backward gradient and | ||
| # receive forward activation by using bridge communicator. | ||
| input_dict[bridge_comm.src_module_name] = ( | ||
| bridge_comm.send_backward_recv_forward( | ||
| grad_dict[bridge_comm.src_module_name] | ||
| ) | ||
| grad_to_send = _ensure_3d_tensor(grad_dict[bridge_comm.src_module_name]) | ||
| activation_tensor = bridge_comm.send_backward_recv_forward(grad_to_send) | ||
| input_dict[bridge_comm.src_module_name] = _restore_tensor_shape( | ||
| activation_tensor | ||
| ) | ||
| else: | ||
| # If not first stage, send backward gradient and receive forward activation | ||
| # by using P2P communicator. | ||
| input_dict[module_name] = ( | ||
| rank_module_info.p2p_communicator.send_backward_recv_forward( | ||
| grad_dict[module_name], tensor_shapes=tensor_shape, is_first_stage=False | ||
| ) | ||
| grad_to_send = _ensure_3d_tensor(grad_dict[module_name]) | ||
| activation_tensor = rank_module_info.p2p_communicator.send_backward_recv_forward( | ||
| grad_to_send, tensor_shapes=tensor_shape, is_first_stage=False | ||
| ) | ||
| input_dict[module_name] = _restore_tensor_shape(activation_tensor) | ||
| return input_dict | ||
|
|
||
| def recv_backward( | ||
|
|
@@ -385,16 +406,18 @@ def recv_backward( | |
| ) | ||
| grad_dict = {} | ||
| for module_name, rank_module_info in self.rank_module_map.items(): | ||
| if rank_module_info.pp_rank == rank_module_info.pp_size - 1: | ||
| if rank_module_info.pp_stage == rank_module_info.pp_size - 1: | ||
| # If last stage, and has incoming modules, receive backward gradient | ||
| # by using bridge communicator. | ||
| for bridge_comm in rank_module_info.bridge_comms_as_src_module: | ||
| grad_dict[bridge_comm.src_module_name] = bridge_comm.recv_backward() | ||
| recv_grad_tensor = bridge_comm.recv_backward() | ||
| grad_dict[bridge_comm.src_module_name] = _restore_tensor_shape(recv_grad_tensor) | ||
| else: | ||
| # If not last stage, receive backward gradient by using P2P communicator. | ||
| grad_dict[module_name] = rank_module_info.p2p_communicator.recv_backward( | ||
| recv_grad_tensor = rank_module_info.p2p_communicator.recv_backward( | ||
| tensor_shapes=tensor_shape, is_last_stage=False | ||
| ) | ||
| grad_dict[module_name] = _restore_tensor_shape(recv_grad_tensor) | ||
| return grad_dict | ||
|
|
||
| def send_backward(self, grad_dict: Dict[str, torch.Tensor], is_first_stage: bool = False): | ||
|
|
@@ -408,16 +431,16 @@ def send_backward(self, grad_dict: Dict[str, torch.Tensor], is_first_stage: bool | |
| f"[send_backward] grad_dict keys: {grad_dict.keys()}, is_first_stage: {is_first_stage}" | ||
| ) | ||
| for module_name, rank_module_info in self.rank_module_map.items(): | ||
| if rank_module_info.pp_rank == 0: | ||
| if rank_module_info.pp_stage == 0: | ||
| # If first stage, and has incoming modules, send backward activation | ||
| # by using bridge communicator. | ||
| for bridge_comm in rank_module_info.bridge_comms_as_dest_module: | ||
| bridge_comm.send_backward(grad_dict[bridge_comm.src_module_name]) | ||
| grad_to_send = _ensure_3d_tensor(grad_dict[bridge_comm.src_module_name]) | ||
| bridge_comm.send_backward(grad_to_send) | ||
| else: | ||
| # If not first stage, send backward activation by using P2P communicator. | ||
| rank_module_info.p2p_communicator.send_backward( | ||
| grad_dict[module_name], is_first_stage=False | ||
| ) | ||
| grad_to_send = _ensure_3d_tensor(grad_dict[module_name]) | ||
| rank_module_info.p2p_communicator.send_backward(grad_to_send, is_first_stage=False) | ||
|
|
||
| @staticmethod | ||
| def compute_total_pipeline_stages( | ||
|
|
||
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
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.
Are we sure that the tensor must be 2D or 3D? Should we catch some exception here?