Skip to content

Commit c8a0e4b

Browse files
authored
Remove buggy check in jax tensor indexing (#302)
* fix advanced indexing bug * format
1 parent 11de384 commit c8a0e4b

2 files changed

Lines changed: 49 additions & 4 deletions

File tree

effectful/handlers/jax/_handlers.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,6 @@ def update_sizes(sizes, op, size):
7777

7878
def _getitem_sizeof(x: jax.Array, key: tuple[Expr[IndexElement], ...]):
7979
if is_eager_array(x):
80-
if len(key) > x.ndim:
81-
raise IndexError(
82-
f"Indexing with too many dimensions: expected {x.ndim} got {len(key)}"
83-
)
8480
for i, k in enumerate(key):
8581
if isinstance(k, Term) and len(k.args) == 0 and len(k.kwargs) == 0:
8682
update_sizes(sizes, k.op, x.shape[i])

tests/test_handlers_jax.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,52 @@ def test_jax_len():
305305

306306
for row in t_i:
307307
assert len(row) == 4
308+
309+
310+
def test_jax_dimension_addition():
311+
"""Test jax_getitem with dimension addition via None indexing."""
312+
i = defop(jax.Array, name="i")
313+
i2 = defop(i)
314+
315+
# Basic case: indexing with slice and defop
316+
x = jax_getitem(jnp.eye(3), (i(), slice(None)))
317+
assert x.shape == (3,)
318+
assert fvsof(x) >= {i}
319+
320+
# Multiple defops with None - this should work fine
321+
x2 = jax_getitem(jnp.eye(3), (i(), i2(), None))
322+
assert x2.shape == (1,)
323+
assert fvsof(x2) >= {i, i2}
324+
325+
# The problematic case: indexing a Term with defop and None
326+
# This should work but may currently fail
327+
x3 = jax_getitem(x, (i2(), None))
328+
assert x3.shape == (1,)
329+
assert fvsof(x3) >= {i, i2}
330+
331+
# Additional test cases for dimension addition
332+
# Test with multiple None dimensions
333+
x4 = jax_getitem(x, (i2(), None, None))
334+
assert x4.shape == (1, 1)
335+
assert fvsof(x4) >= {i, i2}
336+
337+
# Test with slice and None
338+
x5 = jax_getitem(x, (slice(None), None))
339+
assert x5.shape == (3, 1)
340+
assert fvsof(x5) >= {i}
341+
342+
# Test with Ellipsis and None
343+
x6 = jax_getitem(x, (..., None))
344+
assert x6.shape == (3, 1)
345+
assert fvsof(x6) >= {i}
346+
347+
# Test nested indexing with dimension addition
348+
base = jnp.ones((2, 3, 4))
349+
y = jax_getitem(base, (i(), slice(None), slice(None)))
350+
assert y.shape == (3, 4)
351+
assert fvsof(y) >= {i}
352+
353+
# Index the result with another defop and add dimension
354+
y2 = jax_getitem(y, (i2(), slice(None), None))
355+
assert y2.shape == (4, 1)
356+
assert fvsof(y2) >= {i, i2}

0 commit comments

Comments
 (0)