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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

torch.cat without copying memory #70600

Closed
defoishugo opened this issue Jan 4, 2022 · 3 comments
Closed

torch.cat without copying memory #70600

defoishugo opened this issue Jan 4, 2022 · 3 comments
Labels
module: numpy Related to numpy support, and also numpy compatibility of our operators module: viewing and reshaping triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@defoishugo
Copy link

defoishugo commented Jan 4, 2022

馃殌 The feature, motivation and pitch

Principle

Today, the concatenation implemented on pytorch consists in the allocation of a new tensor.
I would like to know if it is possible to realize a concatenation of contiguous and/or non-contiguous tensors without memory duplication.

Example 1 : contiguous concatenation

The following code is executed with allocation of a new tensor concatenated_tensor:

import torch 

tensor1 = torch.rand(2,3,4)
tensor2 = torch.rand(2,3,4)
concatenated_tensor = torch.cat(tensor1, tensor2)

I'd like to enable the same scenario, but have concatenated_tensor as a view of tensor1 and tensor2.
In terms of UX, I don't know what to propose.

Note: since I'm a new pytorch user, maybe the word "view" is not appropriate. The low-level idea is to consider concatenated_tensor as a list of pointers to tensors.

Example 2 : non-contiguous concatenation

Next, I would like to enable the following scenario, if possible:

import torch 

tensor1 = torch.rand(2,3,4).expand(10, -1, -1)
tensor2 = torch.rand(2,3,4).expand(10, -1, -1)
concatenated_tensor = torch.cat(tensor1, tensor2)

Alternatives

No response

Additional context

Discussed in #70283 with @ejguan.
See discussion/34609.

cc @mruberry @rgommers

@ejguan ejguan added module: numpy Related to numpy support, and also numpy compatibility of our operators module: viewing and reshaping triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Jan 4, 2022
@rgommers
Copy link
Collaborator

rgommers commented Jan 4, 2022

I would like to know if it is possible to realize a concatenation of contiguous and/or non-contiguous tensors without memory duplication.

The answers of @albanD on https://discuss.pytorch.org/t/concatenate-tensors-without-memory-copying/34609/13 explain how difficult this is. It does not fit the strided model of a tensor at all. It'd be a ton of work - I think it's safe to say this won't happen.

I'll let someone else decide, but I propose to close this issue.

@defoishugo
Copy link
Author

No problem, we'll close this issue.

@ngimel
Copy link
Collaborator

ngimel commented Jan 10, 2022

nestedtensor https://github.com/pytorch/nestedtensor might be doing what you want. Also, if you don't need autograd, _foreach_* ops support operations on the lists of tensors, so you don't need to concatenate them in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: numpy Related to numpy support, and also numpy compatibility of our operators 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

4 participants