-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix nesting of forwarddiff and sparsity detection
Duo with SciML/PreallocationTools.jl#91, preventing circular dependency
- Loading branch information
1 parent
4ea5cf0
commit 7513ec6
Showing
4 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,49 @@ | ||
module PreallocationToolsSymbolicsExt | ||
|
||
using PreallocationTools | ||
import PreallocationTools: _restructure, get_tmp | ||
using Symbolics, ForwardDiff | ||
|
||
function get_tmp(dc::DiffCache, u::Type{X}) where {T,N, X<: ForwardDiff.Dual{T, Num, N}} | ||
if length(dc.du) > length(dc.any_du) | ||
resize!(dc.any_du, length(dc.du)) | ||
end | ||
_restructure(dc.du, dc.any_du) | ||
end | ||
|
||
function get_tmp(dc::DiffCache, u::X) where {T,N, X<: ForwardDiff.Dual{T, Num, N}} | ||
if length(dc.du) > length(dc.any_du) | ||
resize!(dc.any_du, length(dc.du)) | ||
end | ||
_restructure(dc.du, dc.any_du) | ||
end | ||
|
||
function get_tmp(dc::DiffCache, u::AbstractArray{X}) where {T,N, X<: ForwardDiff.Dual{T, Num, N}} | ||
if length(dc.du) > length(dc.any_du) | ||
resize!(dc.any_du, length(dc.du)) | ||
end | ||
_restructure(dc.du, dc.any_du) | ||
end | ||
|
||
function get_tmp(dc::FixedSizeDiffCache, u::Type{X}) where {T,N, X<: ForwardDiff.Dual{T, Num, N}} | ||
if length(dc.du) > length(dc.any_du) | ||
resize!(dc.any_du, length(dc.du)) | ||
end | ||
_restructure(dc.du, dc.any_du) | ||
end | ||
|
||
function get_tmp(dc::FixedSizeDiffCache, u::X) where {T,N, X<: ForwardDiff.Dual{T, Num, N}} | ||
if length(dc.du) > length(dc.any_du) | ||
resize!(dc.any_du, length(dc.du)) | ||
end | ||
_restructure(dc.du, dc.any_du) | ||
end | ||
|
||
function get_tmp(dc::FixedSizeDiffCache, u::AbstractArray{X}) where {T,N, X<: ForwardDiff.Dual{T, Num, N}} | ||
if length(dc.du) > length(dc.any_du) | ||
resize!(dc.any_du, length(dc.du)) | ||
end | ||
_restructure(dc.du, dc.any_du) | ||
end | ||
|
||
end |
This file contains 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,23 @@ | ||
using ForwardDiff, SparseArrays, Symbolics, PreallocationTools | ||
# Test Nesting https://discourse.julialang.org/t/preallocationtools-jl-with-nested-forwarddiff-and-sparsity-pattern-detection-errors/107897 | ||
|
||
function foo(x, cache) | ||
d = get_tmp(cache, x) | ||
|
||
d[:] = x | ||
|
||
0.5 * x'*x | ||
end | ||
|
||
function residual(r, x, cache) | ||
function foo_wrap(x) | ||
foo(x, cache) | ||
end | ||
|
||
r[:] = ForwardDiff.gradient(foo_wrap, x) | ||
end | ||
|
||
cache = DiffCache(zeros(2)) | ||
pattern = Symbolics.jacobian_sparsity((r, x) -> residual(r, x, cache), zeros(2), zeros(2)) | ||
@test pattern == sparse([1 0 | ||
0 1]) |
This file contains 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