Skip to content

Add StickySessionRoutingStrategy for GeneratorRouter#3625

Open
pzhan9 wants to merge 1 commit into
pytorch:mainfrom
pzhan9:pzhang/sticky-session-routing
Open

Add StickySessionRoutingStrategy for GeneratorRouter#3625
pzhan9 wants to merge 1 commit into
pytorch:mainfrom
pzhan9:pzhang/sticky-session-routing

Conversation

@pzhan9

@pzhan9 pzhan9 commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

As title.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jun 10, 2026
Comment on lines +135 to +138
if config.max_sessions <= 0:
raise ValueError(
f"max_sessions must be positive, got {config.max_sessions}"
)

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.

should go in post_init

Comment on lines +163 to +164
if len(self._sessions) > self._max_sessions:
self._sessions.popitem(last=False)

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.

add to TODO to log this using structured logger @felipemello1

maybe logger.warning for now?

estimated_cost: int = 1
"""Estimated request cost used by load-aware routing strategies."""

session_id: str | None = None

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.

where are you assigning this to a context?

also this is leaking unnecessary info to non-stick routing strategies

Comment on lines +154 to +158
if sticky_generator is not None and any(
h is sticky_generator for h in candidates
):
self._sessions.move_to_end(routing_ctx.session_id)
return sticky_generator

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.

It seems if a generator is in weight-sync, you'd choose a new session, instead of wait until this generator's weight-sync finishes.

Do we know if this trade-off is worth it, especially for extreme long horizon rollout?


chosen = self._fallback_strategy.choose(routing_ctx, candidates)
self._sessions[routing_ctx.session_id] = chosen
self._sessions.move_to_end(routing_ctx.session_id)

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.

please add more comments on the strategy here

return min(candidates, key=lambda h: h.reserved_load)


class StickySessionRoutingStrategy(RoutingStrategy):

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.

The logic around assigning context.session_id is not clear, as it's not used anywhere https://github.com/pytorch/torchtitan/blob/main/torchtitan/experiments/rl/trainer.py#L592

Most importantly, we should make sure all the same prompts from a single GRPO group can route to the same generator.

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.

please enhance the multi-generator CI test with this feature

felipemello1 pushed a commit to felipemello1/torchtitan that referenced this pull request Jun 11, 2026
…g seam; simplify AlphabetSort example

- rollout/types.py: GenerateFn is now a Protocol with an explicit signature
  (prompt_token_ids/request_id/session_id/sampling_config -> Completion|None), not a loose Callable.
- session_id seam: run_single_rollout passes a stable per-rollout session_id (sticky-routing key)
  plus a per-turn request_id, threaded rollouter -> generate_fn -> generator.generate. A single
  generator ignores session_id; ready for the multi-generator router (pytorch#3583/pytorch#3625).
- run_single_rollout takes rollout_id (built in run_group_rollouts) instead of sample_idx.
- examples/alphabet_sort/env.py: fixed format example ending in "..." (dropped the randomized
  placeholder-row machinery); restored the original docstrings.
- docstring/comment cleanups (plain wording).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/rl ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants