NeMo icon indicating copy to clipboard operation
NeMo copied to clipboard

Duplicate ddp_config Configuration in MegatronStrategy Constructor

Open SJmdy opened this issue 1 year ago • 1 comments

Describe the bug

The MegatronStrategy class in nemo/lightning/pytorch/strategies/megatron_strategy.py contains duplicate code that configures self.ddp_config twice with identical logic. The same configuration block appears at lines 353-364 and again at lines 399-409, This will cause the FSDP settings in ddp_config to become ineffective.

        self._ddp = ddp
        if ddp == "megatron":
            self.ddp_config = DistributedDataParallelConfig(check_for_nan_in_grad=True)
        elif isinstance(ddp, DistributedDataParallelConfig):
            self.ddp_config = ddp
        elif ddp == "pytorch":
            if fsdp is not None:
                raise ValueError("Please set ddp to megatron to use FSDP.")
            self.ddp_config = None
            self.no_ddp_communication_hook = False
        else:
            raise ValueError(f"Invalid DDP type: {ddp}")

        self._fsdp = None

        use_custom_fsdp = getattr(self.ddp_config, "use_custom_fsdp", False)
        use_megatron_fsdp = getattr(self.ddp_config, "use_megatron_fsdp", False)
        if fsdp is None and self.ddp_config and (use_custom_fsdp or use_megatron_fsdp):
            logging.warning(
                "FSDP option is not set but ddp_config use megatron-fsdp is set to true. "
                "Setting FSDP option to megatron"
            )
            fsdp = 'megatron'
            if use_megatron_fsdp and self.save_ckpt_format != "fsdp_dtensor":
                raise NotImplementedError(
                    f"Megatron-FSDP checkpointing is not supported with {self.save_ckpt_format}."
                )

        if fsdp == "pytorch":
            raise NotImplementedError("PyTorch FSDP2 is not supported with MegatronParallel.")
        elif fsdp == "megatron":
            self._fsdp = fsdp
            if hasattr(self.ddp_config, "use_custom_fsdp") and not use_custom_fsdp:
                self.ddp_config.use_custom_fsdp = True
                logging.warning("Setting ddp_config.use_custom_fsdp to True for MCore FSDP.")
                logging.warning(
                    "Deprecation Notice: `use_custom_fsdp` will be deprecated in M-Core 0.14. "
                    "Please use `use_megatron_fsdp` instead."
                )
            elif hasattr(self.ddp_config, "use_megatron_fsdp") and not use_megatron_fsdp:
                self.ddp_config.use_megatron_fsdp = True
                logging.warning("Setting ddp_config.use_megatron_fsdp to True for MCore FSDP.")
            logging.info("FSDP option is set to MCore. Using MCore's Custom FSDP for DP.")
        elif fsdp is not None:
            raise ValueError(f'Invalid DDP type: {fsdp}, please choose from ["megatron", "pytorch"].')

        if ddp == "megatron":
            self.ddp_config = DistributedDataParallelConfig(check_for_nan_in_grad=True)
        elif isinstance(ddp, DistributedDataParallelConfig):
            self.ddp_config = ddp
        elif ddp == "pytorch":
            if self._fsdp is not None:
                raise ValueError("Please set ddp to megatron to use FSDP.")
            self.ddp_config = None
            self.no_ddp_communication_hook = False
        else:
            raise ValueError(f"Invalid DDP type: {ddp}")

SJmdy avatar Oct 08 '25 07:10 SJmdy

Hi we have moved our development from this repo to Megatron-Bridge repo. Can you please see if you see an issue there as well? If yes, please create an issue there. Thanks

abhinavg4 avatar Jan 13 '26 19:01 abhinavg4