-
Notifications
You must be signed in to change notification settings - Fork 5
Respect strong zero in axpby
#55
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
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
eaa0438
Respect strong zero in axpby
kshyatt b18b799
false is also a strong zero
kshyatt cd2d212
Add test
kshyatt 3f24768
Add test to complicated as well
kshyatt a3dbd28
Add some NaN tests
kshyatt dd6f58b
Check the strong zero in the non-JLArrays simple case too
kshyatt f49b8ef
One last fix
kshyatt 3c47539
NaN tests for complicated too
kshyatt 3d19abe
Bump patch version
kshyatt 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
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
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 |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| module StaticSVec | ||
| using VectorInterface | ||
| using JLArrays | ||
| using Test | ||
| using TestExtras | ||
|
|
||
| deepcollect(x::JLArray) = collect(x) | ||
| deepcollect(x::Number) = x | ||
|
|
||
| x = JLVector(randn(3)) | ||
| y = JLVector(randn(3)) | ||
| nan_y = JLVector(vcat(randn(2), NaN)) | ||
|
|
||
| @testset "scalartype" begin | ||
| s = @constinferred scalartype(x) | ||
| @test s == Float64 | ||
| end | ||
|
|
||
| @testset "zerovector" begin | ||
| z = @constinferred zerovector(x) | ||
| @test z isa JLVector{Float64} | ||
| @test all(iszero, deepcollect(z)) | ||
| @test all(deepcollect(z) .=== zero(scalartype(x))) | ||
| z1 = @constinferred zerovector!!(x) | ||
| @test z1 isa JLVector{Float64} | ||
| @test all(deepcollect(z1) .=== zero(scalartype(x))) | ||
|
|
||
| z3 = @constinferred zerovector(x, ComplexF64) | ||
| @test z3 isa JLVector{ComplexF64} | ||
| @test all(deepcollect(z3) .=== zero(ComplexF64)) | ||
| z4 = @constinferred zerovector!!(x, ComplexF64) | ||
| @test z4 isa JLVector{ComplexF64} | ||
| @test all(deepcollect(z4) .=== zero(ComplexF64)) | ||
| end | ||
|
|
||
| @testset "scale" begin | ||
| α = randn() | ||
| z = @constinferred scale(x, α) | ||
| @test z isa JLVector{Float64} | ||
| @test all(deepcollect(z) .== α .* deepcollect(x)) | ||
|
|
||
| z2 = @constinferred scale!!(x, α) | ||
| @test z2 isa JLVector{Float64} | ||
| @test deepcollect(z2) ≈ (α .* deepcollect(x)) | ||
| z2 = @constinferred scale!!(y, x, α) | ||
| @test z2 isa JLVector{Float64} | ||
| @test deepcollect(z2) ≈ (α .* deepcollect(x)) | ||
|
|
||
| α = randn(ComplexF64) | ||
| z4 = @constinferred scale(x, α) | ||
| @test z4 isa JLVector{ComplexF64} | ||
| @test deepcollect(z4) ≈ (α .* deepcollect(x)) | ||
| z5 = @constinferred scale!!(x, α) | ||
| @test z5 isa JLVector{ComplexF64} | ||
| @test deepcollect(z5) ≈ (α .* deepcollect(x)) | ||
|
|
||
| z6 = @constinferred scale!!(zerovector(x), x, α) | ||
| @test z6 isa JLVector{ComplexF64} | ||
| @test deepcollect(z6) ≈ (α .* deepcollect(x)) | ||
|
|
||
| ycomplex = zerovector(y, ComplexF64) | ||
| α = randn(Float64) | ||
| z8 = @constinferred scale!!(ycomplex, x, α) | ||
| @test scalartype(z8) == ComplexF64 | ||
| @test all(deepcollect(z8) .== α .* deepcollect(x)) | ||
| end | ||
|
|
||
| @testset "add" begin | ||
| α, β = randn(2) | ||
| z = add(y, x) | ||
| @test z isa JLVector{Float64} | ||
| @test all(deepcollect(z) .== deepcollect(x) .+ deepcollect(y)) | ||
| z = add(y, x, α) | ||
| @test deepcollect(z) ≈ muladd.(deepcollect(x), α, deepcollect(y)) | ||
| z = add(y, x, α, β) | ||
| @test deepcollect(z) ≈ muladd.(deepcollect(x), α, deepcollect(y) .* β) | ||
|
|
||
| z2 = @constinferred add!!(y, x) | ||
| @test z2 isa JLVector{Float64} | ||
| @test deepcollect(z2) ≈ (deepcollect(x) .+ deepcollect(y)) | ||
| z2 = @constinferred add!!(y, x, α) | ||
| @test deepcollect(z2) ≈ (muladd.(deepcollect(x), α, deepcollect(y))) | ||
| z2 = @constinferred add!!(y, x, α, β) | ||
| @test deepcollect(z2) ≈ (muladd.(deepcollect(x), α, deepcollect(y) .* β)) | ||
|
|
||
| α, β = randn(ComplexF64, 2) | ||
| z4 = add(y, x, α) | ||
| @test z4 isa JLVector{ComplexF64} | ||
| @test deepcollect(z4) ≈ (muladd.(deepcollect(x), α, deepcollect(y))) | ||
| z4 = add(y, x, α, β) | ||
| @test deepcollect(z4) ≈ (muladd.(deepcollect(x), α, deepcollect(y) .* β)) | ||
|
|
||
| z5 = @constinferred add!!(y, x, α) | ||
| @test z5 isa JLVector{ComplexF64} | ||
| @test deepcollect(z5) ≈ (muladd.(deepcollect(x), α, deepcollect(y))) | ||
| z5 = @constinferred add!!(y, x, α, β) | ||
| @test deepcollect(z5) ≈ (muladd.(deepcollect(x), α, deepcollect(y) .* β)) | ||
|
|
||
| # test strong zero | ||
| α = randn(ComplexF64) | ||
| z6 = add(y, x, α, Zero()) | ||
| @test deepcollect(z6) ≈ (muladd.(deepcollect(x), α, deepcollect(y) .* Zero())) | ||
| z6 = add(y, x, α, false) | ||
| @test deepcollect(z6) ≈ (muladd.(deepcollect(x), α, deepcollect(y) .* false)) | ||
|
|
||
| α = randn(scalartype(x)) | ||
| z6 = deepcopy(nan_y) | ||
| z6 = @constinferred add!(z6, x, α, Zero()) | ||
| @test !any(isnan, z6) | ||
| @test deepcollect(z6) ≈ (muladd.(deepcollect(x), α, deepcollect(nan_y) .* Zero())) | ||
| z6 = deepcopy(nan_y) | ||
| z6 = @constinferred add!(z6, x, α, false) | ||
| @test !any(isnan, z6) | ||
| @test deepcollect(z6) ≈ (muladd.(deepcollect(x), α, deepcollect(nan_y) .* false)) | ||
| z6 = deepcopy(nan_y) | ||
| z6 = @constinferred add!(z6, x, α, 0.0) | ||
| @test any(isnan, z6) | ||
| end | ||
|
|
||
| @testset "inner" begin | ||
| s = @constinferred inner(x, y) | ||
| @test s ≈ inner(deepcollect(x), deepcollect(y)) | ||
|
|
||
| α, β = randn(ComplexF64, 2) | ||
| s2 = @constinferred inner(scale(x, α), scale(y, β)) | ||
| @test s2 ≈ inner(α * deepcollect(x), β * deepcollect(y)) | ||
| end | ||
|
|
||
| end |
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
Oops, something went wrong.
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'm slightly confused you also added the
=== falsebranch here, does that effectively mean that we want to interpretfalseas a strong zero as well?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.
In Julia
falseis always strong zero, I thought?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.
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 did not know this 😆 I thought that was a BLAS-specific thing... In that case, ignore my comments :p
A different thing that just popped in my brain is whether it would make sense to replace these checks everywhere with
isstrongzeroandisstrongone, to enforce that this is handled consistently?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 think the strong zero check is only made here, so I don't know if it makes sense to create a whole new function for it
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.
The reason I was thinking about this is because we also use this downstream every now and again, e.g. https://github.com/QuantumKitHub/TensorOperations.jl/blob/ef937adcfa4e6bc4476f8197a62a2da45cf3b1d9/src/implementation/strided.jl#L142 where we actually turn every zero into a strong one, and I do like the idea of having this a bit more formalized/consistent. Does not have to be in this PR though!
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 might just merge this and tag then to keep stuff moving for the other PR