Skip to content

fix: validate filter and out_backprop dimensions in Conv2DBackpropInput to prevent crash on invalid rank - #125946

Merged
copybara-service[bot] merged 1 commit into
tensorflow:masterfrom
adi-IL:fix/conv2d-transpose-filter-rank-validation
Sep 29, 2026
Merged

copybara-service[bot] merged 1 commit into
tensorflow:masterfrom
adi-IL:fix/conv2d-transpose-filter-rank-validation

Conversation

@adi-IL

@adi-IL adi-IL commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

Fixes #125506

Summary of Changes

  1. Root Cause:

    • In tensorflow/core/kernels/conv_grad_input_ops.h, Conv2DBackpropInputOp::Compute and Conv2DCustomBackpropInputOp::Compute checked out_backprop.dims() == 4 (with a typo mentioning input_sizes), but did not validate filter.dims() == 4.
    • When a scalar or invalid rank filter (such as filters=8) was passed to tf.nn.conv2d_transpose or tf.raw_ops.Conv2DBackpropInput, querying filter_shape.dim_size(2) or filter_shape.dim_size(3) in Conv2DBackpropComputeInputShape (tensorflow/core/kernels/conv_grad_shape_utils.cc) and Compute triggered a fatal check failure abort (Check failed: d < dims() (2 vs. 0)).
    • Furthermore, Conv2DBackpropComputeInputShape lacked validation that filter_shape and out_backprop_shape were 4-dimensional when input_sizes contained 2 spatial dimensions before indexing filter_shape.dim_size(2).
  2. Fix:

    • Added explicit OP_REQUIRES(context, filter.dims() == 4, ...) validation in Conv2DBackpropInputOp::Compute and Conv2DCustomBackpropInputOp::Compute (conv_grad_input_ops.h).
    • Corrected the diagnostic string on out_backprop dimension validation to state out_backprop must be 4-dimensional.
    • Added shape dimension validation in Conv2DBackpropComputeInputShape (conv_grad_shape_utils.cc) ensuring filter_shape and out_backprop_shape have rank 4 before indexing depth dimensions when input_sizes.dim_size(0) == 2.
    • Added rank validation on filters in conv2d_transpose_v2 (tensorflow/python/ops/nn_ops.py), raising a clear ValueError when non-4D filters are passed.
    • Added unit tests testConv2DTransposeInvalidFilterRank and testConv2DTransposeInvalidOutputShapeRank in tensorflow/python/kernel_tests/nn_ops/conv2d_transpose_test.py.

Validation

  • Verified rank validation logic and error raising in unit tests.

@google-ml-butler google-ml-butler Bot added the size:M CL Change Size: Medium label Aug 22, 2026
@google-ml-butler
google-ml-butler Bot requested a review from cantonios August 22, 2026 22:28
@google-ml-butler google-ml-butler Bot added the awaiting review Pull request awaiting review label Aug 22, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

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 shape validation checks to ensure that filter and out_backprop tensors are 4-dimensional in Conv2D backprop operations, along with corresponding unit tests. The review feedback suggests moving the C++ shape validation checks to the beginning of the function so they run unconditionally across all execution paths, and modifying the Python error message to print only the shape of the filters tensor instead of the entire tensor object to prevent performance overhead and log pollution.

Comment on lines +188 to +197
if (filter_shape.dims() != 4) {
return absl::InvalidArgumentError(absl::StrCat(
"Conv2DBackpropInput: filter must be 4-dimensional, not ",
filter_shape.dims()));
}
if (out_backprop_shape.dims() != 4) {
return absl::InvalidArgumentError(absl::StrCat(
"Conv2DBackpropInput: out_backprop must be 4-dimensional, not ",
out_backprop_shape.dims()));
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The validation of filter_shape and out_backprop_shape dimensions is currently placed inside the if (input_sizes.dim_size(0) == 2) block. However, Conv2D backprop operations always require 4-dimensional filter and out_backprop shapes, regardless of the size of input_sizes. Placing these checks inside the conditional block means that other execution paths (e.g., when input_sizes.dim_size(0) == 4) will bypass this validation, potentially leading to out-of-bounds accesses or crashes later in the function.

Consider moving these checks to the very beginning of Conv2DBackpropComputeInputShape so they are executed unconditionally.

References
  1. Ensure proper error handling and defensive checks, and validate tensor shapes and execution paths unconditionally to prevent crashes across all execution paths.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in latest commit. Moved filter_shape and out_backprop_shape 4D validation to the beginning of Conv2DBackpropComputeInputShape so they execute unconditionally across all paths.

Comment thread tensorflow/python/ops/nn_ops.py Outdated
Comment on lines +2757 to +2760
filters = ops.convert_to_tensor(filters, name="filters")
if filters.shape.rank is not None and filters.shape.rank != 4:
raise ValueError(f"`filters` must be 4-dimensional. "
f"Received: filters={filters} with rank {filters.shape.rank}")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Printing the entire filters tensor object in the ValueError message can be extremely verbose and cause significant log pollution or performance overhead if the tensor is large (especially in eager mode where the full tensor content might be formatted into the string).

Instead, print only the shape of the tensor in the error message.

Suggested change
filters = ops.convert_to_tensor(filters, name="filters")
if filters.shape.rank is not None and filters.shape.rank != 4:
raise ValueError(f"`filters` must be 4-dimensional. "
f"Received: filters={filters} with rank {filters.shape.rank}")
filters = ops.convert_to_tensor(filters, name="filters")
if filters.shape.rank is not None and filters.shape.rank != 4:
raise ValueError(f"filters must be 4-dimensional. "
f"Received: filters with shape {filters.shape}")
References
  1. Avoid patterns that can cause performance overhead or log pollution, such as formatting entire large tensor objects into error messages.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in latest commit. Updated the ValueError formatting to display filters.shape instead of formatting the entire tensor object.

@adi-IL
adi-IL force-pushed the fix/conv2d-transpose-filter-rank-validation branch from 41a1fe0 to b4c65ed Compare August 22, 2026 22:32
@keerthanakadiri
keerthanakadiri requested a review from a team August 24, 2026 04:44
@github-project-automation github-project-automation Bot moved this to Assigned Reviewer in PR Queue Aug 24, 2026
@keerthanakadiri keerthanakadiri added the prtype:bugfix PR to fix a bug label Aug 24, 2026
@dmiltr3
dmiltr3 self-requested a review August 24, 2026 14:20
@google-ml-butler google-ml-butler Bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Aug 24, 2026
@github-project-automation github-project-automation Bot moved this from Assigned Reviewer to Approved by Reviewer in PR Queue Aug 24, 2026
@nithyak0204 nithyak0204 removed the awaiting review Pull request awaiting review label Aug 25, 2026
@nithyak0204 nithyak0204 removed the kokoro:force-run Tests on submitted change label Sep 2, 2026
copybara-service Bot pushed a commit that referenced this pull request Sep 29, 2026
[List of integrated commits](triton-lang/triton@a77e7c7...2074a1b)

FUTURE_COPYBARA_INTEGRATE_REVIEW=#125946 from adi-IL:fix/conv2d-transpose-filter-rank-validation b4c65ed
PiperOrigin-RevId: 978548887
@copybara-service
copybara-service Bot merged commit 5f17f49 into tensorflow:master Sep 29, 2026
16 of 17 checks passed
@github-project-automation github-project-automation Bot moved this from Approved by Reviewer to Merged in PR Queue Sep 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

prtype:bugfix PR to fix a bug ready to pull PR ready for merge process size:M CL Change Size: Medium

Projects

Status: Merged

Development

Successfully merging this pull request may close these issues.

tf.nn.conv2d_transpose aborts process with malformed filter/output_shape arguments

5 participants