Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash of max_pool3d when ksize is 0 or negative #51975

Merged
merged 2 commits into from Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions tensorflow/core/kernels/pooling_ops_3d.cc
Expand Up @@ -141,6 +141,11 @@ class Pooling3DOp : public UnaryOp<T> {
OP_REQUIRES(context, ksize_.size() == 5,
errors::InvalidArgument("Sliding window ksize field must "
"specify 5 dimensions"));
bool non_negative = std::all_of(
ksize_.begin(), ksize_.end(), [](int k) { return k > 0; });
OP_REQUIRES(context, non_negative,
errors::InvalidArgument("Sliding window ksize field must "
"have non-negative dimensions"));
OP_REQUIRES_OK(context, context->GetAttr("strides", &stride_));
OP_REQUIRES(context, stride_.size() == 5,
errors::InvalidArgument("Sliding window stride field must "
Expand Down
18 changes: 18 additions & 0 deletions tensorflow/python/kernel_tests/pooling_ops_3d_test.py
Expand Up @@ -21,6 +21,7 @@
import numpy as np

from tensorflow.python.framework import constant_op
from tensorflow.python.framework import errors
from tensorflow.python.framework import test_util
from tensorflow.python.ops import gradient_checker
from tensorflow.python.ops import gradients_impl
Expand Down Expand Up @@ -505,6 +506,23 @@ def testAvgPoolGradSamePadding3_1_3d(self):
strides=(1, 1, 1),
padding="SAME")

def testMaxPool3DZeroPoolSize(self):
# Test case for GitHub issue 51936.
for f in [nn_ops.max_pool3d, nn_ops.avg_pool3d]:
with self.session():
with self.assertRaises((errors.InvalidArgumentError, ValueError)):
input_sizes = [3, 4, 10, 11, 12]

input_data = 1.
input_tensor = constant_op.constant(
input_data, shape=input_sizes, name="input")
pool_3d = f(
input_tensor,
ksize=[2, 2, 0],
strides=1,
padding="VALID")
self.evaluate(pool_3d)


if __name__ == "__main__":
test.main()