Skip to content

LTM post-sim: preserve emission order for rel_loop_score denominator summation #468

Closed

Description

@bpowers

Problem

PR #461 (branch `ltm-perf-enable-always`, commit d46ba7f) moved `rel_loop_score` normalization from compile-time synthetic variables to post-simulation computation in `src/simlin-engine/src/ltm_post.rs`.

The old compile-time emitter (`ltm_augment::generate_relative_loop_score_equation`, removed in commit 1234624) generated equation text that summed `ABS(loop_score_*)` in the order loops appeared in the `detected_loops` Vec (roughly emission order: `b1, b2, ..., b10, ..., r1, r2, ...`). The new post-sim helpers `compute_rel_loop_scores` and `compute_partition_denominator` sort loop IDs lexicographically (e.g., `b1, b10, b11, ..., b2, ..., r1, r10, ...`) to pin determinism across salsa cache invalidations.

Because IEEE-754 addition is non-associative, the sort change produces bit-level differences in the denominator sum relative to the old emission-order accumulation.

Quantitative impact

For a partition of N loops with `|loop_score|` magnitudes around scale `S`, per-step drift is at most `~N * S * 2^-52`. Existing tests tolerate 1e-10 (proptest) / 0.05 (integration); for realistic partition sizes (<= 10k at `MAX_LTM_TOTAL_CIRCUITS`) and typical `loop_score` magnitudes, the drift is well below these tolerances. No test fails today and no user-facing decision (e.g. loop ranking) changes.

Why this is being deferred rather than fixed in PR #461

  • The 'correct' old order was insertion order into a `HashMap<String, Option>`, which is not preserved by the current `LtmVariablesResult` shape (HashMap is unordered).
  • Preserving emission order would require either migrating to `IndexMap` or storing a parallel `Vec` of loop_ids ordered by emission time, threading through `LtmVariablesResult` and every consumer.
  • The `ltm_post.rs` code comment at lines 73-81 already explicitly acknowledges that lex-sort was chosen for determinism vs HashMap hash-randomized iteration. The design plan `docs/design-plans/2026-04-18-ltm-cap-lift-diagnosis.md` documents the semantic replacement (not bit-exact).

Why this matters

  • Correctness: drift is below current test tolerances but not zero. Downstream consumers that diff timeseries across engine versions (e.g., CI snapshot comparisons, external reports that pin bit-exact output) would see spurious diffs after the refactor.
  • Determinism story: the current comment pins determinism 'across runs on the same input' (HashMap hash-randomization), but silently changes the semantic vs. the pre-PR emitter. Restoring parity removes a subtle behavior change from the refactor.
  • Maintenance: a future reader comparing against the pre-PR implementation will observe bit-level differences and not have an obvious pointer to the intentional order change.

Possible approaches

Preferred: Switch `LtmVariablesResult.loop_partitions` (declared in `src/simlin-engine/src/db.rs:1989`) from `HashMap<String, Option>` to `IndexMap<String, Option>`. Populate in emission order during `model_ltm_variables` (`src/simlin-engine/src/db_ltm.rs:2362` / 2643). Post-sim helpers iterate in map order instead of sorting, restoring bit-for-bit parity with the old compile-time emitter while preserving deterministic iteration across salsa cache invalidations.

Alternative: Store a parallel `Vec` of loop IDs ordered by emission time on `LtmVariablesResult` and have post-sim consumers iterate that Vec instead of the HashMap. Marginally more memory, but avoids a new dependency.

Component(s) affected

  • `src/simlin-engine/src/db.rs` (`LtmVariablesResult.loop_partitions` declaration, line 1989)
  • `src/simlin-engine/src/db_ltm.rs` (population at lines 2187, 2258, 2362, 2643, 2743)
  • `src/simlin-engine/src/ltm_post.rs` (consumers at lines 61, 154, 271, 411; the lex-sort comment at 73-81 should be updated)
  • `src/simlin-engine/src/ltm_finding.rs:679` (also consumes `loop_partitions`)

Severity

Low. No correctness or user-visible behavior change under current test tolerances; impact is bit-level drift only.

Discovery context

Flagged by Codex iter-20 P3 during review of PR #461 (branch `ltm-perf-enable-always`). Filed for follow-up rather than fixed in the PR because the fix requires threading an IndexMap (or parallel Vec) through `LtmVariablesResult` and every consumer, which is out of scope for the cap-lift work.

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

    ltm

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions