Skip to content

Added sparse support for abs and sign functions - #43330

Closed
vfdev-5 wants to merge 12 commits into
pytorch:masterfrom
Quansight:vfdev-5/sparse-torch-abs
Closed

vfdev-5 wants to merge 12 commits into
pytorch:masterfrom
Quansight:vfdev-5/sparse-torch-abs

Conversation

@vfdev-5

@vfdev-5 vfdev-5 commented Aug 20, 2020 •

Copy link
Copy Markdown
Contributor

Description:


OLD

  • added C++ code for sparse abs and sign ops similarly to log1p op
  • added tests
    • coalesced input CPU/CUDA
    • uncoalesced input CPU/CUDA
    • backprop for abs.
    • TODO: backprop for sign -> to fix in another PR
  • added tests for absolute

Blocked by #43699

@dr-ci

dr-ci Bot commented Aug 20, 2020 •

Copy link
Copy Markdown

🔗 Helpful links

💊 CI failures summary and remediations

As of commit bb5e742 (more details on the Dr. CI page):


Commit bb5e742 was recently pushed. Waiting for builds...


This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions to the (internal) Dr. CI Users group.

Click here to manually regenerate this comment.

@vfdev-5
vfdev-5 force-pushed the vfdev-5/sparse-torch-abs branch from 0bc74ee to 87f7c4b Compare August 20, 2020 16:09

@pearu pearu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

I added some suggestions on what to test: hybrid tensors, negative entries in uncoalesced tensors.

Also, I think inplace abs on uncoalesed tensors should never be implemented, hence, drop the "yet!" promise.

Comment thread test/test_sparse.py Resolved
Comment thread test/test_sparse.py Resolved
Comment thread test/test_sparse.py Resolved
@vfdev-5
vfdev-5 force-pushed the vfdev-5/sparse-torch-abs branch from 87f7c4b to 8f47993 Compare August 26, 2020 12:14

@pearu pearu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

I have some nit-comments regarding tests but I think you can drop the Draft status.

Comment thread test/test_sparse.py Resolved
Comment thread test/test_sparse.py Resolved
Comment thread test/test_sparse.py Resolved
Comment thread test/test_sparse.py Resolved
Comment thread test/test_sparse.py Resolved
@vfdev-5
vfdev-5 marked this pull request as ready for review August 27, 2020 13:25
@vfdev-5 vfdev-5 changed the title [WIP] Added sparse support for abs and sign functions Added sparse support for abs and sign functions Aug 28, 2020
@ailzhang
ailzhang requested a review from pearu August 31, 2020 22:35
@ailzhang ailzhang added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Aug 31, 2020
@vfdev-5 vfdev-5 added the module: sparse Related to torch.sparse label Sep 7, 2020
@vfdev-5
vfdev-5 marked this pull request as draft September 11, 2020 11:25
@rgommers

Copy link
Copy Markdown
Collaborator

I think this PR is a good example of why test infrastructure improvements can help. Adding sparse support to two operators here took:

  • a few entries in native_functions.yaml
  • <60 lines of C++
  • 150 lines of test code

There is quite a bit of generic test machinery in test/test_sparse.py it looks like, but none of it is used in this PR. I'd think it should be possible to make all the dtypes, devices, tensors to test (with different shapes, sparsity patterns, uncoalesced/coalesced, hybrid tensors with dense parts, and maybe special values like nan/inf) and tests to compare sparse to dense with and without autograd generic. And that then adding tests for a new operator like abs should be only a few lines of code.

Note, no action needed in this PR, but we should follow up on this soon so next PRs that add sparse support to new operators are easier/smaller.

@vfdev-5
vfdev-5 marked this pull request as ready for review September 23, 2020 11:52

@pearu pearu left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vfdev-5, I like the nonlinmap_unary_op_impl_out_sparse approach, thanks!
In the testing site, I added suggestions for code reduction. In fact, the code reduction could be increased even more if the suggested for-loops could be incorporated into the _test_unary_op method.

Comment thread test/test_sparse.py

if ops_inplace is not None:
for op in ops_inplace:
self.assertEqual(expected_output, op(sparse_tensor.clone().coalesce()).to_dense())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not test in-place operation. Use:

sparse_tensor_copy = sparse_tensor.clone().coalesce()
op(sparse_tensor_copy)
self.assertEqual(expected_output, sparse_tensor_copy.to_dense())

Comment thread test/test_sparse.py
sparse_tensor,
ops=(torch.log1p, torch.Tensor.log1p),
ops_out=(torch.log1p, ),
ops_inplace=(torch.Tensor.log1p_, )

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also torch.log1p_. Could you add it to ops_inplace?

Comment thread test/test_sparse.py
torch.negative, torch.Tensor.negative, torch.Tensor.negative_,
operator.neg
),
ops_out=(torch.neg, torch.negative),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why

ops_inplace = (torch.neg_, torch.negative_, torch.Tensor.neg_, torch.Tensor.negative_)

is not used here?

Comment thread test/test_sparse.py
def _test_log1p_tensor(self, sparse_tensor):
self._test_unary_op(
sparse_tensor,
ops=(torch.log1p, torch.Tensor.log1p),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include torch.log1p_, torch.Tensor.log1p_ to ops.

Comment thread test/test_sparse.py
@@ -1846,29 +1856,14 @@ def test_neg_negative(self):
self._test_neg_negative(input_uncoalesced)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code block could be reduced to:

for indices, values, size in [
    ([[0, 1, 2]], [3.0, -4.0, 5.0], [3,]),
    ([[1, 3], [2, 4]], [[-1.0, 3.0], [-5.0, 7.0]], [4, 5, 2]),
    ([[0, 1, 2, 0, 1, 2]], 2.0, -3.0, -4.0, 1.0, -1.0, 1.5], [3, ]),
    (torch.zeros([2, 0]), torch.zeros([0, 5, 5, 5, 5, 5, 5, 0]), [0, 0, 5, 5, 5, 5, 5, 5, 0])
]:
    input = torch.sparse_coo_tensor(indices, values, size, device=self.device)
    if not self.is_uncoalesced:
        input = input.coalesce()
    self._test_neg_negative(input)

Comment thread test/test_sparse.py
Comment on lines +1963 to +1997
if not self.is_uncoalesced:
input_coalesced = torch.sparse_coo_tensor(
indices=torch.tensor([[0, 1, 2, 3]]),
values=torch.tensor([3.0, -4.0, 5.0, -6.0]),
size=[4, ],
device=self.device
).coalesce()
self._test_sign(input_coalesced)

# hybrid sparse input
input_coalesced = torch.sparse_coo_tensor(
indices=torch.tensor([[1, 3], [2, 4]]),
values=torch.tensor([[-1.0, 3.0], [-5.0, 7.0]]),
size=[4, 5, 2],
device=self.device
).coalesce()
self._test_sign(input_coalesced)

if self.is_uncoalesced:
# test uncoalesced input
input_uncoalesced = torch.sparse_coo_tensor(
indices=torch.tensor([[0], [1], [2], [0], [1], [2]]).transpose(1, 0),
values=torch.tensor([2.0, -3.0, 4.0, 1.0, -1.0, 1.0]),
size=[3, ],
device=self.device
)
self._test_sign(input_uncoalesced)

input_uncoalesced = torch.sparse_coo_tensor(
indices=torch.zeros([2, 0]),
values=torch.zeros([0, 5, 5, 5, 5, 5, 5, 0]),
size=[0, 0, 5, 5, 5, 5, 5, 5, 0],
device=self.device
)
self._test_sign(input_uncoalesced)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reduce this code block to

for indices, values, size in [
    ([[0, 1, 2]], [3.0, -4.0, 5.0], [3,]),
    ([[1, 3], [2, 4]], [[-1.0, 3.0], [-5.0, 7.0]], [4, 5, 2]),
    ([[0, 1, 2, 0, 1, 2]], 2.0, -3.0, -4.0, 1.0, -1.0, 1.5], [3, ]),
    (torch.zeros([2, 0]), torch.zeros([0, 5, 5, 5, 5, 5, 5, 0]), [0, 0, 5, 5, 5, 5, 5, 5, 0])
]:
    input = torch.sparse_coo_tensor(indices, values, size, device=self.device)
    if not self.is_uncoalesced:
        input = input.coalesce()
    self._test_sign(input)

Comment thread test/test_sparse.py
def _test_sign(self, sparse_tensor):
self._test_unary_op(
sparse_tensor,
ops=(torch.sign, torch.Tensor.sign),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OT: Is there a reason why torch.sign_ is missing?

@@ -5935,10 +5939,14 @@
- func: sign_(Tensor(a!) self) -> Tensor(a!)
use_c10_dispatcher: full
variants: method

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps OT: is there a reason why torch.sign_ does not exist? Use variants: function, method to make torch.sign_ a thing.

Comment on lines 103 to 104
TORCH_CHECK(r.is_sparse(), "Tensor should be sparse");
TORCH_CHECK(t.is_sparse(), "Tensor should be sparse");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the user trigger these errors with their input? If not, use TORCH_INTERNAL_ASSERT instead of TORCH_CHECK.

Comment on lines +119 to +120
TORCH_CHECK(r.is_sparse(), "Tensor should be sparse");
TORCH_CHECK(t.is_sparse(), "Tensor should be sparse");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use TORCH_INTERNAL_ASSERT?

@pytorchbot

Copy link
Copy Markdown
Collaborator

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
Stale pull requests will automatically be closed 30 days after being marked Stale

@vfdev-5

vfdev-5 commented Apr 12, 2022 •

Copy link
Copy Markdown
Contributor Author

Let's close this PR as stale since this is unlikely to move it forward.

@vfdev-5 vfdev-5 closed this Apr 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed module: sparse Related to torch.sparse open source Stale triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants