Skip to content

Reject non-finite LM trials and restore rejected parameters - #442

Open
cola1917 wants to merge 3 commits into
pypose:mainfrom
cola1917:cola1917/fix-lm-nonfinite-trials
Open

cola1917 wants to merge 3 commits into
pypose:mainfrom
cola1917:cola1917/fix-lm-nonfinite-trials

Conversation

@cola1917

@cola1917 cola1917 commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #441.

LM currently accepts a NaN trial loss because last < loss is false for NaN. It can also retain an infinite trial when its rejection budget is exhausted. For example, fitting sqrt(x) to 0.1 from x=1 takes a finite first step to approximately -0.8, returns NaN without a rejection, and fails the next Jacobian evaluation.

  • Reject non-finite trial losses regardless of the remaining retry budget.
  • Restore trainable parameters from pre-step values for rejected trials. Subtracting the step cannot recover a parameter that already contains NaN, and inverse Lie-group updates need not restore the exact original values.
  • Keep the existing retry bound and the previous finite loss when no valid trial is found.
  • Preserve the existing acceptance/retry-limit rules for finite losses; the regression tests include this control.

The implementation change is confined to LevenbergMarquardt.step() and the reject argument documentation. It does not change Jacobian conventions, sparse linear algebra, or the work in #405/#407/#440.

Regression coverage

The new tests use deterministic inputs and do not advance the global RNG:

  • A real sqrt domain overshoot, followed by successful damped retries and convergence, in float32/float64.
  • NaN and infinity with reject=0 and exhausted positive retry budgets, using Constant, Adaptive, and TrustRegion strategies.
  • Exact restoration of ordinary parameters and SE3 parameters after a solver produces a non-finite update, including a frozen parameter and preservation of parameter identity.
  • Ordinary finite accepted steps and existing finite uphill behavior at the retry limit.
  • Real BAE sparse-backend sqrt/exp trials, with retry budgets 0/2, repeated steps, and exact parameter/loss restoration.

On unmodified main 9345958013d4cc73b3d03f6be54236135332699b, the new CPU file has 16 failures and 2 passing controls. The same file passes with this fix on CPU and CUDA.

Validation

  • Python 3.9.25 / PyTorch 2.8.0+cpu: full suite 215 passed, 18 skipped.
  • Python 3.10 / PyTorch 2.7.0+cu128, RTX 4080 SUPER: full suite 249 passed, 2 skipped. The new file explicitly runs both CPU and CUDA cases in this environment.
  • Existing TestOptim.test_optim_anybatch, fixed seeds 100 through 264: 5 failures on main (139/159/236: NaN; 211/264: convergence bound), 0 failures with this fix. This is supporting evidence, not a claim that every numerical-instability source is fixed.
  • Sphinx HTML build with Python 3.10 succeeds with no warnings; the updated argument text appears in the rendered LM page.
  • git diff --check passes.

Full suites were run with initial torch.manual_seed(0) and OMP_NUM_THREADS=2 MKL_NUM_THREADS=2. No existing assertions, skips, RNG seeds, or CI configuration were changed.

Cross-library follow-up

Compared against BAE afad40e, including both LM.step() and Schur.step() and their explicit/matrix-free paths. Both override PyPose's step, so a coordinated fix is proposed in pypose/bae#50. Finite acceptance and retry-budget rules are preserved; non-finite trials always restore the pre-trial parameters/loss. Strategy update ordering is unchanged.

Additional validation with Warp 1.13.0+cu12 and compiled BAE CUDA extensions on the same RTX 4080 SUPER / PyTorch 2.7.0+cu128 environment:

  • New sparse tests: 4 passed with this PR; the same four fail on unmodified PyPose 9345958.
  • Full PyPose suite with BAE available: 254 passed, 1 failed, no skips. The existing sparse chain-PGO translation assertion fails identically on unmodified main (maximum absolute error 0.000990183836037493, tolerance 0.0002). This pre-existing failure is reported rather than changing its threshold.
  • BAE companion regression and normal-operator tests: 37 passed, with either this PR or unmodified PyPose main. See the companion PR for its broader-suite limitations.

Cost and limits

Exact rollback temporarily stores one copy of the trainable parameter values per parameter group: O(P) additional memory, not another Jacobian/Hessian copy. The copies are local to step() and do not enter the optimizer state dict.

The initial CUDA suite above skipped the optional BAE tests. The follow-up now exercises that backend; the existing PGO precision failure is documented above. Model buffers and other side effects of forward() are not rolled back; this fix restores optimizer-owned trainable parameters only. It assumes the starting loss/Jacobian are valid and does not turn an initially invalid model into a valid one.

@cola1917 cola1917 mentioned this pull request Oct 26, 2026

Copy link
Copy Markdown
Contributor

@cola1917, please be cautious about modifying the LM logic here. This logic is shared across multiple libraries, including the LM implementation in pypose/bae, and we want to avoid behavioral inconsistencies between them.

Please compare the trial acceptance/rejection and parameter rollback changes with the corresponding implementations and coordinate any necessary updates so the libraries remain consistent.

@cola1917

Copy link
Copy Markdown
Contributor Author

Thanks for flagging this. I compared both BAE overrides at afad40e: LM.step() and Schur.step() do not inherit this fix. I opened the coordinated patch at pypose/bae#50, covering both explicit and matrix-free paths.

Across the implementations, finite acceptance/retry-limit behavior and strategy-update ordering remain unchanged. Non-finite trials are always rejected, including at the retry limit, and restore the exact pre-trial parameter values and loss rather than applying -D. Retry attempts remain bounded by reject + 1.

I also pushed f84d79e, adding real BAE sparse-backend regression coverage here. The four new cases pass with this PR and fail on unmodified main. The BAE companion has 32 passing regression cases plus 5 normal-operator tests, and works with unmodified PyPose main as well as this PR.

With BAE installed, the full PyPose suite is 254 passed / 1 failed: the existing sparse chain-PGO translation assertion fails identically on unmodified main (max error 0.00099018 vs tolerance 0.0002). I recorded that and the BAE broader-suite limitations in the PR descriptions; I have not changed existing assertions or skips.

antonsoo

This comment was marked as low quality.

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.

LM accepts non-finite trial losses and leaves parameters outside the model domain

3 participants