RuntimeError: empty_nested() must be true before calling recover_memory()



Hi, stan expert:
I encountered an error saying “RuntimeError: empty_nested() must be true before calling recover_memory()”. Not sure how to deal with it.

Below is my system setting:
python 3.6.1
OS X EI Capitan 10.11.6

below is my error. I am not sure what does the model means. It is because something wrong in the model file??

In [8]: run
INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_e65a73d37757b30978f0832f795f847a NOW.
empty_nested() must be true before calling recover_memory()
RuntimeError                              Traceback (most recent call last)
~/Dropbox/Experiments/VPA_code/VPA_pystan/ in <module>()
     42 sm = pystan.StanModel(file='VPA.stan')  # VPA model
---> 43 fit = sm.sampling(data=data, pars=watch_list, iter=nIter, warmup=nWarmup, chains=nChains, init=init0)

~/anaconda/lib/python3.6/site-packages/pystan/ in sampling(self, data, pars, chains, iter, warmup, thin, seed, init, sample_file, diagnostic_file, verbose, algorithm, control, n_jobs, **kwargs)
    724         call_sampler_args = izip(itertools.repeat(data), args_list, itertools.repeat(pars))
    725         call_sampler_star = self.module._call_sampler_star
--> 726         ret_and_samples = _map_parallel(call_sampler_star, call_sampler_args, n_jobs)
    727         samples = [smpl for _, smpl in ret_and_samples]

~/anaconda/lib/python3.6/site-packages/pystan/ in _map_parallel(function, args, n_jobs)
     84             pool.join()
     85     else:
---> 86         map_result = list(map(function, args))
     87     return map_result

stanfit4anon_model_e65a73d37757b30978f0832f795f847a_6566696113056553208.pyx in stanfit4anon_model_e65a73d37757b30978f0832f795f847a_6566696113056553208._call_sampler_star()

stanfit4anon_model_e65a73d37757b30978f0832f795f847a_6566696113056553208.pyx in stanfit4anon_model_e65a73d37757b30978f0832f795f847a_6566696113056553208._call_sampler()

RuntimeError: empty_nested() must be true before calling recover_memory()

Here is my model. It is a little bit complicated but I debug with printing values. It seems OK.

    real errorProb_lpdf(vector y, vector kappa, real kappa_r){
        // y: data vector
        // kappa
        // kappa_r

        vector[num_elements(y)] tmp;
        real lprob;

        tmp = sqrt(square(kappa) + kappa_r^2 + 2 * kappa_r * kappa .* cos(y));
        lprob = 0;
        for (i in 1:num_elements(y)){
            lprob += log((modified_bessel_first_kind(0, tmp[i])./(modified_bessel_first_kind(0, kappa[i])*modified_bessel_first_kind(0, kappa_r))));
        return lprob;

    int find_idx(real x, vector y) {
        // return the index of an nearest nabour of x in y
        // x: a J value
        // y: J_map
        int index;
        int n = 1;
        real tol = max(y);
        for (i in 1:rows(y)) {
          if (fabs(x-y[i])<tol) {
            tol = fabs(x-y[i]);
            index = n;
            n = n + 1;
        return index;

    int map_number;
    int nTrial;
    int N_level[nTrial];
    vector<lower=-pi()/2, upper=pi()/2>[nTrial] error;
    vector[map_number] kappa_map;
    // we pre calculate and input the kappa_map and J_map
    vector[map_number] J_map;
    real<lower=0.0001, upper=100> J1;
    real<lower=-5, upper=5> power;
    real<lower=0.0001,upper=100> tau;
    real<lower=0.0001,upper=100> kappa_r;
    vector<lower=0.0001, upper=700>[nTrial] J;
    vector[nTrial] kappa;
    vector[nTrial] lprob_gamma;
    real Jbar;

    for (i in 1:nTrial){
        Jbar = J1 * pow(N_level[i], power);
        J[i] ~ gamma(Jbar*tau, 1/tau);
        kappa[i] = kappa_map[find_idx(J[i], J_map)];
        lprob_gamma[i] = gamma_lpdf(J[i] | Jbar*tau, 1/tau);
    error ~ errorProb(kappa, kappa_r);
generated quantities{
    // compute overall_likelihood
    vector[nTrial] lprob_gamma;
    vector[nTrial] kappa;
    real loglikeli;

    loglikeli = sum(lprob_gamma) + errorProb_lpdf(error | kappa, kappa_r);


What is your gcc / clang version?

gcc --version

Have you tried to run the code excluding the generated quantities block?

That error message is coming from C++. Not sure why, but I could guess that some vector/matrix is ill-defined.