Skip to content

Commit

Permalink
fix basic scan bug with attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjj committed Jul 19, 2024
1 parent 984eca2 commit 351168e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion jax/_src/lax/control_flow/loops.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ def _create_jaxpr(init):
if len(out_tree_children) != 2:
msg = "scan body output must be a pair, got {}."
raise TypeError(msg.format(tree_unflatten(out_tree, jaxpr.out_avals)))
carry_avals_out = jaxpr.out_avals[:out_tree_children[0].num_leaves]
_, carry_avals_out, _ = split_list(
jaxpr.out_avals, [len(attrs_tracked), out_tree_children[0].num_leaves])
return (init_flat, carry_avals, carry_avals_out, init_tree, in_flat, jaxpr,
consts, out_tree, out_tree_children, attrs_tracked)

Expand Down
15 changes: 15 additions & 0 deletions tests/attrs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,21 @@ def jitted():

jax.jit(jitted)() # don't crash

def test_scan_carry(self):
class A:
...

a = A()

jax_setattr(a, 'x', jnp.zeros(3))

def body(i, _):
x = jax_getattr(a, 'x')
x = x.at[i].set(x[i] + 1)
jax_setattr(a, 'x', x)
return i + 1, None
_, _ = jax.lax.scan(body, 0, None, length=3) # don't crash


class AttrsJVPTest(jtu.JaxTestCase):

Expand Down

0 comments on commit 351168e

Please sign in to comment.