Skip to content
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

jax.vmap: convert mapped input arguments to array #25835

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions jax/_src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,10 @@ def vmap_f(*args, **kwargs):
f = lu.wrap_init(fun)
flat_fun, out_tree = batching.flatten_fun_for_vmap(f, in_tree)
in_axes_flat = flatten_axes("vmap in_axes", in_tree, (in_axes, 0), kws=True)
args_flat = [lax_internal.asarray(arg)
if isinstance(arg, np.ndarray) and ax is not None
else arg
for arg, ax in zip(args_flat, in_axes_flat)]
axis_size_ = (axis_size if axis_size is not None else
_mapped_axis_size(fun, in_tree, args_flat, in_axes_flat, "vmap"))
try:
Expand Down
17 changes: 15 additions & 2 deletions tests/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1812,8 +1812,21 @@ def f(x):
self.assertIsInstance(x, jax.Array)
self.assertNotIsInstance(x, np.ndarray)
return x + 2
jit(f)(3)
jax.vmap(f)(np.arange(3))
result = jit(f)(3)
self.assertIsInstance(result, jax.Array)

result = jax.vmap(f)(np.arange(3))
self.assertIsInstance(result, jax.Array)

def test_numpy_input_with_trivial_function(self):
# Regression test for https://github.com/jax-ml/jax/issues/25745
def f(x):
return x
jit_result = jit(f)(3)
self.assertIsInstance(jit_result, jax.Array)

vmap_result = jax.vmap(f)(np.arange(3))
self.assertIsInstance(vmap_result, jax.Array)

def test_device_put_and_get(self):
x = np.arange(12.).reshape((3, 4)).astype("float32")
Expand Down
Loading