I can differentiate a function of one vector, but not multiple vectors
using ForwardDiff, Zygote
f(x) = 3.0*x[1] + 5.0*x[2]^2.0
fpF(x) = ForwardDiff.gradient(f,x)
fpZ(x) = Zygote.forward_jacobian(f,x)[2]
#
xx = 2.0*ones(2)
f(xx) # 26.0
fpF(xx) # [3.0;20.0]
fpZ(xx) # [3.0;20.0]
#Multiple vectors as args
g(x,y) = 3.0*x[1]*y[1] + 5.0*x[2]^2.0
gpF(x,y) = ForwardDiff.gradient(g,x)
gpZ(x,y) = Zygote.forward_jacobian(g,x)[2]
#
xx = 2.0*ones(2)
yy = 2.0*ones(1)
g(xx,yy) #32.0
julia> gpF(xx,yy)
ERROR: MethodError: no method matching g(::Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(g), Float64}, Float64, 2}})
Closest candidates are:
g(::Any, ::Any) at REPL[251]:1
Stacktrace:
[1] vector_mode_dual_eval(f::typeof(g), x::Vector{Float64}, cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{typeof(g), Float64}, Float64, 2, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(g), Float64}, Float64, 2}}})
@ ForwardDiff ~\.julia\packages\ForwardDiff\QOqCN\src\apiutils.jl:37
[2] vector_mode_gradient(f::typeof(g), x::Vector{Float64}, cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{typeof(g), Float64}, Float64, 2, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(g), Float64}, Float64, 2}}})
@ ForwardDiff ~\.julia\packages\ForwardDiff\QOqCN\src\gradient.jl:106
[3] gradient(f::Function, x::Vector{Float64}, cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{typeof(g), Float64}, Float64, 2, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(g), Float64}, Float64, 2}}}, ::Val{true})
@ ForwardDiff ~\.julia\packages\ForwardDiff\QOqCN\src\gradient.jl:19
[4] gradient(f::Function, x::Vector{Float64}, cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{typeof(g), Float64}, Float64, 2, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(g), Float64}, Float64, 2}}}) (repeats 2 times)
@ ForwardDiff ~\.julia\packages\ForwardDiff\QOqCN\src\gradient.jl:17
[5] gpF(x::Vector{Float64}, y::Vector{Float64})
@ Main .\REPL[252]:1
[6] top-level scope
@ REPL[261]:1
julia> gpZ(xx,yy)
ERROR: MethodError: no method matching g(::Vector{ForwardDiff.Dual{Nothing, Float64, 2}})
Closest candidates are:
g(::Any, ::Any) at REPL[251]:1
Stacktrace:
[1] forward_jacobian(f::typeof(g), x::Vector{Float64}, #unused#::Val{2})
@ Zygote ~\.julia\packages\Zygote\RxTZu\src\lib\forward.jl:23
[2] forward_jacobian(f::Function, x::Vector{Float64})
@ Zygote ~\.julia\packages\Zygote\RxTZu\src\lib\forward.jl:38
[3] gpZ(x::Vector{Float64}, y::Vector{Float64})
@ Main .\REPL[253]:1
[4] top-level scope
@ REPL[262]:1