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

Fix jvp signature for Objective #909

Merged
merged 3 commits into from
Feb 29, 2024
Merged
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
12 changes: 8 additions & 4 deletions desc/objectives/objective_funs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,19 +1021,21 @@
----------
v : tuple of ndarray
Vectors to right-multiply the Jacobian by.
x : ndarray
x : tuple of ndarray
Optimization variables.
constants : list
Constant parameters passed to sub-objectives.

"""
v = v if isinstance(v, (tuple, list)) else (v,)
x = x if isinstance(x, (tuple, list)) else (x,)
assert len(x) == len(v)

Check warning on line 1032 in desc/objectives/objective_funs.py

View check run for this annotation

Codecov / codecov/patch

desc/objectives/objective_funs.py#L1031-L1032

Added lines #L1031 - L1032 were not covered by tests

compute_scaled = lambda *x: self.compute_scaled(*x, constants=constants)
jvpfun = lambda *dx: Derivative.compute_jvp(
compute_scaled, tuple(range(len(x))), dx, *x
)
sig = "(n)" * len(x) + "->(k)"
sig = ",".join(f"(n{i})" for i in range(len(x))) + "->(k)"

Check warning on line 1038 in desc/objectives/objective_funs.py

View check run for this annotation

Codecov / codecov/patch

desc/objectives/objective_funs.py#L1038

Added line #L1038 was not covered by tests
return jnp.vectorize(jvpfun, signature=sig)(*v)

def jvp_unscaled(self, v, x, constants=None):
Expand All @@ -1045,19 +1047,21 @@
----------
v : tuple of ndarray
Vectors to right-multiply the Jacobian by.
x : ndarray
x : tuple of ndarray
Optimization variables.
constants : list
Constant parameters passed to sub-objectives.

"""
v = v if isinstance(v, (tuple, list)) else (v,)
x = x if isinstance(x, (tuple, list)) else (x,)
assert len(x) == len(v)

Check warning on line 1058 in desc/objectives/objective_funs.py

View check run for this annotation

Codecov / codecov/patch

desc/objectives/objective_funs.py#L1057-L1058

Added lines #L1057 - L1058 were not covered by tests

compute_unscaled = lambda *x: self.compute_unscaled(*x, constants=constants)
jvpfun = lambda *dx: Derivative.compute_jvp(
compute_unscaled, tuple(range(len(x))), dx, *x
)
sig = "(n)" * len(x) + "->(k)"
sig = ",".join(f"(n{i})" for i in range(len(x))) + "->(k)"

Check warning on line 1064 in desc/objectives/objective_funs.py

View check run for this annotation

Codecov / codecov/patch

desc/objectives/objective_funs.py#L1064

Added line #L1064 was not covered by tests
return jnp.vectorize(jvpfun, signature=sig)(*v)

def print_value(self, *args, **kwargs):
Expand Down
Loading