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

Segfault on Turing model reduced #2197

Open
mhauru opened this issue Dec 12, 2024 · 0 comments
Open

Segfault on Turing model reduced #2197

mhauru opened this issue Dec 12, 2024 · 0 comments

Comments

@mhauru
Copy link
Contributor

mhauru commented Dec 12, 2024

Hi @wsmoses, as discussed on Slack, I reduced the case where the MWE of #1769 now segfaults. It's not the most minimal, and I don't think I'll have time before January to reduce it further, but it no longer depends on anything in TuringLang.

module MWE

using Distributions
using Enzyme
using Random

Random.seed!(1)

struct Metadata{
    TIdcs<:Dict{Symbol,Int},
    TVN<:AbstractVector{Symbol},
    TVal<:AbstractVector{<:Real},
}
    idcs::TIdcs
    vns::TVN
    ranges::Vector{UnitRange{Int}}
    vals::TVal
end

function Metadata()
    vals = Vector{Real}()

    return Metadata(
        Dict{Symbol,Int}(),
        Vector{Symbol}(),
        Vector{UnitRange{Int}}(),
        vals,
    )
end

function Base.push!(meta::Metadata, vn, val)
    meta.idcs[vn] = length(meta.idcs) + 1
    push!(meta.vns, vn)
    l = length(meta.vals)
    n = length(val)
    push!(meta.ranges, (l + 1):(l + n))
    append!(meta.vals, val)
    return meta
end

function replace_values(metadata::Metadata, x)
    return Metadata(
        metadata.idcs,
        metadata.vns,
        metadata.ranges,
        x,
    )
end

function data_poly(x, degree)
    return hcat([x .^ d for d in 1:degree]...)
end

# Having the third, useless argument seems significant for reproducing the segfault.
function observe(right::Distribution, left, vi)
    return Distributions.loglikelihood(right, left)
end

function model(__varinfo__)
    y = rand(Normal(0, 1), 1000)
    x = y .+ rand(Normal(0, 1.5), 1000)
    X = data_poly(x, 1)
    for i = eachindex(y)
        d2 = Normal(X[i, 1], 1.0)
        _ = observe(d2, y[i], __varinfo__)
    end
    return (0.0, __varinfo__)
end

vi = Metadata()
vn1 = :a
vi = push!(vi, vn1, 0.0)

function ldf(x)
    vi_tmp = replace_values(vi, x)
    return model(vi_tmp)
end
for i in 1:10000
    x = [randn()]
    Enzyme.gradient(Enzyme.set_runtime_activity(Enzyme.Forward), ldf, x)
end


end

Output, which seems to be indeterministic:

[63386] signal (11.2): Segmentation fault: 11
in expression starting at /Users/mhauru/projects/Enzyme-mwes/segfault_from_slowdown/mwe.jl:78
gc_mark_obj8 at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/gc.c:0
gc_mark_outrefs at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/gc.c:2634 [inlined]
gc_mark_loop_serial_ at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/gc.c:2690
gc_mark_loop_serial at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/gc.c:2713
_jl_gc_collect at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/gc.c:3227
ijl_gc_collect at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/gc.c:3524
maybe_collect at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/gc.c:937 [inlined]
jl_gc_pool_alloc_inner at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/gc.c:1293 [inlined]
ijl_gc_pool_alloc at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/gc.c:1341
model at /Users/mhauru/projects/Enzyme-mwes/segfault_from_slowdown/mwe.jl:65 [inlined]
model at /Users/mhauru/projects/Enzyme-mwes/segfault_from_slowdown/mwe.jl:0 [inlined]
fwddiffejulia_model_1098_inner_1wrap at /Users/mhauru/projects/Enzyme-mwes/segfault_from_slowdown/mwe.jl:0
macro expansion at /Users/mhauru/.julia/packages/Enzyme/haqjK/src/compiler.jl:5204 [inlined]
enzyme_call at /Users/mhauru/.julia/packages/Enzyme/haqjK/src/compiler.jl:4750 [inlined]
ForwardModeThunk at /Users/mhauru/.julia/packages/Enzyme/haqjK/src/compiler.jl:4638 [inlined]
runtime_generic_fwd at /Users/mhauru/.julia/packages/Enzyme/haqjK/src/rules/jitrules.jl:303
unknown function (ip: 0x132b3c1e3)
_jl_invoke at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/gf.c:0 [inlined]
ijl_apply_generic at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/gf.c:3077
ldf at /Users/mhauru/projects/Enzyme-mwes/segfault_from_slowdown/mwe.jl:76 [inlined]
fwddiffejulia_ldf_390wrap at /Users/mhauru/projects/Enzyme-mwes/segfault_from_slowdown/mwe.jl:0
macro expansion at /Users/mhauru/.julia/packages/Enzyme/haqjK/src/compiler.jl:5204 [inlined]
enzyme_call at /Users/mhauru/.julia/packages/Enzyme/haqjK/src/compiler.jl:4750 [inlined]
ForwardModeThunk at /Users/mhauru/.julia/packages/Enzyme/haqjK/src/compiler.jl:4638 [inlined]
autodiff at /Users/mhauru/.julia/packages/Enzyme/haqjK/src/Enzyme.jl:654 [inlined]
autodiff at /Users/mhauru/.julia/packages/Enzyme/haqjK/src/Enzyme.jl:524 [inlined]
macro expansion at /Users/mhauru/.julia/packages/Enzyme/haqjK/src/sugar.jl:675 [inlined]
#gradient#126 at /Users/mhauru/.julia/packages/Enzyme/haqjK/src/sugar.jl:577
unknown function (ip: 0x12f4d413f)
_jl_invoke at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/gf.c:0 [inlined]
ijl_apply_generic at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/gf.c:3077
gradient at /Users/mhauru/.julia/packages/Enzyme/haqjK/src/sugar.jl:577
top-level scope at /Users/mhauru/projects/Enzyme-mwes/segfault_from_slowdown/mwe.jl:80
jl_toplevel_eval_flex at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/toplevel.c:925
jl_eval_module_expr at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/toplevel.c:215 [inlined]
jl_toplevel_eval_flex at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/toplevel.c:736
jl_toplevel_eval_flex at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/toplevel.c:877
ijl_toplevel_eval at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/toplevel.c:943 [inlined]
ijl_toplevel_eval_in at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/toplevel.c:985
eval at ./boot.jl:385 [inlined]
include_string at ./loading.jl:2076
_jl_invoke at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/gf.c:0 [inlined]
ijl_apply_generic at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/gf.c:3077
_include at ./loading.jl:2136
include at ./Base.jl:495
jfptr_include_46623.1 at /Users/mhauru/.julia/juliaup/julia-1.10.6+0.aarch64.apple.darwin14/lib/julia/sys.dylib (unknown line)
_jl_invoke at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/gf.c:0 [inlined]
ijl_apply_generic at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/gf.c:3077
exec_options at ./client.jl:323
_start at ./client.jl:557
jfptr__start_83055.1 at /Users/mhauru/.julia/juliaup/julia-1.10.6+0.aarch64.apple.darwin14/lib/julia/sys.dylib (unknown line)
_jl_invoke at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/gf.c:0 [inlined]
ijl_apply_generic at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/gf.c:3077
jl_apply at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/./julia.h:1982 [inlined]
true_main at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/jlapi.c:582
jl_repl_entrypoint at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/jlapi.c:731
Allocations: 61029141 (Pool: 60977960; Big: 51181); GC: 54
Segmentation fault: 11

Enzyme v0.13.22, Julia v1.10.6.

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