Vectorize scatter operation in NumPy backend#22218
Vectorize scatter operation in NumPy backend#222180xRozier wants to merge 3 commits intokeras-team:masterfrom
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Summary of ChangesHello @0xRozier, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the performance of the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This is an excellent change that significantly improves the performance of the scatter operation in the NumPy backend. By replacing the Python for loop with the vectorized np.add.at function, the implementation becomes much more efficient, idiomatic, and concise. The change correctly maintains the behavior of accumulating values for duplicate indices and aligns well with similar patterns found in scatter_update within the same file. The performance gains described are substantial, making this a valuable optimization.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #22218 +/- ##
==========================================
+ Coverage 82.89% 82.90% +0.01%
==========================================
Files 593 594 +1
Lines 64169 65843 +1674
Branches 10073 10292 +219
==========================================
+ Hits 53192 54589 +1397
- Misses 8385 8638 +253
- Partials 2592 2616 +24
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Replace the Python for-loop in `scatter()` with NumPy's `np.add.at` for vectorized index accumulation. This yields ~87x speedup for large-scale scatter operations (e.g. 10^6 updates). Fixes keras-team#22208
Sort the axis list in `RMSNormalization.build()` and in `_rms_normalization()` so that unsorted axes like `[-1, -2]` produce the same `normalized_shape` and scale shape as `[-2, -1]`. Adds a test covering unsorted contiguous axes.
ab20f3a to
89021f4
Compare
|
@hertschuh, if you can take a quick look, it would be great (take your time tho, I'm not in a rush) |
|
There seems to be an unrelated fix with RMS normalization, should that be a separate PR? |
|
You're right, the RMS normalization fix is unrelated. I can remove it from this PR and submit it as a separate one — let me know if you'd prefer that. For context: it addresses a minor bug where passing unsorted axes (e.g. |
Yes, please separate the RMSNormalization fix and remove it from this PR. For one thing, there are already 2 other PRs addressing the same RMSNormalization issue. |
…computation" This reverts commit 89021f4.
|
Done — I've removed the RMSNormalization fix from this PR. It now only contains the scatter vectorization change. |
Summary
forloop inscatter()with NumPy's vectorizednp.add.at, yielding ~87x speedup for large-scale scatter operations (e.g. 10^6 updates)scatter_update()in the same fileDetails
The current implementation iterates through each index with a Python loop:
This bypasses NumPy's internal C-optimized loops. The fix replaces it with:
np.add.atcorrectly handles duplicate indices via cumulative addition, maintaining full compatibility with existing behavior.Benchmark (1M updates on a 1000x1000 array):
Fixes #22208
Test plan
CoreOpsDynamicShapeTest,CoreOpsStaticShapeTest,CoreOpsCorrectnessTest)