Skip to content

reduce_sum fails if tuples are passed #3041

Description

@WardBrian

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

Activity

  1. WardBrian commented on Mar 28, 2024

    @WardBrian
    MemberAuthor

    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

  2. wds15 commented on Apr 1, 2024

    @wds15
    Contributor

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

  3. SteveBronder commented on Apr 1, 2024

    @SteveBronder
    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

  4. nsiccha commented on Jul 21, 2025

    @nsiccha

    This is just me reporting that I've run into this issue, in case it helps to bump its priority up :)

  5. WardBrian commented on Jul 21, 2025

    @WardBrian
    MemberAuthor

    I think @SteveBronder said some of the helpers he had to write for laplace would be applicable here

  6. nsiccha commented on Jul 21, 2025

    @nsiccha

    Cool! It would help me a bit, but I've now also implemented my own workaround (not in C++ but Stan).

  7. nsiccha commented on Jul 23, 2025

    @nsiccha

    My workaround has apparently run into another issue with tuples. I'll prepare an MWE and open an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions