Skip to content

Fix JAX dispatch for multi-output Composite #123

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

Merged
merged 1 commit into from
Dec 14, 2022
Merged
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
13 changes: 10 additions & 3 deletions pytensor/link/jax/dispatch/scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,18 @@ def clip(x, min, max):


@jax_funcify.register(Composite)
def jax_funcify_Composite(op, vectorize=True, **kwargs):
def jax_funcify_Composite(op, node, vectorize=True, **kwargs):
jax_impl = jax_funcify(op.fgraph)

def composite(*args):
return jax_impl(*args)[0]
if len(node.outputs) == 1:

def composite(*args):
return jax_impl(*args)[0]

else:

def composite(*args):
return jax_impl(*args)

return jnp.vectorize(composite)

Expand Down
12 changes: 11 additions & 1 deletion tests/link/jax/test_scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_identity():
),
],
)
def test_jax_Composite(x, y, x_val, y_val):
def test_jax_Composite_singe_output(x, y, x_val, y_val):
x_s = aes.float64("x")
y_s = aes.float64("y")

Expand All @@ -80,6 +80,16 @@ def test_jax_Composite(x, y, x_val, y_val):
_ = compare_jax_and_py(out_fg, test_input_vals)


def test_jax_Composite_multi_output():
x = vector("x")

x_s = aes.float64("xs")
outs = Elemwise(Composite(inputs=[x_s], outputs=[x_s + 1, x_s - 1]))(x)

fgraph = FunctionGraph([x], outs)
compare_jax_and_py(fgraph, [np.arange(10, dtype=config.floatX)])


def test_erf():
x = scalar("x")
out = erf(x)
Expand Down