fix: handle torch.distributions.Distribution outputs in summary() (#329)#394
Merged
Conversation
Models that return a distribution object (e.g. Categorical) from their forward pass previously raised a TypeError. Now batch_shape + event_shape is used as the output size with elem_bytes=0, covering all Distribution subclasses. Fixes TylerYep#329 Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Adds CategoricalOutputModel and NormalOutputModel fixtures and two tests that verify summary() completes and reports correct shapes for models returning torch.distributions.Categorical and Normal. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Owner
|
Thank you for the fix! |
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.
Bug
torchinfo.summary()raises aTypeErrorwhen a model'sforward()returns atorch.distributions.Distributionobject (e.g.Categorical,Normal) instead of a plain tensor.Root cause:
LayerInfo.calculate_size()inlayer_info.pyhandlesTensor,ndarray,dict,list, andtuple, but falls through to a hardraise TypeErrorfor any other type — including distribution objects.Reproduction (from #329):
Fix
Add an
elifbranch incalculate_size()fortorch.distributions.Distribution:batch_shape + event_shapegives the meaningful output shape (e.g.[1]forCategoricalwith a batch of 1).elem_bytes = 0since there is no single underlying tensor to measure.Distributionsubclasses (Categorical,Normal,Bernoulli,MultivariateNormal, etc.), not just the one from the issue report.else: raise TypeErroris preserved for truly unknown types.Tests
test_categorical_distribution_output— verifies the exact reproduction case from Issue withtorchinfo.summary()Failing on Models withtorch.distributions.Categorical#329 no longer raises and reports the correct output shape[1].test_normal_distribution_output— verifies the fix generalises toNormal, which has a non-trivialevent_shape, reporting output shape[1, 5].Closes #329
🤖 Generated with Claude Code