Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type Analysis Error and Segfault for Data structs with Tuples and Arrays in them in Julia 1.11, but not Julia 1.10 #2295

Open
maximilian-gelbrecht opened this issue Feb 3, 2025 · 0 comments

Comments

@maximilian-gelbrecht
Copy link

This came up while working on applying Enzyme to SpeedyWeather.jl

Sorry, for the lengthy title, not sure how to better put this into words.

As a MWE, without our data structures from there:

import Pkg 
Pkg.activate("test")
using Enzyme

@kwdef struct StructA{A<:AbstractArray, B<:AbstractArray} 
    vor::NTuple{2,A} = ntuple(i -> zeros(20,20), 2)
    div::NTuple{2,A} = ntuple(i -> zeros(20,20), 2)
    pres::NTuple{2,B} = ntuple(i -> zeros(20), 2)
    other::B = zeros(20)
    other_int::Int = 0
end  

@kwdef struct StructB{A<:AbstractArray, B<:AbstractArray} 
    vor_tend::A = zeros(20,20)
    div_tend::A = zeros(20,20)
    pres_tend::B = zeros(20)
    other::B = zeros(20)
    other_int::Int = 0
end 

function lf2!(
    progn::StructA,
    tend::StructB)

    for (varname, tendname) in zip((:vor,:div,:pres), (:vor_tend,:div_tend,:pres_tend))
        var_old, var_new = getfield(progn, varname)
        var_tend = getfield(tend, tendname)
        compute_something!(var_old, var_new, var_tend)
    end

    return nothing
end

function compute_something!(A, B, C)
    A .+= C 
    B .-= C 
    return nothing 
end

A = StructA()
dA = StructA(vor=ntuple(i -> ones(20,20), 2), div=ntuple(i -> ones(20,20), 2))
B = StructB()
dB = make_zero(B)

autodiff(Reverse, lf2!, Const, Duplicated(A, dA), Duplicated(B, dB))

With Enzyme in Julia 1.10 this works, with Enzyme in Julia 1.11 this first got me some type analysis errors, and now in this exact version a seg fault.

With simpler data structures, that still contain different array types (like in StructB), but not nested within tuples as above in StructA it also works.

There are ways I could make the code of lf2! a bit more explicit and avoid doing it like above, but it does already work just fine with Enzyme in 1.10 like it is, just not with Julia 1.11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant