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

Simple VarInfo illegal instruction #2163

Open
willtebbutt opened this issue Jan 29, 2024 · 0 comments
Open

Simple VarInfo illegal instruction #2163

willtebbutt opened this issue Jan 29, 2024 · 0 comments

Comments

@willtebbutt
Copy link
Collaborator

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
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