From fb5ff3da7e51c884549a0320f6689226a08bd056 Mon Sep 17 00:00:00 2001 From: ykarnati Date: Sun, 2 Nov 2025 09:19:48 -0800 Subject: [PATCH 01/16] add stage checkers to p2p comm --- .../core/pipeline_parallel/p2p_communication.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/megatron/core/pipeline_parallel/p2p_communication.py b/megatron/core/pipeline_parallel/p2p_communication.py index 63ee9d1f537..df817da289f 100644 --- a/megatron/core/pipeline_parallel/p2p_communication.py +++ b/megatron/core/pipeline_parallel/p2p_communication.py @@ -7,6 +7,7 @@ import torch.distributed as dist from megatron.core.model_parallel_config import ModelParallelConfig +from megatron.core.pipeline_parallel.utils import is_pp_first_stage, is_pp_last_stage from megatron.core.utils import nvtx_decorator # Types @@ -162,6 +163,21 @@ def __init__(self, pp_group: dist.ProcessGroup, config: ModelParallelConfig): else None ) + @property + def is_pp_first_stage(self): + """Return True if pp first stage.""" + return is_pp_first_stage(self.pp_group) + + @property + def is_pp_last_stage(self): + """Return True if pp last stage.""" + return is_pp_last_stage(self.pp_group) + + @property + def num_warmup_microbatches(self): + """Return number of warmup microbatches.""" + return self.pp_group.size() - self.pp_group.rank() - 1 + def _communicate_shapes(self, tensor_send_next, tensor_send_prev, recv_prev, recv_next): """Communicate tensor shapes between stages. Used to communicate tensor shapes before the actual tensor communication happens. From ccfc4a4c843fbac9882cec81fb1bee5825bbff74 Mon Sep 17 00:00:00 2001 From: ykarnati Date: Sun, 2 Nov 2025 09:20:12 -0800 Subject: [PATCH 02/16] ensure 3d tensor in multimodule comm --- .../multimodule_communicator.py | 98 +++++++++++++------ 1 file changed, 66 insertions(+), 32 deletions(-) diff --git a/megatron/core/pipeline_parallel/multimodule_communicator.py b/megatron/core/pipeline_parallel/multimodule_communicator.py index dfda270ef76..fb95e3e3ee5 100644 --- a/megatron/core/pipeline_parallel/multimodule_communicator.py +++ b/megatron/core/pipeline_parallel/multimodule_communicator.py @@ -15,13 +15,37 @@ # Types 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 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 +132,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 +153,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 +228,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 +243,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 +252,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 +278,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,15 +301,17 @@ 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. + tensor_to_send = _ensure_3d_tensor(output_dict[module_name]) rank_module_info.p2p_communicator.send_forward( - output_dict[module_name], is_last_stage=False + tensor_to_send, is_last_stage=False ) def send_forward_recv_backward( @@ -310,21 +336,23 @@ def send_forward_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 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 +377,25 @@ 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] = ( + grad_to_send = _ensure_3d_tensor(grad_dict[bridge_comm.src_module_name]) + activation_tensor = ( bridge_comm.send_backward_recv_forward( - grad_dict[bridge_comm.src_module_name] + 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 +415,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,15 +440,17 @@ 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. + grad_to_send = _ensure_3d_tensor(grad_dict[module_name]) rank_module_info.p2p_communicator.send_backward( - grad_dict[module_name], is_first_stage=False + grad_to_send, is_first_stage=False ) @staticmethod From 77bd063a12c5f86bf035dce6bd2b0b0560ea3de4 Mon Sep 17 00:00:00 2001 From: ykarnati Date: Sun, 2 Nov 2025 10:44:40 -0800 Subject: [PATCH 03/16] changes to 1f1b schedule to support multimodule comm --- megatron/core/pipeline_parallel/schedules.py | 216 +++++++++++++------ 1 file changed, 149 insertions(+), 67 deletions(-) diff --git a/megatron/core/pipeline_parallel/schedules.py b/megatron/core/pipeline_parallel/schedules.py index 11e54e0fa53..ac4973a0efd 100644 --- a/megatron/core/pipeline_parallel/schedules.py +++ b/megatron/core/pipeline_parallel/schedules.py @@ -2,8 +2,8 @@ import contextlib from functools import partial -from typing import Callable, Iterator, List, Optional, Union - +from typing import Callable, Dict, Iterator, List, Optional, Union +from typing import Any import torch from torch.autograd.variable import Variable @@ -30,7 +30,9 @@ nvtx_range_pop, nvtx_range_push, ) - +from megatron.core.pipeline_parallel.multimodule_communicator import ( + MultiModulePipelineCommunicator, +) from .combined_1f1b import ( combined_1f1b_schedule_for_interleaved_pipelining, combined_1f1b_schedule_for_no_pipelining, @@ -135,7 +137,7 @@ def forward_step(data_iterator, model): return forward_backward_func -def deallocate_output_tensor(out, deallocate_pipeline_outputs=False): +def deallocate_output_tensor(out: Union[torch.Tensor, List[torch.Tensor], Dict[str, torch.Tensor]], deallocate_pipeline_outputs=False): '''Pseudo-deallocate (i.e., set to scalar) the output tensor's '.data' field. This method should be called right after the output tensor has been @@ -144,9 +146,19 @@ def deallocate_output_tensor(out, deallocate_pipeline_outputs=False): ''' if (out is None) or (not deallocate_pipeline_outputs): return - assert isinstance(out, torch.Tensor), "expected Tensor, found %s." % type(out).__name__ - assert out._base is None, "counter-productive to free a view of another tensor." - out.data = torch.empty((1,), device=out.device, dtype=out.dtype) + if isinstance(out, torch.Tensor): + assert out._base is None, "counter-productive to free a view of another tensor." + out.data = torch.empty((1,), device=out.device, dtype=out.dtype) + + if isinstance(out, dict): + for v in out.values(): + deallocate_output_tensor(v, deallocate_pipeline_outputs) + return + + if isinstance(out, (list, tuple)): + for v in out: + deallocate_output_tensor(v, deallocate_pipeline_outputs) + return def custom_backward(output, grad_output): @@ -425,7 +437,7 @@ def forward_step( return [output_tensor], num_tokens -def backward_step(input_tensor, output_tensor, output_tensor_grad, model_type, config): +def backward_step_tensor(input_tensor, output_tensor, output_tensor_grad, model_type, config): """Backward step through passed-in output tensor. If last stage, output_tensor_grad is None, otherwise gradient of loss @@ -489,6 +501,85 @@ def backward_step(input_tensor, output_tensor, output_tensor_grad, model_type, c return input_tensor_grad +def _backward_step_dict(input_tensor: Dict[str, torch.Tensor], output_tensor: Union[torch.Tensor, Dict[str, torch.Tensor]], output_tensor_grad: Union[torch.Tensor, Dict[str, torch.Tensor]], model_type: str, config: Any): + """Backward step implementation when inputs/outputs are dictionaries (multi module case).""" + + if config.timers is not None: + config.timers('backward-compute', log_level=2).start() + + # Retain gradients on all input tensors + for module_name, tensor in input_tensor.items(): + if isinstance(tensor, list): + tensor = tensor[0] + if tensor is not None: + tensor.retain_grad() + + # Last stage: output_tensor is a scalar loss, wrap in dict for uniform handling + # Use the first input tensor key as the main module name + # now last stage only has one module LLM, + # we should add these asserts in top level train loop. + if not isinstance(output_tensor, dict): + all_keys = list(input_tensor.keys()) + assert len(all_keys) == 1, "Last stage only has one module - LLM" + main_module_key = all_keys[0] + output_tensor = {main_module_key: output_tensor} + + # Handle output_tensor_grad: None (last stage) or dict (intermediate stages) + if not output_tensor_grad: + # Last stage: no gradient from next stage + output_tensor_grad = {key: None for key in output_tensor.keys()} + + # Apply grad scaling if needed (for last stage) + for module_name in output_tensor.keys(): + if output_tensor_grad[module_name] is None and config.grad_scale_func is not None: + output_tensor[module_name] = config.grad_scale_func(output_tensor[module_name]) + + # Perform backward pass for each module + for module_name in output_tensor.keys(): + output_tensor_module = output_tensor[module_name] + output_tensor_grad_module = output_tensor_grad[module_name] + + # Skip backward if tensor doesn't require gradients + if output_tensor_module is not None and output_tensor_module.requires_grad: + if config.deallocate_pipeline_outputs: + custom_backward(output_tensor_module, output_tensor_grad_module) + else: + torch.autograd.backward( + output_tensor_module, grad_tensors=output_tensor_grad_module + ) + + # Collect gradients for input tensors + input_tensor_grad = {} + for module_name, tensor in input_tensor.items(): + if isinstance(tensor, list): + tensor = tensor[0] + if tensor is None: + input_tensor_grad[module_name] = None + else: + input_tensor_grad[module_name] = tensor.grad + + if config.timers is not None: + config.timers('backward-compute').stop() + + return input_tensor_grad + + +def backward_step(input_tensor: Union[torch.Tensor, Dict[str, torch.Tensor]], output_tensor: Union[torch.Tensor, Dict[str, torch.Tensor]], output_tensor_grad: Union[torch.Tensor, Dict[str, torch.Tensor]], model_type: str, config: Any): + """Backward step wrapper support both tensor and dictionary formats + + The inputs and outputs are dictionaries for multimodule case. + The keys of the dictionaries are the module names. + + Returns: + The input tensor or dictionary of tensors. + """ + + if isinstance(input_tensor, dict): + return _backward_step_dict(input_tensor, output_tensor, output_tensor_grad, model_type, config) + else: + return backward_step_tensor(input_tensor, output_tensor, output_tensor_grad, model_type, config) + + def check_first_val_step(first_val_step, forward_only, cond): """Check if it is the first validation step.""" if (first_val_step is not None) and forward_only: @@ -1935,8 +2026,8 @@ def get_tensor_shapes( micro_batch_size: int, decoder_seq_length: int, config, - tp_group: torch.distributed.ProcessGroup, - cp_group: torch.distributed.ProcessGroup, + tp_group: Optional[torch.distributed.ProcessGroup] = None, + cp_group: Optional[torch.distributed.ProcessGroup] = None, ): """ Determine right tensor sizes (based on position of rank with respect to split rank) and @@ -1944,15 +2035,21 @@ def get_tensor_shapes( """ tensor_shapes = [] - # Use decoder_seq_length if provided, otherwise use seq_length - effective_seq_length = decoder_seq_length if decoder_seq_length is not None else seq_length - effective_seq_length = effective_seq_length // cp_group.size() + if config.variable_seq_lengths: + # with variable seq_lengths, ranks exchange the tensor shape with each other + tensor_shapes.append(()) + return tensor_shapes + else: + assert tp_group is not None and cp_group is not None, "tp_group and cp_group must be provided" + # Use decoder_seq_length if provided, otherwise use seq_length + effective_seq_length = decoder_seq_length if decoder_seq_length is not None else seq_length + effective_seq_length = effective_seq_length // cp_group.size() - if config.sequence_parallel: - effective_seq_length = effective_seq_length // tp_group.size() + if config.sequence_parallel: + effective_seq_length = effective_seq_length // tp_group.size() - tensor_shapes.append((effective_seq_length, micro_batch_size, config.hidden_size)) - return tensor_shapes + tensor_shapes.append((effective_seq_length, micro_batch_size, config.hidden_size)) + return tensor_shapes def forward_backward_pipelining_without_interleaving( @@ -1968,8 +2065,8 @@ def forward_backward_pipelining_without_interleaving( collect_non_loss_data: bool = False, first_val_step: Optional[bool] = None, adjust_tensor_shapes_fn: Optional[Callable] = None, - p2p_communicator: Optional[P2PCommunicator] = None, - pg_collection: Optional[ProcessGroupCollection] = None, + p2p_communicator: Optional[Union[P2PCommunicator, MultiModulePipelineCommunicator]] = None, + pg_collection: Optional[Union[ProcessGroupCollection, List[ProcessGroupCollection]]] = None, ): """Run non-interleaved 1F1B schedule, with communication between pipeline stages. Returns dictionary with losses if the last stage, empty dict otherwise.""" @@ -1990,7 +2087,7 @@ def forward_backward_pipelining_without_interleaving( raise ValueError( "Non-interleaved pipeline parallelism does not support overlapping p2p communication" ) - + tp_group, cp_group, llm_cp_size = None, None, None if p2p_communicator is None and pg_collection is None: p2p_communicator = P2PCommunicator( pp_group=parallel_state.get_pipeline_model_parallel_group(), config=config @@ -2010,33 +2107,21 @@ def forward_backward_pipelining_without_interleaving( pg_collection.dp_cp = parallel_state.get_data_parallel_group( with_context_parallel=True, partial_data_parallel=False ) + llm_cp_size = cp_group.size() elif p2p_communicator is not None and pg_collection is not None: - model_type = get_model_type(model) - assert model_type != ModelType.encoder_and_decoder, ( - "encoder PP stages not yet supported when passing custom process groups. " - "support coming soon!" - ) - assert hasattr(p2p_communicator, 'config'), "p2p_communicator must have a config" - assert hasattr(pg_collection, 'tp'), "pg_collection must have tp_group" - assert hasattr(pg_collection, 'cp'), "pg_collection must have cp_group" - assert hasattr(pg_collection, 'embd'), ( - "pg_collection must have a embd. In previous version, it is used default " - "`parallel_state.default_embedding_ranks` to create the process group. " - " If you are using the default process group, please use " - " `parallel_state.get_embedding_group()` " - "If you don't need embd_group, you need to explicitly set it to None." - ) - assert hasattr(pg_collection, 'pos_embd'), ( - "pg_collection must have a pos_embd. In previous version, it is used default " - "`parallel_state.default_position_embedding_ranks` to create the process group. " - " If you are using the default process group, please use " - " `parallel_state.get_position_embedding_group()` " - "If you don't need pos_embd_group, you need to explicitly set it to None." - ) - assert hasattr(pg_collection, 'pp'), "pg_collection must have pp_group" - assert hasattr(pg_collection, 'dp_cp'), "pg_collection must have dp_cp_group" - tp_group = pg_collection.tp - cp_group = pg_collection.cp + if isinstance(pg_collection, list): + # cases when multiple modules are colocated + assert config.variable_seq_lengths, "variable seq_lengths is required when multiple modules are colocated" + # when llm is colocated for now assume last collection in the list is the llm + # TODO: ykarnati: Have a better interface to handle this (without breaking backward compatibility) + assert hasattr(pg_collection[-1], 'cp'), "pg_collection must have cp_group" + llm_cp_size = pg_collection[-1].cp.size() + else: + assert hasattr(pg_collection, 'tp'), "pg_collection must have tp_group" + assert hasattr(pg_collection, 'cp'), "pg_collection must have cp_group" + tp_group = pg_collection.tp + cp_group = pg_collection.cp + llm_cp_size = pg_collection.cp.size() else: raise ValueError( "Invalid combination of p2p_communicator, pg_collection " @@ -2046,7 +2131,7 @@ def forward_backward_pipelining_without_interleaving( # Needed only when gradients are finalized in M-Core if config.finalize_model_grads_func is not None and not forward_only: embedding_module = clear_embedding_activation_buffer( - config, model, is_pp_last_stage(p2p_communicator.pp_group) + config, model, p2p_communicator.is_last_stage ) if config.timers is not None: @@ -2078,9 +2163,7 @@ def enable_grad_sync(): disable_grad_sync() # Compute number of warmup microbatches. - num_warmup_microbatches = ( - p2p_communicator.pp_group.size() - p2p_communicator.pp_group.rank() - 1 - ) + num_warmup_microbatches = p2p_communicator.num_warmup_microbatches num_warmup_microbatches = min(num_warmup_microbatches, num_microbatches) num_microbatches_remaining = num_microbatches - num_warmup_microbatches @@ -2098,7 +2181,6 @@ def enable_grad_sync(): model_type = get_model_type(model) - rank = p2p_communicator.pp_group.rank() recv_tensor_shapes = get_tensor_shapes( seq_length=seq_length, micro_batch_size=micro_batch_size, @@ -2142,7 +2224,7 @@ def enable_grad_sync(): checkpoint_activations_microbatch = None input_tensor = p2p_communicator.recv_forward( - recv_tensor_shapes, is_pp_first_stage(p2p_communicator.pp_group) + recv_tensor_shapes, p2p_communicator.is_first_stage ) output_tensor, num_tokens = forward_step( forward_step_func, @@ -2157,9 +2239,9 @@ def enable_grad_sync(): checkpoint_activations_microbatch=checkpoint_activations_microbatch, is_first_microbatch=check_first_val_step(first_val_step, forward_only, i == 0), current_microbatch=i, - is_last_stage=is_pp_last_stage(p2p_communicator.pp_group), + is_last_stage=p2p_communicator.is_last_stage, ) - p2p_communicator.send_forward(output_tensor, is_pp_last_stage(p2p_communicator.pp_group)) + p2p_communicator.send_forward(output_tensor, p2p_communicator.is_last_stage) total_num_tokens += num_tokens if not forward_only: @@ -2172,7 +2254,7 @@ def enable_grad_sync(): # receive this tensor here. if num_microbatches_remaining > 0: input_tensor = p2p_communicator.recv_forward( - recv_tensor_shapes, is_pp_first_stage(p2p_communicator.pp_group) + recv_tensor_shapes, p2p_communicator.is_first_stage ) # Run 1F1B in steady state. @@ -2195,28 +2277,28 @@ def enable_grad_sync(): input_tensor, forward_data_store, config, - cp_group_size=pg_collection.cp.size(), + cp_group_size=llm_cp_size, collect_non_loss_data=collect_non_loss_data, checkpoint_activations_microbatch=checkpoint_activations_microbatch, is_first_microbatch=check_first_val_step( first_val_step, forward_only, (i == 0) and (num_warmup_microbatches == 0) ), current_microbatch=i + num_warmup_microbatches, - is_last_stage=is_pp_last_stage(p2p_communicator.pp_group), + is_last_stage=p2p_communicator.is_last_stage, ) total_num_tokens += num_tokens if forward_only: p2p_communicator.send_forward( - output_tensor, is_pp_last_stage(p2p_communicator.pp_group) + output_tensor, p2p_communicator.is_last_stage ) if not last_iteration: input_tensor = p2p_communicator.recv_forward( - recv_tensor_shapes, is_pp_first_stage(p2p_communicator.pp_group) + recv_tensor_shapes, p2p_communicator.is_pp_first_stage ) else: output_tensor_grad = p2p_communicator.send_forward_recv_backward( - output_tensor, send_tensor_shapes, is_pp_last_stage(p2p_communicator.pp_group) + output_tensor, send_tensor_shapes, p2p_communicator.is_last_stage ) # Add input_tensor and output_tensor to end of list. @@ -2232,7 +2314,7 @@ def enable_grad_sync(): # Enable grad sync for the last microbatch in the batch if the full # backward pass completes in the 1F1B stage. if num_warmup_microbatches == 0 and last_iteration: - if config.grad_sync_func is None or rank == 0: + if config.grad_sync_func is None or p2p_communicator.is_first_stage: enable_grad_sync() input_tensor_grad = backward_step( @@ -2242,13 +2324,13 @@ def enable_grad_sync(): if last_iteration: input_tensor = None p2p_communicator.send_backward( - input_tensor_grad, is_pp_first_stage(p2p_communicator.pp_group) + input_tensor_grad, p2p_communicator.is_first_stage ) else: input_tensor = p2p_communicator.send_backward_recv_forward( input_tensor_grad, recv_tensor_shapes, - is_pp_first_stage(p2p_communicator.pp_group), + p2p_communicator.is_first_stage, ) # Run cooldown backward passes. @@ -2261,14 +2343,14 @@ def enable_grad_sync(): # pipeline stages do grad reduction during pipeline # bubble. if i == num_warmup_microbatches - 1: - if config.grad_sync_func is None or rank == 0: + if config.grad_sync_func is None or p2p_communicator.is_first_stage: enable_grad_sync() input_tensor = input_tensors.pop(0) output_tensor = output_tensors.pop(0) output_tensor_grad = p2p_communicator.recv_backward( - send_tensor_shapes, is_pp_last_stage(p2p_communicator.pp_group) + send_tensor_shapes, p2p_communicator.is_last_stage ) input_tensor_grad = backward_step( @@ -2276,7 +2358,7 @@ def enable_grad_sync(): ) p2p_communicator.send_backward( - input_tensor_grad, is_pp_first_stage(p2p_communicator.pp_group) + input_tensor_grad, p2p_communicator.is_first_stage ) # Launch any remaining grad reductions. @@ -2290,7 +2372,7 @@ def enable_grad_sync(): # If defer_embedding_wgrad_compute is enabled we need to do the # weight gradient GEMM's here. finish_embedding_wgrad_compute( - config, embedding_module, is_pp_last_stage(p2p_communicator.pp_group), tp_group + config, embedding_module, p2p_communicator.is_last_stage, tp_group ) # Finalize model grads (perform full grad all-reduce / reduce-scatter for From 0ea94c3b94fbf6864ee2d7c18ee76a466b071eeb Mon Sep 17 00:00:00 2001 From: ykarnati Date: Sun, 2 Nov 2025 14:28:50 -0800 Subject: [PATCH 04/16] remove unsued imports --- .../pipeline_parallel/test_multimodule_communicator.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/unit_tests/pipeline_parallel/test_multimodule_communicator.py b/tests/unit_tests/pipeline_parallel/test_multimodule_communicator.py index 73739859f42..f89b8fa2a77 100644 --- a/tests/unit_tests/pipeline_parallel/test_multimodule_communicator.py +++ b/tests/unit_tests/pipeline_parallel/test_multimodule_communicator.py @@ -1,14 +1,10 @@ -import logging -import os -import sys +import os import pytest import torch import torch.distributed as dist from packaging import version -from megatron.core import parallel_state -from megatron.core.hyper_comm_grid import HyperCommGrid from megatron.core.model_parallel_config import ModelParallelConfig from megatron.core.pipeline_parallel.multimodule_communicator import MultiModulePipelineCommunicator from tests.unit_tests.pipeline_parallel.test_bridge_communicator import ( From d81d90d3f0d0e2696998e128eab2192cfaae0b99 Mon Sep 17 00:00:00 2001 From: ykarnati Date: Sun, 2 Nov 2025 14:29:10 -0800 Subject: [PATCH 05/16] remove extra debug log --- megatron/core/pipeline_parallel/multimodule_communicator.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/megatron/core/pipeline_parallel/multimodule_communicator.py b/megatron/core/pipeline_parallel/multimodule_communicator.py index fb95e3e3ee5..02607cc625f 100644 --- a/megatron/core/pipeline_parallel/multimodule_communicator.py +++ b/megatron/core/pipeline_parallel/multimodule_communicator.py @@ -329,11 +329,6 @@ 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_stage == rank_module_info.pp_size - 1: From 6340fb2c0e928b70c5f35fde0746eb5be6076542 Mon Sep 17 00:00:00 2001 From: ykarnati Date: Sun, 2 Nov 2025 14:29:27 -0800 Subject: [PATCH 06/16] schedule changes to support 1F1B pipelineing --- megatron/core/pipeline_parallel/schedules.py | 34 ++++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/megatron/core/pipeline_parallel/schedules.py b/megatron/core/pipeline_parallel/schedules.py index ac4973a0efd..f77049f60c5 100644 --- a/megatron/core/pipeline_parallel/schedules.py +++ b/megatron/core/pipeline_parallel/schedules.py @@ -2131,7 +2131,7 @@ def forward_backward_pipelining_without_interleaving( # Needed only when gradients are finalized in M-Core if config.finalize_model_grads_func is not None and not forward_only: embedding_module = clear_embedding_activation_buffer( - config, model, p2p_communicator.is_last_stage + config, model, p2p_communicator.is_pp_last_stage ) if config.timers is not None: @@ -2224,7 +2224,7 @@ def enable_grad_sync(): checkpoint_activations_microbatch = None input_tensor = p2p_communicator.recv_forward( - recv_tensor_shapes, p2p_communicator.is_first_stage + recv_tensor_shapes, p2p_communicator.is_pp_first_stage ) output_tensor, num_tokens = forward_step( forward_step_func, @@ -2239,22 +2239,22 @@ def enable_grad_sync(): checkpoint_activations_microbatch=checkpoint_activations_microbatch, is_first_microbatch=check_first_val_step(first_val_step, forward_only, i == 0), current_microbatch=i, - is_last_stage=p2p_communicator.is_last_stage, + is_last_stage=p2p_communicator.is_pp_last_stage, ) - p2p_communicator.send_forward(output_tensor, p2p_communicator.is_last_stage) + p2p_communicator.send_forward(output_tensor, p2p_communicator.is_pp_last_stage) total_num_tokens += num_tokens if not forward_only: input_tensors.append(input_tensor) output_tensors.append(output_tensor) - deallocate_output_tensor(output_tensor[0], config.deallocate_pipeline_outputs) + deallocate_output_tensor(output_tensor, config.deallocate_pipeline_outputs) # Before running 1F1B, need to receive first forward tensor. # If all microbatches are run in warmup / cooldown phase, then no need to # receive this tensor here. if num_microbatches_remaining > 0: input_tensor = p2p_communicator.recv_forward( - recv_tensor_shapes, p2p_communicator.is_first_stage + recv_tensor_shapes, p2p_communicator.is_pp_first_stage ) # Run 1F1B in steady state. @@ -2284,13 +2284,13 @@ def enable_grad_sync(): first_val_step, forward_only, (i == 0) and (num_warmup_microbatches == 0) ), current_microbatch=i + num_warmup_microbatches, - is_last_stage=p2p_communicator.is_last_stage, + is_last_stage=p2p_communicator.is_pp_last_stage, ) total_num_tokens += num_tokens if forward_only: p2p_communicator.send_forward( - output_tensor, p2p_communicator.is_last_stage + output_tensor, p2p_communicator.is_pp_last_stage ) if not last_iteration: input_tensor = p2p_communicator.recv_forward( @@ -2298,13 +2298,13 @@ def enable_grad_sync(): ) else: output_tensor_grad = p2p_communicator.send_forward_recv_backward( - output_tensor, send_tensor_shapes, p2p_communicator.is_last_stage + output_tensor, send_tensor_shapes, p2p_communicator.is_pp_last_stage ) # Add input_tensor and output_tensor to end of list. input_tensors.append(input_tensor) output_tensors.append(output_tensor) - deallocate_output_tensor(output_tensor[0], config.deallocate_pipeline_outputs) + deallocate_output_tensor(output_tensor, config.deallocate_pipeline_outputs) # Pop input_tensor and output_tensor from the start of the list for # the backward pass. @@ -2314,7 +2314,7 @@ def enable_grad_sync(): # Enable grad sync for the last microbatch in the batch if the full # backward pass completes in the 1F1B stage. if num_warmup_microbatches == 0 and last_iteration: - if config.grad_sync_func is None or p2p_communicator.is_first_stage: + if config.grad_sync_func is None or p2p_communicator.is_pp_first_stage: enable_grad_sync() input_tensor_grad = backward_step( @@ -2324,13 +2324,13 @@ def enable_grad_sync(): if last_iteration: input_tensor = None p2p_communicator.send_backward( - input_tensor_grad, p2p_communicator.is_first_stage + input_tensor_grad, p2p_communicator.is_pp_first_stage ) else: input_tensor = p2p_communicator.send_backward_recv_forward( input_tensor_grad, recv_tensor_shapes, - p2p_communicator.is_first_stage, + p2p_communicator.is_pp_first_stage, ) # Run cooldown backward passes. @@ -2343,14 +2343,14 @@ def enable_grad_sync(): # pipeline stages do grad reduction during pipeline # bubble. if i == num_warmup_microbatches - 1: - if config.grad_sync_func is None or p2p_communicator.is_first_stage: + if config.grad_sync_func is None or p2p_communicator.is_pp_first_stage: enable_grad_sync() input_tensor = input_tensors.pop(0) output_tensor = output_tensors.pop(0) output_tensor_grad = p2p_communicator.recv_backward( - send_tensor_shapes, p2p_communicator.is_last_stage + send_tensor_shapes, p2p_communicator.is_pp_last_stage ) input_tensor_grad = backward_step( @@ -2358,7 +2358,7 @@ def enable_grad_sync(): ) p2p_communicator.send_backward( - input_tensor_grad, p2p_communicator.is_first_stage + input_tensor_grad, p2p_communicator.is_pp_first_stage ) # Launch any remaining grad reductions. @@ -2372,7 +2372,7 @@ def enable_grad_sync(): # If defer_embedding_wgrad_compute is enabled we need to do the # weight gradient GEMM's here. finish_embedding_wgrad_compute( - config, embedding_module, p2p_communicator.is_last_stage, tp_group + config, embedding_module, p2p_communicator.is_pp_last_stage, tp_group ) # Finalize model grads (perform full grad all-reduce / reduce-scatter for From fb36fd2b343e92a253cbc795668578277b5b0cd5 Mon Sep 17 00:00:00 2001 From: ykarnati Date: Sun, 2 Nov 2025 14:29:44 -0800 Subject: [PATCH 07/16] wrap with DDP added to transformer block --- .../test_bridge_communicator.py | 50 +++++++++++++------ 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/tests/unit_tests/pipeline_parallel/test_bridge_communicator.py b/tests/unit_tests/pipeline_parallel/test_bridge_communicator.py index 4b426b718eb..f2d87f2f2ee 100644 --- a/tests/unit_tests/pipeline_parallel/test_bridge_communicator.py +++ b/tests/unit_tests/pipeline_parallel/test_bridge_communicator.py @@ -7,23 +7,19 @@ import torch.distributed as dist from packaging import version -from megatron.core import parallel_state from megatron.core.hyper_comm_grid import HyperCommGrid -from megatron.core.model_parallel_config import ModelParallelConfig from megatron.core.models.gpt.gpt_layer_specs import get_gpt_layer_with_transformer_engine_spec from megatron.core.parallel_state import ( get_context_parallel_group, - get_expert_model_parallel_rank, get_tensor_model_parallel_rank, ) from megatron.core.pipeline_parallel.bridge_communicator import BridgeCommunicator from megatron.core.process_groups_config import ProcessGroupCollection -from megatron.core.tensor_parallel.layers import ColumnParallelLinear, RowParallelLinear from megatron.core.tensor_parallel.random import model_parallel_cuda_manual_seed from megatron.core.transformer.transformer_block import TransformerBlock from megatron.core.transformer.transformer_config import TransformerConfig from tests.unit_tests.test_utilities import Utils - +from megatron.core.distributed import DistributedDataParallel, DistributedDataParallelConfig def _create_transformer_block( dtype=torch.bfloat16, hidden_size=4096, pg_collection=None @@ -108,19 +104,15 @@ def _shard_and_copy_( ) -def create_hypercomm_grid(offset=0, tp=1, cp=1, pp=1, dp=1): +def create_hypercomm_grid(offset=0, tp=1, cp=1, pp=1, dp=1, ep=1, etp=1): """Create a HyperCommGrid with tensor parallelism=2, context parallelism=2, and data parallelism=2.""" # Set up environment for world size 8 if not already set - if not dist.is_initialized(): - raise RuntimeError("Distributed process group is not initialized") - - # tests below assume a world size of 8 if "WORLD_SIZE" not in os.environ: os.environ["WORLD_SIZE"] = "8" grid = HyperCommGrid( - shape=[tp, cp, pp, dp], - dim_names=["tp", "cp", "pp", "dp"], + shape=[tp, cp, pp, dp, ep, etp], + dim_names=["tp", "cp", "pp", "dp", "ep", "etp"], rank_offset=offset, backend="nccl", ) @@ -128,6 +120,12 @@ def create_hypercomm_grid(offset=0, tp=1, cp=1, pp=1, dp=1): _ = grid.create_pg(["cp"]) _ = grid.create_pg(["pp"]) _ = grid.create_pg(["dp"]) + _ = grid.create_pg(["ep"]) + _ = grid.create_pg(["tp", "pp"]) + _ = grid.create_pg(["dp", "cp"]) + _ = grid.create_pg(["tp", "cp"]) + _ = grid.create_pg(["tp", "dp", "cp"]) + _ = grid.create_pg(["tp", "ep", "pp"]) return grid @@ -136,6 +134,16 @@ def _get_pg_collection_from_grid(grid): pg_collection.tp = grid.get_pg("tp") pg_collection.cp = grid.get_pg("cp") pg_collection.pp = grid.get_pg("pp") + pg_collection.ep = grid.get_pg("ep") + dp_group = grid.get_pg("dp") + dp_cp_group = grid.get_pg(["dp", "cp"]) + pg_collection.dp = dp_group + pg_collection.dp_cp = dp_cp_group + pg_collection.mp = grid.get_pg(["tp", "pp"]) + pg_collection.dp_cp = grid.get_pg(["dp", "cp"]) + pg_collection.tp_cp = grid.get_pg(["tp", "cp"]) + pg_collection.tp_dp_cp = grid.get_pg(["tp", "dp", "cp"]) + pg_collection.tp_ep_pp = grid.get_pg(["tp", "ep", "pp"]) return pg_collection @@ -147,7 +155,7 @@ def _avg_params(module: torch.nn.Module, group: dist.ProcessGroup = None) -> Non def get_transformer_block_and_grid( - ref_block, + ref_block = None, tp_size=1, cp_size=1, pp_size=1, @@ -156,13 +164,15 @@ def get_transformer_block_and_grid( use_global_parallel_state: bool = False, hidden_size: int = 4096, dtype: torch.dtype = torch.bfloat16, + wrap_with_ddp: bool = False, ): """Utility to build a ``TransformerBlock`` for tests.""" current_rank = dist.get_rank() if use_global_parallel_state: block = _create_transformer_block(dtype=dtype, hidden_size=hidden_size) - _shard_and_copy_(ref_block, block, tp_size, get_tensor_model_parallel_rank()) + if ref_block is not None: + _shard_and_copy_(ref_block, block, tp_size, get_tensor_model_parallel_rank()) grid = None else: grid = create_hypercomm_grid( @@ -173,9 +183,19 @@ def get_transformer_block_and_grid( block = _create_transformer_block( dtype=dtype, hidden_size=hidden_size, pg_collection=pg_collection ) - _shard_and_copy_(ref_block, block, tp_size, pg_collection.tp.rank()) + if ref_block is not None: + _shard_and_copy_(ref_block, block, tp_size, pg_collection.tp.rank()) else: block = None + + if wrap_with_ddp and block is not None: + ddp_config = DistributedDataParallelConfig(overlap_grad_reduce=True, bucket_size=10000) + block = DistributedDataParallel( + config=block.config, ddp_config=ddp_config, module=block, pg_collection=pg_collection + ) + block.pre_process = False + block.post_process = False + block.share_embeddings_and_output_weights = False return block, grid From 35d3d43d3bba2133fdc54d34a53d05e2f457a26a Mon Sep 17 00:00:00 2001 From: ykarnati Date: Sun, 2 Nov 2025 14:30:17 -0800 Subject: [PATCH 08/16] multimodule schedule tests --- .../test_multimodule_schedules.py | 373 ++++++++++++++++++ 1 file changed, 373 insertions(+) create mode 100644 tests/unit_tests/pipeline_parallel/test_multimodule_schedules.py diff --git a/tests/unit_tests/pipeline_parallel/test_multimodule_schedules.py b/tests/unit_tests/pipeline_parallel/test_multimodule_schedules.py new file mode 100644 index 00000000000..724f7906737 --- /dev/null +++ b/tests/unit_tests/pipeline_parallel/test_multimodule_schedules.py @@ -0,0 +1,373 @@ +import logging +import os +from typing import Dict, List +from contextlib import contextmanager +import pytest +import torch +import torch.distributed as dist +from packaging import version + + +import megatron.core.pipeline_parallel.schedules as schedule +from megatron.core import ModelParallelConfig +from megatron.core.distributed.finalize_model_grads import finalize_model_grads +from megatron.core.hyper_comm_grid import HyperCommGrid +from megatron.core.model_parallel_config import ModelParallelConfig + +from megatron.core.pipeline_parallel.multimodule_communicator import ( + MultiModulePipelineCommunicator, +) + +from megatron.core.pipeline_parallel.utils import is_pp_first_stage, is_pp_last_stage + +from tests.unit_tests.test_utilities import Utils +from tests.unit_tests.pipeline_parallel.test_bridge_communicator import ( + _get_pg_collection_from_grid, + get_transformer_block_and_grid, +) +from tests.unit_tests.pipeline_parallel.test_schedules import _populate_embedding_and_position_groups + +rank = Utils.rank +logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') + +class DataIterator: + + def __init__(self, hidden_size: int, seq_length: int, micro_batch_size: int): + self.hidden_size = hidden_size + self.seq_length = seq_length + self.micro_batch_size = micro_batch_size + + def __iter__(self): + return self + + def __next__(self): + return torch.randn( + self.seq_length, + self.micro_batch_size, + self.hidden_size, + device='cuda', + dtype=torch.bfloat16, + ) + + + +class SingleEncoderModel(torch.nn.Module): + def __init__( + self, + hidden_size, + encoder_tp, + encoder_pp, + encoder_dp, + llm_tp, + llm_pp, + llm_dp, + llm_grid_offset, + ): + + super().__init__() + + self.encoder, self.encoder_grid = get_transformer_block_and_grid( + tp_size=encoder_tp, + cp_size=1, + pp_size=encoder_pp, + dp_size=encoder_dp, + hidden_size=hidden_size, + wrap_with_ddp=True, + ) + + self.llm, self.llm_grid = get_transformer_block_and_grid( + tp_size=llm_tp, + cp_size=1, + pp_size=llm_pp, + dp_size=llm_dp, + grid_offset=llm_grid_offset, + hidden_size=hidden_size, + wrap_with_ddp=True, + ) + + # Simple list for iteration + self.modules_and_grids = [ + (self.encoder, self.encoder_grid), + (self.llm, self.llm_grid) + ] + + self.current_rank = dist.get_rank() + self.encoder_input_tensor = None + self.llm_input_tensor = None + + + def finish_grad_sync(self): + """Finish gradient synchronization for all active modules on this rank.""" + for module, grid in self.modules_and_grids: + if module is not None and self.is_current_rank_in_grid(grid): + module.finish_grad_sync() + @contextmanager + def no_sync(self): + contexts = [] + if self.is_current_rank_in_grid(self.encoder_grid): + contexts.append(self.encoder.no_sync()) + if self.is_current_rank_in_grid(self.llm_grid): + contexts.append(self.llm.no_sync()) + + # Enter all contexts + for ctx in contexts: + ctx.__enter__() + + try: + yield + finally: + # Exit all contexts in reverse order + for ctx in reversed(contexts): + ctx.__exit__(None, None, None) + + @property + def ddp_config(self): + # Try to get ddp_config from the first available module on this rank + for module, grid in self.modules_and_grids: + if module is not None and self.is_current_rank_in_grid(grid): + return module.ddp_config + raise AttributeError( + f"No active modules with ddp_config found on rank {self.current_rank}" + ) + + def scale_gradients(self, scaling_factor: float): + """Scale gradients for all active modules on this rank.""" + for module, grid in self.modules_and_grids: + if module is not None and self.is_current_rank_in_grid(grid): + module.scale_gradients(scaling_factor) + + def is_current_rank_in_grid(self, grid: HyperCommGrid) -> bool: + """Check if the current rank is in the grid.""" + return grid.rank_offset <= self.current_rank < (grid.rank_offset + grid.size) + + def finalize_model_grads(self, module=None, num_tokens=None, pg_collection=None): + for module, grid in self.modules_and_grids: + if module is not None and self.is_current_rank_in_grid(grid): + finalize_model_grads([module], num_tokens=None, pg_collection=_get_pg_collection_with_embedding_groups(grid)) + + @contextmanager + def no_sync(self): + contexts = [] + for module, grid in self.modules_and_grids: + if module is not None and self.is_current_rank_in_grid(grid): + contexts.append(module.no_sync()) + + # Enter all contexts + for ctx in contexts: + ctx.__enter__() + + try: + yield + finally: + # Exit all contexts in reverse order + for ctx in reversed(contexts): + ctx.__exit__(None, None, None) + + def set_input_tensor(self, input_tensor: List[Dict[str, torch.Tensor]]): + if self.is_current_rank_in_grid(self.encoder_grid) and 'encoder' in input_tensor[0]: + if isinstance(input_tensor[0]["encoder"], list): + encoder_input_tensor = input_tensor[0]["encoder"][0] + else: + encoder_input_tensor = input_tensor[0]["encoder"] + logging.debug( + f"[Rank {dist.get_rank()} ][SingleEncoderModel] [set_input_tensor] [encoder] input tensor shape: {input_tensor[0]['encoder'][0].shape}" + ) + self.encoder_input_tensor = encoder_input_tensor + elif self.is_current_rank_in_grid(self.llm_grid): + if 'llm' in input_tensor[0]: + if isinstance(input_tensor[0]["llm"], list): + llm_input_tensor = input_tensor[0]["llm"][0] + else: + llm_input_tensor = input_tensor[0]["llm"] + logging.debug( + f"[Rank {dist.get_rank()} ][SingleEncoderModel] [set_input_tensor] [llm] input tensor shape: {llm_input_tensor.shape}" + ) + self.llm_input_tensor = llm_input_tensor + elif 'encoder' in input_tensor[0]: + logging.debug( + f"[Rank {dist.get_rank()} ][SingleEncoderModel] [set_input_tensor] [encoder] input tensor shape: {input_tensor[0]['encoder'].shape}" + ) + self.llm_input_tensor = input_tensor[0]["encoder"] + else: + raise ValueError(f"Rank {dist.get_rank()} is not valid") + + def forward(self, hidden_states): + + current_rank = dist.get_rank() + output_dict = {} + if self.is_current_rank_in_grid(self.encoder_grid): + # if pp rank > 0 in encoder pp group then we use self.encoder_input_tensor as input else we use hidden_states + if is_pp_first_stage(self.encoder_grid.get_pg("pp")): + input_tensor = hidden_states + else: + assert ( + self.encoder_input_tensor is not None + ), "Encoder input tensor is not provided for pp rank > 0" + input_tensor = self.encoder_input_tensor + logging.debug( + f"[Rank {dist.get_rank()} ][SingleEncoderModel] [forward] [encoder] input tensor shape: {input_tensor.shape}" + ) + output_dict["encoder"] = self.encoder(input_tensor, attention_mask=None) + elif self.is_current_rank_in_grid(self.llm_grid): + assert ( + self.llm_input_tensor is not None + ), "LLM input tensor is not provided for pp rank > 0" + input_tensor = self.llm_input_tensor + logging.debug( + f"[Rank {dist.get_rank()} ][SingleEncoderModel] [forward] [llm] input tensor shape: {input_tensor.shape}" + ) + output_dict["llm"] = self.llm(input_tensor, attention_mask=None) + else: + raise ValueError(f"Rank {current_rank} is not valid") + + return output_dict + + + +def _get_pg_collection_with_embedding_groups(grid): + pg_collection = _get_pg_collection_from_grid(grid) + if pg_collection.pp: + pos_embd_pg, embd_pg = _populate_embedding_and_position_groups(pg_collection.pp) + pos_embd_pg = pos_embd_pg if is_pp_first_stage(pg_collection.pp) else None + embd_pg = ( + embd_pg + if (is_pp_last_stage(pg_collection.pp) or is_pp_first_stage(pg_collection.pp)) + else None + ) + pg_collection.pos_embd = pos_embd_pg + pg_collection.embd = embd_pg + + return pg_collection + + +@pytest.mark.skipif( + version.parse(torch.__version__) < version.parse('2.3.0'), + reason="Device mesh feature requires PyTorch 2.3 or later", +) +@pytest.mark.parametrize( + "encoder_tp,encoder_pp,encoder_dp,llm_tp,llm_pp,llm_dp,llm_grid_offset", + [ + # (2, 2, 1, 2, 2, 1, 4), + # (4, 1, 1, 2, 2, 1, 4), + (2, 1, 1, 1, 6, 1, 2), + ], +) +def test_forward_backward_pipelining_without_interleaving_multi_module_single_encoder( + encoder_tp, encoder_pp, encoder_dp, llm_tp, llm_pp, llm_dp, llm_grid_offset +): + + Utils.initialize_distributed() + + def step_func(data_iterator, model): + + def loss_func(output_tensor_dict: Dict[str, torch.Tensor]): + assert ( + 'llm' in output_tensor_dict + ), f'llm is not in output_tensor_dict: {output_tensor_dict}' + loss = output_tensor_dict['llm'].sum() + return loss, {'loss_reduced': loss} + + if data_iterator is not None: + input_tensor = next(data_iterator) + else: + input_tensor = None + + model_output = model(input_tensor) + + return model_output, loss_func + + sequence_length = 512 + micro_batch_size = 1 + hidden_size = 1024 + + # Create model + model = SingleEncoderModel( + hidden_size=hidden_size, + encoder_tp=encoder_tp, + encoder_pp=encoder_pp, + encoder_dp=encoder_dp, + llm_tp=llm_tp, + llm_pp=llm_pp, + llm_dp=llm_dp, + llm_grid_offset=llm_grid_offset, + ) + model.model_type = 'unit-test' + + module_to_grid_map = {'encoder': model.encoder_grid, 'llm': model.llm_grid} + topology = { + 'encoder': ['llm'], # image_encoder sends forward results to llm + 'llm': [], # llm is the last stage here + } + config = ModelParallelConfig(pipeline_dtype=torch.bfloat16) + config.calculate_per_token_loss = False + config.qk_layernorm = False + config.sequence_parallel = False + config.moe_router_enable_expert_bias = False + config.moe_router_load_balancing_type = "aux_loss" + config.variable_seq_lengths = True + config.no_sync_func = model.no_sync + config.finalize_model_grads_func = model.finalize_model_grads + config.fine_grained_activation_offloading = False + + + # Add grad scale function to convert float losses to tensors + def grad_scale_func(loss): + if isinstance(loss, (int, float)): + return torch.tensor(loss, dtype=torch.float32, device='cuda', requires_grad=True) + else: + return loss # Already a tensor + + config.grad_scale_func = grad_scale_func + model.config = config + config.hidden_size = hidden_size + + multimodule_communicator = MultiModulePipelineCommunicator( + module_to_grid_map, topology, config, dim_mapping={'s': 0, 'h': 2, 'b': 1} + ) + + data_iterator = None + if model.is_current_rank_in_grid(model.encoder_grid) and is_pp_first_stage( + model.encoder_grid.get_pg("pp") + ): + data_iterator = DataIterator( + hidden_size=hidden_size, seq_length=sequence_length, micro_batch_size=micro_batch_size + ) + + common_args = { + 'forward_step_func': step_func, + 'data_iterator': data_iterator, + 'model': [model], + 'num_microbatches': 16, + 'seq_length': sequence_length, + 'micro_batch_size': micro_batch_size, + 'forward_only': False, + } + + if 0 <= dist.get_rank() < llm_grid_offset: + pg_collection = _get_pg_collection_with_embedding_groups(model.encoder_grid) + elif llm_grid_offset <= dist.get_rank() < llm_grid_offset + model.llm_grid.size: + pg_collection = _get_pg_collection_with_embedding_groups(model.llm_grid) + else: + raise ValueError(f"Rank {dist.get_rank()} is not valid") + + losses_reduced_explicit = schedule.forward_backward_pipelining_without_interleaving( + p2p_communicator=multimodule_communicator, pg_collection=pg_collection, **common_args + ) + logging.info(f"Losses reduced explicit: {losses_reduced_explicit}") + + + +if __name__ == "__main__": + + logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') + + # Use the same parameters as defined in the pytest.mark.parametrize decorator + test_forward_backward_pipelining_without_interleaving_multi_module_single_encoder( + encoder_tp=2, + encoder_pp=1, + encoder_dp=1, + llm_tp=1, + llm_pp=6, + llm_dp=1, + llm_grid_offset=2 + ) \ No newline at end of file From 7ef2a33bf7b98a17b5ed101565869eae4314c7af Mon Sep 17 00:00:00 2001 From: ykarnati Date: Sun, 2 Nov 2025 16:27:26 -0800 Subject: [PATCH 09/16] set batch p2p comm False for reliable pp communication --- .../test_multimodule_schedules.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/tests/unit_tests/pipeline_parallel/test_multimodule_schedules.py b/tests/unit_tests/pipeline_parallel/test_multimodule_schedules.py index 724f7906737..099674e8ee6 100644 --- a/tests/unit_tests/pipeline_parallel/test_multimodule_schedules.py +++ b/tests/unit_tests/pipeline_parallel/test_multimodule_schedules.py @@ -247,9 +247,10 @@ def _get_pg_collection_with_embedding_groups(grid): @pytest.mark.parametrize( "encoder_tp,encoder_pp,encoder_dp,llm_tp,llm_pp,llm_dp,llm_grid_offset", [ - # (2, 2, 1, 2, 2, 1, 4), - # (4, 1, 1, 2, 2, 1, 4), + (2, 2, 1, 2, 2, 1, 4), + (4, 1, 1, 2, 2, 1, 4), (2, 1, 1, 1, 6, 1, 2), + (2, 2, 1, 1, 4, 1, 4) ], ) def test_forward_backward_pipelining_without_interleaving_multi_module_single_encoder( @@ -277,7 +278,7 @@ def loss_func(output_tensor_dict: Dict[str, torch.Tensor]): return model_output, loss_func sequence_length = 512 - micro_batch_size = 1 + micro_batch_size = 4 hidden_size = 1024 # Create model @@ -305,6 +306,7 @@ def loss_func(output_tensor_dict: Dict[str, torch.Tensor]): config.moe_router_enable_expert_bias = False config.moe_router_load_balancing_type = "aux_loss" config.variable_seq_lengths = True + config.batch_p2p_comm = False config.no_sync_func = model.no_sync config.finalize_model_grads_func = model.finalize_model_grads config.fine_grained_activation_offloading = False @@ -364,10 +366,10 @@ def grad_scale_func(loss): # Use the same parameters as defined in the pytest.mark.parametrize decorator test_forward_backward_pipelining_without_interleaving_multi_module_single_encoder( encoder_tp=2, - encoder_pp=1, + encoder_pp=2, encoder_dp=1, - llm_tp=1, - llm_pp=6, + llm_tp=2, + llm_pp=2, llm_dp=1, - llm_grid_offset=2 + llm_grid_offset=4 ) \ No newline at end of file From 206892934fe7ecef3bd18b4a37f087569c72703f Mon Sep 17 00:00:00 2001 From: ykarnati Date: Sun, 2 Nov 2025 16:40:27 -0800 Subject: [PATCH 10/16] precommit hooks --- .../multimodule_communicator.py | 26 +++---- megatron/core/pipeline_parallel/schedules.py | 67 +++++++++++------- .../test_bridge_communicator.py | 12 ++-- .../test_multimodule_communicator.py | 2 +- .../test_multimodule_schedules.py | 68 +++++++------------ 5 files changed, 83 insertions(+), 92 deletions(-) diff --git a/megatron/core/pipeline_parallel/multimodule_communicator.py b/megatron/core/pipeline_parallel/multimodule_communicator.py index 02607cc625f..56067573b78 100644 --- a/megatron/core/pipeline_parallel/multimodule_communicator.py +++ b/megatron/core/pipeline_parallel/multimodule_communicator.py @@ -15,9 +15,10 @@ # Types 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). """ @@ -30,7 +31,7 @@ def _ensure_3d_tensor(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). """ @@ -40,6 +41,7 @@ def _restore_tensor_shape(tensor): return tensor.squeeze(-1) return tensor + @dataclass class RankModuleInfo: """Information about a rank in a module.""" @@ -310,9 +312,7 @@ def send_forward(self, output_dict: Dict[str, torch.Tensor], is_last_stage: bool else: # If not last stage, send forward activation by using P2P communicator. tensor_to_send = _ensure_3d_tensor(output_dict[module_name]) - rank_module_info.p2p_communicator.send_forward( - tensor_to_send, is_last_stage=False - ) + rank_module_info.p2p_communicator.send_forward(tensor_to_send, is_last_stage=False) def send_forward_recv_backward( self, @@ -336,9 +336,7 @@ def send_forward_recv_backward( # receive backward gradient by using bridge communicator. for bridge_comm in rank_module_info.bridge_comms_as_src_module: tensor_to_send = _ensure_3d_tensor(output_dict[module_name]) - grad_tensor = bridge_comm.send_forward_recv_backward( - tensor_to_send - ) + 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 @@ -377,12 +375,10 @@ def send_backward_recv_forward( # If first stage, and has incoming modules, send backward gradient and # receive forward activation by using bridge communicator. grad_to_send = _ensure_3d_tensor(grad_dict[bridge_comm.src_module_name]) - activation_tensor = ( - bridge_comm.send_backward_recv_forward( - grad_to_send - ) + activation_tensor = bridge_comm.send_backward_recv_forward(grad_to_send) + input_dict[bridge_comm.src_module_name] = _restore_tensor_shape( + activation_tensor ) - 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. @@ -444,9 +440,7 @@ def send_backward(self, grad_dict: Dict[str, torch.Tensor], is_first_stage: bool else: # If not first stage, send backward activation by using P2P communicator. grad_to_send = _ensure_3d_tensor(grad_dict[module_name]) - rank_module_info.p2p_communicator.send_backward( - grad_to_send, is_first_stage=False - ) + rank_module_info.p2p_communicator.send_backward(grad_to_send, is_first_stage=False) @staticmethod def compute_total_pipeline_stages( diff --git a/megatron/core/pipeline_parallel/schedules.py b/megatron/core/pipeline_parallel/schedules.py index f77049f60c5..fe7f42ae1a0 100644 --- a/megatron/core/pipeline_parallel/schedules.py +++ b/megatron/core/pipeline_parallel/schedules.py @@ -2,8 +2,8 @@ import contextlib from functools import partial -from typing import Callable, Dict, Iterator, List, Optional, Union -from typing import Any +from typing import Any, Callable, Dict, Iterator, List, Optional, Union + import torch from torch.autograd.variable import Variable @@ -12,6 +12,7 @@ from megatron.core.pipeline_parallel.fine_grained_activation_offload import ( fine_grained_offloading_reset, ) +from megatron.core.pipeline_parallel.multimodule_communicator import MultiModulePipelineCommunicator from megatron.core.pipeline_parallel.p2p_communication import P2PCommunicator from megatron.core.pipeline_parallel.utils import ( is_pp_first_stage, @@ -30,9 +31,7 @@ nvtx_range_pop, nvtx_range_push, ) -from megatron.core.pipeline_parallel.multimodule_communicator import ( - MultiModulePipelineCommunicator, -) + from .combined_1f1b import ( combined_1f1b_schedule_for_interleaved_pipelining, combined_1f1b_schedule_for_no_pipelining, @@ -137,7 +136,10 @@ def forward_step(data_iterator, model): return forward_backward_func -def deallocate_output_tensor(out: Union[torch.Tensor, List[torch.Tensor], Dict[str, torch.Tensor]], deallocate_pipeline_outputs=False): +def deallocate_output_tensor( + out: Union[torch.Tensor, List[torch.Tensor], Dict[str, torch.Tensor]], + deallocate_pipeline_outputs=False, +): '''Pseudo-deallocate (i.e., set to scalar) the output tensor's '.data' field. This method should be called right after the output tensor has been @@ -149,12 +151,12 @@ def deallocate_output_tensor(out: Union[torch.Tensor, List[torch.Tensor], Dict[s if isinstance(out, torch.Tensor): assert out._base is None, "counter-productive to free a view of another tensor." out.data = torch.empty((1,), device=out.device, dtype=out.dtype) - + if isinstance(out, dict): for v in out.values(): deallocate_output_tensor(v, deallocate_pipeline_outputs) return - + if isinstance(out, (list, tuple)): for v in out: deallocate_output_tensor(v, deallocate_pipeline_outputs) @@ -501,7 +503,13 @@ def backward_step_tensor(input_tensor, output_tensor, output_tensor_grad, model_ return input_tensor_grad -def _backward_step_dict(input_tensor: Dict[str, torch.Tensor], output_tensor: Union[torch.Tensor, Dict[str, torch.Tensor]], output_tensor_grad: Union[torch.Tensor, Dict[str, torch.Tensor]], model_type: str, config: Any): +def _backward_step_dict( + input_tensor: Dict[str, torch.Tensor], + output_tensor: Union[torch.Tensor, Dict[str, torch.Tensor]], + output_tensor_grad: Union[torch.Tensor, Dict[str, torch.Tensor]], + model_type: str, + config: Any, +): """Backward step implementation when inputs/outputs are dictionaries (multi module case).""" if config.timers is not None: @@ -564,7 +572,13 @@ def _backward_step_dict(input_tensor: Dict[str, torch.Tensor], output_tensor: Un return input_tensor_grad -def backward_step(input_tensor: Union[torch.Tensor, Dict[str, torch.Tensor]], output_tensor: Union[torch.Tensor, Dict[str, torch.Tensor]], output_tensor_grad: Union[torch.Tensor, Dict[str, torch.Tensor]], model_type: str, config: Any): +def backward_step( + input_tensor: Union[torch.Tensor, Dict[str, torch.Tensor]], + output_tensor: Union[torch.Tensor, Dict[str, torch.Tensor]], + output_tensor_grad: Union[torch.Tensor, Dict[str, torch.Tensor]], + model_type: str, + config: Any, +): """Backward step wrapper support both tensor and dictionary formats The inputs and outputs are dictionaries for multimodule case. @@ -575,9 +589,13 @@ def backward_step(input_tensor: Union[torch.Tensor, Dict[str, torch.Tensor]], ou """ if isinstance(input_tensor, dict): - return _backward_step_dict(input_tensor, output_tensor, output_tensor_grad, model_type, config) + return _backward_step_dict( + input_tensor, output_tensor, output_tensor_grad, model_type, config + ) else: - return backward_step_tensor(input_tensor, output_tensor, output_tensor_grad, model_type, config) + return backward_step_tensor( + input_tensor, output_tensor, output_tensor_grad, model_type, config + ) def check_first_val_step(first_val_step, forward_only, cond): @@ -2040,7 +2058,9 @@ def get_tensor_shapes( tensor_shapes.append(()) return tensor_shapes else: - assert tp_group is not None and cp_group is not None, "tp_group and cp_group must be provided" + assert ( + tp_group is not None and cp_group is not None + ), "tp_group and cp_group must be provided" # Use decoder_seq_length if provided, otherwise use seq_length effective_seq_length = decoder_seq_length if decoder_seq_length is not None else seq_length effective_seq_length = effective_seq_length // cp_group.size() @@ -2087,7 +2107,7 @@ def forward_backward_pipelining_without_interleaving( raise ValueError( "Non-interleaved pipeline parallelism does not support overlapping p2p communication" ) - tp_group, cp_group, llm_cp_size = None, None, None + tp_group, cp_group, llm_cp_size = None, None, None if p2p_communicator is None and pg_collection is None: p2p_communicator = P2PCommunicator( pp_group=parallel_state.get_pipeline_model_parallel_group(), config=config @@ -2111,9 +2131,12 @@ def forward_backward_pipelining_without_interleaving( elif p2p_communicator is not None and pg_collection is not None: if isinstance(pg_collection, list): # cases when multiple modules are colocated - assert config.variable_seq_lengths, "variable seq_lengths is required when multiple modules are colocated" + assert ( + config.variable_seq_lengths + ), "variable seq_lengths is required when multiple modules are colocated" # when llm is colocated for now assume last collection in the list is the llm - # TODO: ykarnati: Have a better interface to handle this (without breaking backward compatibility) + # TODO: ykarnati: Have a better interface to handle this + # (without breaking backward compatibility) assert hasattr(pg_collection[-1], 'cp'), "pg_collection must have cp_group" llm_cp_size = pg_collection[-1].cp.size() else: @@ -2289,9 +2312,7 @@ def enable_grad_sync(): total_num_tokens += num_tokens if forward_only: - p2p_communicator.send_forward( - output_tensor, p2p_communicator.is_pp_last_stage - ) + p2p_communicator.send_forward(output_tensor, p2p_communicator.is_pp_last_stage) if not last_iteration: input_tensor = p2p_communicator.recv_forward( recv_tensor_shapes, p2p_communicator.is_pp_first_stage @@ -2328,9 +2349,7 @@ def enable_grad_sync(): ) else: input_tensor = p2p_communicator.send_backward_recv_forward( - input_tensor_grad, - recv_tensor_shapes, - p2p_communicator.is_pp_first_stage, + input_tensor_grad, recv_tensor_shapes, p2p_communicator.is_pp_first_stage ) # Run cooldown backward passes. @@ -2357,9 +2376,7 @@ def enable_grad_sync(): input_tensor, output_tensor, output_tensor_grad, model_type, config ) - p2p_communicator.send_backward( - input_tensor_grad, p2p_communicator.is_pp_first_stage - ) + p2p_communicator.send_backward(input_tensor_grad, p2p_communicator.is_pp_first_stage) # Launch any remaining grad reductions. if no_sync_context is not None: diff --git a/tests/unit_tests/pipeline_parallel/test_bridge_communicator.py b/tests/unit_tests/pipeline_parallel/test_bridge_communicator.py index f2d87f2f2ee..b0db1a273d2 100644 --- a/tests/unit_tests/pipeline_parallel/test_bridge_communicator.py +++ b/tests/unit_tests/pipeline_parallel/test_bridge_communicator.py @@ -7,19 +7,17 @@ import torch.distributed as dist from packaging import version +from megatron.core.distributed import DistributedDataParallel, DistributedDataParallelConfig from megatron.core.hyper_comm_grid import HyperCommGrid from megatron.core.models.gpt.gpt_layer_specs import get_gpt_layer_with_transformer_engine_spec -from megatron.core.parallel_state import ( - get_context_parallel_group, - get_tensor_model_parallel_rank, -) +from megatron.core.parallel_state import get_context_parallel_group, get_tensor_model_parallel_rank from megatron.core.pipeline_parallel.bridge_communicator import BridgeCommunicator from megatron.core.process_groups_config import ProcessGroupCollection from megatron.core.tensor_parallel.random import model_parallel_cuda_manual_seed from megatron.core.transformer.transformer_block import TransformerBlock from megatron.core.transformer.transformer_config import TransformerConfig from tests.unit_tests.test_utilities import Utils -from megatron.core.distributed import DistributedDataParallel, DistributedDataParallelConfig + def _create_transformer_block( dtype=torch.bfloat16, hidden_size=4096, pg_collection=None @@ -155,7 +153,7 @@ def _avg_params(module: torch.nn.Module, group: dist.ProcessGroup = None) -> Non def get_transformer_block_and_grid( - ref_block = None, + ref_block=None, tp_size=1, cp_size=1, pp_size=1, @@ -187,7 +185,7 @@ def get_transformer_block_and_grid( _shard_and_copy_(ref_block, block, tp_size, pg_collection.tp.rank()) else: block = None - + if wrap_with_ddp and block is not None: ddp_config = DistributedDataParallelConfig(overlap_grad_reduce=True, bucket_size=10000) block = DistributedDataParallel( diff --git a/tests/unit_tests/pipeline_parallel/test_multimodule_communicator.py b/tests/unit_tests/pipeline_parallel/test_multimodule_communicator.py index f89b8fa2a77..5bb3f1bdf62 100644 --- a/tests/unit_tests/pipeline_parallel/test_multimodule_communicator.py +++ b/tests/unit_tests/pipeline_parallel/test_multimodule_communicator.py @@ -1,5 +1,5 @@ - import os + import pytest import torch import torch.distributed as dist diff --git a/tests/unit_tests/pipeline_parallel/test_multimodule_schedules.py b/tests/unit_tests/pipeline_parallel/test_multimodule_schedules.py index 099674e8ee6..29d30d6a305 100644 --- a/tests/unit_tests/pipeline_parallel/test_multimodule_schedules.py +++ b/tests/unit_tests/pipeline_parallel/test_multimodule_schedules.py @@ -1,35 +1,33 @@ import logging import os -from typing import Dict, List from contextlib import contextmanager +from typing import Dict, List + import pytest import torch import torch.distributed as dist from packaging import version - import megatron.core.pipeline_parallel.schedules as schedule from megatron.core import ModelParallelConfig from megatron.core.distributed.finalize_model_grads import finalize_model_grads from megatron.core.hyper_comm_grid import HyperCommGrid from megatron.core.model_parallel_config import ModelParallelConfig - -from megatron.core.pipeline_parallel.multimodule_communicator import ( - MultiModulePipelineCommunicator, -) - +from megatron.core.pipeline_parallel.multimodule_communicator import MultiModulePipelineCommunicator from megatron.core.pipeline_parallel.utils import is_pp_first_stage, is_pp_last_stage - -from tests.unit_tests.test_utilities import Utils from tests.unit_tests.pipeline_parallel.test_bridge_communicator import ( _get_pg_collection_from_grid, get_transformer_block_and_grid, ) -from tests.unit_tests.pipeline_parallel.test_schedules import _populate_embedding_and_position_groups +from tests.unit_tests.pipeline_parallel.test_schedules import ( + _populate_embedding_and_position_groups, +) +from tests.unit_tests.test_utilities import Utils rank = Utils.rank logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') + class DataIterator: def __init__(self, hidden_size: int, seq_length: int, micro_batch_size: int): @@ -50,7 +48,6 @@ def __next__(self): ) - class SingleEncoderModel(torch.nn.Module): def __init__( self, @@ -86,21 +83,18 @@ def __init__( ) # Simple list for iteration - self.modules_and_grids = [ - (self.encoder, self.encoder_grid), - (self.llm, self.llm_grid) - ] + self.modules_and_grids = [(self.encoder, self.encoder_grid), (self.llm, self.llm_grid)] self.current_rank = dist.get_rank() self.encoder_input_tensor = None self.llm_input_tensor = None - def finish_grad_sync(self): """Finish gradient synchronization for all active modules on this rank.""" for module, grid in self.modules_and_grids: if module is not None and self.is_current_rank_in_grid(grid): module.finish_grad_sync() + @contextmanager def no_sync(self): contexts = [] @@ -108,11 +102,11 @@ def no_sync(self): contexts.append(self.encoder.no_sync()) if self.is_current_rank_in_grid(self.llm_grid): contexts.append(self.llm.no_sync()) - + # Enter all contexts for ctx in contexts: ctx.__enter__() - + try: yield finally: @@ -126,9 +120,7 @@ def ddp_config(self): for module, grid in self.modules_and_grids: if module is not None and self.is_current_rank_in_grid(grid): return module.ddp_config - raise AttributeError( - f"No active modules with ddp_config found on rank {self.current_rank}" - ) + raise AttributeError(f"No active modules with ddp_config found on rank {self.current_rank}") def scale_gradients(self, scaling_factor: float): """Scale gradients for all active modules on this rank.""" @@ -139,11 +131,15 @@ def scale_gradients(self, scaling_factor: float): def is_current_rank_in_grid(self, grid: HyperCommGrid) -> bool: """Check if the current rank is in the grid.""" return grid.rank_offset <= self.current_rank < (grid.rank_offset + grid.size) - + def finalize_model_grads(self, module=None, num_tokens=None, pg_collection=None): for module, grid in self.modules_and_grids: if module is not None and self.is_current_rank_in_grid(grid): - finalize_model_grads([module], num_tokens=None, pg_collection=_get_pg_collection_with_embedding_groups(grid)) + finalize_model_grads( + [module], + num_tokens=None, + pg_collection=_get_pg_collection_with_embedding_groups(grid), + ) @contextmanager def no_sync(self): @@ -151,11 +147,11 @@ def no_sync(self): for module, grid in self.modules_and_grids: if module is not None and self.is_current_rank_in_grid(grid): contexts.append(module.no_sync()) - + # Enter all contexts for ctx in contexts: ctx.__enter__() - + try: yield finally: @@ -223,7 +219,6 @@ def forward(self, hidden_states): return output_dict - def _get_pg_collection_with_embedding_groups(grid): pg_collection = _get_pg_collection_from_grid(grid) if pg_collection.pp: @@ -246,15 +241,10 @@ def _get_pg_collection_with_embedding_groups(grid): ) @pytest.mark.parametrize( "encoder_tp,encoder_pp,encoder_dp,llm_tp,llm_pp,llm_dp,llm_grid_offset", - [ - (2, 2, 1, 2, 2, 1, 4), - (4, 1, 1, 2, 2, 1, 4), - (2, 1, 1, 1, 6, 1, 2), - (2, 2, 1, 1, 4, 1, 4) - ], + [(2, 2, 1, 2, 2, 1, 4), (4, 1, 1, 2, 2, 1, 4), (2, 1, 1, 1, 6, 1, 2), (2, 2, 1, 1, 4, 1, 4)], ) def test_forward_backward_pipelining_without_interleaving_multi_module_single_encoder( - encoder_tp, encoder_pp, encoder_dp, llm_tp, llm_pp, llm_dp, llm_grid_offset + encoder_tp, encoder_pp, encoder_dp, llm_tp, llm_pp, llm_dp, llm_grid_offset ): Utils.initialize_distributed() @@ -310,7 +300,6 @@ def loss_func(output_tensor_dict: Dict[str, torch.Tensor]): config.no_sync_func = model.no_sync config.finalize_model_grads_func = model.finalize_model_grads config.fine_grained_activation_offloading = False - # Add grad scale function to convert float losses to tensors def grad_scale_func(loss): @@ -358,18 +347,11 @@ def grad_scale_func(loss): logging.info(f"Losses reduced explicit: {losses_reduced_explicit}") - if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') # Use the same parameters as defined in the pytest.mark.parametrize decorator test_forward_backward_pipelining_without_interleaving_multi_module_single_encoder( - encoder_tp=2, - encoder_pp=2, - encoder_dp=1, - llm_tp=2, - llm_pp=2, - llm_dp=1, - llm_grid_offset=4 - ) \ No newline at end of file + encoder_tp=2, encoder_pp=2, encoder_dp=1, llm_tp=2, llm_pp=2, llm_dp=1, llm_grid_offset=4 + ) From 00dd7b0e7f5d885aecf3263c7d7c3677c0f72ebc Mon Sep 17 00:00:00 2001 From: ykarnati Date: Sun, 2 Nov 2025 16:44:39 -0800 Subject: [PATCH 11/16] add copyright header --- tests/unit_tests/pipeline_parallel/test_bridge_communicator.py | 2 ++ .../pipeline_parallel/test_multimodule_communicator.py | 1 + .../unit_tests/pipeline_parallel/test_multimodule_schedules.py | 2 ++ 3 files changed, 5 insertions(+) diff --git a/tests/unit_tests/pipeline_parallel/test_bridge_communicator.py b/tests/unit_tests/pipeline_parallel/test_bridge_communicator.py index b0db1a273d2..eec2b3160f6 100644 --- a/tests/unit_tests/pipeline_parallel/test_bridge_communicator.py +++ b/tests/unit_tests/pipeline_parallel/test_bridge_communicator.py @@ -1,3 +1,5 @@ +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. + import logging import os import sys diff --git a/tests/unit_tests/pipeline_parallel/test_multimodule_communicator.py b/tests/unit_tests/pipeline_parallel/test_multimodule_communicator.py index 5bb3f1bdf62..f1b00f9e0ba 100644 --- a/tests/unit_tests/pipeline_parallel/test_multimodule_communicator.py +++ b/tests/unit_tests/pipeline_parallel/test_multimodule_communicator.py @@ -1,3 +1,4 @@ +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. import os import pytest diff --git a/tests/unit_tests/pipeline_parallel/test_multimodule_schedules.py b/tests/unit_tests/pipeline_parallel/test_multimodule_schedules.py index 29d30d6a305..86dcc5d73ad 100644 --- a/tests/unit_tests/pipeline_parallel/test_multimodule_schedules.py +++ b/tests/unit_tests/pipeline_parallel/test_multimodule_schedules.py @@ -1,3 +1,5 @@ +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. + import logging import os from contextlib import contextmanager From 1ff716f1df54f87d633d769545236158147f110b Mon Sep 17 00:00:00 2001 From: ykarnati Date: Sun, 2 Nov 2025 17:42:27 -0800 Subject: [PATCH 12/16] fix multimodule test --- .../pipeline_parallel/test_multimodule_communicator.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/unit_tests/pipeline_parallel/test_multimodule_communicator.py b/tests/unit_tests/pipeline_parallel/test_multimodule_communicator.py index f1b00f9e0ba..6bb6662301d 100644 --- a/tests/unit_tests/pipeline_parallel/test_multimodule_communicator.py +++ b/tests/unit_tests/pipeline_parallel/test_multimodule_communicator.py @@ -666,7 +666,7 @@ def test_send_forward_recv_forward_with_transformer_blocks_and_different_paralle # If current rank is in the first grid, run first block and send output if grid_1 is not None and mllm_comm.is_current_rank_in_grid(grid_1): rank_module_info = mllm_comm.rank_module_map['image_encoder'] - if rank_module_info.pp_rank == 0: + if rank_module_info.pp_stage == 0: hidden_states = block_grid_1(hidden_states=hidden_states, attention_mask=None) mllm_comm.send_forward({'image_encoder': hidden_states}) else: @@ -680,15 +680,15 @@ def test_send_forward_recv_forward_with_transformer_blocks_and_different_paralle # If current rank is in second grid, receive and run the second block if grid_2 is not None and mllm_comm.is_current_rank_in_grid(grid_2): rank_module_info = mllm_comm.rank_module_map['llm'] - if rank_module_info.pp_rank == 0: + if rank_module_info.pp_stage == 0: input_dict = mllm_comm.recv_forward() hidden_states = input_dict['image_encoder'] hidden_states = block_grid_2(hidden_states=hidden_states, attention_mask=None) - if rank_module_info.pp_rank == rank_module_info.pp_size - 1: + if rank_module_info.pp_stage == rank_module_info.pp_size - 1: output_grid_2 = hidden_states else: mllm_comm.send_forward({'llm': hidden_states}) - elif rank_module_info.pp_rank < rank_module_info.pp_size - 1: + elif rank_module_info.pp_stage < rank_module_info.pp_size - 1: input_dict = mllm_comm.recv_forward( tensor_shape=( sequence_length, @@ -751,7 +751,7 @@ def test_send_forward_recv_forward_with_transformer_blocks_and_different_paralle if ( grid_2 is not None and mllm_comm.is_current_rank_in_grid(grid_2) - and rank_module_info.pp_rank == rank_module_info.pp_size - 1 + and rank_module_info.pp_stage == rank_module_info.pp_size - 1 ): if grid1_dp == grid2_dp: # DP size matches: all outputs directly compared From 6c96ab10733c8716e973488248c9480e5352d932 Mon Sep 17 00:00:00 2001 From: ykarnati Date: Mon, 3 Nov 2025 22:13:30 -0800 Subject: [PATCH 13/16] add test cases for different dp --- megatron/core/pipeline_parallel/bridge_communicator.py | 6 +++--- .../pipeline_parallel/test_multimodule_schedules.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/megatron/core/pipeline_parallel/bridge_communicator.py b/megatron/core/pipeline_parallel/bridge_communicator.py index f1e74a2f16d..5bb09570f37 100644 --- a/megatron/core/pipeline_parallel/bridge_communicator.py +++ b/megatron/core/pipeline_parallel/bridge_communicator.py @@ -494,7 +494,7 @@ def recv_backward(self) -> torch.Tensor: received_gradients_list.append(grad_tensor) # Concatenate received gradients - aggregated_gradient = torch.cat(received_gradients_list, dim=0) + aggregated_gradient = torch.cat(received_gradients_list, dim=self.dim_mapping['b']) logging.debug( f"[Bridge Communicator] [receive_backward] Rank {self.current_rank} " f"agg grad shape {aggregated_gradient.shape} sum {aggregated_gradient.sum()}" @@ -615,7 +615,7 @@ def send_forward_recv_backward( req.wait() # Concatenate received gradients - aggregated_gradient = torch.cat(received_gradients_list, dim=0) + aggregated_gradient = torch.cat(received_gradients_list, dim=self.dim_mapping['b']) logging.debug( f"[Bridge Communicator] [send_forward_recv_backward] Rank {self.current_rank} " f"agg grad shape {aggregated_gradient.shape} sum {aggregated_gradient.sum()}" @@ -737,7 +737,7 @@ def send_backward_recv_forward( req.wait() # Concatenate received activations - aggregated_activation = torch.cat(received_activations_list, dim=0) + aggregated_activation = torch.cat(received_activations_list, dim=self.dim_mapping['b']) logging.debug( f"[Bridge Communicator] [send_backward_recv_backward] Rank {self.current_rank} " f"agg act shape {aggregated_activation.shape} sum {aggregated_activation.sum()}" diff --git a/tests/unit_tests/pipeline_parallel/test_multimodule_schedules.py b/tests/unit_tests/pipeline_parallel/test_multimodule_schedules.py index 86dcc5d73ad..c19dbd5e6c9 100644 --- a/tests/unit_tests/pipeline_parallel/test_multimodule_schedules.py +++ b/tests/unit_tests/pipeline_parallel/test_multimodule_schedules.py @@ -243,7 +243,7 @@ def _get_pg_collection_with_embedding_groups(grid): ) @pytest.mark.parametrize( "encoder_tp,encoder_pp,encoder_dp,llm_tp,llm_pp,llm_dp,llm_grid_offset", - [(2, 2, 1, 2, 2, 1, 4), (4, 1, 1, 2, 2, 1, 4), (2, 1, 1, 1, 6, 1, 2), (2, 2, 1, 1, 4, 1, 4)], + [(2, 2, 1, 2, 2, 1, 4), (4, 1, 1, 2, 2, 1, 4), (2, 1, 1, 1, 6, 1, 2), (2, 2, 1, 1, 4, 1, 4), (2, 1, 2, 1, 1, 4, 4),(2, 1, 2, 2, 2, 1, 4)], ) def test_forward_backward_pipelining_without_interleaving_multi_module_single_encoder( encoder_tp, encoder_pp, encoder_dp, llm_tp, llm_pp, llm_dp, llm_grid_offset @@ -355,5 +355,5 @@ def grad_scale_func(loss): # Use the same parameters as defined in the pytest.mark.parametrize decorator test_forward_backward_pipelining_without_interleaving_multi_module_single_encoder( - encoder_tp=2, encoder_pp=2, encoder_dp=1, llm_tp=2, llm_pp=2, llm_dp=1, llm_grid_offset=4 + encoder_tp=2, encoder_pp=1, encoder_dp=2, llm_tp=1, llm_pp=1, llm_dp=4, llm_grid_offset=4 ) From 6418dac6cd864ada8d881b38da7dcf0af99123ac Mon Sep 17 00:00:00 2001 From: ykarnati Date: Mon, 3 Nov 2025 22:25:04 -0800 Subject: [PATCH 14/16] format --- megatron/core/pipeline_parallel/bridge_communicator.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/megatron/core/pipeline_parallel/bridge_communicator.py b/megatron/core/pipeline_parallel/bridge_communicator.py index 5bb09570f37..9580f3924c8 100644 --- a/megatron/core/pipeline_parallel/bridge_communicator.py +++ b/megatron/core/pipeline_parallel/bridge_communicator.py @@ -737,7 +737,9 @@ def send_backward_recv_forward( req.wait() # Concatenate received activations - aggregated_activation = torch.cat(received_activations_list, dim=self.dim_mapping['b']) + aggregated_activation = torch.cat( + received_activations_list, dim=self.dim_mapping['b'] + ) logging.debug( f"[Bridge Communicator] [send_backward_recv_backward] Rank {self.current_rank} " f"agg act shape {aggregated_activation.shape} sum {aggregated_activation.sum()}" From 113283f10d9fe4b448c95d8b72820dbc242fc9bc Mon Sep 17 00:00:00 2001 From: ykarnati Date: Mon, 3 Nov 2025 22:30:19 -0800 Subject: [PATCH 15/16] format --- .../pipeline_parallel/test_multimodule_schedules.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/unit_tests/pipeline_parallel/test_multimodule_schedules.py b/tests/unit_tests/pipeline_parallel/test_multimodule_schedules.py index c19dbd5e6c9..b801a353cf2 100644 --- a/tests/unit_tests/pipeline_parallel/test_multimodule_schedules.py +++ b/tests/unit_tests/pipeline_parallel/test_multimodule_schedules.py @@ -243,7 +243,14 @@ def _get_pg_collection_with_embedding_groups(grid): ) @pytest.mark.parametrize( "encoder_tp,encoder_pp,encoder_dp,llm_tp,llm_pp,llm_dp,llm_grid_offset", - [(2, 2, 1, 2, 2, 1, 4), (4, 1, 1, 2, 2, 1, 4), (2, 1, 1, 1, 6, 1, 2), (2, 2, 1, 1, 4, 1, 4), (2, 1, 2, 1, 1, 4, 4),(2, 1, 2, 2, 2, 1, 4)], + [ + (2, 2, 1, 2, 2, 1, 4), + (4, 1, 1, 2, 2, 1, 4), + (2, 1, 1, 1, 6, 1, 2), + (2, 2, 1, 1, 4, 1, 4), + (2, 1, 2, 1, 1, 4, 4), + (2, 1, 2, 2, 2, 1, 4), + ], ) def test_forward_backward_pipelining_without_interleaving_multi_module_single_encoder( encoder_tp, encoder_pp, encoder_dp, llm_tp, llm_pp, llm_dp, llm_grid_offset From 4f8dff6738e924abe01b53acbbb7145d261e6fc4 Mon Sep 17 00:00:00 2001 From: ykarnati Date: Wed, 5 Nov 2025 14:40:16 -0800 Subject: [PATCH 16/16] change back to pp_rank --- .../multimodule_communicator.py | 28 +++++++++---------- .../test_multimodule_communicator.py | 10 +++---- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/megatron/core/pipeline_parallel/multimodule_communicator.py b/megatron/core/pipeline_parallel/multimodule_communicator.py index 56067573b78..4733db65115 100644 --- a/megatron/core/pipeline_parallel/multimodule_communicator.py +++ b/megatron/core/pipeline_parallel/multimodule_communicator.py @@ -47,7 +47,7 @@ class RankModuleInfo: """Information about a rank in a module.""" # 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 + pp_rank: int # the stage of the current rank in the current module's pipeline 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 @@ -134,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_stage == 0) of some module + 1. The current rank must be in the first PP stage (pp_rank == 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_stage == 0: + if rank_module_info.pp_rank == 0: # Check if this module is a source module (no incoming connections) if self._is_source_module(module_name): return True @@ -155,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_stage == rank_module_info.pp_size - 1: + if rank_module_info.pp_rank == 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 @@ -230,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_stage = rank_in_pp_group % pp_size + pp_rank = 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_stage == 0: + if pp_rank == 0: for bridge_comm in self.bridge_comms: if ( bridge_comm.is_current_rank_in_grid(bridge_comm.dest_grid) @@ -245,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_stage == pp_size - 1: + if pp_rank == pp_size - 1: for bridge_comm in self.bridge_comms: if ( bridge_comm.is_current_rank_in_grid(bridge_comm.src_grid) @@ -254,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_stage=pp_stage, + pp_rank=pp_rank, pp_size=pp_size, p2p_communicator=p2p_comm, bridge_comms_as_dest_module=bridge_comms_as_dest_module, @@ -280,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_stage == 0: + if rank_module_info.pp_rank == 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: @@ -303,7 +303,7 @@ 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_stage == rank_module_info.pp_size - 1: + if rank_module_info.pp_rank == 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: @@ -331,7 +331,7 @@ def send_forward_recv_backward( """ grad_dict = {} for module_name, rank_module_info in self.rank_module_map.items(): - if rank_module_info.pp_stage == rank_module_info.pp_size - 1: + if rank_module_info.pp_rank == 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: @@ -370,7 +370,7 @@ def send_backward_recv_forward( ) input_dict = {} for module_name, rank_module_info in self.rank_module_map.items(): - if rank_module_info.pp_stage == 0: + if rank_module_info.pp_rank == 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. @@ -406,7 +406,7 @@ def recv_backward( ) grad_dict = {} for module_name, rank_module_info in self.rank_module_map.items(): - if rank_module_info.pp_stage == rank_module_info.pp_size - 1: + if rank_module_info.pp_rank == 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: @@ -431,7 +431,7 @@ 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_stage == 0: + if rank_module_info.pp_rank == 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: diff --git a/tests/unit_tests/pipeline_parallel/test_multimodule_communicator.py b/tests/unit_tests/pipeline_parallel/test_multimodule_communicator.py index 6bb6662301d..f1b00f9e0ba 100644 --- a/tests/unit_tests/pipeline_parallel/test_multimodule_communicator.py +++ b/tests/unit_tests/pipeline_parallel/test_multimodule_communicator.py @@ -666,7 +666,7 @@ def test_send_forward_recv_forward_with_transformer_blocks_and_different_paralle # If current rank is in the first grid, run first block and send output if grid_1 is not None and mllm_comm.is_current_rank_in_grid(grid_1): rank_module_info = mllm_comm.rank_module_map['image_encoder'] - if rank_module_info.pp_stage == 0: + if rank_module_info.pp_rank == 0: hidden_states = block_grid_1(hidden_states=hidden_states, attention_mask=None) mllm_comm.send_forward({'image_encoder': hidden_states}) else: @@ -680,15 +680,15 @@ def test_send_forward_recv_forward_with_transformer_blocks_and_different_paralle # If current rank is in second grid, receive and run the second block if grid_2 is not None and mllm_comm.is_current_rank_in_grid(grid_2): rank_module_info = mllm_comm.rank_module_map['llm'] - if rank_module_info.pp_stage == 0: + if rank_module_info.pp_rank == 0: input_dict = mllm_comm.recv_forward() hidden_states = input_dict['image_encoder'] hidden_states = block_grid_2(hidden_states=hidden_states, attention_mask=None) - if rank_module_info.pp_stage == rank_module_info.pp_size - 1: + if rank_module_info.pp_rank == rank_module_info.pp_size - 1: output_grid_2 = hidden_states else: mllm_comm.send_forward({'llm': hidden_states}) - elif rank_module_info.pp_stage < rank_module_info.pp_size - 1: + elif rank_module_info.pp_rank < rank_module_info.pp_size - 1: input_dict = mllm_comm.recv_forward( tensor_shape=( sequence_length, @@ -751,7 +751,7 @@ def test_send_forward_recv_forward_with_transformer_blocks_and_different_paralle if ( grid_2 is not None and mllm_comm.is_current_rank_in_grid(grid_2) - and rank_module_info.pp_stage == rank_module_info.pp_size - 1 + and rank_module_info.pp_rank == rank_module_info.pp_size - 1 ): if grid1_dp == grid2_dp: # DP size matches: all outputs directly compared