Skip to content

feat: add filter_keys​ to log only specified device stats#21707

Merged
deependujha merged 4 commits into
Lightning-AI:masterfrom
deependujha:feat/device-stats-filter-keys
Jun 1, 2026
Merged

feat: add filter_keys​ to log only specified device stats#21707
deependujha merged 4 commits into
Lightning-AI:masterfrom
deependujha:feat/device-stats-filter-keys

Conversation

@deependujha
Copy link
Copy Markdown
Collaborator

@deependujha deependujha commented May 9, 2026

What does this PR do?

Adds a filter_keys: Optional[Set[str]] = None argument to DeviceStatsMonitor.
When provided, only the specified keys from get_device_stats() are logged.
If a key in filter_keys is not found in the collected stats, a rank_zero_warn is emitted to catch typos early.

fixes #11796

Motivation

The original issue requested fine-grained control over which device metrics get logged.
The cpu_stats flag added in #11795 resolved the CPU+GPU simultaneous logging gap.
This PR addresses the remaining open ask: filtering which specific metric keys are tracked,
without introducing per-accelerator flags or a new class hierarchy.

Changes

  • DeviceStatsMonitor.__init__ accepts a new filter_keys argument
  • Filtering happens after all stats are collected (post CPU merge), so it works
    uniformly across all accelerators
  • Unrecognized keys emit a rank_zero_warn rather than raising, to avoid breaking
    runs over a typo

Usage

# log only peak and current allocated GPU memory
DeviceStatsMonitor(
    filter_keys={"allocated_bytes.all.current", "allocated_bytes.all.peak"}
)

# log CPU stats alongside a subset of metrics
DeviceStatsMonitor(
    cpu_stats=True,
    filter_keys={"cpu_percent", "allocated_bytes.all.current"},
)

Tests

  • test_device_stats_monitor_filter_keys: parametrized, verifies correct keys are
    present/absent on CPU
  • test_device_stats_monitor_filter_keys_unrecognized_warns: verifies UserWarning
    is emitted for unknown keys
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

📚 Documentation preview 📚: https://pytorch-lightning--21707.org.readthedocs.build/en/21707/

@github-actions github-actions Bot added the pl Generic label for PyTorch Lightning package label May 9, 2026
@deependujha deependujha requested a review from Copilot May 9, 2026 12:25
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds an optional filter_keys argument to DeviceStatsMonitor to allow users to restrict which device stats keys get logged, with a warning for unknown keys to help catch typos.

Changes:

  • Extend DeviceStatsMonitor.__init__ with filter_keys: Optional[set[str]] and apply filtering before logging.
  • Emit a rank_zero_warn when filter_keys contains keys not present in collected stats.
  • Add unit tests covering key filtering behavior and warning emission for unknown keys.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/lightning/pytorch/callbacks/device_stats_monitor.py Adds filter_keys support, warning on unknown keys, and updates docstring/examples.
tests/tests_pytorch/callbacks/test_device_stats_monitor.py Adds tests validating filtering behavior and warning on unrecognized keys.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/lightning/pytorch/callbacks/device_stats_monitor.py Outdated
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 9, 2026

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

Codecov Report

❌ Patch coverage is 84.61538% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 79%. Comparing base (932b7e3) to head (beed2c7).
⚠️ Report is 4 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 (932b7e3) and HEAD (beed2c7). Click for more details.

HEAD has 2027 uploads less than BASE
Flag BASE (932b7e3) HEAD (beed2c7)
cpu 497 42
python 36 3
lightning_fabric 160 0
pytest 248 0
python3.12 143 12
python3.13 106 9
lightning 176 15
python3.11 70 6
python3.12.7 107 9
python3.10 35 3
pytorch2.5.1 18 3
pytest-full 249 42
pytorch2.8 36 6
pytorch_lightning 161 27
pytorch2.9 36 6
pytorch2.10 36 6
pytorch2.1 35 6
pytorch2.2.2 17 3
pytorch2.3 18 3
pytorch2.7 17 3
pytorch2.6 18 3
pytorch2.4.1 18 3
Additional details and impacted files
@@            Coverage Diff            @@
##           master   #21707     +/-   ##
=========================================
- Coverage      87%      79%     -8%     
=========================================
  Files         270      267      -3     
  Lines       23973    23926     -47     
=========================================
- Hits        20748    18814   -1934     
- Misses       3225     5112   +1887     

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread src/lightning/pytorch/callbacks/device_stats_monitor.py
Comment thread tests/tests_pytorch/callbacks/test_device_stats_monitor.py
@deependujha deependujha merged commit 2849907 into Lightning-AI:master Jun 1, 2026
92 of 94 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pl Generic label for PyTorch Lightning package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improved control of device stats callbacks

4 participants