-
Notifications
You must be signed in to change notification settings - Fork 257
api: Symmetric interp gradients #3001
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a452ad2
dsl: Let an Eq carry its own interp mode
mloubout 9c692ec
dsl: Place a product by where its derivatives land
mloubout 94089ff
dsl: Accept the degenerate rebuild of a zero-order derivative
mloubout eb91c3f
tests: Cover symmetric placement and per-equation interp mode
mloubout a5a789c
examples: Show why symmetric interpolation is needed for gradients
mloubout ec12fb1
dsl: mute sympy matrix warning
mloubout 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
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
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 |
|---|---|---|
|
|
@@ -36,6 +36,15 @@ class Eq(sympy.Eq, Evaluable, Pickable): | |
| An ordered list of Dimensions that do not explicitly appear in either the | ||
| left-hand side or in the right-hand side, but that should be honored when | ||
| constructing an Operator. | ||
| interp_mode : str, optional, default=None | ||
| Overrides the Operator's `sym_opt={'interp-mode': ...}` for this | ||
| equation only. An Operator generally wants one mode -- `'direct'`, which | ||
| keeps finite-difference stencils compact -- while a single equation in | ||
| it may need `'symmetric'`, which re-associates a product of staggered | ||
| operands so that the discretized operator is the transpose of itself. | ||
| A gradient accumulated alongside the propagation it is the adjoint of is | ||
| the typical case: the stencils must stay compact, but the accumulation | ||
| has to be an exact transpose or the gradient degrades to first order. | ||
|
|
||
| Examples | ||
| -------- | ||
|
|
@@ -60,10 +69,10 @@ class Eq(sympy.Eq, Evaluable, Pickable): | |
| is_Reduction = False | ||
|
|
||
| __rargs__ = ('lhs', 'rhs') | ||
| __rkwargs__ = ('subdomain', 'coefficients', 'implicit_dims') | ||
| __rkwargs__ = ('subdomain', 'coefficients', 'implicit_dims', 'interp_mode') | ||
|
|
||
| def __new__(cls, lhs, rhs=0, subdomain=None, coefficients=None, | ||
| implicit_dims=None, **kwargs): | ||
| implicit_dims=None, interp_mode=None, **kwargs): | ||
| if coefficients is not None: | ||
| _ = deprecations.coeff_warn | ||
| kwargs['evaluate'] = False | ||
|
|
@@ -76,9 +85,15 @@ def __new__(cls, lhs, rhs=0, subdomain=None, coefficients=None, | |
| obj._subdomain = subdomain | ||
| obj._substitutions = coefficients | ||
| obj._implicit_dims = as_tuple(implicit_dims) | ||
| obj._interp_mode = interp_mode | ||
|
|
||
| return obj | ||
|
|
||
| @property | ||
| def interp_mode(self): | ||
| """Per-equation override of the Operator's `interp-mode`, or None.""" | ||
| return self._interp_mode | ||
|
|
||
| @classmethod | ||
| def _apply_coeffs(cls, expr, coefficients): | ||
| """ | ||
|
|
@@ -108,14 +123,17 @@ def _evaluate(self, **kwargs): | |
|
|
||
| The RHS of the Equation is evaluated at the indices of the LHS if required. | ||
| """ | ||
| if self._interp_mode is not None: | ||
|
Contributor
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. u probably don't need the
Contributor
Author
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. No it replaces the Operator level one already in kwargs |
||
| kwargs['interp_mode'] = self._interp_mode | ||
| try: | ||
| lhs = self.lhs._evaluate(**kwargs) | ||
| rhs = self.rhs._eval_at(self.lhs, **kwargs)._evaluate(**kwargs) | ||
| except AttributeError: | ||
| lhs, rhs = self._evaluate_args(**kwargs) | ||
| eq = self.func(lhs, rhs, subdomain=self.subdomain, | ||
| coefficients=self.substitutions, | ||
| implicit_dims=self._implicit_dims) | ||
| implicit_dims=self._implicit_dims, | ||
| interp_mode=self._interp_mode) | ||
|
|
||
| return eq | ||
|
|
||
|
|
||
Oops, something went wrong.
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.
aka
... = as_tuple(candidates) or diff_op._args_diff