Unhide unique from C++, make unique partially scriptable#15256
Unhide unique from C++, make unique partially scriptable#15256zasdfgbnm wants to merge 28 commits intopytorch:masterfrom
Conversation
| } | ||
|
|
||
| std::tuple<Tensor, Tensor> | ||
| _unique_dim_cuda(const Tensor& self, const int64_t dim, const bool sorted, const bool return_inverse) { |
There was a problem hiding this comment.
why removing this function? shouldn't cpu and cuda share the same logic?
There was a problem hiding this comment.
Because unique_dim now just calls unique, so the logic here is moved to _unique_cuda
There was a problem hiding this comment.
_unique_dim_cpu is removed as well
I see,
Thanks! I think it's good to keep it to ensure the unique is working end to end :), maybe we can rename the test to something like |
|
@wanchaol Thanks for the |
| - name: uniform_(Tensor self, double from, double to, Generator generator) | ||
| self: zeros_like(grad) | ||
|
|
||
| - name: _unique(Tensor self, bool sorted, bool return_inverse) |
There was a problem hiding this comment.
is there reasons for not implementing autograd for unique?
There was a problem hiding this comment.
What happens if we didn't put an operator to this list? Isn't not_implemented the default? I'm not sure about this.
I think the only reason for not supporting unique's autograd is, nobody need it, so nobody complain about it, so nobody go ahead and implement it...
There was a problem hiding this comment.
sure if nobody wants it then we don't need to implement it, but I think it's good to keep the correct function signature here, in your case this might be unique(Tensor self, bool sorted, bool return_inverse, int64_t? dim), and keep the not_implemented here because it will be used to give decent error msg to user that this function is not having autograd implemented yet.
torch/functional.py
Outdated
| sorted=sorted, | ||
| return_inverse=return_inverse, | ||
| ) | ||
| output, inverse_indices = torch._C._VariableFunctions.unique( |
There was a problem hiding this comment.
why we don't use the same torch.unique as in torch/tensor.py?
There was a problem hiding this comment.
I think it should be the same reason as why using torch._C._VariableFunctions.unique as in:
https://github.com/pytorch/pytorch/blob/master/torch/functional.py#L198
Which is due to:
https://github.com/pytorch/pytorch/blob/master/torch/__init__.py#L262
When import, torch.unique would be replaced by functional.unique, so using torch.unique here would cause infinite recursion here.
facebook-github-bot
left a comment
There was a problem hiding this comment.
@wanchaol has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
|
@wanchaol Just updated it, I'm not sure if it fixes the problem or not... |
|
I'm ignorant of whether we have a general policy of how to deal with Python kw args in C++, and I don't really have an opinion on the specific case either... |
|
@wanchaol is it possible to add an OSS test that covers the ONNX symbolic? |
|
@zou3519 Yes, we have oss test in test_operators to assert the onnx proto generated from ONNX symbolic. |
|
@wanchaol I've modified the symbolic and added test. How does it looks this time? |
facebook-github-bot
left a comment
There was a problem hiding this comment.
@zou3519 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
|
@zasdfgbnm @wanchaol says it looks fine. |
facebook-github-bot
left a comment
There was a problem hiding this comment.
@zou3519 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
|
It looks like there is still something not right with the @wanchaol could you please take a look? |
@ezyang Sure I am looking into it |
|
@zasdfgbnm I investigated it with our internal failures, it is basically a backward compatibility issue. There're regression tests that indicates multiple serialized models have "_unique" as the operator name, so if the PR change the name of it, the serialized models will break, and per @jhcross, we could not automatically redeploy the binaries without retraining(or somehow updating) all of those models. This should be a BC change, since pytorch and c2 don't have operator versioning for serialized models, we can only wait for either operator versioning available, or we retrained all of the serialized models. As of for now, in order to unblock this, one possible solution is that we keep both names for "_unique" and "unique" available, "_unique" just need to call "unique" in this PR. |
facebook-github-bot
left a comment
There was a problem hiding this comment.
@wanchaol has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
Summary: This PR does three things: ~~Allow `int64_t?` in function schema, which provide an elegant way of implementing null-able int arguments, as discussed in #15208 (review) ~~Originally implemented in #15235 ~~Example:~~ ```yaml - func: myop(Tensor self, int64_t? dim=None) -> Tensor variants: function ``` ~~cc: zou3519~~ Edit: implemented in #15234 Previously tried in #12064. There was a problem that C++ does not have kwarg support, which makes it confusing to know whether `unique(t, 1)` actually means `unique(t, dim=1)` or `unique(t, sorted=1)`. Now I think I have a better idea on how to implement this: there are two ATen operators: `unique` and `unique_dim`. `unique` has the same signature as in python, and exported to both python and C++. `unique_dim` has signature `unique_dim(tensor, dim, sorted=False, return_inverse=False)`, and only exported to C++, which could be used more naturally for a C++ user. Differential Revision: D13540278 Pulled By: wanchaol fbshipit-source-id: 3768c76a90b0881f565a1f890459ebccbdfe6ecd
|
@wanchaol What happened to this? I saw it merged but then reverted. Do I need to do anything? |
@zasdfgbnm this still cause some internal integration tests failed (it is not being observed until we landed it). I will do some investigation and get back to you with the details |
|
closed in favor of #17097 |
This PR does three things:
Null-able integer argument in schema
Allowint64_t?in function schema, which provide an elegant way of implementing null-able int arguments, as discussed in #15208 (review)Originally implemented in #15235Example:cc: @zou3519Edit: implemented in #15234
Unhide unique from C++
Previously tried in #12064. There was a problem that C++ does not have kwarg support, which makes it confusing to know whether
unique(t, 1)actually meansunique(t, dim=1)orunique(t, sorted=1).Now I think I have a better idea on how to implement this: there are two ATen operators:
uniqueandunique_dim.uniquehas the same signature as in python, and exported to both python and C++.unique_dimhas signatureunique_dim(tensor, dim, sorted=False, return_inverse=False), and only exported to C++, which could be used more naturally for a C++ user.cc: @ezyang @t-vi How does it look like this time?
Unique is now partially scriptable
As a result of the above two,
torch.uniquecan now be partially scripted. See test below:gives