ForwardDiff.jl icon indicating copy to clipboard operation
ForwardDiff.jl copied to clipboard

seed! dimension mismatch

Open michel2323 opened this issue 5 years ago • 0 comments

Hi,

I use ForwardDiff.jl applied to CUDA arrays and I want to vary the number of partials in the dual. I want to seed each entry in the array with my own values and partials. For doing that I used seed! and ran into issues. I fixed it by implementing my own seed function:

function myseed!(duals::AbstractArray{ForwardDiff.Dual{T,V,N}}, x,
               seeds::AbstractArray{ForwardDiff.Partials{N,V}}) where {T,V,N}
    for i in 1:size(duals,1)
        duals[i] = ForwardDiff.Dual{T,V,N}(x[i], seeds[i])
    end
    return duals
end

The original implementation looks like this:

function seed!(duals::AbstractArray{Dual{T,V,N}}, x,
               seeds::NTuple{N,Partials{N,V}}) where {T,V,N}
    for i in 1:N
        duals[i] = Dual{T,V,N}(x[i], seeds[i])
    end
    return duals
end

I don't see why the loop should be over N, the number of partials. Shouldn't it be over the number of duals, and thus over the size of the dual array? In particular, I ended up with uninitialized values with the original implementation, which can give you nasty wrong values.

michel2323 avatar Jan 21 '20 19:01 michel2323