feat: Add routing replay support for FSDP#240
Conversation
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
|
/gemini review Please focus on whether the routing returned in R3 mode and R2 mode is correct, and whether R3 mode correctly uses the vLLM expert routing. |
There was a problem hiding this comment.
Code Review
This pull request introduces GRPO training with routing replay for MoE models under the FSDP backend, supporting three routing replay modes (disabled, R2, and R3). The changes span the training cookbook, CLI arguments, data formats, expert parallel logic, router replay utilities, transformers model class, input processor, and vLLM sampler to handle recording, replaying, and aligning expert routing decisions. The review feedback highlights several critical issues that need to be addressed: potential AttributeError exceptions when labels is None in transformers.py, a guaranteed runtime crash in base.py due to incorrect tensor stacking (concat = False resulting in a 5D tensor instead of 4D), a TypeError when calling self.experts as a nn.ModuleList when EP = 1, potential padding errors if experts_seq_len > input_seq_len, robust handling of string annotations in inspect.signature, and an unused variable recompute_logps in the cookbook.
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.
|
@tpx818 Please review again. |
PR type
PR information
This PR introduces routing replay support for Mixture-of-Experts (MoE) models when using FSDP (Fully Sharded Data Parallel) backend in the twinkle framework. It enables three replay modes (disabled, R2, and R3) to ensure consistent expert routing during reinforcement learning training. The feature is demonstrated through a GRPO training example on math tasks.
Key Changes:
Core Router Replay Infrastructure (
src/twinkle/model/transformers/moe/router_replay.py):RouterReplayActionenum defining three replay modes: RECORD, REPLAY_FORWARD, and REPLAY_BACKWARDExpert Parallel Module Enhancements (
src/twinkle/model/transformers/moe/expert_parallel.py):TransformersModel Integration (
src/twinkle/model/transformers/transformers.py):enable_router_replayconfiguration option_router_replay_setup()method for managing replay lifecycleforward(),forward_only(), andbackward()methodsforward_backward()operationsData Format Updates:
InputFeaturewithrouted_expertsfield (src/twinkle/data_format/input_feature.py)routed_expertsoutput support inModelOutput(src/twinkle/data_format/output.py)routed_expertsfield toSampledSequence(src/twinkle/data_format/sampling.py)Input Processing Pipeline (
src/twinkle/processor/base.py):align_routed_experts()preprocessing step to align vLLM routing data with input sequence lengthsvLLM Sampler Integration (
src/twinkle/sampler/vllm_sampler/files):routed_expertsdata from vLLM sampling outputUsage Example (new files):
cookbook/rl/grpo/short_math_grpo_routing_replay.py- 334 lines)short_math_grpo_routing_replay.sh)Replay Modes:
Usage Tutorial
Refer to cookbook/rl/grpo/short_math_grpo_routing_replay.py
Experiment results