Skip to content

⚡ Bolt: Vectorize BasicEstimator.predict for ~22x speedup#36

Open
guesswh0 wants to merge 1 commit into
masterfrom
bolt-vectorize-basic-estimator-6139538611339517072
Open

⚡ Bolt: Vectorize BasicEstimator.predict for ~22x speedup#36
guesswh0 wants to merge 1 commit into
masterfrom
bolt-vectorize-basic-estimator-6139538611339517072

Conversation

@guesswh0
Copy link
Copy Markdown
Owner

Vectorized the BasicEstimator.predict method in face_engine/models/basic_estimator.py to significantly improve performance.

Key changes:

  • Updated __init__ to initialize fitted_norms_sq.
  • Modified fit to pre-calculate squared norms of the fitted embeddings.
  • Vectorized predict using the formula $||a - b||^2 = ||a||^2 + ||b||^2 - 2ab$ to calculate distances in a single matrix operation.
  • Added a load method update for backward compatibility with older serialized models.
  • Included numerical stability guards and input validation.

Performance improvement: ~22x speedup for 500 query embeddings against 2000 stored embeddings.


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

💡 What:
Replaced the Python loop in `BasicEstimator.predict` with a vectorized NumPy implementation using the squared distance expansion formula. Added pre-calculation of fitted embedding norms in the `fit` method.

🎯 Why:
The previous implementation performed distance calculations in a loop, which is extremely inefficient for large batches of query or fitted embeddings (O(M*N*D) in Python).

📊 Impact:
Reduces prediction time by ~95% (~22x speedup). Benchmarked at ~0.014s (down from ~0.32s) for 500 query vs 2000 fitted embeddings.

🔬 Measurement:
Verified via `benchmark_basic_estimator_v2.py` (now deleted) comparing results and timing against the original implementation. Core tests in `tests/test_face_engine_models.py` pass.

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