Skip to content

Fix final RL validation logging in W&B - #5648

Draft
vkuzo wants to merge 1 commit into
gh/vkuzo/4/headfrom
gh/vkuzo/5/head
Draft

vkuzo wants to merge 1 commit into
gh/vkuzo/4/headfrom
gh/vkuzo/5/head

Conversation

@vkuzo

@vkuzo vkuzo commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Summary:

RL has two distinct clocks. The optimizer step identifies the model version:
step 0 before training, then steps 1 through 150 in the reference run.
W&B's internal _step identifies a committed logging entry. Once an entry is
committed, W&B expects subsequent entries to advance its internal step.

Previously, WandBLogger passed the optimizer step to wandb.log as W&B's
internal step and committed every call. The observed sequence was:

Event Model version Old W&B call Outcome
Initial validation 0 step=0, commit=True Saved: 0.10
Final training metrics 150 step=150, commit=True Saved; next step is 151
Final validation 150 step=150, commit=True Rejected as out of order

The final validation really ran: the 150-step DAPO run recorded 5/30 (0.17)
held-out correct answers in the console and rollout records, versus 3/30
(0.10) before training. W&B's latest validation value stayed at 0.10
because the initial validation was its only accepted validation entry.
The dashboard was therefore missing a result, not reporting that final
validation failed or that the model still scored 0.10.

Separate model version from W&B event order. Add an optional step_metric to
the shared WandBLogger; when supplied, it registers that metric with
wandb.define_metric as the axis for other metrics. RL opts in with
trainer/step. Every RL log payload includes trainer/step=,
but wandb.log no longer receives that value as its internal step.
Committing a log entry can advance W&B's internal _step without advancing
the optimizer step.

The corrected sequence for the same run would be:

W&B entry 0: trainer/step=0, validation=0.10
W&B entry 150: trainer/step=150, training metrics
W&B entry 151: trainer/step=150, validation=0.17

The last two entries are consecutive W&B events for the same model
version. Both plot at trainer/step=150, so the final validation is kept in
W&B history and its 0.17 becomes the latest validation summary value.
Charts explicitly configured to use W&B's _step should instead use
trainer/step when they need optimizer steps on the x-axis.

Only the RL metrics processor enables this behavior. Console and
TensorBoard retain the actual optimizer step, and other WandBLogger
callers keep explicit-step logging. Existing runs are not backfilled:
the fix prevents this loss of data on future runs.

Test Plan:

  • 88 targeted CPU tests passed for RL metrics and WandBLogger.
  • Offline W&B smoke check: validation 0.10 at model step 0, training at
    step 150, and validation 0.17 at model step 150; the W&B summary reports
    0.17 at trainer/step=150.

[ghstack-poisoned]
@vkuzo

vkuzo commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor Author

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 28, 2026
felipemello1 added a commit that referenced this pull request Nov 7, 2026
… a step are kept (#5903)

## Summary (human-written)
Previously I added "commit=True" to titan, because in coreweave i would
see multiple steps and wouldnt see logging.
This damage pretraining, because log is called twice, and once you log a
step, you connect log again for the same step.

This PR solves both problems in one way:
- Add a step_metric, instead of step

This automatically commits, and support multiple .log calls to the same
step.

## Agent details

**TLDR:** W&B drops every validation row logged at a step that training
already logged. That is every core validation row at the default
settings. Now each W&B row carries the training step as `trainer/step`
and logs without `step=`, so W&B keeps every row, in core training and
RL alike. On the core debug model, main keeps 1 of 4 validation rows in
W&B; this PR keeps 4 of 4.

```text
main:     log(train, step=10)       -> row 10 sent
          log(validation, step=10)  -> dropped
this PR:  log(train, step=10)       -> row {trainer/step: 10, loss: ...}
          log(validation, step=10)  -> row {trainer/step: 10, validation_metrics/loss: ...}
```

```python
# before
self.wandb.log(wandb_metrics, step=step, commit=True)
# after (the two define_metric calls run once, in __init__)
self.wandb.define_metric("trainer/step")
self.wandb.define_metric("*", step_metric="trainer/step")
self.wandb.log({**wandb_metrics, "trainer/step": step})
```

**Problem**
1. `WandBLogger.log` calls `wandb.log(metrics, step=N, commit=True)`.
2. W&B ignores a log to a step below its current one. After row N is
sent, step N is below it.
3. Core training logs validation right after training, at the same step
(`Trainer.train`).
4. At the defaults (`log_freq=10`, validator `freq=10`), both log at the
same steps, so every core validation row is dropped.
5. RL drops its final validation pass the same way: it logs at the last
step, after training logged that step.

**Solution**
1. Every row carries the training step in a regular key, `trainer/step`.
2. Rows log without `step=`, so W&B sends each one at once.
`commit=True` is W&B's default when `step` is not given.
3. `define_metric` makes `trainer/step` the x-axis of the generated
charts.
4. Unchanged: TensorBoard and MLflow. Metric keys keep their names;
every W&B row gains one key, `trainer/step`.

**Results**
```text
main vs this PR, same recipe: core debug model (llama3 debugmodel), 1 GPU, 10 steps, log_freq=5, validator freq=5.
Validation ran at steps 1, 2, 5 and 10. Training logged at steps 1, 5 and 10. W&B rows read back with the API.

                          main (9f70e55)            this PR
validation rows in W&B    1 of 4 (step 2)             4 of 4 (steps 1, 2, 5, 10)
training rows in W&B      3 (steps 1, 5, 10)          3 (steps 1, 5, 10)
summary validation loss   6.97 (step 2's, stale)      4.14 (step 10's)
```
- On main, W&B dropped validation at steps 1, 5 and 10: training had
already logged those steps.

```text
Online run, wandb 0.31.0, with this PR's WandBLogger at its head. Rows read back with wandb.Api().run(...).scan_history(), 20 s after close():

_step  trainer/step  logged
0      0             validation/reward 0.1
1      1             loss 0.9
2      2             loss 0.8
3      3             loss 0.7
4      1             validation/reward 0.2   <- older trainer/step, logged after step 3: kept
5      4             loss 0.6
6      2             validation/reward 0.3   <- last row, older trainer/step: kept
```
- All 7 rows were kept.
- With `wandb.log` calls that pass no `step=`, a CPU repro also measured
rows landing in 7-18 s, and every row surviving a kill 20 s after the
last log.
- Not checked: a kill right after a log.

```text
dapo_math, Qwen3.5-4B-Base, 4K context, 1 trainer GPU + 3 generator GPUs, seed 0, 10 steps; W&B rows read back

                  this PR                                    before this PR (same recipe, 50 steps, 11-02)
validation rows   2: trainer/step 0 and 10 (the final pass)   1: step 0; the final pass at step 50 was dropped
training rows     10 (trainer/step 1-10)                      50
steps 1-10        reward ~0.2, loss ~0.02, grad norm ~0.09 in both runs; the same training keys,
                  apart from keys added on main since 11-02
```

**Costs.** W&B's `_step` (`Step` in the UI) now counts rows:
1. A saved panel with `Step` on its x-axis must switch to
`trainer/step`. Runs from before this PR have no `trainer/step`, so a
panel that mixes old and new runs needs `Step` for the old ones. Not
checked in the UI.
2. `WANDB_RESUME_FROM` and `WANDB_FORK_FROM` take `<run>?_step=N`. To
rewind to training step S, look up the `_step` of a row whose
`trainer/step` is S (`METRICS.md` shows how). A rewind also drops
validation rows for older steps that were logged after that row.
3. Only if you set `WANDB_RUN_ID` to an existing run (torchtitan starts
a new run on every launch): the run appends its rows, so replayed steps
show twice. Before, W&B dropped them.
4. The runs table's `trainer/step` column shows the last row's step.
After a validation row for an older step, that is not the newest step.

You may ask:
- Why not RL only? #5648 makes the same change opt-in, and only RL opts
in. Core keeps `step=`, so core validation rows stay dropped (#5027).
This PR changes the default for core and RL.
- Why not just drop `commit=True`? W&B then holds row N until step N + 1
logs. RL rows would land one RL step late (minutes), and a scancel or
time limit would lose the last row.
- Why not collect a step's logs and commit after the last one? Every
trainer would need to call that commit, after its last log of each step.
#5227 did this; it is closed in favor of this PR.
- Is it OK that `Step` counts rows for every W&B user? It follows W&B's
own guidance. The `wandb.log` docstring (wandb 0.31.0) says the step
must always increase, that any metric can be the x-axis, and that it is
often better to treat the W&B step like a timestamp than a training
step.

NOTE:
- Fixes #5027.
- The RL periodic-validation PR (#5228) logs each pass at the step it
started, after training has logged later steps. It needs this PR, or W&B
drops those rows.

**Test plan**
```text
PYTHONPATH=$PWD pytest tests/unit_tests/cpu/observability tests/rl/unit_tests/cpu/test_metrics.py -q   # 230 passed, 1 skipped (main: 230 passed, 1 skipped)
PYTHONPATH=$PWD pytest tests/unit_tests/cpu -q --continue-on-collection-errors                         # see the line below
pre-commit run --files <changed files>                                                                 # all hooks pass; pyrefly 0 errors
```
- `tests/unit_tests/cpu`: 1927 passed, 5 failed, 5 skipped, 3 xfailed, 1
error. Main fails the same 5 tests and errors on the same file on this
host (aarch64 CPU: inductor's flex_attention lowering in the LoRA and
Qwen3.5 mrope tests, and the quantization test's import).
- The RL logger test's call assert now expects `{"k": 1.0,
"trainer/step": 3}` (`tests/rl/unit_tests/cpu/test_metrics.py`).
- Not checked in the W&B UI: a saved panel that mixes runs from before
and after this PR. Runs from before have no `trainer/step` key (checked
on the 11-02 dapo run's rows), so a panel on `trainer/step` has no x
value for them.
- CI: `RL Multi-GPU Unit Tests` fails
`test_bitwise_parity.py::TestBitwiseParityQwen35DebugVarlen::test_trainer_vs_vllm_prefill`.
Main `9f70e5597` fails the same test.

---------

Co-authored-by: Felipe Mello <[email protected]>

This branch has not been deployed

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

Labels

ciflow/rl 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.

1 participant