CmdStanR throws error on compiling model

Hi folks, so I’m working with a model that compiles fine with the latest rstan, but I would like to take advantage of the reduce_sum capabilities in available in cmdstan.

I am running this on a more or less “fresh” install of R, cmdstan, etc, on a new Mac I got running Catalina (10.15.4). Rstan works fine. I haven’t done anything to the C++ toolchain (haven’t added a Makevars file or anything like that). I just installed cmdstanr today.

> cmdstan_version()
[1] "2.23.0"

However, the model will not compile. I get this error:

Compiling Stan program...
In file included from /var/folders/vz/4l1x57s13_b33rh17_qqhzdw0000gn/T/RtmpJl6dV0/model-84751e3c16fa.hpp:1:
In file included from stan/src/stan/model/model_header.hpp:4:
In file included from /Users/thomasvladeck/.cmdstanr/cmdstan-2.23.0/stan/lib/stan_math/stan/math.hpp:19:
In file included from /Users/thomasvladeck/.cmdstanr/cmdstan-2.23.0/stan/lib/stan_math/stan/math/rev.hpp:9:
In file included from /Users/thomasvladeck/.cmdstanr/cmdstan-2.23.0/stan/lib/stan_math/stan/math/prim.hpp:8:
In file included from /Users/thomasvladeck/.cmdstanr/cmdstan-2.23.0/stan/lib/stan_math/stan/math/prim/err.hpp:4:
In file included from /Users/thomasvladeck/.cmdstanr/cmdstan-2.23.0/stan/lib/stan_math/stan/math/prim/err/check_2F1_converges.hpp:6:
/Users/thomasvladeck/.cmdstanr/cmdstan-2.23.0/stan/lib/stan_math/stan/math/prim/fun/fabs.hpp:37:10: error: implicit instantiation of undefined template 'stan::math::apply_scalar_unary<stan::math::fabs_fun, unsigned long, void>'
  return apply_scalar_unary<fabs_fun, Container>::apply(x);
         ^
/var/folders/vz/4l1x57s13_b33rh17_qqhzdw0000gn/T/RtmpJl6dV0/model-84751e3c16fa.hpp:509:42: note: in instantiation of function template specialization 'stan::math::fabs<unsigned long, nullptr>' requested here
                             stan::math::fabs((x - xprime))) / p)), 2)) /
                                         ^
/var/folders/vz/4l1x57s13_b33rh17_qqhzdw0000gn/T/RtmpJl6dV0/model-84751e3c16fa.hpp:1074:13: note: in instantiation of function template specialization 'reduce_sum_model_namespace::locally_periodic_kernel<unsigned long, unsigned long, double, double, double, double>' requested here
            locally_periodic_kernel(i, j, sigma, p, l1, l2, pstream__),
            ^
/Users/thomasvladeck/.cmdstanr/cmdstan-2.23.0/stan/lib/stan_math/stan/math/prim/meta/apply_scalar_unary.hpp:38:8: note: template is declared here
struct apply_scalar_unary;
       ^
1 error generated.
make: *** [/var/folders/vz/4l1x57s13_b33rh17_qqhzdw0000gn/T/RtmpJl6dV0/model-84751e3c16fa] Error 1
Error: An error occured during compilation! See the message above for more information.

It appears to be a problem with fabs which is called in this function:

  real locally_periodic_kernel(real x, real xprime, real sigma, real p, real l1, real l2) {
    real cov_val;

    cov_val = sigma^2 * exp(-2 * sin(pi() * fabs(x-xprime)/p)^2 / l1^2) *
      exp(-(x-xprime)^2 / (2 * l2^2));

    return(cov_val);
  }

I haven’t been able to find a similar error anywhere. Any ideas what I need to do to get this to compile with cmdstanr?

On a friend’s recommendation, I tried sqrt(square(.)), and a similar issue:

In file included from /var/folders/vz/4l1x57s13_b33rh17_qqhzdw0000gn/T/RtmpJl6dV0/model-84751ef63173.hpp:1:
In file included from stan/src/stan/model/model_header.hpp:4:
In file included from /Users/thomasvladeck/.cmdstanr/cmdstan-2.23.0/stan/lib/stan_math/stan/math.hpp:19:
In file included from /Users/thomasvladeck/.cmdstanr/cmdstan-2.23.0/stan/lib/stan_math/stan/math/rev.hpp:6:
In file included from /Users/thomasvladeck/.cmdstanr/cmdstan-2.23.0/stan/lib/stan_math/stan/math/rev/core.hpp:24:
In file included from /Users/thomasvladeck/.cmdstanr/cmdstan-2.23.0/stan/lib/stan_math/stan/math/rev/core/operator_divide_equal.hpp:5:
In file included from /Users/thomasvladeck/.cmdstanr/cmdstan-2.23.0/stan/lib/stan_math/stan/math/rev/core/operator_division.hpp:9:
In file included from /Users/thomasvladeck/.cmdstanr/cmdstan-2.23.0/stan/lib/stan_math/stan/math/rev/core/std_complex.hpp:5:
In file included from /Users/thomasvladeck/.cmdstanr/cmdstan-2.23.0/stan/lib/stan_math/stan/math/prim/core/complex_base.hpp:4:
/Users/thomasvladeck/.cmdstanr/cmdstan-2.23.0/stan/lib/stan_math/stan/math/prim/fun/square.hpp:51:10: error: implicit instantiation of undefined template 'stan::math::apply_scalar_unary<stan::math::square_fun, unsigned long, void>'
  return apply_scalar_unary<square_fun, Container>::apply(x);
         ^
/var/folders/vz/4l1x57s13_b33rh17_qqhzdw0000gn/T/RtmpJl6dV0/model-84751ef63173.hpp:509:47: note: in instantiation of function template specialization 'stan::math::square<unsigned long, nullptr>' requested here
                             stan::math::sqrt(square((x - xprime)))) / p)),
                                              ^
/var/folders/vz/4l1x57s13_b33rh17_qqhzdw0000gn/T/RtmpJl6dV0/model-84751ef63173.hpp:1074:13: note: in instantiation of function template specialization 'reduce_sum_model_namespace::locally_periodic_kernel<unsigned long, unsigned long, double, double, double, double>' requested here
            locally_periodic_kernel(i, j, sigma, p, l1, l2, pstream__),
            ^
/Users/thomasvladeck/.cmdstanr/cmdstan-2.23.0/stan/lib/stan_math/stan/math/prim/meta/apply_scalar_unary.hpp:38:8: note: template is declared here
struct apply_scalar_unary;
       ^
1 error generated.
make: *** [/var/folders/vz/4l1x57s13_b33rh17_qqhzdw0000gn/T/RtmpJl6dV0/model-84751ef63173] Error 1
Error: An error occured during compilation! See the message above for more information.```

Sorry you’re getting an error. Just so I’m 100% clear, you’re able to compile other models with CmdStanR just not this one?

I’m also on Mac OS X Catalina and I tried a toy example with fabs() and it seems to run fine. Can you see if my toy example works?

stan_program <- write_stan_tempfile("
parameters {
  real x;
}
model {
  real mu = -2;
  x ~ normal(fabs(mu), 1);
}
"
)

mod <- cmdstan_model(stan_program)
fit <- mod$sample()

If that works, then we might need to see the rest of your Stan program to figure this out. Are you able to share it? Or a simplified version of it that still causes the error?

This might be a bug in Math. Or was in 2.23. Can you try with the release candidate @tvladeck.

Cmdstan 2.24 release candidate now available

See last post for cmdstanr instructions.

this model fit fine!

and to Rok,

Indeed, it worked with this new RC. Thank you! Sorry for the false alarm

Not a false alarm! Glad it works for you and that we fixed it for 2.24!

Also @tvladeck I have to say that I love the https://rt.live/ project! Just wish it was powered by Stan :) kidding of course. Keep up the great work!

Appreciate that! And so you know, a lot of prototyping in Stan happened to get that model off the ground!