Skip to content

Add vectorized versions of the free functions#2574

Merged
SteveBronder merged 5 commits into
developfrom
feature/vectorized-free
Sep 3, 2021
Merged

Add vectorized versions of the free functions#2574
SteveBronder merged 5 commits into
developfrom
feature/vectorized-free

Conversation

@SteveBronder
Copy link
Copy Markdown
Collaborator

Summary

This is for stan-dev/stanc3#947 and allows the compiler to simply call the free functions for standard vectors holding containers instead of generating a for loop and iterating over them.

All the functions here are pretty much the same with the scheme being

template <typename T, require_std_vector_t<T>* = nullptr>
auto <constrain>_free(const T& x) {
  std::vector<decltype(<constrain>_free(x[0]))> x_free(x.size());
  std::transform(x.begin(), x.end(), x_free.begin(), [](auto&& xx) {
    return <constrain>_free(xx);
  });
  return x_free;
}

Tests

Tests are added in each *_constrain_test.cpp to run the standard test to check input/output but instead uses a standard vector of eigen types. Change tests can be run using

./runTests.py -j4 test/unit/math/prim/fun/cholesky_corr_transform_test.cpp \
test/unit/math/prim/fun/cholesky_factor_transform_test.cpp \
test/unit/math/prim/fun/corr_matrix_transform_test.cpp \
test/unit/math/prim/fun/cov_matrix_transform_test.cpp \
test/unit/math/prim/fun/ordered_transform_test.cpp \
test/unit/math/prim/fun/positive_ordered_transform_test.cpp \
test/unit/math/prim/fun/simplex_transform_test.cpp \
test/unit/math/prim/fun/unit_vector_transform_test.cpp

Side Effects

Nope!

Release notes

Vectorize unconstrain functions

Checklist

  • Math issue #(issue number)

  • 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

Copy link
Copy Markdown
Member

@bob-carpenter bob-carpenter left a comment

Choose a reason for hiding this comment

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

Thanks. This looks good.

I have a question about whether this will work for size 0 vectors as I don't know enough about decltype actually works. Assuming that's OK, this is OK to merge.

I made a suggestion for eliminating boilerplate, but it should be considered optional. It also contains a suggestion for solving the x[0] not existing problem brought up in the first comment.

Comment thread stan/math/prim/fun/cholesky_corr_free.hpp
Comment thread stan/math/prim/fun/cholesky_factor_free.hpp
@stan-buildbot
Copy link
Copy Markdown
Contributor


Name Old Result New Result Ratio Performance change( 1 - new / old )
gp_pois_regr/gp_pois_regr.stan 3.55 3.48 1.02 1.95% faster
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.02 0.02 0.98 -2.31% slower
eight_schools/eight_schools.stan 0.09 0.09 1.0 -0.23% slower
gp_regr/gp_regr.stan 0.14 0.14 1.0 -0.34% slower
irt_2pl/irt_2pl.stan 5.07 5.06 1.0 0.28% faster
performance.compilation 90.9 88.77 1.02 2.35% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 8.14 8.24 0.99 -1.23% slower
pkpd/one_comp_mm_elim_abs.stan 31.82 29.56 1.08 7.12% faster
sir/sir.stan 124.02 120.32 1.03 2.98% faster
gp_regr/gen_gp_data.stan 0.03 0.03 0.98 -1.87% slower
low_dim_gauss_mix/low_dim_gauss_mix.stan 2.96 3.05 0.97 -3.21% slower
pkpd/sim_one_comp_mm_elim_abs.stan 0.37 0.39 0.95 -5.39% slower
arK/arK.stan 2.04 2.05 1.0 -0.48% slower
arma/arma.stan 0.25 0.25 1.02 1.83% faster
garch/garch.stan 0.6 0.61 0.98 -1.73% slower
Mean result: 1.00065898959

Jenkins Console Log
Blue Ocean
Commit hash: f35f8f1


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
Copy Markdown
Member

@bob-carpenter bob-carpenter left a comment

Choose a reason for hiding this comment

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

Thanks! So glad you could get rid of all that boilerplate and use an existing framework. Feel free to merge if tests pass.

Comment thread stan/math/prim/fun/cholesky_factor_free.hpp
@stan-buildbot
Copy link
Copy Markdown
Contributor


Name Old Result New Result Ratio Performance change( 1 - new / old )
gp_pois_regr/gp_pois_regr.stan 3.5 3.51 1.0 -0.36% slower
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.02 0.02 0.99 -0.57% slower
eight_schools/eight_schools.stan 0.09 0.09 1.01 1.41% faster
gp_regr/gp_regr.stan 0.14 0.14 1.0 0.06% faster
irt_2pl/irt_2pl.stan 5.09 5.08 1.0 0.22% faster
performance.compilation 91.07 88.83 1.03 2.47% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 8.33 8.13 1.02 2.41% faster
pkpd/one_comp_mm_elim_abs.stan 29.65 31.09 0.95 -4.84% slower
sir/sir.stan 121.3 121.39 1.0 -0.07% slower
gp_regr/gen_gp_data.stan 0.03 0.03 0.99 -1.03% slower
low_dim_gauss_mix/low_dim_gauss_mix.stan 2.96 2.99 0.99 -1.07% slower
pkpd/sim_one_comp_mm_elim_abs.stan 0.37 0.39 0.96 -3.85% slower
arK/arK.stan 2.05 2.04 1.0 0.43% faster
arma/arma.stan 0.25 0.25 0.99 -1.52% slower
garch/garch.stan 0.6 0.6 1.0 0.42% faster
Mean result: 0.996448210928

Jenkins Console Log
Blue Ocean
Commit hash: 45dd53f


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

@SteveBronder SteveBronder merged commit d59561b into develop Sep 3, 2021
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.

3 participants