RStan crash question

243 views
Skip to first unread message

Bob Carpenter

unread,
Jan 4, 2015, 10:35:30 PM1/4/15
to stan-...@googlegroups.com
This was sent to me and the author said I could forward the issue,
since I didn't immediately know what's up.

Is this just another lib issue? It followed an update or R, rstan, etc.
(see the message)

- Bob

...

> I’ve been learning Stan lately, and am really liking it. I’m stumped on something, though. I have some code which I swear worked 2 weeks ago. Now I’ve updated R, rstan, etc., and for some reason I’m getting a failure (on my mac, and I also get the same error on a Ubuntu server). Find the code below. It’s a very vanilla Poisson regression. Strangely, the code works great with “stan”, but fails with “optimizing”. Any ideas? Am I making a stupid error?
>
...
>
>
> rm(list=ls())
> library(rstan)
>
> stan_code <- '
> data {
> int<lower=1> n_train_data;
> int<lower=1> n_vars;
> int<lower=0> y_train[n_train_data];
> matrix[n_train_data, n_vars] X_train;
> vector[n_vars] prior_mean;
> matrix[n_vars, n_vars] prior_cov;
> }
> parameters {
> vector[n_vars] beta;
> }
> model {
> beta ~ multi_normal(prior_mean, prior_cov);
> y_train ~ poisson_log(X_train*beta);
> }
> '
>
> X_train <- matrix(.1*rnorm(10*1000), 1000, 10)
> y_train <- rpois(1000, lambda = as.vector(exp(X_train %*%1:10/2)))
>
> prior_mean <- rep(0,10)
> prior_cov <- as.matrix(diag(10))
> stan_model_params <- list(prior_mean = prior_mean, prior_cov = prior_cov)
> stan_data_details <- list(n_train_data = dim(X_train)[1], n_vars = dim(X_train)[2])
>
> # This works fine
> stan_opt <- stan(model_code = stan_code, data = c(list(X_train = X_train, y_train = y_train), stan_model_params, stan_data_details), init="0", chains = 1, iter = 1000)
> plot(stan_opt)
>
> # This fails
> m <- stan_model(model_code = stan_code)
> stan_opt <- optimizing(m, data = c(list(X_train = X_train, y_train = y_train), stan_model_params, stan_data_details), init="0")
>
> # Here's the error message:
>
> # *** caught bus error ***
> # address 0x7fff7de53380, cause 'non-existent physical address'
> #
> # Traceback:
> # 1: .External(list(name = "CppMethod__invoke_notvoid", address = <pointer: 0x60000002b420>, dll = list(name = "Rcpp", path = "/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp/libs/Rcpp.so", dynamicLookup = TRUE, handle = <pointer: 0x600000145b70>, info = <pointer: 0x1003b4360>), numParameters = -1L), <pointer: 0x6000001e8f00>, <pointer: 0x600000236fc0>, .pointer, ...)
> # 2: sampler$call_sampler(c(args, dotlist))
> # 3: .local(object, ...)
> # 4: optimizing(m, data = c(test_data, stan_model_params, stan_data_details), init = "0")
> # 5: optimizing(m, data = c(test_data, stan_model_params, stan_data_details), init = "0")
> #
> # Possible actions:
> # 1: abort (with core dump, if enabled)
> # 2: normal R exit
> # 3: exit R without saving workspace
> # 4: exit R saving workspace
>
> # Further, here's the sessionInfo:
>
> # R version 3.1.2 (2014-10-31)
> # Platform: x86_64-apple-darwin10.8.0 (64-bit)
> #
> # locale:
> # [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
> #
> # attached base packages:
> # [1] stats graphics grDevices utils datasets methods base
> #
> # other attached packages:
> # [1] rstan_2.5.0 inline_0.3.13 Rcpp_0.11.3
> #
> # loaded via a namespace (and not attached):
> # [1] codetools_0.2-9 stats4_3.1.2
>
>
>

Ben Goodrich

unread,
Jan 5, 2015, 1:16:29 AM1/5/15
to stan-...@googlegroups.com
It is a bug. Here is the first part of the backtrace. All kinds of suspicious stuff going on here...

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000091 in ?? ()
(gdb) bt
#0  0x0000000000000091 in ?? ()
#1  0x00007ffff06fb059 in stan::agrad::recover_memory () at /usr/local/lib/R/site-library/rstan/include//stansrc/stan/agrad/rev/var_stack.hpp:59
#2  0x00007ffff0738fe2 in stan::model::log_prob_grad<true, false, model1e72583ff10_stan_code_namespace::model1e72583ff10_stan_code> (model=...,
    params_r
=std::vector of length 10, capacity 10 = {...}, params_i=std::vector of length 0, capacity 0, gradient=std::vector of length 10, capacity 10 = {...},
    msgs
=0x7ffff0ac76f0 <rstan::io::rcout>) at /usr/local/lib/R/site-library/rstan/include//stansrc/stan/model/util.hpp:108
#3  0x00007ffff07e0f71 in stan::optimization::ModelAdaptor<model1e72583ff10_stan_code_namespace::model1e72583ff10_stan_code>::operator() (this=0x7fffffff65f8, x=...,
    f
=@0x7fffffff6528: 2.374444316439047e-316, g=...) at /usr/local/lib/R/site-library/rstan/include//stansrc/stan/optimization/bfgs.hpp:357
#4  0x00007ffff07e93ae in stan::optimization::BFGSMinimizer<stan::optimization::ModelAdaptor<model1e72583ff10_stan_code_namespace::model1e72583ff10_stan_code>, stan::optimization::LBFGSUpdate<double, -1>, double, -1>::initialize (this=0x7fffffff64c0, x0=...) at /usr/local/lib/R/site-library/rstan/include//stansrc/stan/optimization/bfgs.hpp:148
#5  0x00007ffff07e92dc in stan::optimization::BFGSLineSearch<model1e72583ff10_stan_code_namespace::model1e72583ff10_stan_code, stan::optimization::LBFGSUpdate<double, -1>, double, -1>::initialize (this=0x7fffffff64c0, params_r=std::vector of length 10, capacity 10 = {...}) at /usr/local/lib/R/site-library/rstan/include//stansrc/stan/optimization/bfgs.hpp:417
#6  0x00007ffff0780054 in stan::optimization::BFGSLineSearch<model1e72583ff10_stan_code_namespace::model1e72583ff10_stan_code, stan::optimization::LBFGSUpdate<double, -1>, double, -1>::BFGSLineSearch (this=0x7fffffff64c0, model=..., params_r=std::vector of length 10, capacity 10 = {...}, params_i=std::vector of length 0, capacity 0, msgs=0x7ffff0ac76f0 <rstan::io::rcout>)
    at
/usr/local/lib/R/site-library/rstan/include//stansrc/stan/optimization/bfgs.hpp:409
#7  0x00007ffff06ffbb9 in rstan::(anonymous namespace)::sampler_command<model1e72583ff10_stan_code_namespace::model1e72583ff10_stan_code, boost::random::additive_combine_engine<boost::random::linear_congruential_engine<unsigned int, 40014u, 0u, 2147483563u>, boost::random::linear_congruential_engine<unsigned int, 40692u, 0u, 2147483399u> > > (args=..., model=..., holder=...,
    qoi_idx
=std::vector of length 11, capacity 16 = {...}, fnames_oi=warning: Type size unknown, assuming 1. Try casting to a known type, or void *.
warning
: Type size unknown, assuming 1. Try casting to a known type, or void *.
Python Exception <class 'gdb.error'> Cannot perform pointer math on incomplete type "std::basic_string<char, std::char_traits<char>, std::allocator<char> >", try casting to a known type, or void *.:
std
::vector of length 88, capacity 160, base_rng=...) at /usr/local/lib/R/site-library/rstan/include/rstan/stan_fit.hpp:720
#8  0x00007ffff070ee34 in rstan::stan_fit<model1e72583ff10_stan_code_namespace::model1e72583ff10_stan_code, boost::random::additive_combine_engine<boost::random::linear_congruential_engine<unsigned int, 40014u, 0u, 2147483563u>, boost::random::linear_congruential_engine<unsigned int, 40692u, 0u, 2147483399u> > >::call_sampler (this=0x2433e10, args_=0x2dd82d8)
    at
/usr/local/lib/R/site-library/rstan/include/rstan/stan_fit.hpp:1311
#9  0x00007ffff077d48c in Rcpp::CppMethod1<rstan::stan_fit<model1e72583ff10_stan_code_namespace::model1e72583ff10_stan_code, boost::random::additive_combine_engine<boost::random::linear_congruential_engine<unsigned int, 40014u, 0u, 2147483563u>, boost::random::linear_congruential_engine<unsigned int, 40692u, 0u, 2147483399u> > >, SEXPREC*, SEXPREC*>::operator() (this=0x920ee0,
   
object=0x2433e10, args=0x7fffffff76c0) at /usr/lib/R/site-library/Rcpp/include/Rcpp/module/Module_generated_CppMethod.h:111
#10 0x00007ffff071c59c in Rcpp::class_<rstan::stan_fit<model1e72583ff10_stan_code_namespace::model1e72583ff10_stan_code, boost::random::additive_combine_engine<boost::random::linear_congruential_engine<unsigned int, 40014u, 0u, 2147483563u>, boost::random::linear_congruential_engine<unsigned int, 40692u, 0u, 2147483399u> > > >::invoke_notvoid (this=0x24e0810, method_xp=0x20f4d10,
   
object=0x13f04e8, args=0x7fffffff76c0, nargs=1) at /usr/lib/R/site-library/Rcpp/include/Rcpp/module/class.h:234


Ben

Jiqiang Guo

unread,
Jan 5, 2015, 10:23:49 AM1/5/15
to stan-...@googlegroups.com
It is a bug of stan. I am attaching the standalone stan code and a sample data.

$ lldb ./test optimize data file=seg.Rdump
(lldb) target create "./test"
Current executable set to './test' (x86_64).
(lldb) settings set -- target.run-args "optimize" "data" "file=seg.Rdump"
(lldb) run
Process 7943 launched: './test' (x86_64)
method = optimize
optimize
algorithm = lbfgs (Default)
lbfgs
init_alpha = 0.001 (Default)
tol_obj = 9.9999999999999998e-13 (Default)
tol_rel_obj = 10000 (Default)
tol_grad = 1e-08 (Default)
tol_rel_grad = 10000000 (Default)
tol_param = 1e-08 (Default)
history_size = 5 (Default)
iter = 2000 (Default)
save_iterations = 0 (Default)
id = 0 (Default)
data
file = seg.Rdump
init = 2 (Default)
random
seed = 3252267060
output
file = output.csv (Default)
diagnostic_file = (Default)
refresh = 100 (Default)

Process 7943 stopped
* thread #1: tid = 0x651bc, 0x00000001000468dc test`double stan::model::log_prob_grad<true, false, test_model_namespace::test_model>(test_model_namespace::test_model const&, std::__1::vector<double, std::__1::allocator<double> >&, std::__1::vector<int, std::__1::allocator<int> >&, std::__1::vector<double, std::__1::allocator<double> >&, std::__1::basic_ostream<char, std::__1::char_traits<char> >*) + 636, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
frame #0: 0x00000001000468dc test`double stan::model::log_prob_grad<true, false, test_model_namespace::test_model>(test_model_namespace::test_model const&, std::__1::vector<double, std::__1::allocator<double> >&, std::__1::vector<int, std::__1::allocator<int> >&, std::__1::vector<double, std::__1::allocator<double> >&, std::__1::basic_ostream<char, std::__1::char_traits<char> >*) + 636
test`double stan::model::log_prob_grad<true, false, test_model_namespace::test_model>(test_model_namespace::test_model const&, std::__1::vector<double, std::__1::allocator<double> >&, std::__1::vector<int, std::__1::allocator<int> >&, std::__1::vector<double, std::__1::allocator<double> >&, std::__1::basic_ostream<char, std::__1::char_traits<char> >*) + 636:
-> 0x1000468dc: callq *0x8(%rax)
0x1000468df: movq (%r14), %rcx
0x1000468e2: movq 0x8(%r14), %rax
0x1000468e6: incq %rbx
(lldb) bt
* thread #1: tid = 0x651bc, 0x00000001000468dc test`double stan::model::log_prob_grad<true, false, test_model_namespace::test_model>(test_model_namespace::test_model const&, std::__1::vector<double, std::__1::allocator<double> >&, std::__1::vector<int, std::__1::allocator<int> >&, std::__1::vector<double, std::__1::allocator<double> >&, std::__1::basic_ostream<char, std::__1::char_traits<char> >*) + 636, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
* frame #0: 0x00000001000468dc test`double stan::model::log_prob_grad<true, false, test_model_namespace::test_model>(test_model_namespace::test_model const&, std::__1::vector<double, std::__1::allocator<double> >&, std::__1::vector<int, std::__1::allocator<int> >&, std::__1::vector<double, std::__1::allocator<double> >&, std::__1::basic_ostream<char, std::__1::char_traits<char> >*) + 636
frame #1: 0x000000010004634d test`stan::optimization::ModelAdaptor<test_model_namespace::test_model>::operator()(Eigen::Matrix<double, -1, 1, 0, -1, 1> const&, double&, Eigen::Matrix<double, -1, 1, 0, -1, 1>&) + 253
frame #2: 0x0000000100048c8b test`stan::optimization::BFGSMinimizer<stan::optimization::ModelAdaptor<test_model_namespace::test_model>, stan::optimization::LBFGSUpdate<double, -1>, double, -1>::initialize(Eigen::Matrix<double, -1, 1, 0, -1, 1> const&) + 347
frame #3: 0x0000000100048aee test`stan::optimization::BFGSLineSearch<test_model_namespace::test_model, stan::optimization::LBFGSUpdate<double, -1>, double, -1>::initialize(std::__1::vector<double, std::__1::allocator<double> > const&) + 270
frame #4: 0x00000001000486fe test`stan::optimization::BFGSLineSearch<test_model_namespace::test_model, stan::optimization::LBFGSUpdate<double, -1>, double, -1>::BFGSLineSearch(test_model_namespace::test_model&, std::__1::vector<double, std::__1::allocator<double> > const&, std::__1::vector<int, std::__1::allocator<int> > const&, std::__1::basic_ostream<char, std::__1::char_traits<char> >*) + 158
frame #5: 0x00000001000088cc test`int stan::common::command<test_model_namespace::test_model>(int, char const**) + 11612
frame #6: 0x0000000100003e92 test`main + 18
frame #7: 0x00007fff849445c9 libdyld.dylib`start + 1
frame #8: 0x00007fff849445c9 libdyld.dylib`start + 1
(lldb)


Jiqiang
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

test.stan
seg.Rdump

Bob Carpenter

unread,
Jan 5, 2015, 1:50:30 PM1/5/15
to stan-...@googlegroups.com
Thanks much for the reproducible example. I filed a Stan bug report:

https://github.com/stan-dev/stan/issues/1200

- Bob
> <test.stan><seg.Rdump>

Reply all
Reply to author
Forward
0 new messages