Skip to content

[DTensor] Fix mypy on register_op_strategy#167673

Closed
wconstab wants to merge 6 commits intogh/wconstab/455/basefrom
gh/wconstab/455/head
Closed

[DTensor] Fix mypy on register_op_strategy#167673
wconstab wants to merge 6 commits intogh/wconstab/455/basefrom
gh/wconstab/455/head

Conversation

@wconstab
Copy link
Contributor

@wconstab wconstab commented Nov 12, 2025

@pytorch-bot
Copy link

pytorch-bot bot commented Nov 12, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/167673

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (1 Unrelated Failure)

As of commit 16d4c15 with merge base 9760a63 (image):

FLAKY - The following job failed but was likely due to flakiness present on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot bot added ciflow/inductor oncall: distributed Add this issue/PR to distributed oncall triage queue labels Nov 12, 2025
wconstab added a commit that referenced this pull request Nov 12, 2025
ghstack-source-id: 7d4d18b
Pull Request resolved: #167673
cc H-Huang awgu wanchaol fegin fduwjj wz337 d4l3k pragupta msaroufim dcci

[ghstack-poisoned]
wconstab added a commit that referenced this pull request Nov 12, 2025
ghstack-source-id: 59ef401
Pull Request resolved: #167673
@wconstab wconstab added the topic: not user facing topic category label Nov 12, 2025
@wconstab wconstab requested a review from zpcore November 13, 2025 05:17
Copy link
Member

@zpcore zpcore left a comment

Choose a reason for hiding this comment

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

LGTM!

def register_op_strategy(
op, schema_info=None
) -> Callable[[Callable[_P, _T]], Callable[_P, _T]]:
# pyre-fixme[2]: Parameter must be annotated.
Copy link
Collaborator

@Skylion007 Skylion007 Nov 13, 2025

Choose a reason for hiding this comment

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

Actually, wait this is a decorator so that would erase typing info of the thing it wraps

Comment on lines +52 to +55
def register_op_strategy(
op, schema_info=None
) -> Callable[[Callable[_P, _T]], Callable[_P, _T]]:
# pyre-fixme[2]: Parameter must be annotated.

op: Union[torch._ops.OpOverload, list[torch._ops.OpOverload]],
schema_info: Optional[RuntimeSchemaInfo] = None,
) -> Callable[[Callable[[OpSchema], StrategyType]], Callable[[OpSchema], StrategyType]]:
Copy link
Collaborator

@Skylion007 Skylion007 Nov 13, 2025

Choose a reason for hiding this comment

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

Wait, does it preserve args and return type? If so

Suggested change
def register_op_strategy(
op, schema_info=None
) -> Callable[[Callable[_P, _T]], Callable[_P, _T]]:
# pyre-fixme[2]: Parameter must be annotated.
op: Union[torch._ops.OpOverload, list[torch._ops.OpOverload]],
schema_info: Optional[RuntimeSchemaInfo] = None,
) -> Callable[[Callable[[OpSchema], StrategyType]], Callable[[OpSchema], StrategyType]]:
_OpSchemaT = TypeVar("_SchemaT", bound=OpSchema)
_StrategyTypeT = TypeVar("_StrategyT", bound=StrategyType)
def register_op_strategy(
op: Union[torch._ops.OpOverload, list[torch._ops.OpOverload]],
schema_info: Optional[RuntimeSchemaInfo] = None,
) -> Callable[[Callable[[_OpSchemaT], _StrategyTypeT]], Callable[[_OpSchemaT], _StrategyTypeT]]:

This is way the callable typing is full perserved AND type checked. The wrapper would need to be updated too of course. But I think actually the current typing in this PR might be as specific as one can be unfortunately.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh, is this because the original OpSchema passed to the callable could be a subclass of OpSchema, but the wrapping would force the typechecker to treat it specifically as a base OpSchema from the point of wrapping?

wconstab added a commit that referenced this pull request Nov 18, 2025
ghstack-source-id: 59ef401
Pull Request resolved: #167673
wconstab added a commit that referenced this pull request Nov 18, 2025
ghstack-source-id: 59ef401
Pull Request resolved: #167673
cc H-Huang awgu wanchaol fegin fduwjj wz337 d4l3k pragupta msaroufim dcci

[ghstack-poisoned]
@wconstab
Copy link
Contributor Author

@Skylion007 if you don't mind taking another look, I think I understood this now and I updated my PR accordingly. Thanks for your pointers!

@wconstab wconstab requested a review from Skylion007 November 19, 2025 04:52
# MyStrategyType(StrategyType).
_OpSchemaT = TypeVar("_OpSchemaT", bound=OpSchema)
_StrategyTypeT = TypeVar("_StrategyTypeT", bound=StrategyType)
_ShardingStrategyFuncT = Callable[[_OpSchemaT], _StrategyTypeT]
Copy link
Collaborator

@Skylion007 Skylion007 Nov 19, 2025

Choose a reason for hiding this comment

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

Slight nit: Thai is a bit of weird type and is a strict type alias because it references typevars. May want to annotate as
_ShardingStrategyFuncT: TypeAlias = to keep IDEs happy/unconfused

@pytorchmergebot
Copy link
Collaborator

Starting merge as part of PR stack under #168113

pytorchmergebot pushed a commit that referenced this pull request Nov 19, 2025
tiendatngcs pushed a commit to tiendatngcs/pytorch-Dec25 that referenced this pull request Dec 10, 2025
@github-actions github-actions bot deleted the gh/wconstab/455/head branch December 20, 2025 02:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor Merged oncall: distributed Add this issue/PR to distributed oncall triage queue topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants