-
Notifications
You must be signed in to change notification settings - Fork 176
Cache TLM and Hessian solvers for NLVS blocks #4554
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
Draft
angus-g
wants to merge
2
commits into
main
Choose a base branch
from
angus-g/cache-nlvs-hessian
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -29,6 +29,8 @@ class Solver(Enum): | |||||
| """Enum for solver types.""" | ||||||
| FORWARD = 0 | ||||||
| ADJOINT = 1 | ||||||
| TLM = 2 | ||||||
| HESSIAN = 3 | ||||||
|
|
||||||
|
|
||||||
| class GenericSolveBlock(Block): | ||||||
|
|
@@ -227,6 +229,9 @@ def _assemble_and_solve_adj_eq(self, dFdu_adj_form, dJdu, compute_bdy): | |||||
|
|
||||||
| return adj_sol, adj_sol_bdy | ||||||
|
|
||||||
| def _hessian_solve(self, *args): | ||||||
| return self._assemble_and_solve_adj_eq(*args) | ||||||
|
|
||||||
| def _compute_adj_bdy(self, adj_sol, adj_sol_bdy, dFdu_adj_form, dJdu): | ||||||
| adj_sol_bdy = firedrake.assemble(dJdu - firedrake.action(dFdu_adj_form, adj_sol)) | ||||||
| return adj_sol_bdy.riesz_representation("l2") | ||||||
|
|
@@ -385,8 +390,7 @@ def _assemble_and_solve_soa_eq(self, dFdu_form, adj_sol, hessian_input, | |||||
| b = self._assemble_soa_eq_rhs(dFdu_form, adj_sol, hessian_input, | ||||||
| d2Fdu2) | ||||||
| dFdu_form = firedrake.adjoint(dFdu_form) | ||||||
| adj_sol2, adj_sol2_bdy = self._assemble_and_solve_adj_eq(dFdu_form, b, | ||||||
| compute_bdy) | ||||||
| adj_sol2, adj_sol2_bdy = self._hessian_solve(dFdu_form, b, compute_bdy) | ||||||
| if self.adj2_cb is not None: | ||||||
| self.adj2_cb(adj_sol2) | ||||||
| if self.adj2_bdy_cb is not None and compute_bdy: | ||||||
|
|
@@ -685,11 +689,30 @@ def _adjoint_solve(self, dJdu, compute_bdy): | |||||
| u_sol, adj_sol_bdy, jac_adj, dJdu_copy) | ||||||
| return u_sol, adj_sol_bdy | ||||||
|
|
||||||
| def _hessian_solve(self, adj_form, rhs, compute_bdy): | ||||||
| # self._ad_solver_replace_forms(Solver.HESSIAN) | ||||||
| # self._ad_solvers["hessian_lvs"].invalidate_jacobian() | ||||||
| self._ad_solvers["hessian_lvs"]._problem.F._components[1].assign(rhs) | ||||||
| self._ad_solvers["hessian_lvs"].solve() | ||||||
| u_sol = self._ad_solvers["hessian_lvs"]._problem.u | ||||||
|
|
||||||
| adj_sol_bdy = None | ||||||
| if compute_bdy: | ||||||
| jac_adj = self._ad_solvers["hessian_lvs"]._problem.J | ||||||
| adj_sol_bdy = self._compute_adj_bdy( | ||||||
| u_sol, adj_sol_bdy, jac_adj, rhs.copy() | ||||||
| ) | ||||||
|
|
||||||
| return u_sol, adj_sol_bdy | ||||||
|
|
||||||
| def _ad_assign_map(self, form, solver): | ||||||
| if solver == Solver.FORWARD: | ||||||
| count_map = self._ad_solvers["forward_nlvs"]._problem._ad_count_map | ||||||
| else: | ||||||
| elif solver == Solver.ADJOINT: | ||||||
| count_map = self._ad_solvers["adjoint_lvs"]._problem._ad_count_map | ||||||
| elif solver == Solver.TLM: | ||||||
| count_map = self._ad_solvers["tlm_lvs"]._problem._ad_count_map | ||||||
|
|
||||||
| assign_map = {} | ||||||
| form_ad_count_map = dict((count_map[coeff], coeff) | ||||||
| for coeff in form.coefficients()) | ||||||
|
|
@@ -700,8 +723,10 @@ def _ad_assign_map(self, form, solver): | |||||
| firedrake.Cofunction)): | ||||||
| coeff_count = coeff.count() | ||||||
| if coeff_count in form_ad_count_map: | ||||||
| assign_map[form_ad_count_map[coeff_count]] = \ | ||||||
| block_variable.saved_output | ||||||
| if solver == Solver.HESSIAN: | ||||||
| assign_map[form_ad_count_map[coeff_count]] = block_variable.tlm_value | ||||||
| else: | ||||||
| assign_map[form_ad_count_map[coeff_count]] = block_variable.saved_output | ||||||
|
|
||||||
| if ( | ||||||
| solver == Solver.ADJOINT | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this make sure that the state is updated correctly for the Hessian solve?
Suggested change
|
||||||
|
|
@@ -712,6 +737,7 @@ def _ad_assign_map(self, form, solver): | |||||
| if coeff_count in form_ad_count_map: | ||||||
| assign_map[form_ad_count_map[coeff_count]] = \ | ||||||
| block_variable.saved_output | ||||||
|
|
||||||
| return assign_map | ||||||
|
|
||||||
| def _ad_assign_coefficients(self, form, solver): | ||||||
|
|
@@ -724,9 +750,17 @@ def _ad_solver_replace_forms(self, solver=Solver.FORWARD): | |||||
| problem = self._ad_solvers["forward_nlvs"]._problem | ||||||
| self._ad_assign_coefficients(problem.F, solver) | ||||||
| self._ad_assign_coefficients(problem.J, solver) | ||||||
| else: | ||||||
| elif solver == Solver.ADJOINT: | ||||||
| self._ad_assign_coefficients( | ||||||
| self._ad_solvers["adjoint_lvs"]._problem.J, solver) | ||||||
| elif solver == Solver.TLM: | ||||||
| self._ad_assign_coefficients( | ||||||
| self._ad_solvers["tlm_lvs"]._problem.J, solver | ||||||
| ) | ||||||
| elif solver == Solver.HESSIAN: | ||||||
| self._ad_assign_coefficients( | ||||||
| self._ad_solvers["hessian_lvs"]._problem.J, solver | ||||||
| ) | ||||||
|
|
||||||
| def prepare_evaluate_adj(self, inputs, adj_inputs, relevant_dependencies): | ||||||
| compute_bdy = self._should_compute_boundary_adjoint( | ||||||
|
|
@@ -803,6 +837,54 @@ def evaluate_adj_component(self, inputs, adj_inputs, block_variable, idx, | |||||
|
|
||||||
| return dFdm | ||||||
|
|
||||||
| def evaluate_tlm_component(self, inputs, tlm_inputs, block_variable, idx, | ||||||
| prepared=None): | ||||||
| F_form = prepared["form"] | ||||||
| dFdu = prepared["dFdu"] | ||||||
|
|
||||||
| bcs = [] | ||||||
| dFdm = 0. | ||||||
| for block_variable in self.get_dependencies(): | ||||||
| tlm_value = block_variable.tlm_value | ||||||
| c = block_variable.output | ||||||
| c_rep = block_variable.saved_output | ||||||
|
|
||||||
| if isinstance(c, firedrake.DirichletBC): | ||||||
| if tlm_value is None: | ||||||
| bcs.append(c.reconstruct(g=0)) | ||||||
| else: | ||||||
| bcs.append(tlm_value) | ||||||
| continue | ||||||
| elif isinstance(c, firedrake.MeshGeometry): | ||||||
| X = firedrake.SpatialCoordinate(c) | ||||||
| c_rep = X | ||||||
|
|
||||||
| if tlm_value is None: | ||||||
| continue | ||||||
|
|
||||||
| if c == self.func and not self.linear: | ||||||
| continue | ||||||
|
|
||||||
| dFdm += firedrake.derivative(-F_form, c_rep, tlm_value) | ||||||
|
|
||||||
| if isinstance(dFdm, float): | ||||||
| v = dFdu.arguments()[0] | ||||||
| dFdm = firedrake.inner( | ||||||
| firedrake.Constant(numpy.zeros(v.ufl_shape)), v | ||||||
| ) * firedrake.dx | ||||||
|
|
||||||
| dFdm = ufl.algorithms.expand_derivatives(dFdm) | ||||||
| dFdm = firedrake.assemble(dFdm) | ||||||
|
|
||||||
| # XXX I dunno how this works | ||||||
| self._ad_solver_replace_forms(Solver.TLM) | ||||||
| self._ad_solvers["tlm_lvs"].invalidate_jacobian() | ||||||
| # update RHS | ||||||
| self._ad_solvers["tlm_lvs"]._problem.F._components[1].assign(dFdm) | ||||||
|
|
||||||
| self._ad_solvers["tlm_lvs"].solve() | ||||||
| return self._ad_solvers["tlm_lvs"]._problem.u | ||||||
|
|
||||||
|
|
||||||
| class ProjectBlock(SolveVarFormBlock): | ||||||
| def __init__(self, v, V, output, bcs=[], *args, **kwargs): | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the hessian solve don't you need to update from
tlm_valueas well as fromsaved_output, rather than instead of? Otherwise the adjoint Jacobian in the_ad_solvers["hessian_lvs"]won't be assembled around the correct state.