Repository navigation
Exact adjoint for fw3d_mode=False, without forcing fw3d_mode (devito#2780) #124
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
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -411,7 +411,7 @@ def before_forward(self, wavelets, vp, rho=None, alpha=None, **kwargs): | |
| # ValueError: Cannot access `shape_allocated` as unfinalized - so no size estimate | ||
| pass | ||
|
|
||
| if self._needs_grad(wavelets, rho, alpha, **kwargs): | ||
| if self._needs_grad(rho, alpha, **kwargs): | ||
| p_saved_expr = p | ||
| else: | ||
| p_saved_expr = self._forward_save(p) | ||
|
|
@@ -727,7 +727,6 @@ def before_adjoint(self, adjoint_source, wavelets, vp, rho=None, alpha=None, **k | |
| time_bounds = kwargs.get('time_bounds', (0, self.time.extended_num)) | ||
|
|
||
| fw3d_mode = kwargs.pop('fw3d_mode', False) | ||
| fw3d_mode = True # TODO Force fw3d_mode pending Devito-side fix | ||
| platform = kwargs.get('platform', 'cpu') | ||
| is_nvidia = platform is not None and 'nvidia' in platform | ||
|
|
||
|
|
@@ -749,13 +748,11 @@ def before_adjoint(self, adjoint_source, wavelets, vp, rho=None, alpha=None, **k | |
| # Create stencil | ||
| stencil = self._stencil(p_a, wavelets, vp, rho=rho, alpha=alpha, direction='backward', **kwargs) | ||
|
|
||
| # Define the source injection function to generate the corresponding code | ||
| t = rec.time_dim | ||
| # Define the receiver injection, into p_a.backward after the stencil. Injecting into p_a | ||
| # before the stencil would be the same sum, but Devito then compiles a wrong time loop when | ||
| # an equation after the stencil reads p_a.backward (devitocodes/devito#2780) | ||
| vp2 = self.dev_grid.vars.vp**2 | ||
| if not fw3d_mode: | ||
| rec_term = rec.inject(field=p_a, expr=-rec.subs({t: t-1}) * self.time.step**2 * vp2) | ||
| else: | ||
| rec_term = rec.inject(field=p_a.backward, expr=-rec * self.time.step**2 * vp2) | ||
| rec_term = rec.inject(field=p_a.backward, expr=-rec * self.time.step**2 * vp2) | ||
|
|
||
| if wavelets.needs_grad: | ||
| src_term = src.interpolate(expr=-p_a) | ||
|
|
@@ -792,12 +789,8 @@ def before_adjoint(self, adjoint_source, wavelets, vp, rho=None, alpha=None, **k | |
| if self.attenuation_power == 2: | ||
| kwargs['devito_config']['opt'] = 'noop' | ||
|
|
||
| if fw3d_mode: | ||
| self.adjoint_operator.set_operator(stencil + rec_term + src_term + gradient_update + update_saved, | ||
| **kwargs) | ||
| else: | ||
| self.adjoint_operator.set_operator(rec_term + stencil + src_term + gradient_update + update_saved, | ||
| **kwargs) | ||
| self.adjoint_operator.set_operator(stencil + rec_term + src_term + gradient_update + update_saved, | ||
| **kwargs) | ||
| self.adjoint_operator.compile() | ||
|
|
||
| else: | ||
|
|
@@ -875,6 +868,13 @@ def before_adjoint(self, adjoint_source, wavelets, vp, rho=None, alpha=None, **k | |
|
|
||
| self.dev_grid.vars.rec.data[:] = adjoint_source.T * window | ||
|
|
||
| # The forward records p.forward, one step after the field the adjoint operator is the exact | ||
| # transpose for, so delay the adjoint source by one step to make the adjoint exact. fw3d_mode | ||
| # keeps Fullwave3D's pairing, without the delay, unless shift_adjoint_source=True | ||
| if kwargs.get('shift_adjoint_source', not fw3d_mode): | ||
| self.dev_grid.vars.rec.data[1:] = self.dev_grid.vars.rec.data[:-1].copy() | ||
| self.dev_grid.vars.rec.data[0] = 0. | ||
|
|
||
| if self.interpolation_type == 'linear': | ||
| self.dev_grid.vars.src.coordinates.data[:] = source_coordinates | ||
| self.dev_grid.vars.rec.coordinates.data[:] = receiver_coordinates | ||
|
|
@@ -1013,22 +1013,26 @@ def prepare_grad_vp(self, vp, **kwargs): | |
| subdomain = abox if self.adaptive_boxes else interior | ||
|
|
||
| wavelets, _, rho, alpha = kwargs.get('wrt') | ||
| if self._needs_grad(wavelets, rho, alpha): | ||
| if self._needs_grad(rho, alpha, **kwargs): | ||
|
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. same comment as above |
||
| p_dt = self._forward_save_undersampled(p, **kwargs) | ||
| p_dt_fun = self.dev_grid.function('p_dt', space_order=0) | ||
| p_dt_update = (devito.Eq(p_dt_fun, p_dt, subdomain=subdomain),) | ||
| # p_dt_fun has no time dimension, so tie the updates to the saved time steps, as when | ||
| # p_saved holds the derivative; otherwise every time step adds the last saved value | ||
| implicit_dims = (p.time_dim,) | ||
| p_dt_update = (devito.Eq(p_dt_fun, p_dt, implicit_dims=implicit_dims, subdomain=subdomain),) | ||
| else: | ||
| p_dt = p | ||
| p_dt_fun = p_dt | ||
| p_dt_update = () | ||
| implicit_dims = None | ||
|
|
||
| w = self._time_weights(**kwargs) | ||
|
|
||
| grad = self.dev_grid.function('grad_vp', space_order=0) | ||
| grad_update = devito.Inc(grad, w * p_dt_fun * p_a, subdomain=subdomain) | ||
| grad_update = devito.Inc(grad, w * p_dt_fun * p_a, implicit_dims=implicit_dims, subdomain=subdomain) | ||
|
|
||
| prec = self.dev_grid.function('prec_vp', space_order=0) | ||
| prec_update = devito.Inc(prec, w * p_dt_fun * p_dt_fun, subdomain=subdomain) | ||
| prec_update = devito.Inc(prec, w * p_dt_fun * p_dt_fun, implicit_dims=implicit_dims, subdomain=subdomain) | ||
|
|
||
| return p_dt_update + (grad_update, prec_update) | ||
|
|
||
|
|
@@ -1706,7 +1710,7 @@ def _forward_save(self, field): | |
|
|
||
| def _forward_save_undersampled(self, field, **kwargs): | ||
| return self.dev_grid.undersampled_time_derivative(field, self.undersampling_factor, | ||
| bounds=kwargs.get('time_bounds', None), | ||
| time_bounds=kwargs.get('time_bounds', None), | ||
| deriv_order=2, fd_order=2) | ||
|
|
||
| def _adjoint_save(self, field): | ||
|
|
||
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.
why is wavelets removed?
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.
claude reply:
p_saved is only used by the rho (and alpha) gradient, which needs p itself. The wavelet gradient doesn't read it: prepare_grad_wavelets returns () and the gradient comes from src.interpolate(-p_a) in the adjoint. So with wavelets in the check, a vp + wavelet inversion saved raw p for nothing, and the vp gradient had to rebuild p.dt2 from samples undersampling steps apart. That path was broken (bounds= TypeError, then No value found for parameter time_under0f from the streaming code), and once fixed it is less accurate than saving p.dt2 directly (corr 0.954 vs the full-rate gradient for stride, 0.847 for stride-private's first derivative).
Without wavelets in the check, vp + wavelet saves the same derivative as vp alone and gives a bit-identical vp gradient (rel. diff 0 at save_undersampling 1 and 8, stride and stride-private). The wavelet dot test is unchanged (spread 5.5e-5).
The second line also passes **kwargs now, so force_raw_wavefield is seen by both the forward and prepare_grad_vp. Before, force_raw_wavefield=True with a vp-only gradient saved raw p in the forward but used it as p.dt2 in the adjoint.
If you'd rather keep raw p whenever wavelets are inverted, I can put wavelets back; the joint path now works and is exact at save_undersampling=1.