Skip to content

Conversation

@driazati
Copy link
Contributor

@driazati driazati commented Sep 19, 2019

When used as annotations on Python functions, NamedTuples go through our Python annotation -> type mapping which previously had no way of lookup up NamedTuples (which are created lazily by checking if the type has certain properties, so the lookup is creating the TupleType from scratch). This PR threads through the necessary data to make them work.

Fixes #26437

Differential Revision: D17486441

@driazati driazati requested a review from apaszke as a code owner September 19, 2019 00:05
@pytorchbot pytorchbot added oncall: jit Add this issue/PR to JIT oncall triage queue module: pybind Related to our Python bindings / interactions with other Python libraries labels Sep 19, 2019
@driazati driazati changed the title wip [jit] Resolve NamedTuple types in Python Sep 19, 2019
@driazati driazati requested a review from eellison September 19, 2019 18:01
Copy link
Contributor

@eellison eellison left a comment

Choose a reason for hiding this comment

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

Cool! Thanks for doing this.

lgtm if tests pass. as a follow up we can try this with generic UDT besides NamedTuples - seems like the logic would work as well.

elif hasattr(ann, "__torch_script_class__"):
return ClassType(_qualified_name(ann))
elif lookup is not None:
# Maybe resolve a NamedTuple to a Tuple Type
Copy link
Contributor

Choose a reason for hiding this comment

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

It's okay if this is limited to NamedTuple types for now, however I think the logic extends to any custom type...



def ann_to_type(ann):
def ann_to_type(ann, lookup=None):
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe more descriptive name than lookup ? or add comment that it's an (rcb, sourcerange) tuple

@eellison
Copy link
Contributor

i think tests didn't run, rebase ?

@facebook-github-bot
Copy link
Contributor

@driazati merged this pull request in 4c40dbc.

mingbowan pushed a commit to mingbowan/pytorch that referenced this pull request Sep 23, 2019
Summary:
When used as annotations on Python functions, `NamedTuple`s go through our Python annotation -> type mapping which previously had no way of lookup up `NamedTuple`s (which are created lazily by checking if the type has certain properties, so the lookup is creating the `TupleType` from scratch). This PR threads through the necessary data to make them work.

Fixes pytorch#26437
Pull Request resolved: pytorch#26443

Pulled By: driazati

Differential Revision: D17486441

fbshipit-source-id: a6bbb543ff05a5abe61f1a7f68db9ecdb652b358
@facebook-github-bot facebook-github-bot deleted the driazati/nt2 branch July 13, 2020 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merged module: pybind Related to our Python bindings / interactions with other Python libraries oncall: jit Add this issue/PR to JIT oncall triage queue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[JIT] Named Tuple annotation doesn't work with annotations.py

6 participants