Skip to content

Conversation

@shashaka
Copy link
Collaborator

Adds keras.ops.empty_like, which creates a new tensor with the same shape and dtype as the input tensor, with uninitialized values.
Supported across NumPy, TensorFlow, PyTorch, and JAX backends.
Not supported on OpenVINO.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @shashaka, 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 Keras operations API by introducing the empty_like function. This addition provides a consistent and backend-agnostic way for users to create uninitialized tensors that mirror the shape and data type of an existing tensor. This is crucial for operations where memory allocation is needed without immediate value assignment, aligning Keras with common tensor manipulation patterns found in other deep learning libraries.

Highlights

  • New Function Added: Introduced keras.ops.empty_like, a new function that creates a tensor with the same shape and data type as a given input tensor, but with uninitialized values.
  • Backend Support: The empty_like function is implemented and supported across NumPy, TensorFlow, PyTorch, and JAX backends, ensuring multi-backend compatibility.
  • OpenVINO Exclusion: Explicitly marked empty_like as not supported for the OpenVINO backend, raising a NotImplementedError if attempted, and added its tests to the exclusion list.
  • API Integration and Testing: The new function is integrated into the Keras API structure, including keras.ops and keras.ops.numpy namespaces, and comprehensive unit tests have been added to verify its behavior for both symbolic and concrete tensors.
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.

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 introduces the empty_like operation. The overall structure and API definition are good, but there are some issues with the backend implementations. Specifically, the dtype handling when it is not provided is incorrect across all backends; it should default to the input tensor's dtype, not config.floatx(). I've provided suggestions to fix this. Additionally, the docstring for the new operation is missing a code example, which is required by the Keras API guidelines. Finally, the tests for empty_like are not comprehensive enough to catch the dtype inference bug, and I've suggested an additional test case to cover this.

@shashaka
Copy link
Collaborator Author

/gemini review

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 introduces the keras.ops.empty_like function, providing implementations for NumPy, JAX, TensorFlow, and PyTorch backends, while correctly marking it as not supported for OpenVINO. The changes are well-structured and include corresponding tests.

My main feedback is regarding the TensorFlow backend implementation, which currently uses tf.zeros_like. This creates a zero-initialized tensor, which is inconsistent with the function's documentation and its behavior in other backends. I've suggested using tf.empty_like to ensure correctness and consistency.

@hertschuh
Copy link
Collaborator

@shashaka

May I ask what the rationale is for adding this op. Is the hope to get better performance than zeros_like?

JAX implements empty_like as zeros_like per documentation. The TensorFlow implementation uses zeros_like. So only the Torch backend might benefit from this under a set of specific conditions.

Also note that on JAX and TensorFlow, the compiler will be smart enough under some conditions to not materialize the array at all, which is even more performant than not initializing the values.

@shashaka
Copy link
Collaborator Author

@hertschuh
Thanks for the review.
You are absolutely right. empty_like does not provide real performance improvements for most backends since both JAX and TensorFlow handle it in the same way as zeros_like.

My main motivation was API consistency with NumPy to make it easier for users porting NumPy code into keras.ops.
However, since the practical backend benefit is minimal, I agree that it is not essential at this point.

I will go ahead and close this PR. Thank you again for the detailed explanation and feedback.

@shashaka shashaka closed this Nov 11, 2025
@hertschuh
Copy link
Collaborator

Actually, after I wrote this comment, I noticed that keras already implements empty. So, for consistency, yes, let's also add empty_like.

Please re-open and sorry for the churn.

@hertschuh hertschuh reopened this Nov 11, 2025
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

@codecov-commenter
Copy link

codecov-commenter commented Nov 11, 2025

Codecov Report

❌ Patch coverage is 89.65517% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.66%. Comparing base (4d30a7f) to head (5b03efc).

Files with missing lines Patch % Lines
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 ⚠️
keras/src/ops/numpy.py 92.85% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master   #21840   +/-   ##
=======================================
  Coverage   82.66%   82.66%           
=======================================
  Files         577      577           
  Lines       59477    59506   +29     
  Branches     9329     9330    +1     
=======================================
+ Hits        49167    49193   +26     
- Misses       7907     7910    +3     
  Partials     2403     2403           
Flag Coverage Δ
keras 82.49% <89.65%> (+<0.01%) ⬆️
keras-jax 63.31% <68.96%> (+<0.01%) ⬆️
keras-numpy 57.55% <68.96%> (+<0.01%) ⬆️
keras-openvino 34.35% <55.17%> (+0.01%) ⬆️
keras-tensorflow 64.12% <68.96%> (+<0.01%) ⬆️
keras-torch 63.61% <75.86%> (+<0.01%) ⬆️

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 addition!

@google-ml-butler google-ml-butler bot added kokoro:force-run ready to pull Ready to be merged into the codebase labels Nov 11, 2025
@hertschuh hertschuh merged commit bfde12b into keras-team:master Nov 11, 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 Nov 11, 2025
@shashaka shashaka deleted the empty_like branch November 12, 2025 00:15
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