- 
                Notifications
    You must be signed in to change notification settings 
- Fork 19.6k
Make confusion metrics compilable. #21775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
By removing the use of `ops.nonzero` which returns an array of non-predetermined size. Follow-up to https://github.com/keras-team/keras/pull/21765/files#r2462099871 Fixes keras-team#19376
| Summary of ChangesHello @hertschuh, 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 addresses a critical compilation issue within Keras's confusion metrics, specifically when utilizing the JAX backend. The problem stemmed from the use of  Highlights
 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
 | 
| Thanks! | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request refactors the confusion matrix-based metrics to make them JAX-compilable by removing the usage of ops.nonzero. The new implementation in _find_max_under_constraint uses a masking approach which is a good, JIT-compatible pattern. I've suggested a minor refinement to use ops.where for potentially better readability. The associated cleanup in the JAX backend, which removes now-unnecessary special handling for nonzero, is also a good improvement. Additionally, a typo in a docstring was fixed. Overall, these are solid changes that improve the codebase.
| return ops.max( | ||
| ops.multiply(dependent, ops.cast(feasible, dependent.dtype)), | ||
| initial=0, | ||
| ) | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This implementation is correct and makes the function JIT-compatible. For slightly improved readability, you could consider using ops.where to explicitly mask the dependent tensor. This avoids the implicit boolean-to-float conversion from ops.cast and can make the intent clearer.
| return ops.max( | |
| ops.multiply(dependent, ops.cast(feasible, dependent.dtype)), | |
| initial=0, | |
| ) | |
| return ops.max( | |
| ops.where(feasible, dependent, 0.0), | |
| initial=0, | |
| ) | 
| Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@            Coverage Diff             @@
##           master   #21775      +/-   ##
==========================================
- Coverage   82.64%   82.63%   -0.01%     
==========================================
  Files         577      577              
  Lines       59254    59249       -5     
  Branches     9292     9291       -1     
==========================================
- Hits        48968    48963       -5     
  Misses       7903     7903              
  Partials     2383     2383              
 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:
 | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
| 
 Ah I'm sorry! I was too gung ho with my fix, thank you for doing this! | 
By removing the use of
ops.nonzerowhich returns an array of non-predetermined size.Follow-up to https://github.com/keras-team/keras/pull/21765/files#r2462099871
Fixes #19376