Skip to content

perf: closed-form singular system for the thin-object phase transfer function - #607

Open
ieivanov wants to merge 1 commit into
wo-4.0from
closed-form-thin-singular-system
Open

ieivanov wants to merge 1 commit into
wo-4.0from
closed-form-thin-singular-system

Conversation

@ieivanov

@ieivanov ieivanov commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #606, following #606 (comment): the change starts on wo-4.0, replaces the role of the use_svd=False approximation (kept, deprecated), and tests both the singular systems and their gradients.

What changes

  • isotropic_thin_3d.calculate_singular_system (use_svd=True, the default) solves each spatial frequency's 2 x 2 Gram eigenproblem A A^H in closed form instead of running a batched torch.linalg.svd over the 2 x Z matrices. U is the eigenvectors, S the square roots of the eigenvalues (descending, as before) and Vh_k = u_k^H A / s_k. That is the SVD of A up to the phase of each singular-vector pair, which cancels in U S_reg Vh, so every inverse built from it is unchanged.
  • The work runs in double precision where the device supports it (not on MPS), since the Gram matrix squares the condition number: in single precision the smallest singular values (about 1e-5 of the largest) come out several times too large. The 2 x 2 problem is solved elementwise rather than with torch.linalg.eigh, which for a large batch of complex128 matrices asked cuSOLVER for hundreds of GiB on the GPU.
  • It is differentiable: square roots and divisions are guarded (_safe_sqrt, _safe_divide), so gradients stay finite where a singular value or the whole transfer function is zero (outside the pupil). As for any SVD, gradients of the singular vectors grow as the two singular values approach each other.
  • reconstruct(decomposition="auto") now always uses the exact system. Before, auto switched to the per-channel approximation whenever the tilt or NA required gradients, so a gradient-based fit and the final reconstruction used different decompositions. This changes the results of gradient-based fits that relied on auto (for example phase.optimize with adam); grid search and Nelder-Mead fits already used the SVD.
  • use_svd=False / decomposition="per_channel" still works, unchanged, and emits a DeprecationWarning, so callers that pass it explicitly keep their results.

The old dual_variable_tikhonov_deconvolution_2d in util.py solves the same 2 x 2 system directly for the Tikhonov case; this keeps the singular-system interface, so S stays available to tikhonov_regularized_inverse_filter (apodization) and to the stored transfer functions.

Benchmark (48 x 640 x 960 tilted transfer function, NA 1.35 / 0.52, RTX PRO 6000 Blackwell, torch 2.10 cu128)

time max error of the Tikhonov inverse vs a double-precision CPU SVD
closed form (this PR) 0.13 s 1.7e-7
torch.linalg.svd, complex64, default driver (before) 117 s 5.9e-5

Singular values agree with the GPU SVD to 5.5e-7 of the largest. The gradient of a Tikhonov loss with respect to the tilt through the closed form takes 0.57 s and is finite; peak memory 13.7 GiB. Timings come from this GPU only.

Tests (tests/models/test_isotropic_thin_3d_singular_system.py, new)

  • Singular values, the Tikhonov inverse, orthonormality of U and of Vh's rows, and U S Vh = A, against torch.linalg.svd in double precision, on a tilted transfer function.
  • Batched input equals each item.
  • Degenerate frequencies: zero transfer function, a Gram matrix that is a multiple of the identity, a rank-one matrix, an absorption-only row; all finite and orthonormal.
  • torch.autograd.gradcheck of the singular values and of a phase-invariant use of the system (the Tikhonov inverse applied to data).
  • Gradients with respect to the tilt equal torch.linalg.svd's to 1e-6 (double precision).
  • The gradient through reconstruct is finite and matches finite differences (to 5e-3; reconstruct builds the transfer function in single precision).
  • decomposition="auto" with gradients equals "svd" and warns about nothing; per_channel still returns the identity U and warns.
  • Existing suites on CPU: tests/models, tests/optim, tests/api_tests, tests/calibrate, test_reconstruct.py, test_examples.py (447 passed) and the rest without widgets and mmcore (460 passed); on a GPU node the gpu-marked tests (5 passed) and the models and optim suites (127 passed).

🤖 Generated with Claude Code

…function (#606)

calculate_singular_system now solves each spatial frequency's 2 x 2 Gram
eigenproblem in closed form, in double precision where the device supports
it, instead of a batched torch.linalg.svd of the 2 x Z matrices. It is the
same singular system up to the phase of each singular-vector pair, which
cancels in the inverse filter, about 900 times faster on a 48 x 640 x 960
stack on a GPU (0.13 s against 117 s), and differentiable, with square
roots and divisions guarded where a singular value or the transfer function
is zero.

- decomposition="auto" now always uses the exact system, so gradient-based
  fits and the final reconstruction share one decomposition.
- use_svd=False / decomposition="per_channel" still works and emits a
  DeprecationWarning.
- Tests: singular values, orthonormality and the inverse against
  torch.linalg.svd, degenerate frequencies, batching, gradcheck, gradients
  against the SVD's, and the gradient through reconstruct.
@ieivanov
ieivanov marked this pull request as ready for review December 2, 2026 01:10
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.

1 participant