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

Initial implicit/truncated backward modes #29

Merged
merged 14 commits into from
Jan 19, 2022
Prev Previous commit
Next Next commit
fix gradient scaling for #39
  • Loading branch information
bamos committed Jan 19, 2022
commit f9cb5992c77c09a34addf05fe480c58f306fca1b
20 changes: 14 additions & 6 deletions theseus/optimizer/nonlinear/nonlinear_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def _optimize_loop(
info,
track_best_solution,
verbose,
force_update,
truncated_grad_loop,
**kwargs,
):
converged_indices = torch.zeros_like(info.last_err).bool()
Expand All @@ -197,8 +197,15 @@ def _optimize_loop(
info.status[:] = NonlinearOptimizerStatus.FAIL
return info

if truncated_grad_loop:
step_size = 1.0
force_update = True
else:
step_size = self.params.step_size
force_update = False

self.retract_and_update_variables(
delta, converged_indices, force_update=force_update
delta, converged_indices, step_size, force_update=force_update
)

# check for convergence
Expand Down Expand Up @@ -251,7 +258,7 @@ def _optimize_impl(
info=info,
track_best_solution=track_best_solution,
verbose=verbose,
force_update=False,
truncated_grad_loop=False,
**kwargs,
)
elif backward_mode in [BackwardMode.IMPLICIT, BackwardMode.TRUNCATED]:
Expand All @@ -272,7 +279,7 @@ def _optimize_impl(
info=info,
track_best_solution=track_best_solution,
verbose=verbose,
force_update=False,
truncated_grad_loop=False,
**kwargs,
)

Expand All @@ -283,7 +290,7 @@ def _optimize_impl(
info=grad_loop_info,
track_best_solution=False,
verbose=verbose,
force_update=True,
truncated_grad_loop=True,
**kwargs,
)

Expand Down Expand Up @@ -313,10 +320,11 @@ def retract_and_update_variables(
self,
delta: torch.Tensor,
converged_indices: torch.Tensor,
step_size: float,
force_update: bool = False,
):
var_idx = 0
delta = self.params.step_size * delta
delta = step_size * delta
for var in self.linear_solver.linearization.ordering:
new_var = var.retract(delta[:, var_idx : var_idx + var.dof()])
var.update(new_var.data, batch_ignore_mask=converged_indices)
bamos marked this conversation as resolved.
Show resolved Hide resolved
Expand Down