Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
## 2026-05-01 - Avoid High-Level Tensor Ops in Scalar Reductions
**Learning:** High-level `Tensor` operations like `sub()` and `mul()` trigger intermediate heap allocations for shape and stride metadata. When computing scalar reductions (like MSE, distances, or loss functions), using these operations introduces severe memory overhead inside hot loops. Attempting to use `.min()` length truncation as a safeguard is an anti-pattern as it masks shape mismatch errors.
**Action:** For scalar reductions, assert shape equality (`assert_eq!(a.shape, b.shape)`) and perform a single-pass iteration directly over the underlying borrowed data arrays (`a.data.borrow()`) to eliminate intermediate allocations and safely compute the result.

## 2026-05-22 - Squared Distance Comparisons in Hot Spatial Scans
**Learning:** `SparseAttentionGraph::add_point` performs hot O(N) spatial scans. The `ManifoldPoint::is_neighbor` method previously computed a full `libm::sqrt` using `self.distance(other)` to threshold points. This introduced costly `sqrt` and array operations that could be avoided.
**Action:** When determining distance thresholds in hot paths, prefer squared distance comparisons (`d^2 < r^2`) and introduce an inline loop with early exits (`if !(sum < eps_sq)`) instead of using a standalone `distance` calculation. Explicitly reject negative or NaN thresholds before the loop (e.g. `!(epsilon > 0.0)`).
21 changes: 19 additions & 2 deletions crates/aether-core/src/manifold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
// ═══════════════════════════════════════════════════════════════════════════════
//


#![allow(dead_code)]

use libm::sqrt;
Expand Down Expand Up @@ -76,7 +75,25 @@ impl<const D: usize> ManifoldPoint<D> {

/// Check if within epsilon-neighborhood (sparse attention criterion)
pub fn is_neighbor(&self, other: &Self, epsilon: f64) -> bool {
self.distance(other) < epsilon
if !(epsilon > 0.0) {
return false;
}

let eps_sq = epsilon * epsilon;
let mut sum = 0.0;

for i in 0..D {
let d = self.coords[i] - other.coords[i];
sum += d * d;

// Early exit if distance exceeds threshold.
// We use `!(sum < eps_sq)` to safely handle NaNs without altering logic.
if !(sum < eps_sq) {
return false;
}
}

true
}
}

Expand Down