Enzyme.jl
Enzyme.jl copied to clipboard
including LoopVectorization causes enzyme to segfault or mutate inputs its not supposed to
importing LoopVectorization or importing any package that imports it will cause the following code to segfault or it mutates res2 in this example (it shouldn't)
using LoopVectorization
using Enzyme
mutable struct SS
dim::Int
repart::UnitRange
impart::UnitRange
end
SS(n) = SS(n,1:n,n+1:2n)
function loss(u,res)
return sum(u[1:10].^2 .+ u[11:20].^2 .- res)
end
function loss2(sys,u,res)
return sum( u[sys.repart].^2 .+ u[sys.impart].^2 .- res )
end
sys2 = SS(10)
res = rand(sys2.dim)
res2 = deepcopy(res)
u = rand(2*sys2.dim)
out = zero(u)
display(res)
out.=0
autodiff(Reverse,loss,Active,Duplicated(u,out),Const(res))
display("---")
display(res)
out.=0
autodiff(Reverse,loss2,Active,Const(sys2),Duplicated(u,out),Const(res2))
display("---")
display(res2)
Does LV change anything about sum?
I think broadcasting is changed, slightly smaller MWE:
using LoopVectorization
using Enzyme
mutable struct SS
repart::UnitRange
end
function loss2(sys,u,res)
m5 = Base.broadcasted(+, Base.getindex(u, sys.repart), res)
# m5 = Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, typeof(+), Tuple{Vector{Float64}, Vector{Float64}}}(+, (Base.getindex(u, sys.repart), res), nothing)
return @inbounds iterate( m5 )[1]::Float64
end
sys = SS(1:4)
res = [1., 3., 5., 7.]
u = [2., 4., 6., 8.]
out=zero(res)
autodiff(Reverse,loss2,Active,Const(sys),Duplicated(u,out),Const(res))
@show res
@show u
@show out
Problem is not throwing a mismatched activity error on an insert value for a constant inserted pointer.
A simpler, forward mode, for example:
julia> Enzyme.autodiff(Forward, (x,y)->(x,y), Duplicated(u, out), Const(res))
Fixed on main by jll bump