Repository navigation
fix: validate filter and out_backprop dimensions in Conv2DBackpropInput to prevent crash on invalid rank - #125946
Conversation
There was a problem hiding this comment.
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.
| 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())); | ||
| } |
There was a problem hiding this comment.
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
- Ensure proper error handling and defensive checks, and validate tensor shapes and execution paths unconditionally to prevent crashes across all execution paths.
There was a problem hiding this comment.
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.
| 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}") |
There was a problem hiding this comment.
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.
| 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
- Avoid patterns that can cause performance overhead or log pollution, such as formatting entire large tensor objects into error messages.
There was a problem hiding this comment.
Addressed in latest commit. Updated the ValueError formatting to display filters.shape instead of formatting the entire tensor object.
…ut to prevent crash on invalid rank
41a1fe0 to
b4c65ed
Compare
[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
5f17f49
into
tensorflow:master
Fixes #125506
Summary of Changes
Root Cause:
tensorflow/core/kernels/conv_grad_input_ops.h,Conv2DBackpropInputOp::ComputeandConv2DCustomBackpropInputOp::Computecheckedout_backprop.dims() == 4(with a typo mentioninginput_sizes), but did not validatefilter.dims() == 4.filters=8) was passed totf.nn.conv2d_transposeortf.raw_ops.Conv2DBackpropInput, queryingfilter_shape.dim_size(2)orfilter_shape.dim_size(3)inConv2DBackpropComputeInputShape(tensorflow/core/kernels/conv_grad_shape_utils.cc) andComputetriggered a fatal check failure abort (Check failed: d < dims() (2 vs. 0)).Conv2DBackpropComputeInputShapelacked validation thatfilter_shapeandout_backprop_shapewere 4-dimensional wheninput_sizescontained 2 spatial dimensions before indexingfilter_shape.dim_size(2).Fix:
OP_REQUIRES(context, filter.dims() == 4, ...)validation inConv2DBackpropInputOp::ComputeandConv2DCustomBackpropInputOp::Compute(conv_grad_input_ops.h).out_backpropdimension validation to stateout_backprop must be 4-dimensional.Conv2DBackpropComputeInputShape(conv_grad_shape_utils.cc) ensuringfilter_shapeandout_backprop_shapehave rank 4 before indexing depth dimensions wheninput_sizes.dim_size(0) == 2.filtersinconv2d_transpose_v2(tensorflow/python/ops/nn_ops.py), raising a clearValueErrorwhen non-4D filters are passed.testConv2DTransposeInvalidFilterRankandtestConv2DTransposeInvalidOutputShapeRankintensorflow/python/kernel_tests/nn_ops/conv2d_transpose_test.py.Validation