Skip to content

[metrics] W&B: log each row with a trainer/step key, so later logs to a step are kept - #5903

Merged
felipemello1 merged 9 commits into
mainfrom
felipemello1/wandb-trainer-step
Nov 7, 2026
Merged

felipemello1 merged 9 commits into
mainfrom
felipemello1/wandb-trainer-step

Conversation

@felipemello1

@felipemello1 felipemello1 commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

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.

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: ...}
# 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

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 (9f70e5597)            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.
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.
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? Fix final RL validation logging in W&B #5648 makes the same change opt-in, and only RL opts in. Core keeps step=, so core validation rows stay dropped (wandb silently drop validator log #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. [fix] wandb: stop dropping validation metrics logged at a training step (1/2) #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:

Test plan

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.

Felipe Mello added 3 commits November 6, 2026 07:25
… a step are kept

WandBLogger.log called wandb.log(metrics, step=N, commit=True). W&B drops any
later log to a step it already sent, so validation logged at a step that
training already logged never reached W&B. At the core defaults (log_freq=10,
validator freq=10) that is every validation row; RL drops its final pass.

Now every row carries the training step as "trainer/step", and rows log
without step=, so W&B sends each one at once and keeps it. define_metric makes
trainer/step the x-axis of the generated charts, and its summary the max.
W&B's _step now counts rows: METRICS.md says how to rewind or fork by
trainer/step, and what changes for saved panels and resumed run ids.
…p= rule once

summary="max" only adds a trainer/step.max summary key; summary["trainer/step"] still holds the
last row's value, so the comment that claimed otherwise was wrong. The plain
define_metric("trainer/step") matches the run that showed every row kept.

The reason rows log without step= now lives at the log call and in METRICS.md only.
@pytorch-bot pytorch-bot Bot added the ciflow/rl label Nov 6, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 6, 2026
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: requesting a review of this draft.

@claude please review

@claude

claude Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 2m 12s —— View job


Needs discussion -- the patch is correct and the smallest of the three open fixes (#5227, #5648, #5903); maintainers need to pick one and accept that W&B's Step now counts rows. Review

@claude claude Bot left a comment

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.

Verdict: Needs discussion -- the patch is correct and the smallest of the three open fixes for this bug (#5227, #5648, #5903). But it changes what W&B's Step means for every W&B user, and the maintainers have to choose one of the three PRs.

What this PR does. WandBLogger.log no longer passes step= to wandb.log. Instead it adds the training step to every row as a regular key, trainer/step, and __init__ uses define_metric to make trainer/step the x-axis for all metrics. So every call becomes its own row, sent right away, and W&B never sees a log to a step lower than the one it already has, which is when it drops rows.

Example. Core trainer at the defaults (log_freq=10, validator freq=10), step 10:

main:  wandb.log({loss..}, step=10, commit=True)             -> W&B internal step becomes 11
       wandb.log({validation_metrics/loss..}, step=10, ...)  -> 10 < 11, dropped
PR:    wandb.log({loss.., trainer/step: 10})                 -> _step=k
       wandb.log({validation_metrics/loss.., trainer/step: 10}) -> _step=k+1, kept

I checked this on HEAD: Trainer.train logs training (trainer.py:494) and then validation (trainer.py:539 -> MetricsProcessor.log_validation, metrics.py:652) at the same step. So on main every core validation row is dropped at the defaults.

Must fix

  1. PR description: "Summary (human-written)" is empty. Also, the description mentions "an open RL-only draft", "an open PR", "the open issue", and "the RL periodic-validation PR" without numbers. Link them (#5648, #5227, the issue, the validation PR) so maintainers can compare.
  2. Overlapping PRs: #5227 (yours) and #5648 (vkuzo) edit the same wandb.log line, so only one of the three can land. If maintainers pick this one, close #5227, and coordinate with vkuzo on #5648. #5648 uses the same trainer/step + define_metric idea, but as an opt-in step_metric argument and only for RL.
  3. torchtitan/observability/METRICS.md:38 -- the doc narrates history ("before this change"). Inline suggestion attached.

Questions

  • For maintainers: are you OK with _step / Step counting rows instead of training steps for all W&B users? That affects saved panels, WANDB_RESUME_FROM/WANDB_FORK_FROM (?_step=N no longer means training step N), and duplicate rows when a run logs again under the same run id. The PR lists these costs honestly. In my view it is still the right trade: #5227 needs a new BaseLogger.commit() hook that trainer.py and torchft must call at the right time, and #5648 adds a knob and leaves core validation dropped.
  • The test-plan item "saved panel in a project with an older run" is still unchecked. Please run it before this leaves draft: it is the one user-visible cost not yet measured.
Details
  • Gate: the problem is real on main, and the fix is at the right layer (the W&B logger itself). There is no new config field and no new hook in core, and TensorBoard and MLflow are untouched. Net +10 lines of code plus 14 lines of docs.
  • define_metric("trainer/step") followed by define_metric("*", step_metric="trainer/step"): "*" also matches trainer/step itself. That is the same shape as W&B's documented define_metric("train/*", step_metric="train/step") example, and the PR's online read-back shows it works.
  • Within each series, trainer/step only goes up (training rows always; validation passes run one after another), so the lines draw correctly. Out-of-order rows only appear in the mixed trainer/step column of the runs table, which the PR already lists as a cost.
  • The tag prefix never touches the trainer/step key. No in-repo caller passes a non-None tag today, so a tagged step metric can't collide with it.
  • No new test is needed. The updated RL assert covers the call shape, and asserting the define_metric mock calls would just repeat mock calls (tests.md).

Not verified: how saved W&B panels show a mix of old runs (x-axis Step) and new runs (x-axis trainer/step).

Comment thread torchtitan/observability/METRICS.md Resolved
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: requesting a review of the updated draft.

@claude please review

@claude

claude Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 53s —— View job


Needs discussion: no new code problems and the METRICS.md wording is fixed, but maintainers still need to pick one of #5227 / #5648 / #5903 and decide whether W&B's Step should count rows. Review

@claude claude Bot left a comment

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.

Verdict: Needs discussion -- the code has no new problems since the last review, and the METRICS.md history wording is fixed. Two decisions still belong to the maintainers: which of #5227 / #5648 / #5903 lands, and whether W&B's Step should count rows for every W&B user.

What changed since the last review (commits 8e34bc1, a45e04a, plus a merge of main). These touch only METRICS.md and wording. The metrics.py logic is the same: rows go out without step=, carry trainer/step, and define_metric("*", step_metric="trainer/step") runs once in __init__ (metrics.py:254-265).

Example. Core trainer at the defaults, step 10:

main:  log({loss..}, step=10, commit=True)           -> sent; W&B's step moves past 10
       log({validation_metrics/loss..}, step=10)     -> dropped
PR:    log({loss.., trainer/step: 10})               -> _step=k
       log({validation_metrics/loss.., trainer/step: 10}) -> _step=k+1, kept

Status of earlier must-fix items

  1. Description links: resolved. #5027, #5227, #5648 and #5228 are now numbered.
  2. METRICS.md narrating history: resolved. METRICS.md:38 now states only the current rule ("In saved panels, use trainer/step as the x-axis").
  3. "Summary (human-written)": still empty. The author fills this in, not an agent, before the PR leaves draft.
  4. Overlap with #5227 and #5648: still open. All three edit the same wandb.log line, so only one can land. If this one is picked, close #5227 and coordinate with vkuzo on #5648.

Questions

  • For maintainers: is _step / Step counting rows (not training steps) acceptable for all W&B users? The costs are in the description and METRICS.md:37-44: saved panels, ?_step=N in WANDB_RESUME_FROM / WANDB_FORK_FROM, and replayed steps appearing twice when a run logs again under the same id. I still think this is the right trade over #5227, which adds a BaseLogger.commit() call to core, and #5648, which is RL only and leaves core validation dropped.
  • Test-plan item "saved panel in a project with an older run" is still unchecked. It is the one user-visible cost no one has measured yet.
Details
  • WandBLogger.log is the only wandb.log / define_metric call in the repo, so no other callsite still passes step=.
  • The tag prefix applies only to metric keys. trainer/step is never prefixed, and no in-repo caller passes a non-None tag.
  • The docstring example (metrics.py:208-210) uses validation_loss, but the real key is validation_metrics/loss. That only matters for illustration and doesn't need a change.
  • No new test needed: the updated RL assert (tests/rl/unit_tests/cpu/test_metrics.py:690) pins the call shape.

Not verified: how saved W&B panels display a mix of old runs (x-axis Step) and new runs (x-axis trainer/step).

@felipemello1 felipemello1 left a comment

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.

this pr doesnt make any chanes to RL or the trainer. Do we need to?

Also, you ran wandb in main vs this pr. Please highlight the results in the pr.

Comment thread torchtitan/observability/METRICS.md Resolved
Comment thread torchtitan/observability/metrics.py Resolved
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: addressed the open review items at 7c5a54b:

  • [fix] wandb: stop dropping validation metrics logged at a training step (1/2) #5227 is closed in favor of this PR. The body now says how Fix final RL validation logging in W&B #5648 differs: the same change, opt-in, used only by RL, so core validation rows stay dropped there.
  • Step counting rows: the body cites W&B's own wandb.log docstring (0.31.0), which says to treat the W&B step like a timestamp and use any metric as the x-axis.
  • The saved-panel check is now a NOTE (not checked in the UI; older runs have no trainer/step key).
  • The WandBLogger docstring example uses the real core key, validation_metrics/loss.
  • The human-written summary is filled in.

@claude please review

@claude

claude Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 1m 29s —— View job


Needs discussion: the earlier bot items are resolved, but your own 02:20 comments on METRICS.md and the metrics.py code comment are still open (suggestions attached), and maintainers still need to choose between this PR and #5648. Review

@claude claude Bot left a comment

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.

Verdict: Needs discussion -- the code is still correct, and every item from the last bot review is resolved. Two of your own 02:20 inline comments are still open at HEAD. Maintainers still have to choose between this PR and #5648, and decide whether W&B's Step should count rows.

What this PR does. WandBLogger.log no longer passes step= to wandb.log. It adds the training step to each row as a trainer/step key, and define_metric makes trainer/step the x-axis of the charts. Each call becomes its own row, so a validation row logged after training at the same step is no longer dropped.

Example. Core trainer at the defaults, step 10:

main:  log({loss..}, step=10, commit=True)          -> sent
       log({validation_metrics/loss..}, step=10)    -> dropped
PR:    log({loss.., trainer/step: 10})              -> _step=k
       log({validation_metrics/loss.., trainer/step: 10}) -> _step=k+1, kept

Status of earlier items

  • Human-written summary: resolved.
  • #5227: resolved, now closed.
  • How #5648 differs: resolved, explained in the body. #5648 (vkuzo) is still open and edits the same line, so a maintainer has to choose one PR.
  • Step counting rows: the body now cites W&B's wandb.log docstring. The decision still belongs to the maintainers.
  • Docstring key: resolved (validation_metrics/loss).

Must fix

  1. torchtitan/observability/METRICS.md:33-45 -- your comment (make it less verbose; does titan resume runs?) is still open. On resume: every launch, checkpoint resume included, creates a new W&B run, because WANDB_RUN_ID / WANDB_RESUME_FROM / WANDB_FORK_FROM are unset by default (metrics.py:243-249). So the duplicate-rows bullet can go. The inline comment has a 5-line suggestion.
  2. torchtitan/observability/metrics.py:263-264 -- your comment (the wording reads like it describes the old behavior) is still open. The inline comment has a 2-line suggestion.

Answers to your 02:20 review

  • "This PR doesn't change RL or the trainer. Do we need to?" No. Core (MetricsProcessor) and RL recipes (torchtitan_recipes/rl/alphabet_sort.py) both log through this WandBLogger.log, so fixing it here covers both. The only RL change needed is the updated assert in tests/rl/unit_tests/cpu/test_metrics.py:690. Changing the trainer would be the #5227 approach, which is now closed.
  • "Highlight main vs this PR results." Done: the body now has the dapo_math table (validation rows: 2 here vs 1 on main). The two runs aren't the same length (10 vs 50 steps), but the dropped row (the final pass) is the same in both, so the comparison holds.

Not verified: what wandb.init(id=<existing id>) does without resume= in wandb 0.31; how saved panels show a mix of old (Step) and new (trainer/step) runs.

Comment thread torchtitan/observability/METRICS.md Resolved
Comment thread torchtitan/observability/metrics.py Resolved
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: answers to the 02:20 review, at 7b9805a:

  • RL and the trainer need no change. Core (torchtitan/observability/metrics.py, MetricsProcessor) and RL (torchtitan/rl/observability/metrics/processor.py) both build their W&B backend from WandBLogger.Config, so the fix in WandBLogger.log covers both. The only RL change is the updated test assert.
  • main vs this PR is now the first Results block: the same core debug-model recipe on main and on this PR. main keeps 1 of 4 validation rows in W&B; this PR keeps 4 of 4.
  • METRICS.md is 5 lines, and the log comment says only what the code relies on (replies in those threads).

@claude please review

@claude

claude Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 39s —— View job


Needs discussion: everything from earlier reviews is resolved at 7b9805a and I found no new problems; maintainers still need to choose between this PR and #5648 and decide whether W&B's Step should count rows. Review

@claude claude Bot left a comment

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.

Verdict: Needs discussion -- every author and bot item is now resolved at 7b9805a and I found no new problems. Two decisions are left for the maintainers: whether this PR or #5648 lands, and whether W&B's Step should count rows for every W&B user.

What this PR does. WandBLogger.log no longer passes step= to wandb.log. It adds the training step to each row as a trainer/step key, and define_metric("*", step_metric="trainer/step") makes that key the x-axis. Each call becomes its own row, so a validation row logged after training at the same step is no longer dropped.

Example. Core trainer at the defaults, step 10:

main:  log({loss..}, step=10, commit=True)                -> sent
       log({validation_metrics/loss..}, step=10)          -> dropped
PR:    log({loss.., trainer/step: 10})                    -> _step=k
       log({validation_metrics/loss.., trainer/step: 10}) -> _step=k+1, kept

Status of earlier items

  • METRICS.md:33-37 (you asked for it to be less verbose and to drop the resume note): resolved. It is now 5 lines. The replay bullet is gone, which is right, since every launch starts a new run unless WANDB_RUN_ID is set. The ?_step=N clause stays, because _step no longer means the training step.
  • metrics.py:263-264 comment (it read like the old behavior): resolved. It now states only what the code relies on.
  • "Does RL or the trainer need a change?": resolved, no. WandBLogger is the only wandb.log / define_metric call in the repo. Core MetricsProcessor and every RL recipe (dapo_math, search_r1, verifiers_dapo_math, alphabet_sort) build it from WandBLogger.Config(). The RL test goes through MetricsProcessor.log -> WandBLogger.log, so its updated assert pins the new call shape.
  • main-vs-PR results: resolved. The first Results block runs the same core debug recipe on both: 1 of 4 validation rows on main, 4 of 4 on this PR.

Questions

  • For maintainers: #5648 (vkuzo, still open) edits the same wandb.log line as an opt-in that only RL uses. Only one of the two can land. Please pick one, and decide whether Step counting rows (the costs are in the description and METRICS.md) is acceptable for all W&B users.

Not verified: how saved W&B panels show a mix of runs from before this PR (no trainer/step key) and runs from after it.

@felipemello1
felipemello1 marked this pull request as ready for review November 7, 2026 03:03
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 7, 2026
@github-actions
github-actions Bot requested a review from pzhan9 November 7, 2026 03:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests 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.

wandb silently drop validator log

2 participants