Skip to content

Added sparse support for asin and neg functions, updated log1p - #44028

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

vfdev-5 wants to merge 10 commits into
pytorch:masterfrom
Quansight:vfdev-5/sparse-torch-asin

Conversation

@vfdev-5

@vfdev-5 vfdev-5 commented Sep 2, 2020 •

Copy link
Copy Markdown
Contributor

Description:

  • added C++ code for sparse asin and neg ops similarly to log1p op
  • added tests
    • coalesced input CPU/CUDA
    • uncoalesced input CPU/CUDA
  • added tests for negative and arcsin

Backprop will be addressed in another PR.

@dr-ci

dr-ci Bot commented Sep 2, 2020 •

Copy link
Copy Markdown

💊 CI failures summary and remediations

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


💚 💚 Looks good so far! There are no failures yet. 💚 💚


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 on the GitHub issue tracker or post in the (internal) Dr. CI Users group.

See how this bot performed.

This comment has been revised 21 times.

@smessmer smessmer added module: sparse Related to torch.sparse triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Sep 2, 2020

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

LGTM, added few nits to tests.
Could you also add tests for autograd support of sparse neg and asin?

Comment thread test/test_sparse.py Resolved
Comment thread test/test_sparse.py Resolved
Comment thread test/test_sparse.py Resolved
@pearu

pearu commented Sep 4, 2020

Copy link
Copy Markdown
Collaborator

Could you also add tests for autograd support of sparse neg and asin?

Nvm, I noticed that the plan is to address autograd support in another PR.

@pytorch pytorch deleted a comment from codecov Bot Sep 5, 2020
@codecov

codecov Bot commented Sep 10, 2020 •

Copy link
Copy Markdown

Codecov Report

Merging #44028 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master   #44028   +/-   ##
=======================================
  Coverage   68.08%   68.08%           
=======================================
  Files         384      384           
  Lines       49776    49776           
=======================================
  Hits        33892    33892           
  Misses      15884    15884           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2c1b215...09d14eb. Read the comment docs.

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

LGTM, thanks @vfdev-5 !

@rgommers
rgommers requested a review from mruberry September 11, 2020 17:14
Comment thread aten/src/ATen/native/UnaryOps.cpp Resolved
Comment thread test/test_sparse.py Resolved
Comment thread test/test_sparse.py Resolved
Comment thread test/test_sparse.py Resolved
@mruberry

Copy link
Copy Markdown
Collaborator

Hey @vfdev-5! Thanks for fixing the negative alias. I just have a couple questions about dispatch and test coverage.

@vfdev-5

vfdev-5 commented Sep 15, 2020

Copy link
Copy Markdown
Contributor Author

@mruberry I updated the PR according to your review and left one thread as unresolved if we have to continue the discussion about the dispatch.

@vfdev-5
vfdev-5 requested a review from mruberry September 15, 2020 08:58
@mruberry

Copy link
Copy Markdown
Collaborator

Looking good, @vfdev-5, we can revisit dispatch later as I don't have a strong opinion on it at the moment.

Just a couple nits: cleaning up an assert, minor comment update, and you can remove an extra check. After that I think we're good to go! Nice work.

@vfdev-5 vfdev-5 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@mruberry thanks a lot for your review, explanations and suggestions ! I really appreciate them !
I'll update the code and tests according to that. By the way, if you do not mind I can also update in this PR the code and test for log1p I inspired a lot from (I also rename the PR mentioning log1p). Otherwise, I can create another PR for that, but changes are rather minor...

Comment thread test/test_sparse.py Resolved
@mruberry

Copy link
Copy Markdown
Collaborator

@mruberry thanks a lot for your review, explanations and suggestions ! I really appreciate them !
I'll update the code and tests according to that. By the way, if you do not mind I can also update in this PR the code and test for log1p I inspired a lot from (I also rename the PR mentioning log1p). Otherwise, I can create another PR for that, but changes are rather minor...

Sure. If you'd like to add log1p that's OK.

@vfdev-5 vfdev-5 changed the title Added sparse support for asin and neg functions Added sparse support for asin and neg functions, updated log1p Sep 16, 2020
@vfdev-5
vfdev-5 requested a review from mruberry September 18, 2020 08:56
return r;
}

SparseTensor& neg_sparse_(SparseTensor& t) {

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.

Writing each of these separately is OK for now but in the future we should probably make a helper function that implements unary elementwise functions (with an option for whether they are or aren't linear maps).

Comment thread test/test_sparse.py
with self.assertRaisesRegex(RuntimeError, "log1p of a sparse tensor is made to be non-differentiable"):
y.backward(x)

def test_log1p(self):

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.

We should think about generating these tests in the future using the OpInfos in common_methods_invocations.py. We could add an attribute to the OpInfos like "supports_sparse" to only selection functions intended to work on sparse tensors.

@mruberry mruberry 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.

Cool; thanks @vfdev-5!

@facebook-github-bot facebook-github-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@mruberry has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@vfdev-5

vfdev-5 commented Sep 18, 2020 •

Copy link
Copy Markdown
Contributor Author

@mruberry thanks for the review ! I agree with that we should think about how make those things more refactored. I'm OK either to rework this PR and thus define a way to handle those element-wise ops or do it in another PR (for example here : #43330) and then refactor these ops after.

@mruberry

Copy link
Copy Markdown
Collaborator

@mruberry thanks for the review ! I agree with that we should think about how make those things more refactored. I'm OK either to rework this PR and thus define a way to handle those element-wise ops or do it in another PR (for example here : #43330) and then refactor these ops after.

We should stop this PR here to merge it.

A separate PR could add test support (using OpInfos) or implementation support (see native/UnaryOps.cpp for some ideas) for ops like this, if you're planning to add more of them, that is. I'm not sure how many there are.

@vfdev-5

vfdev-5 commented Sep 21, 2020 •

Copy link
Copy Markdown
Contributor Author

@mruberry thanks for the suggestions ! Yes, the plan is to add more functions like these. Here is a list of functions that should be potentially added: https://github.com/Quansight-Labs/rfcs/blob/pearu/rfc0005/RFC0003-sparse-roadmap/SparseSupportState.md#trigonometry-functions

For a test support using OpInfos, we have N similar element-wise functions to test on various sparse data. I was analyzing your PR on OpInfo and UnaryUfuncInfo. Do you think TestUnaryUfuncs can be taken as example for what we would like to get with sparse ?

Concering implementation support, I think we can do this with a template functions like that

SparseTensor& asin_out_sparse(SparseTensor& r, const SparseTensor& t) {
  return linmap_unary_op_impl_out_sparse<&Tensor::asin_>(r, t, "asin: in-place on uncoalesced tensors is not supported");
}

using FuncSig = void (Tensor::*)();

template<FuncSig F>
void linmap_unary_op_impl_out_sparse(Tensor & r, const Tensor & t, const std::string & err_msg)
{
  TORCH_CHECK(r.is_sparse(), "Tensor should be sparse");
  TORCH_CHECK(t.is_sparse(), "Tensor should be sparse");

  if (is_same_tensor(r, t)) {
    // don't have in-place asin for uncoalesced input because coalesce() is not in-place, see above comment
    TORCH_CHECK(r.is_coalesced(), err_msg);
  } else {
    copy_sparse_to_sparse_(r, t.coalesce());
  }
  (r._values().*F)();
  return r;
}

I'll try to apply these ideas to #43330

@mruberry

Copy link
Copy Markdown
Collaborator

@vfdev-5 Yep. I think extending the current UnaryUfuncInfos with sparse properties and data would be interesting to pursue. Maybe a function like "get_sparse_samples" analogous to sample_inputs() and a property like supports_sparse_tensors would be a good start?

Template sounds like a great idea, and thanks for the RFC link. I'd like to see the Sparse tracking issue (#44634) with when we expect these features to be delivered to, so we can coordinate with PyTorch releases and better understand our sparse investment.

@vfdev-5
vfdev-5 deleted the vfdev-5/sparse-torch-asin branch September 22, 2020 09:13
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@mruberry merged this pull request in c947ab0.

@vfdev-5

vfdev-5 commented Sep 23, 2020 •

Copy link
Copy Markdown
Contributor Author

I think extending the current UnaryUfuncInfos with sparse properties and data would be interesting to pursue. Maybe a function like "get_sparse_samples" analogous to sample_inputs() and a property like supports_sparse_tensors would be a good start?

@mruberry UnaryUfuncInfos look like to be tightly related to numpy functions as required a ref to a numpy function. And I have a difficulty to estimate the side-effect if I add more functions to op_db to test the sparse support for my purposes (e.g. abs, sign).
Currently, C++ refactoring with implementing two other functions is done in this PR : #43330
A proper refactoring of python tests is to be discussed and to be done.

@mruberry

Copy link
Copy Markdown
Collaborator

I think extending the current UnaryUfuncInfos with sparse properties and data would be interesting to pursue. Maybe a function like "get_sparse_samples" analogous to sample_inputs() and a property like supports_sparse_tensors would be a good start?

@mruberry UnaryUfuncInfos look like to be tightly related to numpy functions as required a ref to a numpy function. And I have a difficulty to estimate the side-effect if I add more functions to op_db to test the sparse support for my purposes (e.g. abs, sign).
Currently, C++ refactoring with implementing two other functions is done in this PR : #43330
A proper refactoring of python tests is to be discussed and to be done.

UnaryUfuncInfos is currently tightly coupled to testing against NumPy, and this will probably change in the future. When adding annotations for sparse support, however, I was thinking you'd write tests explicitly targeting sparse. These sparse tests don't have to use the current references. They could even have their own sparse_refs, for example.

@vfdev-5

vfdev-5 commented Sep 23, 2020 •

Copy link
Copy Markdown
Contributor Author

@mruberry thanks a lot for prompt reply !

however, I was thinking you'd write tests explicitly targeting sparse.

Yes, I was thinking about something like

@ops(op_db)
def test_unary_sparse_ops(self, op):
    sparse_tensor = op.sample_sparse_input(...)
    # run and assert test

My problem is with predefined op_db list.
Maybe, I can set another list like that inside common_methods_invocations for sparse ops ?

@mruberry

mruberry commented Sep 23, 2020 •

Copy link
Copy Markdown
Collaborator

Sure, you could create sparse_unary_ufuncs like how unary_ufuncs is created. Another option would be to filter the op list when passing to the @ops decorator:

@ops(*[op for op in unary_ufuncs if op.supports_sparse)

but if you're going to do that more than once you might as well pre-filter and save the filtered list, like your suggesting.

@mruberry

Copy link
Copy Markdown
Collaborator

Example sublists:

@ops(unary_ufuncs)

@ops((_fn for _fn in unary_ufuncs if _fn.domain != (None, None)))

@vfdev-5

vfdev-5 commented Sep 23, 2020

Copy link
Copy Markdown
Contributor Author

That's true! But in addition I'm also missing there functions like abs and sign

@mruberry

Copy link
Copy Markdown
Collaborator

That's true! But in addition I'm also missing there functions like abs and sign

It should be straightforward to add those functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merged module: sparse Related to torch.sparse open source 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