Skip to content
Permalink
Browse files Browse the repository at this point in the history
Adding missing requirement on inputs for MirrorPadGrad op and updatin…
…g arithmetic to account for int32 padding values.

PiperOrigin-RevId: 480691952
  • Loading branch information
jsmeredith authored and tensorflower-gardener committed Oct 12, 2022
1 parent f82f6ca commit 717ca98
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
14 changes: 11 additions & 3 deletions tensorflow/core/kernels/image/mirror_pad_op.cc
Expand Up @@ -297,13 +297,21 @@ class MirrorPadGradOp : public OpKernel {
TensorShape output_shape;
typename TTypes<Tpaddings>::ConstMatrix paddings = in1.matrix<Tpaddings>();
for (int d = 0; d < dims; ++d) {
const Tpaddings before = paddings(d, 0); // Pad before existing elements.
const Tpaddings after = paddings(d, 1); // Pad after existing elements.
const int64_t before = paddings(d, 0); // Pad before existing elements.
const int64_t after = paddings(d, 1); // Pad after existing elements.
OP_REQUIRES(context, before >= 0 && after >= 0,
errors::InvalidArgument(
"Paddings must be non-negative: ", before, ", ", after));

const int64_t out_size = in0.dim_size(d) - (before + after);
const int64_t in_size = in0.dim_size(d);
const int64_t total_padding = before + after;
OP_REQUIRES(
context, total_padding < in_size && total_padding >= 0,
errors::InvalidArgument(
"Total paddings must be less than the input dimension size: ",
total_padding, " was not less than ", in_size));

const int64_t out_size = in_size - total_padding;
if (offset_ == 0) { // SYMMETRIC mode.
OP_REQUIRES(context, before <= out_size && after <= out_size,
errors::InvalidArgument("paddings must be no greater "
Expand Down
15 changes: 15 additions & 0 deletions tensorflow/python/kernel_tests/array_ops/array_ops_test.py
Expand Up @@ -1617,6 +1617,21 @@ def testEager(self):
[[0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 2, 3, 0, 0],
[0, 0, 4, 5, 6, 0, 0], [0, 0, 0, 0, 0, 0, 0]])

# b/246325518: Bad shape size. Explicitly testing different execution paths.
def testInvalidMirrorPadGradEagerMode(self):
with context.eager_mode():
with self.assertRaises(Exception):
gen_array_ops.MirrorPadGrad(
input=[1], paddings=[[0x77f00000, 0xa000000]], mode="REFLECT")

# b/246325518: Bad shape size. Explicitly testing different execution paths.
def testInvalidMirrorPadGradGraphMode(self):
with context.graph_mode():
with self.assertRaises(Exception):
result = gen_array_ops.MirrorPadGrad(
input=[1], paddings=[[0x77f00000, 0xa000000]], mode="REFLECT")
self.evaluate(result)

def testSymmetricMirrorPadGrad(self):
t = np.broadcast_to(np.arange(0, 7), (3, 2, 1, 7))
paddings = constant_op.constant([
Expand Down

0 comments on commit 717ca98

Please sign in to comment.