stan-dev / stanc3

The Stan transpiler (from Stan to C++ and beyond).
BSD 3-Clause "New" or "Revised" License
142 stars 46 forks source link

Complex containers #1133

Closed WardBrian closed 2 years ago

WardBrian commented 2 years ago

Closes #1116 . This branch has gotten to a really good place. I believe with the exception of https://github.com/stan-dev/math/issues/2671 that the basic tests I've written here should compile and pass.

Thanks to previous PRs like #1102, #1115, #1106, most of these changes are pretty "boring", mostly just doing the obvious things based on the existing container types and the promotion semantics.

Things this PR adds:

  1. complex_matrix, complex_vector, complex_row_vector types. The behavior and code generation of these largely mirrors the existing Eigen types.
  2. Promotion from matrix, etc. to complex_matrix as necessary (e.g. in assignment, in function calls)
  3. Basic math signatures (add, subtract, multiply, minus, elt_multiply, elt_divide) and basic container functions (e.g rows and the like)

Things this PR does not add:

  1. Many, many other function signatures which would be valid or interesting for complex types. There are things like pow, which might work already, and things like FFTs, which will need math support. I leave these all for later PRs.
  2. Add "mixed" signatures for things like multiply: (complex_vector, row_vector)=> complex_matrix. NB: this does currently work, but it does so by promoting the row_vector to complex_row_vector. We can later change this for speedups.

Submission Checklist

Release notes

Added complex containers: complex_matrix, complex_vector, and complex_row_vector with basic arithmetic support. More special functions and specializations will follow. Note that real-valued containers will be promoted to their complex variants as needed

Copyright and Licensing

By submitting this pull request, the copyright holder is agreeing to license the submitted work under the BSD 3-clause license (https://opensource.org/licenses/BSD-3-Clause)

WardBrian commented 2 years ago

@SteveBronder the above commit adds the test we looked at on Friday, if you still have the chance to look at https://github.com/stan-dev/math/issues/2671

SteveBronder commented 2 years ago

@WardBrian just put up the PR! Are you able to make a local cmdstan and try out https://github.com/stan-dev/math/pull/2680?

WardBrian commented 2 years ago

@SteveBronder I ran this branch with the pr here: https://jenkins.flatironinstitute.org/blue/organizations/jenkins/Stan%2FStanc3/detail/PR-1133/7/pipeline/ and everything is green!

I would still like to see https://github.com/stan-dev/math/issues/2676 get addressed whenever @rok-cesnovar has the chance before this would get merged, just to automatically test all the declared complex signatures.

SteveBronder commented 2 years ago

Will look at this tmrw!

WardBrian commented 2 years ago

@SteveBronder did you get a chance to look at this?

SteveBronder commented 2 years ago

Sorry its on my plate today

SteveBronder commented 2 years ago

This all looks good! I think it just needs the thing in mem_pattern then it's good to merge

WardBrian commented 2 years ago

I'm not sure I understand what needs to change in mem_pattern to get the necessary behavior

SteveBronder commented 2 years ago

I'll take a look today. It's just that on the first pass we want to make sure that if a matrix/vector is being assigned to a complex matrix/vector then it is tagged as not possible to be an SoA matrix. So in query_initial_demotable_stmt we want to look for assignments whose meta type is ComplexMatrix/Vector and if the rhs expression is an Autodiffable Matrix/Vector then we want to add any named matrix/vector objects in the rhs expression to the list of matrices that cannot be promoted to SoA matrices. So we want to make sure the below on O1 will not have A_p be an SoA matrix.

parameters {
 matrix[10, 10] A_p;
}
transformed parameters {
 complex_matrix[10, 10] A_complex_tp = A_p;
}

https://github.com/stan-dev/stanc3/blob/master/src/analysis_and_optimization/Mem_pattern.ml#L357

WardBrian commented 2 years ago

I'd appreciate you taking a look, that part of the code is tough for me

SteveBronder commented 2 years ago

See https://github.com/stan-dev/stanc3/pull/1133/commits/bb816a57934e74f8bd54b64e2ce8e157ce2200f9 I think that's all it needs so that it knows that is an object that cannot be promoted. Do you still need to add any new error messages specifically for complex types?

WardBrian commented 2 years ago

I believe all the error messages should be good to go

WardBrian commented 2 years ago

Looks like it needs a dune promote

WardBrian commented 2 years ago

Thanks @SteveBronder - I'll hit merge on this as soon as 2.29.2 is out and good to go

WardBrian commented 2 years ago

Well, also after @bob-carpenter and I update the relevant docs