Skip to content

research(fixed-light): phase 4 — curvature kernel A/B on Delaunay, then A-prime permute-active-last (numba CPU) #274

Closed

Description

@Jammy2211

Overview

Phase 3 (#267) took the production numba CPU fixed-light call from 413.3 to 230.0 ms, leaving two dominant sites: sparse_numba.curvature_matrix at 87.8 ms (38 %) and solver.fnnls_cholesky at 61.1 ms (27 %). This phase takes those two as levers 4a and 4b. Lever 4a is a measurement, not a rewrite: curvature_matrix_via_sparse_operator_from already dispatches between a two-stage kernel and a direct quadruple loop on a pixel-count cap that was calibrated on the HST rectangular fiducial and never measured on the Delaunay fixed-light cell. Lever 4b is A′ — permute the active columns last and take one potrf, measured at 41 ms against fnnls_cholesky's 61.1 ms during lever 3's design, at the cost of a 5e-13 perturbation to the passive factor, so it needs a witness definition before it gets a kernel.

Plan

  • Cap fix first. The cell's MAX_INSTRUMENTATION_OVERHEAD = 1.03 is a fixed ~6–8 ms cost read as a ratio; it killed RAL job 343355 at 224 ms and will kill anything shorter. Restate it as a millisecond budget before measuring anything.
  • Two waves, sequential PRs, never stacked (the research: fixed-light follow-up round 3 — the non-solver levers on numba CPU and the A100 together (phase 3) #267 lesson: stacked profiling PRs go red-by-construction once the library moves). Wave A ships and merges before wave B starts.
  • Prototype in autolens_profiling, promote conditionally. Kernels are prototyped behind the existing injection seam and A/B'd in one process as extra routes of the fixed_light_numba.py cell (the phase 2–3 pattern); a PyAutoArray change lands only when a kernel is proven, as its own small PR.
  • Wave A — overhead gate as an ms budget, a curvature-kernel injection seam with three kernels (two_stage control, direct, two_stage_touched), cell routes b_direct / b_touched, an s4 witness, a RAL CPU-only leg, the note; then a conditional PyAutoArray PR.
  • Wave B (after wave A merges) — the 4b witness definition first, then fnnls_cholesky_permuted behind lever 3's solver seam, then its own A/B and its own conditional PyAutoArray PR.
  • Verdict rule, written before the run: a kernel is a lever if it beats route b by ≥ 5 % on the whole call with the ABBA gate PASS. Below that the answer is "two-stage is right on Delaunay too" and the PyAutoArray work shrinks to the docstring fix.
  • The docstring fix at inversion_imaging_numba_util.py:781 ships regardless of the numbers.
Detailed implementation plan

Affected Repositories

  • autolens_profiling (primary) — the cell, the kernel seam, the witness, the submit, the tests, the note.
  • PyAutoArray (conditional) — the docstring fix always; a kernel/dispatch change only if a prototyped kernel beats b by ≥ 5 %.

Branch Survey

Repository Branch Dirty Claim
autolens_profiling main @ cea41bb clean claimed by hst-gpu-residue-p2 (#273, feature/hst-gpu-residue-p2)
PyAutoArray main @ 91240e43 clean unclaimed (delaunay-area-magnification-audit worktree exists on another branch, no active.md conflict)

worktree_check_conflict fixed-light-numba-s4 autolens_profiling PyAutoArray exits 1 on the p2 claim. Its branch touches fixed_light_trace.py, host_callback_probe.py, library_solver_injection.py, test_fixed_light_cell.py, test_fixed_light_vmap_submit.py, a vmap submit; this task touches none of those. Shared generated surfaces: README.md (regenerate after whichever merges second) and one hpc/README.md route row. Waived on the human's approval 2026-09-17, fresh parallel worktree off origin/main — the same call p2 itself recorded against #267.

Suggested branch: feature/fixed-light-numba-s4 (in both repos). Worktree: ~/Code/PyAutoLabs-wt/fixed-light-numba-s4/ (autolens_profiling first; PyAutoArray added via worktree_add_repo when step 8 starts).

Implementation Steps

Wave A — cap fix + lever 4a (autolens_profiling PR, then optional PyAutoArray PR)

  1. Cell: overhead gate as a millisecond budget (scripts/imaging/likelihood_breakdown/fixed_light_numba.py:409-424, :1888-1894, :2014-2020, JSON :1965, :2249). Replace the ratio gate with MAX_INSTRUMENTATION_OVERHEAD_MS = 12.0 (= 1.03 × 400 ms, the calibration the ratio encoded; measured fixed cost 6–8 ms) on overhead_ms = (ratio − 1) × mean clean call. Keep the ratio recorded, keep MIN_BLOCKS_FOR_OVERHEAD_ASSERT = 3, record both in gate_thresholds. Update test_fixed_light_numba.py::test_the_overhead_gate_is_repeat_conditional and add one test that a 1.037 ratio at 224 ms PASSES and a 1.037 at 400 ms FAILS.
  2. Curvature-kernel injection seam — new scripts/misc/likelihood_breakdown/fixed_light_numpy_kernels.py, mirroring fixed_light_numpy_solvers.numpy_solver_injected (:371): a context manager curvature_kernel_injected(kernel, *, label) that rebinds inversion_imaging_numba_util.curvature_matrix_via_sparse_operator_from (attribute access from sparse.py:385, so rebinding works), counts calls, restores by identity. Three kernels it can install:
    • two_stage — library dispatcher with two_stage_max_pix_pixels=10**9 (what production runs; control);
    • direct — dispatcher with two_stage_max_pix_pixels=0 (library's own direct kernel);
    • two_stage_touched — a numba copy of the two-stage kernel whose stage 2 scatters only over the indices stage 1 touched (tracked in a small index list) and zeroes only those. Same per-element summation order ⇒ expected bit-identical to two_stage (adding exact zeros is a no-op); asserted, not assumed.
  3. Cell routes — add b_direct and b_touched to ALL_ROUTE_KEYS (:223; excluded from DEFAULT_ROUTE_KEYS like d_np), both _ROUTES_LIKE_B, entered outside call_accounting.install exactly as d_np is (:1785-1789, :2027); JSON injected_kernel block per row (kernel dotted name, n_calls > 0 hard-asserted, mirroring injected_solver :2030-2047). One process, rows b, b_direct, b_touched under the ABBA harness = the A/B.
  4. Witness — new scripts/imaging/likelihood_breakdown/fixed_light_numba_s4_witness.py (CLI as the l3 witness: --mesh --dataset --threads --config-name, smoke exit, verdict PASS/FAIL, non-zero exit on FAIL): W1 each injected kernel fires once per figure_of_merit; W2 curvature_matrix direct vs two-stage max-rel ≤ 1e-9 and touched vs two-stage bit-identical; W3 log evidence ≤ 1e-9 relative across the three; W4 site medians of 20 for the three kernels on identical inputs (RECORDED).
  5. RAL submit — new hpc/batch_cpu/submit_breakdown_imaging_fixed_light_numba_s4_delaunay_ral_hst_fp64 (single library, one arm: --routes b,b_direct,b_touched --threads 1 --n-repeats 64 --nnls-warm-start on --instances iid, gpu partition CPUs-only, thread env pins and NUMBA cache dir as the l3 submit, then the witness) with a # WALL-BASIS: block (check_submits.py contract; the l3 submit has none). Name deliberately avoids the _numba_levers_ infix (that family's test asserts two library arms) and lives in batch_cpu (not globbed by test_fixed_light_cell.py). Static test scripts/misc/test/test_fixed_light_s4.py: kernels agree on a tiny fixture (reuse test_fixed_light_numba.py's tiny/tiny_s3_pair fixtures), touched bit-identical, seam restores by identity, submit bash -n + runs scripts that exist.
  6. Run + harvest — hpc/sync push-submit-style submission from this CLI session, poll, pull results/breakdown/imaging/fixed_light_numba_delaunay_hpc_ral_cpu_fp64_fixed_light_numba_s4_* JSON/PNG + witness JSON. Verdict rule, written before the run: a kernel is a lever if it beats b by ≥ 5 % on the whole call with the ABBA gate PASS; below that the answer is "two-stage is right on Delaunay too" and item 7 shrinks to the docstring.
  7. Note + dashboards — new results/notes/fixed_lens_light_s4_2026_09.md (Scope / Provenance / Gate roll-up / 4a headline / decomposition / verdict / Next, the levers-note shape), build_readme.py regenerated; hpc/README.md route row.
  8. PyAutoArray PR (wave A, conditional) — always: fix the stale docstring at inversion_imaging_numba_util.py:781 (names a CURVATURE_TWO_STAGE_COST_RATIO_THRESHOLD that does not exist; code branches on pix_pixels). If direct wins on Delaunay: replace the pixel-count cap with a geometry rule (mean mappings per data pixel u0 from pix_lengths, threshold set from the measurement), keeping two_stage_max_pix_pixels as the test override. If two_stage_touched wins: it becomes the two-stage body (bit-identical, so test__…two_stage_from__matches_reference_kernel :829 and the dispatch test :947 stand; add a touched-vs-untouched bit-identity test). Pending-release, library CI green, then the profiling note's "library state" line updated in the same wave A PR before merge.

Wave B — lever 4b, A′ (after wave A merges)

  1. Witness design first, as a note section + test, no kernel yet — define "same active set at 5e-13" on the cell's seeded draw set (--instances iid, the phase-2 graded draws): per draw, active-set Jaccard vs the library fnnls_cholesky (expect 1.0; report every draw < 1.0 with the KKT residual of both solutions), Δ log evidence in nats (gate ≤ 1e-9 relative like W3), PDIP fallback count unchanged, solver_stats iteration counts. Written into fixed_lens_light_s4_2026_09.md "4b witness" and reviewed by the human before step 10.
  2. Kernel — fnnls_cholesky_permuted in fixed_light_numpy_solvers.py next to nnls_factor_reuse: the warm-start factorisation at fnnls.py:127-137 (ZTZ[P_inorder][:, P_inorder], double fancy-index) and the per-iteration row gather :199-201 replaced by one symmetric permutation of ZTZ to passive-first order and one potrf on the leading block; outer loop :185-234 otherwise unchanged; stats/factor published in the same shape so lever 3's log_det_from_passive_cholesky_from still reads it. Injected as route d_perm through numpy_solver_injected (signature solver(ZTZ, ZTx, *, stats), :453-458).
  3. A/B + witness on RAL — same submit family, rows b vs d_perm, --n-repeats 64; witness script fixed_light_numba_s4b_witness.py implementing step 9's metrics. Verdict: lever if ≥ 5 % on the whole call AND the witness passes on every draw. Promote to PyAutoArray fnnls_cholesky only then, as its own PR.
  4. Close: note "Phase 4 — the whole campaign" table chaining from 413.3 ms; /prm.

Key Files

autolens_profiling (wave A): scripts/imaging/likelihood_breakdown/fixed_light_numba.py, new scripts/misc/likelihood_breakdown/fixed_light_numpy_kernels.py, new scripts/imaging/likelihood_breakdown/fixed_light_numba_s4_witness.py, new hpc/batch_cpu/submit_breakdown_imaging_fixed_light_numba_s4_delaunay_ral_hst_fp64, new scripts/misc/test/test_fixed_light_s4.py, scripts/misc/test/test_fixed_light_numba.py, new results/notes/fixed_lens_light_s4_2026_09.md, results JSON/PNG, README.md (generated), hpc/README.md.

PyAutoArray: autoarray/inversion/inversion/imaging_numba/inversion_imaging_numba_util.py (+ test_autoarray/.../test_inversion_imaging_util.py if a kernel is promoted).

Verification

  • Local (laptop, 1 thread): pytest scripts/misc/test/test_fixed_light_s4.py scripts/misc/test/test_fixed_light_numba.py; ruff check . && ruff format --check .; build_readme.py --check; check_submits.py --check; the cell with --routes b,b_direct,b_touched --n-repeats 6 and the witness run to completion locally before any RAL submit.
  • RAL: one job for 4a (cell + witness), ABBA gate PASS both ways, overhead_ms recorded; the note quotes control/feature ms, site ms, and the W2/W3 numbers.
  • Pins: log evidence ≤ 1e-9 relative on every row; touched kernel bit-identical to two-stage; log_likelihood not compared across processes.
  • Ship: /ship_workspace for the profiling PR (Heart RED ack), /ship_library for the PyAutoArray PR; /prm each in turn, never stacked.

Original Prompt

Click to expand starting prompt

Fixed-light numba CPU round 4 — the curvature-matrix kernel A/B on Delaunay, then permute-active-last

Type: research
Target: autolens_profiling
Repos:

  • autolens_profiling
  • PyAutoArray
    Themes:
  • profiling
  • pixelization
  • cpu
  • numba
  • inversion
    Difficulty: large
    Sizing-note: the intake sizing faculty derived too-large (score 12); large is kept — the two levers are
    ordered and 4a is a measurement of two kernels that already exist; if it must be split, split between
    lever 4a and lever 4b, never between a lever and its witness.
    Autonomy: human-required
    Priority: high
    Status: formalised
    Epic: fixed-lens-light-numba-cpu
    Phase: 4
    Consequence: judge
    Witness: On HST Delaunay N=1500 numba CPU (RAL gpu partition CPUs-only, 1 thread), each lever carries a
    before/after whole-call table with thread and backend recorded per leg; lever 4a returns the library's log
    evidence to <= 1e-9 relative; lever 4b keeps the active set on every seeded draw within the witness's declared
    tolerance with the PDIP fallback count unchanged; the note chains the cumulative from 413.3 ms.
    Review-minutes: 30
    Unattended: never
    Filed: 2026-09-16

Original request (verbatim)

and you think theres nowhere else tp gp wotj numba? without digging through deeper stuff is level 4 not low hanging
[...] i think 1 and 2 are worth doing

Context

Phase 3 (#267, complete/2026/09/fixed-light-numba-levers.md) took the production numba CPU
fixed-light call from 413.3 to 230.0 ms. Its residue, from the lever-3 decomposition
(results/notes/fixed_lens_light_levers_2026_09.md, "Next"): sparse_numba.curvature_matrix
87.8 ms (38 %), solver.fnnls_cholesky 61.1 ms (27 %), delaunay.triangulation 18.9 ms (8 %),
everything else under 9 ms a site. This phase inserts ahead of the renumbered memo / scaling /
verdict phases of fixed-lens-light-numba-cpu, which become 5, 6 and 7. Two levers, measurement
first, in order.

Lever 4a — A/B the two existing curvature kernels on Delaunay (low-hanging)

curvature_matrix_via_sparse_operator_from (PyAutoArray
inversion/inversion/imaging_numba/inversion_imaging_numba_util.py) dispatches on a pixel-count cap
(two_stage_max_pix_pixels = CURVATURE_TWO_STAGE_MAX_PIX_PIXELS) between the two-stage kernel and
the direct quadruple loop. The two-stage docstring itself says it wins for bilinear rectangular
(u0 = 4, 2.9x at the HST rectangular fiducial) and can lose for barycentric Delaunay (u0 ~ 1.55),
because stage 2 does a dense pix_pixels-long AXPY per mapping of every data pixel and then
re-zeroes the full accumulator, regardless of how few indices stage 1 touched. The choice was
never measured on the Delaunay fixed-light cell.

  1. A/B the two existing kernels on the HST Delaunay N=1500 fixed-light cell (route b, memo ON):
    RAL gpu partition CPUs-only, 1 thread across numba and BLAS, the research: fixed-light follow-up round 3 — the non-solver levers on numba CPU and the A100 together (phase 3) #267 two-arm harness with a
    private merge-base PyAutoArray on PYTHONPATH as control, --n-repeats 64. The direct kernel is
    already reachable by passing two_stage_max_pix_pixels=0; no library change is needed for the
    measurement.
  2. If neither pure form wins, a touched-index variant of stage 2: record the indices stage 1
    touched, scatter only over them, zero only them. Same inputs, same outputs, same
    halved-diagonal / A + A.T contract.
  3. Fix the stale dispatcher docstring regardless: it names
    CURVATURE_TWO_STAGE_COST_RATIO_THRESHOLD, the code branches on pix_pixels.

Pins: the kernels agree to floating-point reassociation (~4e-13 measured), log evidence <= 1e-9
relative, log_likelihood not comparable across arms.

Lever 4b — A-prime: permute the active columns last, one potrf

Lever 3's design measured the permuted factorisation at 41 ms against fnnls_cholesky's 61.1 ms,
reading ~15-20 ms more by folding both fancy-index copies and the second pass away. The cost: the
passive factor is perturbed at 5e-13, which reaches the active set's knife-edge inclusion
decisions, so the solve is no longer byte-identical and lever 3's cleanest property is given up.

  1. Witness design FIRST: what "the same active set" means at 5e-13 — active-set Jaccard across the
    seeded graded draw set, evidence delta in nats, PDIP fallback count unchanged. Written down and
    agreed before a kernel exists.
  2. The kernel, on the numpy/numba path only (fnnls_cholesky), behind the same seam lever 3 used.
  3. The A/B on the same cell and host, control = lever 4a's merged state.

Cell prerequisite (before either lever)

The harness's max_instrumentation_overhead_ratio = 1.03 was calibrated on a ~400 ms call; its
fixed cost does not shrink with the call (1.0147 at 413 ms, 1.0366 at 224 ms) and it killed RAL
job 343355. Fix it in the cell before measuring: scale the cap with call length, or require a
minimum block count, or state it as a per-call millisecond budget (note "Next" item 6).

Out of scope

fnnls algorithm changes beyond A-prime (phase 2 closed the solver round); the edge-zeroed
generalisation and the covariance third factorisation (zero gain on the production cell); the
delaunay.triangulation / find_simplex site (a separate candidate); the JAX path (both log-det
levers proven CPU-only, the A100 has no fnnls factor to read); route a, Euclid, rectangular meshes,
the source-pixel sweep, the sparse-operator/profile-subtracted-image bug.

Witness

On HST Delaunay N=1500 numba CPU, each lever carries a before/after whole-call table with thread
and backend settings recorded per leg; 4a returns the library's log evidence to <= 1e-9 relative;
4b keeps the active set on every seeded draw within the witness's declared tolerance and the PDIP
fallback count unchanged; the note carries both tables and the cumulative chain from 413.3 ms.

Activity

  1. Jammy2211 commented on Sep 17, 2026

    @Jammy2211
    ContributorAuthor

    Wave A — laptop verification green, branch pushed, RAL job 343394 submitted (2026-09-17)

    Branch feature/fixed-light-numba-s4 pushed at c7b72f6 (six commits on origin/main: e0ca612 ms-budget overhead gate, f88b9e0 curvature-kernel seam, 8998ec9 routes b_direct/b_touched, 960db5d s4 witness, 635a83d batch_cpu s4 submit + WALL-BASIS, c7b72f6 route-table alignment).

    Verification block, laptop leg (1 thread):

    • pytest test_fixed_light_s4.py test_fixed_light_numba.py — 42 passed.
    • ruff check clean, ruff format --check 187 files already formatted.
    • build_readme.py --check clean (86 versioned artifacts); check_submits.py --check 3 contracts, 0 failing.
    • Cell --routes b,b_direct,b_touched --n-repeats 6 ran end to end (exit 0, timing_status: wiring_only_not_measured — smoke config, numbers not quotable). injected_kernel.n_calls 21 / 19 on the two injected rows = host-dependent steady-state warm-up (6–12 calls, discarded) + 4 × 3 ABBA blocks; the timed counts are identical (12 each). Overhead gate PASS on all three rows under the 12 ms budget; seed_source: memo everywhere.
    • Witness (full, not smoke): PASS. W1 one kernel call per figure_of_merit for all three; W2 direct vs two-stage max rel 4.05e-13 (gate 1e-9), touched vs two-stage bit-identical; W3 evidence rel 1.11e-15 (direct), 0.0 (touched); W4 laptop site medians two_stage 101.5 ms, direct 114.9 ms (0.88×), touched 126.4 ms (0.80×) — RECORDED only, the verdict is the RAL whole-call row.

    RAL: worktree /mnt/ral/jnightin/autolens_profiling_wt/fixed-light-numba-s4 @ c7b72f6 (lensed_source.fits pre-seeded from the levers worktree, per the #273 array-guard finding); sbatch from hpc/batch_cpu/ → job 343394, gpu partition CPUs-only, both nodes idle at submit. WALL-BASIS estimate ~2 h. Harvest, the s4 note, /ship_workspace and the PyAutoArray docstring PR follow in this session; the verdict rule (≥ 5 % whole-call over b, ABBA PASS) is unchanged from the plan.

  2. Jammy2211 commented on Sep 17, 2026

    @Jammy2211
    ContributorAuthor

    Wave A shipped to PR-open — lever 4a verdict: no lever (2026-09-17)

    PRs (independent, either merge order; neither depends on the other's code):

    Verdict, RAL job 343394 (euclid-ral-gpu-1, gpu CPUs-only, one thread, --n-repeats 64, one library one process; rule: lever iff ≥ 5 % on the whole call with ABBA PASS):

    row whole call vs b ABBA
    b two-stage (production) 230.149 ms — −1.13 ms PASS
    b_direct 244.924 ms +6.42 % slower −1.62 ms PASS
    b_touched 236.913 ms +2.94 % slower +0.02 ms PASS

    Witness PASS: W2 direct rel 4.100e-13, touched bit-identical; W3 rel 7.385e-16; W4 site medians 85.043 / 102.052 (0.83×) / 97.019 ms (0.88×). Two-stage is right on Delaunay too (u0 = 1.604); the two-stage docstring's Delaunay prediction is refuted at this cell and #557 corrects it. Control b reproduces lever 3's 230.031 ms at +0.05 %; the campaign chain 413.301 → 230.031 ms is unchanged. Full record: results/notes/fixed_lens_light_s4_2026_09.md (Scope / Provenance / Gate roll-up / headline / decomposition / verdict / campaign / Next / Artifacts).

    Findings recorded in the note's Next: the submit's WALL-BASIS (7200 s) overestimated the one-arm job by ~44× (measured 162 s) — re-pin in a follow-up, not in this PR; the laptop and RAL W4 agree on the winner only, the ranking of the two losers is host-dependent; the touched variant loses because index bookkeeping costs more than the L1-resident dense zero-and-AXPY it saves.

    Heart gate: RED with exactly install verification FAILED (testpypi; checks F) and release validation FAILED (stage integrate) (released Colab bootstrap, unrelated); shipped to PR-open on the human's instruction against the active.md note naming these two; two transient "N commit(s) behind origin" reasons were local staleness, cleared by fast-forwarding the canonical PyAutoFit/PyAutoArray mains. Merge stays with /prm.

    Next: human /prm on #275 and #557; then wave B (lever 4b, A′ — witness definition first, plan steps 9–12) as a new sequential PR after #275 merges. RAL worktree /mnt/ral/jnightin/autolens_profiling_wt/fixed-light-numba-s4 (job logs in hpc/batch_cpu/output|error/*.343394.*) is left for cleanup at close-out.

  3. added a commit that references this issue on Sep 17, 2026
  4. Jammy2211 commented on Sep 17, 2026

    @Jammy2211
    ContributorAuthor

    Shipped

    Wave A of phase 4 (fixed-lens-light-numba-cpu) is merged:

    Lever 4a verdict: no lever. b 230.149 ms, b_direct 244.924 (+6.42 %), b_touched 236.913 (+2.94 %), ABBA PASS on every row; witness PASS (touched bit-identical, direct 4.1e-13; evidence 7.4e-16). Two-stage is right on Delaunay too; the campaign chain 413.301 → 230.031 ms is unchanged.

    Wave B (lever 4b, A′) was not started under this issue. It is re-filed as PyAutoMind/draft/research/autolens_profiling/fixed_light_numba_s4b_permute_active_last.md (issue plan steps 9–12, witness definition first) and will get its own issue. Closing this one as the wave A record; completion record PyAutoMind/complete/2026/09/fixed-light-numba-s4.md.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions