Where would I put code to vectorize for loops? #877
Replies: 4 comments 5 replies
-
Oh and for data {
int N;
int K;
int my_idx[K]
}
parameters {
vector[N] y;
}
// stuff..
model {
vector[N] x;
for (i in my_idx) {
x[i] = exp(y[i]);
} Which would just become x[my_idx] = exp(y[my_idx]); And if we preserve knowledge of the lower upper this would probably work with the same scheme but only indexing |
Beta Was this translation helpful? Give feedback.
-
I think this should be a part of one of the optimization levels. |
Beta Was this translation helpful? Give feedback.
-
If anyone has a minute to sort something out for me, I wanted to goof around with this and so far think I have everything setup to just do a no-op optimization pass over the statements, but I can't figure out how to plugin in the new optimization step https://github.com/stan-dev/stanc3/compare/master...SteveBronder:feature/loop-reducer?expand=1 When I run make I'm getting an error
Which is from not iterating over the statements in
How do I iterate over those statements? |
Beta Was this translation helpful? Give feedback.
-
@seantalts do functions hold sized types or unsized types? It looks like they hold unsized types which is going to make this for loop thing v hard since then we lose size information and I can't check that the upper of the loop is the same size as the inner types of functions |
Beta Was this translation helpful? Give feedback.
-
I'm trying to parse through
transform_mir.ml
and write something where, for examplewe can take that code and transform it to
if the following conditions are met:
For
type'sloopvar: i
(so no (i - 1
) or anything else.upper
is the same size as the indexed types' sizex
andy
must be of sizeN
f
has a vectorized equivalent which can be called.I'm having a hard time figuring out where this would happen? Would this be something when going from the AST to MIR? Or is this a thing that would happen in
transform_mir
? The only close thing I could find like this is whenever we take aDeclVar
and create both anAssignment
andDecl
.It feels like what I'd like to do is for a list of the statements in
log_prob
log_prob
for anyFor
whoselower
andupper
is a linear integer sequence. Any statements that are not loops just get put into the returned list as isAssignment
type where the lhs and rhs will be the inner indexed type and the vectorized function applied to the rhs.For (3) I think I could do something like I did in #868 where I could just have a
query_body
function taking in a check for the above conditions and returning back an Optional type that forSome
has a tuple of the stuff I need to build the assign.Has anyone thought of this more and would have some pointers? This would be a very nice optimization with the new matrix type since now all these simple loops become candidates for it. And also I like the idea of not forcing users to have to write vectorized functions, loops are nice and make sense! But it would be cool to let folks write loops and then for simple cases we can make them into their vectorized equivalents. This is also nice for things like
brms
where it applies a for loop over the link function for sigma since users can supply their own custom link function. But often times they are doing exactly the loop above or something else that has a vectorized equivalent.For multi statements I think it would get more complicated than I want to tackle right now. Like we would need to scan each statement of the loop satisfying the checks above, then for the ones that pass the check we pull them out as an assignment and for the ones that don't pass we write a new for loop excluding the ops that passed.
Beta Was this translation helpful? Give feedback.
All reactions