Skip to content
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

Add basic sparse handling #246

Closed
wants to merge 12 commits into from

Conversation

DhairyaLGandhi
Copy link
Contributor

@DhairyaLGandhi DhairyaLGandhi commented Aug 18, 2020

Questions:

  • how do I test things?
  • Will this be the right test here?
  • Also this adds an rrule for a constructor, is that a problem?

@willtebbutt
Copy link
Member

willtebbutt commented Aug 18, 2020

how do I test things?

See other examples in this package, e.g. here. ChainRulesTestUtils.jl should always be used, unless it's actually not possible for some reason.

Will this be the right test here?

Not clear what you mean by this. Could you clarify please?

Also this adds an rrule for a constructor, is that a problem?

rrules for constructors are fine :)

@DhairyaLGandhi
Copy link
Contributor Author

Not clear what you mean by this. Could you clarify please?
I feel like I'm using the API incorrectly, but none seemed to work

rrule_test(x -> sum(sparse(x)), rand(), (rand(3,3), rand()))

Should have, I thought, but maybe you could clarify

@willtebbutt
Copy link
Member

rrule_test(x -> sum(sparse(x)), rand(), (rand(3,3), rand()))

Should have, I thought, but maybe you could clarify

The first element of the last argument is the value at which you want to evaluate the rrule, call it x. That looks like it's fine in your case. The second element should be an appropriate differential for x, call it dx. Since x is a Matrix{Float64}, dx should be a Matrix{Float64} of the same size.

As regards the first argument, rrule_test literally only handles rrules, and can't handle their composition with other things. Consequently, if you want to test sparse, you should only pass in sparse. The reason it can't handle composition with other functions is because you would need an AD to do this, and ChainRules is AD-agnostic. Call the first argument f.

Finally, the second argument has to be an appropriate differential for f(x). In this case, this means that you need to define what it means to be an appropriate differential for a SparseMatrixCSC{Float64,Int64}. I'm guessing that some arbitrary AbstractMatrix{Float64} is not generally appropriate because it will destroy the complexity guarantees associated with sparse arrays.

On that note, it might be worth reading #232 if you've not already -- there's a large discussion going on about the "right" way to think about this, which might be helpful. It would be great if you could chime in with your thoughts on the matter.

@willtebbutt willtebbutt self-requested a review August 19, 2020 07:55
@DhairyaLGandhi
Copy link
Contributor Author

The reason it can't handle composition with other functions is because you would need an AD to do this, and ChainRules is AD-agnostic

That would explain why sparse wouldn't work either. It would need to test SparseMatrixCSC directly, since there is no AD recursion into the stacks happening either. Thanks for pointing me to the issue, I do think having a robust testing pipeline is necessary for the different levels of abstractions, and this makes sense for ChainRules.

@willtebbutt
Copy link
Member

That would explain why sparse wouldn't work either. It would need to test SparseMatrixCSC directly, since there is no AD recursion into the stacks happening either.

That's not quite true. IIUC you've implemented an rrule for sparse directly, so you should be able to use CRTU (ChainRulesTestUtils) on it. The question is just what the differential of the output should be.

Thanks for pointing me to the issue, I do think having a robust testing pipeline is necessary for the different levels of abstractions, and this makes sense for ChainRules.

Agreed. The issue in question has signficant ramifications for Zygote as well though -- there are currently loads of rules implemented there that are causing the kinds of problems discussed in that issue. But that's a separate issue.

Copy link
Contributor

@nickrobinson251 nickrobinson251 left a comment

Choose a reason for hiding this comment

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

thanks for the contribution!

I think @willtebbutt is helping out with testing. i've left some on the src 👍

src/rulesets/SparseArrays/array.jl Outdated Show resolved Hide resolved
using SparseArrays

function rrule(::Type{<:SparseMatrixCSC{T,N}}, arr) where {T,N}
function SparseMatrix_pullback(Δ)
Copy link
Contributor

Choose a reason for hiding this comment

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

please can you use 4-space indenting throughout, as per the style guide? https://github.com/invenia/BlueStyle#synopsis :)

src/rulesets/SparseArrays/array.jl Outdated Show resolved Hide resolved
src/rulesets/SparseArrays/array.jl Outdated Show resolved Hide resolved
src/rulesets/SparseArrays/array.jl Outdated Show resolved Hide resolved
src/rulesets/SparseArrays/array.jl Outdated Show resolved Hide resolved
@sethaxen
Copy link
Member

sethaxen commented Feb 4, 2021

I have a current project for which I need some rrules for SparseMatrixCSC. @DhairyaLGandhi are you planning to work on this in the near future? And if not, would you mind if I continued this work (in a new PR)?

@DhairyaLGandhi
Copy link
Contributor Author

Sure, please go ahead! Apologies I missed this comment in the GitHub blackhole. I need this for a project as well, so would love to get this through.

@willtebbutt
Copy link
Member

@sethaxen please ping me when ready for review.

@@ -0,0 +1,15 @@
using SparseArrays
Copy link
Member

Choose a reason for hiding this comment

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

Not a full review, but just so it doesn't get missed, it is the pattern of this package, inline with BlueStyle,
to do all the usings at the top level file

@CarloLucibello
Copy link
Contributor

Just hit this problem in CarloLucibello/GraphNeuralNetworks.jl#113.
Is there any interest in finishing up this PR soon (it has been open for a year and a half)? Otherwise, I'll create a new one

@sethaxen
Copy link
Member

@CarloLucibello i won't be able to work on this soon. Feel free to take over!

@CarloLucibello CarloLucibello mentioned this pull request Jan 22, 2022
@CarloLucibello
Copy link
Contributor

I think this can be closed now that #579 is merged

@mcabbott mcabbott closed this Feb 8, 2022
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.

7 participants