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

cmdstan eigen error vs rstan informative error #731

Closed
spinkney opened this issue Oct 23, 2020 · 3 comments · Fixed by #849
Closed

cmdstan eigen error vs rstan informative error #731

spinkney opened this issue Oct 23, 2020 · 3 comments · Fixed by #849

Comments

@spinkney
Copy link
Contributor

spinkney commented Oct 23, 2020

Having a udf with a 0 index compiles fine in both rstan and cmdstan. At run time they produce different errors and I believe cmdstanr should produce a better error.

cmdstanr error

> mod_test$sample(data = list(N = 100, y = rnorm(100)),
+                chains = 2,
+                parallel_chains = 2
+ )
Running MCMC with 2 parallel chains...

Chain 1 test: stan/lib/stan_math/lib/eigen_3.3.7/Eigen/src/Core/DenseCoeffsBase.h:162: Eigen::DenseCoeffsBase<Derived, 0>::CoeffReturnType Eigen::DenseCoeffsBase<Derived, 0>::operator[](Eigen::Index) const [with Derived = Eigen::Matrix<double, -1, 1>; Eigen::DenseCoeffsBase<Derived, 0>::CoeffReturnType = const double&; Eigen::Index = long int]: Assertion `index >= 0 && index < size()' failed.

rstan error

> out_test <- sampling(mod_test_rstan,
+                 data = list(N = 100, y = rnorm(100)),
+                 chains = 2,
+                 iter = 1000)

SAMPLING FOR MODEL 'test' NOW (CHAIN 1).
Chain 1: Unrecoverable error evaluating the log probability at the initial value.
Chain 1: Exception: Exception: []: accessing element out of range. index 0 out of range; expecting index to be between 1 and 100; index position = 1x  (in 'model5a82748ab0ae_test' at line 7)
  (in 'model5a82748ab0ae_test' at line 19)

test model

functions {
  real does_nothing (vector x){
    return x[0];
  }
}
data {
  int<lower=0> N;
  vector[N] y;
}
parameters {
  real mu;
  real<lower=0> sigma;
}
transformed parameters {
  real z = does_nothing(y);
}
model {
  y ~ normal(mu, sigma);
}

versions

cmdstan_version()
[1] "2.24.0"

stan_version()
[1] "2.21.0"
@bbbales2
Copy link
Member

bbbales2 commented Oct 23, 2020

This error seems to be triggered specifically in function calls with certain variables.

For instance this gives me a reasonable error (no function):

parameters {
  real a;
}

transformed parameters {
  vector[1] b;
  
  b[0] = 1.0;
}

model {
  a ~ std_normal();
}

This also gives me a reasonable error (matrix in function):

functions {
  real whatever(matrix a) {
    return a[0, 0];
  }
}

parameters {
  real a;
}

transformed parameters {
  matrix[1, 1] b;
  
  b[1, 1] = whatever(b);
}

model {
  a ~ std_normal();
}

And this gives me an Eigen error (row_vector does too):

functions {
  real whatever(vector a) {
    return a[0];
  }
}

parameters {
  real a;
}

transformed parameters {
  vector[1] b;
  
  b[1] = whatever(b);
}

model {
  a ~ std_normal();
}

@rok-cesnovar
Copy link
Member

There was one attempt at fixing this: #521

But that proved to cause a performance regression. We should give this another go once the expression rework is done in /stan.

@rok-cesnovar
Copy link
Member

The issue covering this is also still open: #489

We can merge the two issues I guess.

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

Successfully merging a pull request may close this issue.

3 participants