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

make var_views assignable #2065

Merged
merged 9 commits into from
Sep 18, 2020
Merged

Conversation

t4c1
Copy link
Contributor

@t4c1 t4c1 commented Sep 8, 2020

Summary

Makes it possible to assign to blocks, and other views into var_value (does not include assignments of single elements).

Tests

Tested assignment of one block to another that contains aliasing.

Side Effects

With this there is no reason anymore to call var_value a static matrix :)

Release notes

Blocks, and other views into var_value can now be assigned to.

Checklist

  • Math issue How to add static matrix? #1805

  • Copyright holder: Tadej Ciglarič

    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 4.27 4.1 1.04 3.83% faster
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.02 0.02 1.01 1.13% faster
eight_schools/eight_schools.stan 0.09 0.09 1.0 -0.11% slower
gp_regr/gp_regr.stan 0.2 0.2 0.98 -2.11% slower
irt_2pl/irt_2pl.stan 5.29 5.26 1.01 0.53% faster
performance.compilation 89.79 87.44 1.03 2.62% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 8.22 8.28 0.99 -0.73% slower
pkpd/one_comp_mm_elim_abs.stan 27.36 27.76 0.99 -1.46% slower
sir/sir.stan 130.48 135.47 0.96 -3.83% slower
gp_regr/gen_gp_data.stan 0.05 0.05 1.01 0.89% faster
low_dim_gauss_mix/low_dim_gauss_mix.stan 3.34 3.33 1.0 0.47% faster
pkpd/sim_one_comp_mm_elim_abs.stan 0.39 0.38 1.03 2.53% faster
arK/arK.stan 2.55 1.87 1.36 26.61% faster
arma/arma.stan 0.74 0.73 1.01 0.97% faster
garch/garch.stan 0.73 0.73 1.0 0.37% faster
Mean result: 1.02793056502

Jenkins Console Log
Blue Ocean
Commit hash: e631b04


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

@SteveBronder SteveBronder left a comment

Choose a reason for hiding this comment

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

Looks like I went a little coo-coo trying to be const correct here when it was not needed lol. Just a note to add a construct so

var_value<Eigen::MatrixXd> A_block = A_v.block(0, 0, 3, 3);

works and then this looks good! I think we will need that for the compiler unless we can get away with using auto in the generated code

it would also be good to add more tests, though I can add a bunch of stress tests in #2064

stan/math/rev/core/var.hpp Show resolved Hide resolved
Comment on lines +433 to +435
template <typename S, require_convertible_t<S&, value_type>* = nullptr,
require_any_not_plain_type_t<T, S>* = nullptr>
var_value<T> operator=(const var_value<S>& other) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

docs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Comment on lines +223 to +232
TEST_F(AgradRev, var_matrix_view_assignment) {
Eigen::MatrixXd A(4, 4);
A << 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15;
stan::math::var_value<Eigen::MatrixXd> A_v(A);
A_v.block(0, 0, 3, 3) = A_v.block(1, 1, 3, 3);
stan::math::sum(stan::math::from_var_value(A_v)).grad();
Eigen::MatrixXd deriv(4, 4);
deriv << 0, 0, 0, 1, 0, 1, 1, 2, 0, 1, 1, 2, 1, 2, 2, 2;
EXPECT_MATRIX_FLOAT_EQ(A_v.adj(), deriv);
}
Copy link
Collaborator

@SteveBronder SteveBronder Sep 8, 2020

Choose a reason for hiding this comment

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

Adding stan::math::var_value<Eigen::MatrixXd> A_block = A_v.block(0, 0, 3, 3); fails with

test/unit/math/rev/core/var_test.cpp: In member function ‘virtual void AgradRev_var_matrix_view_assignment_Test::TestBody()’:
test/unit/math/rev/core/var_test.cpp:228:61: error: conversion from ‘var_value<Eigen::Block<Eigen::Map<Eigen::Matrix<double, -1, -1>, 8, Eigen::Stride<0, 0> >, -1, -1, false>>’ to non-scalar type ‘var_value<Eigen::Matrix<double, -1, -1>>’ requested
  228 |   stan::math::var_value<Eigen::MatrixXd> A_block = A_v.block(0, 0, 3, 3);

I think we just need a little constructor to do that

  /**
   * Construct a `var_value` with an inner plain matrix type from a `var_value`
   *  holding an expression of another matrix.
   * @tparam S A type representing an eigen expression
   * @param other An eigen expression.
   */
  template <typename S, require_convertible_t<S&, value_type>* = nullptr,
            require_not_plain_type_t<S>* = nullptr,
            require_not_same_t<T, S>* = nullptr>
  var_value(const var_value<S>& other) : vi_(new vari_type(other.vi_->val_)) {
    reverse_pass_callback(
        [this_vi = this->vi_, other_vi = other.vi_]() mutable {
          this_vi->adj_ += other_vi->adj_;
        });
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed and added a test.

@stan-buildbot
Copy link
Contributor


Name Old Result New Result Ratio Performance change( 1 - new / old )
gp_pois_regr/gp_pois_regr.stan 4.26 4.23 1.01 0.85% faster
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.02 0.02 0.95 -5.68% slower
eight_schools/eight_schools.stan 0.09 0.09 0.99 -1.51% slower
gp_regr/gp_regr.stan 0.2 0.2 1.01 1.25% faster
irt_2pl/irt_2pl.stan 5.22 5.25 0.99 -0.66% slower
performance.compilation 88.88 87.51 1.02 1.53% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 8.2 8.24 1.0 -0.49% slower
pkpd/one_comp_mm_elim_abs.stan 26.86 28.63 0.94 -6.59% slower
sir/sir.stan 132.04 132.07 1.0 -0.02% slower
gp_regr/gen_gp_data.stan 0.05 0.05 1.0 -0.06% slower
low_dim_gauss_mix/low_dim_gauss_mix.stan 3.33 3.29 1.01 1.27% faster
pkpd/sim_one_comp_mm_elim_abs.stan 0.39 0.38 1.01 1.42% faster
arK/arK.stan 2.54 1.88 1.36 26.24% faster
arma/arma.stan 0.73 0.72 1.01 0.91% faster
garch/garch.stan 0.73 0.73 0.99 -0.52% slower
Mean result: 1.01873740436

Jenkins Console Log
Blue Ocean
Commit hash: e631b04


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
Copy link
Collaborator

Yo fyi I merged this into #2064 because I would need it there anyway. We can either do one branch at a time or do just #2064 your call

@t4c1
Copy link
Contributor Author

t4c1 commented Sep 9, 2020

it would also be good to add more tests, though I can add a bunch of stress tests in #2064

Yeah I tend to be a bit lazy with tests. I think this is enough to show it works. If you want to add more, feel free. Or if you want me to add something specific, feel free to ask.

I also intend to add something similar for scalars. However, I wan to avoid merge conflicts, so I will wait until #2064 is merged. Also it contains some changes I'd need to do anyway.

@stan-buildbot
Copy link
Contributor


Name Old Result New Result Ratio Performance change( 1 - new / old )
gp_pois_regr/gp_pois_regr.stan 4.24 4.15 1.02 2.02% faster
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.02 0.02 1.01 1.43% faster
eight_schools/eight_schools.stan 0.09 0.09 1.01 1.06% faster
gp_regr/gp_regr.stan 0.2 0.2 0.99 -1.4% slower
irt_2pl/irt_2pl.stan 5.27 5.2 1.01 1.31% faster
performance.compilation 90.13 87.73 1.03 2.66% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 8.2 8.36 0.98 -2.05% slower
pkpd/one_comp_mm_elim_abs.stan 26.82 26.93 1.0 -0.4% slower
sir/sir.stan 129.51 138.11 0.94 -6.64% slower
gp_regr/gen_gp_data.stan 0.05 0.05 1.0 -0.33% slower
low_dim_gauss_mix/low_dim_gauss_mix.stan 3.35 3.32 1.01 0.77% faster
pkpd/sim_one_comp_mm_elim_abs.stan 0.38 0.38 0.99 -1.23% slower
arK/arK.stan 2.54 1.83 1.39 28.17% faster
arma/arma.stan 0.73 0.71 1.03 2.58% faster
garch/garch.stan 0.73 0.72 1.0 0.34% faster
Mean result: 1.02670430571

Jenkins Console Log
Blue Ocean
Commit hash: ff9c67a


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-buildbot
Copy link
Contributor


Name Old Result New Result Ratio Performance change( 1 - new / old )
gp_pois_regr/gp_pois_regr.stan 4.15 4.16 1.0 -0.18% slower
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.02 0.02 1.0 0.16% faster
eight_schools/eight_schools.stan 0.09 0.09 0.97 -3.26% slower
gp_regr/gp_regr.stan 0.2 0.2 1.01 0.55% faster
irt_2pl/irt_2pl.stan 5.23 5.19 1.01 0.91% faster
performance.compilation 90.1 87.79 1.03 2.57% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 8.42 8.39 1.0 0.39% faster
pkpd/one_comp_mm_elim_abs.stan 28.17 26.3 1.07 6.64% faster
sir/sir.stan 133.27 138.09 0.97 -3.62% slower
gp_regr/gen_gp_data.stan 0.05 0.05 1.01 0.72% faster
low_dim_gauss_mix/low_dim_gauss_mix.stan 3.33 3.3 1.01 1.01% faster
pkpd/sim_one_comp_mm_elim_abs.stan 0.39 0.4 0.97 -2.8% slower
arK/arK.stan 2.57 1.82 1.41 29.2% faster
arma/arma.stan 0.72 0.72 1.0 -0.14% slower
garch/garch.stan 0.73 0.73 0.99 -0.51% slower
Mean result: 1.0296980799

Jenkins Console Log
Blue Ocean
Commit hash: ff9c67a


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-buildbot
Copy link
Contributor


Name Old Result New Result Ratio Performance change( 1 - new / old )
gp_pois_regr/gp_pois_regr.stan 4.25 4.12 1.03 3.09% faster
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.02 0.02 1.02 1.56% faster
eight_schools/eight_schools.stan 0.1 0.1 1.01 0.76% faster
gp_regr/gp_regr.stan 0.19 0.2 0.99 -1.48% slower
irt_2pl/irt_2pl.stan 5.28 5.23 1.01 0.88% faster
performance.compilation 90.02 87.88 1.02 2.38% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 8.19 8.23 1.0 -0.45% slower
pkpd/one_comp_mm_elim_abs.stan 27.83 27.82 1.0 0.04% faster
sir/sir.stan 132.12 139.09 0.95 -5.27% slower
gp_regr/gen_gp_data.stan 0.05 0.05 0.99 -0.68% slower
low_dim_gauss_mix/low_dim_gauss_mix.stan 3.4 3.33 1.02 1.96% faster
pkpd/sim_one_comp_mm_elim_abs.stan 0.39 0.39 1.0 0.37% faster
arK/arK.stan 2.55 2.55 1.0 -0.22% slower
arma/arma.stan 0.73 0.74 0.99 -1.16% slower
garch/garch.stan 0.73 0.73 1.0 -0.32% slower
Mean result: 1.00134003937

Jenkins Console Log
Blue Ocean
Commit hash: 160e2c3


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

@t4c1
Copy link
Contributor Author

t4c1 commented Sep 17, 2020

@SteveBronder This is waiting for next review.

Copy link
Collaborator

@SteveBronder SteveBronder left a comment

Choose a reason for hiding this comment

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

So I think this looks good. Splitting out var_value can either be done in this PR or #2064

stan/math/rev/core/var.hpp Show resolved Hide resolved
stan/math/rev/core/var.hpp Show resolved Hide resolved
@t4c1 t4c1 merged commit ed6ab4b into stan-dev:develop Sep 18, 2020
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.

4 participants