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

Make tf.random_uniform([0], maxval=0, dtype=tf.int32) not crash #22147

Merged
merged 1 commit into from
Sep 21, 2018
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
10 changes: 7 additions & 3 deletions tensorflow/core/kernels/random_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,13 @@ class RandomUniformIntOp : public OpKernel {
errors::InvalidArgument("maxval must be 0-D, got shape ",
maxval.shape().DebugString()));

// Verify that minval < maxval
// Allocate output, and exit early if possible
Tensor* output;
OP_REQUIRES_OK(ctx, AllocateOutputWithShape(ctx, shape, 0, &output));
if (output->NumElements() == 0) return;

// Verify that minval < maxval. This check intentionally happens after the
// early exit for empty output. Zero impossible things are fine.
IntType lo = minval.scalar<IntType>()();
IntType hi = maxval.scalar<IntType>()();
OP_REQUIRES(
Expand All @@ -243,8 +249,6 @@ class RandomUniformIntOp : public OpKernel {
Distribution;
Distribution dist(lo, hi);

Tensor* output;
OP_REQUIRES_OK(ctx, AllocateOutputWithShape(ctx, shape, 0, &output));
auto output_flat = output->flat<IntType>();
functor::FillPhiloxRandom<Device, Distribution>()(
ctx, ctx->eigen_device<Device>(),
Expand Down
9 changes: 9 additions & 0 deletions tensorflow/python/kernel_tests/random/random_ops_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,15 @@ def testUniformInts(self):
error = np.abs(counts - mean)
self.assertLess(error.max(), 5 * std)

# Check that minval = maxval is fine iff we're producing no numbers
def testUniformIntsDegenerate(self):
for dt in dtypes.int32, dtypes.int64:
def sample(n):
return self._Sampler(n, minv=0, maxv=0, dtype=dt, use_gpu=True)()
self.assertEqual(sample(0).shape, (10, 0))
with self.assertRaisesOpError('Need minval < maxval, got 0 >= 0'):
sample(1)

# Checks that the CPU and GPU implementation returns the same results,
# given the same random seed
def testCPUGPUMatch(self):
Expand Down