Read code changes as Git diffs. Identifying strings are masked; binary files are listed by their full relative names.
mindspeed_llm · 3 changed files
Base commit: 72c49a6260c73669678b8f39b1ba41a4789bb555
diff --git a/mindspeed_llm/core/models/common/embeddings/rotary_pos_embedding.py b/mindspeed_llm/core/models/common/embeddings/rotary_pos_embedding.pyindex bd2b975cb13f4d801c4b7668fb4c4e0e22d3b015..ca8cb224136ea11c68023edaea6dff0c80a3b919 100644--- a/mindspeed_llm/core/models/common/embeddings/rotary_pos_embedding.py+++ b/mindspeed_llm/core/models/common/embeddings/rotary_pos_embedding.py@@ -200,6 +200,24 @@ def _process_partial_rope(freqs, t): return torch.cat((t, t_pass), dim=-1) +def _cached_cos_sin(freqs: Tensor, scale: float, dtype: torch.dtype):+ """Return RoPE trigonometric tensors shared by all layers in one forward pass."""+ cache_key = (scale, dtype)+ cache = getattr(freqs, "_mindspeed_cos_sin_cache", None)+ if cache is not None and cache[0] == cache_key:+ return cache[1], cache[2]++ cos = (torch.cos(freqs) * scale).to(dtype)+ sin = (torch.sin(freqs) * scale).to(dtype)+ if not freqs.requires_grad:+ # RotaryEmbedding creates one temporary frequency tensor per+ # microbatch and shares it across every transformer layer. Attaching+ # the derived values to that tensor gives the cache the same lifetime+ # without retaining activations between microbatches.+ freqs._mindspeed_cos_sin_cache = (cache_key, cos, sin)+ return cos, sin++ def apply_rotary_pos_emb(t, freqs, rotary_interleaved=False): """ For legacy rotary pos embedding.@@ -255,8 +273,7 @@ def apply_rotary_pos_emb_bshd( *dims, d = t.shape t = t.reshape(*dims, d // 2, 2).transpose(-2, -1).reshape(*dims, d) - cos_ = (torch.cos(freqs) * _mscale).to(t.dtype)- sin_ = (torch.sin(freqs) * _mscale).to(t.dtype)+ cos_, sin_ = _cached_cos_sin(freqs, _mscale, t.dtype) if args.use_fused_rotary_pos_emb: mode = 1 if rotary_interleaved else 0@@ -315,4 +332,4 @@ def apply_rotary_pos_emb_bshd_in_complex( if rotary_interleaved: y = torch.cat([y[..., 0::2], y[..., 1::2]], dim=-1) - return y.to(t.dtype)\ No newline at end of file+ return y.to(t.dtype)diff --git a/mindspeed_llm/features_manager/megatron_basic/training_basic.py b/mindspeed_llm/features_manager/megatron_basic/training_basic.pyindex dde78badc7ab8b8bf68eba47782cf99ffc8864b2..3e5a0ac3e391d6b226c0b2980fdfd45afbaa241d 100644--- a/mindspeed_llm/features_manager/megatron_basic/training_basic.py+++ b/mindspeed_llm/features_manager/megatron_basic/training_basic.py@@ -59,6 +59,28 @@ class TrainingBasicFeature(MindSpeedFeature): args.overlap_p2p_comm = None def validate_args(self, args):+ # At the default (advanced) optimization level, hide distributed-+ # optimizer communication behind model compute whenever data parallelism+ # makes those collectives necessary. Megatron's overlap path preserves+ # the optimizer/update semantics; it only changes when the reduce-scatter+ # and parameter all-gather are issued. Optimization levels 0 and 1 keep+ # the explicitly synchronous behavior for debugging and compatibility.+ if (+ args.optimization_level >= 2+ and args.use_distributed_optimizer+ and args.data_parallel_size > 1+ and not args.use_legacy_models+ and not args.use_custom_fsdp+ ):+ if not (args.overlap_grad_reduce and args.overlap_param_gather):+ print_rank0_by_args(+ args,+ "[INFO] Enabling gradient-reduce and parameter-gather overlap "+ "for the distributed optimizer.",+ )+ args.overlap_grad_reduce = True+ args.overlap_param_gather = True+ # mitigate FSDP2 performance degradation if getattr(args, "use_torch_fsdp2", False): os.environ['MULTI_STREAM_MEMORY_REUSE'] = '2'@@ -115,4 +137,4 @@ class TrainingBasicFeature(MindSpeedFeature): patch_manager.register_patch('megatron.training.training.train', train) patch_manager.register_patch('megatron.training.training.load_checkpoint',- load_checkpoint_wrapper)\ No newline at end of file+ load_checkpoint_wrapper)diff --git a/pretrain_gpt.py b/pretrain_gpt.pyindex 5a8506bb3f73e23ca5e1c1e2562d0a861c433cfb..4d5133cb0d36c07aeefdc17062b027626b66a09b 100644--- a/pretrain_gpt.py+++ b/pretrain_gpt.py@@ -306,4 +306,12 @@ def main(): if __name__ == "__main__":- main()+ try:+ main()+ finally:+ # Tear down HCCL collectively while every rank is still executing+ # Python. Relying on interpreter shutdown destroys process groups in+ # an arbitrary order and can leave each torchrun worker waiting for the+ # backend's shutdown timeout.+ if torch.distributed.is_initialized():+ torch.distributed.destroy_process_group()