Repository navigation
[Typing] Improve ModelProtocol typing - #2246
Conversation
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
|
|
||
|
|
||
| class ModelProtocol(Protocol): | ||
| class ModelProtocol(nn.Module): |
There was a problem hiding this comment.
Is it because multi-inheritance is not friendly for typing?
There was a problem hiding this comment.
Well, it is more about we use ModelProtocol as the type through all the code, which pyrefly cannot understand the relation between ModelProtocol and nn.Module. So the original multi-inheritance that downstream models inheriting both from nn.Module and ModelProtocol doesn't work. We can do:
-
ModelProtocol inherits from both nn.Module and Protocol, but then I don't see the benefit of using Protocol + nn.Module compared to inheriting from only nn.Module.
-
Make the ModelProtocol define all the nn.Module methods that we may use. This seems to be overkill.
There was a problem hiding this comment.
I see. I don't have strong objection, but it indeed sounds a bit weird that a "protocol" inherits a heavy class.
which pyrefly cannot understand the relation between ModelProtocol and nn.Module
This does sound incompatibility between pyrefly and multi-inheritance? Google tells me
Python: Dynamic typing relies on isinstance() for runtime checks, but static type checkers (like Mypy) use complex algorithms (C3 MRO) to determine the correct method resolution order for type inference in multiple inheritance.
I wonder what's your take @rchen152
There was a problem hiding this comment.
Do you have an example of a case in which pyrefly doesn't understand the relationship between ModelProtocol and nn.Module? Without a concrete example, it's hard for me to tell which situation is happening:
- Classes are explicitly multiply inheriting from ModelProtocol and nn.Module, but pyrefly does not understand the multiple inheritance (a bug in pyrefly).
- Classes are only explicitly inheriting from either nn.Module or ModelProtocol, but they are conceptually linked in a way that isn't encoded in the types (either a limitation of the type system or a bug in torchtitan).
There was a problem hiding this comment.
This does sound incompatibility between pyrefly and multi-inheritance? Google tells me
No, I don't think so. If we have an object that inherits from both ModelProtocol and nn.Module, pyrefly can figure it out. But the current issue is that we upcast the object to ModelProtocol in our code (e.g., our model_builder returns ModelProtocol), which loses the operation information of nn.Module (e.g., state_dict). pyrefly has to report the errors because ModelProtocol doesn't define state_dict.
There was a problem hiding this comment.
I see. It sounds that torchtitan doesn't enforce hygiene when using the protocol.
The alternative solution is to create another class which inherits ModelProtocol and nn.Module, which seems unnecessary. I think it's fine to land this -- I plan to do some global refactor anyways.
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * #2260 * #2246 * __->__ #2245 As title
|
|
||
|
|
||
| class ModelProtocol(Protocol): | ||
| class ModelProtocol(nn.Module): |
There was a problem hiding this comment.
I see. It sounds that torchtitan doesn't enforce hygiene when using the protocol.
The alternative solution is to create another class which inherits ModelProtocol and nn.Module, which seems unnecessary. I think it's fine to land this -- I plan to do some global refactor anyways.
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * __->__ #2260 * #2246 * #2245 This API is not needed any more. pyrefly will give us a warning.
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * pytorch#2260 * pytorch#2246 * __->__ pytorch#2245 As title
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * pytorch#2260 * __->__ pytorch#2246 * pytorch#2245
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * __->__ pytorch#2260 * pytorch#2246 * pytorch#2245 This API is not needed any more. pyrefly will give us a warning.
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * pytorch#2260 * pytorch#2246 * __->__ pytorch#2245 As title
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * pytorch#2260 * __->__ pytorch#2246 * pytorch#2245
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * __->__ pytorch#2260 * pytorch#2246 * pytorch#2245 This API is not needed any more. pyrefly will give us a warning.
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * pytorch#2260 * pytorch#2246 * __->__ pytorch#2245 As title
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * pytorch#2260 * __->__ pytorch#2246 * pytorch#2245
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * __->__ pytorch#2260 * pytorch#2246 * pytorch#2245 This API is not needed any more. pyrefly will give us a warning.
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * pytorch#2260 * pytorch#2246 * __->__ pytorch#2245 As title
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * pytorch#2260 * __->__ pytorch#2246 * pytorch#2245
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * __->__ pytorch#2260 * pytorch#2246 * pytorch#2245 This API is not needed any more. pyrefly will give us a warning.
Stack from ghstack (oldest at bottom):