Environment:
- CmdStan 2.37.0
- cmdstanr 0.9.0
- Windows 11 (Windows_NT 10.0.26200), x64, g++ toolchain from RTools
Calling normal_rng with a zero-length vector mu and scalar sigma in generated quantities crashes the CmdStan executable with an access violation (Windows exit code -1073741819 / 0xC0000005) and no error message. Via cmdstanr the only symptom is Warning: Chain 1 finished unexpectedly!.
This was very difficult to debug given the lack of an informative error message. I went down the wrong rabbit hole for a while because debugging print statements worked in transformed data but didn't print in transformed parameters; even though the error wound up being in what I figured would be executed after transformed parameters (generated quantities).
I fixed my code by using a logical gate, but it seems like that shouldn't be necessary - it return a zero-length result or reject() with an informative message.
R reprex (cmdstanr)
library(cmdstanr)
stan_file <- write_stan_file('
data {
int<lower=0> N;
}
transformed data {
print("TD");
}
parameters {
real mu;
}
transformed parameters {
print("TP");
}
model {
mu ~ std_normal();
}
generated quantities {
real marker = 0;
vector[N] z;
print("GQ");
z = to_vector(normal_rng(zeros_vector(N), 2.5));
marker = 1;
}
')
mod <- cmdstan_model(stan_file)
fit <- mod$sample(data = list(N = 1), chains = 1, iter_warmup = 1, iter_sampling = 1)
# Works:
gq_ok <- mod$generate_quantities(fitted_params = fit, data = list(N = 1))
# Crashes silently after the TD block but before TP:
gq_bad <- mod$generate_quantities(fitted_params = fit, data = list(N = 0))
# Sampling with N = 0 also crashes (at the first post-warmup GQ evaluation):
fit_bad <- mod$sample(data = list(N = 0), chains = 1, iter_warmup = 1, iter_sampling = 1)
Environment:
Calling
normal_rngwith a zero-length vectormuand scalarsigmaingenerated quantitiescrashes the CmdStan executable with an access violation (Windows exit code-1073741819/0xC0000005) and no error message. Via cmdstanr the only symptom isWarning: Chain 1 finished unexpectedly!.This was very difficult to debug given the lack of an informative error message. I went down the wrong rabbit hole for a while because debugging
printstatements worked intransformed databut didn't print intransformed parameters; even though the error wound up being in what I figured would be executed aftertransformed parameters(generated quantities).I fixed my code by using a logical gate, but it seems like that shouldn't be necessary - it return a zero-length result or
reject()with an informative message.R reprex (cmdstanr)