Skip to content

feat(fault): native fault kernel in the new package loop-model-core (spec 007) - #431

Draft
lachlangrose wants to merge 39 commits into
masterfrom
feat/007-native-fault-kernel
Draft

lachlangrose wants to merge 39 commits into
masterfrom
feat/007-native-fault-kernel

Conversation

@lachlangrose

Copy link
Copy Markdown
Member

Summary

Spec 007: a native (Rust) fault kernel for LoopStructural, with the Python path kept as the reference and the fallback.

  • S1 (Python): a feature computes the restore order of its faults once for each state of the faults (restore_order), and the three members of a cut fault frame share one restoration. Bit-identical results.
  • S2/S3 (native): a new package loop-model-core (packages/loop_model_core, module loop_model_core) restores points through the faults of a supported feature and evaluates field values, grid gradients and the tetrahedron gradient of faulted features. LoopStructural uses it automatically when it is installed (pip install loopstructural[native]) and FAULT_KERNEL_VERSION matches.
    • Switch: LoopStructuralConfig.native_faults or LOOPSTRUCTURAL_NATIVE_FAULTS=0 (the setting wins).
    • Threads: LoopStructuralConfig.native_threads (None, 0 or a negative value: all cores).
    • Unsupported parts (Python-function profiles, a fold on a tetrahedral mesh, subclasses of the fault types, forward movement through a cutting fault) use the Python path.
  • S4/S5 (packages): own wheel workflow (model-core-wheels.yml, 5 platforms, abi3-py39), PyPI upload, CI checks in native.yml, and release-please and dev-tools entries. loop_interpolation_core keeps only the solver.

Spec, plan, tasks and evidence: specs/007-native-fault-kernel/ (verification.md has all measurements).

Results (T030 computer, i7-12700H)

Criterion Target Result
SC-001 Python cost of a cutting fault (94 faults, 500 pts) ≤ 10 ms 10.7 ms (39.1 ms before): fails narrowly
SC-002 native/Python, 1 thread ≤ 0.20 0.23–0.30 (3.4–4.3× faster): fails
SC-003 native/Python, 20 threads ≤ 0.03 0.022–0.028: pass
SC-004 94 faults, evaluate_model at 5000 pts ≤ 0.5 s 0.063 s (Python 0.97 s, master 2.09 s): pass
SC-005 golden tests with native on pass pass
SC-006 suite native on / off / not installed pass pass, except 1 pre-existing failure (below)
SC-007 extra memory, 10M points spec 003 rule 685 MB (Python 6430 MB): pass
SC-008 prebuilt wheels on 5 platforms install + golden tests Linux x86_64 checked locally; the others need this PR's CI run

Native vs Python: largest difference 9.3e-10 on the golden models and 1.3e-12 on the 94-fault model (tolerance 1e-8 × box); NaN points equal; model ids equal.

Test plan

  • pytest with the native path on (3343 passed), off (3343 passed), and without loop-model-core (3311 passed). The only failure, tests/unit/modelling/test_native_fault_frame.py::test_the_native_fault_frame_is_the_cg_fault_frame, is a native solver test that also fails on master whenever loop_interpolation_core is installed (out of scope).
  • ruff check LoopStructural packages: no new errors (34 on master).
  • Both crates: cargo fmt --check, clippy (none, parallel, python) with -D warnings, cargo test --release (also --features parallel).
  • 94-fault model: native vs Python in one build at 500/2000/5000 points; official benchmark with native on.
  • CI: model-core-wheels.yml and core-wheels.yml on all platforms (SC-008); macOS x86_64 is cross-compiled and needs a check by hand.

Before merge / release

  • uv.lock is not changed: the master lock is already stale (release-please raised versions without a re-lock). A separate re-lock on master adds loop-model-core.
  • PyPI: add this repository and pypi.yml as a trusted publisher of the new project loop-model-core.
  • Squash-merge with a feat title, so release-please makes the first loop-model-core release.
  • Open decisions: accept SC-001 and SC-002 as measured, or add a follow-up (share the grid lookup between value and gradient in grid_field.rs). 34 minor review findings are listed in specs/007-native-fault-kernel/run-state.json.

🤖 Generated with Claude Code

https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo

lachlangrose and others added 30 commits November 27, 2026 12:30
…er handoff

benchmarks/fault_evaluation/many_faults.py builds the deterministic 94-fault
model (54 isolated, 40 cutting faults; about 7 minutes), times evaluate_model
and the cost of each fault, and saves or compares the model ids and group
values bit for bit (SC-001, SC-004). A fast test checks the fault layout
without a build. HANDOFF.md tells the implementer what to read, the order of
work and how to measure.

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

T001 records the native fault decision in ROADMAP.md. T002 profiles the
94-fault model: the restore order work takes most of the time of a cut
fault. T003 and T014 add failing tests for restore_order and the native
settings. T008 adds the Rust module fault with types and stubs. T009
records the likely cause of the proof-of-concept difference.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
…es and k-d tree

T004 adds restore_order: a feature keeps the restore order of its faults and
computes it again only when a cutting fault changes (spec 007 story 1).
T010, T011 and T023 port the structured grid lookup, the fault displacement
profiles and an exact nearest-point search to the Rust module fault, with
tests against values from Python. T015 adds LoopStructuralConfig.native_faults,
native_threads and the native kernel switch. The T003 test no longer passes
equal_nan to assert_array_equal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
GridField::gradient gives a zero gradient outside the grid, as the Python
interpolator does. Only a NaN coordinate gives a NaN gradient. A NaN
gradient outside the grid would make a restored point NaN.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
T012 adds the native restoration of the Rust module fault: apply and
restore port FaultSegment.apply_to_points and restore_through, with the
block sides, reverse movement, the cutter chain, the region masks and the
step direction of #425. A thread pool serves the batch functions.
T019 adds --native-faults {on,off} to the benchmark runner. T005 measures
the 94-fault model after the order cache: 13.6 ms for each cutting fault
(39.1 ms before), so SC-001 still fails and D1b runs next.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
…kernel to Python

T006 (D1b) restores the points once for the three members of a cut fault
frame, with bit-identical results. The 94-fault model now takes 10.7 ms for
each cutting fault (39.1 ms before stage S1); SC-001 asks for 10 ms. The
lookup-count test allows fewer lookups for the child_faults case, with the
maintainer's agreement. T013 adds the PyO3 classes FaultChain and GridField
and FAULT_KERNEL_VERSION = 1. T024 adds the native tetrahedron gradient with
the nearest-point fill and the refinement rounds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
… and CI checks

T017 and T018 add the tests of the native restoration (parity with the
Python path on the golden models, the switch, and story 5: fallback, no
stale data, GIL release, threads); they fail until T016. T025 adds
FaultChain.feature_gradient. T020 runs tests/fault_evaluation with the
native core in CI, and T030 adds a fault kernel smoke test to the wheel
workflow. T007 records stage S1 in ROADMAP.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
T016 adds chain_for and restore to _native_faults: a supported feature gets
a FaultChain built from its faults, kept with a state token, and the Python
path calls the kernel first. A fault whose cutters are not restored at its
turn (forward movement) uses the Python path. The largest native difference
on the golden models is 9.3e-10. T034 runs the spec 003 tests of the Python
internals with the native path off. T031 documents the fault kernel. The
T006, T017 and T018 tests follow the decisions in the plan answers.

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

T021 measures stage S2 on the T030 computer: the native restoration takes
0.23 to 0.30 of the Python time on 1 thread (SC-002 asks for 0.20 or less)
and 0.023 to 0.028 on 20 threads (SC-003 passes). The extra memory for 10
million points is 685 MB (Python 6430 MB; SC-007 passes). The largest native
difference on the golden models is 9.3e-10 m. T026 adds the parity tests of
native values, gradients and model ids; they fail until T027. New task T035
makes the 1-thread kernel faster.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
The native field parity test now checks that the native path is used for
each faulted feature, so it cannot pass on the Python path twice. The
gradient test on a fault also checks the Python path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
T027 adds field_for: a supported feature evaluates its values and grid
gradients with a native GridField, and the gradient of a faulted feature
with FaultChain.feature_gradient (tetrahedron, nearest fill, refinement).
The largest native difference of the field parity tests is 1.2e-10. The
fold feature on a tetrahedral mesh keeps the Python path. T035 removes
hot-path work in the native restoration; a quiet re-measure shows no
measurable gain, so SC-002 still fails (0.23 to 0.30; target 0.20).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
T028 measures the 94-fault model with the native path: evaluate_model at
5000 points takes 0.059 s (target 0.5 s; Python 0.971 s in the same run,
2.09 s on master), and the golden tests pass with the native path on.
T022 records stage S2 in ROADMAP.md. New task T036 lets the 94-fault
benchmark run with the native path on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
… record S3 and S4 checks

T036 writes None for the per-fault times when the native path makes no
Python fault call, so many_faults no longer fails with the native path on.
T029 records stage S3 in ROADMAP.md. T032 builds the native wheel on Linux
x86_64, installs it in a clean environment and passes the smoke test and
the golden tests; the other platforms need a CI run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
T033 runs the full test suite three times: with the native path on and off
3341 tests pass, and without the native package 3296 pass. The only failure
is a native solver test that also fails on master with the core installed.
Ruff finds no new errors. ROADMAP.md records stage S4.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
The maintainer moves the native fault kernel out of loop_interpolation_core
into a new package loop-model-core, in spec 007. Design D5 and tasks T037
to T042 move the crate, the Python import, the extra, CI and release, and
fix two end review findings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
…op-model-core

The fault kernel evaluates a built model and shares no code with the solver,
so it moves out of loop_interpolation_core into packages/loop_model_core
(Python module loop_model_core, FAULT_KERNEL_VERSION 1). The Rust code and
its tests move with git mv; the PyO3 classes FaultChain and GridField move
to the new crate. The solver core keeps only the solver.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
lachlangrose and others added 9 commits November 27, 2026 17:08
LoopStructural imports the fault kernel from loop_model_core, and the
extra native installs loop-model-core. A new workflow builds and smoke
tests its wheels on the five platforms; pypi.yml publishes it, native.yml
checks and tests it, and release-please and the dev-tools config know the
package. The solver core wheel test keeps only the solver check. uv.lock is
not changed: the master lock is already stale (plan Answers, T038).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
… subclasses to Python

native_threads() gives 0 (all cores) for a negative setting, as -1 in
joblib; before, the native call raised OverflowError. The support checks of
the native path use exact types for FaultSegment, FaultDisplacement and the
region classes, so a user subclass takes the Python path (FR-008). The
settings test fixture now restores the module state after each test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
With both native cores, with the native path off, and without
loop-model-core, the full suite passes except one native solver test that
also fails on master. Ruff finds no new errors. Both crates pass fmt,
clippy and tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XADRvNCFVAUoEtXrZywGSo
Native and Python results agree on the 94-fault model at 500, 2000 and
5000 points: the largest difference is 1.3e-12 (tolerance 1e-4), the NaN
points are equal and no model id differs. The official benchmark runs with
the native path on: 0.063 s at 5000 points (SC-004, target 0.5 s).

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

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