Skip to content

[Typing] Improve ModelProtocol typing - #2246

Merged
fegin merged 10 commits into
mainfrom
gh/fegin/61/head
Jan 20, 2026
Merged

fegin merged 10 commits into
mainfrom
gh/fegin/61/head

Conversation

@fegin

@fegin fegin commented Jan 16, 2026 •

Copy link
Copy Markdown
Contributor

fegin added 2 commits January 16, 2026 14:41
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Jan 16, 2026
ghstack-source-id: e22c3a7
Pull-Request: #2246
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jan 16, 2026
fegin added 2 commits January 20, 2026 00:09
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Jan 20, 2026
ghstack-source-id: 96de613
Pull-Request: #2246
fegin added 2 commits January 20, 2026 00:18
[ghstack-poisoned]
[ghstack-poisoned]
fegin added 4 commits January 20, 2026 09:54
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@fegin fegin changed the title [Not Ready] Improve ModelProtocol typing [Typing] Improve ModelProtocol typing Jan 20, 2026


class ModelProtocol(Protocol):
class ModelProtocol(nn.Module):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it because multi-inheritance is not friendly for typing?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. 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.

  2. Make the ModelProtocol define all the nn.Module methods that we may use. This seems to be overkill.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. Classes are explicitly multiply inheriting from ModelProtocol and nn.Module, but pyrefly does not understand the multiple inheritance (a bug in pyrefly).
  2. 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).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.



class ModelProtocol(Protocol):
class ModelProtocol(nn.Module):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@fegin
fegin changed the base branch from gh/fegin/61/base to main January 20, 2026 22:33
@fegin
fegin merged commit 42fd903 into main Jan 20, 2026
fegin added a commit that referenced this pull request Jan 20, 2026
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.
wwwjn pushed a commit to wwwjn/torchtitan that referenced this pull request Jan 30, 2026
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 13, 2026
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 13, 2026
TXacs pushed a commit to McmillanTAC/torchtitan that referenced this pull request Apr 13, 2026
TXacs pushed a commit to McmillanTAC/torchtitan that referenced this pull request Apr 13, 2026
ACharacterInASimulation pushed a commit to ACharacterInASimulation/torchtitan that referenced this pull request Apr 21, 2026
ACharacterInASimulation pushed a commit to ACharacterInASimulation/torchtitan that referenced this pull request Apr 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants