Fix NoneType crash when model.eval() does not return self#578
Open
Pchambet wants to merge 1 commit intojacobgil:masterfrom
Open
Fix NoneType crash when model.eval() does not return self#578Pchambet wants to merge 1 commit intojacobgil:masterfrom
Pchambet wants to merge 1 commit intojacobgil:masterfrom
Conversation
Separate model assignment from the eval() call so that self.model
is always the model instance, even when a custom Module subclass
overrides eval() without returning self.
Standard nn.Module.eval() returns self, but third-party wrappers
(e.g. some quantization or tracing toolkits) sometimes omit the
return, causing model.eval() to yield None. The next line then
fails with:
AttributeError: 'NoneType' object has no attribute 'parameters'
This follows the same defensive pattern already used in
guided_backprop.py (line 49).
Fixes jacobgil#569
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.
Problem
Passing a model whose
.eval()does not returnselfcauses an immediate crash inBaseCAM.__init__:Root cause
Standard
nn.Module.eval()returnsself, but some third-party wrappers (quantization toolkits, custom tracing wrappers) override.eval()without propagating the return value — makingmodel.eval()returnNone.Fix
Separate assignment from the
.eval()call:This guarantees
self.modelalways holds the model reference, regardless of what.eval()returns.Precedent in this codebase
guided_backprop.pyalready uses the safe pattern:This PR aligns
base_cam.pywith that existing convention.Fixes #569