Skip to content

ltm: cross-element loops should normalize by Δ-aggregate, not diagonal A2A link score #503

Closed

Description

@bpowers

Summary

The mixed-loop branch in build_element_level_loops (src/simlin-engine/src/db_ltm.rs:2247-2320) handles cross-element circuits — those that arise when a wildcard reducer like SUM(pop[*]) connects array elements through a scalar aggregate. The current treatment scores such loops as scalar loops whose links reference the diagonal A2A link score values (the {from}->{to} Bare-shape variable), on the rationale that "the wildcard reducer already encodes the cross-element contribution as a single A2A link-score value per neighbour."

A truly principled treatment would normalize the cross-element link score against the change in the aggregate (e.g., SUM(pop[*])), not against per-element Δsource. The diagonal A2A score asks "how much of Δshare[NYC] came from Δpop[NYC]" but a cross-element link pop[Boston] -> share[NYC] (via SUM(pop[*])) is asking "how much of Δshare[NYC] came from Δpop[Boston]" — these are different quantities.

Why this matters

For models where wildcard reducers aggregate magnitudes that don't differ wildly per element, the approximation is reasonable — relative loop scores are still in the right ballpark and dominance ordering is usually preserved. For models with highly heterogeneous element magnitudes (e.g., a large-population region dominating SUM(pop[*])), the diagonal link score systematically over-counts each element's individual contribution and under-counts cross-element coupling.

The implementation acknowledges this explicitly in shape_aware_source_ref (src/simlin-engine/src/ltm_augment.rs:732-738):

- `Wildcard` / `DynamicIndex` -> `from` (TODO: the truly principled
  denominator would be the change in the aggregate
  `SUM(from[*])` / `from[expr]` referenced live in the partial,
  but that requires re-parsing partial_eq or threading the AST
  subtree through. For now we keep the variable-level Δfrom; the
  resulting under/over-counting is the same documented edge-
  aliasing limitation already pinned by integration tests).

Locations

  • Comment documenting the approximation: src/simlin-engine/src/ltm_augment.rs:732-738
  • Cross-element circuit treatment: src/simlin-engine/src/db_ltm.rs:2247-2320
  • Test fixture: test/cross_element_ltm
  • Related design plan: docs/design-plans/2026-04-25-ltm-per-ref-elem-graph.md

Suggested fix

  1. When generating a link score for a Wildcard / DynamicIndex shape, thread the actual aggregate expression (e.g., SUM(from[*]) as it appears in the target's AST) through to shape_aware_source_ref so the SAFEDIV denominator can be (aggregate - PREVIOUS(aggregate)) instead of (from - PREVIOUS(from)).
  2. Cross-element loop scoring in build_element_level_loops should then reference the wildcard-shaped link score (suffixed with \u{205A}wildcard) directly rather than collapsing to the Bare diagonal.
  3. Validate against test/cross_element_ltm and add a fixture with heterogeneous element magnitudes to exercise the discrepancy.

Paper reference

Schoenberg, Hayward, Eberlein 2023 "Improving Loops that Matter," Eq. 3 establishes the link score's denominator semantics. The cross-element / aggregate-reducer case is not covered in the literature, which is why this is a Simlin-specific extension that should match Eq. 3's spirit.

Severity

medium (silent correctness — under/over-counting in a documented edge case; relative ordering of dominant loops typically preserved on homogeneous arrays, but breaks down on heterogeneous element magnitudes)

Discovery context

Identified during a deep design review of the Loops That Matter implementation against the reference papers, examining the gap between the documented TODO in shape_aware_source_ref and the cross-element handling in build_element_level_loops.

Tracking

Part of LTM tracking epic: #488. Sister issue to #487 (rel_loop_score partition normalisation), which addresses a different normalization gap (cross-loop partition lookup) using the same Loop-level machinery this issue would also touch.

Activity

  1. added
    ltm
    on May 9, 2026
  2. bpowers commented on May 9, 2026

    @bpowers
    OwnerAuthor

    Resolved by commits a05c921..4ff1ab4 on branch ltm-503-cross-element-agg (key commits: 44527d1 enumerate_agg_nodes, 3eca55f element-graph reroute through agg nodes, 9a59777 $⁚ltm⁚agg auxiliaries, fdaa06b agg-node link scores, c252286 recover cross-element loops through agg nodes, d14c978 score cross-element loops on the element-level path, 49be92d retire the wildcard/dynamic per-shape link-score path).

    Implemented via the aggregate-node approach rather than the threading-SUM(from[*])-through-shape_aware_source_ref approach this issue originally sketched: each maximal inlined reducer subexpression is hoisted into a synthetic $⁚ltm⁚agg⁚{n} auxiliary (mirroring how the LTM papers handle DELAY3/SMOOTH internal structure), causality is routed pop[d] → agg → target, the two halves are scored with real per-element link scores and composed by the chain rule (so each source element's fractional contribution to the aggregate's velocity is recovered -- exactly the missing factor for heterogeneous element magnitudes), the agg node is trimmed from the user-facing loop list, cross-element loops are scored on the element-level path with subscripted link-score refs, and the ⁚wildcard/⁚dynamic per-shape link-score machinery (and the shape_aware_source_ref TODO this issue quotes) was retired.

    See docs/design-plans/2026-05-09-ltm-503-cross-element-agg.md and the 'Aggregate Nodes' section of docs/design/ltm--loops-that-matter.md. Two follow-ups remain tracked: GH #514 (sliced reducer sub-expressions not yet hoisted), GH #515 (MAX_AGG_PETALS cap on cross-agg loop recovery), GH #516 (agg-hop link polarity Undetermined).

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