[TF:XLA] Fix NaN in StatelessRandomNormal if the underlying uniform distribution...
authorPeter Hawkins <phawkins@google.com>
Tue, 8 May 2018 15:07:08 +0000 (08:07 -0700)
committerTensorFlower Gardener <gardener@tensorflow.org>
Tue, 8 May 2018 21:03:29 +0000 (14:03 -0700)
PiperOrigin-RevId: 195819645

tensorflow/compiler/tests/stateless_random_ops_test.py
tensorflow/compiler/tf2xla/kernels/stateless_random_ops.cc

index 4336ebd..b6f8390 100644 (file)
@@ -86,6 +86,15 @@ class StatelessRandomOpsTest(XLATestCase):
         # seed were not fixed.
         self.assertTrue(self._chi_squared(y, 10) < 16.92)
 
+  def testRandomNormalIsFinite(self):
+    with self.test_session() as sess, self.test_scope():
+      for dtype in self._random_types():
+        seed_t = array_ops.placeholder(dtypes.int32, shape=[2])
+        x = stateless.stateless_random_uniform(
+            shape=[10000], seed=seed_t, dtype=dtype)
+        y = sess.run(x, {seed_t: [0x12345678, 0xabcdef12]})
+        self.assertTrue(np.all(np.isfinite(y)))
+
   def _normal_cdf(self, x):
     """Cumulative distribution function for a standard normal distribution."""
     return 0.5 + 0.5 * np.vectorize(math.erf)(x / math.sqrt(2))
index 6340c22..a99d4dd 100644 (file)
@@ -255,7 +255,8 @@ class StatelessRandomNormalOp : public XlaOpKernel {
                                         seed_shape.DebugString()));
     xla::XlaOp seed = ctx->Input(1);
     xla::XlaBuilder* builder = ctx->builder();
-    auto uniform = RandomUniform(builder, seed, shape, -1.0, 1.0);
+    auto uniform =
+        RandomUniform(builder, seed, shape, std::nextafter(-1.0f, 0.0f), 1.0);
     // Convert uniform distribution to normal distribution by computing
     // sqrt(2) * erfinv(x)
     auto normal = builder->Mul(builder->ConstantR0<float>(std::sqrt(2.0)),