TuringLang / Turing.jl

Bayesian inference with probabilistic programming.
https://turinglang.org
MIT License
2.04k stars 219 forks source link

Simple VarInfo illegal instruction #2163

Closed willtebbutt closed 3 weeks ago

willtebbutt commented 9 months ago

As discussed with @torfjelde on slack:

using Distributions, Turing

# LDA example -- copied over from
# https://github.com/TuringLang/Turing.jl/issues/668#issuecomment-1153124051
function _make_data(D, K, V, N, α, η)
    β = Matrix{Float64}(undef, V, K)
    for k in 1:K
        β[:,k] .= rand(Dirichlet(η))
    end

    θ = Matrix{Float64}(undef, K, D)
    z = Vector{Int}(undef, D * N)
    w = Vector{Int}(undef, D * N)
    doc = Vector{Int}(undef, D * N)
    i = 0
    for d in 1:D
        θ[:,d] .= rand(Dirichlet(α))
        for n in 1:N
            i += 1
            z[i] = rand(Categorical(θ[:, d]))
            w[i] = rand(Categorical(β[:, z[i]]))
            doc[i] = d
        end
    end
    return (D=D, K=K, V=V, N=N, α=α, η=η, z=z, w=w, doc=doc, θ=θ, β=β)
end

data = let D = 2, K = 2, V = 160, N = 290
    _make_data(D, K, V, N, ones(K), ones(V))
end

# LDA with vectorization and manual log-density accumulation
@model function LatentDirichletAllocationVectorizedCollapsedMannual(
    D, K, V, α, η, w, doc
)
    β ~ filldist(Dirichlet(η), K)
    θ ~ filldist(Dirichlet(α), D)

    log_product = log.(β * θ)
    Turing.@addlogprob! sum(log_product[CartesianIndex.(w, doc)])
    # Above is equivalent to below
    #product = β * θ
    #dist = [Categorical(product[:,i]) for i in 1:D]    
    #w ~ arraydist([dist[doc[i]] for i in 1:length(doc)])
end

model = LatentDirichletAllocationVectorizedCollapsedMannual(
    data.D, data.K, data.V, data.α, data.η, data.w, data.doc,
)

ctx = Turing.DefaultContext()
vi = Turing.SimpleVarInfo(model)
vi_linked = Turing.link(vi, model)

the final line yields

[1]    38239 illegal hardware instruction  julia

Run on Julia v1.10.0, with

  [31c24e10] Distributions v0.25.107
  [fce5fe82] Turing v0.30.3
penelopeysm commented 1 month ago

I can't reproduce this on my M1 Mac:

julia> vi_linked = Turing.link(vi, model)
Transformed SimpleVarInfo((β = [-0.5586704534964646 0.4449257033648504; 0.49462131524462283 -4.267434908105872; … ; 0.5517609535028092 -2.945874650059821; 2.082444142385803 0.6952883449480989], θ = [-1.8888464575737707 -3.09863439109128]), -3696.582422864908)

julia> versioninfo()
Julia Version 1.10.0
Commit 3120989f39b (2023-12-25 18:01 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 10 × Apple M1 Pro
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, apple-m1)
  Threads: 1 on 8 virtual cores

(ppl) pkg> st
Status `~/ppl/Project.toml`
⌃ [31c24e10] Distributions v0.25.107
⌃ [fce5fe82] Turing v0.30.3

It runs successfully with Julia 1.10.5 and the latest versions of Turing/Distributions, too.

Is this OS- or architecture-specific?

penelopeysm commented 1 month ago

Norepro on Intel macOS too. I don't have any other laptops to hand 😄

julia> vi_linked = Turing.link(vi, model)
Transformed SimpleVarInfo((β = [-1.8679219824723639 0.04287603501415216; 0.4508670007190849 1.027370357861928; … ; 0.4085340253630684 0.8295277069346981; 0.022551723323387833 0.7550515967305788], θ = [1.3985778149898214 0.8668408166024291]), -3640.0134613280497)

julia> versioninfo()
Julia Version 1.10.0
Commit 3120989f39b (2023-12-25 18:01 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (x86_64-apple-darwin22.4.0)
  CPU: 12 × Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, skylake)
  Threads: 1 on 12 virtual cores

(test) pkg> st
Status `~/test/Project.toml`
⌃ [31c24e10] Distributions v0.25.107
⌃ [fce5fe82] Turing v0.30.3
Info Packages marked with ⌃ have new versions available and may be upgradable.
yebai commented 1 month ago

@penelopeysm, feel free to close it. It's okay to reopen it if this issue is observed again.

willtebbutt commented 3 weeks ago

Also norepro on my mac. Closing. HOpefully it will never re-emerge.