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
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
## 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-06-12 - Inline Squared Distance with Early Exits
**Learning:** Calling `libm::sqrt` for distance calculations inside hot spatial scans and neighborhood graph algorithms introduces significant performance overhead. Furthermore, extracting `sqrt` to just compare against `epsilon` is unnecessary since `distance < epsilon` is equivalent to `distance^2 < epsilon^2` for `epsilon > 0`.
**Action:** When performing neighborhood scans or distance thresholding, pre-calculate the squared threshold (`epsilon * epsilon`), assert `epsilon > 0` before loops, and replace external function calls with an inline loop. Crucially, use early exits formatted as `!(sum < eps_sq)` to safely short-circuit comparisons when dealing with potential `NaN` values, instead of normal boolean logic which can have tricky `NaN` interactions.
22 changes: 17 additions & 5 deletions crates/aether-core/src/ml/clustering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// ═══════════════════════════════════════════════════════════════════════════════
//


#![allow(dead_code)]

use libm::{fabs, sqrt};
Expand Down Expand Up @@ -274,18 +273,22 @@ impl<const D: usize> KMeans<D> {

/// Automatically determine optimal K using topological analysis
pub fn auto_k_selection<const D: usize>(data: &[[f64; D]], n: usize, epsilon: f64) -> usize {
if !(epsilon > 0.0) {
return n.clamp(1, MAX_POINTS);
}

// Build epsilon-neighborhood graph and count connected components (Ξ²β‚€)
let mut components = n.min(MAX_POINTS);
let mut components = 0;
let mut visited = [false; MAX_POINTS];
let n = n.min(MAX_POINTS);
let eps_sq = epsilon * epsilon;

for start in 0..n {
if visited[start] {
continue;
}

// BFS from this point
components -= 1;
let mut stack = [0usize; 64];
let mut top = 1;
stack[0] = start;
Expand All @@ -302,8 +305,17 @@ pub fn auto_k_selection<const D: usize>(data: &[[f64; D]], n: usize, epsilon: f6
// Add neighbors
for i in 0..n {
if !visited[i] && i != current {
let dist = distance(&data[current], &data[i]);
if dist < epsilon && top < 64 {
let mut sum = 0.0;
for d in 0..D {
let diff = data[current][d] - data[i][d];
sum += diff * diff;
}

if !(sum < eps_sq) {
continue;
}

if top < 64 {
stack[top] = i;
top += 1;
}
Expand Down