Skip to content

support ep_fsdp#56

Closed
kevssim wants to merge 5 commits intodevfrom
dev-wkw
Closed

support ep_fsdp#56
kevssim wants to merge 5 commits intodevfrom
dev-wkw

Conversation

@kevssim
Copy link
Copy Markdown
Collaborator

@kevssim kevssim commented Feb 12, 2026

No description provided.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @kevssim, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces support for Expert FSDP (Fully Sharded Data Parallel) within the twinkle framework. This new parallelism strategy allows for more efficient and flexible sharding of expert layers in Mixture-of-Experts models, particularly beneficial for large-scale distributed training. By integrating ep_fsdp into the DeviceMesh and NativeFSDPStrategy, the system can now leverage an additional dimension for distributing expert parameters, enhancing scalability and resource utilization for MoE architectures.

Highlights

  • New Parallelism Dimension: Introduced ep_fsdp (Expert FSDP Parallel) as a new dimension within the DeviceMesh to enable more granular control over expert sharding in Mixture-of-Experts (MoE) models.
  • Expert FSDP Integration: Implemented the core logic for ep_fsdp within the NativeFSDPStrategy, allowing expert blocks to be fully sharded across the ep_fsdp dimension. This includes new helper functions for building ep_fsdp specific device meshes and collecting expert parameters.
  • Configuration and Compatibility: Added configuration options (ep_fsdp: True) to enable this new mode and included checks to ensure ep_fsdp is only applied when experts are represented as tensors (e.g., gate_up_proj/down_proj), not nn.ModuleList.
  • Cookbook Example Update: Updated the ep_fsdp_qwen3_moe.py cookbook example to demonstrate the usage of the new ep_fsdp dimension in DeviceMesh configuration.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • cookbook/sft/ep_fsdp_qwen3_moe.py
    • Updated DeviceMesh.from_sizes to include ep_fsdp_size and set dp_size to None for a specific configuration.
    • Enabled ep_fsdp in the expert_parallel configuration for the Qwen3 MoE model.
  • src/twinkle/model/transformers/moe/expert_parallel.py
    • Added ep_fsdp attribute to ExpertParallelConfig.
    • Introduced logic to determine if ep_fsdp mode is enabled based on configuration and device mesh.
    • Modified shard_experts to accept and utilize the ep_fsdp_enabled flag.
    • Added a NotImplementedError for ep_fsdp when experts are stored as nn.ModuleList.
  • src/twinkle/model/transformers/strategy/native_fsdp.py
    • Added enable_ep_fsdp parameter to NativeFSDPStrategy initialization.
    • Implemented conditional logic to enable and apply ep_fsdp sharding for expert blocks.
    • Added helper functions _build_ep_fsdp_mesh, _is_ep_fsdp_mode_enabled, _collect_block_expert_params, _ensure_ep_fsdp_supported, and _maybe_shard_ep_expert_blocks to manage ep_fsdp specific operations.
  • src/twinkle/model/transformers/transformers.py
    • Added _enable_expert_ep_fsdp attribute to manage the state of expert FSDP.
    • Passed the enable_ep_fsdp flag to the NativeFSDPStrategy constructor.
    • Implemented _should_enable_expert_ep_fsdp static method to determine if expert FSDP should be activated.
  • src/twinkle/utils/platform.py
    • Documented ep_fsdp as a new parallelism dimension.
    • Added ep_fsdp_size to the DeviceMesh dataclass and its from_sizes constructor.
    • Updated DeviceMesh internal logic to correctly handle the ep_fsdp dimension when constructing the mesh and its names.
    • Refactored create_process_group and extracted get_ranks_for_dims to support the new dimension.
    • Included ep_fsdp in the valid_dim_names and the order property.
    • Added ep_fsdp_rank and ep_fsdp_world_size properties to DeviceMesh.
  • tests/DeviceMesh/test_device_mesh.py
    • Added test_ep_fsdp_rank_and_world_size to verify the correct functioning of ep_fsdp ranks and world sizes.
    • Added test_without_dp_dimension to ensure DeviceMesh correctly handles configurations without a data parallel dimension.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

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

Choose a reason for hiding this comment

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

Code Review

The pull request introduces support for ep_fsdp (Expert FSDP Parallel), which allows Mixture-of-Experts (MoE) experts to be sharded using FSDP within an Expert Parallel (EP) group. This is a valuable addition for improving memory efficiency in large MoE models. The implementation is well-integrated into the existing DeviceMesh and NativeFSDPStrategy classes. I have identified a potential robustness issue in the new get_ranks_for_dims method and a redundant import that should be addressed.


def create_process_group(self, dims):
"""Create a process group by dims"""
import torch.distributed as dist
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

This local import of torch.distributed is redundant as it is already imported at the top of the file (line 11).

@tastelikefeet tastelikefeet deleted the branch dev February 13, 2026 09:40
@tastelikefeet tastelikefeet deleted the dev-wkw branch February 13, 2026 09:41
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.

2 participants