-
-
Notifications
You must be signed in to change notification settings - Fork 190
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
How to add static matrix? #1805
Comments
I think the first suggestion could be simplified a bit by writing a specialization for The second suggestion ( |
Why not just something like the following, which would work for matrices, vectors, and row vectors?
For the dynamic case, both members need to be |
I'm not sure what you have in mind here can you elaborate?
Maybe I need to think about it
Yeah mine was just pseudocode
I thought for static matrices we wanted to not allow assignment after the first declaration? In my mind this would never be used by users, but the compiler would do an optimization sweep and if a matrix was never assigned after first initialization it would change it from a normal matrix to a static matrix |
That's for the dynamic case, not the static case. We do. I'm in favor of enabling users to write efficient code rather than hoping the compiler can sort it out. But this might be a case where we can guarantee the compiler finds all the relevant optimizations. Also, I was wrong about need two |
Was going to make a separate issue, but in a side email Bob brought up a point that if we templated var and vari ala template <typename T>
struct var {
vari<T>* vi_;
}
template <typename T>
struct vari {
T val_;
T adj_;
} This would work nicely for static matrices via The example below is a little more far out / groovy, but we can also pass through our vari classes as a template. I think that would let us do some pretty fun things |
Hmm, var<matrix_cl<double>> lol, that seems pretty cool to me! |
I guess there will be some rough edgess to smooth. But this doees look like the simplest way forward. |
@t4c1 do you think our first step here is just to make the var and vari templates and generically just do |
Also I'm going to bring this up in the stan meeting tmrw, maybe we could also host an impromptu stan math meeting about it |
I'm not sure what you mean. What would the I don't think we want any backward-compatibility breaking moves from this. |
Ack markdown got rid of my brackets, just added them |
tbh I'm not really sure how we do this without breaking backwards compatibility in some form. We either have to move up to C++17 for the template constructor deduction or have at least |
Then I think we should wait for C++17. |
I'm mixed about it, we can discuss it in the meeting tmrw. This is a really nice solution to have pretty much everything use the OpenCL backend as well |
We could maintain backwards compatibility by doing this:
|
! Excellent! |
Took me about 5 minutes to throw up a basic implementation using the aliases! https://github.com/stan-dev/math/compare/feature/var-template So that's cool, we can use the aliases as a shim till we generalize stuff to take in generic This feels like a thing that should have an RFC associated with it to talk about how the static matrix stuff will work. Looking over our ops I think this solution satisfies a lot of what we want! |
Yes, a design doc would be good. But your port didn't use that shim everywhere and I didn't understand where the The |
Lol is this supposed to be legal code? fvar<Eigen::Matrix<var, -1, 1>> A(Eigen::Matrix<var, -1, 1>::Zero(10));
fvar<Eigen::Matrix<var, -1, 1>> B(Eigen::Matrix<var, -1, 1>::Zero(10));
auto xx = multiply(A, B); Because that compiles |
We only want to construct fvar out of fvar or double, I think. |
Description
@bob-carpenter has a nice discourse thread discussing how a
static_matrix
type would work in the stan language. This idea has come up again now for efficient autodiff with Eigen #1785 and I think is the path forward to use the OpenCL backend for autodiff #1639My main question is how we would go about implementing this. Should we make a stan
Matrix
type that for doubles just inherits from Eigen and for var is two Eigen matrices? Something likeOne other fun idea with this is to add an
ExecutionPolicy
template argument sort of like in std parallel. This would be an enum with values likesequential
,parallel_opencl
,parallel_tbb
etc. and could allow for pretty simple swaps of execution backends.I'm open to any suggestions!
Current Version:
v3.1.0
The text was updated successfully, but these errors were encountered: