Skip to content

searches: standalone overhead demo for MultiStartGradient NaN accounting - #127

Merged
Jammy2211 merged 2 commits into
mainfrom
feature/multistart-nan-step-diagnostics
Aug 15, 2026
Merged

Jammy2211 merged 2 commits into
mainfrom
feature/multistart-nan-step-diagnostics

Conversation

@Jammy2211

Copy link
Copy Markdown
Contributor

Summary

A standalone demonstration that the per-step value-NaN / gradient-NaN accounting added in PyAutoLabs/PyAutoFit#1473 does not cost run time, measured against a real lens likelihood rather than a toy objective.

Result (local CPU, mge, 16 starts, ndim 15, 1.03 s/step):

variant cost % of a step
fused (shipped) 4.1 µs 0.00039%
host (pull whole gradient) 7.0 µs 0.00069%
eager (reduce outside the jit) 29.4 µs 0.00286%

The counter-intuitive ranking is the point: eager looks like the obvious optimization over host — it shrinks the transfer from (n_starts, ndim) floats to (n_starts,) bools — and is in fact the worst, because an un-jitted reduction buys a kernel dispatch plus a host round-trip to save a few KB that were never the cost. That inversion is what selected the shipped design.

Design note: why this is not a before/after diff

The feature ships unconditionally, so there is no flag to A/B inside one process and a standalone script cannot hold two library versions. The script measures the two halves separately and divides: the accounting op on a cached gradient at microsecond resolution as the numerator, the real jitted vmapped value_and_grad step as the denominator.

The first version instead differenced two end-to-end loops, which put a ~4 µs effect and a ~1 s step on the same stopwatch. Run-to-run jitter (~10–35 ms) was ~1000× the signal, so variants came out negative — faster than a baseline doing strictly less work — and the verdict line would have called a 9 ms regression "below the noise floor". That loop is retained as a labelled sanity check which now reports its own resolution and states that it bounds rather than measures.

A duplicate-baseline control run is included deliberately. Without it the script could only report "we measured no impact", which is unfalsifiable; with it, it reports an overhead against a measured noise floor — a claim that can fail.

Caveats (stated in the script and the README)

  • fused is measured on a proxy objective at the real array shapes, because differencing the two real jitted calls hits the same resolution wall. Its cost is a property of the shapes, not the likelihood. This is an upper bound on the reduction in isolation; XLA's fusion into the real backward pass is not captured.
  • Absolute values move ~50% between CPU runs; the ordering has been stable across three independent measurements. Read the ranking as the result, the magnitudes as order-of-magnitude.
  • CPU cannot see the host variant's true cost — a device→host copy is a same-address-space memcpy there. A GPU row is still owed, and is the reason to run this on the laptop GPU and then the A100 (whose faster likelihood shrinks the denominator and so inflates the overhead fraction — the conservative case).

Test Plan

  • ruff check + ruff format --check clean (the PR gate)
  • Honours AUTOLENS_PROFILING_SMOKE=1 (imports + setup, exit 0) and added to the smoke list in lint.yml
  • Full run completed on local CPU; JSON + PNG committed under results/searches/multi_start_nan_accounting/
  • GPU row (laptop GPU, then A100 with jax_enable_x64 set explicitly — it is not inherited under sbatch, and float32 would halve the gradient array, understating the exact quantity under test)

Related but distinct: scripts/misc/hazards/checks/nonfinite_gradient.py detects non-finite gradients on a likelihood surface; this measures what it costs to count them during a fit.

Generated by the PyAutoLabs agent workflow.

Jammy2211 and others added 2 commits August 14, 2026 18:42
PyAutoFit#1472 adds per-step value-NaN / gradient-NaN lane accounting to
MultiStartGradient. This measures whether that costs run time, against a real
lens likelihood rather than a toy objective.

The feature ships unconditionally, so there is no flag to A/B inside one
process and a standalone script cannot hold two library versions. Instead the
two halves are measured separately and divided: a real jitted vmapped
value_and_grad step as the denominator, each accounting variant as the
numerator.

Includes a duplicate-baseline CONTROL run. Without it the script could only
report "we measured no impact", which is unfalsifiable; with it, it reports an
overhead against a measured noise floor -- a claim that can fail.

Records the counter-intuitive result the design turned on: reducing gradient
finiteness on device but OUTSIDE the jit is the WORST option, not the best,
because an un-jitted reduction costs a kernel dispatch plus a host round-trip
to save a few KB that were never the bottleneck.

Refs PyAutoLabs/PyAutoFit#1472

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The first version differenced two end-to-end loops. On a real lens likelihood
that puts a ~4us effect and a ~1s step on the same stopwatch: the run-to-run
jitter (~10-35ms) is ~1000x the signal, so variants came out NEGATIVE -- faster
than a baseline doing strictly less work -- and the verdict line would have
called a 9ms regression "below the noise floor".

Measure the numerator directly instead: the accounting op on a cached gradient
at microsecond resolution, with Python call overhead measured by a _noop control
and subtracted, divided by the separately-measured real step cost. The
end-to-end loop stays as a labelled sanity check that now reports its own
resolution and states that it bounds rather than measures.

`fused` is measured on a proxy objective at the real array shapes, because
differencing the two real jitted calls hits the same resolution wall. Its cost
is a property of the shapes, not the likelihood. Documented as an upper bound
on the reduction in isolation.

Local CPU (mge, 16 starts, 1.03s/step): fused 4.1us (0.00039% of a step),
host 7.0us, eager 29.4us. Ordering stable across three independent runs;
magnitudes move ~50% between CPU runs.

Also adds per-rep progress output -- the run is many minutes on CPU and a silent
process is indistinguishable from a hung one.

Refs PyAutoLabs/PyAutoFit#1472

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@Jammy2211 Jammy2211 added the pending-release PR queued for the next release build label Aug 14, 2026
@Jammy2211
Jammy2211 merged commit a34d619 into main Aug 15, 2026
1 check passed
@Jammy2211
Jammy2211 deleted the feature/multistart-nan-step-diagnostics branch August 15, 2026 00:39
@Jammy2211 Jammy2211 removed the pending-release PR queued for the next release build label Oct 7, 2026
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