core/core/physics
Shay 2a2ef9ce49
perf(salience): vectorize curvature pairwise loop — 57× faster, 42% e2e (#96)
cProfile attribution (2026-05-21) identified
``core.physics.salience.SalienceOperator.compute`` as 64% of total
``ChatRuntime.chat()`` time.  Pre-fix it was a nested Python loop
over ``regions × regions`` with one ``np.linalg.norm`` call per
pair.  For N≈500 mounted-vocab regions per turn that meant ~250k
norm calls per turn, dominating end-to-end latency.

Fix: numpy broadcast for pairwise displacement, distance,
pressure-delta, and contribution.  Same math; same contract.
ULP-level reassociation drift is absorbed by the 12-decimal
precision ``_salience_address`` already used for content
addressing, and by the float32 conversion at the downstream
``SalienceMap.scores_arr`` site, so neither the content_address
nor the top-k ordering changes.

Measurements (region set: N=493, dim=5, seeded):

  vectorized:  11.78 ms/call
  old-loop:   672.30 ms/call
  speedup:    57.1×

End-to-end on 8 cognition-shape prompts:

  pre-fix:  ~970 ms/turn
  post-fix:  565 ms/turn   (-42%)

Validation:

  * 15 new tests in ``tests/test_salience_vectorize_parity.py``:
      - parity with a nested-loop reference to 1e-9 absolute on
        curvature_magnitude, gradient_vector, influence_radius
        across N ∈ {1, 2, 8, 32, 128, 493}
      - content_address byte-identical across N ∈ {1, 8, 32, 128}
      - top-16 ordering matches the reference at N ∈ {32, 128, 493}
      - empty regions returns empty map
      - single region has zero curvature
  * ``core eval cognition`` byte-identical: public 100/100/91.7/100.
  * ``core test --suite cognition`` 120/0/1, ``smoke`` 67/0.

The file's pre-existing docstring promised a Rust path
(``core_rs::physics::salience::compute_curvature``) that does not
yet exist — the numpy vectorization realizes the lift now while
keeping the Rust port a future optimization on stable semantics
(CLAUDE.md: "Rust backend parity only after Python semantics are
locked by tests").
2026-05-20 21:29:42 -07:00
..
__init__.py Implement core physics and pack validation 2026-05-14 12:35:19 -07:00
articulation.py Implement core physics and pack validation 2026-05-14 12:35:19 -07:00
attention.py Implement core physics and pack validation 2026-05-14 12:35:19 -07:00
binding.py Implement core physics and pack validation 2026-05-14 12:35:19 -07:00
digest.py Implement core physics and pack validation 2026-05-14 12:35:19 -07:00
drive.py Implement core physics and pack validation 2026-05-14 12:35:19 -07:00
energy.py Tighten hot-path backend consistency 2026-05-15 08:14:38 -07:00
exertion.py feat(physics): add mind-physics layer — ADR-0008/0009/0010, blueprint, and operator stubs 2026-05-12 23:20:58 -07:00
identity.py feat(telemetry): ADR-0078 Phase 1 — composer/graph atom equivalence (observational) 2026-05-20 06:14:25 -07:00
inhibition.py Implement core physics and pack validation 2026-05-14 12:35:19 -07:00
learning.py Implement core physics and pack validation 2026-05-14 12:35:19 -07:00
reasoning.py Fix main chat test regressions 2026-05-14 15:48:45 -07:00
salience.py perf(salience): vectorize curvature pairwise loop — 57× faster, 42% e2e (#96) 2026-05-20 21:29:42 -07:00
valence.py Implement core physics and pack validation 2026-05-14 12:35:19 -07:00