Support named tuple return from operators on JIT#16253
Support named tuple return from operators on JIT#16253zasdfgbnm wants to merge 22 commits intopytorch:masterfrom
Conversation
|
Could you also include an example of how to use this? |
|
@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 >>> 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. |
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. |
|
@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 |
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 :) |
|
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. |
zdevito
left a comment
There was a problem hiding this comment.
Looks good. Small nits, and then it is ready to land.
…dtuple-return-jit
|
@zdevito I actually did a bit more than you suggest:
|
facebook-github-bot
left a comment
There was a problem hiding this comment.
@zdevito has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
facebook-github-bot
left a comment
There was a problem hiding this comment.
@zdevito has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
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
Fixes: #16233
The following changes are made:
TupleTypeto store optional field namesTupleTypeas return type.SchemaParserto set field names of returned schema.SimpleValue::attrto emit tuple indexing for named tuple.