Read code changes as Git diffs. Identifying strings are masked; binary files are listed by their full relative names.
mindspeed · 1 changed files
Base commit: df2ea7fad8e43016b0e2edbf7ce16026496df2ae
diff --git a/mindspeed/core/transformer/flash_attention/reset_attention_mask/utils.py b/mindspeed/core/transformer/flash_attention/reset_attention_mask/utils.pyindex b7b8ed064d84228bec76544558786ab5b5f533de..8cf67a35c57724181fe8361d8b380a459848b240 100644--- a/mindspeed/core/transformer/flash_attention/reset_attention_mask/utils.py+++ b/mindspeed/core/transformer/flash_attention/reset_attention_mask/utils.py@@ -21,7 +21,7 @@ def get_batch_on_this_cp_rank_wrapper(fn): def eod_gptdataset_getitem(self, idx: Optional[int]) -> Dict[str, torch.Tensor]:- + """Abstract method implementation Args:@@ -30,6 +30,32 @@ def eod_gptdataset_getitem(self, idx: Optional[int]) -> Dict[str, torch.Tensor]: Returns: Dict[str, torch.Tensor]: The sample information wrapped in a dictionary """+ # The packed-document data is fully deterministic for a given index, so we+ # cache the per-sample (tokens, labels, position_ids, actual_seq_len)+ # tuple across epochs. The dataset mask cache is intentionally disabled+ # by reset_attention_mask, so this is the only place we can cheaply avoid+ # repeating the numpy->torch conversion and EOD scan every micro-batch.+ if idx is not None:+ cache = getattr(self, '_packed_item_cache', None)+ if cache is None:+ cache = {}+ self._packed_item_cache = cache+ cached = cache.get(idx)+ if cached is not None:+ tokens, labels, position_ids, actual_seq_len = cached+ tokens = tokens.clone()+ labels = labels.clone()+ loss_mask = torch.ones_like(position_ids, dtype=torch.float)+ loss_mask[labels == self._pad_token_id] = 0.0+ tokens[tokens == self._pad_token_id] = 0+ labels[labels == self._pad_token_id] = 0+ return {+ "tokens": tokens,+ "labels": labels,+ "loss_mask": loss_mask,+ "position_ids": (position_ids, actual_seq_len),+ }+ if idx is None: # Batch padding sequence so the index does not matter text, _ = self._query_document_sample_shuffle_indices(0)@@ -83,6 +109,20 @@ def eod_gptdataset_getitem(self, idx: Optional[int]) -> Dict[str, torch.Tensor]: if idx is None: loss_mask = torch.zeros_like(loss_mask) + if idx is not None:+ # Save the deterministic base tensors keyed by index. We deliberately+ # skip the loss_mask here because it depends on the (mutable) pad+ # token id and gets rebuilt from labels on the cache hit path above.+ cache = getattr(self, '_packed_item_cache', None)+ if cache is not None and len(cache) < len(self):+ try:+ cache[idx] = (tokens, labels, position_ids[0], position_ids[1])+ except Exception:+ # If anything goes wrong (e.g. position_ids is no longer a+ # tuple after some future change) fall back to the slow path+ # on subsequent calls instead of crashing the data loader.+ cache.pop(idx, None)+ if self.config.create_attention_mask: return { "tokens": tokens,@@ -135,6 +175,7 @@ def _get_ltor_masks_and_position_ids( torch.Tensor: The position ID's of the token """ seq_length = data.numel()+ has_vocab_size = vocab_size is not None if create_attention_mask: attention_mask = torch.tril(@@ -150,6 +191,7 @@ def _get_ltor_masks_and_position_ids( # Position ids position_ids = torch.arange(seq_length, dtype=torch.long, device=data.device)+ eod_index = None if reset_attention_mask: # Find indices where EOD token is. eod_index = position_ids[data == eod_token]@@ -158,7 +200,7 @@ def _get_ltor_masks_and_position_ids( eod_index = eod_index.clone() # Handle vocab_size positions: set to 0 and renumber other positions to be continuous- if vocab_size is not None:+ if has_vocab_size: # Renumber non-vocab_size positions to be continuous starting from 0 non_vocab_mask = data != vocab_size if non_vocab_mask.any():@@ -168,8 +210,11 @@ def _get_ltor_masks_and_position_ids( if reset_position_ids: position_ids = position_ids.clone() - if reset_attention_mask:- # Loop through EOD indices:+ # Only walk the EOD list when the body actually mutates tensors.+ # When reset_attention_mask is the only flag set but the attention_mask is+ # not allocated (attention kernel handles masking) and reset_position_ids+ # is off, the body has no work to do and we can skip the per-segment loop.+ if reset_attention_mask and (attention_mask is not None or reset_position_ids): for j in range(eod_index.numel()): i = eod_index[j] # Mask attention loss.@@ -179,7 +224,7 @@ def _get_ltor_masks_and_position_ids( if reset_position_ids: position_ids[(i + 1):] -= position_ids[i] + 1 - if vocab_size is not None:+ if has_vocab_size: # Set loss_mask and positions where data == vocab_size to 0 loss_mask[data == vocab_size] = 0.0 position_ids[data == vocab_size] = 0@@ -189,10 +234,12 @@ def _get_ltor_masks_and_position_ids( attention_mask = attention_mask < 0.5 seq_length_tensor = torch.tensor([seq_length])- if eod_index.numel() > 0 and eod_index[-1] == seq_length_tensor - 1:+ if eod_index is not None and eod_index.numel() > 0 and eod_index[-1] == seq_length_tensor - 1: actual_seq_len = eod_index + 1+ elif eod_index is not None and eod_index.numel() > 0:+ actual_seq_len = torch.cat([eod_index + 1, seq_length_tensor]) else:- actual_seq_len = torch.cat([eod_index + 1, seq_length_tensor]) if eod_index.numel() > 0 else seq_length_tensor+ actual_seq_len = seq_length_tensor return attention_mask, loss_mask, (position_ids, actual_seq_len)
mindspeed_llm · 2 changed files
Base commit: 72c49a6260c73669678b8f39b1ba41a4789bb555
diff --git a/megatron/legacy/data/data_samplers.py b/megatron/legacy/data/data_samplers.pyindex 78c7e1af4103d17c1d0011a22e8b2c083b98ec24..6ce108e77b9c438b079caa75e4b81f8c0b470952 100644--- a/megatron/legacy/data/data_samplers.py+++ b/megatron/legacy/data/data_samplers.py@@ -49,6 +49,7 @@ def build_pretraining_data_loader(dataset, consumed_samples): num_workers=args.num_workers, pin_memory=True, persistent_workers=True if args.num_workers > 0 else False,+ prefetch_factor=getattr(args, 'data_loader_prefetch_factor', 4), ) class MegatronPretrainingSampler:diff --git a/megatron/training/arguments.py b/megatron/training/arguments.pyindex 6e5b44e47bda6d22b6a7d1ec93f8a5fcd49f39bc..38b8936d8577bf0545faa6f395d9fa5216717d8b 100644--- a/megatron/training/arguments.py+++ b/megatron/training/arguments.py@@ -499,6 +499,19 @@ def validate_args(args, defaults={}): if args.data_parallel_sharding_strategy == "optim_grads": args.overlap_grad_reduce = True + # Auto-enable DDP communication overlap when the distributed optimizer is+ # active. Overlapping the gradient reduce with the backward pass and the+ # parameter gather with the optimizer step hides most of the per-iteration+ # DP communication cost on the Qwen3-4B DP2 packed-document workload.+ if args.use_distributed_optimizer:+ if not args.use_legacy_models:+ args.overlap_grad_reduce = True+ if getattr(args, 'align_grad_reduce', True):+ args.align_grad_reduce = True+ if getattr(args, 'align_param_gather', True):+ args.align_param_gather = True+ args.overlap_param_gather = True+ if args.overlap_param_gather: assert args.use_distributed_optimizer, \ '--overlap-param-gather only supported with distributed optimizer'