Skip to content

Conversation

@MalyalaKarthik66
Copy link
Contributor

@MalyalaKarthik66 MalyalaKarthik66 commented Nov 15, 2025

Fix: #20949
This PR fixes the Torch backend error where output_padding >= stride results in invalid shapes for ConvTranspose ops.
Changes

  • Added clamping logic in _convert_conv_transpose_padding_args_from_keras_to_torch to ensure output_padding < stride (Torch requirement).

  • Added clear UserWarning when clamping occurs.

  • Added dedicated test: test_output_padding_clamped_for_torch_constraint.

  • This ensures consistent behavior across backends and prevents Torch runtime failures.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @MalyalaKarthik66, 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 addresses a critical compatibility issue with the Torch backend when handling ConvTranspose layers. It ensures that the output_padding parameter, which has specific constraints in Torch (must be less than stride), is automatically adjusted if an invalid value is provided. This prevents runtime failures, maintains consistent behavior across different backends, and provides clear warnings to users when such adjustments occur.

Highlights

  • Torch Backend Fix: Resolved an issue in the Torch backend where output_padding >= stride for ConvTranspose operations resulted in invalid shapes and runtime errors.
  • Clamping Logic: Implemented clamping logic within _convert_conv_transpose_padding_args_from_keras_to_torch to ensure that output_padding always adheres to Torch's requirement of being less than stride.
  • User Notification: Introduced a UserWarning that is triggered when output_padding is clamped, informing the user about the automatic adjustment.
  • New Test Case: Added a dedicated test, test_output_padding_clamped_for_torch_constraint, to verify the correct behavior of the new clamping mechanism.
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 addresses a runtime error in the Torch backend for ConvTranspose layers where output_padding could be greater than or equal to stride. The fix replaces a ValueError with a warning and clamps the output_padding value, which is a good approach to improve user experience and backend consistency. A new test case validates this behavior. My review includes a suggestion to consolidate the clamping logic into a single place for better maintainability and to make it more robust against invalid stride values, which involves removing a redundant block of code.

Comment on lines +99 to +105
warnings.warn(
f"Torch backend requires output_padding < stride. "
f"Clamping output_padding {torch_output_padding} -> {stride - 1} "
f"for stride {stride}.",
UserWarning,
)
torch_output_padding = stride - 1
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This clamping logic is a good improvement. However, the redundant clamping logic later in compute_conv_transpose_padding_args_for_torch (lines 187-201) uses max(0, s - 1), which is safer as it prevents negative padding if stride is less than 1. It would be best to incorporate that safer logic here and remove the redundant code block. Using a temporary variable for the new padding value would also improve the warning message's clarity.

Suggested change
warnings.warn(
f"Torch backend requires output_padding < stride. "
f"Clamping output_padding {torch_output_padding} -> {stride - 1} "
f"for stride {stride}.",
UserWarning,
)
torch_output_padding = stride - 1
new_output_padding = max(0, stride - 1)
warnings.warn(
f"Torch backend requires output_padding < stride. "
f"Clamping output_padding {torch_output_padding} -> {new_output_padding} "
f"for stride {stride}.",
UserWarning,
)
torch_output_padding = new_output_padding

Comment on lines +187 to +201
# --- FIX FOR TORCH CONSTRAINT: output_padding < stride ---
corrected_output_paddings = []
for s, op in zip(
strides
if isinstance(strides, (list, tuple))
else [strides] * num_spatial_dims,
torch_output_paddings,
):
max_allowed = max(0, s - 1)
if op > max_allowed:
corrected_output_paddings.append(max_allowed)
else:
corrected_output_paddings.append(op)

torch_output_paddings = corrected_output_paddings
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

With the clamping logic now handled within _convert_conv_transpose_padding_args_from_keras_to_torch (and improved with the suggestion in the other comment), this entire block of code becomes redundant. Removing it will simplify the function and avoid logic duplication.

@codecov-commenter
Copy link

codecov-commenter commented Nov 15, 2025

Codecov Report

❌ Patch coverage is 77.77778% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.47%. Comparing base (edbf8f5) to head (0f9fd4d).

Files with missing lines Patch % Lines
keras/src/backend/common/backend_utils.py 77.77% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master   #21852   +/-   ##
=======================================
  Coverage   82.47%   82.47%           
=======================================
  Files         577      577           
  Lines       59508    59516    +8     
  Branches     9332     9334    +2     
=======================================
+ Hits        49080    49088    +8     
  Misses       8015     8015           
  Partials     2413     2413           
Flag Coverage Δ
keras 82.30% <77.77%> (+<0.01%) ⬆️
keras-jax 62.90% <77.77%> (+<0.01%) ⬆️
keras-numpy 57.56% <77.77%> (+<0.01%) ⬆️
keras-openvino 34.35% <77.77%> (+<0.01%) ⬆️
keras-tensorflow 64.13% <77.77%> (+<0.01%) ⬆️
keras-torch 63.61% <77.77%> (+<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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

3 participants