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

Avoid materializing I, simplify _eyelike with CUDA and make it differentiable #1254

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/lib/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ end

end

@adjoint (::Type{T})(xs::Array) where {T <: CUDA.CuArray} =
_eyelike(y::CUDA.CuVector{T}) where T = CUDA.CuArray(I(length(y)))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
_eyelike(y::CUDA.CuVector{T}) where T = CUDA.CuArray(I(length(y)))
_eyelike(y::CUDA.CuVector) = CUDA.CuArray(I(length(y)))

To avoid the unbound type param.

@adjoint (::Type{T})(xs::AbstractArray) where {T <: CUDA.CuArray} =
Copy link
Member

Choose a reason for hiding this comment

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

IIUC the intent of the PR, would we not want a dedicated path UniformScaling? Taking <:AbstractArray presumes we can convert back to that subtype, but the implementation can only convert back to Array at present. Adapt.jl may be able to help us here.

T(xs), Δ -> (convert(Array, Δ), )

@adjoint function sum(xs::CUDA.AbstractGPUArray; dims = :)
Expand Down
7 changes: 1 addition & 6 deletions src/lib/grad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,7 @@ _jvec(x::Number) = _jvec(vcat(x))
_jvec(x) = throw(ArgumentError("jacobian expected a function which returns an array, or a scalar, got $(typeof(x))"))
_jvec(x::AbstractArray{<:Complex}) = throw(ArgumentError("jacobian does not accept complex output"))

_eyelike(y::Vector) = Matrix{eltype(y)}(I, length(y), length(y))
function _eyelike(y::AbstractVector) # version which works on GPU
out = fill!(similar(y, length(y), length(y)), 0)
out[LinearAlgebra.diagind(out)] .= 1
out
end
_eyelike(y::AbstractVector{T}) where T = T.(I(length(y)))

_gradcopy!(dst::AbstractArray, src::AbstractArray{<:Number}) = copyto!(dst, src)
_gradcopy!(dst::AbstractArray, src::Number) = copyto!(dst, src)
Expand Down
7 changes: 7 additions & 0 deletions test/cuda.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ end
@test j2[v1] ≈ cu(res2)
end

@testset "UniformScaling" begin
r = cu(rand(3))
@test gradient(r) do r
sum(Zygote._eyelike(r) .+ r)
end == (cu(fill(3.f0,3)), )
end

@testset "gradient algebra" begin
w, b = rand(2) |> cu, rand(2) |> cu
x1, x2 = rand(2) |> cu, rand(2) |> cu
Expand Down
44 changes: 0 additions & 44 deletions test/gradcheck.jl
Original file line number Diff line number Diff line change
@@ -1,47 +1,3 @@
using Zygote, Test, Random, LinearAlgebra, Statistics, SparseArrays, FillArrays,
AbstractFFTs, FFTW, Distances
using Zygote: gradient
using Base.Broadcast: broadcast_shape
using Distributed: pmap, CachingPool, workers
import FiniteDifferences

function ngradient(f, xs::AbstractArray...)
grads = zero.(xs)
for (x, Δ) in zip(xs, grads), i in 1:length(x)
δ = sqrt(eps())
tmp = x[i]
x[i] = tmp - δ/2
y1 = f(xs...)
x[i] = tmp + δ/2
y2 = f(xs...)
x[i] = tmp
Δ[i] = (y2-y1)/δ
end
return grads
end

function gradcheck(f, xs...)
grad_zygote = gradient(f, xs...)
grad_finite_difference = ngradient(f, xs...)
return all(isapprox.(grad_zygote, grad_finite_difference; rtol = 1e-5, atol = 1e-5))
end

gradtest(f, xs::AbstractArray...) = gradcheck((xs...) -> sum(sin.(f(xs...))), xs...)
gradtest(f, dims...) = gradtest(f, rand.(Float64, dims)...)

# utilities for using gradcheck with complex matrices
_splitreim(A) = (real(A),)
_splitreim(A::AbstractArray{<:Complex}) = reim(A)

_joinreim(A, B) = complex.(A, B)
_joinreim(A) = A

function _dropimaggrad(A)
back(Δ) = real(Δ)
back(Δ::Nothing) = nothing
return Zygote.hook(back, A)
end

Random.seed!(0)

@testset "println, show, string, etc" begin
Expand Down
43 changes: 43 additions & 0 deletions test/gradcheck_utils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Zygote, Test, Random, LinearAlgebra, Statistics, SparseArrays, FillArrays,
AbstractFFTs, FFTW, Distances
using Zygote: gradient
using Base.Broadcast: broadcast_shape
using Distributed: pmap, CachingPool, workers
import FiniteDifferences

function ngradient(f, xs::AbstractArray...)
grads = zero.(xs)
for (x, Δ) in zip(xs, grads), i in 1:length(x)
δ = sqrt(eps())
tmp = x[i]
x[i] = tmp - δ/2
y1 = f(xs...)
x[i] = tmp + δ/2
y2 = f(xs...)
x[i] = tmp
Δ[i] = (y2-y1)/δ
end
return grads
end

function gradcheck(f, xs...)
grad_zygote = gradient(f, xs...)
grad_finite_difference = ngradient(f, xs...)
return all(isapprox.(grad_zygote, grad_finite_difference; rtol = 1e-5, atol = 1e-5))
end

gradtest(f, xs::AbstractArray...) = gradcheck((xs...) -> sum(sin.(f(xs...))), xs...)
gradtest(f, dims...) = gradtest(f, rand.(Float64, dims)...)

# utilities for using gradcheck with complex matrices
_splitreim(A) = (real(A),)
_splitreim(A::AbstractArray{<:Complex}) = reim(A)

_joinreim(A, B) = complex.(A, B)
_joinreim(A) = A

function _dropimaggrad(A)
back(Δ) = real(Δ)
back(Δ::Nothing) = nothing
return Zygote.hook(back, A)
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using Zygote, Test
using Zygote: gradient, ZygoteRuleConfig
using CUDA
using CUDA: has_cuda
include("gradcheck_utils.jl")

@testset "all" begin # Overall testset ensures it keeps running after failure

Expand Down