-
-
Notifications
You must be signed in to change notification settings - Fork 217
Description
Hello,
I noticed an unusual silent error when trying to compute the jacobian
of a function with scalar output when the input is an array of SVector
s. While gradient
and pullback
give the expected results, jacobian
results an Any
array full of zeros.
This is my mwe
using Zygote
using StaticArrays
function mwe(arr)
tmp = [x[1] for x in arr]
abs(tmp[1])
end
arr = fill(SVector(1.0, 2.0), 10)
g, = Zygote.gradient(mwe, arr) # OK
j, = Zygote.jacobian(mwe, arr) # wrong
z, zback = Zygote.pullback(mwe, arr) # OK
dz, = zback(1.0)
@show g j dz
# g = SVector{2, Float64}[[1.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]
# j = Any[0 0 0 0 0 0 0 0 0 0]
# dz = SVector{2, Float64}[[1.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]
I used Julia v1.11.6, Zygote v0.7.10, and StaticArrays v1.9.14.
I noticed that the array of Any
is happening because type promotion between the scalar output and SVector
array element type fails here
Line 183 in e0af1a8
T = promote_type(eltype(x), eltype(y)) |
While for my actual calculation gradient
is the right function to call, I spent a long time trying figure out if Zygote was failing to differentiate my code due to this silent error. Perhaps it would be worth adding a warning to the docs that only AbstractArray
arguments with numeric element type are expected to work?