Skip to content

[Refactor] Consolidate stats() in BaseCollector and add WandB monitoring example - #4400

Merged
vmoens merged 7 commits into
pytorch:mainfrom
coder-jayp:feat/pull-based-stats
Sep 20, 2026
Merged

vmoens merged 7 commits into
pytorch:mainfrom
coder-jayp:feat/pull-based-stats

Conversation

@coder-jayp

@coder-jayp coder-jayp commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Consolidates single-process and multiprocessing collector statistics in BaseCollector.stats(), preserving shared in-flight trajectory counters, non-blocking aggregate snapshots, and the existing per-worker control-channel behavior. Workers return data to the coordinator, where logging happens.

The public stats() API stays the same. The before/after below shows the regression-test scenario: one healthy Ray worker is held inside its first collection call for longer than the stats timeout.

Before: the stats request waits behind collection on the same actor queue, times out, and the healthy worker is omitted from the snapshot.

snapshot = collector.stats(workers="both", timeout=1.0)
snapshot["workers_alive"]  # 0, even though the worker is collecting
"worker_0/frames" in snapshot  # False: no worker snapshot arrived

After: the stats request runs in its own concurrency group and can report progress while collection is still busy.

snapshot = collector.stats(workers="both", timeout=1.0)
snapshot["workers_alive"]  # 1: the worker answered during collection
snapshot["worker_0/frames"]  # 0: its first batch has not finished yet

The new example uses this snapshot API through LoggerMonitor to send aggregate and per-worker progress to W&B from the coordinator:

with LoggerMonitor(logger, poll_interval=0.5) as monitor:
    monitor.watch(
        collector,
        name="ray_collector",
        schedule=Every.counter("frames", 100),
        stats_kwargs={"workers": "both"},
    )
    for batch in collector:
        ...  # Training or other batch processing; monitoring runs separately.

Collection and state-changing calls retain the default actor queue. Remote polling remains timeout-bounded, and caller-supplied actor options and concurrency groups are preserved.

Validation:

  • All 11 TestCollectorStats cases pass, including two real-Ray tests with Ray 2.54.0.
  • The new regression holds collection behind a gate, verifies stats respond while the gate is closed, then releases it and verifies the collected batch. Restoring the old stats dispatch makes this test fail because workers_alive becomes zero.
  • The exact example completes with offline W&B. A separate manual monitor poll verifies persisted aggregate and per-worker frame counters from two live Ray workers.
  • Flake8, ufmt, and git diff --check pass.
  • The example is registered as runnable CI coverage with offline W&B and an isolated log directory. The manifest/sharding checks and actual Ray/W&B example pass through the CI runner (12 passed).

@pytorch-bot

pytorch-bot Bot commented Sep 16, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4400

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 16, 2026

@theap06 theap06 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

left some comments. overall design lgtm.

Comment thread examples/ray_wandb_monitor.py
Comment thread torchrl/collectors/_base.py Outdated
@vmoens vmoens added the ci/optdeps Run the full tests-optdeps suite on this PR label Sep 19, 2026
@github-actions github-actions Bot added the CI Has to do with CI setup (e.g. wheels & builds, tests...) label Sep 19, 2026
@vmoens
vmoens merged commit f1d59e2 into pytorch:main Sep 20, 2026
107 of 111 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci/optdeps Run the full tests-optdeps suite on this PR CI Has to do with CI setup (e.g. wheels & builds, tests...) CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Collectors Examples Integrations/torch_geometric Integrations Refactoring Refactoring of an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants