-
Notifications
You must be signed in to change notification settings - Fork 133
Implement vectorize_node
for CheckAndRaise
Op
#553
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
Conversation
b494851
to
bb3fda4
Compare
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #553 +/- ##
=======================================
Coverage 80.90% 80.91%
=======================================
Files 162 162
Lines 46416 46428 +12
Branches 11354 11356 +2
=======================================
+ Hits 37554 37568 +14
+ Misses 6639 6638 -1
+ Partials 2223 2222 -1
|
# Only y is batched | ||
vect_out = vectorize_graph(out, {x: x, y: batch_y}) | ||
assert vect_out.type.shape == (2, None) | ||
assert vect_out.owner.op == second # broadcast |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does vectorizing y make a second, but vectorizing x doesn't? second
is the strange dummy ExpandDims (Dimshuffle?) Op
that always gets rewritten away right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
y
is never an output of CheckAndRaise
, so it doesn't need to be vectorized (and it can't, since the Op demands it be a scalar bool). The condition is either a scalar True or False, and broadcasting (and then reducing again with all
) would be a no-op.
Second is the Elemwise implicit form of Broadcasting, that allows easier rewrites during canonicalization. When needed in the final graph it gets eventually rewritten as an Alloc:
pytensor/pytensor/tensor/rewriting/basic.py
Lines 1 to 23 in 9df55cc
""" Tensor optimizations addressing the ops in basic.py. | |
Notes | |
----- | |
There are two ways of broadcasting arrays: | |
second(x, y) == alloc(y, broadcast_shapes(x.shape, y.shape)) | |
The second can be more efficient because x doesn't usually need to be computed when we only want its shape. | |
It may also allow other rewrites that don't try to modify x when it has multiple clients (for fear of duplicating computation). | |
However, the first one is easier to reason about. | |
Knowing we have such a graph allows to do certain rewrites such as "sinking" broadcasting operations below Elemwise. | |
The same rewrites with alloc would be more complicated as we would need to symbolically combine the shapes of each one. | |
As an example contrast rewriting the following two equivalent graphs | |
alloc(x, broadcast_shapes(x.shape, y.shape)) + alloc(y, broadcast_shapes(x.shape, y.shape)) -> x + y | |
second(y, x) + second(x, y) -> x + y | |
Theano developers (mostly) preferred to use the first form during canonicalization and introduce the second form later, | |
via rewrites like `local_fill_to_alloc`, and using the `alloc_like` helper inside rewrites. | |
Many stabilize and stabilization rewrites refuse to be applied when a variable has multiple clients, so this is important. | |
""" |
Description
Implement vectorize dispatch for Assert. This crops up a lot when vectorizing logp graphs in PyMC.
Related Issue
Checklist
Type of change