Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RStan is passing the wrong type for the code argument to stanc.js on Windows #1145

Open
WardBrian opened this issue Dec 4, 2024 · 3 comments

Comments

@WardBrian
Copy link
Member

This has lead to stan-dev/stanc3#1446 and the seven duplicates therein.

I think the issue is the code that reads from a file is returning a vector of strings, rather than just one string. No idea why this is OS-specific.

This might be related to my pr #1124 which touches some of the same code

Description:

This model consistently raises an error on Windows, but not on other platforms:

data {
  int<lower=0> T; # rows
  int<lower=0> N; # cols
  
  matrix[T, N] x;
  matrix[T, N] y;
}

parameters {
  real alpha;
  real beta;
  
  real mu_alpha;
  real<lower=0> sigma_alpha;
  
  real mu_beta;
  real<lower=0> sigma_beta;
}

model {
  
  
  for(i in 1:T){
    for(j in 1:N){
      y[i, j] ~ poisson(exp(alpha + beta*x[i, j]))
    }
  }
  
  alpha ~ normal(mu_alpha, sigma_alpha);
  beta ~ normal(mu_beta, sigma_beta);
  
  mu_alpha ~ normal(0, 1)
  sigma_alpha ~ gamma(2, 2)
  
  mu_beta ~ normal(0, 1)
  sigma_beta ~ gamma(2, 2)
  
}

> rstan::stanc("./m2.stan") yields

Error in rstan::stanc("./m2.stan") : 0
Internal compiler error:
TypeError: b.charCodeAt is not a function

By replacing the stanc.js with the non-minified version, and adding some extra printing code to it, this became clear:

  1. b above is the code string
  2. In this case, the code is not actually a string, but an array of strings.

That is why this code does not lead to a crash:

code <- paste(readLines('./m2.stan'), collapse="")
rstan::stanc(model_code=code)

(though, it does raise a genuine syntax error, which is good!)

RStan Version:

‘2.36.0.9000’

R Version:

"R version 4.4.2 (2024-10-31 ucrt)"

Operating System:

Windows 11

@WardBrian
Copy link
Member Author

WardBrian commented Dec 4, 2024

Note that directly using rstan:::stanc_ctx$call("stanc", ...) also never seems to raise this error, so something that is munging model_code is returning something that isn't a string.

The array being passed seems to be split at newlines:
["data {"," int<lower=0> T; # rows"," int<lower=0> N; # cols",""," matrix[T, N] x;"," matrix[T, N] y;","}","parameters {"," real alpha;"," real beta;",""," real mu_alpha;"," real<lower=0> sigma_alpha;",""," real mu_beta;"," real<lower=0> sigma_beta;","}","model {","",""," for(i in 1:T){"," for(j in 1:N){"," y[i, j] ~ poisson(exp(alpha + beta*x[i, j]))"," }"," }",""," alpha ~ normal(mu_alpha, sigma_alpha);"," beta ~ normal(mu_beta, sigma_beta);",""," mu_alpha ~ normal(0, 1)"," sigma_alpha ~ gamma(2, 2)",""," mu_beta ~ normal(0, 1)"," sigma_beta ~ gamma(2, 2)","","}"]

@hsbadr could you look into this? It's generating a lot of noise in the stanc3 repo, and it looks like most of the users are using experimental

@WardBrian
Copy link
Member Author

Note: setting my line endings to CRLF on ubuntu still didn't trigger the same behavior as Windows

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant