⚡ Bolt: Optimize squared L2 norm calculations via np.einsum - #172
Conversation
…n performance-critical paths Co-authored-by: stffns <70039235+stffns@users.noreply.github.com>
|
👋 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 New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
|
Bugbot is not enabled for your account, so this pull request was not reviewed. Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs. |
|
Warning Review limit reached
Next review available in: 27 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (9)
📝 WalkthroughWalkthroughSquared-norm and distance calculations in k-means, PQ, and IVFPQ now use ChangesEinsum distance updates
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In @.jules/bolt.md:
- Line 5: Update the section headed “2024-05-18 - Replacing sum of squares with
einsum in hot loops” in .jules/bolt.md by adding a blank line immediately after
the heading, before its paragraph content.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: 50b144e1-eda0-4c51-ae86-79f4c8c4f825
📒 Files selected for processing (4)
.jules/bolt.mdsnapvec/_ivfpq.pysnapvec/_kmeans.pysnapvec/_pq.py
| **Learning:** In performance-critical paths, computing the batch norm of a 2D array via `np.linalg.norm(arr, axis=1)` is relatively slow. Using `np.sqrt(np.einsum('ij,ij->i', arr, arr))` is significantly faster (~4x speedup on a laptop CPU for typical batch sizes). If `keepdims=True` behavior is needed, appending `[:, np.newaxis]` matches the original shape seamlessly. | ||
| **Action:** Always prefer `np.sqrt(np.einsum('ij,ij->i', arr, arr))` over `np.linalg.norm(arr, axis=1)` when computing row-wise vector norms in NumPy to eliminate dispatch overhead and improve execution speed. | ||
|
|
||
| ## 2024-05-18 - Replacing sum of squares with einsum in hot loops |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Add a blank line after this heading.
markdownlint reports MD022 because the heading is not separated from its paragraph.
Proposed fix
## 2024-05-18 - Replacing sum of squares with einsum in hot loops
+
**Learning:** In performance-critical NumPy operations...📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| ## 2024-05-18 - Replacing sum of squares with einsum in hot loops | |
| ## 2024-05-18 - Replacing sum of squares with einsum in hot loops | |
🧰 Tools
🪛 markdownlint-cli2 (0.23.1)
[warning] 5-5: Headings should be surrounded by blank lines
Expected: 1; Actual: 0; Below
(MD022, blanks-around-headings)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In @.jules/bolt.md at line 5, Update the section headed “2024-05-18 - Replacing
sum of squares with einsum in hot loops” in .jules/bolt.md by adding a blank
line immediately after the heading, before its paragraph content.
Source: Linters/SAST tools
…n performance-critical paths Co-authored-by: stffns <70039235+stffns@users.noreply.github.com>
…n performance-critical paths Co-authored-by: stffns <70039235+stffns@users.noreply.github.com>
…n performance-critical paths Co-authored-by: stffns <70039235+stffns@users.noreply.github.com>
💡 What: Replaced row-wise squared Euclidean norm calculations like
(X ** 2).sum(axis=1)withnp.einsum('ij,ij->i', X, X)[:, None]in hot paths (K-Means, PQ, and IVF-PQ). Also applied this to 3D arrays usingnp.einsum('ijk,ijk->ij', X, X).🎯 Why: Using explicit powers like
X ** 2forces NumPy to allocate and copy a full intermediate array of the exact same size asXbefore performing the.sum()reduction. For batched computations (e.g.n=100_000, d=128), this overhead dominates execution time and saturates memory bandwidth.np.einsumevaluates the contraction natively without intermediate allocations.📊 Impact: Typical ~3-5x execution speedup for distance/norm calculations across K-means seeding, Lloyd iteration assignments, and IVF residual scoring during index construction.
🔬 Measurement: Verified via ad-hoc benchmarking scripts (e.g.,
(X ** 2).sum(1)takes ~2.6s per 100 loops vs ~0.6s fornp.einsum('ij,ij->i', X, X)). Also ran fullpytestsuite locally to guarantee mathematical and semantic correctness (outputs remain identical up to float32 epsilons).PR created automatically by Jules for task 6498332361972021749 started by @stffns
Summary by CodeRabbit
Performance
Maintenance