-
Notifications
You must be signed in to change notification settings - Fork 19.6k
Support scalar view for tf backend. #21802
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
Summary of ChangesHello @wenyi-guo, 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 enhances the 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
|
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 adds support for scalar views in the TensorFlow backend by modifying the view operation. The changes correctly handle scalar inputs, which would previously cause an error. The logic has been updated to check item sizes for scalars and ensure compatibility. Additionally, new tests have been added to verify this new functionality for scalar inputs. While the fix for scalars is correct, the logic for tensors with a zero-sized last dimension has been made overly restrictive. I've provided a suggestion to correct this, making the behavior consistent with other backends for such edge cases.
| old_shape = list(shape_op(x)) | ||
| last_dim_size = old_shape[-1] if len(old_shape) > 0 else 0 | ||
| if (last_dim_size == 0 and old_itemsize != new_itemsize) or ( | ||
| last_dim_size * old_itemsize % new_itemsize != 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.
The current logic to handle scalars and tensors with a last dimension of size 0 is a bit too broad. The condition last_dim_size == 0 is true for both scalars and tensors with a shape like (2, 0). For scalars, it's correct to only allow views if the item size is the same. However, for tensors with a zero-sized last dimension, this is too restrictive. For example, numpy.zeros((2,0), 'int16').view('int8') is a valid operation, but this implementation would reject it.
A better approach is to explicitly distinguish between a scalar tensor (ndim == 0) and a tensor with a zero-sized dimension. This will make the behavior consistent with NumPy for this edge case.
old_shape = list(shape_op(x))
is_scalar = not old_shape
last_dim_size = old_shape[-1] if not is_scalar else 0
if (is_scalar and old_itemsize != new_itemsize) or (
not is_scalar and last_dim_size * old_itemsize % new_itemsize != 0
):
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #21802 +/- ##
==========================================
- Coverage 82.63% 76.85% -5.78%
==========================================
Files 577 577
Lines 59415 59415
Branches 9313 9313
==========================================
- Hits 49097 45663 -3434
- Misses 7913 11302 +3389
- Partials 2405 2450 +45
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:
|
Support scalar view for tf backend. Only works if new dtype item size is same as old dtype. This is same implementation as other backends.