Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request for torch.split to accept a tensor for input split_size_or_sections #47479

Closed
edqwerty10 opened this issue Nov 6, 2020 · 8 comments
Closed
Labels
enhancement Not as big of a feature, but technically not a bug. Should be easy to fix module: ux module: viewing and reshaping triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@edqwerty10
Copy link

🚀 Feature

For the pytorch operator torch.split(tensor, split_size_or_sections, dim) we would like the input split_size_or_sections to also handle tensors. Right now it handles list of ints and an int.

Motivation

When tracing a model, we currently do split_size_or_sections=tensor.tolist() when calling torch.split but tracing can't record this conversion of tensor to list of ints so the traced model fails on other inputs. This is currently blocking a diff that tries to improve runtime performance by using tensor operations, D24595761.

Pitch

Have torch.split(tensor, tensor, int) work so that tracing can properly record the operations for a traced model.

Alternatives

We currently don't have alternatives (I think). We are migrating to a scripted model (which works in this case) but support for tracing is still requested.

Additional context

Here is a diff D24595761 that tries to improve performance by using torch operators only but fails for a traced model

@dzhulgakov
Copy link
Collaborator

The issue is that there's some shady conversion going on here:

return super(Tensor, self).split_with_sizes(split_size, dim)
so today it seems that we convert 1-element tensor as a "number of split" not "sizes of splits".

What requested can be safely applied to split_with_sizes though because it's unambiguous.

cc @gchanan @mruberry for opinions

@glaringlee glaringlee added feature A request for a proper, new feature. triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Nov 6, 2020
@ngimel ngimel added enhancement Not as big of a feature, but technically not a bug. Should be easy to fix and removed feature A request for a proper, new feature. labels Nov 6, 2020
@mruberry
Copy link
Collaborator

mruberry commented Nov 6, 2020

We also have a NumPy compatible tensor_split that should throw an error currently when passed a tensor. We could avoid a BC concern and document this function treating a CPU tensor as a list.

This raises the more general questions:

  • do we really want CPU tensors to be interpreted as lists consistently?
  • is tracing going to continue to be around, or do we expect changes to better-support tracing are temporary?

I think it'd be OK (from a UX perspective) to consistently interpret CPU tensors as lists when an operand can take a list and as a scalar when an operand can only be a scalar. Device types like XLA will probably want the tensor to be passed as an XLA tensor, however. See #31558 and cc @ailzhang. Unfortunately I don't think we can always match these tensors to the device type that will run the operation because passing a CUDA tensor and converting it to a list would cause cross-device data movement.

For the second question, I suppose tracing will be around long enough that adding this support to a few functions would be OK. Especially since if we used tensor_split I don't think we'd have any BC concerns.

Do you think tensor_split would work for you, @edqwerty10?

@edqwerty10
Copy link
Author

Hi @mruberry, this is great thank you for the breakdown. Yes tensor_split should work for us as well!

@mruberry
Copy link
Collaborator

OK. We would accept a PR updating tensor_split to accept a CPU tensor in place of a list. Note this is consistent with NumPy:

import numpy as np
a = np.array((1, 2, 3, 4))
np.array_split(a, np.array((1, 2)))
: [array([1]), array([2]), array([3, 4])]

np.array_split(a, [1, 2])
: [array([1]), array([2]), array([3, 4])]

@mruberry
Copy link
Collaborator

The fastest way would be for you to submit a PR implementing the behavior. If that's a pain you can ping me internally and we can prioritize the request.

@xw285cornell
Copy link
Contributor

Any chance we can allow a cuda tensor for indices?

@mruberry
Copy link
Collaborator

Any chance we can allow a cuda tensor for indices?

No, because the indices tensor is used to define the tensor outputs, and the metadata for tensors lives on the CPU, so the CPU has to be able to access the data in indices.

edqwerty10 pushed a commit to edqwerty10/pytorch that referenced this issue Dec 20, 2020
…rgument (pytorch#49169)

Summary:
Pull Request resolved: pytorch#49169

Trying to solve PR request pytorch#47479.
This diff tries to overload method `torch.tensor_split` to also accept a tensor for argument `split_size_or_sections` which currently accepts a python list or int. The motivation is to avoid converting a tensor to a list so that when tracing a model/module the tensor operations can be recorded.

Implementation is following the diff that originally added the `tensor_split` method D24166164 (pytorch@ef4817f).

Test Plan:
```
buck test caffe2/test:torch -- tensor_split
```
https://www.internalfb.com/intern/testinfra/testconsole/testrun/5910974550563805/

```
buck test caffe2/test:others -- tensor_split
```
https://www.internalfb.com/intern/testinfra/testconsole/testrun/1688849905082678/

Reviewed By: mruberry

Differential Revision: D25440885

fbshipit-source-id: ca5d134cfb91fa0efc3dec5257dbc97532eb2d74
facebook-github-bot pushed a commit that referenced this issue Dec 21, 2020
…rgument (#49169)

Summary:
Pull Request resolved: #49169

Trying to solve PR request #47479.
This diff tries to overload method `torch.tensor_split` to also accept a tensor for argument `split_size_or_sections` which currently accepts a python list or int. The motivation is to avoid converting a tensor to a list so that when tracing a model/module the tensor operations can be recorded.

Implementation is following the diff that originally added the `tensor_split` method D24166164 (ef4817f).

Test Plan:
```
buck test caffe2/test:torch -- tensor_split
```
https://www.internalfb.com/intern/testinfra/testconsole/testrun/5910974550563805/

```
buck test caffe2/test:others -- tensor_split
```
https://www.internalfb.com/intern/testinfra/testconsole/testrun/1688849905082678/

Reviewed By: mruberry

Differential Revision: D25440885

fbshipit-source-id: 6705dc551279e3a5eb1e5ec1ede2728eab85ffb1
@mruberry
Copy link
Collaborator

mruberry commented Jan 3, 2021

Closing since we resolved this request by implementing the functionality in torch.tensor_split, and the request for this functionality in torch.split is a dupe of #16703.

@mruberry mruberry closed this as completed Jan 3, 2021
hwangdeyu pushed a commit to hwangdeyu/pytorch that referenced this issue Jan 6, 2021
…rgument (pytorch#49169)

Summary:
Pull Request resolved: pytorch#49169

Trying to solve PR request pytorch#47479.
This diff tries to overload method `torch.tensor_split` to also accept a tensor for argument `split_size_or_sections` which currently accepts a python list or int. The motivation is to avoid converting a tensor to a list so that when tracing a model/module the tensor operations can be recorded.

Implementation is following the diff that originally added the `tensor_split` method D24166164 (pytorch@ef4817f).

Test Plan:
```
buck test caffe2/test:torch -- tensor_split
```
https://www.internalfb.com/intern/testinfra/testconsole/testrun/5910974550563805/

```
buck test caffe2/test:others -- tensor_split
```
https://www.internalfb.com/intern/testinfra/testconsole/testrun/1688849905082678/

Reviewed By: mruberry

Differential Revision: D25440885

fbshipit-source-id: 6705dc551279e3a5eb1e5ec1ede2728eab85ffb1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Not as big of a feature, but technically not a bug. Should be easy to fix module: ux module: viewing and reshaping triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

No branches or pull requests

6 participants