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 unary functions for var<Matrix> #2527

Merged
merged 25 commits into from
Jul 18, 2021
Merged

Conversation

SteveBronder
Copy link
Collaborator

Summary

Adds several unary functions for var matrix as well as overloads for division.

Tests

Tests added for each new function

./runTests.py -j4 test/unit/math/mix/fun/divide_test.cpp \
test/unit/math/mix/fun/inv_cloglog_test.cpp \
test/unit/math/mix/fun/inv_sqrt_test.cpp \
test/unit/math/mix/fun/inv_square_test.cpp \
test/unit/math/mix/fun/inv_test.cpp \
test/unit/math/mix/fun/lambertw_test.cpp \
test/unit/math/mix/fun/lgamma_test.cpp \
test/unit/math/mix/fun/log10_test.cpp \
test/unit/math/mix/fun/log1m_exp_test.cpp \
test/unit/math/mix/fun/log1m_inv_logit_test.cpp \
test/unit/math/mix/fun/log2_test.cpp \
test/unit/math/mix/fun/logit_test.cpp

Side Effects

Nopes

Release notes

Adds several unary functions for var as well as division.

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

@andrjohns
Copy link
Collaborator

I can review this one, just lmk when it's ready

@stan-buildbot
Copy link
Contributor


Name Old Result New Result Ratio Performance change( 1 - new / old )
gp_pois_regr/gp_pois_regr.stan 3.07 3.13 0.98 -1.88% slower
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.02 0.02 0.98 -1.54% slower
eight_schools/eight_schools.stan 0.11 0.12 0.98 -2.51% slower
gp_regr/gp_regr.stan 0.16 0.16 1.02 2.01% faster
irt_2pl/irt_2pl.stan 5.87 5.91 0.99 -0.69% slower
performance.compilation 89.27 86.85 1.03 2.72% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 8.59 8.65 0.99 -0.6% slower
pkpd/one_comp_mm_elim_abs.stan 29.59 29.63 1.0 -0.13% slower
sir/sir.stan 127.19 128.98 0.99 -1.41% slower
gp_regr/gen_gp_data.stan 0.03 0.04 1.0 -0.47% slower
low_dim_gauss_mix/low_dim_gauss_mix.stan 3.0 2.99 1.01 0.56% faster
pkpd/sim_one_comp_mm_elim_abs.stan 0.39 0.39 0.99 -0.97% slower
arK/arK.stan 2.56 2.55 1.0 0.14% faster
arma/arma.stan 0.65 0.65 1.0 -0.33% slower
garch/garch.stan 0.64 0.64 1.0 -0.36% slower
Mean result: 0.996549733023

Jenkins Console Log
Blue Ocean
Commit hash: 87a66ec


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

@hsbadr
Copy link
Member

hsbadr commented Jul 3, 2021

@SteveBronder JFYI; After e80abe9:

stan/math/prim/fun/square.hpp:56:58: error: incomplete typestan::math::apply_scalar_unary<stan::math::square_fun, stan::math::var_value<double>, void>used in nested name specifier
   56 |   return apply_scalar_unary<square_fun, Container>::apply(x);
          detected during:
            instantiation of "auto stan::math::square(const Container &) [with Container=stan::math::var, <unnamed>=(void *)nullptr, <unnamed>=(void *)nullptr, <unnamed>=(void *)nullptr]" at line 207 of "stan/math/prim/core/complex_base.hpp"
            instantiation of "stan::math::complex_base<ValueType>::complex_type &stan::math::complex_base<ValueType>::operator/=(const std::complex<U> &) [with ValueType=stan::math::var, U=stan::math::var]" at line 24 of "stan/math/prim/core/operator_division.hpp"
            instantiation of "stan::complex_return_t<U, V> stan::math::internal::complex_divide(const U &, const V &) [with U=std::complex<stan::math::var>, V=std::complex<stan::math::var>]" at line 122 of "stan/math/rev/core/operator_division.hpp"

@SteveBronder
Copy link
Collaborator Author

Oh my! Yes much appreciated! Where did you get that error?

@hsbadr
Copy link
Member

hsbadr commented Jul 3, 2021

Oh my! Yes much appreciated! Where did you get that error?

Testing with the experimental version of RStan.

@hsbadr
Copy link
Member

hsbadr commented Jul 6, 2021

@SteveBronder

stan/math/prim/core/complex_base.hpp:207:34: error: no matching function for call to ‘square(stan::math::complex_base<stan::math::var_value<double> >::value_type)’
  207 |     value_type sum_sq_im = square(other.real()) + square(other.imag());

stan/math/rev/core/operator_division.hpp:122:34:   required from here
/usr/include/c++/11.1.0/type_traits:2514:11: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
 2514 |     using enable_if_t = typename enable_if<_Cond, _Tp>::type;
Log:
In file included from /usr/lib/R/library/StanHeaders/include/stan/math/rev/core/std_complex.hpp:5,
                 from /usr/lib/R/library/StanHeaders/include/stan/math/rev/core/operator_division.hpp:11,
                 from /usr/lib/R/library/StanHeaders/include/stan/math/rev/core/operator_divide_equal.hpp:5,
                 from /usr/lib/R/library/StanHeaders/include/stan/math/rev/core.hpp:29,
                 from /usr/lib/R/library/StanHeaders/include/src/stan/model/model_base.hpp:5,
                 from Module.cpp:2:
/usr/lib/R/library/StanHeaders/include/stan/math/prim/core/complex_base.hpp: In instantiation of ‘stan::math::complex_base<ValueType>::complex_type& stan::math::complex_base<ValueType>::operator/=(const std::
complex<_Up>&) [with U = stan::math::var_value<double>; ValueType = stan::math::var_value<double>; stan::math::complex_base<ValueType>::complex_type = std::complex<stan::math::var_value<double> >]’:
/usr/lib/R/library/StanHeaders/include/stan/math/prim/core/operator_division.hpp:24:5:   required from ‘stan::complex_return_t<U, V> stan::math::internal::complex_divide(const U&, const V&) [with U = std::com
plex<stan::math::var_value<double> >; V = std::complex<stan::math::var_value<double> >; stan::complex_return_t<U, V> = std::complex<stan::math::var_value<double> >]’
/usr/lib/R/library/StanHeaders/include/stan/math/rev/core/operator_division.hpp:122:34:   required from here
/usr/lib/R/library/StanHeaders/include/stan/math/prim/core/complex_base.hpp:207:34: error: no matching function for call to ‘square(stan::math::complex_base<stan::math::var_value<double> >::value_type)’
  207 |     value_type sum_sq_im = square(other.real()) + square(other.imag());
      |                            ~~~~~~^~~~~~~~~~~~~~
In file included from /usr/lib/R/library/StanHeaders/include/stan/math/prim/core/complex_base.hpp:4,
                 from /usr/lib/R/library/StanHeaders/include/stan/math/rev/core/std_complex.hpp:5,
                 from /usr/lib/R/library/StanHeaders/include/stan/math/rev/core/operator_division.hpp:11,
                 from /usr/lib/R/library/StanHeaders/include/stan/math/rev/core/operator_divide_equal.hpp:5,
                 from /usr/lib/R/library/StanHeaders/include/stan/math/rev/core.hpp:29,
                 from /usr/lib/R/library/StanHeaders/include/src/stan/model/model_base.hpp:5,
                 from Module.cpp:2:
/usr/lib/R/library/StanHeaders/include/stan/math/prim/fun/square.hpp:68:13: note: candidate: ‘template<class Container, stan::require_container_st<std::is_arithmetic, Container>* <anonymous> > auto stan::math
::square(const Container&)’
   68 | inline auto square(const Container& x) {
      |             ^~~~~~
/usr/lib/R/library/StanHeaders/include/stan/math/prim/fun/square.hpp:68:13: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/11.1.0/unordered_map:38,
                 from /usr/lib/R/library/Rcpp/include/Rcpp/platform/compiler.h:153,
                 from /usr/lib/R/library/Rcpp/include/Rcpp/r/headers.h:66,
                 from /usr/lib/R/library/Rcpp/include/RcppCommon.h:30,
                 from /usr/lib/R/library/Rcpp/include/Rcpp.h:27,
                 from Module.cpp:1:
/usr/include/c++/11.1.0/type_traits: In substitution of ‘template<bool _Cond, class _Tp> using enable_if_t = typename std::enable_if::type [with bool _Cond = false; _Tp = void]’:
/usr/lib/R/library/StanHeaders/include/stan/math/prim/meta/require_helpers.hpp:19:7:   required by substitution of ‘template<class Check> using require_t = std::enable_if_t<Check::value> [with Check = std::in
tegral_constant<bool, false>]’
/usr/lib/R/library/StanHeaders/include/stan/math/prim/meta/is_container.hpp:26:1:   required by substitution of ‘template<template<class ...> class TypeCheck, class ... Check> using require_container_st = sta
n::require_t<std::integral_constant<bool, stan::math::conjunction<std::integral_constant<bool, stan::math::disjunction<stan::is_eigen<typename std::decay<Check>::type>, stan::is_std_vector<typename std::decay
<Check>::type, void> >::value>..., TypeCheck<typename stan::scalar_type<Check, void>::type>...>::value> > [with TypeCheck = std::is_arithmetic; Check = {stan::math::var_value<double, void>}]’
/usr/lib/R/library/StanHeaders/include/stan/math/prim/fun/square.hpp:67:66:   required from ‘stan::math::complex_base<ValueType>::complex_type& stan::math::complex_base<ValueType>::operator/=(const std::compl
ex<_Up>&) [with U = stan::math::var_value<double>; ValueType = stan::math::var_value<double>; stan::math::complex_base<ValueType>::complex_type = std::complex<stan::math::var_value<double> >]’
/usr/lib/R/library/StanHeaders/include/stan/math/prim/core/operator_division.hpp:24:5:   required from ‘stan::complex_return_t<U, V> stan::math::internal::complex_divide(const U&, const V&) [with U = std::com
plex<stan::math::var_value<double> >; V = std::complex<stan::math::var_value<double> >; stan::complex_return_t<U, V> = std::complex<stan::math::var_value<double> >]’
/usr/lib/R/library/StanHeaders/include/stan/math/rev/core/operator_division.hpp:122:34:   required from here
/usr/include/c++/11.1.0/type_traits:2514:11: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
 2514 |     using enable_if_t = typename enable_if<_Cond, _Tp>::type;
      |           ^~~~~~~~~~~
In file included from /usr/lib/R/library/StanHeaders/include/stan/math/prim/core/complex_base.hpp:4,
                 from /usr/lib/R/library/StanHeaders/include/stan/math/rev/core/std_complex.hpp:5,
                 from /usr/lib/R/library/StanHeaders/include/stan/math/rev/core/operator_division.hpp:11,
                 from /usr/lib/R/library/StanHeaders/include/stan/math/rev/core/operator_divide_equal.hpp:5,
                 from /usr/lib/R/library/StanHeaders/include/stan/math/rev/core.hpp:29,
                 from /usr/lib/R/library/StanHeaders/include/src/stan/model/model_base.hpp:5,
                 from Module.cpp:2:
/usr/lib/R/library/StanHeaders/include/stan/math/prim/core/complex_base.hpp: In instantiation of ‘stan::math::complex_base<ValueType>::complex_type& stan::math::complex_base<ValueType>::operator/=(const std::
complex<_Up>&) [with U = stan::math::var_value<double>; ValueType = stan::math::var_value<double>; stan::math::complex_base<ValueType>::complex_type = std::complex<stan::math::var_value<double> >]’:
/usr/lib/R/library/StanHeaders/include/stan/math/prim/core/operator_division.hpp:24:5:   required from ‘stan::complex_return_t<U, V> stan::math::internal::complex_divide(const U&, const V&) [with U = std::com
plex<stan::math::var_value<double> >; V = std::complex<stan::math::var_value<double> >; stan::complex_return_t<U, V> = std::complex<stan::math::var_value<double> >]’
/usr/lib/R/library/StanHeaders/include/stan/math/rev/core/operator_division.hpp:122:34:   required from here
/usr/lib/R/library/StanHeaders/include/stan/math/prim/fun/square.hpp:54:13: note: candidate: ‘template<class Container, stan::require_not_stan_scalar_t<Container>* <anonymous>, stan::require_not_var_matrix_t<
Container>* <anonymous>, stan::require_not_nonscalar_prim_or_rev_kernel_expression_t<Container>* <anonymous> > auto stan::math::square(const Container&)’
   54 | inline auto square(const Container& x) {
      |             ^~~~~~
/usr/lib/R/library/StanHeaders/include/stan/math/prim/fun/square.hpp:54:13: note:   template argument deduction/substitution failed:
/usr/lib/R/library/StanHeaders/include/stan/math/prim/fun/square.hpp:26:15: note: candidate: ‘double stan::math::square(double)’
   26 | inline double square(double x) { return std::pow(x, 2); }
      |               ^~~~~~
/usr/lib/R/library/StanHeaders/include/stan/math/prim/fun/square.hpp:26:29: note:   no known conversion for argument 1 from ‘stan::math::complex_base<stan::math::var_value<double> >::value_type’ {aka ‘stan::m
ath::var_value<double>’} to ‘double26 | inline double square(double x) { return std::pow(x, 2); }
      |                      ~~~~~~~^
In file included from /usr/lib/R/library/StanHeaders/include/stan/math/rev/core/std_complex.hpp:5,
                 from /usr/lib/R/library/StanHeaders/include/stan/math/rev/core/operator_division.hpp:11,
                 from /usr/lib/R/library/StanHeaders/include/stan/math/rev/core/operator_divide_equal.hpp:5,
                 from /usr/lib/R/library/StanHeaders/include/stan/math/rev/core.hpp:29,
                 from /usr/lib/R/library/StanHeaders/include/src/stan/model/model_base.hpp:5,
                 from Module.cpp:2:
/usr/lib/R/library/StanHeaders/include/stan/math/prim/core/complex_base.hpp:207:57: error: no matching function for call to ‘square(stan::math::complex_base<stan::math::var_value<double> >::value_type)’
  207 |     value_type sum_sq_im = square(other.real()) + square(other.imag());
      |                                                   ~~~~~~^~~~~~~~~~~~~~
In file included from /usr/lib/R/library/StanHeaders/include/stan/math/prim/core/complex_base.hpp:4,
                 from /usr/lib/R/library/StanHeaders/include/stan/math/rev/core/std_complex.hpp:5,
                 from /usr/lib/R/library/StanHeaders/include/stan/math/rev/core/operator_division.hpp:11,
                 from /usr/lib/R/library/StanHeaders/include/stan/math/rev/core/operator_divide_equal.hpp:5,
                 from /usr/lib/R/library/StanHeaders/include/stan/math/rev/core.hpp:29,
                 from /usr/lib/R/library/StanHeaders/include/src/stan/model/model_base.hpp:5,
                 from Module.cpp:2:
/usr/lib/R/library/StanHeaders/include/stan/math/prim/fun/square.hpp:68:13: note: candidate: ‘template<class Container, stan::require_container_st<std::is_arithmetic, Container>* <anonymous> > auto stan::math
::square(const Container&)’
   68 | inline auto square(const Container& x) {
      |             ^~~~~~
/usr/lib/R/library/StanHeaders/include/stan/math/prim/fun/square.hpp:68:13: note:   template argument deduction/substitution failed:
/usr/lib/R/library/StanHeaders/include/stan/math/prim/fun/square.hpp:54:13: note: candidate: ‘template<class Container, stan::require_not_stan_scalar_t<Container>* <anonymous>, stan::require_not_var_matrix_t<
Container>* <anonymous>, stan::require_not_nonscalar_prim_or_rev_kernel_expression_t<Container>* <anonymous> > auto stan::math::square(const Container&)’
   54 | inline auto square(const Container& x) {
      |             ^~~~~~
/usr/lib/R/library/StanHeaders/include/stan/math/prim/fun/square.hpp:54:13: note:   template argument deduction/substitution failed:
/usr/lib/R/library/StanHeaders/include/stan/math/prim/fun/square.hpp:26:15: note: candidate: ‘double stan::math::square(double)’
   26 | inline double square(double x) { return std::pow(x, 2); }
      |               ^~~~~~
/usr/lib/R/library/StanHeaders/include/stan/math/prim/fun/square.hpp:26:29: note:   no known conversion for argument 1 from ‘stan::math::complex_base<stan::math::var_value<double> >::value_type’ {aka ‘stan::m
ath::var_value<double>’} to ‘double26 | inline double square(double x) { return std::pow(x, 2); }
      |                      ~~~~~~~^
make: *** [/usr/lib64/R/etc/Makeconf:177: Module.o] Error 1

@SteveBronder
Copy link
Collaborator Author

@hsbadr ty! Yes I'm seeing that locally as well. The commit I just made should handle that. The problem was that the complex number stuff was using square() but was importing stuff in a weird way. I just changed the complex number stuff so it used val() * val() instead of directly using square() so should be better

@hsbadr
Copy link
Member

hsbadr commented Jul 6, 2021

@hsbadr ty! Yes I'm seeing that locally as well. The commit I just made should handle that. The problem was that the complex number stuff was using square() but was importing stuff in a weird way. I just changed the complex number stuff so it used val() * val() instead of directly using square() so should be better

Yes, it compiles successfully now. Thanks!

@wds15
Copy link
Contributor

wds15 commented Jul 7, 2021

This PR is also stalled because of a Jenkins hiccup as it looks.

@hsbadr sorry for off-topic... but is there any hope for rstan to hit CRAN any time soon? I am asking as our next major production system is gearing up for an update and I would like to get rstan in a more recent version. If you have any suggestions let me know... should I ping you on discourse (what was your username there again?).

@hsbadr
Copy link
Member

hsbadr commented Jul 7, 2021

is there any hope for rstan to hit CRAN any time soon?

Not until @bgoodri gives it sometime or agrees on creating a new package.

I am asking as our next major production system is gearing up for an update and I would like to get rstan in a more recent version. If you have any suggestions let me know

You may add our r-packages to your repos:

install.packages("StanHeaders", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))

or install the experimental version from GitHub.

should I ping you on discourse (what was your username there again?).

hsbadr

@SteveBronder
Copy link
Collaborator Author

Aight @andrjohns I think this is ready! Couple notes to point out

  1. This adds support for scalar ./ Matrix division
  2. For one of the divide() functions I had to add a is_fvar_or_arithmetic meta type trait
  3. I think the thing that needs most scrutizined are some of the requires I used
  4. Like when I did add() I ended up moving divide() for vars to core/operator_divide
  5. There was some weird goings ons with includes because std::complex<var>'s divide used the square() stan math function, but square() for var was not defined by the time it was used. So in lieu of that I made them into a * a. Not great but also not the worst

Besides that I think it's a pretty normal PR!

@stan-buildbot
Copy link
Contributor


Name Old Result New Result Ratio Performance change( 1 - new / old )
gp_pois_regr/gp_pois_regr.stan 3.15 3.07 1.03 2.56% faster
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.02 0.02 1.0 -0.37% slower
eight_schools/eight_schools.stan 0.11 0.12 0.98 -2.16% slower
gp_regr/gp_regr.stan 0.16 0.16 1.0 0.46% faster
irt_2pl/irt_2pl.stan 5.96 5.88 1.01 1.28% faster
performance.compilation 89.56 86.82 1.03 3.06% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 8.99 8.58 1.05 4.49% faster
pkpd/one_comp_mm_elim_abs.stan 29.37 30.08 0.98 -2.44% slower
sir/sir.stan 129.74 132.8 0.98 -2.36% slower
gp_regr/gen_gp_data.stan 0.03 0.03 1.0 -0.27% slower
low_dim_gauss_mix/low_dim_gauss_mix.stan 3.0 2.99 1.0 0.47% faster
pkpd/sim_one_comp_mm_elim_abs.stan 0.38 0.42 0.91 -9.96% slower
arK/arK.stan 2.53 2.54 1.0 -0.06% slower
arma/arma.stan 0.64 0.65 0.99 -1.32% slower
garch/garch.stan 0.65 0.64 1.0 0.42% faster
Mean result: 0.996836816714

Jenkins Console Log
Blue Ocean
Commit hash: f0d6a83


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.

Looking good! My main feedback is that I think most of the additional rev overloads wouldn't be necessary, as long as the existing specifications were modified to use Math functions that are scalar/Matrix agnostic

*/
template <typename Scalar, typename Mat, require_eigen_t<Mat>* = nullptr,
require_t<bool_constant<std::is_arithmetic<Scalar>::value
|| is_fvar<Scalar>::value>>* = nullptr,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if you could replace all of these specialisations with:

template <typename Ta, typename Tb>
inline auto divide(const Ta& a, const Tb& b) {
  return (as_array_or_scalar(a) / as_array_or_scalar(b)).matrix();
}

With the appropriate requires to disambiguate from the varmat overloads?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh absolutely! Much cleaner!

Comment on lines 36 to 43
template <typename T, require_eigen_t<T>* = nullptr>
inline auto inv(const var_value<T>& a) {
auto denom = to_arena(a.val().array().square());
return make_callback_var(inv(a.val()), [a, denom](auto& vi) mutable {
a.adj().array() -= vi.adj().array() / denom;
});
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

If you use square(), you can combine both overloads:

template <typename T>
inline auto inv(const var_value<T>& a) {
  auto denom = to_arena(square(a.val()));
  return make_callback_var(inv(a.val()), [a, denom](auto& vi) mutable {
    a.adj() -= vi.adj() / denom;
  });
}

@@ -31,6 +31,15 @@ inline var inv_cloglog(const var& a) {
});
}

template <typename T, require_eigen_t<T>* = nullptr>
inline auto inv_cloglog(const var_value<T>& a) {
auto precomp_exp = (a.val().array() - a.val().array().exp()).exp();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here. if you use the Stan Math exp() then you can collapse the overloads

@@ -35,6 +36,14 @@ inline var inv_sqrt(const var& a) {
});
}

template <typename T, require_eigen_t<T>* = nullptr>
inline auto inv_sqrt(const var_value<T>& a) {
auto denom = to_arena(a.val().array() * a.val().array().sqrt());
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'll stop adding comments for this, but same here

@SteveBronder
Copy link
Collaborator Author

Ty on the catch for reducing the number of overloads, I had to make a little change to as_array_or_scalar() and was able to remove almost all the extra overloads!

@SteveBronder
Copy link
Collaborator Author

@andrjohns I think this is ready for review!

@stan-buildbot
Copy link
Contributor


Name Old Result New Result Ratio Performance change( 1 - new / old )
gp_pois_regr/gp_pois_regr.stan 3.05 3.08 0.99 -0.88% slower
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.02 0.02 0.97 -2.62% slower
eight_schools/eight_schools.stan 0.11 0.11 0.99 -1.3% slower
gp_regr/gp_regr.stan 0.16 0.16 1.01 1.21% faster
irt_2pl/irt_2pl.stan 5.93 5.91 1.0 0.33% faster
performance.compilation 89.03 86.57 1.03 2.76% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 8.61 8.56 1.01 0.55% faster
pkpd/one_comp_mm_elim_abs.stan 29.3 29.01 1.01 0.96% faster
sir/sir.stan 126.82 121.95 1.04 3.84% faster
gp_regr/gen_gp_data.stan 0.04 0.04 1.0 -0.05% slower
low_dim_gauss_mix/low_dim_gauss_mix.stan 3.07 3.0 1.02 2.23% faster
pkpd/sim_one_comp_mm_elim_abs.stan 0.4 0.4 1.0 -0.44% slower
arK/arK.stan 2.53 2.53 1.0 0.15% faster
arma/arma.stan 0.64 0.64 1.0 0.05% faster
garch/garch.stan 0.64 0.64 1.0 -0.43% slower
Mean result: 1.00450323365

Jenkins Console Log
Blue Ocean
Commit hash: 4ebad3b


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.

Just some super minor comments, almost done!

@@ -204,7 +204,8 @@ class complex_base {
template <typename U>
complex_type& operator/=(const std::complex<U>& other) {
using stan::math::square;
value_type sum_sq_im = square(other.real()) + square(other.imag());
value_type sum_sq_im
= (other.real() * other.real()) + (other.imag() * other.imag());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Might be worth opening a new issue about the square oddities so it gets looked at

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah I'm going to open a bigger issue about our include orders.

Comment on lines 16 to 17
* Extends std::true_type if all the provided types are either fvar or
* an arithmetic type, extends std::false_type otherwise.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Need to update the doc on this

@@ -295,6 +295,13 @@ class var_value<T, require_floating_point_t<T>> {
}
};

namespace internal {
template <typename T>
using require_matrix_var_value = require_t<bool_constant<
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not a huge fan of defining additional require_s within individual headers, but if you don't see this being used anywhere else then it's fine to leave

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So this one is very particular. I'd like to leave it here till unless we need it in other places in the future. This was just in the vari<> template at first and hasn't been used anywhere else. I just pulled it out because it's a bit cleaner looking.

@stan-buildbot
Copy link
Contributor


Name Old Result New Result Ratio Performance change( 1 - new / old )
gp_pois_regr/gp_pois_regr.stan 3.03 3.02 1.0 0.3% faster
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.02 0.02 0.94 -6.54% slower
eight_schools/eight_schools.stan 0.12 0.11 1.02 1.5% faster
gp_regr/gp_regr.stan 0.16 0.16 0.97 -3.26% slower
irt_2pl/irt_2pl.stan 5.9 5.99 0.98 -1.54% slower
performance.compilation 89.06 86.39 1.03 2.99% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 8.64 8.56 1.01 0.98% faster
pkpd/one_comp_mm_elim_abs.stan 30.75 29.22 1.05 4.98% faster
sir/sir.stan 131.76 123.94 1.06 5.94% faster
gp_regr/gen_gp_data.stan 0.04 0.03 1.01 1.39% faster
low_dim_gauss_mix/low_dim_gauss_mix.stan 3.07 2.99 1.03 2.54% faster
pkpd/sim_one_comp_mm_elim_abs.stan 0.39 0.41 0.94 -6.72% slower
arK/arK.stan 2.53 2.54 1.0 -0.44% slower
arma/arma.stan 0.64 0.65 0.99 -1.22% slower
garch/garch.stan 0.64 0.64 1.0 0.18% faster
Mean result: 1.0019343151

Jenkins Console Log
Blue Ocean
Commit hash: c9c6f0d


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.

LGTM!

@SteveBronder SteveBronder merged commit 8cd7dd0 into develop Jul 18, 2021
@rok-cesnovar rok-cesnovar deleted the feature/varmat-funs-misc1 branch July 19, 2021 06:25
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.

5 participants