-
Notifications
You must be signed in to change notification settings - Fork 243
fixes the kron
implementation for sparse + diagonal matrix
#2804
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
Open
tam724
wants to merge
6
commits into
JuliaGPU:master
Choose a base branch
from
tam724:fix_kron_cusparse_diag
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
be66023
fix + tests
tam724 58123fa
use adapt(..)
tam724 5e51422
support adjoint(::Diagonal{<:CuVector{<:Complex}}) (e.g. mul! resulte…
tam724 c12cd91
Implement kron for {Adjoint, Transpose}(CuSparseMatrixCOO) and Diagon…
tam724 25b4648
Merge branch 'master' into fix_kron_cusparse_diag
tam724 fd04f1c
Reduce tests and test only Diagonal{Bool}
tam724 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,10 +7,12 @@ m = 10 | |
B = sprand(T, m, m, 0.3) | ||
ZA = spzeros(T, m, m) | ||
C = I(div(m, 2)) | ||
D = Diagonal(rand(T, m)) | ||
@testset "type = $typ" for typ in [CuSparseMatrixCSR, CuSparseMatrixCSC] | ||
dA = typ(A) | ||
dB = typ(B) | ||
dZA = typ(ZA) | ||
dD = Diagonal(CuArray(D.diag)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. |
||
@testset "opnorm and norm" begin | ||
@test opnorm(A, Inf) ≈ opnorm(dA, Inf) | ||
@test opnorm(A, 1) ≈ opnorm(dA, 1) | ||
|
@@ -42,6 +44,12 @@ m = 10 | |
@test collect(kron(opa(dZA), C)) ≈ kron(opa(ZA), C) | ||
@test collect(kron(C, opa(dZA))) ≈ kron(C, opa(ZA)) | ||
end | ||
@testset "kronecker product with Diagonal opa = $opa" for opa in (identity, transpose, adjoint) | ||
@test collect(kron(opa(dA), dD)) ≈ kron(opa(A), D) | ||
@test collect(kron(dD, opa(dA))) ≈ kron(D, opa(A)) | ||
@test collect(kron(opa(dZA), dD)) ≈ kron(opa(ZA), D) | ||
@test collect(kron(dD, opa(dZA))) ≈ kron(D, opa(ZA)) | ||
end | ||
end | ||
end | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I would also need an opinion about these lines. This still leads to unexpected behaviour, when the diagonal of B contains
false
elements. But first I did not want to break with previous behaviour of the function.My idea for a clean solution would be to only allow Diagonal{TD, <:CuVector{TD}}, but this would break code that depends on the previous behaviour.
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.
Why can't we implement the
Bool
case as multiplication bydiag
? Relying onfalse
elements still performing multiplication as if byI
seems like something we shouldn't support. The behavior here should simply match Base, so I guess we can remove theBool
/I
special casing altogether?Uh oh!
There was an error while loading. Please reload this page.
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.
Currently somebody might expect
kron(cu(sprand(2, 2, 1.0)), I(2))
to work. This would error afterwards.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.
That works with SparseArrays.jl, so would have to work here too.
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.
should it?
I would expect the CUDA
kron
to work with the CUDADiagonal
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.
I'm not following. I'm saying that because
kron(sprand(2, 2, 1.0), I(2))
works on the CPU, with Array, it should work on the GPU withCuArray
.Uh oh!
There was an error while loading. Please reload this page.
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.
Sorry for the confusion, but I think we are on the same page.
Before this PR the method allowed kron for the combination of a CuArray (sparse) and a CPU Diagonal (by ignoring the diag field of
Diagonal
and incorrectly assuming thatDiagonal
represents sized identity matrix).Handling the Diagonal correctly would disallow this combination but break with the previous behaviour. If we are okay with that, CUDA.jl should implement
That would break user code that "worked" before this PR, like:
I'm hesitant because I don't want to break others code. And because this was also tested behaviour:
CUDA.jl/test/libraries/cusparse/linalg.jl
Lines 39 to 44 in 66ab572
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.
I don't have the time right now to look at this myself, so sorry for the naive questions, but why isn't it possible to support both
I
inputs as well asDiagonal
ones, with the latter respecting the actual diagonal values, by correctly determining the value to broadcast instead of hard-codingCUDA.ones
? Or, worst case, by using two different methods?