Skip to content

Conversation

@danielenricocahall
Copy link
Contributor

@danielenricocahall danielenricocahall commented Oct 29, 2025

Addressing issue detailed in #19753 where Torch and OpenVINO properly raise an exception when a kernel is bigger than the input but Tensorflow, Numpy, and Jax do not.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @danielenricocahall, 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 bug in the Keras TensorFlow backend where convolution operations with kernels larger than the input would silently produce zero-dimensional outputs instead of raising an error. By introducing an explicit ValueError check, the behavior of the TensorFlow backend now aligns with PyTorch, ensuring consistent and predictable error handling across different Keras backends for such edge cases.

Highlights

  • TensorFlow Convolution Error Handling: Implemented an explicit check within the TensorFlow backend's conv function to raise a ValueError if the convolution operation results in an output with a zero-sized dimension. This addresses an inconsistency where TensorFlow previously produced zero-sized outputs silently, unlike PyTorch which raises an exception.
  • New Test Case: Added a new test test_conv_raises_exception_on_zero_dims to conv_test.py to verify that the Conv2D layer correctly raises a ValueError when a kernel larger than the input leads to zero-dimensional output.
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 aims to fix a bug in the TensorFlow backend where a convolution with a kernel larger than the input does not raise an error, unlike other backends like Torch. The proposed solution correctly adds a check to raise a ValueError and includes a corresponding unit test. However, the current implementation only handles cases with static input shapes and will not work for dynamic shapes, which is a common use case in Keras. I've provided suggestions to make the check robust for dynamic shapes by using tf.Assert and adjusting the test to be more backend-agnostic regarding the specific exception type.

danielenricocahall and others added 4 commits October 28, 2025 22:51
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@danielenricocahall danielenricocahall changed the title Address bug with convolution using Tensorflow backend Address bug with convolution using Tensorflow, Numpy, Jax backends Oct 29, 2025
@danielenricocahall
Copy link
Contributor 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 addresses a bug where convolution operations in the Tensorflow, Numpy, and Jax backends would silently produce an empty output if the kernel size was larger than the input size. The changes introduce checks in each backend to raise an exception in such cases, which is the correct behavior. A corresponding test case is also added to verify the fix. The implementation is solid. I have one suggestion to improve the consistency of the error message in the TensorFlow backend to make it more general and helpful for users, in line with the other backends.

@danielenricocahall
Copy link
Contributor Author

/gemini summary

@gemini-code-assist
Copy link
Contributor

Summary of Changes

This pull request addresses a consistency bug across Keras backends where convolution operations with a kernel larger than the input would silently produce zero-dimensional outputs in TensorFlow, NumPy, and JAX. The changes introduce explicit error checks to these backends, ensuring that a ValueError or tf.errors.InvalidArgumentError is raised in such scenarios. This standardization improves error reporting and user feedback, preventing silent failures and making the behavior consistent with other backends like Torch and OpenVINO.

Highlights

  • Standardized Error Handling for Convolution: Implemented explicit checks in the JAX, NumPy, and TensorFlow backends to raise an error when a convolution operation results in an empty output, aligning their behavior with Torch and OpenVINO.
  • TensorFlow Backend Specifics: For the TensorFlow backend, tf.Assert is now used within the conv function to dynamically check for zero-sized output dimensions, ensuring robust error detection for both static and dynamic input shapes.
  • New Test Case for Empty Convolution Output: A new test, test_conv_raises_exception_on_zero_dims, has been added to conv_test.py to verify that the Conv2D layer correctly raises an exception when a convolution kernel is too large for the input, leading to an empty output.
Changelog
  • keras/src/backend/jax/nn.py
    • Modified the conv function to store the result of jax.lax.conv_general_dilated in a variable.
    • Added a conditional check: if result.size == 0, a ValueError is raised with a descriptive message.
  • keras/src/backend/numpy/nn.py
    • Modified the conv function to store the result of jax.lax.conv_general_dilated (wrapped in np.array) in a variable.
    • Added a conditional check: if result.size == 0, a ValueError is raised with a descriptive message.
  • keras/src/backend/tensorflow/nn.py
    • Modified the _conv inner function to store the result of tf.nn.convolution in a variable.
    • Introduced tf.Assert to check if all dimensions of the result_shape are greater than zero.
    • The tf.Assert includes a detailed error message that provides context about the empty output and suggests checking input and convolution parameters.
    • The result is returned within a tf.control_dependencies block to ensure the assertion is executed.
  • keras/src/layers/convolutional/conv_test.py
    • Added a new test method test_conv_raises_exception_on_zero_dims.
    • This test creates a Conv2D layer with a kernel larger than the input and asserts that calling the layer with this input raises a generic Exception, accommodating different backend-specific exception types.
Activity
  • gemini-code-assist[bot] provided an initial summary of changes.
  • danielenricocahall requested a review from gemini-code-assist[bot].
  • danielenricocahall requested a summary from gemini-code-assist[bot].
  • gemini-code-assist[bot] suggested using tf.Assert for dynamic shape checking in the TensorFlow backend, which was incorporated.
  • gemini-code-assist[bot] recommended catching a generic Exception in the new test case for backend-agnosticism, which was incorporated.
  • gemini-code-assist[bot] proposed a more general and consistent error message for the empty output condition, which was incorporated.

@codecov-commenter
Copy link

codecov-commenter commented Oct 29, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.63%. Comparing base (c2bc6cf) to head (877a2ea).
⚠️ Report is 12 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #21796      +/-   ##
==========================================
- Coverage   82.63%   82.63%   -0.01%     
==========================================
  Files         577      577              
  Lines       59316    59425     +109     
  Branches     9300     9316      +16     
==========================================
+ Hits        49018    49107      +89     
- Misses       7910     7913       +3     
- Partials     2388     2405      +17     
Flag Coverage Δ
keras 82.46% <100.00%> (-0.01%) ⬇️
keras-jax 63.32% <30.76%> (-0.01%) ⬇️
keras-numpy 57.56% <30.76%> (+0.01%) ⬆️
keras-openvino 34.30% <0.00%> (+<0.01%) ⬆️
keras-tensorflow 64.12% <38.46%> (+<0.01%) ⬆️
keras-torch 63.62% <0.00%> (-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 fix!

Comment on lines 321 to 334
result_shape = tf.shape(result)
assertion = tf.Assert(
tf.reduce_all(result_shape > 0),
[
"The convolution operation resulted in an empty output. "
"Output shape:",
result_shape,
". This can happen if the input is too small for the given "
"kernel size, strides, dilation rate, and padding mode. "
"Please check the input shape and convolution parameters.",
],
)
with tf.control_dependencies([assertion]):
return tf.identity(result)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I appreciate supporting dynamic shapes too, but I hear this type of dynamic asserts can be expensive. So let's only cover the statically known shape case (which should cover the vast majority of uses).

result_shape = result.shape
if result_shape.is_fully_defined() and math.prod(result_shape.as_list()) == 0:
    raise ValueError(
    ...
    )
return result

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done! Gemini's suggestion was good but maybe overzealous :)

@google-ml-butler google-ml-butler bot added kokoro:force-run ready to pull Ready to be merged into the codebase labels Oct 31, 2025
@hertschuh hertschuh merged commit 6d06085 into keras-team:master Oct 31, 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 31, 2025
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