Skip to content

Conversation

@wenyi-guo
Copy link
Collaborator

Support view() on backeds numpy, jax, tensorflow, pytorch.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @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 integrates a view() operation into keras.ops, allowing users to reinterpret the underlying data of a tensor as a new data type without altering its bitwise content. This feature is crucial for advanced data manipulation and interoperability, providing a consistent API across various backends like NumPy, JAX, TensorFlow, and PyTorch, albeit with some backend-specific limitations for the type argument.

Highlights

  • New keras.ops.view() function: Introduces a new operation to create a bitwise view of tensor data with a different dtype without copying the underlying data.
  • Multi-backend support: The view() function is implemented for NumPy, JAX, TensorFlow, and PyTorch backends, providing consistent functionality across the Keras ecosystem.
  • type argument limitation: The type argument for view() is currently only fully supported by the NumPy backend; TensorFlow and PyTorch backends will raise a NotImplementedError if this argument is used.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@wenyi-guo wenyi-guo changed the title Suuport keras.op.view() to view the same data bitwise at a new dtype Suport keras.op.view() to view the same data bitwise at a new dtype Oct 22, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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 keras.op.view across multiple backends. The implementation is mostly correct, but there are a few issues to address. The most critical one is an incorrect shape inference logic in the symbolic View operation, which fails to account for shape changes when the data type's item size changes. This could lead to errors in model construction. I've also pointed out that the test suite for this new operation is not comprehensive enough, as it misses this important shape-changing scenario. Finally, there's a minor simplification possible in the NumPy backend implementation. Addressing these points will make the new feature more robust and maintainable.

@codecov-commenter
Copy link

codecov-commenter commented Oct 23, 2025

Codecov Report

❌ Patch coverage is 82.08955% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.63%. Comparing base (47fcb39) to head (28c460f).
⚠️ Report is 9 commits behind head on master.

Files with missing lines Patch % Lines
keras/src/ops/numpy.py 76.92% 3 Missing and 3 partials ⚠️
keras/src/backend/tensorflow/numpy.py 84.61% 2 Missing and 2 partials ⚠️
keras/api/_tf_keras/keras/ops/__init__.py 0.00% 1 Missing ⚠️
keras/api/_tf_keras/keras/ops/numpy/__init__.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #21763      +/-   ##
==========================================
- Coverage   82.69%   82.63%   -0.06%     
==========================================
  Files         573      577       +4     
  Lines       58888    59316     +428     
  Branches     9218     9300      +82     
==========================================
+ Hits        48696    49018     +322     
- Misses       7845     7910      +65     
- Partials     2347     2388      +41     
Flag Coverage Δ
keras 82.46% <82.08%> (-0.04%) ⬇️
keras-jax 63.32% <41.79%> (+0.08%) ⬆️
keras-numpy 57.56% <41.79%> (-0.17%) ⬇️
keras-openvino 34.29% <16.41%> (-0.11%) ⬇️
keras-tensorflow 64.11% <70.14%> (+0.09%) ⬆️
keras-torch 63.62% <43.28%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Collaborator

@hertschuh hertschuh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

A few things for consistency with other ops.

Copy link
Collaborator

@hertschuh hertschuh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last nitpick about the unit test.

Copy link
Collaborator

@hertschuh hertschuh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed a couple small things earlier.

@google-ml-butler google-ml-butler bot added kokoro:force-run ready to pull Ready to be merged into the codebase labels Oct 25, 2025
@hertschuh hertschuh merged commit c2bc6cf into keras-team:master Oct 25, 2025
11 checks passed
@google-ml-butler google-ml-butler bot removed awaiting review ready to pull Ready to be merged into the codebase labels Oct 25, 2025
@wenyi-guo wenyi-guo deleted the op-view branch October 30, 2025 23:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants