Skip to content

Conversation

@umanwizard
Copy link
Contributor

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.

@umanwizard umanwizard force-pushed the sparse_cat_and_stack branch from 807ba97 to 54ddd12 Compare November 5, 2018 19:05
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.

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.

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.

col += this_piece_size;
}
auto sizes_copy = sizes.vec();
sizes_copy[dim] = cumulative_offset;

This comment was marked as off-topic.

@umanwizard
Copy link
Contributor Author

@gchanan thanks for the review. I have updated the PR.

Copy link
Contributor

@gchanan gchanan left a 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.

// 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.

" 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.

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.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a 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.

zdevito pushed a commit to zdevito/ATen that referenced this pull request Nov 8, 2018
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
@umanwizard umanwizard deleted the sparse_cat_and_stack branch November 9, 2018 02:49
facebook-github-bot pushed a commit that referenced this pull request Nov 26, 2018
… 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
@ezyang ezyang added the merged label Jun 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants