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-06-01 - Avoid Double Allocations and Bounds Checks in Tensor Operations
**Learning:** In element-wise Tensor operations (`add`, `mul`, `sub`, `scale`, `map`), manually iterating by index and pushing to a pre-allocated vector triggers bounds checks. Additionally, passing the collected vector to `Tensor::new()` creates a second redundant heap allocation because `new()` clones the slice into a `Vec`.
**Action:** Use iterator chains (`.iter().zip().map().collect()`) to allow LLVM to elide bounds checks, and use `Tensor::from_vec()` to initialize the tensor directly from the collected vector without duplicating allocations.
63 changes: 32 additions & 31 deletions crates/aether-core/src/ml/tensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// ═══════════════════════════════════════════════════════════════════════════════
//


#[cfg(feature = "alloc")]
use alloc::rc::Rc;
#[cfg(feature = "alloc")]
Expand Down Expand Up @@ -196,46 +195,48 @@ impl Tensor {
/// Element-wise addition
pub fn add(&self, other: &Tensor) -> Tensor {
assert_eq!(self.shape, other.shape, "Shape mismatch for add");
let total_size: usize = self.shape.iter().product();
let mut result_data = Vec::with_capacity(total_size);

let data_a = self.data.borrow();
let data_b = other.data.borrow();

for i in 0..total_size {
result_data.push(data_a[i] + data_b[i]);
}
// Bolt optimization: using iterators to allow LLVM to elide bounds checks.
// Using `Tensor::from_vec` avoids a redundant O(N) heap allocation compared to `Tensor::new()`.
let result_data: Vec<f64> = data_a
.iter()
.zip(data_b.iter())
.map(|(&a, &b)| a + b)
.collect();

Self::new(&result_data, &self.shape)
Self::from_vec(result_data, self.shape.clone())
}

/// Element-wise multiplication
pub fn mul(&self, other: &Tensor) -> Tensor {
assert_eq!(self.shape, other.shape, "Shape mismatch for mul");
let total_size: usize = self.shape.iter().product();
let mut result_data = Vec::with_capacity(total_size);

let data_a = self.data.borrow();
let data_b = other.data.borrow();

for i in 0..total_size {
result_data.push(data_a[i] * data_b[i]);
}
// Bolt optimization: using iterators to allow LLVM to elide bounds checks.
// Using `Tensor::from_vec` avoids a redundant O(N) heap allocation compared to `Tensor::new()`.
let result_data: Vec<f64> = data_a
.iter()
.zip(data_b.iter())
.map(|(&a, &b)| a * b)
.collect();

Self::new(&result_data, &self.shape)
Self::from_vec(result_data, self.shape.clone())
}

/// Scalar multiplication
pub fn scale(&self, s: f64) -> Tensor {
let total_size: usize = self.shape.iter().product();
let mut result_data = Vec::with_capacity(total_size);
let data = self.data.borrow();

for i in 0..total_size {
result_data.push(data[i] * s);
}
// Bolt optimization: using iterators to allow LLVM to elide bounds checks.
// Using `Tensor::from_vec` avoids a redundant O(N) heap allocation compared to `Tensor::new()`.
let result_data: Vec<f64> = data.iter().map(|&val| val * s).collect();

Self::new(&result_data, &self.shape)
Self::from_vec(result_data, self.shape.clone())
}

/// Transpose (2D)
Expand Down Expand Up @@ -267,32 +268,32 @@ impl Tensor {
/// Element-wise subtraction
pub fn sub(&self, other: &Tensor) -> Tensor {
assert_eq!(self.shape, other.shape, "Shape mismatch for sub");
let total_size: usize = self.shape.iter().product();
let mut result_data = Vec::with_capacity(total_size);

let data_a = self.data.borrow();
let data_b = other.data.borrow();

for i in 0..total_size {
result_data.push(data_a[i] - data_b[i]);
}
// Bolt optimization: using iterators to allow LLVM to elide bounds checks.
// Using `Tensor::from_vec` avoids a redundant O(N) heap allocation compared to `Tensor::new()`.
let result_data: Vec<f64> = data_a
.iter()
.zip(data_b.iter())
.map(|(&a, &b)| a - b)
.collect();

Self::new(&result_data, &self.shape)
Self::from_vec(result_data, self.shape.clone())
}

/// Element-wise mapping
pub fn map<F>(&self, f: F) -> Self
where
F: Fn(f64) -> f64,
{
let total_size: usize = self.shape.iter().product();
let mut result_data = Vec::with_capacity(total_size);
let data = self.data.borrow();

for i in 0..total_size {
result_data.push(f(data[i]));
}
// Bolt optimization: using iterators to allow LLVM to elide bounds checks.
// Using `Tensor::from_vec` avoids a redundant O(N) heap allocation compared to `Tensor::new()`.
let result_data: Vec<f64> = data.iter().map(|&val| f(val)).collect();

Self::new(&result_data, &self.shape)
Self::from_vec(result_data, self.shape.clone())
}
}