Skip to content

tf.raw_ops.ExtractImagePatches: Assertion failure in shape inference step #63067

Closed

Description

@Sehun0819

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

Yes

Source

source

TensorFlow version

tf 2.17

Custom code

Yes

OS platform and distribution

Linux Ubuntu 22.04 LTS

Mobile device

No response

Python version

3.11.7

Bazel version

6.5.0

GCC/compiler version

clang 16

CUDA/cuDNN version

No response

GPU model and memory

No response

Current behavior?

tf.raw_ops.ExtractImagePatches can lead to assertion failure in shape inference step.
Error location is here:

      TF_RETURN_IF_ERROR(c->Multiply(
          c->Dim(input_shape, 3), ksize_rows * ksize_cols, &output_depth_dim));

Because it does not check validity of ksize_rows * ksize_cols, the negative value of it is fed to Multiply:

Status InferenceContext::Multiply(DimensionHandle first,
                                  DimensionOrConstant second,
                                  DimensionHandle* out)

And it ends up with assertion failure at a constructor of DimensionOrConstant.

Standalone code to reproduce the issue

import tensorflow as tf

tf.compat.v1.disable_eager_execution()

tf.raw_ops.ExtractImagePatches(
    images=tf.random.normal([1,1,1,1]),
    ksizes=[1,-1,2,1],
    strides=[1,1,1,1],
    rates=[1,1,1,1],
    padding="VALID",
    name=None
)

Relevant log output

Release build: Outputs nothing

Debug build:

2024-02-27 20:05:58.701202: F ./tensorflow/core/framework/shape_inference.h:891] Check failed: val >= 0 || val == InferenceContext::kUnknownDim Dimension must be non-negative or equal to InferenceContext::kUnknownDim but got -2
Aborted (core dumped)

Activity

  1. Sehun0819 commented on Feb 27, 2024

    @Sehun0819
    Author

    In release build, we can observe unstable intermediate tensor with invalid dim(gist).

    import tensorflow as tf
    
    tf.compat.v1.disable_eager_execution()
    
    x = tf.raw_ops.ExtractImagePatches(
        images=tf.random.normal([1,1,1,1]),
        ksizes=[1,-1,2,1],
        strides=[1,1,1,1],
        rates=[1,1,1,1],
        padding="VALID",
        name=None
    )
    
    # Prints "True"
    print(tf.is_tensor(x))
    
    # Ends up with "ValueError: Dimension -2 must be >= 0"
    print(x)
  2. SuryanarayanaY commented on Feb 27, 2024

    @SuryanarayanaY
    Collaborator

    Hi @Sehun0819 ,

    I think this behaviour exists if negative values passed to strides and rates also. Validation might be costly in that case.Since this behaviour is specific to a debug build and official builds won't have this behaviour, could you please mention the build command used for this build. May be there exists a nice way for fixing this.

    Thanks!

  3. added
    comp:ops
    comp:core
    TF 2.15
    on Feb 27, 2024
  4. Sehun0819 commented on Feb 27, 2024

    @Sehun0819
    Author

    @SuryanarayanaY
    Hi!
    I built it following manual with --config=dbg.

    I just found that it runs without any error message when both ksizes[1] and ksizes[2] are negative(gist), and it seems even worse.

    import tensorflow as tf
    
    x = tf.raw_ops.ExtractImagePatches(
        images=tf.random.normal([1,1,1,1]),
        ksizes=[1,-1,-2,1],
        strides=[1,1,1,1],
        rates=[1,1,1,1],
        padding="VALID",
        name=None
    )
    
    # Tensor("ExtractImagePatches_8:0", shape=(1, 3, 4, 2), dtype=float32)
    print(x)
  5. SuryanarayanaY commented on Mar 1, 2024

    @SuryanarayanaY
    Collaborator

    Hi @Sehun0819 ,

    The DCHECK pointed here already addresses in PR #63089. I believe the API still lacks some validations.

  6. added
    awaiting review
    on Mar 19, 2024
  7. removed
    awaiting review
    on Jun 3, 2026
  8. Kayyuri commented on Jun 3, 2026

    @Kayyuri

    Hi @Sehun0819,
    Thank you so much for filing this issue! We truly appreciate your support. At the moment, this request is not prioritized and mark it for community contributions. If you would like to contribute on a fix, please feel free to fork the repository and submit a pull request in accordance with the TensorFlow CONTRIBUTION GUIDELINES and link this issue in your PR to ensure the maintainers can easily review and collaborate.
    To avoid duplicate work, please leave a comment here if you decide to take it on!

    Thank you again for your interest and we look forward to seeing your contributions!

  9. added
    stat:contribution welcome
    on Jun 3, 2026
  10. 2 remaining items

  11. added
    awaiting PR merge
    comp:ops
    comp:core
    and removed
    comp:ops
    comp:core
    TF 2.15
    awaiting PR merge
    on Oct 1, 2026
  12. google-ml-butler commented on Oct 7, 2026

    @google-ml-butler

    Are you satisfied with the resolution of your issue?
    Yes
    No

  13. added a commit that references this issue on Oct 7, 2026
    9e6d81b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

awaiting PR mergecomp:corecomp:opstype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions