Cmdstanr: Issues with specifying init file (relative paths, warning with multiple chains, should broadcast init argument)

Created on 4 Nov 2019  路  4Comments  路  Source: stan-dev/cmdstanr

I'd be good to be able to use relative paths when passing files to init. Right now if you do something like:

model = cmdstan_model("/home/bbales2/cmdstan-1246/examples/bernoulli/bernoulli.stan", quiet = FALSE)

init_d = list(theta = 1)
stan_rdump(names(init_d), "init.dat", envir = list2env(init_d))

fit = model2$sample(data = list(N = 1, y = 1),
                    num_chains = 1,
                    init = "init.dat")

You'll get:

Running MCMC sampling with 1 chain(s)...
Running ./normal 'id=1' random 'seed=10954025' 'init=init.dat' data 'file=/tmp/RtmpPgyXnL/standata-326050dfd6b.json' \
  output 'file=/tmp/RtmpPgyXnL/normal-stan-sample-1.csv' 'method=sample' 'save_warmup=0' 'algorithm=hmc' \
  'engine=nuts' adapt 'engaged=1'
Chain 1 finished unexpectedly!

Also if you do multiple chains:

fit = model2$sample(data = list(N = 1, y = 1),
                    num_chains = 2,
                    init = rep("/home/bbales2/cmdstanr/init.dat", 2))

You get a warning that looks like it probably needs fixed:

Chain 2 finished in 0.0 seconds.
Both chains finished succesfully.
Mean execution time: 0.0 secondsWarning message:
In if (substr(path, nchar(path), nchar(path)) == "/") { :
  the condition has length > 1 and only the first element will be used

Finally if I only provide one init file I think it makes sense to broadcast it out:

fit = model2$sample(data = list(N = 1, y = 1),
                    num_chains = 2,
                    init = "/home/bbales2/cmdstanr/init.dat")

Returns the error:

Error: If 'init' is specified as a character vector it must haveone element per chain.
bug

All 4 comments

It seems that you are running this on the parallel-cmdstanr branch. I am 90% sure that work is not the culprit here but if you have time can you please verify that this behavior also happens on master.

Good call. My session was all screwed up. Errors remain the same though:

model = cmdstan_model("/home/bbales2/cmdstan-1246/examples/bernoulli/bernoulli.stan", quiet = FALSE)

init_d = list(theta = 0.5)
stan_rdump(names(init_d), "init.dat", envir = list2env(init_d))

# Fails and shouldn't
fit = model$sample(data = list(N = 2, y = c(1, 0)),
                    num_chains = 1,
                    init = "init.dat")

# works 
fit = model$sample(data = list(N = 2, y = c(1, 0)),
                    num_chains = 1,
                    init = "/home/bbales2/cmdstanr/init.dat")

# Throws warning at the end
fit = model$sample(data = list(N = 2, y = c(1, 0)),
                    num_chains = 2,
                    init = rep("/home/bbales2/cmdstanr/init.dat", 2))

# It'd be convenient if this worked but maybe we don't want to broadcast init files (and that's fine if we don't)
fit = model$sample(data = list(N = 2, y = c(1, 0)),
                    num_chains = 2,
                    init = "/home/bbales2/cmdstanr/init.dat")

My guess is that we currently dont prefix the path of the init file with the path of the current workspace path. The cmdstan executable gets "init.dat" which means it expects it
next to the exe file (which is in /home/bbales2/cmdstan-1246/examples/bernoulli/).

If you put init.dat there things will work, but that is not the solution we want to go with. Should be an easy fix.

Yeah @rok-cesnovar is probably right about the paths. Shouldn鈥檛 be hard to fix.

Regarding recycling init files, I agree that we should do that by default, but maybe we should throw a warning (or informational message) since it鈥檚 not advisable for the typical user to initialize the chains to the same values (and it could be easy to forget to specify num_chains files)?

Was this page helpful?
0 / 5 - 0 ratings