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

Fix master #282

Merged
merged 3 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SparseDiffTools"
uuid = "47a9eef4-7e08-11e9-0b38-333d64bd3804"
authors = ["Pankaj Mishra <pankajmishra1511@gmail.com>", "Chris Rackauckas <contact@chrisrackauckas.com>"]
version = "2.15.0"
version = "2.15.1"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
3 changes: 2 additions & 1 deletion ext/SparseDiffToolsZygoteExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@

### Jac, Hes products

function numback_hesvec!(dy, f::F, x, v, cache1 = similar(v), cache2 = similar(v), cache3 = similar(v)) where {F}
function numback_hesvec!(dy, f::F, x, v, cache1 = similar(v), cache2 = similar(v),

Check warning on line 48 in ext/SparseDiffToolsZygoteExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseDiffToolsZygoteExt.jl#L48

Added line #L48 was not covered by tests
cache3 = similar(v)) where {F}
g = let f = f
(dx, x) -> dx .= first(Zygote.gradient(f, x))
end
Expand Down
2 changes: 1 addition & 1 deletion src/SparseDiffTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import ADTypes: AbstractADType, AutoSparseZygote, AbstractSparseForwardMode,
import ForwardDiff: Dual, jacobian, partials, DEFAULT_CHUNK_THRESHOLD
# Array Packages
using ArrayInterface, SparseArrays
import ArrayInterface: matrix_colors
import ArrayInterface: matrix_colors, allowed_setindex!
import StaticArrays
import StaticArrays: StaticArray, SArray, MArray, Size
# Others
Expand Down
7 changes: 4 additions & 3 deletions src/differentiation/jaches_products.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
vec(partials.(vec(f(y)), 1))
end

function num_jacvec!(dy, f, x, v, cache1 = similar(v), cache2 = similar(v), cache3 = similar(v);
compute_f0 = true)
function num_jacvec!(dy, f, x, v, cache1 = similar(v), cache2 = similar(v),

Check warning on line 35 in src/differentiation/jaches_products.jl

View check run for this annotation

Codecov / codecov/patch

src/differentiation/jaches_products.jl#L35

Added line #L35 was not covered by tests
cache3 = similar(v); compute_f0 = true)
vv = reshape(v, axes(x))
compute_f0 && (f(cache1, x))
T = eltype(x)
Expand Down Expand Up @@ -134,7 +134,8 @@
partials.(g(Dual{DeivVecTag}.(x, v)), 1)
end

function num_hesvecgrad!(dy, g, x, v, cache1 = similar(v), cache2 = similar(v), cache3 = similar(v))
function num_hesvecgrad!(dy, g, x, v, cache1 = similar(v), cache2 = similar(v),

Check warning on line 137 in src/differentiation/jaches_products.jl

View check run for this annotation

Codecov / codecov/patch

src/differentiation/jaches_products.jl#L137

Added line #L137 was not covered by tests
cache3 = similar(v))
T = eltype(x)
# Should it be min? max? mean?
ϵ = sqrt(eps(real(T))) * max(one(real(T)), abs(norm(x)))
Expand Down
32 changes: 24 additions & 8 deletions src/differentiation/vecjac_products.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function num_vecjac!(du, f::F, x, v, cache1 = similar(v), cache2 = similar(v), cache3 = similar(v);
compute_f0 = true) where {F}
function num_vecjac!(du, f::F, x, v, cache1 = similar(v), cache2 = similar(v),

Check warning on line 1 in src/differentiation/vecjac_products.jl

View check run for this annotation

Codecov / codecov/patch

src/differentiation/vecjac_products.jl#L1

Added line #L1 was not covered by tests
cache3 = similar(x); compute_f0 = true) where {F}
compute_f0 && (f(cache1, x))
T = eltype(x)
# Should it be min? max? mean?
Expand All @@ -15,19 +15,35 @@
return du
end

# Special Non-Allocating case for StaticArrays
function num_vecjac(f::F, x::SArray, v::SArray, f0 = nothing) where {F}
f0 === nothing ? (_f0 = f(x)) : (_f0 = f0)
vv = reshape(v, axes(_f0))
T = eltype(x)
ϵ = sqrt(eps(real(T))) * max(one(real(T)), abs(norm(x)))
du = zeros(typeof(x))
for i in 1:length(x)
cache = Base.setindex(x, x[i] + ϵ, i)
f0 = f(cache)
du = Base.setindex(du, (((f0 .- _f0) ./ ϵ)' * vv), i)
end
return du

Check warning on line 30 in src/differentiation/vecjac_products.jl

View check run for this annotation

Codecov / codecov/patch

src/differentiation/vecjac_products.jl#L19-L30

Added lines #L19 - L30 were not covered by tests
end

function num_vecjac(f::F, x, v, f0 = nothing) where {F}
f0 === nothing ? (_f0 = f(x)) : (_f0 = f0)
vv = reshape(v, axes(_f0))
T = eltype(x)
# Should it be min? max? mean?
ϵ = sqrt(eps(real(T))) * max(one(real(T)), abs(norm(x)))
du = similar(x)
cache = copy(x)
cache = similar(x)
copyto!(cache, x)

Check warning on line 41 in src/differentiation/vecjac_products.jl

View check run for this annotation

Codecov / codecov/patch

src/differentiation/vecjac_products.jl#L40-L41

Added lines #L40 - L41 were not covered by tests
avik-pal marked this conversation as resolved.
Show resolved Hide resolved
for i in 1:length(x)
cache[i] += ϵ
f0 = f(x)
cache[i] = x[i]
du[i] = (((f0 .- _f0) ./ ϵ)' * vv)[1]
cache = allowed_setindex!(cache, x[i] + ϵ, i)
f0 = f(cache)
cache = allowed_setindex!(cache, x[i], i)
du = allowed_setindex!(du, (((f0 .- _f0) ./ ϵ)' * vv)[1], i)

Check warning on line 46 in src/differentiation/vecjac_products.jl

View check run for this annotation

Codecov / codecov/patch

src/differentiation/vecjac_products.jl#L43-L46

Added lines #L43 - L46 were not covered by tests
end
return vec(du)
end
Expand Down Expand Up @@ -93,7 +109,7 @@
end

function _vecjac(f::F, fu, u, autodiff::AutoFiniteDiff) where {F}
cache = (similar(fu), similar(fu), similar(fu))
cache = (similar(fu), similar(fu), similar(u))

Check warning on line 112 in src/differentiation/vecjac_products.jl

View check run for this annotation

Codecov / codecov/patch

src/differentiation/vecjac_products.jl#L112

Added line #L112 was not covered by tests
pullback = nothing
return AutoDiffVJP(f, u, cache, autodiff, pullback)
end
Expand Down
17 changes: 16 additions & 1 deletion test/test_vecjac_products.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using SparseDiffTools, Zygote
using SparseDiffTools, Zygote, ForwardDiff
using LinearAlgebra, Test
using StaticArrays

using Random
Random.seed!(123)
Expand Down Expand Up @@ -170,3 +171,17 @@ L = VecJac(f3_iip, copy(x); autodiff = AutoFiniteDiff(), fu = copy(y))
L = VecJac(f3_oop, copy(x); autodiff = AutoZygote())
@test size(L) == (length(x), length(y))
@test L * y ≈ Zygote.jacobian(f3_oop, copy(x))[1]' * y

@info "Testing StaticArrays"

const A_sa = rand(SMatrix{4, 4, Float32})
_f_sa(x) = A_sa * (x .^ 2)

u = rand(SVector{4, Float32})
v = rand(SVector{4, Float32})

J = ForwardDiff.jacobian(_f_sa, u)
Jᵀv_true = J' * v

@test num_vecjac(_f_sa, u, v) isa SArray
@test num_vecjac(_f_sa, u, v)≈Jᵀv_true atol=1e-3
Loading