Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions tensor-shapes/jax-pyrefly-stubs/jax-stubs/_shapes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1896,6 +1896,16 @@ def take_shape(
)
return dsl.Invalid("axis must be an integer or None")

@type_shape_dsl_function
def reduceat_shape(
a_shape: IntTuple,
idx_shape: IntTuple,
axis: int,
) -> IntTuple:
if len(idx_shape) != 1:
return dsl.Invalid("reduceat requires 1-D indices")
return take_shape(a_shape, idx_shape, axis)

@type_shape_dsl_function
def take_along_axis_shape(
arr_shape: IntTuple,
Expand Down Expand Up @@ -2371,3 +2381,7 @@ def glu_shape(shape: IntTuple, axis: int) -> IntTuple:
return dsl.IntTuple(
(halved if index == norm_axis else shape[index] for index in range(rank))
)

@type_shape_dsl_function
def outer_shape(left: IntTuple, right: IntTuple) -> IntTuple:
return dsl.concat(left, right)
168 changes: 127 additions & 41 deletions tensor-shapes/jax-pyrefly-stubs/jax-stubs/numpy/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ from jax._shapes import (
matmul_shape,
matvec_shape,
moveaxis_shape,
outer_shape,
packbits_shape,
pad_pairs_shape,
pad_scalar_shape,
Expand All @@ -68,6 +69,7 @@ from jax._shapes import (
polyint_shape,
ravel_shape,
reduce_shape,
reduceat_shape,
repeat_shape,
reshape_scalar_shape,
reshape_shape,
Expand Down Expand Up @@ -909,30 +911,29 @@ def unwrap[Shape: _Shape = []](

# Broadcasting elementwise binary functions. Each takes a scalar in either
# position as well as an array: rejecting `jnp.add(a, 1)` would flag valid code.
def add[Shape1: _Shape = [], Shape2: _Shape = []](
x1: _ArrayLike[Shape1], x2: _ArrayLike[Shape2], /
) -> _Array[broadcast(Shape1, Shape2)]: ...
add: BinaryUfunc

def arctan2[Shape1: _Shape = [], Shape2: _Shape = []](
x1: _ArrayLike[Shape1], x2: _ArrayLike[Shape2], /
) -> _Array[broadcast(Shape1, Shape2)]: ...
def atan2[Shape1: _Shape = [], Shape2: _Shape = []](
x1: _ArrayLike[Shape1], x2: _ArrayLike[Shape2], /
) -> _Array[broadcast(Shape1, Shape2)]: ...
def bitwise_and[Shape1: _Shape = [], Shape2: _Shape = []](
x1: _ArrayLike[Shape1], x2: _ArrayLike[Shape2], /
) -> _Array[broadcast(Shape1, Shape2)]: ...

bitwise_and: BinaryUfunc

def bitwise_left_shift[Shape1: _Shape = [], Shape2: _Shape = []](
x1: _ArrayLike[Shape1], x2: _ArrayLike[Shape2], /
) -> _Array[broadcast(Shape1, Shape2)]: ...
def bitwise_or[Shape1: _Shape = [], Shape2: _Shape = []](
x1: _ArrayLike[Shape1], x2: _ArrayLike[Shape2], /
) -> _Array[broadcast(Shape1, Shape2)]: ...

bitwise_or: BinaryUfunc

def bitwise_right_shift[Shape1: _Shape = [], Shape2: _Shape = []](
x1: _ArrayLike[Shape1], x2: _ArrayLike[Shape2], /
) -> _Array[broadcast(Shape1, Shape2)]: ...
def bitwise_xor[Shape1: _Shape = [], Shape2: _Shape = []](
x1: _ArrayLike[Shape1], x2: _ArrayLike[Shape2], /
) -> _Array[broadcast(Shape1, Shape2)]: ...

bitwise_xor: BinaryUfunc

def copysign[Shape1: _Shape = [], Shape2: _Shape = []](
x1: _ArrayLike[Shape1], x2: _ArrayLike[Shape2], /
) -> _Array[broadcast(Shape1, Shape2)]: ...
Expand Down Expand Up @@ -969,12 +970,10 @@ def ldexp[Shape1: _Shape = [], Shape2: _Shape = []](
def left_shift[Shape1: _Shape = [], Shape2: _Shape = []](
x1: _ArrayLike[Shape1], x2: _ArrayLike[Shape2], /
) -> _Array[broadcast(Shape1, Shape2)]: ...
def logaddexp[Shape1: _Shape = [], Shape2: _Shape = []](
x1: _ArrayLike[Shape1], x2: _ArrayLike[Shape2], /
) -> _Array[broadcast(Shape1, Shape2)]: ...
def logaddexp2[Shape1: _Shape = [], Shape2: _Shape = []](
x1: _ArrayLike[Shape1], x2: _ArrayLike[Shape2], /
) -> _Array[broadcast(Shape1, Shape2)]: ...

logaddexp: BinaryUfunc
logaddexp2: BinaryUfunc

@overload
def cross[
Axis: Flag[int],
Expand Down Expand Up @@ -1186,18 +1185,16 @@ def vecmat[LeftShape: _Shape, RightShape: _Shape](
x2: _ShapedArrayLike[RightShape],
/,
) -> _Array[vecmat_shape(LeftShape, RightShape)]: ...
def maximum[Shape1: _Shape = [], Shape2: _Shape = []](
x1: _ArrayLike[Shape1], x2: _ArrayLike[Shape2], /
) -> _Array[broadcast(Shape1, Shape2)]: ...
def minimum[Shape1: _Shape = [], Shape2: _Shape = []](
x1: _ArrayLike[Shape1], x2: _ArrayLike[Shape2], /
) -> _Array[broadcast(Shape1, Shape2)]: ...

maximum: BinaryUfunc
minimum: BinaryUfunc

def mod[Shape1: _Shape = [], Shape2: _Shape = []](
x1: _ArrayLike[Shape1], x2: _ArrayLike[Shape2], /
) -> _Array[broadcast(Shape1, Shape2)]: ...
def multiply[Shape1: _Shape = [], Shape2: _Shape = []](
x1: _ArrayLike[Shape1], x2: _ArrayLike[Shape2], /
) -> _Array[broadcast(Shape1, Shape2)]: ...

multiply: BinaryUfunc

def nextafter[Shape1: _Shape = [], Shape2: _Shape = []](
x1: _ArrayLike[Shape1], x2: _ArrayLike[Shape2], /
) -> _Array[broadcast(Shape1, Shape2)]: ...
Expand All @@ -1213,9 +1210,9 @@ def remainder[Shape1: _Shape = [], Shape2: _Shape = []](
def right_shift[Shape1: _Shape = [], Shape2: _Shape = []](
x1: _ArrayLike[Shape1], x2: _ArrayLike[Shape2], /
) -> _Array[broadcast(Shape1, Shape2)]: ...
def subtract[Shape1: _Shape = [], Shape2: _Shape = []](
x1: _ArrayLike[Shape1], x2: _ArrayLike[Shape2], /
) -> _Array[broadcast(Shape1, Shape2)]: ...

subtract: BinaryUfunc

def true_divide[Shape1: _Shape = [], Shape2: _Shape = []](
x1: _ArrayLike[Shape1], x2: _ArrayLike[Shape2], /
) -> _Array[broadcast(Shape1, Shape2)]: ...
Expand Down Expand Up @@ -2544,16 +2541,14 @@ def less[Shape1: _Shape = [], Shape2: _Shape = []](
def less_equal[Shape1: _Shape = [], Shape2: _Shape = []](
x1: _ArrayLike[Shape1], x2: _ArrayLike[Shape2], /
) -> _Array[broadcast(Shape1, Shape2)]: ...
def logical_and[Shape1: _Shape = [], Shape2: _Shape = []](
x1: _ArrayLike[Shape1], x2: _ArrayLike[Shape2], /
) -> _Array[broadcast(Shape1, Shape2)]: ...

logical_and: BinaryUfunc

def logical_not[Shape: _Shape = []](x: _ArrayLike[Shape], /) -> _Array[Shape]: ...
def logical_or[Shape1: _Shape = [], Shape2: _Shape = []](
x1: _ArrayLike[Shape1], x2: _ArrayLike[Shape2], /
) -> _Array[broadcast(Shape1, Shape2)]: ...
def logical_xor[Shape1: _Shape = [], Shape2: _Shape = []](
x1: _ArrayLike[Shape1], x2: _ArrayLike[Shape2], /
) -> _Array[broadcast(Shape1, Shape2)]: ...

logical_or: BinaryUfunc
logical_xor: BinaryUfunc

def not_equal[Shape1: _Shape = [], Shape2: _Shape = []](
x1: _ArrayLike[Shape1], x2: _ArrayLike[Shape2], /
) -> _Array[broadcast(Shape1, Shape2)]: ...
Expand Down Expand Up @@ -3320,10 +3315,12 @@ class ufunc:
def reduce(
self,
a: Any,
axis: int = 0,
axis: int | None = 0,
dtype: Any = None,
out: Any = None,
keepdims: bool = False,
initial: Any = None,
where: Any = None,
) -> Any: ...
def accumulate(
self,
Expand All @@ -3332,16 +3329,105 @@ class ufunc:
dtype: Any = None,
out: Any = None,
) -> Any: ...
def at(
self,
a: Any,
indices: Any,
b: Any = None,
/,
*,
inplace: bool = True,
) -> Any: ...
def reduceat(
self,
a: Any,
indices: Sequence[int],
indices: Any,
axis: int = 0,
dtype: Any = None,
out: Any = None,
) -> Any: ...
def outer(self, A: Any, B: Any, /, **kwargs: Any) -> Any: ...

class BinaryUfunc(Protocol):
@property
def nin(self) -> int: ...
@property
def nout(self) -> int: ...
@property
def nargs(self) -> int: ...
@property
def identity(self) -> bool | int | float: ...
def __call__[Shape1: _Shape = [], Shape2: _Shape = []](
self, x1: _ArrayLike[Shape1], x2: _ArrayLike[Shape2], /
) -> _Array[broadcast(Shape1, Shape2)]: ...
@overload
def reduce[
Axis: Flag[_Axis],
KeepDims: Flag[bool],
Shape: _Shape = [],
](
self,
a: _ArrayLike[Shape],
axis: Axis = 0,
dtype: DTypeLike | None = None,
out: None = None,
keepdims: KeepDims = False,
initial: _ArrayLike[Any] | None = None,
where: _ArrayLike[Any] | None = None,
) -> _Array[reduce_shape(Shape, Axis, KeepDims)]: ...
@overload
def reduce(
self,
a: _ArrayLike[Any],
axis: Sequence[int] | int | None = 0,
dtype: DTypeLike | None = None,
out: None = None,
keepdims: bool = False,
initial: _ArrayLike[Any] | None = None,
where: _ArrayLike[Any] | None = None,
) -> _Array[IntTuple]: ...
def accumulate[Shape: _Shape = []](
self,
a: _ArrayLike[Shape],
axis: int = 0,
dtype: DTypeLike | None = None,
out: None = None,
) -> _Array[Shape]: ...
def at[Shape: _Shape = []](
self,
a: _ArrayLike[Shape],
indices: Any,
b: _ArrayLike[Any] | None = None,
/,
*,
inplace: bool = True,
) -> _Array[Shape]: ...
@overload
def reduceat[
Shape: _Shape = [],
IdxShape: _Shape = [],
Axis: Flag[int] = 0,
](
self,
a: _ArrayLike[Shape],
indices: _ArrayLike[IdxShape],
axis: Axis = 0,
dtype: DTypeLike | None = None,
out: None = None,
) -> _Array[reduceat_shape(Shape, IdxShape, Axis)]: ...
@overload
def reduceat(
self,
a: _ArrayLike[Any],
indices: _ArrayLike[Any],
axis: int = 0,
dtype: DTypeLike | None = None,
out: None = None,
) -> _Array[IntTuple]: ...
def outer[Shape1: _Shape = [], Shape2: _Shape = []](
self, a: _ArrayLike[Shape1], b: _ArrayLike[Shape2], /
) -> _Array[outer_shape(Shape1, Shape2)]: ...

class ComplexWarning(UserWarning): ...

def astype[Shape: _Shape = []](
Expand Down
25 changes: 25 additions & 0 deletions tensor-shapes/jax-pyrefly-stubs/test/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,28 @@ def test_arraylike_operands() -> None:
assert_shape(jnp.add(np.ones((2, 1)), jnp.ones((1, 3))).shape, (2, 3))
assert_shape(jnp.maximum(np.ones((2, 3)), 0.0).shape, (2, 3))
assert_shape(jnp.equal(np.ones((2, 3)), 1).shape, (2, 3))


def test_binary_ufunc_methods() -> None:
x = jnp.ones((3, 4))
y = jnp.ones((2, 5))
b = jnp.ones((3, 4), dtype=jnp.int32)

assert_shape(jnp.maximum.accumulate(x, axis=-1).shape, (3, 4))
assert_shape(jnp.minimum.accumulate(x, axis=0).shape, (3, 4))
assert_shape(jnp.add.accumulate(x, axis=1).shape, (3, 4))
assert_shape(jnp.bitwise_or.accumulate(b, axis=0).shape, (3, 4))

assert_shape(jnp.add.reduce(x).shape, (4,))
assert_shape(jnp.maximum.reduce(x, axis=-1).shape, (3,))
assert_shape(jnp.multiply.reduce(x, axis=0, keepdims=True).shape, (1, 4))
assert_shape(jnp.bitwise_xor.reduce(b, axis=1).shape, (3,))
assert_shape(jnp.logical_and.reduce(x > 0, axis=0).shape, (4,))
assert_shape(jnp.logical_or.reduce(x > 0, axis=None).shape, ())
assert_shape(jnp.logaddexp.reduce(x, axis=-1).shape, (3,))

assert_shape(jnp.add.outer(x, y).shape, (3, 4, 2, 5))
assert_shape(jnp.multiply.outer(jnp.ones(3), jnp.ones(4)).shape, (3, 4))

assert_shape(jnp.add.at(x, 0, jnp.ones(4), inplace=False).shape, (3, 4))
assert_shape(jnp.add.reduceat(x, jnp.array([0, 2]), axis=0).shape, (2, 4))
Loading