-
Notifications
You must be signed in to change notification settings - Fork 21.6k
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
dense.to_sparse() re: #8853 #12171
dense.to_sparse() re: #8853 #12171
Conversation
torch/_tensor_docs.py
Outdated
to_sparse() -> Tensor | ||
|
||
Returns a sparse copy of the tensor. Torch supports sparse tensors | ||
in coordinate format. See https://pytorch.org/docs/stable/sparse.html. |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
@@ -2384,6 +2384,14 @@ def callable(a, b) -> number | |||
0.012766935862600803 | |||
""") | |||
|
|||
add_docstr_all('to_sparse', |
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.
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.
|
||
LongTensor indices = self.nonzero().transpose(0, 1); | ||
std::vector<int64_t> maskShape; | ||
if(sparseDims != dims){ |
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.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
facfac6
to
4b8027e
Compare
CIs failed look related |
0823819
to
840624c
Compare
@weiyangfb ok ready for review again |
test/test_sparse.py
Outdated
expected, _, _ = self._gen_sparse(sparse_dims, nnz, shape) | ||
d = expected.to_dense() | ||
result = d.to_sparse(sparse_dims) | ||
self.assertEqual(expected.to_dense(), result.to_dense()) # == not implemented for sparse tensors yet |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
test/test_sparse.py
Outdated
shape = [10, 5, 19, 8] | ||
for dim, dim_sz in enumerate(shape): | ||
sparse_dims = dim + 1 | ||
for nnz in range(1, dim_sz): |
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.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
test/test_sparse.py
Outdated
d = expected.to_dense() | ||
result = d.to_sparse(sparse_dims) | ||
self.assertEqual(expected.to_dense(), result.to_dense()) # == not implemented for sparse tensors yet | ||
self.assertEqual(sparse_dims, result._sparseDims()) |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
self.assertEqual(sparse_dims, result._sparseDims()) | ||
|
||
sp, _, _ = self._gen_sparse(2, 10, [3, 3, 3]) | ||
self.assertRaises(RuntimeError, lambda: sp.to_sparse()) |
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.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
}else{ | ||
Tensor i = nz.narrow(0, 0, sparseDims); | ||
std::tie(indices, std::ignore) = _unique_dim(i, 1); | ||
indices = indices.clone(); |
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.
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.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/_tensor_docs.py
Outdated
|
||
Returns a sparse copy of the tensor. PyTorch supports sparse tensors in | ||
:ref:`coordinate format <sparse-docs>`. | ||
""") |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
f30d038
to
e7a624a
Compare
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, there are some minor nits
@weiyangfb sounds good, done & done! |
6ff7c27
to
dbff197
Compare
build failures were due to #12984. Retriggered the build via rebase |
@realdoug please rebase to fix the CI failures |
dbff197
to
5ac4ecb
Compare
@@ -2238,6 +2238,18 @@ | |||
variants: function, method | |||
device_guard: False | |||
|
|||
- func: to_sparse(Tensor self, int64_t sparseDims) -> Tensor |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
3cc749f
to
cd62fcd
Compare
int64_t dims = self.dim(); | ||
AT_CHECK(sparse_dim > 0, "sparse_dim must be >0"); | ||
AT_CHECK(sparse_dim <= dims, | ||
"sparseDims must be less than or equal to self.dim()"); |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
|
||
Tensor nz = self.nonzero().transpose(0, 1); | ||
if(nz.numel() == 0){ | ||
return new_with_dims_sparse(sparse_dim, dims-sparse_dim, sizes, sparse_options); |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/_tensor_docs.py
Outdated
>>> d.to_sparse() | ||
tensor(indices=tensor([[1, 1], | ||
[0, 2]]), | ||
values=tensor([ 9, 10]), |
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.
torch/_tensor_docs.py
Outdated
size=(3, 3), nnz=2, layout=torch.sparse_coo) | ||
>>> d.to_sparse(1) | ||
tensor(indices=tensor([[1]]), | ||
values=tensor([[ 9, 0, 10]]), |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
LongTensor indices; | ||
if(sparse_dim == dims){ | ||
indices = nz.clone(); | ||
}else{ |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
std::vector<int64_t> sizes = self.sizes().vec(); | ||
|
||
Tensor nz = self.nonzero().transpose(0, 1); | ||
if(nz.numel() == 0){ |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
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.
weiyangfb has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
need a rebase :( |
d352d8c
to
4041e34
Compare
4041e34
to
65375b1
Compare
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.
weiyangfb has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
Summary: Here is my stab at ```dense.to_sparse``` Pull Request resolved: pytorch/pytorch#12171 Differential Revision: D10859078 Pulled By: weiyangfb fbshipit-source-id: 5df72f72ba4f8f10e283402ff7731fd535682664
landed, Thanks @realdoug ! |
Here is my stab at
dense.to_sparse