You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently Gridap at src/Arrays/Autodiff.jl uses nothing based ForwardDiff.Tag for constructing the configs for ForwardDiff.gradient and ForwardDiff.jacobian,
this is not a problem for spatial gradient/laplacians of FEFunctions, as ForwardDiff machinery is not being used inside. Currently, I am working with CellFields where the spatial gradient/laplacian requires the usage of ForwardDiff and the interaction of the tag generated in this ForwardDiff usage has interaction problem with nothing tag from derivatives with respect DOF with respect to the functionals involving the spatial derivative, related to determining the order of the tags. Following is the MWE with only ForwardDiff:
using ForwardDiff
using LinearAlgebra
f(θ,x) = θ⋅x
functiontest_func(θ)
_f(x) =f(θ,x)
x = [0.5,0.6]
norm(ForwardDiff.gradient(_f,x))
end
θ = [2.0,0.5]
# following the strategy of `Gridap.Arrays` `Autodiff.jl`
cfg = ForwardDiff.GradientConfig(nothing,θ,ForwardDiff.Chunk{length(θ)}())
cfg isa ForwardDiff.GradientConfig{Nothing}
functioncfg_type(cfg::ForwardDiff.GradientConfig{T}) where T
T
endcfg_type(cfg)
xdual = cfg.duals
ForwardDiff.seed!(xdual, θ, cfg.seeds)
ydual =test_func(xdual) # this is the place where tag order error occurs
result =similar(θ, ForwardDiff.valtype(ydual))
result = ForwardDiff.extract_gradient!(cfg_type(cfg),result,ydual)
ydual = test_func(xdual) call results in the following error, this where the interaction of dual numbers from different ForwardDiff calls occurs. This is due to one tag being Nothing.
The possible fix is to have a dummy tag for AD functionality for functionals in Gridap, instead of nothing. Having the following works without problems and the results match with manual gradients and FiniteDifferences for complicated cases:
dummy() =nothing# a better name can be chosen functionreturn_cache(k::ConfigMap{typeof(ForwardDiff.gradient)},x)
cfg = ForwardDiff.GradientConfig(dummy,x,ForwardDiff.Chunk{length(x)}())
cfg
endfunctionreturn_cache(k::ConfigMap{typeof(ForwardDiff.jacobian)},x)
cfg = ForwardDiff.JacobianConfig(dummy,x,ForwardDiff.Chunk{length(x)}())
cfg
end
Introducing this would not disturb or break anything or fail in the existing use cases.
The text was updated successfully, but these errors were encountered:
For the records, we talked about having a more general approach to solve this issue. Instead of a fixed dummy function, use the function at hand that we are about to differentiate. To this end we need to modify the ConfigMap struct as
struct ConfigMap{F} <:Map
f::FendConfigMap()=ConfigMap(nothing) # required for backward compatibility
The above approach seems to make things slower, please check #806 (comment). I feel we need an extra input parameter in gradient, jacobian (or _gradient or _jacobian so as to not disturb the high-level API) when we want to trigger tag checking, this is required only when we have CellFields where ForwardDiff will be use and not in FEFunction cases. See the section in ForwardDiff.jl docs on tag checking. We can use a input parameter like Val{true/false}.
Currently
Gridap
atsrc/Arrays/Autodiff.jl
usesnothing
basedForwardDiff.Tag
for constructing theconfig
s forForwardDiff.gradient
andForwardDiff.jacobian
,Gridap.jl/src/Arrays/Autodiff.jl
Lines 51 to 59 in 78b27c4
this is not a problem for spatial
gradient/laplacian
s ofFEFunction
s, asForwardDiff
machinery is not being used inside. Currently, I am working withCellFields
where the spatialgradient/laplacian
requires the usage ofForwardDiff
and the interaction of thetag
generated in thisForwardDiff
usage has interaction problem withnothing
tag from derivatives with respect DOF with respect to the functionals involving the spatial derivative, related to determining the order of the tags. Following is the MWE with onlyForwardDiff
:ydual = test_func(xdual)
call results in the following error, this where the interaction of dual numbers from differentForwardDiff
calls occurs. This is due to one tag beingNothing
.cc @amartinhuertas @fverdugo @santiagobadia
The possible fix is to have a dummy tag for AD functionality for functionals in Gridap, instead of
nothing
. Having the following works without problems and the results match with manual gradients andFiniteDifferences
for complicated cases:Introducing this would not disturb or break anything or fail in the existing use cases.
The text was updated successfully, but these errors were encountered: