Skip to content

Conversation

@ssam18
Copy link
Contributor

@ssam18 ssam18 commented Nov 19, 2025

Summary

Fixes issue #20914 where ops.tile returns all-None shapes when called inside a Layer's call method with concrete integer repeats on the TensorFlow backend.

Problem

When ops.tile was called inside a Layer with concrete integer repeats like [1, 2, 1, 1], the TensorFlow backend was converting those repeats to a tensor, which prevented TensorFlow's shape inference from properly determining the output shape. This resulted in all dimensions being None.

For example:

class TileLayer(layers.Layer):
    def call(self, x):
        return ops.tile(x, [1, 2, 1, 1])

x = Input(shape=(3, 2, 2))
result = TileLayer()(x)
# Before fix: result.shape = (None, None, None, None)
# After fix:  result.shape = (None, 6, 2, 2)

Solution

  1. TensorFlow Backend: Modified tile() to detect when repeats contains only concrete integer values and pass them directly to tf.tile as a Python list instead of converting to a tensor. This allows TensorFlow's shape inference to work correctly.

  2. Symbolic Ops: Enhanced Tile.compute_output_spec() to handle symbolic repeat values more gracefully by checking if each repeat is a concrete integer before attempting multiplication.

  3. Tests: Added regression tests to verify shape inference works correctly both in direct ops.tile calls and when used inside Layer.call().

Testing

  • Added test case in NumpyOneInputOpsDynamicShapeTest.test_tile for multi-dimensional inputs
  • Added new test test_tile_shape_inference_in_layer specifically for the Layer use case
  • Verified both TensorFlow and JAX backends work correctly
  • All existing tests continue to pass

Fixes #20914

When ops.tile is called inside a Layer's call method with concrete
integer repeats, the TensorFlow backend was converting those repeats
to a tensor, which prevented TensorFlow's shape inference from properly
determining the output shape. This resulted in all-None shapes.

Changes:
1. Modified TensorFlow backend's tile() to detect when repeats contains
   only concrete integer values and pass them directly to tf.tile as a
   Python list/tuple instead of converting to a tensor. This allows
   TensorFlow's shape inference to work correctly.

2. Enhanced ops.numpy.Tile.compute_output_spec() to handle symbolic
   repeat values more gracefully by checking if each repeat is a
   concrete integer before attempting multiplication.

3. Added regression tests to verify shape inference works correctly
   both in direct ops.tile calls and when used inside Layer.call().

Fixes keras-team#20914

Signed-off-by: Samaresh Kumar Singh <[email protected]>
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @ssam18, 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 resolves a critical shape inference bug in ops.tile when operating on the TensorFlow backend, particularly within Keras Layers. Previously, using concrete integer repeat values would lead to undefined output shapes. The changes ensure that tf.tile receives concrete repeat values in a format it can use for accurate shape inference, and the symbolic shape computation logic is also improved to handle these cases correctly.

Highlights

  • TensorFlow ops.tile Shape Inference Fix: Addresses an issue where ops.tile on the TensorFlow backend would produce all-None output shapes when used within a Keras Layer with concrete integer repeats.
  • Concrete Repeats Handling in TensorFlow Backend: The TensorFlow backend's tile() implementation now detects concrete integer repeats and passes them as a Python list to tf.tile, enabling correct shape inference.
  • Improved Symbolic Shape Computation: Enhanced Tile.compute_output_spec() to correctly infer output shapes by explicitly checking if each repeat value is a concrete integer, even when dealing with symbolic inputs.
  • New Regression Tests: New test cases have been added to validate the fix for multi-dimensional inputs and specifically for ops.tile usage within Keras Layers, ensuring proper shape inference.
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 effectively addresses the shape inference issue with ops.tile on the TensorFlow backend, particularly when used within a Layer. The approach of detecting concrete integer repeats and passing them as a Python list to tf.tile is a solid solution that correctly enables static shape inference. The corresponding adjustments in Tile.compute_output_spec to handle symbolic repeats more gracefully are also well-implemented. The inclusion of targeted regression tests is a great addition that ensures the fix is robust and prevents future regressions.

My only suggestion is to refine the exception handling in the TensorFlow backend tile function to be more specific, which will improve maintainability and debugging.

@codecov-commenter
Copy link

codecov-commenter commented Nov 19, 2025

Codecov Report

❌ Patch coverage is 75.58140% with 21 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.56%. Comparing base (edbf8f5) to head (61e4526).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
keras/src/ops/numpy.py 0.00% 5 Missing and 2 partials ⚠️
keras/src/backend/tensorflow/numpy.py 77.27% 3 Missing and 2 partials ⚠️
keras/api/_tf_keras/keras/initializers/__init__.py 0.00% 3 Missing ⚠️
keras/api/_tf_keras/keras/applications/__init__.py 0.00% 2 Missing ⚠️
...pi/_tf_keras/keras/applications/resnet/__init__.py 0.00% 1 Missing ⚠️
..._tf_keras/keras/applications/resnet_v2/__init__.py 0.00% 1 Missing ⚠️
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   #21860      +/-   ##
==========================================
+ Coverage   82.47%   82.56%   +0.08%     
==========================================
  Files         577      577              
  Lines       59508    59597      +89     
  Branches     9332     9355      +23     
==========================================
+ Hits        49080    49206     +126     
+ Misses       8015     7982      -33     
+ Partials     2413     2409       -4     
Flag Coverage Δ
keras 82.37% <75.58%> (+0.08%) ⬆️
keras-jax 62.83% <63.95%> (-0.07%) ⬇️
keras-numpy 57.49% <63.95%> (-0.07%) ⬇️
keras-openvino 34.31% <55.81%> (-0.04%) ⬇️
keras-tensorflow 64.39% <75.58%> (+0.26%) ⬆️
keras-torch 63.53% <63.95%> (-0.08%) ⬇️

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.

Run api_gen.py to regenerate API directory with properly formatted
imports and ordering. This is required by the pre-commit hook.

Signed-off-by: Samaresh Kumar Singh <[email protected]>
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.

Inconsistent behaviours between backends on ops.tile

3 participants