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

Complex containers #1133

Merged
merged 28 commits into from
Mar 29, 2022
Merged

Complex containers #1133

merged 28 commits into from
Mar 29, 2022

Conversation

WardBrian
Copy link
Member

@WardBrian WardBrian commented Feb 23, 2022

Closes #1116 . This branch has gotten to a really good place. I believe with the exception of stan-dev/math#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 WardBrian added feature New feature or request big-exciting-project Large projects that we're excited about but might take a long time and possibly not end up working o labels Feb 23, 2022
@WardBrian
Copy link
Member Author

@SteveBronder the above commit adds the test we looked at on Friday, if you still have the chance to look at stan-dev/math#2671

@SteveBronder
Copy link
Contributor

@WardBrian just put up the PR! Are you able to make a local cmdstan and try out stan-dev/math#2680?

@WardBrian
Copy link
Member Author

@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 stan-dev/math#2676 get addressed whenever @rok-cesnovar has the chance before this would get merged, just to automatically test all the declared complex signatures.

@SteveBronder
Copy link
Contributor

Will look at this tmrw!

@WardBrian
Copy link
Member Author

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

@SteveBronder
Copy link
Contributor

Sorry its on my plate today

Copy link
Contributor

@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.

Everything looks good to me! Though I think we need to do one thing in mem_pattern to check that any matrix or vector that is assigned to a complex matrix or vector is demoted to AoS. Besides that I'd like to try to cleanup the requires a bit on the functions so we don't have so many as I worry that could hurt compile time a good bit. But I can't do that then this PR looks good

src/frontend/Promotion.ml Outdated Show resolved Hide resolved
src/frontend/Promotion.ml Outdated Show resolved Hide resolved
src/frontend/Typechecker.ml Outdated Show resolved Hide resolved
src/middle/Stan_math_signatures.ml Outdated Show resolved Hide resolved
src/stan_math_backend/Stan_math_code_gen.ml Outdated Show resolved Hide resolved
@SteveBronder
Copy link
Contributor

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

@WardBrian
Copy link
Member Author

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

@SteveBronder
Copy link
Contributor

SteveBronder commented Mar 15, 2022

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
Copy link
Member Author

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

@SteveBronder
Copy link
Contributor

See bb816a5 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
Copy link
Member Author

I believe all the error messages should be good to go

@WardBrian
Copy link
Member Author

Looks like it needs a dune promote

Copy link
Contributor

@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.

lgtm!

@WardBrian
Copy link
Member Author

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

@WardBrian
Copy link
Member Author

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

@WardBrian WardBrian mentioned this pull request Mar 22, 2022
2 tasks
@WardBrian WardBrian merged commit c47e3f2 into stan-dev:master Mar 29, 2022
@WardBrian WardBrian deleted the complex-containers branch May 10, 2022 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
big-exciting-project Large projects that we're excited about but might take a long time and possibly not end up working o feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Complex containers
2 participants