Skip to content

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

Draft
lachlangrose wants to merge 97 commits into
masterfrom
feat/003-fault-evaluation-performance
Draft

lachlangrose wants to merge 97 commits into
masterfrom
feat/003-fault-evaluation-performance

Conversation

@lachlangrose

Copy link
Copy Markdown
Member

Summary

This PR makes the evaluation of faulted models faster (spec 003, specs/003-fault-evaluation-performance/). It does not change the results: golden tests compare all outputs with the frozen legacy outputs.

Draft: the final acceptance run (T030) is blocked by two spec decisions (OP3 and OP4, see below). The user guide (T031) and the final project checks (T032) are not done yet.

Changes

  • Shared fault restorations (LoopStructural/modelling/features/_evaluation.py, _evaluation_cache.py). In one call, each fault is applied once, not once for each feature. A bounded cache (model.evaluation_cache: enabled, max_bytes, clear()) keeps restorations between calls. The key is the ordered fault set plus a model revision, so groups with the same faults share one entry. Any change of the model state gives a new key; a state that cannot be read safely bypasses the cache.
  • Faster grid lookup (packages/loop_common/.../_3d_structured_grid.py, fault/_fault_segment.py). One fault step finds each grid cell once and uses it for all frame components.
  • Gradient output fix (D0): during refinement, the faulted gradient keeps its N-by-3 shape.
  • Golden tests (tests/fault_evaluation/): deterministic models, frozen legacy outputs (data/manifest.json) and a 1 % perturbation check.
  • Benchmark tools (benchmarks/fault_evaluation/): a runner with a standard suite and a full suite, comparison, acceptance report and call counters. pytest-benchmark is added to the dev extra.
  • Spec documents (specs/003-fault-evaluation-performance/): plan, tasks, run state, the baseline runs (baseline.json, baseline_repeat.json) and the profile reports. The JSON baselines and manifest.json are most of the line count of this PR.

Measured results

plugin model (6 faults), value operations, median of one call (local run, revision b59cf1e; one process, 5 rounds; not the official acceptance run):

Points Before (cold / warm) After (cold / warm)
5,000 0.92 s / 0.91 s 0.28-0.30 s / 0.009-0.014 s
20,000 4.34-4.41 s / 4.34-4.37 s 1.28-1.43 s / 0.021-0.024 s

Fault calls for a cold call fell from 12 to 6. The D4a profile gates all pass (profile-d4a.md).

Open points

  • OP3 (SC-008): the target is 15 minutes for the standard suite. The first acceptance run took about 50 minutes. Most of that time is fixed work (counter pass, memory pass, process starts), not evaluation. This needs a spec decision.
  • OP4 (SC-006): peak memory must be at most 120 % of the baseline. Some cases have a very small baseline, so a few MB give a large ratio. This needs a spec decision.
  • SC-010 for onlap_domain: the warm / cold ratio is 0.254 (limit 0.10). T030 decides it.

Tests

  • pytest tests/fault_evaluation: 561 passed (golden tests 19 s, limit 120 s).
  • Ruff and Black are clean on the changed files. The full project suite and the package suites run in T032 and are not run for this draft.

Note for reviewers

The spec branch also holds the local dev-toolkit files (.claude/, .agents/skills/, .dev-tools/). The last commit of this branch removes them, so the PR diff does not include them. They are still in the commit history, so a squash merge is recommended.

🤖 Generated with Claude Code

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 25 commits November 4, 2026 10:31
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…est (T045)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The spec branch keeps them for the local spec workflow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… and SC-008

Remove the restoration cache between evaluation calls and the lock for each
model (C-012). Add a 16 MiB limit for small memory baselines to SC-006
(C-013). Make the standard suite shorter to keep the 15-minute limit of
SC-008 (C-014). Put back the standard-suite text that the cascade of spec
005 removed by mistake.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copy the 003 spec with C-012 to C-014 to the main checkout. Spec 003
keeps no restored points between calls, so spec 005 has no 003 cache to
invalidate. Update the 003 state in the index.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvJdxgnyc4tvLrYVXSk
Add the tasks for the replan of 2026-11-18: merge master and move the value
lookup (T046), remove the cache between calls and the model lock (T047,
T048), make the standard suite shorter and apply the new acceptance rules
(T049, T050), and measure the gates again (T051). T030 has no block now and
needs T051. Change the plan so that the T048 check does not read the files
that T049 and T050 change, and so that the orchestrator makes the merge
commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvJdxgnyc4tvLrYVXSk
Merge master with a merge commit, not a rebase, because the result files
name the commits of this branch. Resolve the conflict in the structured grid:
the value lookup moves to StructuredGrid3DGeometry, and StructuredGrid keeps
the gradient fast path. Keep the newer 003 spec text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvJdxgnyc4tvLrYVXSk
… suite (T047, T049)

Delete the tests of the cache between calls. Add tests that two calls on one
model run at the same time and that no restored points stay after a call.
Change the benchmark and acceptance tests for the cold-only standard suite,
the pass point sets, cold_view and the SC-006 allowance. The tests fail until
T048 and T050.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvJdxgnyc4tvLrYVXSk
Add a test for the SC-006 row of plan D10: the worst ratio, the failing cases
with both values in the reason, and the number of compared cases. Record the
new row key compared_cases in the plan and in the context of T050.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvJdxgnyc4tvLrYVXSk
…del lock (T048)

Delete the persistent restoration cache, the revision hooks and the lock for
each model (spec C-012). The scope of one evaluation call stays: it shares
restored points inside the call and keeps nothing after it. Two calls on one
model can now run at the same time. The builder and interpolator files are
the same as on master again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvJdxgnyc4tvLrYVXSk
The test used a baseline of 100,000,000 bytes. There, one byte above the
allowance is only 1.168 times the baseline and passes by the ratio. Use
50,000,000 bytes, where the allowance alone decides. Record the answer in
the plan.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvJdxgnyc4tvLrYVXSk
…cceptance rules (T050)

Use only the cold state, the 16 large cases of C-016 and a set of points for
each pass (PASS_POINTS). Add cold_view to read the old baseline file. SC-006
passes at 1.20 times the baseline or at most 16 MiB more. Remove SC-010 and
the cache code of the runner and of the memory worker. Compare shows "not
measured" for a value that a pass did not measure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvJdxgnyc4tvLrYVXSk
The baseline file has 5 gradient cases at 100,000 points that the standard
suite does not run (C-016). Add run.standard_view, and use it on the base in
the acceptance and compare tools when the candidate is a standard run. A
missing case still fails for the full suite. Record the rule in the plan.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvJdxgnyc4tvLrYVXSk
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvJdxgnyc4tvLrYVXSk
… call in 003

T051 measured 16 fault applications for the evaluation of all groups one by
one, because C-012 removed the reuse between calls. The user chose C-017: the
SC-003 count and the SC-004 ratio use one evaluate_model call; the SC-003
time limit stays on all groups one by one. Add the tasks T052 and T053, and
change the G1 gate of T051.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvJdxgnyc4tvLrYVXSk
…call (T052)

The SC-003 count and the SC-004 ratio read the model_ids cases (spec C-017).
The SC-003 time ratio stays on all_group_values. The tests fail until T053.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvJdxgnyc4tvLrYVXSk
…_model call (T053)

The SC-003 count and the SC-004 ratio read the model_ids cases (spec C-017).
The SC-003 time ratio stays on the evaluation of all groups one by one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvJdxgnyc4tvLrYVXSk
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvJdxgnyc4tvLrYVXSk
…r is available

The run at b6ff1a8 passed SC-002, SC-003, SC-004, SC-006, SC-008 and
SC-009. SC-005 and SC-007 failed, probably because of load on the computer,
and low memory stopped the memory check. Add OP6, block T030 by it, and
record how to resume.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvJdxgnyc4tvLrYVXSk
Add the reduced-scope work (T046-T053): the merge of master, the removal of
the cache between calls and the model lock, the shorter standard suite and
the new acceptance rules. T030 is parked (OP6). The dev-toolkit files stay
out of the PR branch.

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

Copy link
Copy Markdown
Member Author

Update 2026-11-18: reduced scope (spec C-012 to C-017)

This push adds tasks T046 to T053:

  • Merge of master (T046). The value lookup moved to StructuredGrid3DGeometry. No golden reference changed.
  • No cache between calls and no model lock (T047, T048, C-012). The scope of one call stays: it shares restored points inside one evaluation call and keeps nothing after it. Two calls on one model can run at the same time. evaluation_cache and evaluation_revision are removed. They were never released.
  • Shorter standard suite (T049, T050, C-014, C-016). The suite has 131 cold cases. The counters pass runs the 5,000 and 20,000-point cases, and the memory pass runs the 5,000-point cases.
  • New acceptance rules (C-013, C-015, C-017). SC-006 passes at 120 % of the baseline or at most 16 MiB more. SC-010 is removed. The SC-003 count and SC-004 use one evaluate_model call (T052, T053).

tests/fault_evaluation: 347 passed. Golden tests pass. Ruff shows no new finding.

Acceptance run (T030, revision b6ff1a8): parked

SC Result Measured Limit
SC-002 pass 0.105 0.40
SC-003 pass 0.127, 4 fault calls 0.15
SC-004 pass 0.54 1.5
SC-005 fail 0.520 (0.40 in the repeat run) 0.50
SC-006 pass all cases within 120 % or +16 MiB —
SC-007 fail worst noise 2.02 1.10
SC-008 pass 581 s 900 s
SC-009 pass 0.487 0.50

The two runs of the same code differ by 0.48 to 2.98 times, in both directions. The computer was low on memory, and that also stopped the 10-million-point memory check. The SC-005 and SC-007 failures are probably caused by load on the computer. T030 is parked until a quiet computer is available (plan OP6).

Open tasks

  • T030: measure again on a quiet computer.
  • T031: documentation.
  • T032: final checks.
  • The end review of the spec.

This PR stays a draft until these are done.

🤖 Generated with Claude Code

https://claude.ai/code/session_01XADRvJdxgnyc4tvLrYVXSk

lachlangrose and others added 3 commits November 22, 2026 14:50
Spec 005 files and README take master. Code conflicts resolved on top of
master's block / restored_through fault API: the evaluation scope wraps
GeologicalFeature.evaluate_value and evaluate_gradient, the scope restores
through restore_through, and the shared frame lookup is kept as a fast path
in FaultSegment._evaluate_frame (need_* are now keyword-only).

Known: 003 tests that call _evaluate_frame positionally, check the old public
signatures, or compare against frozen pre-005 golden output still fail.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
- plugin recipe: drop fault_pitch (master rejects the slip vector it produced)
- frame tests: pass need_gradient by keyword; legacy copy applies reverse per step
- signature test: evaluate_value/evaluate_gradient now take block, restored_through
- edge test and one gradient test: values frozen from master 790036d
- gradient test double accepts restored_through

The 16 synthetic and plugin golden archives are unchanged. Tests outside
tests/fault_evaluation were not run after the merge.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Exports a fitted fault model to raw arrays plus reference results, so a
native oracle can be validated against the Python implementation.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvHwfGoDbJ1XFwymbUm

This branch has not been deployed

No deployments
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