From b5781251d435adf8623f0fb0c1328d9e54eb2c8b Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 12 May 2026 12:24:29 -0400 Subject: [PATCH 1/4] remove extra std::forward in fwd/functor/reduce_sum loop --- stan/math/fwd/functor/reduce_sum.hpp | 2 +- .../unit/math/rev/functor/reduce_sum_test.cpp | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/stan/math/fwd/functor/reduce_sum.hpp b/stan/math/fwd/functor/reduce_sum.hpp index f8e03158c30..9fa28811f4a 100644 --- a/stan/math/fwd/functor/reduce_sum.hpp +++ b/stan/math/fwd/functor/reduce_sum.hpp @@ -85,7 +85,7 @@ struct reduce_sum_impl { } sum += ReduceFunction()(std::forward(sub_slice), start, end, msgs, - std::forward(args)...); + args); } return sum; } diff --git a/test/unit/math/rev/functor/reduce_sum_test.cpp b/test/unit/math/rev/functor/reduce_sum_test.cpp index b68c2ef6946..1a6ea820cb2 100644 --- a/test/unit/math/rev/functor/reduce_sum_test.cpp +++ b/test/unit/math/rev/functor/reduce_sum_test.cpp @@ -429,3 +429,31 @@ TEST_F(AgradRev, StanMathRev_reduce_sum_linked_args) { stan::math::recover_memory(); } + + +TEST(StanMathPrim_reduce_sum, no_threads_return_type_deduction) { + struct int_sum_fn { + int operator()(const std::vector& slice, size_t, size_t, + std::ostream*) const { + return std::accumulate(slice.begin(), slice.end(), 0); + } + }; + + std::vector data = {1, 2, 3, 4}; + // This must compile (even without STAN_THREADS) + auto result = stan::math::reduce_sum(data, 1, nullptr); + EXPECT_DOUBLE_EQ(result, 10.0); // return_type_t> = double +} + +TEST(StanMathPrim_reduce_sum, no_threads_empty_return_type_deduction) { + struct int_sum_fn { + int operator()(const std::vector& slice, size_t, size_t, + std::ostream*) const { + return 0; + } + }; + + std::vector empty; + auto result = stan::math::reduce_sum(empty, 1, nullptr); + EXPECT_DOUBLE_EQ(result, 0.0); +} From 17fb5e15511e854ff0c94cf117c16955edd24274 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 12 May 2026 12:26:29 -0400 Subject: [PATCH 2/4] remove extra std::forward in fwd/functor/reduce_sum loop --- .../unit/math/rev/functor/reduce_sum_test.cpp | 27 ------------------- 1 file changed, 27 deletions(-) diff --git a/test/unit/math/rev/functor/reduce_sum_test.cpp b/test/unit/math/rev/functor/reduce_sum_test.cpp index 1a6ea820cb2..bc64400e11b 100644 --- a/test/unit/math/rev/functor/reduce_sum_test.cpp +++ b/test/unit/math/rev/functor/reduce_sum_test.cpp @@ -430,30 +430,3 @@ TEST_F(AgradRev, StanMathRev_reduce_sum_linked_args) { stan::math::recover_memory(); } - -TEST(StanMathPrim_reduce_sum, no_threads_return_type_deduction) { - struct int_sum_fn { - int operator()(const std::vector& slice, size_t, size_t, - std::ostream*) const { - return std::accumulate(slice.begin(), slice.end(), 0); - } - }; - - std::vector data = {1, 2, 3, 4}; - // This must compile (even without STAN_THREADS) - auto result = stan::math::reduce_sum(data, 1, nullptr); - EXPECT_DOUBLE_EQ(result, 10.0); // return_type_t> = double -} - -TEST(StanMathPrim_reduce_sum, no_threads_empty_return_type_deduction) { - struct int_sum_fn { - int operator()(const std::vector& slice, size_t, size_t, - std::ostream*) const { - return 0; - } - }; - - std::vector empty; - auto result = stan::math::reduce_sum(empty, 1, nullptr); - EXPECT_DOUBLE_EQ(result, 0.0); -} From 9c342f074eb625b984552071c920390ba41eb359 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Tue, 12 May 2026 12:27:29 -0400 Subject: [PATCH 3/4] [Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1 --- test/unit/math/rev/functor/reduce_sum_test.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/test/unit/math/rev/functor/reduce_sum_test.cpp b/test/unit/math/rev/functor/reduce_sum_test.cpp index bc64400e11b..b68c2ef6946 100644 --- a/test/unit/math/rev/functor/reduce_sum_test.cpp +++ b/test/unit/math/rev/functor/reduce_sum_test.cpp @@ -429,4 +429,3 @@ TEST_F(AgradRev, StanMathRev_reduce_sum_linked_args) { stan::math::recover_memory(); } - From 8ef464d040847fee34c2bf45ecc065cf80ee9a7f Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 12 May 2026 15:07:16 -0400 Subject: [PATCH 4/4] fix loop forward in fwd/functor/reduce_sum.hpp --- stan/math/fwd/functor/reduce_sum.hpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/stan/math/fwd/functor/reduce_sum.hpp b/stan/math/fwd/functor/reduce_sum.hpp index 9fa28811f4a..0a0bbeadc7b 100644 --- a/stan/math/fwd/functor/reduce_sum.hpp +++ b/stan/math/fwd/functor/reduce_sum.hpp @@ -73,19 +73,18 @@ struct reduce_sum_impl { msgs, std::forward(args)...); } else { return_type_t sum = 0.0; + std::decay_t sub_slice; for (size_t i = 0; i < (vmapped.size() + grainsize - 1) / grainsize; ++i) { size_t start = i * grainsize; size_t end = std::min((i + 1) * grainsize, vmapped.size()) - 1; - - std::decay_t sub_slice; + sub_slice.clear(); sub_slice.reserve(end - start + 1); + for (size_t i = start; i <= end; ++i) { sub_slice.emplace_back(vmapped[i]); } - - sum += ReduceFunction()(std::forward(sub_slice), start, end, msgs, - args); + sum += ReduceFunction()(sub_slice, start, end, msgs, args...); } return sum; }