[LLM] WS4 Post-Training Observability: Implement PostTrainingLogger
Description
This PR implements the Observability workstream (WS4) from RFC #3948. It introduces a modular PostTrainingLogger that unifies metric emission across TorchRL's post-training algorithms, making the observability stack consumable by external training loops.
Key Changes:
PostTrainingLogger: New backend-agnostic logger for tracking loss components, buffer utilization, and inference staleness. Usesgetattrduck-typing to safely ingest bothGRPOLossOutputandSFTLossOutput.- Recipe Refactoring: Updated GRPO and Expert Iteration (EI) recipes to delegate to the new logger.
- Deprecation Handling: EI legacy metric keys are temporarily emitted in parallel with explicit
DeprecationWarnings (targeted for removal inv0.15.0perCLAUDE.md). - Docs & Tests: Added full Sphinx documentation and comprehensive unit testing in
test_loggers.py.
Motivation and Context
Solves the need for component-level interoperability in TorchRL's LLM stack. Users can now drop this logger into their own existing loops without migrating their entire workflow.
close #3948
- [x] I have raised an issue to propose this change (required for new features and bug fixes)
Types of changes
- [ ] New feature (non-breaking change which adds core functionality)
- [x] Documentation (update in the documentation)
- [x] Example (update in the folder of examples)
Checklist
- [x] I have read the CONTRIBUTION guide (required)
- [x] My change requires a change to the documentation.
- [x] I have updated the tests accordingly (required for a bug fix or a new feature).
- [x] I have updated the documentation accordingly.
:link: Helpful Links
:test_tube: See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/3950
- :page_facing_up: Preview Python docs built from this PR
Note: Links to docs will display an error until the docs builds have been completed.
:warning: 15 Awaiting Approval
As of commit 9b5c0ecc0440e7ad5be97b393b3d2bc28b0a5f0f with merge base d7659c781a55f7680a025f16015064b0a8169344 ():
AWAITING APPROVAL - The following workflows need approval before CI can run:
- Build Aarch64 Linux Wheels (gh)
- Build Linux Wheels (gh)
- Build M1 Wheels (gh)
- Build Windows Wheels (gh)
- Continuous Benchmark (PR) (gh)
- Generate documentation (gh)
- Habitat Tests on Linux (gh)
- Libs Tests on Linux (gh)
- Lint (gh)
- LLM Tests on Linux (gh)
- Push Binary Nightly (gh)
- SOTA Tests on Linux (gh)
- Tutorials Tests on Linux (gh)
- Unit-tests on Linux (gh)
- Validate Test Partitioning (gh)
This comment was automatically generated by Dr. CI and updates every 15 minutes.
⚠️ PR Title Label Error
PR title must start with a label prefix in brackets (e.g., [BugFix]).
Current title: WS4 Post-Training Observability: Implement PostTrainingLogger
Supported Prefixes (case-sensitive)
Your PR title must start with exactly one of these prefixes:
| Prefix | Label Applied | Example |
|---|---|---|
[Algorithm] |
new algo | [Algorithm] Add new RL objective |
[BE] |
BE | [BE] Improve error messages |
[Benchmark] or [Benchmarks] |
Benchmarks | [Benchmark] Add collector benchmark |
[BugFix] |
BugFix | [BugFix] Fix memory leak in collector |
[Example] or [Examples] |
Examples | [Example] Add training script |
[Feature] |
Feature | [Feature] Add new optimizer |
[Doc] or [Docs] |
Documentation | [Doc] Update installation guide |
[Refactor] |
Refactoring | [Refactor] Clean up module imports |
[CI] |
CI | [CI] Fix workflow permissions |
[Test] or [Tests] |
Tests | [Tests] Add unit tests for buffer |
[Trainer] or [Trainers] |
Trainers | [Trainer] Add trainer config |
[Environment] or [Environments] |
Environments | [Environments] Add Gymnasium support |
[Data] |
Data | [Data] Fix replay buffer sampling |
[LLM] |
llm/ | [LLM] Add reward model integration |
[Minor] |
small change | [Minor] Fix typo in error message |
[Performance] or [Perf] |
Performance | [Performance] Optimize tensor ops |
[BC-Breaking] |
bc breaking | [BC-Breaking] Remove deprecated API |
[Deprecation] |
Deprecation | [Deprecation] Mark old function |
[Algorithm] or [Algorithms] |
new algo | [Algorithm] Add new objective |
[Quality] |
Quality | [Quality] Fix typos and add codespell |
[Versioning] |
versioning | [Versioning] Bump release version |
[WIP] |
WIP | [WIP] Draft implementation |
Note: Common variations like singular/plural are supported (e.g., [Doc] or [Docs]).
@vmoens, I would appreciate your feedback on this when you have a moment. Thanks.
Hi @theap06, any feedback on this PR?
@theap06 Thanks for the detailed review. All points addressed.
Reviewed current head 4a2d797f0700 after rebasing onto the fetched origin/main, including the latest corrective commit and all five unresolved review threads. The latest code does address those threads: metrics are batch-namespaced, buffer counters use public stats(), EI accesses are guarded, GRPO legacy keys warn with a removal version, and the persistent logger makes warning suppression effective. The 20 PostTrainingLogger tests pass.
There is still an optimizer-step accounting bug across the new logger and its sync call sites. torchrl/record/loggers/llm.py:114-118 treats step % gradient_accumulation_steps == 0 as the optimizer boundary and derives optim_steps = step // accumulation, while both sota-implementations/grpo/grpo-sync.py:245 and sota-implementations/expert-iteration/expert-iteration-sync.py:278 actually step on (global_step + 1) % accumulation == 0. With accumulation 4, the optimizer runs at step 3 but the logger omits that gradient norm, then reports a stale norm at step 4 and still reports zero optimizer steps at step 3. The new unit test encodes the opposite schedule from the callers. Please make the callers and logger share one convention, preferably fixing the one-based caller loops to step on global_step % accumulation == 0, and add an integration-level assertion around the call site.
Checks: pytest -q test/test_loggers.py -k PostTrainingLogger gives 20 passed; complete diff and latest thread state inspected; git diff --check is clean.
@vmoens Fixed in 9b5c0ecc0 — changed both grpo-sync.py and expert-iteration-sync.py to use global_step % accumulation == 0 to align with the logger's boundary. Added an integration-level test that simulates the actual loop pattern and asserts grad_norm fires at exactly steps 4, 8, 12. 21 tests passing.
@vmoens The recent merge of GRPOTrainer removed the manual training loops, which conflicts with this branch.
Does it still make sense to keep PostTrainingLogger to preserve the extended LLM-specific metrics (unpadded sequence lengths, policy staleness, buffer stats, throughput)?
If yes, I'll resolve the conflicts.
@vmoens Makes sense. Closing this PR.