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

reduce_sum fails if tuples are passed #3041

Open
WardBrian opened this issue Mar 28, 2024 · 3 comments
Open

reduce_sum fails if tuples are passed #3041

WardBrian opened this issue Mar 28, 2024 · 3 comments

Comments

@WardBrian
Copy link
Member

Description

First reported on the forums

The issue appears to be because deep_copy_vars does not have an overload for tuples.

Example

Partial example given on the forums:

real partial_sum_lpmf(
    data array[] int seq_region, data int start, data int end, data int n_date_max, data int n_call_max,
    data array[] int n_site, data array[,] int n_date, data array[,,] int n_call, data array[,,] int dates, data array[,,] int tau, data array[,,] vector y, 
    vector eta, vector gamma, vector phi, tuple(vector, vector) lambda_mu) {

output (truncated):

^
/Users/s447341/.cmdstan/cmdstan-2.33.1/stan/lib/stan_math/stan/math/prim/functor/reduce_sum.hpp:207:10: note: in instantiation of member function 'stan::math::internal::reduce_sum_impl<occu_ps_ms_trm_tuple_model_namespace::partial_sum_lpmf_rsfunctor__<true>, void, stan::math::var_value<double>, const std::vector<int> &, const int &, const int &, const std::vector<int> &, const std::vector<std::vector<int>> &, const std::vector<std::vector<std::vector<int>>> &, const std::vector<std::vector<std::vector<int>>> &, const std::vector<std::vector<std::vector<int>>> &, const std::vector<std::vector<std::vector<Eigen::Matrix<double, -1, 1>>>> &, Eigen::Matrix<stan::math::var_value<double>, -1, 1> &, Eigen::Matrix<stan::math::var_value<double>, -1, 1> &, Eigen::Matrix<stan::math::var_value<double>, -1, 1> &, std::tuple<Eigen::Matrix<stan::math::var_value<double>, -1, 1>, Eigen::Matrix<stan::math::var_value<double>, -1, 1>> &>::operator()' requested here
  return internal::reduce_sum_impl<ReduceFunction, void, return_type, Vec,
         ^
/var/folders/gr/hv6k6041275bm9w8vvn4ms3sz23ct8/T/Rtmp0Nghm8
/model-12c15458f3352.hpp:2235:12: note: in instantiation of function template specialization 'occu_ps_ms_trm_tuple_model_namespace::occu_ps_ms_trm_tuple_model::log_prob_impl<true, false, Eigen::Matrix<stan::math::var_value<double>, -1, 1>, Eigen::Matrix<int, -1, 1>, nullptr, nullptr, nullptr>' requested here
    return log_prob_impl<propto__, jacobian__>(params_r, params_i, pstream);
           ^
/Users/s447341/.cmdstan/cmdstan-2.33.1/stan/src/stan/model/model_base_crtp.hpp:120:50: note: in instantiation of function template specialization 'occu_ps_ms_trm_tuple_model_namespace::occu_ps_ms_trm_tuple_model::log_prob<true, false, stan::math::var_value<double>>' requested here
    return static_cast<const M*>(this)->template log_prob<true, false>(theta,
                                                 ^
/var/folders/gr/hv6k6041275bm9w8vvn4ms3sz23ct8/T/Rtmp0Nghm8/model-12c15458f3352.hpp:983:3: note: in instantiation of member function 'stan::model::model_base_crtp<occu_ps_ms_trm_tuple_model_namespace::occu_ps_ms_trm_tuple_model>::log_prob_propto' requested here
  ~occu_ps_ms_trm_tuple_model() {}
  ^
/Users/s447341/.cmdstan/cmdstan-2.33.1/stan/lib/stan_math/stan/math/rev/core/deep_copy_vars.hpp:33:13: note: candidate function not viable: no known conversion from 'std::tuple<Eigen::Matrix<stan::math::var_value<double>, -1, 1>, Eigen::Matrix<stan::math::var_value<double>, -1, 1>>' to 'const var' (aka 'const var_value<double>') for 1st argument
inline auto deep_copy_vars(const var& arg) {
            ^
/Users/s447341/.cmdstan/cmdstan-2.33.1/stan/lib/stan_math/stan/math/rev/core/deep_copy_vars.hpp:23:14: note: candidate template ignored: requirement 'is_arithmetic<std::tuple<Eigen::Matrix<stan::math::var_value<double, void>, -1, 1, 0, -1, 1>, Eigen::Matrix<stan::math::var_value<double, void>, -1, 1, 0, -1, 1>>>::value' was not satisfied [with Arith = std::tuple<Eigen::Matrix<stan::math::var_value<double>, -1, 1>, Eigen::Matrix<stan::math::var_value<double>, -1, 1>> &]
inline 
Arith deep_copy_vars(Arith&& arg) {
             ^
/Users/s447341/.cmdstan/cmdstan-2.33.1/stan/lib/stan_math/stan/math/rev/core/deep_copy_vars.hpp:45:13: note: candidate template ignored: requirement 'integral_constant<bool, false>::value' was not satisfied [with VarVec = std::tuple<Eigen::Matrix<stan::math::var_value<double>, -1, 1>, Eigen::Matrix<stan::math::var_value<double>, -1, 1>> &]
inline auto deep_copy_vars(VarVec&& arg) {
            ^
/Users/s447341/.cmdstan/cmdstan-2.33.1/stan/lib/stan_math/stan/math/rev/core/deep_copy_vars.hpp:63:13: note: candidate template ignored: requirement 'integral_constant<bool, false>::value' was not satisfied [with VecContainer = std::tuple<Eigen::Matrix<stan::math::var_value<double>, -1, 1>, Eigen::Matrix<stan::math::var_value<double>, -1, 1>> &]
inline auto deep_copy_vars(VecContainer&& arg) {
            ^
/Users/s447341/.cmdstan/cmdstan-2.33.1/stan/lib/stan_math/stan/math/rev/core/deep_copy_vars.hpp:79:13: note: candidate template ignored: requirement 'integral_constant<bool, false>::value' was not satisfied [with EigT = std::tuple<Eigen::Matrix<stan::math::var_value<double>, -1, 1>, Eigen::Matrix<stan::math::var_value<double>, -1, 1>> &]
inline auto deep_copy_vars(EigT&& arg) {
            ^

4 errors generated.

make: *** [/var/folders/gr/hv6k6041275bm9w8vvn4ms3sz23ct8/T/Rtmp0Nghm8/model-12c15458f3352] Error 1

Error: An error occured during compilation! See the message above for more information.

Expected Output

Current Version:

v4.8.1

@WardBrian
Copy link
Member Author

I think this would be trivial if we had a map or something like stan::math::for_each that returned a new tuple, but unfortunately my C++-foo is not sufficient to write such a function

@wds15
Copy link
Contributor

wds15 commented Apr 1, 2024

My C++-template-metaprogramming-foo isn't up for this either. Sounds like a job for @SteveBronder ? If time permits obviously...

@SteveBronder
Copy link
Collaborator

Yes I'm pretty sure I know how to fix this. Ben Bales and I did a thing for the ode solvers that should be able to be ported over pretty smoothly and fix everything

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

3 participants