Skip to content

perf: faster evaluation of faulted models (spec 003) - #391

Merged
lachlangrose merged 137 commits into
masterfrom
feat/003-fault-evaluation-performance
Nov 27, 2026
Merged

lachlangrose merged 137 commits into
masterfrom
feat/003-fault-evaluation-performance

Conversation

@lachlangrose

@lachlangrose lachlangrose commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Summary

Spec 003 (specs/003-fault-evaluation-performance/): faster evaluation of faulted models, with golden tests that freeze the results and benchmarks that measure the cost. All eight success criteria pass in the final acceptance run (T030), against a Linux baseline of current master.

What changes in the library

  • One restoration per call (LoopStructural/modelling/features/_evaluation.py): inside one evaluation call (evaluate_value, evaluate_gradient, evaluate_model, evaluate_model_gradient, …) a short-lived scope shares the restored points of equal fault sets, so each fault is applied once per batch instead of once per group and per unconformity region. Nothing is kept between calls (C-012). The caller's batches are kept for the call; fault-frame step batches and the fault-side probes of a gradient keep one batch per fault set and size (FR-010, C-018). Points of dtype object and array displacements fall back to direct restoration.
  • Shared grid lookup in a fault step (fault/_fault_segment.py, loop_common structured grid): when the three fault-frame coordinates share one grid, one cell search and one set of trilinear weights serve all three values and the gradient. The fast path keeps the regions (with block and already-restored faults), the stale-frame check, and the NaN rules of fix(loop-interpolation): report failed solves, keep the chosen solver, NaN for NaN points #413; a subclass with its own evaluation keeps the member calls.
  • Smaller fixes: the faulted gradient keeps its N×3 shape during refinement (D0); the unused use_threads switch is removed.
  • Public API: unchanged (FR-011; signature test).

Results (T030, final run, 2026-11-27)

Candidate e88269b1 against master db5e65ac plus the same harness (bench/003-linux-baseline-db5e65ac), Linux, i7-12700H, standard suite twice each (measurements.md, acceptance.json):

SC Case Before After Value Limit
002 oldest group, 4 faults + 3 unconformities, 5000 pts 1.42 s, 16 fault calls 0.14 s, 4 0.098 0.40
003 all groups / one evaluate_model 4.61 s; 52 calls 0.55 s; 4 0.120 0.15
004 3 unconformities vs 0 1.06 1.5
005 plugin model, 20 000 pts 3.01 s 0.65 s 0.217 0.50
006 peak memory (tracemalloc) worst 1.215, within +16 MiB 1.2× or +16 MiB
007 run-to-run noise 1.071 1.10
008 standard suite time 329 s 900 s
009 one fault application per point 0.40 s 0.14 s 0.347 0.50

On a 94-fault test model (40 cutting faults, chains up to 20 deep) evaluate_model takes 1.8–2.1 s at 500–5000 points (94 fault calls) against 7.4–9.4 s on master with the same fixes (376 calls).

Tests

Spec decisions made during this work

C-018 (batch rule of FR-008/FR-010), C-019 (Linux baseline on the same computer), C-020 (tracemalloc memory meter instead of process RSS, whose value depended on case order), C-021 (golden references after master #421/#425/#427), C-022 (median of 3 rounds for 1–5 s timing cases). The master fixes found on the way are merged separately (#424, #425, #427, #428, #429).

Still open

  • T031: user documentation of the per-call scope and the benchmark workflow.
  • T032: the final checks record (validation.md); the checks themselves pass (above).

Note for reviewers

Most of the line count is test data (frozen fields, golden references, benchmark result files). The branch history once held local dev-toolkit files, removed again before this PR's diff; a squash merge keeps them out of master's history.

🤖 Generated with Claude Code

https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx

lachlangrose and others added 30 commits November 1, 2026 16:15
The spec asks for golden tests and benchmarks first, then for an evaluation
that applies each fault set once in one evaluation call. research.md has
the analysis of the present code and the baseline numbers, and
measure_baseline.py makes the synthetic baseline again.
- The tolerances stay as they are.
- The real-data golden model is the plugin model, after it is refined.
- Restored points can be kept between calls, with invalidation on each
  model change, a size limit and thread safety (FR-009, FR-010, FR-010a,
  SC-010).
- The benchmarks use pytest-benchmark and run only on a developer's
  computer, not in CI.
data/plugin_model/ has the LoopStructural calls that the QGIS plugin made
(recorded from the plugin), the data of each feature, the solved field of
each interpolator, and values of the model in QGIS at 5000 points.
plugin_model.py builds the model without QGIS from these files, with the
stored fields (no solve), and gives the same values as in QGIS (difference
0). The spec and research.md record the answer to C-002, a new FR-001a
(stored fields, so that the golden results do not depend on the solver),
and what a golden model with stored fields must do.
Update dev-tools-setup files and add the spec and code-reviewer skills
and agents from the dev-tools checkout.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…tests

Add the opt-in benchmark runner, case generation and instrumentation.
Add pytest-benchmark to the dev extra only. Add the failing tests for
compare_results.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Add compare_results and the compare command for schema v1 results.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Warn when per-case provenance fields differ between two results.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…k runner

Save each finished case to a checkpoint file. Add --work-dir and --resume.
Refuse a resume when the revision or dirty state differs. Record
suite_wall_seconds in the result.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Exclude the work directory and the output file from the dirty flag.
Refuse a resume when the work directory has data but no meta.json.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ormance

Add a standard suite of about 1 hour and keep the full suite on request.
Add tasks T035 to T039 and move the baseline task after them.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
lachlangrose and others added 6 commits November 26, 2026 14:39
…changed (C-021)

Master #421 changes fault gradients near faults, fault_pitch and fold
rotation angles. 60 outputs of 17 golden models change (mostly
gradients; cutting_faults also some values, restored points and one
model id). The new references come from master a942177 with the fix of
#424, captured at the stored reference points. Evidence:

- the spec 003 branch gives the same bits as that code for all 970
  arrays of the 21 golden models;
- in each updated model, every array that #421 does not change is
  bit-identical to the old reference.

The manifest records the code, the reason and the changed keys under
provenance.reference_updates, with the new sha256 and summaries.

The gradient batch tests replay the refinement of #421: only the
tetrahedra that cross a fault are made smaller, at most 10 times, and a
point whose tetrahedron still crosses a fault is NaN. cutting_faults
group_0 now has 29 distinct (fault, batch) calls (25 before #421; the
refinement probes the crossing subset at each step) and still no
duplicate. A gradient has one scope slot for each probe size.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx
…baseline

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx
use_threads was a module constant fixed to False (a known threading bug,
2024-04-19), so the ThreadPoolExecutor branch of _evaluate_frame_legacy
never ran. If it had been turned on, the worker threads would not see
the evaluation scope (a new thread does not get the context variables of
the caller), so the restorations would stop being shared. Remove the
branch, the import, the constant and the test that only checked that the
constant stays False. The spec edge case "Threads" records the removal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx
The acceptance check wanted a gradient_decision field with the OP1 rule
in the candidate result, but no runner wrote it, so the check failed on
every real run. Spec C-018 now records the rule, and the FR-008 count
tests check the behaviour, so a copied field proves nothing. Remove the
row, the constant and its tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx
The candidate 6c2bbb0 against the Linux baseline (master a942177 with
#424, branch bench/003-linux-baseline at 7b4d910), standard suite twice
each, tracemalloc memory meter. acceptance.json passes: SC-002 0.098,
SC-003 0.122, SC-004 1.09, SC-005 0.216, SC-006 worst 1.215 within the
16 MiB allowance, SC-007 1.06, SC-008 331 s, SC-009 0.365. The
10-million-point check peaks at 7.10 GB with one restored batch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx
…me gradient

Since #421 a gradient whose tetrahedron still crosses a fault after the
refinement is NaN. A fault whose frame is cut by younger faults takes the
step direction of apply_to_points from that gradient, so a point on a
cutting fault was moved to NaN. A builder that restores a regular grid
through such a fault then failed with "Cannot resize the grid" (seen with
a chain of cuts: D cut by C, C by B, B by A; 215 of 10890 grid points of D
were NaN).

Where the frame of a cut fault has a NaN gradient at a finite point, use
the gradient of the frame without the cutting faults there; a step only
needs a direction. A frame without cutting faults keeps a NaN gradient,
so its points stay NaN as before (test_fault_frame_validation). A point
with a NaN coordinate is not moved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx
lachlangrose and others added 14 commits November 26, 2026 17:08
#425 (open against master) keeps a step direction where a cutting fault
leaves no frame gradient after #421, so a point on a cutting fault is no
longer moved to NaN. The conflict is in apply_to_points: the branch takes
the frame gradient from _evaluate_frame; the fallback of #425 follows it.
A cut frame always takes the legacy member path, where the NaN comes from.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx
…fter #425 (C-021)

#425 moves a point on a cutting fault with the frame direction instead of
to NaN. The C-021 capture (master a942177 with #424) had those NaN
points in cutting_faults: 1 restored point, 1 value and 2 gradient rows,
and one model id that they changed. The 5 changed outputs come from
master 315dff4 with #425; the spec branch gives the same bits for all
970 golden arrays. The manifest records the update.

On this branch a frame without faults takes the shared grid lookup, which
does not call evaluate_gradient, so the #425 test of the NaN rule turns
the lookup off to reach the member calls.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx
A fault created with faults=[A] is faulted by the younger fault A, so A
must be restored first. Features keep their faults newest first, and A
must exist before the fault it cuts, so A came after it in every list:
restore_through applied the cut fault first, its frame restored every
point through A again inside each step, and the result near the
intersection differed. In a chain of cutting faults the work multiplied
at each level (a 5-fault test model: 3460 fault calls and 14.2 s for 2000
points; restored cutters first: 5 calls and 0.034 s).

restore_through now orders the faults so that a cutting fault comes
before every fault of the list that it cuts (cutters_first); faults with
no such relation keep the order of the list, so models without faulted
faults do not change. Moving points forward (reverse) puts the cutting
fault last. The builder rotates data vectors in the same order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx
#427 (open against master) restores a cutting fault before the fault it
cuts, so a fault frame displaced by a younger fault no longer restores
every point through it again at each step. It is the same commit as on
the PR branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx
_cutters read the members by index, which calls __getitem__ of any
fault-like object in a list (a test double asserts its key). The members
are in StructuralFrame.features.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx
…feat/003-fault-evaluation-performance

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx
…fter #427 (C-021)

#427 restores a cutting fault before the fault it cuts. In cutting_faults
the frame of fault_1 is faulted by fault_0, so fault_0 is now restored
first and the frame of fault_1 makes no nested restoration. The 5
changed outputs of cutting_faults (group_0 value and gradient, the
surface at 50 and the unit boundary distances; the model ids do not
change) come from master 315dff4 with #425 and #427; the spec branch
gives the same bits for all 970 golden arrays.

The counts of cutting_faults follow: evaluate_model 2 fault calls (22),
group_0 gradient 9 (29), none nested; the value call keeps one batch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx
A fault applied through a list that names only its direct cutters (a
fault frame builder, or a feature with faults=[C]) restored through C
alone. The frame of C was built in the space restored through its own
cutter B, so C restored every point through B again at each step, and B
through A: the nesting gained one level for each link of a chain, and the
cost multiplied (build of a 5-fault chain: 272.7 s and 44850 fault calls).

restore_through now adds, transitively, the faults that cut a fault of
the list (with_cutters) and restores them in one pass, youngest first,
each told what is already restored. A fault frame cut by B is then also
moved by the younger faults that cut B, where their displacement reaches
it. The same 5-fault chain builds in 7.0 s (1923 fault calls); a 6-fault
chain in 15 s. The builder rotates data vectors through the same faults.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx
…to feat/003-fault-evaluation-performance

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx
The builder rotated each data vector (normal, gradient, tangent) by every
fault at its present-day position, with no restored_through. A fault frame
displaced by younger faults then restored the vector's tetrahedron through
its whole chain of cutting faults at each step, so building a fault deep in
a chain cost about (chain length)^2 x steps fault applications: the
deepest fault of a 5-deep chain made 1661 fault calls and took 9.6 s, and
deeper faults of a 94-fault model took up to 920 s each.

Each vector is now rotated by each fault at its position restored so far,
youngest fault first (the order restore_through uses), and each fault is
told which faults are already restored. The same build takes 1.0 s with
12-14 fault calls per fault. With one fault, or faults that do not move
each other's data, the rotation is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx
…nto feat/003-fault-evaluation-performance

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx
A feature depends on the builders of its faults, and a fault frame on the
builders of the faults that cut it. BaseBuilder.up_to_date recursed into
every fault builder, and each fault frame into its three coordinate
builders, with no memory of what was already checked, so one check walked
every path through that graph (x3 at each fault frame). It runs at the
start of every evaluation. In a network of cutting faults this dominated:
building one fault of a 94-fault model made 36 million up_to_date calls
(and as many logger.info calls) for 652 fault applications.

The outermost call now keeps the set of builders it has checked (in a
context variable), and a builder already in it returns at once. The
recursion still brings the faults up to date before the builder itself,
so a skipped builder is already up to date.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx
… once) into feat/003-fault-evaluation-performance

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx
…o feat/003-fault-evaluation-performance

The branch already had the same fixes as merges of their PR branches.
Two conflicts: _fault_segment.py keeps the branch's placement of the #425
fallback (after the frame gradient of _evaluate_frame), and
test_cut_fault_step_direction.py keeps the branch's version that turns
the shared frame lookup off for the NaN-rule test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx
lachlangrose and others added 2 commits November 27, 2026 10:28
The final T030 run on Linux passed every criterion except SC-007, twice,
on a different case each time (noise 1.158, then 1.121; limit 1.10). The
runner timed a case of 1 s or more only once, and one sample of a 1-5 s
call varies by about 10 % on the T030 computer. A timing case with a
median from 1 s up to 5 s now runs again with 3 rounds (MEDIUM_CASE_*),
after the short-case repeat. The limit of SC-007 does not change; spec
003 records the decision as C-022.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx
Candidate e88269b against the Linux baseline of master db5e65a (all the
fixes merged; branch bench/003-linux-baseline-db5e65ac at 23a37ee),
standard suite twice each, tracemalloc memory meter, 3 rounds for cases
from 1 s up to 5 s (C-022). acceptance.json passes: SC-002 0.098, SC-003
0.120, SC-004 1.06, SC-005 0.217, SC-006 worst 1.215 within the 16 MiB
allowance, SC-007 1.071, SC-008 329 s, SC-009 0.347. The 10-million-point
check peaks at 7.11 GB with one restored batch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx
@lachlangrose
lachlangrose marked this pull request as ready for review November 27, 2026 00:48
…evaluation-performance

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx
@lachlangrose
lachlangrose merged commit 6874364 into master Nov 27, 2026
56 checks passed
@lachlangrose
lachlangrose deleted the feat/003-fault-evaluation-performance branch November 27, 2026 01:26
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