Fix SpikeDetection.load_state_dict referencing nonexistent self.running#21752
Open
Kymi808 wants to merge 1 commit into
Open
Fix SpikeDetection.load_state_dict referencing nonexistent self.running#21752Kymi808 wants to merge 1 commit into
Kymi808 wants to merge 1 commit into
Conversation
`SpikeDetection.__init__` stores the running-mean metric on
`self.running_mean` (line 57). `SpikeDetection.state_dict` saves it under
the key `"running"` via `self.running_mean.state_dict()` (line 156).
`SpikeDetection.load_state_dict` then tries to restore it with
`self.running.load_state_dict(...)` — but `self.running` is never defined,
so any resume of a SpikeDetection callback state crashes with:
AttributeError: 'SpikeDetection' object has no attribute 'running'
The asymmetry with the symmetric line right below it
(`self.running_mean.base_metric.load_state_dict(...)`) confirms it's a
typo of `self.running_mean`, not a deliberate different attribute. Adds a
regression test that round-trips state_dict / load_state_dict on a fresh
SpikeDetection.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SpikeDetectioninsrc/lightning/fabric/utilities/spike.pystores its running-mean metric onself.running_mean(set in__init__at line 57) and saves it viastate_dictunder the key"running":But
load_state_dictthen tries to restore it viaself.running, an attribute that is never defined anywhere:So any resume of a
SpikeDetectioncallback's state crashes:The asymmetry with the very next line — which correctly uses
self.running_mean.base_metric— confirms it's a typo forself.running_mean, not a deliberate different attribute.state_dictandload_state_dictwere never round-trippable.Fix
Test
Adds
test_spike_detection_state_dict_roundtripnext to the existing tests intests/tests_fabric/utilities/test_spike.py. It constructs twoSpikeDetectioninstances and round-tripsstate_dict/load_state_dict. Fails onmasterwith theAttributeErrorabove; passes with this change. Reuses the same_TORCHMETRICS_GREATER_EQUAL_1_0_0skip the surrounding tests use.ruff checkandruff format --checkclean on both files.Notes
No related open PRs:
gh pr list --repo Lightning-AI/pytorch-lightning --state all --search "SpikeDetection"returns #21664 (anOverride-decorator typing change, unrelated) and #19282 (a kwarg-typo fix merged in 2024, unrelated). No test pins the current broken behavior.