Skip to content

Conversation

SteveBronder
Copy link
Collaborator

Summary

This fixes #3208 by using perfect forwarding for all functions that use our underlying apply family of functions for calling functions on containers and containers and containers. The issue was that the Holder class, when used in the apply functors, did not have enough type information to know which arguments it should take ownership of.

Consider the following function, where all types are passed in via constant reference.

template <typename T1, typename T2, require_any_container_t<T1, T2>* = nullptr>
inline auto gamma_p(const T1& a, const T2& b) {
  return apply_scalar_binary(
      [](const auto& c, const auto& d) { return gamma_p(c, d); }, a, b);
}

Calling this function with an Eigen expression that has a temporary in it would not give apply_scalar_binary and the Holder inside of apply_scalar_binary enough information to know that the Holder class should own any of the input arguments. As an example we can look at a simplified version of the code used in poisson_lccdf.hpp.

auto log_Pi = log(gamma_p(n_val + 1.0, lambda_val)));
double log_sum = sum(log_Pi);

gamma_p uses apply_scalar_binary and log uses apply_scalar_unary. We need to make sure the inputs and results of the gamma_p function do not fall out of scope by the time we go through log and then assign to log_Pi. Before this PR it would be possible for the expression n_val + 1.0 to fall out of scope as well as the result of gamma_p to go out of scope from log after log_Pi is assigned.

To combat this we now use perfect forwarding for all of the functions that use our internal apply family of functors. This should allow the Holder used internally by the apply functors to know which types need to be owned by it to make sure things do not fall out of scope.

Tests

There is no new tests for this. Since it is an isue on gcc I do wonder how we should test this in our CI/CD?

Side Effects

I'd like to think of some test we can write so that, in the future, developers do not accidentally write functions that use the apply family of functors that do not use perfect forwarding.

Release notes

Adds perfect forwarding to all functions that use the apply family of functors.

Checklist

  • Copyright holder: Steve Bronder

    The copyright holder is typically you or your assignee, such as a university or company. By submitting this pull request, the copyright holder is agreeing to the license the submitted work under the following licenses:
    - Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause)
    - Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)

  • the basic tests are passing

    • unit tests pass (to run, use: ./runTests.py test/unit)
    • header checks pass, (make test-headers)
    • dependencies checks pass, (make test-math-dependencies)
    • docs build, (make doxygen)
    • code passes the built in C++ standards checks (make cpplint)
  • the code is written in idiomatic C++ and changes are documented in the doxygen

  • the new changes are tested

@WardBrian
Copy link
Member

@SteveBronder anything I can do to help this over the line?

@SteveBronder
Copy link
Collaborator Author

Right now it is just making sure the tests pass. I think I got it so we should be good!

Copy link
Member

@WardBrian WardBrian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gonna leave the proper review to @andrjohns, just two things that stood out

@WardBrian
Copy link
Member

Math pipeline looks good, upstream has some (I believe legitimate) failures. All seem to blame trigamma in some way or another


--- Compiling C++ code ---
clang++-6.0 -std=c++17 -D_REENTRANT -Wno-sign-compare -Wno-ignored-attributes      -I stan/lib/stan_math/lib/tbb_2020.3/include    -O0 -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.87.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 -include-pch stan/src/stan/model/model_header.hpp.gch/model_header_4_2.hpp.gch -x c++ -o ../stanc3/test/integration/good/stanc_helper.o ../stanc3/test/integration/good/stanc_helper.hpp
In file included from ../stanc3/test/integration/good/int_overloads.hpp:1:
In file included from stan/src/stan/model/model_header.hpp:6:
In file included from /home/jenkins/workspace/Stan_Stan_downstream_tests/performance-tests-cmdstan/cmdstan/stan/lib/stan_math/stan/math.hpp:19:
In file included from /home/jenkins/workspace/Stan_Stan_downstream_tests/performance-tests-cmdstan/cmdstan/stan/lib/stan_math/stan/math/rev.hpp:11:
In file included from /home/jenkins/workspace/Stan_Stan_downstream_tests/performance-tests-cmdstan/cmdstan/stan/lib/stan_math/stan/math/rev/constraint.hpp:24:
In file included from /home/jenkins/workspace/Stan_Stan_downstream_tests/performance-tests-cmdstan/cmdstan/stan/lib/stan_math/stan/math/prim/constraint.hpp:4:
In file included from /home/jenkins/workspace/Stan_Stan_downstream_tests/performance-tests-cmdstan/cmdstan/stan/lib/stan_math/stan/math/prim/fun.hpp:335:
/home/jenkins/workspace/Stan_Stan_downstream_tests/performance-tests-cmdstan/cmdstan/stan/lib/stan_math/stan/math/prim/fun/trigamma.hpp:142:12: error: function 'trigamma<stan::math::var_value<double, void> &, nullptr>' with deduced return type cannot be used before it is defined
    return trigamma(std::forward<T>(x));
           ^
/home/jenkins/workspace/Stan_Stan_downstream_tests/performance-tests-cmdstan/cmdstan/stan/lib/stan_math/stan/math/rev/functor/apply_scalar_unary.hpp:34:15: note: in instantiation of function template specialization 'stan::math::trigamma_fun::fun<stan::math::var_value<double, void> &>' requested here
    return F::fun(std::forward<T2>(x));
              ^
/home/jenkins/workspace/Stan_Stan_downstream_tests/performance-tests-cmdstan/cmdstan/stan/lib/stan_math/stan/math/prim/fun/trigamma.hpp:159:47: note: in instantiation of function template specialization 'stan::math::apply_scalar_unary<stan::math::trigamma_fun, stan::math::var_value<double, void> &, void>::apply<stan::math::var_value<double, void> &>' requested here
  return apply_scalar_unary<trigamma_fun, T>::apply(std::forward<T>(x));
                                              ^
../stanc3/test/integration/good/int_overloads.hpp:521:44: note: in instantiation of function template specialization 'stan::math::trigamma<stan::math::var_value<double, void> &, nullptr>' requested here
      transformed_param_real = stan::math::trigamma(p_real);
                                           ^
../stanc3/test/integration/good/int_overloads.hpp:811:12: note: in instantiation of function template specialization 'int_overloads_model_namespace::int_overloads_model::log_prob_impl<false, false, Eigen::Matrix<stan::math::var_value<double, void>, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, nullptr, nullptr, nullptr>' requested here
    return log_prob_impl<propto__, jacobian__>(params_r, params_i, pstream);
           ^
/home/jenkins/workspace/Stan_Stan_downstream_tests/performance-tests-cmdstan/cmdstan/stan/src/stan/model/model_base_crtp.hpp:98:50: note: in instantiation of function template specialization 'int_overloads_model_namespace::int_overloads_model::log_prob<false, false, stan::math::var_value<double, void> >' requested here
    return static_cast<const M*>(this)->template log_prob<false, false>(theta,
                                                 ^
../stanc3/test/integration/good/int_overloads.hpp:129:3: note: in instantiation of member function 'stan::model::model_base_crtp<int_overloads_model_namespace::int_overloads_model>::log_prob' requested here
  ~int_overloads_model() {}
  ^
/home/jenkins/workspace/Stan_Stan_downstream_tests/performance-tests-cmdstan/cmdstan/stan/lib/stan_math/stan/math/prim/fun/trigamma.hpp:158:13: note: 'trigamma<stan::math::var_value<double, void> &, nullptr>' declared here
inline auto trigamma(T&& x) {
            ^
1 error generated.
make: [make/program:77: ../stanc3/test/integration/good/int_overloads.o] Error 1 (ignored)

@SteveBronder
Copy link
Collaborator Author

Yes I missed one requires in trigamma

@stan-buildbot
Copy link
Contributor


Name Old Result New Result Ratio Performance change( 1 - new / old )
arma/arma.stan 0.31 0.3 1.03 3.31% faster
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.01 0.01 1.1 9.1% faster
gp_regr/gen_gp_data.stan 0.02 0.02 1.05 4.77% faster
gp_regr/gp_regr.stan 0.09 0.09 1.04 3.39% faster
sir/sir.stan 68.28 68.45 1.0 -0.25% slower
irt_2pl/irt_2pl.stan 4.0 4.03 0.99 -0.57% slower
eight_schools/eight_schools.stan 0.06 0.05 1.05 4.5% faster
pkpd/sim_one_comp_mm_elim_abs.stan 0.24 0.24 1.0 0.31% faster
pkpd/one_comp_mm_elim_abs.stan 19.33 18.78 1.03 2.82% faster
garch/garch.stan 0.43 0.41 1.06 5.54% faster
low_dim_gauss_mix/low_dim_gauss_mix.stan 2.82 2.57 1.1 8.89% faster
arK/arK.stan 1.86 1.71 1.09 8.02% faster
gp_pois_regr/gp_pois_regr.stan 2.85 2.81 1.02 1.56% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 8.52 8.38 1.02 1.63% faster
performance.compilation 174.93 178.41 0.98 -1.99% slower
Mean result: 1.0364710339049357

Jenkins Console Log
Blue Ocean
Commit hash: e775f450bb86eacc0a0443dbaf4a36bc3407e56f


Machine information No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focal

CPU:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
Address sizes: 46 bits physical, 48 bits virtual
CPU(s): 80
On-line CPU(s) list: 0-79
Thread(s) per core: 2
Core(s) per socket: 20
Socket(s): 2
NUMA node(s): 2
Vendor ID: GenuineIntel
CPU family: 6
Model: 85
Model name: Intel(R) Xeon(R) Gold 6148 CPU @ 2.40GHz
Stepping: 4
CPU MHz: 2400.000
CPU max MHz: 3700.0000
CPU min MHz: 1000.0000
BogoMIPS: 4800.00
Virtualization: VT-x
L1d cache: 1.3 MiB
L1i cache: 1.3 MiB
L2 cache: 40 MiB
L3 cache: 55 MiB
NUMA node0 CPU(s): 0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78
NUMA node1 CPU(s): 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59,61,63,65,67,69,71,73,75,77,79
Vulnerability Gather data sampling: Mitigation; Microcode
Vulnerability Itlb multihit: KVM: Mitigation: VMX disabled
Vulnerability L1tf: Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable
Vulnerability Mds: Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Meltdown: Mitigation; PTI
Vulnerability Mmio stale data: Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Reg file data sampling: Not affected
Vulnerability Retbleed: Mitigation; IBRS
Vulnerability Spec rstack overflow: Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2: Mitigation; IBRS; IBPB conditional; STIBP conditional; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected
Vulnerability Srbds: Not affected
Vulnerability Tsx async abort: Mitigation; Clear CPU buffers; SMT vulnerable
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single pti intel_ppin ssbd mba ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req pku ospke md_clear flush_l1d arch_capabilities

G++:
g++ (Ubuntu 9.4.0-1ubuntu1~20.04) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Clang:
clang version 10.0.0-4ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

@WardBrian WardBrian requested a review from andrjohns August 4, 2025 13:57
@WardBrian
Copy link
Member

I'm running the distribution tests on this branch with all the tests enabled + -O3 + ASan here. It is almost done, and so far looks good

@stan-buildbot
Copy link
Contributor


Name Old Result New Result Ratio Performance change( 1 - new / old )
arma/arma.stan 0.31 0.32 0.98 -2.35% slower
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.01 0.01 1.1 8.92% faster
gp_regr/gen_gp_data.stan 0.02 0.02 1.05 4.47% faster
gp_regr/gp_regr.stan 0.09 0.09 1.06 5.73% faster
sir/sir.stan 67.69 68.55 0.99 -1.27% slower
irt_2pl/irt_2pl.stan 4.0 3.99 1.0 0.33% faster
eight_schools/eight_schools.stan 0.05 0.05 1.02 1.88% faster
pkpd/sim_one_comp_mm_elim_abs.stan 0.24 0.25 0.97 -3.09% slower
pkpd/one_comp_mm_elim_abs.stan 19.06 18.68 1.02 1.99% faster
garch/garch.stan 0.43 0.4 1.07 6.25% faster
low_dim_gauss_mix/low_dim_gauss_mix.stan 2.67 2.56 1.04 4.13% faster
arK/arK.stan 1.75 1.72 1.02 1.85% faster
gp_pois_regr/gp_pois_regr.stan 2.8 2.71 1.03 3.15% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 8.51 8.39 1.01 1.36% faster
performance.compilation 177.97 178.78 1.0 -0.45% slower
Mean result: 1.0235379783466787

Jenkins Console Log
Blue Ocean
Commit hash: e775f450bb86eacc0a0443dbaf4a36bc3407e56f


Machine information No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focal

CPU:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
Address sizes: 46 bits physical, 48 bits virtual
CPU(s): 80
On-line CPU(s) list: 0-79
Thread(s) per core: 2
Core(s) per socket: 20
Socket(s): 2
NUMA node(s): 2
Vendor ID: GenuineIntel
CPU family: 6
Model: 85
Model name: Intel(R) Xeon(R) Gold 6148 CPU @ 2.40GHz
Stepping: 4
CPU MHz: 2400.000
CPU max MHz: 3700.0000
CPU min MHz: 1000.0000
BogoMIPS: 4800.00
Virtualization: VT-x
L1d cache: 1.3 MiB
L1i cache: 1.3 MiB
L2 cache: 40 MiB
L3 cache: 55 MiB
NUMA node0 CPU(s): 0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78
NUMA node1 CPU(s): 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59,61,63,65,67,69,71,73,75,77,79
Vulnerability Gather data sampling: Mitigation; Microcode
Vulnerability Itlb multihit: KVM: Mitigation: VMX disabled
Vulnerability L1tf: Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable
Vulnerability Mds: Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Meltdown: Mitigation; PTI
Vulnerability Mmio stale data: Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Reg file data sampling: Not affected
Vulnerability Retbleed: Mitigation; IBRS
Vulnerability Spec rstack overflow: Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2: Mitigation; IBRS; IBPB conditional; STIBP conditional; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected
Vulnerability Srbds: Not affected
Vulnerability Tsx async abort: Mitigation; Clear CPU buffers; SMT vulnerable
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single pti intel_ppin ssbd mba ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req pku ospke md_clear flush_l1d arch_capabilities

G++:
g++ (Ubuntu 9.4.0-1ubuntu1~20.04) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Clang:
clang version 10.0.0-4ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

@WardBrian
Copy link
Member

Looks like that full run passed 👍🏻

@andrjohns are you able to review again? Thanks!

Copy link
Member

@WardBrian WardBrian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went through all of @andrjohns comments to confirm they were addressed, and a few other small things stood out to me

@stan-buildbot
Copy link
Contributor


Name Old Result New Result Ratio Performance change( 1 - new / old )
arma/arma.stan 0.3 0.31 0.99 -0.79% slower
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.01 0.01 1.1 9.21% faster
gp_regr/gen_gp_data.stan 0.03 0.02 1.08 7.01% faster
gp_regr/gp_regr.stan 0.09 0.09 1.07 6.89% faster
sir/sir.stan 69.61 66.67 1.04 4.23% faster
irt_2pl/irt_2pl.stan 3.98 3.96 1.0 0.37% faster
eight_schools/eight_schools.stan 0.06 0.05 1.09 8.1% faster
pkpd/sim_one_comp_mm_elim_abs.stan 0.27 0.24 1.12 10.81% faster
pkpd/one_comp_mm_elim_abs.stan 18.91 18.46 1.02 2.39% faster
garch/garch.stan 0.45 0.4 1.12 10.51% faster
low_dim_gauss_mix/low_dim_gauss_mix.stan 2.78 2.57 1.08 7.49% faster
arK/arK.stan 1.95 1.7 1.15 12.8% faster
gp_pois_regr/gp_pois_regr.stan 2.91 2.75 1.06 5.5% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 9.09 8.34 1.09 8.17% faster
performance.compilation 181.41 177.75 1.02 2.02% faster
Mean result: 1.0691770183654012

Jenkins Console Log
Blue Ocean
Commit hash: 792180447d143165290a7cf746790514fdd0d084


Machine information No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focal

CPU:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
Address sizes: 46 bits physical, 48 bits virtual
CPU(s): 80
On-line CPU(s) list: 0-79
Thread(s) per core: 2
Core(s) per socket: 20
Socket(s): 2
NUMA node(s): 2
Vendor ID: GenuineIntel
CPU family: 6
Model: 85
Model name: Intel(R) Xeon(R) Gold 6148 CPU @ 2.40GHz
Stepping: 4
CPU MHz: 2400.000
CPU max MHz: 3700.0000
CPU min MHz: 1000.0000
BogoMIPS: 4800.00
Virtualization: VT-x
L1d cache: 1.3 MiB
L1i cache: 1.3 MiB
L2 cache: 40 MiB
L3 cache: 55 MiB
NUMA node0 CPU(s): 0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78
NUMA node1 CPU(s): 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59,61,63,65,67,69,71,73,75,77,79
Vulnerability Gather data sampling: Mitigation; Microcode
Vulnerability Itlb multihit: KVM: Mitigation: VMX disabled
Vulnerability L1tf: Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable
Vulnerability Mds: Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Meltdown: Mitigation; PTI
Vulnerability Mmio stale data: Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Reg file data sampling: Not affected
Vulnerability Retbleed: Mitigation; IBRS
Vulnerability Spec rstack overflow: Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2: Mitigation; IBRS; IBPB conditional; STIBP conditional; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected
Vulnerability Srbds: Not affected
Vulnerability Tsx async abort: Mitigation; Clear CPU buffers; SMT vulnerable
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single pti intel_ppin ssbd mba ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req pku ospke md_clear flush_l1d arch_capabilities

G++:
g++ (Ubuntu 9.4.0-1ubuntu1~20.04) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Clang:
clang version 10.0.0-4ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

WardBrian
WardBrian previously approved these changes Aug 12, 2025
Copy link
Member

@WardBrian WardBrian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just gave this a once over and it looks good to me now. We can wait a couple days before merging if you want to give @andrjohns another chance to look

@stan-buildbot
Copy link
Contributor


Name Old Result New Result Ratio Performance change( 1 - new / old )
arma/arma.stan 0.32 0.3 1.07 6.67% faster
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.01 0.01 1.13 11.19% faster
gp_regr/gen_gp_data.stan 0.02 0.02 1.05 4.49% faster
gp_regr/gp_regr.stan 0.09 0.09 1.04 4.18% faster
sir/sir.stan 70.79 67.14 1.05 5.16% faster
irt_2pl/irt_2pl.stan 4.25 4.02 1.06 5.47% faster
eight_schools/eight_schools.stan 0.06 0.05 1.06 5.98% faster
pkpd/sim_one_comp_mm_elim_abs.stan 0.27 0.24 1.11 9.94% faster
pkpd/one_comp_mm_elim_abs.stan 19.41 18.67 1.04 3.81% faster
garch/garch.stan 0.45 0.4 1.12 10.41% faster
low_dim_gauss_mix/low_dim_gauss_mix.stan 2.77 2.55 1.09 8.04% faster
arK/arK.stan 1.91 1.71 1.12 10.55% faster
gp_pois_regr/gp_pois_regr.stan 2.84 2.72 1.04 4.24% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 8.88 8.35 1.06 5.97% faster
performance.compilation 177.67 179.98 0.99 -1.3% slower
Mean result: 1.068712301746707

Jenkins Console Log
Blue Ocean
Commit hash: a534e266df6d111101436ad27c8924a8af101e00


Machine information No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focal

CPU:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
Address sizes: 46 bits physical, 48 bits virtual
CPU(s): 80
On-line CPU(s) list: 0-79
Thread(s) per core: 2
Core(s) per socket: 20
Socket(s): 2
NUMA node(s): 2
Vendor ID: GenuineIntel
CPU family: 6
Model: 85
Model name: Intel(R) Xeon(R) Gold 6148 CPU @ 2.40GHz
Stepping: 4
CPU MHz: 2400.000
CPU max MHz: 3700.0000
CPU min MHz: 1000.0000
BogoMIPS: 4800.00
Virtualization: VT-x
L1d cache: 1.3 MiB
L1i cache: 1.3 MiB
L2 cache: 40 MiB
L3 cache: 55 MiB
NUMA node0 CPU(s): 0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78
NUMA node1 CPU(s): 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59,61,63,65,67,69,71,73,75,77,79
Vulnerability Gather data sampling: Mitigation; Microcode
Vulnerability Itlb multihit: KVM: Mitigation: VMX disabled
Vulnerability L1tf: Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable
Vulnerability Mds: Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Meltdown: Mitigation; PTI
Vulnerability Mmio stale data: Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Reg file data sampling: Not affected
Vulnerability Retbleed: Mitigation; IBRS
Vulnerability Spec rstack overflow: Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2: Mitigation; IBRS; IBPB conditional; STIBP conditional; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected
Vulnerability Srbds: Not affected
Vulnerability Tsx async abort: Mitigation; Clear CPU buffers; SMT vulnerable
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single pti intel_ppin ssbd mba ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req pku ospke md_clear flush_l1d arch_capabilities

G++:
g++ (Ubuntu 9.4.0-1ubuntu1~20.04) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Clang:
clang version 10.0.0-4ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

@SteveBronder SteveBronder merged commit a2d0372 into develop Aug 14, 2025
34 checks passed
@WardBrian WardBrian deleted the fix/exprs-apply branch August 14, 2025 19:54
@WardBrian
Copy link
Member

@SteveBronder it looks like a few of the SoA signatures aren't compiling any more:
https://jenkins.flatironinstitute.org/blue/organizations/jenkins/Stan%2FStanc3/detail/PR-1527/7/pipeline

'../test/integration/good/function-signatures/math/matrix/trigamma.stan' had fails '[]' and errors '['Did not compile!']'
'../test/integration/good/function-signatures/math/matrix/trunc.stan' had fails '[]' and errors '['Did not compile!']'
'../test/integration/good/function-signatures/math/matrix/bessel_second_kind.stan' had fails '[]' and errors '['Did not compile!']'
'../test/integration/good/function-signatures/math/matrix/round.stan' had fails '[]' and errors '['Did not compile!']'

All of the errors are error: no matching function for call to 'NAME', except for the bessel second kind, which is

In file included from ../../test/integration/good/function-signatures/math/matrix/bessel_second_kind.hpp:1:
In file included from stan/src/stan/model/model_header.hpp:6:
In file included from /home/jenkins/workspace/Stan_Stanc3_PR-1527@2/compile-good-O1/performance-tests-cmdstan/cmdstan/stan/lib/stan_math/stan/math.hpp:19:
In file included from /home/jenkins/workspace/Stan_Stanc3_PR-1527@2/compile-good-O1/performance-tests-cmdstan/cmdstan/stan/lib/stan_math/stan/math/rev.hpp:11:
In file included from /home/jenkins/workspace/Stan_Stanc3_PR-1527@2/compile-good-O1/performance-tests-cmdstan/cmdstan/stan/lib/stan_math/stan/math/rev/constraint.hpp:24:
In file included from /home/jenkins/workspace/Stan_Stanc3_PR-1527@2/compile-good-O1/performance-tests-cmdstan/cmdstan/stan/lib/stan_math/stan/math/prim/constraint.hpp:4:
In file included from /home/jenkins/workspace/Stan_Stanc3_PR-1527@2/compile-good-O1/performance-tests-cmdstan/cmdstan/stan/lib/stan_math/stan/math/prim/fun.hpp:32:
/home/jenkins/workspace/Stan_Stanc3_PR-1527@2/compile-good-O1/performance-tests-cmdstan/cmdstan/stan/lib/stan_math/stan/math/prim/fun/bessel_second_kind.hpp:59:16: error: function 'bessel_second_kind<const std::vector<int, std::allocator<int> > &, stan::math::var_value<Eigen::Matrix<double, -1, 1, 0, -1, 1>, void>, nullptr>' with deduced return type cannot be used before it is defined
        return bessel_second_kind(std::forward<decltype(c)>(c),
               ^
/home/jenkins/workspace/Stan_Stanc3_PR-1527@2/compile-good-O1/performance-tests-cmdstan/cmdstan/stan/lib/stan_math/stan/math/rev/functor/apply_scalar_binary.hpp:53:10: note: in instantiation of function template specialization 'stan::math::bessel_second_kind(const std::vector<int, std::allocator<int> > &&, stan::math::var_value<Eigen::Matrix<double, -1, 1, 0, -1, 1>, void> &&)::(anonymous class)::operator()<const std::vector<int, std::allocator<int> > &, stan::math::var_value<Eigen::Matrix<double, -1, 1, 0, -1, 1>, void> >' requested here
  return std::forward<F>(f)(std::forward<T1>(x), std::forward<T2>(y));
         ^
/home/jenkins/workspace/Stan_Stanc3_PR-1527@2/compile-good-O1/performance-tests-cmdstan/cmdstan/stan/lib/stan_math/stan/math/prim/fun/bessel_second_kind.hpp:57:10: note: in instantiation of function template specialization 'stan::math::apply_scalar_binary<(lambda at /home/jenkins/workspace/Stan_Stanc3_PR-1527@2/compile-good-O1/performance-tests-cmdstan/cmdstan/stan/lib/stan_math/stan/math/prim/fun/bessel_second_kind.hpp:58:7), const std::vector<int, std::allocator<int> > &, stan::math::var_value<Eigen::Matrix<double, -1, 1, 0, -1, 1>, void>, nullptr, nullptr>' requested here
  return apply_scalar_binary(
         ^
../../test/integration/good/function-signatures/math/matrix/bessel_second_kind.hpp:1798:21: note: in instantiation of function template specialization 'stan::math::bessel_second_kind<const std::vector<int, std::allocator<int> > &, stan::math::var_value<Eigen::Matrix<double, -1, 1, 0, -1, 1>, void>, nullptr>' requested here
        stan::math::bessel_second_kind(d_int_array,
                    ^
/home/jenkins/workspace/Stan_Stanc3_PR-1527@2/compile-good-O1/performance-tests-cmdstan/cmdstan/stan/lib/stan_math/stan/math/prim/fun/bessel_second_kind.hpp:56:13: note: 'bessel_second_kind<const std::vector<int, std::allocator<int> > &, stan::math::var_value<Eigen::Matrix<double, -1, 1, 0, -1, 1>, void>, nullptr>' declared here
inline auto bessel_second_kind(T1&& a, T2&& b) {

@WardBrian
Copy link
Member

I'm guessing it is because the rev specializations of those functions hardcode var or var_value rather than using template traits

@SteveBronder SteveBronder restored the fix/exprs-apply branch August 15, 2025 14:56
@WardBrian
Copy link
Member

@WardBrian
Copy link
Member

WardBrian commented Aug 15, 2025

@SteveBronder do you have a sense of how easy this would be to fix?

If it ends up being complicated I would advocate for reverting this and leaving math as-is in the release, for the sake of getting all the other bugfixes in the last 8 months in the hands of users

@SteveBronder
Copy link
Collaborator Author

'../test/integration/good/function-signatures/math/matrix/trigamma.stan' had fails '[]' and errors '['Did not compile!']'
'../test/integration/good/function-signatures/math/matrix/trunc.stan' had fails '[]' and errors '['Did not compile!']'
'../test/integration/good/function-signatures/math/matrix/bessel_second_kind.stan' had fails '[]' and errors '['Did not compile!']'
'../test/integration/good/function-signatures/math/matrix/round.stan' had fails '[]' and errors '['Did not compile!']'

I'm looking at these functions and idt trigamma, trunc, and round should have worked for SoA matrices in the first place. Like looking at trigamma they all use trigama_impl which is only made for scalar types. And round and trunc do not have SoA specializations either. So I think in the compiler we should turn those ones off.

The Bessel function one I think is a real error from ADL since the perfect forwarding function is being chosen over the others. I can open up a PR for that one rn

@WardBrian
Copy link
Member

I'm looking at these functions and idt trigamma, trunc, and round should have worked for SoA matrices in the first place.

Are you sure? They've been marked as SoA supported since the first compiler pr stan-dev/stanc3#955.
Why have they been compiling until now?

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

Successfully merging this pull request may close these issues.

Undefined behavior in combination of apply_scalar_binary/make_holder/to_ref_if
5 participants