Skip to content

PERF: broadcast the Fourier probe shift over probe modes - #108

Merged
mdw771 merged 1 commit into
mainfrom
probe_mode_shift_memory
Nov 13, 2026
Merged

mdw771 merged 1 commit into
mainfrom
probe_mode_shift_memory

Conversation

@yijiang1

Copy link
Copy Markdown
Collaborator

Features/fixes

With subpixel_shift_all_probe_modes=True, the Fourier probe shift no longer builds a phase ramp for every (position, probe mode) pair. shift_unique_probes and adjoint_shift_probe_update_direction flattened the probes to (batch × modes, h, w) and repeated each shift once per mode. So interpolation.translate built a dense complex multiplier for each of the batch × modes images, then multiplied the spectrum out of place. Those are two batch × modes × N² arrays that are not needed: 4.45 GiB each at 512², 15 modes and batch 152.

  • interpolation.translate accepts (N, ..., H, W) images with one (N, 2) shift per leading index.
    • The Fourier and periodic cubic path builds one multiplier per shift, broadcasts it over the dimensions in between, and multiplies the fresh FFT output in place.
    • It stays out of place when autograd is recording, or when the product would promote (for example a complex128 multiplier on complex64 images). The autodiff path and mixed precision therefore behave as before.
    • Bilinear and nearest flatten the images and repeat the shifts inside translate, so callers don't depend on the method.
  • Both probe-shift callers pass the (batch, modes, h, w) tensor through instead of flattening it.
  • The translate and shift_images docstrings describe the new shape.

Results are unchanged. With the default subpixel_shift_all_probe_modes=False only mode 0 is shifted, so memory and speed don't change either.

Tests:

  • 51 new cases in tests/test_image_shift_adjoint.py. All of them fail on main, whose translate takes only (N, H, W). They check that:
    • a (N, M, H, W) stack gives the same result as the flattened stack with each shift repeated M times. This covers Fourier, cubic, bilinear and nearest, forward and adjoint, pad 0 and 1, and float32, complex64 and complex128. The input is not modified.
    • image and shift gradients through the broadcast multiplier match the flattened computation.
    • a complex128 multiplier on complex64 images still promotes the result to complex128.
  • The existing test_probe_shift_adjoint_uses_forward_padding already runs both callers with 3 modes, and checks the adjoint against autograd of the forward shift.

Checked main (cdea57b) against this branch:

  • Full test suite (without PTYCHO_CI_DATA_DIR): the pass/fail lists are identical apart from the 51 new tests, which pass (1524 → 1575 passed). The same 49 tests fail in both because they need the CI data. ruff check src/ (0.11.12) passes.
  • shift_images, stock (flattened) vs this branch (4D): this is the comparison behind the new tests. On CUDA it is bitwise identical in all 32 cases. On CPU, the 8 padded Fourier/cubic cases differ by 1–2 ulp (≤ 2.4e-7), because the vectorized kernel rounds the loop remainder of a broadcast multiply differently. The new test's tolerance allows for that.
  • LSQML on a real APS 31-ID-E scan, on one RTX A6000 (47.54 GiB):
    • 303 positions at 512², a probe with 15 incoherent × 2 OPR modes, and a 2432 × 1732 object.
    • Fourier interpolation, compact batching, momentum 0.5. The inputs and settings come from the APS pear workflow in which this OOM appeared.
    • All modes are shifted unless noted. Time per epoch is the mean of epochs 3–10.
batches allocator main this branch
2 × 152 default OOM in epoch 1 OOM in epoch 3 (37.67 GiB peak; see below)
2 × 152 expandable_segments:True 46.51 GiB allocated, 46.88 reserved; 1.66 s 37.90 GiB allocated, 41.74 reserved; 1.05 s
3 × 101 default 31.20 GiB allocated, 34.04 reserved; 1.20 s 25.48 GiB allocated, 26.64 reserved; 1.06 s
2 × 152, first mode only (the default option) default 34.56 GiB allocated, 44.39 reserved; 0.78 s 34.56 GiB allocated, 44.39 reserved; 0.78 s
  • Same results: loss histories are identical in every run that both trees finished: 10 epochs at 2 × 152 with expandable segments, 10 epochs at 3 × 101, and 4 epochs first-mode-only. With allow_nondeterministic_algorithms=False (3 epochs at 3 × 101), the final object and probe are bitwise identical.
  • This change alone does not make 2 × 152 fit on this card with the default caching allocator.
    • The branch fails in the same adjoint shift (ifft2) in epoch 3. At that point 33.33 GiB is allocated, but 11.06 GiB of the reserved memory is in pieces too small for the 4.45 GiB block it needs.
    • On main it fails in epoch 1, with 41.82 GiB allocated and 2.56 GiB in pieces.
    • Both failures reproduced in a second run, with no other process on the GPU.
    • The fragmentation is not specific to this change: the unchanged first-mode-only run also reserves 44.39 GiB for a 34.56 GiB peak.
  • PYTORCH_ALLOC_CONF=expandable_segments:True removes the fragmentation. With it the branch runs this case with 5.8 GiB to spare. main fits only just, with 0.66 GiB to spare, and is 1.6× slower per epoch. As noted in subpixel_shift_all_probe_modes=True: the Fourier shift builds a phase ramp per probe mode (OOM at 15 modes, 512²) #106, the setting may be worth recommending in the docs for runs with many modes.

The S0073 inputs and the scripts can be shared offline.

Related issues (optional)

Closes #106. Follow-up to #96.

Mentions

@mdw771 @hoidn

Checklist (Don't skip!)

Have you...

  • Formatted your code properly adhering to PEP-8? Considering using RUFF to format your code automatically (https://docs.astral.sh/ruff/installation/).
  • Cleaned up your code to remove large commented-out blocks?
  • Properly documented any new API knobs and functions with non-trivial signatures?
  • Resolved all merge conflicts with the main branch?
  • Checked the diffs between your branch and the main branch to ensure that your changes are not introducing any regressions or unnecessary/irrelevant changes?
  • Checked the diffs to ensure your PR only includes one feature/fix? Multiple features/fixes/changes should be split into different PRs.
  • Added unit tests for new features if necessary?

🤖 Generated with Claude Code

With subpixel_shift_all_probe_modes=True, shift_unique_probes and
adjoint_shift_probe_update_direction flattened the probes to
(batch * modes, h, w) and repeated each shift once per mode, so translate
built a dense complex multiplier for every (position, mode) pair and then
multiplied the spectrum out of place: two batch x modes x N^2 arrays that are
not needed, which made LSQML run out of memory with many modes at 512^2.

translate now accepts (N, ..., H, W) images with one shift per leading index.
The Fourier and periodic cubic path builds one multiplier per shift,
broadcasts it over the dimensions in between, and multiplies the fresh FFT
output in place unless autograd is recording or the product would promote.
The spatial kernels flatten the images and repeat the shifts themselves. Both
probe-shift callers pass the 4D tensor through. Results are unchanged
(bitwise identical on CUDA).

Closes #106

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mdw771
mdw771 merged commit 2f757ef into main Nov 13, 2026
2 checks passed
@mdw771
mdw771 deleted the probe_mode_shift_memory branch November 13, 2026 21:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

subpixel_shift_all_probe_modes=True: the Fourier shift builds a phase ramp per probe mode (OOM at 15 modes, 512²)

2 participants