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

stanc --O flag fails to build simple Stan model #1299

Closed
andrewfowlie opened this issue Oct 30, 2024 · 3 comments
Closed

stanc --O flag fails to build simple Stan model #1299

andrewfowlie opened this issue Oct 30, 2024 · 3 comments

Comments

@andrewfowlie
Copy link

Summary:

Compilation error on simple Stan model only when using --O stanc flag.

Description:

The --O flag generates a model header including a stan::model::assign call that doesn't appear to have a matching implementation.

Reproducible Steps:

For the simple model bug.stan

parameters {
  vector[1] x;
}
transformed parameters {
  vector[1] theta = 1 * x;
}
model {
    target += theta[1];  # using theta here seems necessary for the bug
}

the build

make bug   # works

works as expected, whereas

rm bug  # start from fresh
make bug STANCFLAGS=--O  # fails

fails with (partial) output

g++ -std=c++17 -pthread -D_REENTRANT -Wno-sign-compare -Wno-ignored-attributes -Wno-class-memaccess      -I stan/lib/stan_math/lib/tbb_2020.3/include    -O3 -I src -I stan/src -I stan/lib/rapidjson_1.1.0/ -I lib/CLI11-1.9.1/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.4.0 -I stan/lib/stan_math/lib/boost_1.84.0 -I stan/lib/stan_math/lib/sundials_6.1.1/include -I stan/lib/stan_math/lib/sundials_6.1.1/src/sundials    -DBOOST_DISABLE_ASSERTS          -c -Wno-ignored-attributes   -x c++ -o bug.o bug.hpp
bug.hpp: In instantiation of ‘stan::scalar_type_t<T2> bug_model_namespace::bug_model::log_prob_impl(VecR&, VecI&, std::ostream*) const [with bool propto__ = false; bool jacobian__ = false; VecR = Eigen::Matrix<stan::math::var_value<double>, -1, 1>; VecI = Eigen::Matrix<int, -1, 1>; stan::require_vector_like_t<VecR>* <anonymous> = 0; stan::require_vector_like_vt<std::is_integral, VecI>* <anonymous> = 0; stan::require_st_var<VecR>* <anonymous> = 0; stan::scalar_type_t<T2> = stan::math::var_value<double>; std::ostream = std::basic_ostream<char>]’:
bug.hpp:371:0:   required from ‘T_ bug_model_namespace::bug_model::log_prob(Eigen::Matrix<T_a, -1, 1>&, std::ostream*) const [with bool propto__ = false; bool jacobian__ = false; T_ = stan::math::var_value<double>; std::ostream = std::basic_ostream<char>]’
stan/src/stan/model/model_base_crtp.hpp:98:0:   required from ‘stan::math::var stan::model::model_base_crtp<M>::log_prob(Eigen::Matrix<stan::math::var_value<double>, -1, 1>&, std::ostream*) const [with M = bug_model_namespace::bug_model; stan::math::var = stan::math::var_value<double>; std::ostream = std::basic_ostream<char>]’
stan/src/stan/model/model_base_crtp.hpp:96:0:   required from here
bug.hpp:129: error: no matching function for call to ‘assign(Eigen::Matrix<stan::math::var_value<double>, -1, 1>&, stan::math::var_value<Eigen::Matrix<double, -1, 1> >, const char [30])’
  129 |       stan::model::assign(lcm_sym7__, stan::math::multiply(1, x),
      |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  130 |         "assigning variable lcm_sym7__");
      | 
In file included from stan/src/stan/model/indexing.hpp:9,
                 from stan/src/stan/model/model_header.hpp:13:

Expected Output:

I don't know whether my Stan program contains some undefined behavior such that it should never compile, or if this is a bug with the --O flag.

I understand that the --O flag enables experimental optimization features, that may or may not yield performance benefits, but I didn't expect it to fail to build such a simple program.

If it's just the case that we should expect that --O sometimes fails to build valid Stan programs, please make that clearer in the documents.

Current Version:

v2.35.0

@WardBrian
Copy link
Member

We will investigate, but I do want to say that yes, regular users should basically never use this flag. We renamed it to —Oexperimental for a reason, and if not for worrying about people already using it we probably would have removed it

@andrewfowlie
Copy link
Author

Thanks Brian! In that case, one way forward:

  • confirming that it is a bug only in —O and not in other optimization levels
  • telling in the docs that —O it isn’t guaranteed to build valid Stan programs

This will put more people off using it!

@WardBrian
Copy link
Member

I did some analysis of the cause and opened the above issues

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

2 participants