Skip to content

Support named tuple return from operators on JIT#16253

Closed
zasdfgbnm wants to merge 22 commits intopytorch:masterfrom
zasdfgbnm:namedtuple-return-jit
Closed

Support named tuple return from operators on JIT#16253
zasdfgbnm wants to merge 22 commits intopytorch:masterfrom
zasdfgbnm:namedtuple-return-jit

Conversation

@zasdfgbnm
Copy link
Collaborator

@zasdfgbnm zasdfgbnm commented Jan 23, 2019

Fixes: #16233

The following changes are made:

  • Modify TupleType to store optional field names
  • Modify schema matching to return fill in those field names when creating TupleType as return type.
  • Modify codegen of JIT to copy field names to schema string
  • Modify SchemaParser to set field names of returned schema.
  • Modify SimpleValue::attr to emit tuple indexing for named tuple.

@zasdfgbnm zasdfgbnm changed the title [WIP] Support named tuple return from operators [WIP] Support named tuple return from operators on JIT Jan 23, 2019
Copy link
Contributor

@zdevito zdevito left a comment

Choose a reason for hiding this comment

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

This looks pretty good, thanks! I have a few places where we can make things simpler. I also reached out to @ezyang about what the subtyping relationship for named tuples should be.

@zasdfgbnm zasdfgbnm changed the title [WIP] Support named tuple return from operators on JIT Support named tuple return from operators on JIT Jan 23, 2019
@zasdfgbnm
Copy link
Collaborator Author

@zdevito @ezyang @driazati I've made the improvement according to review.

@sidazhang
Copy link

Could you also include an example of how to use this?

@zasdfgbnm
Copy link
Collaborator Author

zasdfgbnm commented Jan 24, 2019

@sidazhang Do you mean just this JIT support or the returned named tuple in general? Does test_torch.py#L6913 help?

@ezyang Maybe we need to change the docs of, for example max, from

>>> a = torch.randn(4, 4)
>>> a
tensor([[-1.2360, -0.2942, -0.1222,  0.8475],
        [ 1.1949, -1.1127, -2.2379, -0.6702],
        [ 1.5717, -0.9207,  0.1297, -1.8768],
        [-0.6172,  1.0036, -0.6060, -0.2432]])
>>> torch.max(a, 1)
torch.return_types.max(values=tensor([0.8475, 1.1949, 1.5717, 1.0036]), indices=tensor([3, 0, 0, 1]))

to something like

>>> a = torch.randn(4, 4)
>>> a
tensor([[-1.2360, -0.2942, -0.1222,  0.8475],
        [ 1.1949, -1.1127, -2.2379, -0.6702],
        [ 1.5717, -0.9207,  0.1297, -1.8768],
        [-0.6172,  1.0036, -0.6060, -0.2432]])
>>> torch.max(a, 1)
torch.return_types.max(values=tensor([0.8475, 1.1949, 1.5717, 1.0036]), indices=tensor([3, 0, 0, 1]))
>>> torch.max(a, 1).values
tensor([0.8475, 1.1949, 1.5717, 1.0036]
>>> torch.max(a, 1).indices
tensor([3, 0, 0, 1])
>>> values, indices = torch.max(a, 1)

?? What do you think? Namedtuple is a rather advanced topic of Python, and I believe that there are lots of deep learning users do not know what is a namedtuple, how to use it, and how it prints.

@sidazhang
Copy link

@sidazhang Do you mean just this JIT support or the returned named tuple in general? Does test_torch.py#L6913 help?

The test unfortunately is not super helpful in showing how I can use namedtuples in my own code. Like how to write a function that returns a namedtuple.

@zasdfgbnm
Copy link
Collaborator Author

@sidazhang Do you mean, you want to create your own function that returns namedtuple, and use it on JIT? This is not the purpose of this PR.

The title of this PR might unfortunately not explain things well, but what is happening is, we are changing the return type of some builtin operators from tuple to namedtuple. For example, in the past, we have to write torch.max(a, dim=1)[1] but now users could also write torch.max(a, dim=1).indices, which is more readable. This PR add support of this new way of returning to JIT. But this support is currently only limited to builtin operators, not operators defined by users.

@sidazhang
Copy link

@sidazhang Do you mean, you want to create your own function that returns namedtuple, and use it on JIT? This is not the purpose of this PR.

The title of this PR might unfortunately not explain things well, but what is happening is, we are changing the return type of some builtin operators from tuple to namedtuple. For example, in the past, we have to write torch.max(a, dim=1)[1] but now users could also write torch.max(a, dim=1).indices, which is more readable. This PR add support of this new way of returning to JIT. But this support is currently only limited to builtin operators, not operators defined by users.

Got it!

namedtuple would be nice to have for user defined functions. but probably just a nice to have. Not a high priority feature for me :)

@ezyang
Copy link
Contributor

ezyang commented Jan 25, 2019

Since custom user functions are done using pybind11, I suppose you could roll it by hand. It wouldn't be as nice as having it done automatically, but it seems feasible.

Copy link
Contributor

@zdevito zdevito left a comment

Choose a reason for hiding this comment

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

Looks good. Small nits, and then it is ready to land.

@zasdfgbnm
Copy link
Collaborator Author

@zdevito I actually did a bit more than you suggest:

  • isSubtypeOf also had the same issue of casting multiple times, it's fixed now.
  • the compare_names was actually an reimplementation of optional equality, so it is also removed now

zasdfgbnm added a commit to zasdfgbnm/pytorch that referenced this pull request Jan 29, 2019
Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@zdevito has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@zdevito has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

zdevito pushed a commit to zdevito/ATen that referenced this pull request Feb 11, 2019
Summary:
Fixes: pytorch/pytorch#16233

The following changes are made:
- Modify `TupleType` to store optional field names
- Modify schema matching to return fill in those field names when creating  `TupleType` as return type.
- Modify codegen of JIT to copy field names to schema string
- Modify `SchemaParser` to set field names of returned schema.
- Modify `SimpleValue::attr` to emit tuple indexing for named tuple.
Pull Request resolved: pytorch/pytorch#16253

Reviewed By: ezyang

Differential Revision: D13954298

Pulled By: zdevito

fbshipit-source-id: 247d483d78a0c9c12d1ba36e1f1ec6c3f1a3007b
@zasdfgbnm zasdfgbnm deleted the namedtuple-return-jit branch February 11, 2019 04:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[jit] Support named tuple return from operators

6 participants