Skip to content

[Observability 5/7] RolloutLogger for RL rollout JSONL with filtering - #2605

Closed
felipemello1 wants to merge 1 commit into
gh/felipemello1/21/basefrom
gh/felipemello1/21/head
Closed

felipemello1 wants to merge 1 commit into
gh/felipemello1/21/basefrom
gh/felipemello1/21/head

Conversation

@felipemello1

@felipemello1 felipemello1 commented Mar 16, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

How to review

Prioritize the files under /observability. Check how they are used in the toy example. No need to nitpick the toy example.

Summary

RolloutLogger writes RL rollouts to JSONL with optional filtering. Takes a list[dict] with no enforced schema — users pass whatever fields their rollouts have (prompt, response, reward, etc). Metadata is nested under __metadata__ to avoid key collisions.

Optional filter_fn for selecting which rollouts to log, e.g. filter_top_bottom(records, key, k) keeps the top-k and bottom-k by a given key — useful for logging only the best and worst rollouts per batch.

Integrated into toy_rl: the controller logs filtered rollouts after each RL step.

Example

from torchtitan.observability import RolloutLogger

rollout_logger = RolloutLogger(
    output_dir="outputs/rollouts",
    filter_fn=lambda records: filter_top_bottom(records, key="reward", k=2),
)

rollouts = [
     {"prompt": "what is 2+2?", "response": "4", "reward": 0.95},
     {"prompt": "write a poem", "response": "roses...", "reward": 0.3}
     ]

# Log rollout dicts with optional metadata
rollout_logger.log(
    rollouts,
    metadata={"step": 42, "batch_size": 64},
)
rollout_logger.close()

Output JSONL (metadata stored under __metadata__ to avoid key collisions):

{"prompt": "what is 2+2?", "response": "4", "reward": 0.95, "__metadata__": {"step": 42, "batch_size": 64}}
{"prompt": "write a poem", "response": "roses...", "reward": 0.3, "__metadata__": {"step": 42, "batch_size": 64}}

Test plan

Run toy_rl: python -m torchtitan.experiments.observability.toy_rl

  • New unit tests for RolloutLogger: write, filter, metadata nesting, close
  • Integration: toy_rl produces rollouts.jsonl with filtered entries
  • Integration: rollouts have metadata nesting with step info

Output folder (toy_rl on PR6 — note rollouts/ directory)

outputs/toy_rl/
├── analysis/
│   └── system_metrics_gantt.json
├── experiment_logs/
│   ├── controller_rank_0_experiment.jsonl
│   ├── reward_rank_0_experiment.jsonl
│   ├── rollouter_rank_0_experiment.jsonl
│   └── trainer_rank_{0-3}_experiment.jsonl
├── rollouts/                               ← NEW
│   └── rollouts.jsonl
├── system_logs/
│   ├── controller_rank_0_system.jsonl
│   ├── reward_rank_0_system.jsonl
│   ├── rollouter_rank_0_system.jsonl
│   └── trainer_rank_{0-3}_system.jsonl
└── wandb/

logger.info("Actors spawned.")

rollout_dir = os.path.join(OUTPUT_DIR, "rollouts")
rollout_logger = RolloutLogger(

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.

Would it be good for each actor to have its logger, as long as they created on their dedicated mesh/process?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be good for each actor to have its logger

Can you help me understand a bit better what you mean?

'RolloutLogger' is an specific class to log rollouts, where the rollout is a product of generation + rewards. So the flow is:

  1. Controller calls actors
  2. Controller puts together the rollout for training
  3. Controller calls RolloutLogger(rollouts, filter, metadata)

it produces this guy

{"prompt": "what is 2+2?", "response": "4", "reward": 0.95, "__metadata__": {"step": 42, "batch_size": 64}}
{"prompt": "write a poem", "response": "roses...", "reward": 0.3, "__metadata__": {"step": 42, "batch_size": 64}}
Image

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.

so is it fair to say it's a helper for RL just like MetricsProcessor for sft?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't if i would call it a 'helper'. It is another axis of logging. Users want to inspect their rollouts frequently. TBR and Tinker also log their rollouts.

if fn is not None:
records = fn(records)
extra = {"__metadata__": metadata} if metadata else {}
self._file.write("\n".join(json.dumps({**r, **extra}) for r in records) + "\n")

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.

who's consuming the logs from RolloutLogger?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, no one. It gets saved in the folder for user inspection. In the future, we could have some application consume it for rich display on a website, or even add it to wandb logging.

Image

@@ -16,6 +16,7 @@
record_metric,

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.

high level this seems potentially useful but a) not part of this PR stack and b) should not be in "titan/observability" but rather in the RL experiments.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i am ok with that. It is self contained. We can promote to /torchtitan later if it seems fit.

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

Labels

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.

3 participants