Skip to content

feat: Log key prefix controls for Trainer, WandbLogger, and CometLogger #21783

Open
pupeno wants to merge 1 commit into
Lightning-AI:masterfrom
pupeno:feat/log-key-prefix
Open

feat: Log key prefix controls for Trainer, WandbLogger, and CometLogger #21783
pupeno wants to merge 1 commit into
Lightning-AI:masterfrom
pupeno:feat/log-key-prefix

Conversation

@pupeno

@pupeno pupeno commented Jun 28, 2026

Copy link
Copy Markdown

What does this PR do?

Fixes #21782

Adds three independent parameters that let users control the metric keys generated by Lightning's
own components, without touching user-provided metric names:

  • Trainer(log_key_prefix=...): prefixes Trainer-generated metric keys. Currently the only such
    key is the automatic epoch metric injected into every logging step. Default None/""
    preserves previous behaviour.

  • WandbLogger(log_key_prefix=...): controls the W&B-generated step key used in define_metric
    and experiment.log. Previously hardcoded to "trainer/global_step"; now configurable,
    defaulting to "global_step". The existing prefix= argument is unchanged and continues to
    apply only to pass-through metric keys.

  • CometLogger(epoch_key=...): controls which incoming metric key is extracted and forwarded to
    Comet's dedicated epoch= argument. Previously hardcoded to "epoch". None disables epoch
    extraction entirely. Default "epoch" preserves previous behaviour.

The design principle: each log_key_prefix (or epoch_key) applies only to keys generated by
that component
, never to keys the user logged via self.log(...) or logger.log_metrics(...).

This makes it straightforward to align all Lightning-generated keys under a shared namespace:

Trainer(log_key_prefix="trainer/")
WandbLogger(log_key_prefix="trainer/")
CometLogger(epoch_key="trainer/epoch")

Breaking change: WandbLogger previously hardcoded "trainer/global_step" as the step axis
key. With this PR, the default is bare "global_step". Pass log_key_prefix="trainer/" to
restore the previous behaviour.

Before submitting
  • Was this discussed/agreed via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or minor internal changes/refactors)

PR review

Anyone in the community is welcome to review the PR.
Before you start reviewing, make sure you have read the review guidelines. In short, see the following bullet-list:

Reviewer checklist
  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

Adds three independent parameters that let users control the metric keys
generated by Lightning's own components, without touching user-provided
metric names:

- `Trainer(log_key_prefix=...)`: prefixes Trainer-generated metric keys.
  Currently the only such key is the automatic `epoch` metric injected
  into every logging step. Default `None`/`""` preserves current
  behavior.

- `WandbLogger(log_key_prefix=...)`: controls the W&B-generated step key
  used in `define_metric` and `experiment.log`. Previously hardcoded to
  `"trainer/global_step"`; now configurable, defaulting to
  `"global_step"`.
  The existing `prefix=` argument is unchanged and continues to apply
  only
  to incoming/pass-through metric keys.

- `CometLogger(epoch_key=...)`: controls which incoming metric key is
  popped and forwarded to Comet's dedicated `epoch=` argument.
  Previously
  hardcoded to `"epoch"`. `None` disables epoch extraction entirely.

The design principle is: each `log_key_prefix` (or `epoch_key`) applies
only to keys *generated by that component*, never to keys the user
logged
via `self.log(...)` or `logger.log_metrics(...)`.

This makes it straightforward to align all Lightning-generated keys
under
a shared namespace. For example:

```python
Trainer(log_key_prefix="trainer/")
WandbLogger(log_key_prefix="trainer/")
CometLogger(epoch_key="trainer/epoch")
```
@deependujha

Copy link
Copy Markdown
Collaborator

Thanks for the thorough proposal and PR, the namespace inconsistency you're describing is real, and the Trainer-side piece is clean.

Would you be open to splitting this into two PRs? I think it'll move faster:

PR 1: Trainer only. The log_key_prefix kwarg on Trainer plus the one-line change in logger_connector.py (setdefault(f"{prefix}epoch", ...)). This part is fully additive and backward-compatible — defaults to bare epoch, so nothing changes for existing users. And because the key is injected upstream in the logger connector before any logger sees it, this benefits every logger downstream without touching any logger file. That makes it reviewable on the Trainer side independently.

PR 2: per-logger keys. Each logger's handling of the keys it injects itself. Those touch logger files and need the respective codeowners, so keeping them separate avoids blocking the Trainer change on that review.

If you split it, the Trainer PR should be straightforward to land. Thanks again.

@pupeno

pupeno commented Jun 28, 2026

Copy link
Copy Markdown
Author

Ah, yes, that makes sense. I'll have it in a moment. Do you want me to create separate github issues or only separate PRs?

@pupeno

pupeno commented Jun 28, 2026

Copy link
Copy Markdown
Author

@codecov-commenter

codecov-commenter commented Jul 2, 2026

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79%. Comparing base (fe6b1cc) to head (eb9bfda).
⚠️ Report is 2 commits behind head on master.
✅ All tests successful. No failed tests found.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

❗ There is a different number of reports uploaded between BASE (fe6b1cc) and HEAD (eb9bfda). Click for more details.

HEAD has 8687 uploads less than BASE
Flag BASE (fe6b1cc) HEAD (eb9bfda)
cpu 1977 42
python 144 3
lightning_fabric 630 0
pytest 988 0
python3.12 567 12
python3.13 413 9
lightning 709 15
python3.11 283 6
python3.12.7 428 9
python3.10 142 3
pytorch2.1 142 6
pytest-full 989 42
pytorch2.8 141 6
pytorch_lightning 638 27
pytorch2.6 72 3
pytorch2.7 70 3
pytorch2.2.2 69 3
pytorch2.4.1 70 3
pytorch2.10 143 6
pytorch2.5.1 72 3
pytorch2.9 140 6
pytorch2.3 70 3
Additional details and impacted files
@@            Coverage Diff            @@
##           master   #21783     +/-   ##
=========================================
- Coverage      87%      79%     -8%     
=========================================
  Files         270      267      -3     
  Lines       24005    23950     -55     
=========================================
- Hits        20776    18825   -1951     
- Misses       3229     5125   +1896     

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Log key prefix controls for Trainer, WandbLogger, and CometLogger

3 participants