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

Dynamic models fail at chain concatenation #465

Open
mhauru opened this issue Nov 20, 2024 · 0 comments
Open

Dynamic models fail at chain concatenation #465

mhauru opened this issue Nov 20, 2024 · 0 comments

Comments

@mhauru
Copy link
Member

mhauru commented Nov 20, 2024

MWE from the Turing test suite, just run with multiple chains:

using Turing, Random
using Turing.RandomMeasures: DirichletProcess, ChineseRestaurantProcess
import ReverseDiff

Random.seed!(23)

@model function imm(y, alpha, ::Type{M}=Vector{Float64}) where {M}
    N = length(y)
    rpm = DirichletProcess(alpha)

    z = zeros(Int, N)
    cluster_counts = zeros(Int, N)
    fill!(cluster_counts, 0)

    for i in 1:N
        z[i] ~ ChineseRestaurantProcess(rpm, cluster_counts)
        cluster_counts[z[i]] += 1
    end

    Kmax = findlast(!iszero, cluster_counts)
    m = M(undef, Kmax)
    for k in 1:Kmax
        m[k] ~ Normal(1.0, 1.0)
    end
end

num_zs = 100
num_samples = 100
num_chains = 6
model = imm(Random.randn(num_zs), 1.0)
adbackend = AutoReverseDiff()
chn = sample(model, Gibbs(PG(10, :z), HMC(0.01, 4, :m; adtype=adbackend)), MCMCThreads(), num_samples, num_chains)

This fails with

Sampling (6 threads) 100%|█████████████████████████████████████████████████████| Time: 0:00:08
ERROR: ArgumentError: chain names differ
Stacktrace:
  [1] _cat(::Val{…}, c1::Chains{…}, args::Chains{…})
    @ MCMCChains ~/.julia/packages/MCMCChains/zFCJy/src/chains.jl:809
  [2] chainscat
    @ ~/.julia/packages/MCMCChains/zFCJy/src/chains.jl:753 [inlined]
  [3] _mapreduce(f::typeof(identity), op::typeof(chainscat), ::IndexLinear, A::Vector{Chains{…}})
    @ Base ./reduce.jl:440
  [4] _mapreduce_dim
    @ ./reducedim.jl:367 [inlined]
  [5] mapreduce
    @ ./reducedim.jl:359 [inlined]
  [6] reduce
    @ ./reducedim.jl:408 [inlined]
  [7] chainsstack(c::Vector{Chains{Union{…}, AxisArrays.AxisArray{…}, Missing, @NamedTuple{…}, @NamedTuple{…}}})
    @ AbstractMCMC ~/.julia/packages/AbstractMCMC/FSyVk/src/interface.jl:20
  [8] mcmcsample(rng::TaskLocalRNG, model::DynamicPPL.Model{…}, sampler::DynamicPPL.Sampler{…}, ::MCMCThreads, N::Int64, nchains::Int64; progress::Bool, progressname::String, initial_params::Nothing, initial_state::Nothing, kwargs::@Kwargs{})
    @ AbstractMCMC ~/.julia/packages/AbstractMCMC/FSyVk/src/sample.jl:481
  [9] sample(rng::TaskLocalRNG, model::DynamicPPL.Model{…}, sampler::DynamicPPL.Sampler{…}, ensemble::MCMCThreads, N::Int64, n_chains::Int64; chain_type::Type, progress::Bool, kwargs::@Kwargs{})
    @ Turing.Inference ~/.julia/packages/Turing/Z4MFH/src/mcmc/Inference.jl:348
 [10] sample
    @ ~/.julia/packages/Turing/Z4MFH/src/mcmc/Inference.jl:337 [inlined]
 [11] #sample#6
    @ ~/.julia/packages/Turing/Z4MFH/src/mcmc/Inference.jl:332 [inlined]
 [12] sample
    @ ~/.julia/packages/Turing/Z4MFH/src/mcmc/Inference.jl:321 [inlined]
 [13] #sample#5
    @ ~/.julia/packages/Turing/Z4MFH/src/mcmc/Inference.jl:316 [inlined]
 [14] sample(model::DynamicPPL.Model{…}, alg::Gibbs{…}, ensemble::MCMCThreads, N::Int64, n_chains::Int64)
    @ Turing.Inference ~/.julia/packages/Turing/Z4MFH/src/mcmc/Inference.jl:308
 [15] top-level scope
    @ REPL[37]:1
Some type information was truncated. Use `show(err)` to see complete types.

At the very least this should fail more informatively.

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