Fix #3041: Add tuple support to reduce_sum autodiff helpers#3300
Fix #3041: Add tuple support to reduce_sum autodiff helpers#3300nsiccha wants to merge 9 commits intostan-dev:developfrom
Conversation
Add tuple specialization to apply_scalar_unary so that vectorized math functions (exp, sin, cos, etc.) automatically work on tuples. Also extend require_ad_container_t to accept tuples containing autodiff types, and add is_autodiff support for tuples. Fixes stan-dev#3041 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
These three functions handle var, vector<var>, Eigen<var>, and arithmetic types but not tuples. With STAN_THREADS=true, reduce_sum passes tuple arguments through these functions, causing compilation failures. Add tuple overloads that unpack the tuple via stan::math::apply and recursively process each element. Fixes stan-dev#3041 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This reverts commit 23dafa2.
Remove unnecessary <tuple> includes (already available via prim/meta.hpp). Add doxygen comments matching existing style. Add unit tests for tuple overloads of deep_copy_vars, save_varis, and accumulate_adjoints covering tuple<var, int>, tuple<var, double>, and tuple<var, var>. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
I'm just gonna @WardBrian: The above PR is 100% Claude generated - don't review it yet please. But is there anything "extra" that I should tell Claude (or also watch out for myself)? |
db5f6de to
3f05bea
Compare
|
@SteveBronder is probably the better person to ask, both because he has more experience with Claude and because when working on some Laplace stuff he told me he had some code that he thought would help solve this reduce sum issue |
Jenkins Console Log Machine informationNo LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focalCPU: G++: Clang: |
I'll check the changes before marking it as ready for review.
Human content above
Claude content below
Problem
When
STAN_THREADS=true,reduce_sumwith tuple arguments fails to compile because three internal autodiff helper functions —deep_copy_vars,save_varis, andaccumulate_adjoints— have overloads forvar,std::vector, and Eigen types but notstd::tuple. This prevents using tuples to pass mixed parameter/data arguments toreduce_sumin threaded mode.Change
Three files in
stan/math/rev/core/, each gets a tuple overload that unpacks the tuple viastan::math::applyand recursively processes each element:deep_copy_vars.hpp—deep_copy_vars(Tuple&&): deep-copies var elements, forwards non-var elements unchanged. Includesapply.hpp.save_varis.hpp—save_varis(vari**, Tuple&&, Pargs&&...): unpacks tuple and saves vari pointers from each element. Includesapply.hpp. Forward declaration added.accumulate_adjoints.hpp—accumulate_adjoints(double*, Tuple&&, Pargs&&...): unpacks tuple and accumulates adjoints from each element. Includesapply.hpp. Forward declaration added.Tests added to all three existing test files covering
tuple<var, int>,tuple<var, double>, andtuple<var, var>.MWE
Stan model using
reduce_sumwithtuple(real, int)parameter (mixing a parametermuwith dataK), compiled via BridgeStan withSTAN_THREADS=trueandMATH=pointing to the patched math library. Verifies log density and gradients are finite and consistent. Fails on main (compilation error insave_varis/deep_copy_vars), passes on worktree.MWE
mwe.jlmain output:
worktree output:
Fixes #3041