Repository navigation
Exact adjoint for fw3d_mode=False, without forcing fw3d_mode (devito#2780) - #124
Conversation
a63ddc5 to
bfb5e5a
Compare
before_adjoint forced fw3d_mode = True as a workaround for devitocodes/devito#2780, so fw3d_mode=False was ignored and the adjoint was one step off the forward, which records p.forward. - Remove the forced fw3d_mode, so the default fw3d_mode=False applies again. - Inject the receivers into p_a.backward after the stencil in both modes. Injecting into p_a before the stencil gives the same sum, but Devito then compiles a wrong time loop when a later equation reads p_a.backward (devito#2780). This layout compiles one backward time loop with any Devito version. - Delay the adjoint source by one step when fw3d_mode=False, which makes the adjoint exact. fw3d_mode=True keeps Fullwave3D's pairing, as before. shift_adjoint_source overrides the default in either mode. Also fix the vp gradient computed together with a wavelet, rho or alpha gradient: - Pass time_bounds= (not bounds=) to undersampled_time_derivative. - Keep the function's own undersampled time dimension in undersampled_time_derivative, so the code that streams p_saved has a value for its factor. - Add the vp gradient and preconditioner only at the saved time steps. - Save the raw wavefield only for a rho or alpha gradient (or force_raw_wavefield). The wavelet gradient does not read it, so a vp and wavelet gradient now gives the same vp gradient as a vp gradient alone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1300f44 to
ac8325d
Compare
| pass | ||
|
|
||
| if self._needs_grad(wavelets, rho, alpha, **kwargs): | ||
| if self._needs_grad(rho, alpha, **kwargs): |
There was a problem hiding this comment.
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.
|
|
||
| wavelets, _, rho, alpha = kwargs.get('wrt') | ||
| if self._needs_grad(wavelets, rho, alpha): | ||
| if self._needs_grad(rho, alpha, **kwargs): |
Problem
IsoAcousticDevito.before_adjointforcesfw3d_mode = True # TODO Force fw3d_mode pending Devito-side fix. That causes two problems:p.forward, but the fw3d-form adjoint is the exact transpose of recordingp. A dot-product test of the source-to-receiver map (random broadband source and residual, 3 trials, spread = std/|mean| of<Fs,r>/<s,Fᵀr>) gives a spread of 1.9e-1 onmaster. An exact adjoint gives about 5e-5.fw3d_mode=Falseis ignored. The forcing was added for a Devito scheduling bug, devitocodes/devito#2780:p_a[t]before the stencil (the old non-fw3d receiver injection), then an equation after the stencil readsp_a.backward. stride-private's local preconditioner (p_a.backward.dt) does this. stride's own gradient equations do not.p_abuffer.main): Devito keeps one loop but runs the whole loop forward.Also, a vp gradient computed together with a wavelet, rho or alpha gradient (the "joint" path) did not work:
_forward_save_undersampledpassesbounds=toundersampled_time_derivative, which takestime_bounds=, so it raised aTypeError.p_savedstill used the factor ofp_saved's own time dimension, whichundersampled_time_derivativehad replaced, so it failed withInvalidArgument: No value found for parameter time_under0f.save_undersampling>1:p_dtis aFunctionwith no time dimension, so itsIncran at every time step with the last saved value.Fix
The fixes apply to
fw3d_mode=False, the default.fw3d_mode=Truebehaves as onmaster. This PR needs no Devito change.fw3d_modeis again the caller's choice (defaultFalse).p_a.backwardafter the stencil. Injectingrec(t)intop_abefore the stencil gives the same sum, but it is the layout that triggers devito#2780. With this layout, Devito compiles one backward time loop, with or without the Devito bug.fw3d_mode=False, so the adjoint pairs with recordingp.forwardand is exact.fw3d_mode=Truekeeps Fullwave3D's pairing (no delay), as onmaster.shift_adjoint_source=True/Falseoverrides the default in either mode.bounds=→time_bounds=in_forward_save_undersampled.undersampled_time_derivativekeeps the function's own undersampled time dimension (a new one only with an offset; no caller passes one).p_saved, so only a rho or alpha gradient (orforce_raw_wavefield) saves the raw wavefield. A vp and wavelet gradient now saves the same derivative as a vp gradient alone, so its vp gradient is identical.prepare_grad_vpnow also passesforce_raw_wavefield, so it matches what the forward saved.Behaviour change: with the default
fw3d_mode=False, the adjoint was the forced fw3d form without a delay (one step off). It is now exact.fw3d_mode=Trueis unchanged.stride-private's
IsoAcousticDevitoinheritsbefore_adjoint, so it gets this layout too. Its own joint-path code is fixed in trustimaging/stride-private#92, which needs this PR.Evidence
Small 3D problem, 1 A100,
nvidia-cuda(sponge_boundary_2, hicks, OT4), Devito 4.8.22.dev142 without any devito#2780 fix. Every case compiles one backward adjoint time loop.stride's
IsoAcousticDevito:save_undersampling=8)master(forced fw3d form, no delay)fw3d_mode=True(no delay, asmaster)fw3d_mode=Falsefw3d_mode=False,save_undersampling=1The FD ratio is constant up to stride's gradient scale and sign convention.
stride-private's
IsoAcousticDevito(local preconditioner),save_undersampling=8:fw3d_mode=False, GPUfw3d_mode=False, CPUfw3d_mode=True+shift_adjoint_source=True, GPUfw3d_mode=FalseThese ratios are not constant because stride-private applies its local preconditioner to the gradient. GPU and CPU agree.
Joint path, raw
grad_vpcompared with the vp-only gradient (same setup):save_undersampling=1save_undersampling=8masterTypeErrorTypeErrorforce_raw_wavefield, as for rho), strideAt
save_undersampling=1, the raw-wavefield path differs from the vp-only path by as much as GPU and CPU runs of the same code differ from each other (corr 0.999). Atsave_undersampling=8, it takes time derivatives from samples 8 steps apart, which is less accurate than saving the derivative. Against thesave_undersampling=1gradient, stride's raw-wavefield path is more accurate (corr 0.954) than the vp-only path (0.923). stride-private's first derivative is less accurate (0.847 vs 0.933). The wavelet dot test with stride-private gives a spread of 5.55e-5.Production check (earlier layout)
Before this layout, the exact non-fw3d adjoint injected
rec(t)intop_abefore the stencil and needed a Devito fix for stride-private. With that fix, a full reg004 iteration (stride-private, 5420 shots, 8 A100s), non-fw3d form against the fw3d-form reference run:That difference came from stride-private's vp gradient, which reads
p_a.backward.dt: in the old non-fw3d layout,p_a[t-1]did not yet hold the current injection. This PR's layout is the fw3d layout with a one-step delay, so stride-private's gradient withfw3d_mode=Falsenow matches the delayed fw3d form exactly (table above).Devito side
devito#2780 is still open. devitocodes/devito#3132 proposes a fix. This PR does not depend on it.
🤖 Generated with Claude Code