Make FactorizedTensor.__init_subclass__'s name argument optional - #38
Open
speedyshreya wants to merge 1 commit into
Open
Make FactorizedTensor.__init_subclass__'s name argument optional#38speedyshreya wants to merge 1 commit into
speedyshreya wants to merge 1 commit into
Conversation
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.
Fixes tensorly/tensorly#614 (issue filed on the main tensorly repo; the affected code belongs in this repo).
Problem
torch.nn.utils.parametrize.register_parametrization dynamically builds a subclass of the module it's applied to, via type(f"Parametrized{cls.name}", (cls,), dct). Because init_subclass declared name with no default, this raised TypeError: init_subclass() missing 1 required positional argument: 'name' — PyTorch has no knowledge of the convention and no way to satisfy it. This affects any FactorizedTensor subclass; the reporter hit it via ComplexTuckerTensor inside a neuraloperator FNO model.
Fix
Default name to ''. The name == '' branch already handles subclasses that shouldn't be registered (it's how TensorizedTensor opts out), so this simply makes that path reachable for callers that can't pass the argument at all.
I deliberately did not default to cls.name as suggested in the issue, because that would set cls._name = 'ParametrizedTuckerTensor' — corrupting the public .name property, documented as returning the factorization name — and add a junk parametrizedtuckertensor key to _factorizations on every parametrization. Inheriting the parent's _name keeps .name == 'Tucker', which is correct.
Explicitly-named subclasses are unaffected, and the existing warning for hand-written subclasses omitting a name still fires.
Tests
Added a parametrized regression test covering Tucker, CP, Dense and their Complex variants. TT is excluded as it has no direct top-level parameter to target.
Note: 3 pre-existing failures in test_trl.py (TypeError: init() got an unexpected keyword argument 'verbose') are unrelated — they reproduce on main without this change.