stan-dev / stan-dev/rstan

strange combination of things causes a Segmentation fault (core dumped)

Open
#167 17 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
1.1k
Forks
266
Avg merge
2h 56m
Merged PRs (30d)
1

Description

I have a very complex model which runs in a server and sends me emails when it's done using mailR library in R. It was working fine, but then for a dataset such as this (which I reduced to the minimum here) it caused R to crash with segmentation fault exit. Now, if I use half of the data, the code works, if I remove the generated quantities, it works, and if I don't load mailR it works!

So I tried to reduce the code to the bare minimum that causes the problem (the model doesn't make much sense like this, but it should work; the data is available from the link above) .
Also with this code, it crashes with Segmentation fault, unless I either remove mailR, reduce the data, or very wierdly I remove one out of the three variables that are initialized generated quantities.

So I can live without sending emails from the server, but I think this behavior is really strange, and I guess someone should take a look to see what's happening...

set.seed(123)

library(rstan)
library(mailR)



niter <- 2#1000
chains <- 1#8

load("d.Rda")


model <- "
data {
  int<lower=0> N_obs; 
  int<lower=1> subj[N_obs];    //subject id
  int<lower=1> N_subj;                 //number of subjects
  vector[N_obs] rt;
}


parameters {
  cholesky_factor_corr[2] L_u; 
  vector[2] z_u[N_subj];
  vector<lower=0> [2]  tau_u;     
  real<lower=0> alpha_mu; 
  real<lower=0> alpha_sigma; 
  real<lower=0 > alpha_psi; 
}


transformed parameters {
  matrix[2,2] Lambda_u;
  vector[2]  u[N_subj];         
  real  u_mu[N_subj];         // random intercept and slopes subj
  real  u_sigma[N_subj];         // random intercept and slopes subj

  Lambda_u <- diag_pre_multiply(tau_u,L_u);

  for (i in 1:N_subj){
     u[i] <- Lambda_u * z_u[i];
    u_mu[i] <- u[i][1];
    u_sigma[i] <- u[i][2];

 }




} 

model {
  real psi[N_obs];
  real mu[N_obs];
  real sigma[N_obs];

  alpha_mu ~ normal(0, 10);
  tau_u ~ cauchy(0,100);
  alpha_sigma ~ normal(0, 1);
  alpha_psi ~ normal(0, 250);
  L_u ~ lkj_corr_cholesky(4.0);

  for (i in 1:N_subj){
    z_u[i] ~ normal(0,1);   
  }

 for (i in 1:N_obs){
     mu[i] <- alpha_mu + u_mu[subj[i]] ; 
    sigma[i] <- alpha_sigma + u_sigma[subj[i]]; 
    psi[i] <- alpha_psi; 

  (rt[i]- psi[i])~ lognormal( mu[i], sigma[i]);
  }
}

 generated quantities {
   real A[N_obs];
  real B[N_obs];
  real C[N_obs];
  }
"
ls <- list(
        subj=as.numeric(as.character(d$subj)),
        N_obs=nrow(d),
        N_subj=length(unique(as.numeric(as.character(d$subj)))),
rt= round(d$rt,0)
)



sample <- stan(model_code=model,    
                data=ls, 
                iter=niter,

                init=0,
                chains=chains
                )

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the inline rstan::stan call, the supplied model string, and d.Rda, then run the minimal reproduction while varying mailR, the dataset size, and generated quantities as described. Trace the crash from the reproducible script into the RStan sampling entry point. Done means the minimal case no longer causes a segmentation fault, or the issue is narrowed to a documented component interaction.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
backend
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.