Repository navigation
Conversation
…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
marked this pull request as ready for review
December 2, 2026 01:10
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Resolves #606, following #606 (comment): the change starts on
wo-4.0, replaces the role of theuse_svd=Falseapproximation (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 eigenproblemA A^Hin closed form instead of running a batchedtorch.linalg.svdover the 2 x Z matrices.Uis the eigenvectors,Sthe square roots of the eigenvalues (descending, as before) andVh_k = u_k^H A / s_k. That is the SVD ofAup to the phase of each singular-vector pair, which cancels inU S_reg Vh, so every inverse built from it is unchanged.torch.linalg.eigh, which for a large batch of complex128 matrices asked cuSOLVER for hundreds of GiB on the GPU._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,autoswitched 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 onauto(for examplephase.optimizewithadam); grid search and Nelder-Mead fits already used the SVD.use_svd=False/decomposition="per_channel"still works, unchanged, and emits aDeprecationWarning, so callers that pass it explicitly keep their results.The old
dual_variable_tikhonov_deconvolution_2dinutil.pysolves the same 2 x 2 system directly for the Tikhonov case; this keeps the singular-system interface, soSstays available totikhonov_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)
torch.linalg.svd, complex64, default driver (before)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)Uand ofVh's rows, andU S Vh = A, againsttorch.linalg.svdin double precision, on a tilted transfer function.torch.autograd.gradcheckof the singular values and of a phase-invariant use of the system (the Tikhonov inverse applied to data).torch.linalg.svd's to 1e-6 (double precision).reconstructis finite and matches finite differences (to 5e-3;reconstructbuilds the transfer function in single precision).decomposition="auto"with gradients equals"svd"and warns about nothing;per_channelstill returns the identityUand warns.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 thegpu-marked tests (5 passed) and the models and optim suites (127 passed).🤖 Generated with Claude Code