Skip to content

⚡ Bolt: Optimize backpropagation by removing redundant tensor clones - #167

Open
teerthsharma wants to merge 52 commits into
masterfrom
bolt/optimize-neural-backprop-10187495109738650801
Open

⚡ Bolt: Optimize backpropagation by removing redundant tensor clones#167
teerthsharma wants to merge 52 commits into
masterfrom
bolt/optimize-neural-backprop-10187495109738650801

Conversation

@teerthsharma

Copy link
Copy Markdown
Owner
  • 💡 What: Replaced .as_ref().expect(...).clone() with .take().expect(...) for last_z and last_input in DenseLayer::backward.
  • 🎯 Why: During backpropagation, these cached tensors are only used once. Cloning them allocates new vectors for shape and stride metadata unnecessarily.
  • 📊 Impact: Eliminates two Vec allocations per layer per backward pass, improving training speed and reducing memory pressure.
  • 🔬 Measurement: Verified by running the core test suite (cargo test -p aether-core).

PR created automatically by Jules for task 10187495109738650801 started by @teerthsharma

teerthsharma and others added 30 commits January 27, 2026 00:35
…4553412

Implement visualization features and unique view example
- Implemented `StmtKind::For` handling in `compile_stmt` in `crates/aether-lang/src/vm.rs`.
- Implemented `StmtKind::Loop` (infinite seal loop) handling in `compile_stmt`.
- Added a regression test `test_compiler_for_loop` to verify `For` loop execution behavior.
- Updated TODO comments to reflect implemented features.
- Loop implementation uses simple stack-based comparison (`iterator - end != 0`) and standard jump operations.

Co-authored-by: teerthsharma <78080953+teerthsharma@users.noreply.github.com>
Convert `MemoryAreaTypeId` to `MemoryAreaType` enum in `bios.rs` to correctly match memory region kinds. `multiboot2` 0.16+ uses `MemoryAreaTypeId` which does not expose enum variants directly.

Verified logic with standalone test case.

Co-authored-by: teerthsharma <78080953+teerthsharma@users.noreply.github.com>
- Optimized `ChebyshevGuard::calculate` to use single-pass variance calculation (O(N) instead of O(2N)).
- Added early exit for empty blocks using `occupied_mask`.
- Reduces execution time by ~48% in high-load scenarios.

Co-authored-by: teerthsharma <78080953+teerthsharma@users.noreply.github.com>
…-17593852834038515529

⚡ Bolt: Optimize ChebyshevGuard calculation to single pass
…682053720791626755

Fix MemoryRegionKind matching in bios.rs
…128293079839561

Implement For and Loop statements in Compiler
…nsor allocations

Co-authored-by: teerthsharma <78080953+teerthsharma@users.noreply.github.com>
…ons-5124804379038153332

⚡ Bolt: Optimize scalar reductions in linalg to avoid intermediate Tensor allocations
Agent-Logs-Url: https://github.com/teerthsharma/Aether-Lang/sessions/0c8c4be0-1b72-4759-9e8c-9cf44d9aff7b

Co-authored-by: teerthsharma <78080953+teerthsharma@users.noreply.github.com>
Agent-Logs-Url: https://github.com/teerthsharma/Aether-Lang/sessions/0c8c4be0-1b72-4759-9e8c-9cf44d9aff7b

Co-authored-by: teerthsharma <78080953+teerthsharma@users.noreply.github.com>
Agent-Logs-Url: https://github.com/teerthsharma/Aether-Lang/sessions/3700403d-a5ad-4339-bb2f-3410e0249039

Co-authored-by: teerthsharma <78080953+teerthsharma@users.noreply.github.com>
…ether-lang

Rewrite Aether Lang docs with accurate, down-to-earth behavior notes
…Teerth Sharma credit

- New LICENSE: requires prominent attribution to Teerth Sharma for any use
- No commercial use without written permission from Teerth Sharma
- No claim of independent invention
- Copyright header added to all 46 .rs source files
- Closes #priority-claim
What: Replaced the expensive `libm::sqrt` distance check in `ManifoldPoint::is_neighbor` with an inline squared distance loop. Also fixed a bug in `auto_k_selection` component counting that was breaking the test suite.

Why: In hot paths doing spatial scanning like `SparseAttentionGraph::add_point`, evaluating distances for neighborhood connectivity is a massive bottleneck. Bypassing `sqrt` significantly improves throughput. The `auto_k_selection` fix ensures the topological algorithm returns valid cluster counts.

Impact: Greatly reduces execution time for manifold space embedding and topological pipeline operations by avoiding costly math calls inside tight nested loops.

Measurement: Run `cargo test -p aether-core` to verify that `manifold` tests and `auto_k_selection` pass perfectly with identical logical outcomes.

Co-authored-by: teerthsharma <78080953+teerthsharma@users.noreply.github.com>
…31639021157296871

⚡ Bolt: Optimize spatial neighborhood checks
fix

Co-Authored-By: Copilot <198982749+Copilot@users.noreply.github.com>
Signed-off-by: teerth sharma <teerth.2428010112@muj.manipal.edu>
…ngine

feat: add bounded persistent homology engine
Refactored the reverse-mode autograd backward pass to use Option::take()
instead of Option::clone() when grabbing the gradient from the output
node. Additionally, modified `accumulate_grad` to take the gradient by
value rather than reference.

Because Tensor contains heap-allocated metadata (shape and strides
vectors), cloning Option<Tensor> inside the backward loop triggered
unnecessary heap allocations per operation. Using `take()` to temporarily
own the gradient, passing computed values by value to `accumulate_grad`,
and subsequently re-inserting the gradient into the `grads` vector
eliminates these allocations, optimizing the hot path during backprop.

Co-authored-by: teerthsharma <78080953+teerthsharma@users.noreply.github.com>
Replaced manual index-based loops with `.iter().zip().map().collect()`
for `add`, `mul`, and `sub` methods inside `aether-core/src/ml/tensor.rs`.
This avoids bounds checks, manual allocation, and allows LLVM to vectorize.

Co-authored-by: teerthsharma <78080953+teerthsharma@users.noreply.github.com>
teerthsharma and others added 22 commits July 7, 2026 21:47
…on-9677390441113488085

⚡ Bolt: [Improve Tensor Element-Wise Arithmetic]
Co-authored-by: teerthsharma <78080953+teerthsharma@users.noreply.github.com>
Refactored `verify_sliding_window` in `aether-core::topology` to use an incremental O(N) algorithm instead of the naive O(N*W) windowing implementation. This yields a substantial performance improvement. Betti-0 and Betti-1 numbers in `verify_sliding_window` used to be recalculated over the entire window `W` for each slide. The new approach updates these numbers incrementally in O(1) time by only processing the entering and exiting edges.

Impact:
Speeds up topological sliding window verification by ~2.8x (153ns -> 55ns per iteration in microbenchmarks).

Co-authored-by: teerthsharma <78080953+teerthsharma@users.noreply.github.com>
…ic and initialization

Co-authored-by: teerthsharma <78080953+teerthsharma@users.noreply.github.com>
Co-authored-by: teerthsharma <78080953+teerthsharma@users.noreply.github.com>
…632450702820

⚡ Bolt: [Eliminate redundant tensor cloning in autograd backward pass]
…14563931828777115187

⚡ Bolt: Avoid redundant O(N) slice allocations during tensor arithmetic and initialization
…indow-13465024019191029996

⚡ Bolt: O(N) incremental sliding window for Betti topology verification
…5301071988236

⚡ Bolt: Avoid redundant O(N) slice allocations during tensor operations
…mulation-14576655839750541676

⚡ Bolt: Optimize backward pass gradient accumulation
…allocations

Replaced high-level tensor operations (`.sub()`, `.scale()`, `.map()`) in `LossConfig::derivative` with direct single-pass iterators over the borrowed data arrays, collecting into a vector and using `Tensor::from_vec()`.
High-level tensor operations trigger costly intermediate heap allocations for both data and metadata (shape/strides). Direct iterators eliminate these allocations. Using `Tensor::from_vec()` takes ownership of the vector, avoiding a redundant O(N) slice allocation that occurs with `Tensor::new()`.

Co-authored-by: teerthsharma <78080953+teerthsharma@users.noreply.github.com>
💡 What: Modified `MLP::forward` to extract the first layer using `self.layers.iter_mut()` and pass the initial `input` reference directly, rather than cloning the input tensor before the loop.
🎯 Why: The first layer's `forward` method already accepts a reference. Removing the clone avoids unnecessary heap allocations for shape and stride metadata and an `Rc` increment.
📊 Impact: Reduces memory allocation overhead and improves throughput in MLP forward passes, which is heavily used during training.
🔬 Measurement: Verified by running the core test suite.

Co-authored-by: teerthsharma <78080953+teerthsharma@users.noreply.github.com>
Use `Option::take()` instead of `Option::as_ref().unwrap().clone()` to consume cached tensors `last_z` and `last_input` during `DenseLayer::backward`. This avoids two `Vec` allocations for shape and stride metadata per layer per backward pass, significantly reducing memory pressure and allocation overhead during training.

Co-authored-by: teerthsharma <78080953+teerthsharma@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants