Skip to content

refactor: consolidate Rollout Routing Replay (R3) code under nemo_rl/router_replay/ #4767

Open

Description

@yuki-97

Problem

R3 code is spread across the tree, and the backend-agnostic parts live inside a backend package:

  • nemo_rl/models/megatron/router_replay.py holds the Megatron replay hooks, but also backend-agnostic helpers: router_replay_enabled, configure_vllm_for_router_replay, validate_router_replay_config.
  • nemo_rl/models/automodel/router_replay.py (Automodel replay) imports router_replay_enabled from nemo_rl.models.megatron.router_replay, so the Automodel backend depends on the Megatron package for a function that only reads the policy config. The same import exists in models/automodel/setup.py and policy/workers/automodel_policy_worker.py.
  • nemo_rl/utils/r3_trace.py, nemo_rl/utils/routed_experts_codec.py and nemo_rl/experience/route_assembly.py are R3-only but sit in generic directories.
  • R3 constants (ROUTED_EXPERTS_MISSING_ROUTE_SENTINEL, dtype helpers) live in nemo_rl/models/generation/interfaces.py, and the config types RouterReplayConfig / RouterReplayConfigDisabled in nemo_rl/models/policy/__init__.py.
  • The enabled check is also re-inlined as bool((cfg.get("router_replay") or {}).get("enabled", False)) in models/policy/tq_policy.py and experience/sync_rollout_actor.py.

Proposal

Add a nemo_rl/router_replay/ package and move the R3 code there:

Module Contents
config.py router_replay_enabled, validate_router_replay_config, configure_vllm_for_router_replay (and optionally the config TypedDicts)
megatron.py current models/megatron/router_replay.py
automodel.py current models/automodel/router_replay.py
trace.py current utils/r3_trace.py
codec.py current utils/routed_experts_codec.py
route_assembly.py current experience/route_assembly.py

The routed-experts constants in generation/interfaces.py can move too, or stay if the generation interface needs them without importing the new package. Callers (grpo.py, single_controller_utils/setup.py, the Megatron/Automodel setup and policy workers, tq_policy.py, sync_rollout_actor.py) switch to the shared router_replay_enabled.

Backend modules should keep their megatron.core imports inside functions, as today, so importing nemo_rl.router_replay never pulls in Megatron-Core.

Notes

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions