Skip to content

support megatron_fsdp#153

Merged
Jintao-Huang merged 1 commit into
modelscope:mainfrom
Jintao-Huang:support_megatron_fsdp
Jul 21, 2026
Merged

support megatron_fsdp#153
Jintao-Huang merged 1 commit into
modelscope:mainfrom
Jintao-Huang:support_megatron_fsdp

Conversation

@Jintao-Huang

Copy link
Copy Markdown
Collaborator

No description provided.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds support for Megatron's FullyShardedDataParallel (megatron_FSDP) in the unwrap_model utility function. The review feedback identifies critical compatibility issues with older versions of megatron-core. Specifically, importing FullyShardedDataParallel at the top level can cause an ImportError on older versions, and if it is imported defensively as None, including it in the module_instances tuple will trigger a TypeError during type checking. The reviewer has provided actionable code suggestions to handle these cases safely.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

import torch
from megatron.core import mpu, tensor_parallel
from megatron.core.distributed import DistributedDataParallel as DDP
from megatron.core.distributed import FullyShardedDataParallel as megatron_FSDP

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Importing FullyShardedDataParallel directly at the top level will raise an ImportError on older versions of megatron-core where this class does not exist. Since the repository supports older versions of megatron-core (e.g., version 0.16.0), we should import it defensively within a try-except block.

Suggested change
from megatron.core.distributed import FullyShardedDataParallel as megatron_FSDP
try:
from megatron.core.distributed import FullyShardedDataParallel as megatron_FSDP
except ImportError:
megatron_FSDP = None

if module_instances is None:
from megatron.core.distributed import TorchFullyShardedDataParallel as torch_FSDP
module_instances = (DDP, torch_FSDP, Float16Module)
module_instances = (DDP, torch_FSDP, Float16Module, megatron_FSDP)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

If megatron_FSDP is None (due to older megatron-core versions), including it in the module_instances tuple will cause isinstance(model, module_instances) to raise a TypeError because None is not a valid class/type. We should conditionally add megatron_FSDP to the tuple only if it is not None.

Suggested change
module_instances = (DDP, torch_FSDP, Float16Module, megatron_FSDP)
module_instances = [DDP, torch_FSDP, Float16Module]
if megatron_FSDP is not None:
module_instances.append(megatron_FSDP)
module_instances = tuple(module_instances)

@Jintao-Huang
Jintao-Huang merged commit 1f0b2c4 into modelscope:main Jul 21, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant