The issue can be reduced to the following semi-MWE:
Consider a function that return a literal number, and some vector(s):
using Enzyme
function tester(x::AbstractVector{T}) where T
return (T(pi), sum(x), x.^2)
end
xi = [0.0, 2.0, 0.0]
derivs = Enzyme.autodiff(Forward,tester, Duplicated, BatchDuplicated(xi, onehot(xi)))
Which yields:
(var"1" = (var"1" = (3.141592653589793, 1.0, [0.0, 0.0, 0.0]), var"2" = (3.141592653589793, 1.0, [0.0, 4.0, 0.0]), var"3" = (3.141592653589793, 1.0, [0.0, 0.0, 0.0])),)
I.e. the derivate of the 1st element of the tupple (the literal value) is incorrect.
Note that a very crude “fix” is:
function tester1(x::AbstractVector{T}) where T
return (T(x[begin]*pi/x[begin]), sum(x), x.^2)
end
derivs1 = Enzyme.autodiff(Forward,tester1, Duplicated, BatchDuplicated(xi, onehot(xi)))
#(var"1" = (var"1" = (0.0, 1.0, [0.0, 0.0, 0.0]), var"2" = (0.0, 1.0, [0.0, 4.0, 0.0]), var"3" = (0.0, 1.0, [0.0, 0.0, 0.0])),)
I am not sure if this is a bug or just a limitation of the forward mode (could not find anything in the docs regarding such).