adds optimizations so that (real, data {vector/matrix}) expressions can use SoA - #1709
Open
SteveBronder wants to merge 3 commits into
Open
SteveBronder wants to merge 3 commits into
SteveBronder wants to merge 3 commits into
Conversation
…ations promote the real to a vector such that SoA can be used
- UnsizedType.is_autodiffable_eigen: autodiffable type containing an Eigen matrix - Stan_math_signatures.normalize_fn_name: operator and _lupdf/_lupmf name normalization - Expr.Helpers.rep_like: build rep_vector/rep_row_vector/rep_matrix shaped like another expression Memory_patterns keeps only the SoA-specific whitelist and promotion pass.
WardBrian
reviewed
Sep 12, 2026
Member
There was a problem hiding this comment.
did you mean for this folder to get committed?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note this also moves the for loop compaction from
--Oexperimentalto--O1For cases like
vector fma(var, data vector, var)the compiler cannot use SoA because none of the inputs are var matrix types. So the internals offmajust seevar, data vector, varand then useEigen::Matrix<var>. This pull request adds an additional forward and reverse pass in the monotone framework used by theSoAoptimization routine to detect functions where a scalar could be broadcast to avar_value<Matrix>such that the return is the same value, but with a return type that is avar_value<Matrix>instead of aMatrix<var>. So we can take code like the below which is just var scalars and data vectors and callrep_vectoron the first argument to get back avar<Matrix>typebecomes:
Funnily enough, I have actually caught claude doing this when I ask it to write the program to make it easier to use SoA.
The functions we can do this with are a small but regularly used:
fma,beta,lmultiply,add,subtract,elt_multiply,elt_divide, andmultiply.These are the target functions since all of these functions are elementwise. So whether we do
fma(real, data vector, real)orfma(rep_vector(real), data vector, real)we get the same result.The optimization pass does a speculative rewrite where we look for expressions with above functions and data / scalar patterns when we think we can wrap one var scalar into a
rep_vectorand get the promotion trick to work. We then do a forward and backward pass to check if promoting the result tovar<Matrix>would work. If it will work then we keep the promotion and move on, else we skip promoting that expression.Performance
Here is the gradient of
sum(fma(a, x, b))andsum(fma(rep_vector(a, N), x, b))for autodiff scalarsaandband adata vector
xof length N.The AoS row is the code we emitted before this branch and the SoA row is what we emit now.
varisvarisSubmission Checklist
Release notes
Adds optimization to SoA path for allowing certain expressions with
(real, data vector)patterns to use SoACopyright 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)