stan-dev / stan-dev/rstan

unconstrain_pars() crashes R

Open
#939 7 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

Summary:

unconstrain_pars causes segfault in R

Description:

When running unconstrain_pars on a newly fitted model, it causes a segfault and crashes R.

Reproducible Steps:

Standard eight schools model

data {
  int<lower=0> J;                                                                                                   
  real y[J];                                                                         
  real<lower=0> sigma[J];                                    
}
parameters {
  vector[J] theta_trans;                                                                                       
  real mu;                                                                                            
  real<lower=0> tau;                                                                                                
}
transformed parameters{
  vector[J] theta;                                                                                                             
  theta = theta_trans * tau + mu;
}
model {
  // priors                                                                                                                                       
  target += normal_lpdf(theta_trans | 0, 1);
  target += normal_lpdf(mu | 0, 5);
  target += cauchy_lpdf(tau | 0, 5);
  
  //likelihood                                                                                                                                    
  target += normal_lpdf(y | theta, sigma);
}

library(rstan)
model <- stan_model("eight_schools.stan")
d <- list(
  J = 8,
  y = c(28, 8, -3, 7, -1, 1, 18, 12),
  sigma = c(15, 10, 16, 11, 9, 11, 10, 18)
)
fit <- sampling(model, data = d)

unconstrain_pars(fit, pars = list(mu = 0, theta = rep(1.5, 8), tau = 2))
Current Output:
 *** caught segfault ***
 *** caught segfault ***
address (nil), cause 'memory not mapped'

Traceback:
 1: .External(list(name = "CppMethod__invoke_notvoid", address = <pointer:0x55a5f4813370>,     dll = list(name = "Rcpp", path =                  
"/lib/R/library/Rcpp/libs/Rcpp.so",         dynamicLookup = TRUE, handle = <pointer: 0x55a5f2b78520>, info = <pointer: 0x55a5f202dad0>),
numParameters = -1L),     <pointer: 0x55a5fb4cd110>, <pointer: 0x55a5f76d9760>, .pointer,     ...)
 2: object@.MISC$stan_fit_instance$unconstrain_pars(pars)
 3: .local(object, ...)
 4: unconstrain_pars(fit, pars = list(mu = 0, theta = rep(1.5, 8),     tau = 2))
 5: unconstrain_pars(fit, pars = list(mu = 0, theta = rep(1.5, 8),     tau = 2))
Expected Output:

Interestingly, the following works:

fit1 <- stan_demo("eight_schools")
unconstrain_pars(fit1, pars = list(mu = 0, theta = rep(1.5, 8), tau = 2))
[1] 0.0000000 1.5000000 1.5000000 1.5000000 1.5000000 1.5000000 1.5000000
 [8] 1.5000000 1.5000000 0.6931472
RStan Version:

2.26.1

R Version:

"R version 4.0.5 (2021-03-31)"

Operating System:

Ubuntu 18.04

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 RStan unconstrain_pars entry point shown in the traceback and compare its behavior with the working stan_demo("eight_schools") call. Reproduce the segfault using the supplied eight schools model, fitted object, and pars list under RStan 2.26.1, then verify that unconstrain_pars returns the expected transformed values without crashing R.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.