Skip to content

refactor(generators): assemble routed-expert traces incrementally - #1912

Open
dyurk-lila wants to merge 2 commits into
NovaSky-AI:mainfrom
dyurk-lila:upstream/r3-incremental-traces
Open

refactor(generators): assemble routed-expert traces incrementally#1912
dyurk-lila wants to merge 2 commits into
NovaSky-AI:mainfrom
dyurk-lila:upstream/r3-incremental-traces

Conversation

@dyurk-lila

@dyurk-lila dyurk-lila commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Note on the diff: This PR is part of a routed-expert-replay / sampler-support series and builds on the PRs below. GitHub can't show the intermediate branches here, so the diff is cumulative on top of main — the changes new to this PR sit on top of:

Reviewing in PR order (lowest number first) shows each incremental change cleanly.

Summary

  • Add a reusable TokenMetadataTrace for accumulating token-aligned NumPy metadata.
  • Add RoutedExpertTrace for routed-expert (R3) multi-turn alignment with masked terminal gaps.
  • Expose a typed routed-expert prompt cursor (routed_experts_prompt_start(s)) through the remote inference client and InferenceEngineInput.
  • Adopt incremental route capture in SkyRLGymGenerator.

Why

The multi-turn generator sends the cumulative prompt on every inference request. Routed-expert generation previously returned routing rows for that full prefix on every turn and kept only the latest full tensor. Passing the number of already-captured rows to the engine makes routed-expert response metadata incremental: total route transfer becomes O(N) instead of O(N^2) over a growing trajectory. Full-prompt request traffic is unchanged and continues to benefit from sticky routing and prefix caching.

RoutedExpertTrace records each generation's routes and finalizes a full per-token routed-expert array, padding the loss-masked terminal gap so the result stays aligned to the token sequence. The generic TokenMetadataTrace underneath is a standalone token-aligned accumulator reusable beyond R3.

Testing

  • uv run --isolated --extra dev --extra fsdp pytest tests/backends/skyrl_train/inference_servers/test_remote_inference_client.py tests/train/generators/test_skyrl_gym_generator.py tests/backends/skyrl_train/distributed/test_token_metadata.py — all pass (99 passed), including new coverage for incremental trace assembly, chunked/independent-schema traces, invalid-chunk rejection, and multi-turn suffix / terminal-gap padding.
  • Ruff 0.11.9 and Black 24.10.0 (line length 120): changed files pass.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

@dyurk-lila
dyurk-lila force-pushed the upstream/r3-incremental-traces branch from 059c69f to b6fb2a4 Compare July 20, 2026 20:25
@erictang000 erictang000 self-assigned this Jul 20, 2026
@dyurk-lila
dyurk-lila force-pushed the upstream/r3-incremental-traces branch 3 times, most recently from 321614b to 98a6724 Compare July 31, 2026 22:52
dyurk-lila and others added 2 commits August 5, 2026 00:25
For multi-turn rollouts, build routed-expert (R3) trace data incrementally
as the conversation grows instead of re-gathering the whole conversation's
routes on every turn.

- Add `routed_experts_prompt_starts` to `InferenceEngineInput` and thread a
  per-request `routed_experts_prompt_start` through `RemoteInferenceClient`
  and `RemoteInferenceGenerator.generate()`, forwarded as a sampling param so
  the engine only returns routes for the newly generated suffix.
- Introduce `TokenMetadataTrace` (token-aligned array accumulator) and
  `RoutedExpertTrace`, which records each generation's routes and finalizes a
  full per-token routed-expert array with loss-mask-aware terminal padding.
- Track a `RoutedExpertTrace` on `AgentLoopState` and record routes per turn,
  replacing the previous whole-conversation re-gather in
  `SkyRLGymGenerator.agent_loop`.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@erictang000
erictang000 force-pushed the upstream/r3-incremental-traces branch from 98a6724 to cb96075 Compare August 5, 2026 00:33
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