-
Notifications
You must be signed in to change notification settings - Fork 7.1k
[diffusion] Support RL rollout for the Wan pipeline via a per-request scheduler switch #30036
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zhihengy
wants to merge
10
commits into
sgl-project:main
Choose a base branch
from
Rockdu:feat/wan-rl-rollout
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e33ecb2
[diffusion] Support RL rollout for the Wan pipeline via a per-request…
zhihengy 938c2cb
Address review: per-request scheduler resolution, class-level log flag
zhihengy 9f7661b
Address review: bind rollout scheduler per request in the standard ti…
zhihengy c026edc
Drop stage-binding unit test per author preference
zhihengy 101944a
Fix stale comment on rollout log flag
zhihengy eb4e63c
Tighten rollout_scheduler comment
zhihengy 8c43458
Move rollout binding logic into RolloutTimestepPreparationMixin under…
zhihengy 36d8134
Fix RolloutDenoisingMixin to use the request-bound scheduler
zhihengy 7c5770e
Create the rollout scheduler lazily on first rollout request
zhihengy b329fb7
Tighten rollout binding comment
zhihengy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
python/sglang/multimodal_gen/runtime/post_training/rollout_scheduler.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| """Per-request scheduler switching for RL rollout. | ||
|
|
||
| Serving and RL rollout want different schedulers on the same engine: serving | ||
| keeps the model's scheduler (e.g. UniPC for Wan), while the rollout | ||
| SDE/log-prob path requires a first-order flow-match Euler scheduler. | ||
| ``RolloutSchedulerSwitch`` holds both and dispatches per request on | ||
| ``batch.rollout``; every attribute it does not define delegates to the | ||
| active scheduler, so consumers see a plain scheduler either way. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from sglang.multimodal_gen.runtime.pipelines_core.diffusion_scheduler_utils import ( | ||
| get_or_create_request_scheduler, | ||
| ) | ||
| from sglang.multimodal_gen.runtime.pipelines_core.stages import ( | ||
| TimestepPreparationStage, | ||
| ) | ||
| from sglang.multimodal_gen.runtime.post_training.scheduler_rl_mixin import ( | ||
| SchedulerRLMixin, | ||
| ) | ||
| from sglang.multimodal_gen.runtime.utils.logging_utils import init_logger | ||
|
|
||
| logger = init_logger(__name__) | ||
|
|
||
|
|
||
| class RolloutTimestepPreparationStage(TimestepPreparationStage): | ||
| """Resolve the per-request scheduler before preparing timesteps.""" | ||
|
|
||
| def forward(self, batch, server_args): | ||
| scheduler = get_or_create_request_scheduler(batch, self.scheduler) | ||
| scheduler.prepare_for_batch(batch) | ||
| return super().forward(batch, server_args) | ||
|
|
||
|
|
||
| class RolloutSchedulerSwitch(SchedulerRLMixin): | ||
| """Dispatch between a serving and a rollout scheduler per request.""" | ||
|
|
||
| # Class-level so the info log prints once per process even if the | ||
| # scheduler is cloned per request (isolate=True paths). | ||
| _logged_rollout_check = False | ||
|
|
||
| def __init__(self, serving_scheduler, rollout_scheduler): | ||
| self.serving_scheduler = serving_scheduler | ||
| self.rollout_scheduler = rollout_scheduler | ||
| self._active_scheduler = serving_scheduler | ||
|
|
||
| def prepare_for_batch(self, batch): | ||
| self._active_scheduler = ( | ||
| self.rollout_scheduler if batch.rollout else self.serving_scheduler | ||
| ) | ||
| return self._active_scheduler | ||
|
|
||
| @property | ||
| def active_scheduler(self): | ||
| return self._active_scheduler | ||
|
|
||
| @property | ||
| def order(self): | ||
| return self._active_scheduler.order | ||
|
|
||
| @property | ||
| def num_train_timesteps(self): | ||
| return self._active_scheduler.num_train_timesteps | ||
|
|
||
| @property | ||
| def timesteps(self): | ||
| return self._active_scheduler.timesteps | ||
|
|
||
| @property | ||
| def sigmas(self): | ||
| return self._active_scheduler.sigmas | ||
|
|
||
| @property | ||
| def config(self): | ||
| return self._active_scheduler.config | ||
|
|
||
| def __getattr__(self, name): | ||
| return getattr(self._active_scheduler, name) | ||
|
|
||
| def set_shift(self, shift: float) -> None: | ||
| # Fan out so a launch-time flow_shift override reaches both paths. | ||
| self.serving_scheduler.set_shift(shift) | ||
| self.rollout_scheduler.set_shift(shift) | ||
|
|
||
| def set_begin_index(self, begin_index: int = 0): | ||
| return self._active_scheduler.set_begin_index(begin_index) | ||
|
|
||
| def set_timesteps( | ||
| self, | ||
| num_inference_steps: int | None = None, | ||
| device=None, | ||
| sigmas: list[float] | None = None, | ||
| mu: float | None = None, | ||
| timesteps: list[float] | None = None, | ||
| **kwargs, | ||
| ): | ||
| if self._active_scheduler is self.serving_scheduler: | ||
| if timesteps is not None: | ||
| raise ValueError( | ||
| "the serving scheduler does not support custom timesteps" | ||
| ) | ||
| self.serving_scheduler.set_timesteps( | ||
| num_inference_steps=num_inference_steps, | ||
| device=device, | ||
| sigmas=sigmas, | ||
| mu=mu, | ||
| **kwargs, | ||
| ) | ||
| return | ||
|
|
||
| self.rollout_scheduler.set_timesteps( | ||
| num_inference_steps=num_inference_steps, | ||
| device=device, | ||
| sigmas=sigmas, | ||
| mu=mu, | ||
| timesteps=timesteps, | ||
| **kwargs, | ||
| ) | ||
| self._check_rollout_timesteps() | ||
|
|
||
| def _check_rollout_timesteps(self) -> None: | ||
| # The rollout SDE/log-prob math assumes the flow-match Euler | ||
| # convention timesteps == sigmas[:-1] * num_train_timesteps. | ||
| sigmas = self.rollout_scheduler.sigmas | ||
| timesteps = self.rollout_scheduler.timesteps | ||
| if sigmas is None or timesteps is None or sigmas.numel() < 2: | ||
| return | ||
| reconstructed = sigmas[:-1].to(device=timesteps.device) * float( | ||
| self.rollout_scheduler.config.num_train_timesteps | ||
| ) | ||
| max_abs_diff = (timesteps.float() - reconstructed.float()).abs().max().item() | ||
| if max_abs_diff > 1e-3: | ||
| raise ValueError( | ||
| f"rollout timestep/sigma mismatch: max_abs_diff={max_abs_diff:.6g}" | ||
| ) | ||
| if not RolloutSchedulerSwitch._logged_rollout_check: | ||
| logger.info( | ||
| "RL rollout using %s (timesteps dtype=%s, sigmas dtype=%s, " | ||
| "max_abs_diff=%.6g)", | ||
| type(self.rollout_scheduler).__name__, | ||
| timesteps.dtype, | ||
| sigmas.dtype, | ||
| max_abs_diff, | ||
| ) | ||
| RolloutSchedulerSwitch._logged_rollout_check = True | ||
|
|
||
| def scale_model_input(self, sample, timestep=None): | ||
| return self._active_scheduler.scale_model_input(sample, timestep) | ||
|
|
||
| def step( | ||
| self, | ||
| model_output, | ||
| timestep, | ||
| sample, | ||
| generator=None, | ||
| batch=None, | ||
| return_dict: bool = True, | ||
| **kwargs, | ||
| ): | ||
| if self._active_scheduler is self.serving_scheduler: | ||
| return self.serving_scheduler.step( | ||
| model_output=model_output, | ||
| timestep=timestep, | ||
| sample=sample, | ||
| generator=generator, | ||
| return_dict=return_dict, | ||
| ) | ||
| return self.rollout_scheduler.step( | ||
| model_output=model_output, | ||
| timestep=timestep, | ||
| sample=sample, | ||
| generator=generator, | ||
| batch=batch, | ||
| return_dict=return_dict, | ||
| **kwargs, | ||
| ) | ||
|
|
||
| def index_for_timestep(self, *args, **kwargs): | ||
| return self._active_scheduler.index_for_timestep(*args, **kwargs) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.