Skip to content

Use cache tags in jacvec hesvec operations #197

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
merged 1 commit into from
Aug 23, 2022
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 <[email protected]>", "Chris Rackauckas <[email protected]>"]
version = "1.26.1"
version = "1.26.2"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
9 changes: 6 additions & 3 deletions src/differentiation/jaches_products.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
struct DeivVecTag end

get_tag(::Array{Dual{T,V,N}}) where {T,V,N} = T
get_tag(::Dual{T,V,N}) where {T,V,N} = T

# J(f(x))*v
function auto_jacvec!(
dy,
Expand All @@ -9,7 +12,7 @@ function auto_jacvec!(
cache1 = Dual{typeof(ForwardDiff.Tag(DeivVecTag(),eltype(x))),eltype(x),1}.(x, ForwardDiff.Partials.(tuple.(reshape(v, size(x))))),
cache2 = similar(cache1),
)
cache1 .= Dual{typeof(ForwardDiff.Tag(DeivVecTag(),eltype(x))),eltype(x),1}.(x, ForwardDiff.Partials.(tuple.(reshape(v, size(x)))))
cache1 .= Dual{get_tag(cache1),eltype(x),1}.(x, ForwardDiff.Partials.(tuple.(reshape(v, size(x)))))
f(cache2, cache1)
vecdy = _vec(dy)
vecdy .= partials.(_vec(cache2), 1)
Expand Down Expand Up @@ -135,7 +138,7 @@ function autonum_hesvec!(
)
cache = FiniteDiff.GradientCache(v[1], cache1, Val{:central})
g = (dx, x) -> FiniteDiff.finite_difference_gradient!(dx, f, x, cache)
cache1 .= Dual{typeof(ForwardDiff.Tag(DeivVecTag(),eltype(x))),eltype(x),1}.(x, ForwardDiff.Partials.(tuple.(reshape(v, size(x)))))
cache1 .= Dual{get_tag(cache1),eltype(x),1}.(x, ForwardDiff.Partials.(tuple.(reshape(v, size(x)))))
g(cache2, cache1)
dy .= partials.(cache2, 1)
end
Expand Down Expand Up @@ -175,7 +178,7 @@ function auto_hesvecgrad!(
cache2 = Dual{typeof(ForwardDiff.Tag(DeivVecTag(),eltype(x))),eltype(x),1}.(x, ForwardDiff.Partials.(tuple.(reshape(v, size(x))))),
cache3 = Dual{typeof(ForwardDiff.Tag(DeivVecTag(),eltype(x))),eltype(x),1}.(x, ForwardDiff.Partials.(tuple.(reshape(v, size(x))))),
)
cache2 .= Dual{typeof(ForwardDiff.Tag(DeivVecTag(),eltype(x))),eltype(x),1}.(x, ForwardDiff.Partials.(tuple.(reshape(v, size(x)))))
cache2 .= Dual{get_tag(cache2),eltype(x),1}.(x, ForwardDiff.Partials.(tuple.(reshape(v, size(x)))))
g(cache3, cache2)
dy .= partials.(cache3, 1)
end
Expand Down