⚡ perf: Optimize simhash bit generation and assembly#83
Conversation
Replaced inefficient nested Python loops for token bit generation and extraction in `_simhash_impl` with vectorized NumPy operations and `np.packbits`. Benchmarks indicate an ~12.5x speedup locally (3.14s -> 0.25s). Correctness is preserved by casting bit counts to `np.int32` before summing to properly handle negatives during the boolean vote. Co-authored-by: SuarezPM <110942776+SuarezPM@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. |
💡 **What:** Replaced the highly nested Python loops inside `_simhash_impl` with vectorized NumPy operations and fast bit-packing using `np.packbits` and `int.from_bytes()`. 🎯 **Why:** The existing implementation iterated bitwise 64 times for every token purely in Python, causing enormous performance degradation during scaling or long sequences. 📊 **Measured Improvement:** Utilizing an internal isolated benchmark with 1000 random token ids over 100 loops, execution time decreased from ~3.14s to ~0.25s, achieving a ~12.5x speedup while preserving bit-exact matching. Signed-off-by: Jules <jules@example.com> Co-authored-by: SuarezPM <110942776+SuarezPM@users.noreply.github.com>
💡 **What:** Replaced the highly nested Python loops inside `_simhash_impl` with vectorized NumPy operations and fast bit-packing using `np.packbits` and `int.from_bytes()`. 🎯 **Why:** The existing implementation iterated bitwise 64 times for every token purely in Python, causing enormous performance degradation during scaling or long sequences. 📊 **Measured Improvement:** Utilizing an internal isolated benchmark with 1000 random token ids over 100 loops, execution time decreased from ~3.14s to ~0.25s, achieving a ~12.5x speedup while preserving bit-exact matching. Signed-off-by: Jules <jules@example.com> Co-authored-by: SuarezPM <110942776+SuarezPM@users.noreply.github.com>
💡 **What:** Replaced the highly nested Python loops inside `_simhash_impl` with vectorized NumPy operations and fast bit-packing using `np.packbits` and `int.from_bytes()`. 🎯 **Why:** The existing implementation iterated bitwise 64 times for every token purely in Python, causing enormous performance degradation during scaling or long sequences. 📊 **Measured Improvement:** Utilizing an internal isolated benchmark with 1000 random token ids over 100 loops, execution time decreased from ~3.14s to ~0.25s, achieving a ~12.5x speedup while preserving bit-exact matching. Signed-off-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: SuarezPM <110942776+SuarezPM@users.noreply.github.com>
Applies Jules PR #83 (uv.lock/log churn dropped). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Signed-off-by: Pablo <suarezpm@csnat.unt.edu.ar>
|
Applied to |
Acknowledged. |
PR #83 vectorized _simhash_impl in embeddings/__init__.py, but that module's EmbeddingEngine is a dead duplicate (no importer; the 3 real consumers use embeddings.embedding_engine). This ports the numpy/packbits impl to the LIVE embedding_engine._simhash_impl where simhash actually runs. Output verified identical to the old loop; ~15x faster on CPU (16-4096 tokens, Ryzen 5 3600). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Signed-off-by: Pablo <suarezpm@csnat.unt.edu.ar>
💡 What: Replaced the highly nested Python loops inside
_simhash_implwith vectorized NumPy operations and fast bit-packing usingnp.packbitsandint.from_bytes().🎯 Why: The existing implementation iterated bitwise 64 times for every token purely in Python, causing enormous performance degradation during scaling or long sequences.
📊 Measured Improvement: Utilizing an internal isolated benchmark with 1000 random token ids over 100 loops, execution time decreased from ~3.14s to ~0.25s, achieving a ~12.5x speedup while preserving bit-exact matching.
PR created automatically by Jules for task 18412311951160534470 started by @SuarezPM