Skip to content

Unhide at::unique from C++#12064

Closed
zasdfgbnm wants to merge 3 commits intopytorch:masterfrom
zasdfgbnm:improve_unique
Closed

Unhide at::unique from C++#12064
zasdfgbnm wants to merge 3 commits intopytorch:masterfrom
zasdfgbnm:improve_unique

Conversation

@zasdfgbnm
Copy link
Collaborator

@zasdfgbnm zasdfgbnm commented Sep 25, 2018

Currently we only have at::_unique and at::_unique_dim. It would be convenient to have a overloaded at::unique that handle both cases.

@ezyang
Copy link
Contributor

ezyang commented Sep 26, 2018

I'm ambivalent about this. In Python land, this positional call is invalid:

>>> torch.unique(torch.zeros(2,2), 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/data/users/ezyang/pytorch-tmp/torch/functional.py", line 486, in unique
    return_inverse=return_inverse,
TypeError: _unique(): argument 'sorted' must be bool, not int

We don't have keyword arguments in C++, so it's hard to do this in a clear way. The existing positional booleans already make my skin crawl.

@ezyang
Copy link
Contributor

ezyang commented Sep 26, 2018

cc @gchanan

@zasdfgbnm
Copy link
Collaborator Author

@ezyang Overloading it under the same name unique may or may not be a good idea, as you figure out. But at least we need to delete the _ in front of its name to make it a public C++ API.

@t-vi
Copy link
Collaborator

t-vi commented Sep 26, 2018

I think part of the problem is that Python's torch.unique has a different argument ordering than _unique_dim, in the first, the dimension comes last.
Having deviations in the argument order might be confusing to users (like in @ezyang 's example above).

@zou3519
Copy link
Contributor

zou3519 commented Nov 13, 2018

Edit: sorry, comment on wrong pr

@zasdfgbnm
Copy link
Collaborator Author

So, what is the best way to provide a C++ API of unique?

@zasdfgbnm
Copy link
Collaborator Author

PS: I would suggest resolving this before release of 1.0, otherwise the user would start using _unique and future update might break user code.

@zasdfgbnm zasdfgbnm mentioned this pull request Dec 14, 2018
@zasdfgbnm
Copy link
Collaborator Author

Closing this in favor of #15256

@zasdfgbnm zasdfgbnm closed this Dec 15, 2018
facebook-github-bot pushed a commit that referenced this pull request Jan 21, 2019
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
zdevito pushed a commit to zdevito/ATen that referenced this pull request Jan 21, 2019
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 pytorch/pytorch#15208 (review)

~~Originally implemented in pytorch/pytorch#15235

~~Example:~~

```yaml
- func: myop(Tensor self, int64_t? dim=None) -> Tensor
  variants: function
```

~~cc: zou3519~~

Edit: implemented in pytorch/pytorch#15234

Previously tried in pytorch/pytorch#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
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.

4 participants