If the cur_eqn is program_order_p (or any HOP in the future) add an opt_barrier for every cur_inp i.e. cur_inps = [opt_barrier((token, c))[1] for c in cur_inps] where token, prev_outs_new = optimization_barrier((token, prev_outs)). This is strictly better than the approach before this change (described below). - #40336
Merged
Conversation
copybara-service
Bot
force-pushed
the
test_973217206
branch
from
August 31, 2026 22:27
c5725ff to
8684ed6
Compare
…pt_barrier for every `cur_inp` i.e. `cur_inps = [opt_barrier((token, c))[1] for c in cur_inps]` where `token, prev_outs_new = optimization_barrier((token, prev_outs))`. This is strictly better than the approach before this change (described below).
One thing to note here is that `opt_barrier` won't DCE tokens even if they are unused!
**Why do this?**
Currently we add an opt_barrier of form `opt_barrier((prev_outs, all_cur_inps))`. This means that in a `program_order(False)` scope, we block all equations until all the inputs are ready. This is sub-optimal because if there are equations depending on different inputs, they can execute when their inputs are ready.
`program_order`'s semantics are that every equation will begin after it's previous equation has finished. This change does not violate that. In fact, it makes the scheduling better.
For example:
```
@program_order(True)
def f(x, y):
x = jnp.sin(x)
@program_order(False)
def g(x, y):
x = jax.lax.all_gather(x, ...)
y = jnp.dot(y, y)
return x, y
return g(x, y)
```
Before this change, `all_gather` won't execute until both `x` and `y` are ready even though `all_gather` only needs `x` to be ready. Same for the dot op.
Now, since we do a per-input opt_barrier, program_order's semantics are respected i.e. `g` (`all_gather` and `dot`) won't execute until `jnp.sin` is done but `all_gather` and `dot` are free to be scheduled independently once their inputs are ready (since they are in a program_order(False) scope).
PiperOrigin-RevId: 974123014
copybara-service
Bot
force-pushed
the
test_973217206
branch
from
August 31, 2026 22:53
8684ed6 to
cdbbfe1
Compare
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.
If the cur_eqn is program_order_p (or any HOP in the future) add an opt_barrier for every
cur_inpi.e.cur_inps = [opt_barrier((token, c))[1] for c in cur_inps]wheretoken, prev_outs_new = optimization_barrier((token, prev_outs)). This is strictly better than the approach before this change (described below).One thing to note here is that
opt_barrierwon't DCE tokens even if they are unused!Why do this?
Currently we add an opt_barrier of form
opt_barrier((prev_outs, all_cur_inps)). This means that in aprogram_order(False)scope, we block all equations until all the inputs are ready. This is sub-optimal because if there are equations depending on different inputs, they can execute when their inputs are ready.program_order's semantics are that every equation will begin after it's previous equation has finished. This change does not violate that. In fact, it makes the scheduling better.For example:
Before this change,
all_gatherwon't execute until bothxandyare ready even thoughall_gatheronly needsxto be ready. Same for the dot op.Now, since we do a per-input opt_barrier, program_order's semantics are respected i.e.
g(all_gatheranddot) won't execute untiljnp.sinis done butall_gatheranddotare free to be scheduled independently once their inputs are ready (since they are in a program_order(False) scope).