Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds testing suite for nested binary var matrix functions #2502

Merged
merged 15 commits into from
Jun 30, 2021

Conversation

SteveBronder
Copy link
Collaborator

@SteveBronder SteveBronder commented Jun 9, 2021

Summary

So @andrjohns in #2461 you were totally correct that I wasn't handling nested array structures correctly for var matrix. This fixes that by

  1. adding overloads for apply_scalar_binary() for handling var_value<Matrix> types
  2. adding an overload for as_array_or_scalar() that makes a 2d array from std::vector<std::vector<Scalar>> types
  3. Adding functions to the test suite for testing binary vectorized functions (also with integer containers on the left and right hand side)

With the overload for as_array_or_scalar() this was actually pretty darn easy!

Tests

@andrjohns can you check expect_ad_vectorized_matvar to make sure I'm catching all the specializations I need? I was a bit confused and wasn't totally sure if I caught them all. You can run the functions changed in the PR with

./runTests.py test/unit/math/mix/fun/bessel_first_kind_test.cpp \
test/unit/math/mix/fun/bessel_second_kind_test.cpp \
test/unit/math/mix/fun/beta_test.cpp \
test/unit/math/mix/fun/binary_log_loss_test.cpp \
test/unit/math/mix/fun/falling_factorial_test.cpp 

Side Effects

Nope

Release notes

Adds nested vectorized functions for the new matrix type.

Checklist

  • Math issue How to add static matrix? #1805

  • 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

@stan-buildbot
Copy link
Contributor


Name Old Result New Result Ratio Performance change( 1 - new / old )
gp_pois_regr/gp_pois_regr.stan 1.97 1.95 1.01 1.29% faster
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.01 0.01 1.0 0.47% faster
eight_schools/eight_schools.stan 0.07 0.06 1.02 1.5% faster
gp_regr/gp_regr.stan 0.1 0.1 1.02 1.63% faster
irt_2pl/irt_2pl.stan 3.13 3.1 1.01 0.86% faster
performance.compilation 64.34 60.8 1.06 5.5% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 6.2 6.08 1.02 1.87% faster
pkpd/one_comp_mm_elim_abs.stan 18.12 18.13 1.0 -0.01% slower
sir/sir.stan 81.74 78.91 1.04 3.47% faster
gp_regr/gen_gp_data.stan 0.02 0.02 1.03 2.78% faster
garch/garch.stan 0.33 0.31 1.05 4.96% faster
pkpd/sim_one_comp_mm_elim_abs.stan 0.25 0.26 0.99 -0.8% slower
arK/arK.stan 1.24 1.23 1.01 0.63% faster
arma/arma.stan 0.44 0.43 1.02 1.91% faster
low_dim_gauss_mix/low_dim_gauss_mix.stan 2.0 2.03 0.98 -1.53% slower
Mean result: 1.01698673147

Jenkins Console Log
Blue Ocean
Commit hash: 3fb1507


Machine information ProductName: Mac OS X ProductVersion: 10.14.6 BuildVersion: 18G8022

CPU:
Intel(R) Core(TM) i7-8700B CPU @ 3.20GHz

G++:
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

Clang:
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

@andrjohns
Copy link
Collaborator

Great! I'll have a look on the weekend

Copy link
Collaborator

@andrjohns andrjohns left a comment

Choose a reason for hiding this comment

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

Review (finally)! The testing all looks very comprehensive, so no issues there. Just a couple of suggestions for simplifying the apply_scalar_binary overloads

stan/math/rev/functor/apply_scalar_binary.hpp Show resolved Hide resolved
stan/math/rev/functor/apply_scalar_binary.hpp Show resolved Hide resolved
Comment on lines 152 to 154
* Note: The return expresssion needs to be evaluated, otherwise the captured
* function and scalar fall out of scope.
*/
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can also cut this comment since unaryExpr doesn't get called for varmat types

stan/math/rev/functor/apply_scalar_binary.hpp Outdated Show resolved Hide resolved
test/unit/math/mix/fun/beta2_test.cpp Show resolved Hide resolved
test/unit/math/test_ad.hpp Outdated Show resolved Hide resolved
test/unit/math/test_ad_matvar.hpp Show resolved Hide resolved
@stan-buildbot
Copy link
Contributor


Name Old Result New Result Ratio Performance change( 1 - new / old )
gp_pois_regr/gp_pois_regr.stan 3.1 3.15 0.98 -1.63% slower
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.02 0.02 0.99 -0.77% slower
eight_schools/eight_schools.stan 0.12 0.11 1.06 5.45% faster
gp_regr/gp_regr.stan 0.16 0.16 0.98 -1.89% slower
irt_2pl/irt_2pl.stan 5.88 5.93 0.99 -0.97% slower
performance.compilation 88.99 86.84 1.02 2.41% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 9.19 8.74 1.05 4.88% faster
pkpd/one_comp_mm_elim_abs.stan 29.77 30.23 0.98 -1.54% slower
sir/sir.stan 124.56 125.1 1.0 -0.43% slower
gp_regr/gen_gp_data.stan 0.04 0.04 1.01 0.76% faster
low_dim_gauss_mix/low_dim_gauss_mix.stan 3.0 3.03 0.99 -0.96% slower
pkpd/sim_one_comp_mm_elim_abs.stan 0.39 0.4 0.99 -1.49% slower
arK/arK.stan 2.55 2.55 1.0 0.01% faster
arma/arma.stan 0.64 0.63 1.02 1.7% faster
garch/garch.stan 0.68 0.68 1.0 -0.02% slower
Mean result: 1.00419979892

Jenkins Console Log
Blue Ocean
Commit hash: 42b5287


Machine information ProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G22010

CPU:
Intel(R) Xeon(R) CPU E5-1680 v2 @ 3.00GHz

G++:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.6.0
Thread model: posix

Clang:
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.6.0
Thread model: posix

stan/math/rev/functor/apply_scalar_binary.hpp Show resolved Hide resolved
Comment on lines 68 to 74
template <typename T1, typename T2, typename F,
require_std_vector_vt<std::is_integral, T1>* = nullptr,
require_var_matrix_t<T2>* = nullptr>
inline auto apply_scalar_binary(const T1& x, const T2& y, const F& f) {
check_matching_sizes("Binary function", "x", x, "y", y);
return f(x, y);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
template <typename T1, typename T2, typename F,
require_std_vector_vt<std::is_integral, T1>* = nullptr,
require_var_matrix_t<T2>* = nullptr>
inline auto apply_scalar_binary(const T1& x, const T2& y, const F& f) {
check_matching_sizes("Binary function", "x", x, "y", y);
return f(x, y);
}
template <typename T1, typename T2, typename F,
require_any_std_vector_vt<std::is_integral, T1, T2>* = nullptr,
require_any_var_matrix_t<T1, T2>* = nullptr>
inline auto apply_scalar_binary(const T1& x, const T2& y, const F& f) {
check_matching_sizes("Binary function", "x", x, "y", y);
return f(x, y);
}

Then you don't need the separate overloads

stan/math/rev/functor/apply_scalar_binary.hpp Outdated Show resolved Hide resolved
@stan-buildbot
Copy link
Contributor


Name Old Result New Result Ratio Performance change( 1 - new / old )
gp_pois_regr/gp_pois_regr.stan 3.11 3.06 1.02 1.75% faster
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.02 0.02 0.96 -3.91% slower
eight_schools/eight_schools.stan 0.11 0.11 1.0 -0.15% slower
gp_regr/gp_regr.stan 0.16 0.16 0.99 -0.5% slower
irt_2pl/irt_2pl.stan 5.98 5.87 1.02 1.93% faster
performance.compilation 87.94 86.59 1.02 1.54% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 8.76 8.77 1.0 -0.16% slower
pkpd/one_comp_mm_elim_abs.stan 29.74 29.08 1.02 2.23% faster
sir/sir.stan 126.22 121.22 1.04 3.96% faster
gp_regr/gen_gp_data.stan 0.03 0.04 0.98 -1.85% slower
low_dim_gauss_mix/low_dim_gauss_mix.stan 3.07 3.0 1.02 2.19% faster
pkpd/sim_one_comp_mm_elim_abs.stan 0.42 0.39 1.09 8.09% faster
arK/arK.stan 2.56 2.55 1.0 0.44% faster
arma/arma.stan 0.63 0.63 1.0 -0.37% slower
garch/garch.stan 0.68 0.68 1.01 0.59% faster
Mean result: 1.01134889713

Jenkins Console Log
Blue Ocean
Commit hash: e23c84e


Machine information ProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G22010

CPU:
Intel(R) Xeon(R) CPU E5-1680 v2 @ 3.00GHz

G++:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.6.0
Thread model: posix

Clang:
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.6.0
Thread model: posix

Copy link
Collaborator

@andrjohns andrjohns left a comment

Choose a reason for hiding this comment

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

All looks good!

@SteveBronder SteveBronder merged commit 5c1dae4 into develop Jun 30, 2021
@rok-cesnovar rok-cesnovar deleted the feature/vectorized-binary-funcs1-varmat branch June 30, 2021 06:10
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.

None yet

4 participants