Skip to content
Merged
Show file tree
Hide file tree
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
9 changes: 6 additions & 3 deletions stride/physics/common/devito.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,10 +543,13 @@ def undersampled_time_derivative(self, fun, factor, time_bounds=None, offset=Non
deriv_order=1, fd_order=1):
offset = offset or (0, 0)

time_under, buffer_size = self._time_undersampled('time_under_d', factor, time_bounds, offset)

deriv = devito.Derivative(fun, (fun.dimensions[0], deriv_order), fd_order=fd_order)
deriv = deriv.xreplace({fun.dimensions[0]: time_under})

# Without an offset, keep the function's own undersampled time dimension. A new dimension
# leaves the old one's factor in the code that streams the function, with no value for it
if offset != (0, 0) or not fun.dimensions[0].is_Conditional:
time_under, buffer_size = self._time_undersampled('time_under_d', factor, time_bounds, offset)
deriv = deriv.xreplace({fun.dimensions[0]: time_under})

return deriv

Expand Down
42 changes: 23 additions & 19 deletions stride/physics/iso_acoustic/devito.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is wavelets removed?

Copy link
Copy Markdown
Contributor Author

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.

p_saved_expr = p
else:
p_saved_expr = self._forward_save(p)
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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)

Expand Down Expand Up @@ -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):
Expand Down
Loading