-
Notifications
You must be signed in to change notification settings - Fork 26.3k
implement concatenation of sparse tensors #13577
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
Conversation
807ba97 to
54ddd12
Compare
aten/src/ATen/native/TensorShape.cpp
Outdated
| std::vector<Tensor> values; | ||
| int64_t sparse_dim = tensors[0].sparse_dim(); | ||
| int64_t dense_dim = tensors[0].dense_dim(); | ||
| AT_CHECK(dim < sparse_dim, |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| auto const &t = tensors[i]; | ||
| AT_CHECK(t.is_sparse(), | ||
| "Can't cat dense tensor at position ", i, " with sparse tensor(s)."); | ||
| AT_CHECK(t.sparse_dim() == sparse_dim && t.dense_dim() == dense_dim, |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| return false; | ||
| } | ||
| for (int64_t i = 0; i < s1.size(); ++i) { | ||
| if (i != dim_except && s1[i] != s2[i]) { |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
aten/src/ATen/native/TensorShape.cpp
Outdated
| col += this_piece_size; | ||
| } | ||
| auto sizes_copy = sizes.vec(); | ||
| sizes_copy[dim] = cumulative_offset; |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
|
@gchanan thanks for the review. I have updated the PR. |
gchanan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm, just fix up a few nits please.
aten/src/ATen/native/TensorShape.cpp
Outdated
| // E.g.: catting [[1,2],[0,0]] and [[0,0],[3,4]] | ||
| // yields [[1,2,0,0],[0,0,3,4]] | ||
| AT_CHECK(wrapped < sparse_dim, | ||
| "Can't cat or stack tensors of sparse dim ", sparse_dim, "along non-sparse dimension ", dim); |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
aten/src/ATen/native/TensorShape.cpp
Outdated
| " along dimension ", dim); | ||
| AT_CHECK(t.sparse_dim() == sparse_dim && t.dense_dim() == dense_dim, | ||
| "Tensor at position ", i, " has dimension: sparse ", t.sparse_dim(), ", dense ", t.dense_dim(), | ||
| ". Can't cat with tensor of dimensions ", sparse_dim, ", ", dense_dim); |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
test/test_sparse.py
Outdated
| test_shapes( | ||
| [(3, 10, [2, 3, 4]), (3, 10, [2, 1, 4]), (3, 10, [2, 4, 4])], 1) | ||
| test_shapes([(3, 10, [2, 3, 4]), (3, 10, [2, 1, 4])], 0, | ||
| "can't be concatenated.*along dimension 0") |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
facebook-github-bot
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@umanwizard has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
Summary: With this change applied, `torch.cat` works for sparse tensors. The algorithm is just to concatenate the values, and give the new values the proper indices (which will be the same as their old indices in every dimension except the catted dimension, and their old indices plus the sum of the size of every previous tensor in the catted dimension). This is my first time contributing to PyTorch so please feel free to tell me if this approach seems totally wrong. Coming next: `torch.stack` for sparse tensors. Pull Request resolved: pytorch/pytorch#13577 Differential Revision: D12980948 Pulled By: umanwizard fbshipit-source-id: 51ebdafee7fcd56d9762dcae9ebe5b4ab8e1dd6b
… dimensions (#13761) Summary: Follow-up to #13577 The idea is to take each values tensor, concatenate it with zeros before and after itself (along the dimension corresponding to the one we're catting the tensors along), to get a tensor corresponding to the values for that tensor in the result. Then we concatenate all of those together to get the final values tensor. (Hopefully, this will be more clear from the example in the comments). The indices are more straightforward: since we aren't concatenating along a sparse dimension, they don't change at all, so all we need to do are concatenate the indices from the different tensors together. Pull Request resolved: #13761 Differential Revision: D13160343 Pulled By: umanwizard fbshipit-source-id: 13d7adecd369e0eebdf5bce3d90a51029b66bd1d
With this change applied,
torch.catworks for sparse tensors.The algorithm is just to concatenate the values, and give the new values the proper indices (which will be the same as their old indices in every dimension except the catted dimension, and their old indices plus the sum of the size of every previous tensor in the catted dimension).
This is my first time contributing to PyTorch so please feel free to tell me if this approach seems totally wrong.
Coming next:
torch.stackfor sparse tensors.