Skip to content

Add optional coalesce argument to values(), indices(), and to a new method nse() - #45695

Closed
pearu wants to merge 16 commits into
pytorch:masterfrom
Quansight:pearu/44635-2
Closed

pearu wants to merge 16 commits into
pytorch:masterfrom
Quansight:pearu/44635-2

Conversation

@pearu

@pearu pearu commented Oct 1, 2020 •

Copy link
Copy Markdown
Collaborator

This PR is a follow-up to the discussion in #45400 (comment):

  • indices(coalesce=True) - return the indices of coalesced tensor, indices(False) is identical to _indices().
  • values(coalesce=True) - return the values of coalesced tensor, values(False) is identical to _values().
  • nse(coalesce=True) - return the number of specified elements of a coalesced tensor. nse(False) is identical to _nnz().
  • document coalesce argument in indices, values, nse docstrings.
  • Replace _indices() with indices(False) everywhere in Python code.
  • Replace _values() with values(False) everywhere in Python code.
  • Replace _nnz() with nse(False) everywhere in Python code.
  • Deprecate the usage of _indices, _values, _nse: trigger a deprecation warning when users call these from Python.
  • Create an issue for removing the deprecate method _nnz starting from PyTorch version 1.9:
  • Update torch.sparse documentation page: remove all occurrences of _indices, _values, _nnz; explain coalesce=False in uncoalesce COO tensor section; add a note that coalesce is not a cheap operation and suggest coalesce before processing the outputs of indices/values/nse.
  • Rename nnz to nse everywhere in Python/C++ code, update comments

At the C++ level, we'll continue using the underscore prefixed functions as unsafe API functions.

Summary

The public Python API contains the following methods:

  • torch.Tensor.nse(coalesce=True)
  • torch.Tensor.indices(coalesce=True)
  • torch.Tensor.values(coalesce=True)
    that represent the COO storage format interface of PyTorch tensors with COO layout.

The usage of unsafe Python API methods, torch.Tensor._nse(), torch.Tensor._indices(), torch.Tensor._values(), is deprecated. However, the methods remain to exist in the Python API.

The public C++ API contains the following methods:

  • Tensor.nse(coalesce=true)
  • Tensor.indices(coalesce=true)
  • Tensor.values(coalesce=true)

The unsafe C++ API contains the following methods:

  • Tensor._nse() that is equivalent to Tensor.nse(false)
  • Tensor._indices() that is equivalent to Tensor.indices(false)
  • Tensor._values() that is equivalent to Tensor.values(false)

As a rule, C++ algorithms on tensors with COO layout use the unsafe C++ API.

The private Python/C++ API contains torch.Tensor._values_coalesce() (or Tensor._values_coalesce() in C++) method that usage is deprecated both in Python and C++ (except in calling it from Tensor.values(coalesce) implementation). The method was introduced to ensure that expression values(True) is differentiable while values(False) is not.

Out of all the above methods, only the expression torch.Tensor.values(True) (or Tensor.values(true) in C++) is differentiable.

The usage of torch.Tensor._nnz() (or Tensor._nnz() in C++) is deprecated and these methods will be removed in the future PyTorch release.

@pearu
pearu marked this pull request as draft October 1, 2020 21:21
@pearu pearu changed the title Add optional coalesce argument to values(), indices(), and to new method nse() Add optional coalesce argument to values(), indices(), and to a new method nse() Oct 1, 2020
@pearu pearu self-assigned this Oct 1, 2020
@pearu pearu added the module: sparse Related to torch.sparse label Oct 1, 2020
@pearu pearu mentioned this pull request Oct 22, 2020
8 of 26 tasks
@facebook-github-bot

Copy link
Copy Markdown
Contributor

Hi @pearu!

Thank you for your pull request. We require contributors to sign our Contributor License Agreement, and yours needs attention.

You currently have a record in our system, but we do not have a signature on file.

In order for us to review and merge your code, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@codecov

codecov Bot commented Nov 2, 2020 •

Copy link
Copy Markdown

Codecov Report

Merging #45695 into master will decrease coverage by 0.00%.
The diff coverage is 90.61%.

@@            Coverage Diff             @@
##           master   #45695      +/-   ##
==========================================
- Coverage   60.81%   60.80%   -0.01%     
==========================================
  Files        2748     2748              
  Lines      254039   254129      +90     
==========================================
+ Hits       154497   154531      +34     
- Misses      99542    99598      +56     

@pearu
pearu marked this pull request as ready for review November 2, 2020 19:09
@mruberry mruberry added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Nov 3, 2020
@pearu

pearu commented Nov 3, 2020

Copy link
Copy Markdown
Collaborator Author

@mruberry, please review.

@mruberry

mruberry commented Nov 6, 2020

Copy link
Copy Markdown
Collaborator

@pearu let's give this a moment while we hash out the fundamental design issues. One of the things we talked about (briefly) was not exposing coalescing to users. I don't think that's likely to change but no need to rush this, right?

@pearu

pearu commented Nov 8, 2020

Copy link
Copy Markdown
Collaborator Author

@mruberry, no hurry, we can revisit this PR once the fundamental design discussions are settled down.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@mruberry
mruberry requested review from cpuhrsch and removed request for mruberry January 24, 2022 17:02
@cpuhrsch

Copy link
Copy Markdown
Contributor

@pearu - do we still want to pursue this?

@pearu

pearu commented Jan 27, 2022

Copy link
Copy Markdown
Collaborator Author

@pearu - do we still want to pursue this?

This PR implements no new functionality but introduces a few API changes in order to

  1. get rid of internal methods _values() and _indices() that are exposed to users as public methods in sparse docs.
  2. rename _nnz() to nse() as _nnz() functionality does not correspond to what the acronym "nnz" stands for.

Both make sense to me to improve exposing sparse tensors support to end-users but there appears to be no consensus on the importance nor acceptance of these items.

@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

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.

5 participants