Skip to content
Closed
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
5 changes: 5 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@
## 2025-05-19 - Dot product scalar gradients allocation
**Learning:** During gradient calculation, `float((e * (-gamma * distance)).sum())` creates two full-size `(N, J)` arrays: one for the scaled distance and one for the element-wise multiplication before reduction.
**Action:** Replace `(A * B).sum()` with `np.vdot(A, B)` when scalar reduction is needed over matrix multiplication (where `B` can incorporate scalars naturally like `-gamma * np.vdot(A, B)`). This entirely avoids the 2D array allocation overhead and yields order-of-magnitude improvements in scalar gradient components.


## 2024-05-19 - Fast matrix-matrix products in NumPy
**Learning:** Using element-wise multiplication followed by sum reduction over an axis (e.g. `(e_over_d.T * xi).sum(axis=...)` or similar forms) can be much slower than equivalent matrix multiplication `e_over_d.T @ xi`.
**Action:** Rewrite scalar reductions of broadcasted products into matrix multiplication `@` wherever possible. For instance, `xi * sum_e_over_d - np.dot(e_over_d, zeta)` is fast but we can convert `np.dot` to `@` directly or write `e_over_d @ zeta` for clarity and performance.
Comment thread
seonghobae marked this conversation as resolved.
8 changes: 5 additions & 3 deletions python/fast_mlsirm/objective.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ def neg_loglik_and_grad(
grad_b = e.sum(axis=0)
grad_alpha = np.zeros_like(params.alpha)
if free_alpha:
grad_alpha = (e * params.theta[:, factors]).sum(axis=0) * a
# Optimized alpha gradient: avoid N x J array allocation by using dense matrix multiplication
# and advanced integer indexing instead of element-wise multiplication and summation.
grad_alpha = (e.T @ params.theta)[np.arange(e.shape[1]), factors] * a
Comment thread
seonghobae marked this conversation as resolved.

# Optimized gradient computation: replace loop over dimensions with matrix multiplication
# We embed 'a' directly into the projection matrix to avoid a JxD intermediate array allocation during multiplication
Expand All @@ -150,10 +152,10 @@ def neg_loglik_and_grad(
# Optimized gradient computation: avoid 3D array creation, use 2D matrix multiplication instead
e_over_d = e / distance
sum_e_over_d = e_over_d.sum(axis=1, keepdims=True)
grad_xi = -gamma * (params.xi * sum_e_over_d - np.dot(e_over_d, params.zeta))
grad_xi = -gamma * (params.xi * sum_e_over_d - e_over_d @ params.zeta)

sum_e_over_d_j = e_over_d.sum(axis=0, keepdims=True).T
grad_zeta = gamma * (np.dot(e_over_d.T, params.xi) - params.zeta * sum_e_over_d_j)
grad_zeta = gamma * (e_over_d.T @ params.xi - params.zeta * sum_e_over_d_j)

# Optimized gradient computation: avoid intermediate array allocation by using vdot
grad_tau = float(-gamma * np.vdot(e, distance))
Expand Down
Loading