math icon indicating copy to clipboard operation
math copied to clipboard

Add perfect forwarding and constexpr to reverse mode functions

Open SteveBronder opened this issue 1 year ago • 15 comments

Summary

This is a pretty decently sized PR that does a few things

  1. For functions where perfect forwarding would be nice, we change the signature to do that so temporaries can use the new move semantics from #2928
  2. While I was cleaning doing that I found a lot of places in the reverse mode code that we could use if constexpr and remove uses of promote_scalar_t and forward_as. This gets rid of a lot of overhead metaprogramming we had to do with c++14. Using if constexpr also lead to a lot of other little nice cleanups with redundant code
  3. Return types for all these functions are now an arena type. This is fine for the stan language since we always cast them to an eigen matrix type which will force a copy

Tests

All current tests pass

Side Effects

Release notes

Add perfect forwarding to reverse mode autodiff functions

Checklist

  • [x] 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/)

  • [x] 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)
  • [x] the code is written in idiomatic C++ and changes are documented in the doxygen

  • [x] the new changes are tested

SteveBronder avatar Jul 12 '24 22:07 SteveBronder

Oh clang-format made a wild choice for formatting of long if constexprs. I'll see what's going on here

SteveBronder avatar Jul 12 '24 22:07 SteveBronder

I'm generally in favour of move-semantics, but are we ready to start enforcing a c++17 minimum with if constexpr? That implies a hard requirement for the next Stan version to support C++17, and that feels a little soon if we only started warning with the current release

andrjohns avatar Jul 13 '24 20:07 andrjohns

The message we have says

C++17 support is mandatory in the next release of Stan.

I think it’s fine? People can always use older versions, and basic C++17 support is very old by now

WardBrian avatar Jul 13 '24 20:07 WardBrian

Ah I didn't pay attention to the actual warning, well that makes sense to me then! I'll give this a proper review tomorrow

andrjohns avatar Jul 13 '24 20:07 andrjohns

@andrjohns I need to figure out the weird clang-format stuff before this is ready for review. It's breaking type traits over to newlines for some reason

SteveBronder avatar Jul 13 '24 21:07 SteveBronder

No wokkas, feel free to ping me when it's sorted

andrjohns avatar Jul 13 '24 21:07 andrjohns

@andrjohns Alrighty I think this is ready for review!

SteveBronder avatar Jul 15 '24 20:07 SteveBronder


Name Old Result New Result Ratio Performance change( 1 - new / old )
arma/arma.stan 0.36 0.33 1.08 7.47% faster
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.01 0.01 0.93 -7.69% slower
gp_regr/gen_gp_data.stan 0.02 0.02 1.03 2.74% faster
gp_regr/gp_regr.stan 0.1 0.1 0.98 -1.67% slower
sir/sir.stan 70.95 70.36 1.01 0.83% faster
irt_2pl/irt_2pl.stan 4.29 4.22 1.02 1.65% faster
eight_schools/eight_schools.stan 0.06 0.06 1.07 6.83% faster
pkpd/sim_one_comp_mm_elim_abs.stan 0.25 0.25 1.0 0.38% faster
pkpd/one_comp_mm_elim_abs.stan 19.87 18.71 1.06 5.85% faster
garch/garch.stan 0.47 0.41 1.16 13.79% faster
low_dim_gauss_mix/low_dim_gauss_mix.stan 2.81 2.63 1.07 6.11% faster
arK/arK.stan 1.81 1.72 1.05 4.73% faster
gp_pois_regr/gp_pois_regr.stan 2.84 2.74 1.04 3.58% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 8.98 8.37 1.07 6.84% faster
performance.compilation 185.26 183.52 1.01 0.94% faster
Mean result: 1.038680038150237

Jenkins Console Log Blue Ocean Commit hash: 7c9d4581cb825c3e95cbeb0a41bad59cdf569579


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

stan-buildbot avatar Jul 17 '24 05:07 stan-buildbot

Quick clarification question, you've marked a bunch of functions as inline now - any particular motivation there?

All functions in stan math should be inline so that we are allowed to have multiple definitions across translation units.

Also, with several changes of return types to arena<T> (from just T), is to avoid a copy on return or something else?

This is so we avoid a copy on the return. It's one of the bigger changes we agreed to with the bump to 5.0

SteveBronder avatar Jul 17 '24 18:07 SteveBronder


Name Old Result New Result Ratio Performance change( 1 - new / old )
arma/arma.stan 0.35 0.33 1.08 7.53% faster
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.01 0.01 0.94 -6.4% slower
gp_regr/gen_gp_data.stan 0.03 0.02 1.19 16.02% faster
gp_regr/gp_regr.stan 0.1 0.1 1.04 3.99% faster
sir/sir.stan 69.34 70.09 0.99 -1.08% slower
irt_2pl/irt_2pl.stan 4.17 4.19 1.0 -0.41% slower
eight_schools/eight_schools.stan 0.06 0.06 1.03 3.29% faster
pkpd/sim_one_comp_mm_elim_abs.stan 0.25 0.25 1.0 0.15% faster
pkpd/one_comp_mm_elim_abs.stan 19.79 18.61 1.06 5.96% faster
garch/garch.stan 0.43 0.41 1.07 6.64% faster
low_dim_gauss_mix/low_dim_gauss_mix.stan 2.75 2.6 1.06 5.49% faster
arK/arK.stan 1.79 1.71 1.04 4.08% faster
gp_pois_regr/gp_pois_regr.stan 2.85 2.68 1.06 5.67% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 8.69 8.34 1.04 4.08% faster
performance.compilation 181.73 183.65 0.99 -1.06% slower
Mean result: 1.040106662702048

Jenkins Console Log Blue Ocean Commit hash: c83a397be67c59ba135871db25c53717e78d796e


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: 1138.945 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 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 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

stan-buildbot avatar Jul 19 '24 10:07 stan-buildbot

fyi I'm going to do just the constraints I've done so far. It's a lot of code so I'm going to hold off and fix those in another PR

SteveBronder avatar Jul 19 '24 20:07 SteveBronder


Name Old Result New Result Ratio Performance change( 1 - new / old )
arma/arma.stan 0.35 0.35 1.01 0.95% faster
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.01 0.01 0.98 -1.78% slower
gp_regr/gen_gp_data.stan 0.02 0.02 1.01 1.38% faster
gp_regr/gp_regr.stan 0.11 0.1 1.13 11.45% faster
sir/sir.stan 74.72 70.55 1.06 5.58% faster
irt_2pl/irt_2pl.stan 4.86 3.88 1.25 20.26% faster
eight_schools/eight_schools.stan 0.07 0.05 1.22 17.74% faster
pkpd/sim_one_comp_mm_elim_abs.stan 0.27 0.24 1.12 10.77% faster
pkpd/one_comp_mm_elim_abs.stan 21.33 18.82 1.13 11.75% faster
garch/garch.stan 0.48 0.41 1.17 14.61% faster
low_dim_gauss_mix/low_dim_gauss_mix.stan 3.0 2.61 1.15 12.81% faster
arK/arK.stan 1.95 1.72 1.13 11.66% faster
gp_pois_regr/gp_pois_regr.stan 3.14 2.76 1.14 12.04% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 9.54 8.42 1.13 11.75% faster
performance.compilation 211.48 211.37 1.0 0.05% faster
Mean result: 1.1092383445345873

Jenkins Console Log Blue Ocean Commit hash: 2cd95be39b0482d0fedb7fe9a1e8c77b7084592a


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

stan-buildbot avatar Jul 29 '24 22:07 stan-buildbot

@andrjohns this is ready for review!

SteveBronder avatar Jul 30 '24 15:07 SteveBronder

@SteveBronder are you hoping to get this in before the release? I noticed the makefile changes would also be useful for #3110

WardBrian avatar Sep 27 '24 14:09 WardBrian

Let me give this one hard look over and then I think it's ready to merge

SteveBronder avatar Sep 27 '24 14:09 SteveBronder

This ended up with a bit too much feature creep. I'm going to break this down into two smaller PRs

SteveBronder avatar Feb 10 '25 15:02 SteveBronder