Skip to content

⚡ Bolt: vectorize BasicEstimator.predict#50

Open
guesswh0 wants to merge 1 commit into
masterfrom
bolt/vectorize-basic-estimator-predict-2340906720226119016
Open

⚡ Bolt: vectorize BasicEstimator.predict#50
guesswh0 wants to merge 1 commit into
masterfrom
bolt/vectorize-basic-estimator-predict-2340906720226119016

Conversation

@guesswh0

Copy link
Copy Markdown
Owner

⚡ Bolt: Vectorize BasicEstimator.predict

💡 What

Vectorized the predict method in BasicEstimator using NumPy matrix operations. The implementation now uses the Euclidean distance expansion formula ($||a-b||^2 = ||a||^2 + ||b||^2 - 2ab$) to calculate distances between all query embeddings and all fitted embeddings in a single pass.

🎯 Why

The original implementation used a Python loop to iterate over each query embedding and calculated distances to fitted embeddings one by one using np.linalg.norm. This introduced significant Python-level overhead, especially as the number of queries or fitted faces increased.

📊 Impact

Measurable performance improvement:

  • ~12x speedup for typical workloads.
  • Reduces prediction time from ~0.24s to ~0.019s for 500 queries against 2000 fitted embeddings.

🔬 Measurement

Run the included benchmark script:

PYTHONPATH=. python3 extra/benchmark_estimator.py

This script compares the original loop-based logic with the new vectorized implementation and reports the speedup.


PR created automatically by Jules for task 2340906720226119016 started by @guesswh0

Vectorized the distance calculation in BasicEstimator.predict using
NumPy matrix operations and the Euclidean distance expansion formula.
This eliminates the per-embedding Python loop and provides a ~12x
speedup for batch predictions.

- Updated fit() and load() to pre-calculate fitted embedding norms.
- Updated predict() to use vectorized matrix operations.
- Added np.maximum(dists_sq, 0) to handle floating-point precision issues.
- Ensured backward compatibility for models loaded from older versions.
- Added a benchmark script in extra/benchmark_estimator.py.

Co-authored-by: guesswh0 <10531675+guesswh0@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.

1 participant