Reverse mode specializaiton for matrix add and subtract - #1812
Conversation
|
Thanks for writing tests, but they're probably largely redundant given the testing framework tests which cover all levels of autodiff against values and finite differences of the The one thing that should be done is to add throw tests for mismatched sizes in |
|
I'll take the review for this one (if nobody else had started) |
andrjohns
left a comment
There was a problem hiding this comment.
Looks great! Some suggestions included. The main issue is the use of if constexpr which is above the C++ standard that Stan currently requires, so that will need to be re-written/replaced
| @@ -0,0 +1,538 @@ | |||
| #ifndef STAN_MATH_REV_FUN_MATRIX_ADD_HPP | |||
| #define STAN_MATH_REV_FUN_MATRIX_ADD_HPP | |||
There was a problem hiding this comment.
Shouldn't this be *_FUN_ADD_HPP (i.e. no 'MATRIX') for consistency with prim?
| #include <stan/math/rev/core.hpp> | ||
| #include <stan/math/prim.hpp> | ||
| #include <type_traits> | ||
| #include <iostream> |
There was a problem hiding this comment.
These last two headers should be removed
| * vari's constructor. | ||
| * | ||
| * @param A matrix | ||
| * @param B matrix |
There was a problem hiding this comment.
| * @param B matrix | |
| * @param B matrix |
| * vari's constructor. | ||
| * | ||
| * @param A matrix | ||
| * @param B matrix |
There was a problem hiding this comment.
| * @param B matrix | |
| * @param B matrix |
| * The class stores the structure of each matrix, | ||
| * the double values of A and B, and pointers to | ||
| * the varis for A and B if A or B is a var. It | ||
| * also instatiates and stores pointers to varis |
There was a problem hiding this comment.
| * also instatiates and stores pointers to varis | |
| * also instantiates and stores pointers to varis |
| matrix_d adjAminusB(rows_, cols_); | ||
| adjAminusB = Map<matrix_vi>(variRefAminusB_, rows_, cols_).adj(); | ||
| Map<matrix_vi>(variRefB_, rows_, cols_).adj() -= adjAminusB; |
There was a problem hiding this comment.
| matrix_d adjAminusB(rows_, cols_); | |
| adjAminusB = Map<matrix_vi>(variRefAminusB_, rows_, cols_).adj(); | |
| Map<matrix_vi>(variRefB_, rows_, cols_).adj() -= adjAminusB; | |
| Map<matrix_vi> AminusB(variRefAminusB_, rows_, cols_); | |
| Map<matrix_vi>(variRefB_, rows_, cols_).adj() -= AminusB.adj(); |
| Map<matrix_d> Ad(Ad_, rows_, cols_); | ||
| double cd = c.val(); | ||
| Ad = A.val(); | ||
| if constexpr (MatMinusScal) { |
There was a problem hiding this comment.
if constexpr is c++17, and we're only up to c++14
| * @tparam Scal type of the scalar | ||
| * @tparam Mat type of the matrix or expression | ||
| * @param c Scalar. | ||
| * @param m Matrix. |
There was a problem hiding this comment.
| * @param m Matrix. | |
| * @param A Matrix. |
| double cd = c.val(); | ||
| Map<matrix_d> Ad(Ad_, rows_, cols_); | ||
| Ad = A.val(); | ||
| Map<matrix_vi>(variRefAplusc_, rows_, cols_) | ||
| = (Ad.array() + cd).matrix().unaryExpr([](double x) { | ||
| return new vari(x, false); | ||
| }); |
There was a problem hiding this comment.
Same suggested changes as the above
|
@dpsimpson How did this compare in the end to the un-autodiffed version? |
|
Sorry - got caught up in a work thing. Will do stuff tonight and tomorrow.
…On Mon, 6 Apr 2020 at 16:49, Ben Bales ***@***.***> wrote:
@dpsimpson <https://github.com/dpsimpson> How did this compare in the end
to the un-autodiffed version?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1812 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADRICBTJ7UISYXNXMBBTQB3RLI533ANCNFSM4LXUAOVA>
.
|
|
This will be in 2.24? Or can this move in today? |
|
I think this would be a new feature so it will be in the next release |
|
Of course...this is new stuff. The pr comments sounded as if this is about ready. I expect this pr to speed up things quite a bit which is why I was bumping this for some attention. But let’s wait for the next release. |
|
Definitely next release - swamped with exam stuff. Will pick it up ASAP.
Sorry.
…On Mon, Apr 13, 2020 at 15:11 wds15 ***@***.***> wrote:
Of course...this is new stuff. The pr comments sounded as if this is about
ready. I expect this pr to speed up things quite a bit which is why I was
bumping this for some attention.
But let’s wait for the next release.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1812 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADRICBSKT4CP34L3T54EHSTRMNPWZANCNFSM4LXUAOVA>
.
|
|
Closing this for now but feel free to open when you have time to fixup! |
|
@dpsimpson any chance this can get resurrected? |
|
I thought it was superseded. But absolutely! I’ll have a look over the
weekend.
…On Fri, Feb 3, 2023 at 14:18 Sean Pinkney ***@***.***> wrote:
@dpsimpson <https://github.com/dpsimpson> any chance this can get
resurrected?
—
Reply to this email directly, view it on GitHub
<#1812 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADRICBUVWKTVHXIUUHYZBJLWVR2G5ANCNFSM4LXUAOVA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Summary
This is addresses part of #1787 by adding reverse mode specializations for the matrix
addandsubtractfunctions. This should reduce the size of the autodiff stack.The implementation uses a lot of tricks copped from the specialization of
multiply.subtractuses a few constexpr ifs to avoid re-writing code. I'm getting a C++17 warning, and I can change them to ordinary ifs if it's easier. They're there so I didn't have to implement different vari extensions forA - candc - A(whereAis a matrix andcis a scalar). I had honestly hoped I wouldn't need to write almost equivalent code forsubtract(Scalar,Matrix)andsubtract(Matrix, Scalar)(lines 549-600 ofrev/fun/subtract.hpp) but I couldn't work out how to do that.As always, any suggestions/substitutions/changes greatly appreciated.
Tests
The following tests are implemented for both
addandsubtract:I'm happy to add more, but I couldn't think of any.
Side Effects
This should be a plug in replacement.
Release notes
Replace this text with a short note on what will change if this pull request is merged in which case this will be included in the release notes.
Checklist
Math issue autodiff for matrix add and subtract #1787 (Partial)
Copyright holder: Daniel simpson
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
./runTests.py test/unit)make test-headers)make test-math-dependencies)make doxygen)make cpplint)the code is written in idiomatic C++ and changes are documented in the doxygen
the new changes are tested