Giter Site home page Giter Site logo

Comments (12)

rmcelreath avatar rmcelreath commented on June 23, 2024

Thanks for the report. This happens only when the model definitions are the same? If so, maybe related to how Stan reuses the compiled model binary.

from rethinking.

rmcelreath avatar rmcelreath commented on June 23, 2024

A little experimentation later, still not sure what is causing the problem. But using the previous map2stan result seems to bypass it:

m2 <- map2stan(
  alist(
    pulled_left ~ dbinom(1,theta),
    logit(theta) <- a + bp*prosoc_left + bpc*condition*prosoc_left ,
    a ~ dnorm(0,10),
    bp ~ dnorm(0,10),
    bpc ~ dnorm(0,10)
  ) ,
  data=d, chains=1, cores=1 )

m2_2 <- map2stan( m2 ,data=d, chains=1, cores=1 )

Hopefully that's a useful workaround for others, until I can figure out the core issue.

from rethinking.

kmiddleton avatar kmiddleton commented on June 23, 2024

I think you are correct that stan reusing the compiled model leads to the failure. If you delete the relevant files from the tmp directory, the same model will run twice.

library(rethinking)

data(chimpanzees)

# don't want any variables with NAs
d <- list( 
  pulled_left = chimpanzees$pulled_left ,
  prosoc_left = chimpanzees$prosoc_left ,
  condition = chimpanzees$condition ,
  actor = as.integer( chimpanzees$actor ) ,
  blockid = as.integer( chimpanzees$block )
)

# RStan fit
m2 <- map2stan(
  alist(
    pulled_left ~ dbinom(1,theta),
    logit(theta) <- a + bp*prosoc_left + bpc*condition*prosoc_left ,
    a ~ dnorm(0,10),
    bp ~ dnorm(0,10),
    bpc ~ dnorm(0,10)
  ) ,
  data=d, chains=1, cores=1 )

# Clean up tmpdir ########
tmp_dir <- tempdir()
model_code <- m2@stanfit@stanmodel@model_code
model_name2 <- attr(model_code, "model_name2")
unlink(paste0(tmp_dir, "/", model_name2, c(".rds", ".stan")))
# Seems to work ok without removing the dso, but just in case
dso_name <- m2@stanfit@stanmodel@dso@dso_filename
unlink(paste0(tmp_dir, "/", model_name2, c(".cpp", ".o", ".so")))

m2_2 <- map2stan(
  alist(
    pulled_left ~ dbinom(1,theta),
    logit(theta) <- a + bp*prosoc_left + bpc*condition*prosoc_left ,
    a ~ dnorm(0,10),
    bp ~ dnorm(0,10),
    bpc ~ dnorm(0,10)
  ) ,
  data=d, chains=1, cores=1 )

from rethinking.

rmcelreath avatar rmcelreath commented on June 23, 2024

I haven't had any luck figuring out the root cause. I thought perhaps it was a mismatch between the model name that map2stan uses and the model name internal to the stanfit object and module. But making those identical didn't change anything.

It's perhaps related to this general bug with stan, in which it keeps links to removed objects:
https://groups.google.com/forum/#!category-topic/stan-users/general/Bg8HhyB6qG4

from rethinking.

rmcelreath avatar rmcelreath commented on June 23, 2024

I can reliably prevent the segfault by simply making a copy of the stanfit object in the global environment:

library(rethinking)

flist <- alist(
  y ~ dnorm(mu,1), 
  mu <- a + b*x,
  a ~ dnorm(0,10),
  b ~ dnorm(0,1)
)

set.seed(1)
x <- rnorm(100)
y <- rnorm(100,-0.1*x,1)

d <- list(x=x,y=y)

m1 <- map2stan( flist , data=d , chains=1 )
fit <- m1@stanfit
m2 <- map2stan( flist , data=d , chains=1 )

As soon as the fit copy is removed, it'll segfault again:

rm(fit)
m2 <- map2stan( flist , data=d , chains=1 )

No idea what this means.

from rethinking.

kmiddleton avatar kmiddleton commented on June 23, 2024

Not surprising, because it's a Windows fix, but this commit stan-dev/rstan@6a61721 does not fix the crash on OS X.

Your code above leads to crash with recompiled rstan from the develop tree:

devtools::install_github("stan-dev/rstan", ref = "develop", subdir = "rstan/rstan", dependencies = TRUE)

from rethinking.

j-d-miller avatar j-d-miller commented on June 23, 2024

I've been working through the excellent Rethinking book and have encountered the same problem.

Everything worked well until I got to the map2stan examples in Chapter 10 and then R started to crash whenever I run a given map2stan more than once. (When I call map2stan with a 'map' model as the input R crashes the first time I run the code.)

(Also, as an aside, the function logistic doesn't seem to exist in the rethinking package. Or at least I can't find it.)

from rethinking.

rmcelreath avatar rmcelreath commented on June 23, 2024

I just pushed to the Experimental branch a solution to this issue. It's not a long-term solution, because it prevents Stan from reusing the compiled model. It forces recompilation. But it does stop the segfaults.

https://github.com/rmcelreath/rethinking/tree/Experimental

from rethinking.

j-d-miller avatar j-d-miller commented on June 23, 2024

from rethinking.

kmiddleton avatar kmiddleton commented on June 23, 2024

Works well. I know it's not an ideal fix, but for me, recompiling models is less aggravating than starting over completely after a segfault.

from rethinking.

rmcelreath avatar rmcelreath commented on June 23, 2024

Experimental branch work-around is working for people:

library(devtools)
install_github( "rmcelreath/rethinking" , ref="Experimental" )

from rethinking.

rmcelreath avatar rmcelreath commented on June 23, 2024

Kinda fixed for now, through workaround. Closing the issue, but still worrying about the underlying cause.

from rethinking.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.