Issues when using Turing with DynamicHMC

Hi,

when I try to run the DynamicNUTS example from the documentation upon loading the libraries (and having installed current versions) the below issues occur:

Warning: Error requiring LogDensityProblems from Turing:
UndefVarError: AbstractLogDensityProblem not defined

as well as:

Warning: Error requiring DynamicHMC from Turing:
UndefVarError: NUTS_init_tune_mcmc not defined

This occured on two separate computers (both running windows 10) on a fresh install of julia.

Am I doing something wrong or did something break?

All the best and thanks,
Jakob

Tamas updated DynamicHMC a little while ago, and it broke Turing’s interface to it. I thought we had prevented these kinds of errors, though – could you print out the results of ] status so I can see the version of everything?

As a side note, Turing uses the excellent AdvancedHMC as it’s default HMC backend. It provides NUTS, HMCDA, and HMC samplers, which you can use via

chain = sample(model, HMC(0.01, 7), 1000)
chain = sample(model, NUTS(), 1000)
chain = sample(model, HMCDA(200, 0.65, 0.3), 1000)

Hi Cameron,

thanks for the quick reply.
I didn’t know about the AdvancedHMC stuff, I think that wasn’t there when I last looked at Turing. I’ll give it a shot right away :slight_smile:

Here’s the status print:

  [c52e3926] Atom v0.11.3
  [bbc10e6e] DynamicHMC v2.1.0        
  [e5e0dc1b] Juno v0.7.2
  [6fdf6af0] LogDensityProblems v0.9.1
  [ce6b1742] RDatasets v0.6.4
  [f3b207a7] StatsPlots v0.12.0
  [fce5fe82] Turing v0.7.2
  [e88e6eb3] Zygote v0.3.4

Best
Jakob

All changes to the DynamicHMC API follow SemVer; if there is one that didn’t please open an issue.

@Tamas_Papp would you be willing to have a look at the current inference implemented between Turing and DynamicHMC? I’m sure you will be much fast than anyone from the Turing team to figure out what has to be done.

https://github.com/TuringLang/Turing.jl/blob/master/src/contrib/inference/dynamichmc.jl

I think it would be great if we could continue supporting DynamicHMC as a backend.

See

https://github.com/TuringLang/Turing.jl/pull/960

but the Turing test suite seems to be broken, so I could not run all the tests. The DynamicHMC part works now.

3 Likes

Thanks a lot for the PR.

This test:

Import Turing and DynamicHMC.

compatible with Julia 1.9.4

Fails on Julia 10, reason unknown or due to globals

using DynamicHMC, Turing

Model definition.

@model function gdemo(x, y)
s² ~ InverseGamma(2, 3)
m ~ Normal(0, sqrt(s²))
x ~ Normal(m, sqrt(s²))
y ~ Normal(m, sqrt(s²))
end

Pull 2,000 samples using DynamicNUTS.

chn = sample(gdemo(1.5, 2.0), DynamicNUTS(), 2000)
Chains MCMC chain (2000×3×1 Array{Float64, 3}):

Iterations = 1:1:2000
Number of chains = 1
Samples per chain = 2000
Wall duration = 12.36 seconds
Compute duration = 12.36 seconds
parameters = s², m
internals = lp

Summary Statistics
parameters mean std mcse ess_bulk ess_tail rhat ess_per_sec
Symbol Float64 Float64 Float64 Float64 Float64 Float64 Float64

      s²    2.1124    1.9361    0.0791   743.1429   822.0729    0.9999       60.1102
       m    1.1679    0.8720    0.0331   757.1192   605.4845    1.0101       61.2407

Quantiles
parameters 2.5% 25.0% 50.0% 75.0% 97.5%
Symbol Float64 Float64 Float64 Float64 Float64

      s²    0.5705    1.0455    1.5578    2.4618    7.7490
       m   -0.5559    0.6403    1.1610    1.6665    2.8760

Run fine on Julia 1.9.4 but fails on Julia 1.10

The same response I got 5 years ago should probably still apply: You could try using the NUTS sampler that comes with the Turing ecosystem instead of the one in DynamicHMC. So basically this should do the trick:

using Turing

@model function gdemo(x, y)
  s² ~ InverseGamma(2, 3)
  m ~ Normal(0, sqrt(s²))
  x ~ Normal(m, sqrt(s²))
  y ~ Normal(m, sqrt(s²))
end

chn = sample(gdemo(1.5, 2.0), NUTS(), 2000)

Or is there a specific reason you want to use the one in DynamicHMC?

Thank you for your suggestion, it works. DynamicHMC is an excellent robust package, too bad that Turing interface to DynamicHMC is not compatible.