Skip to content

fix: validate minimum input rank in convolution_internal to prevent crash on invalid shapes - #125859

Merged
copybara-service[bot] merged 1 commit into
tensorflow:masterfrom
adi-IL:fix/convolution-input-rank-validation
Aug 26, 2026
Merged

copybara-service[bot] merged 1 commit into
tensorflow:masterfrom
adi-IL:fix/convolution-input-rank-validation

Conversation

@adi-IL

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

Copy link
Copy Markdown
Contributor

When calling tf.nn.atrous_conv2d or tf.nn.convolution with an invalid input tensor rank (for example, a rank-1 or rank-2 tensor when performing 2D convolution with rank-4 filters), execution on CPU triggers a fatal check failure abort:

Check failed: index >= 0 && index < num_total_dims Invalid index from the dimension: 3, 0, C
*** Check failure stack trace: ***
Aborted (core dumped)

Root cause

In tensorflow/python/ops/nn_ops.py:convolution_internal, num_spatial_dims is computed from filters.shape.rank - 2 (e.g. 4 - 2 = 2 for 2D convolution). The number of batch dimensions was calculated as num_batch_dims = inputs_rank - num_spatial_dims - 1.

When inputs_rank < num_spatial_dims + 1 (e.g. inputs_rank = 1 for a 1D input to 2D convolution), num_batch_dims became negative (1 - 2 - 1 = -2), leading to an invalid channel_index = num_batch_dims + num_spatial_dims = 0. This caused _get_sequence to construct malformed stride/dilation sequences and passed invalid shapes to the C++ kernel where an internal dimension check failure terminated the process on CPU. Additionally, when inputs_rank == 0 (scalar), if inputs_rank: evaluated to False and incorrectly fell back to num_batch_dims = 1.

Fix

In convolution_internal, when inputs_rank is not None, validate that num_batch_dims = inputs_rank - num_spatial_dims - 1 >= 0. If inputs_rank < num_spatial_dims + 1, raise a descriptive ValueError indicating that the input must have rank at least num_spatial_dims + 1 (spatial dimensions + channel dimension), preventing malformed parameters from reaching the underlying kernels.

Testing

  • Added testInvalidInputRank in tensorflow/python/kernel_tests/nn_ops/atrous_conv2d_test.py.
  • Added testInvalidInputRank in tensorflow/python/kernel_tests/nn_ops/atrous_convolution_test.py.
  • Verified syntax with python3 -m py_compile.

Fixes #125510.

@google-ml-butler google-ml-butler Bot added the size:S CL Change Size: Small label Aug 21, 2026
@google-ml-butler
google-ml-butler Bot requested a review from cantonios August 21, 2026 23:23
@google-ml-butler google-ml-butler Bot added the awaiting review Pull request awaiting review label Aug 21, 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 input rank validation to the convolution_internal helper function in tensorflow/python/ops/nn_ops.py to ensure that the input tensor has a rank of at least num_spatial_dims + 1. It also adds corresponding unit tests (testInvalidInputRank) to verify this behavior for both atrous_conv2d and convolution operations. I have no feedback to provide.

@dianatofficial dianatofficial left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Solid PR. Tests cover the important paths.

@keerthanakadiri
keerthanakadiri requested a review from a team August 24, 2026 04:33
@keerthanakadiri keerthanakadiri added the python Pull requests that update Python code label Aug 24, 2026
@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:22
@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
@copybara-service
copybara-service Bot merged commit deb1655 into tensorflow:master Aug 26, 2026
18 of 19 checks passed
@github-project-automation github-project-automation Bot moved this from Approved by Reviewer to Merged in PR Queue Aug 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kokoro:force-run Tests on submitted change prtype:bugfix PR to fix a bug python Pull requests that update Python code ready to pull PR ready for merge process size:S CL Change Size: Small

Projects

Status: Merged

Development

Successfully merging this pull request may close these issues.

tf.nn.atrous_conv2d aborts on CPU with invalid input rank while GPU raises InvalidArgumentError

6 participants