Skip to content

Minimize jacobian refactors #1582

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

Open
wants to merge 3 commits 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
8 changes: 8 additions & 0 deletions pytensor/tensor/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,11 @@ def c_code_cache_version(self):
tensor_from_scalar = TensorFromScalar()


@_vectorize_node.register(TensorFromScalar)
def vectorize_tensor_from_scalar(op, node, batch_x):
return identity(batch_x).owner


class ScalarFromTensor(COp):
__props__ = ()

Expand Down Expand Up @@ -2046,6 +2051,8 @@ def register_transfer(fn):
"""Create a duplicate of `a` (with duplicated storage)"""
tensor_copy = Elemwise(ps.identity)
pprint.assign(tensor_copy, printing.IgnorePrinter())
identity = tensor_copy
pprint.assign(identity, printing.IgnorePrinter())


class Default(Op):
Expand Down Expand Up @@ -4603,6 +4610,7 @@ def ix_(*args):
"matrix_transpose",
"default",
"tensor_copy",
"identity",
"transfer",
"alloc",
"identity_like",
Expand Down
5 changes: 4 additions & 1 deletion pytensor/tensor/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,10 @@ def L_op(self, inputs, outputs, output_grads):
implicit_f = grad(inner_fx, inner_x)

df_dx, *df_dtheta_columns = jacobian(
implicit_f, [inner_x, *inner_args], disconnected_inputs="ignore"
implicit_f,
[inner_x, *inner_args],
disconnected_inputs="ignore",
vectorize=True,
)
grad_wrt_args = implict_optimization_grads(
df_dx=df_dx,
Expand Down