[XLA] Improve numerical stability of Logistic.

PiperOrigin-RevId: 314389569
Change-Id: Ia5b3f01f6c3f56f4c876b406900147dfe32ce0df
This commit is contained in:
A. Unique TensorFlower 2020-06-02 13:08:32 -07:00 committed by TensorFlower Gardener
parent dca04c0a9b
commit 4bddb55693
3 changed files with 7 additions and 18 deletions

View File

@ -510,16 +510,6 @@ class UnaryOpsTest(xla_test.XLATestCase):
],
dtype=dtype))
@test_util.disable_mlir_bridge(
"TODO(b/155501444): Handle _UnaryOpsComposition ops from Grappler")
def testFloatOpsDisabledOnMlirBridge(self):
for dtype in self.float_types:
if dtype != np.float16:
self._assertOpOutputMatchesExpected(
lambda x: math_ops.sigmoid(x) / math_ops.log1p(math_ops.exp(x)),
np.array([-40, 40], dtype=dtype),
expected=np.array([1.0, 0.025], dtype=dtype))
@test_util.disable_mlir_bridge(
"TODO(b/153812660): Handle tf.QuantizeAndDequantize compilation")
def testQuantizeAndDequantize(self):

View File

@ -1394,12 +1394,8 @@ XlaOp NextAfter(XlaOp from, XlaOp to) {
}
XlaOp Logistic(XlaOp x) {
if (x.builder()->GetShape(x).ValueOrDie().element_type() == F16) {
auto half = xla::ScalarLike(x, 0.5);
return half + half * xla::Tanh(half * x);
}
auto one = xla::ScalarLike(x, 1);
return xla::Div(one, (one + xla::Exp(xla::Neg(x))));
auto half = xla::ScalarLike(x, 0.5);
return half + half * xla::Tanh(half * x);
}
// Computes an approximation to the modified Bessel function of the first kind,

View File

@ -35,6 +35,7 @@ from tensorflow.python.framework import tensor_shape
from tensorflow.python.framework import test_util
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.ops import control_flow_v2_toggles
from tensorflow.python.ops import gradients_impl
from tensorflow.python.ops import init_ops
from tensorflow.python.ops import math_ops
@ -1015,7 +1016,8 @@ class LSTMTest(test.TestCase):
})
comparison_fn = self.assertAllEqual
if test_util.is_xla_enabled():
if (test_util.is_xla_enabled() and
control_flow_v2_toggles.control_flow_v2_enabled()):
comparison_fn = self.assertAllClose
if in_graph_mode:
comparison_fn(outputs_static, outputs_dynamic)
@ -1105,7 +1107,8 @@ class LSTMTest(test.TestCase):
})
comparison_fn = self.assertAllEqual
if test_util.is_xla_enabled():
if (test_util.is_xla_enabled() and
control_flow_v2_toggles.control_flow_v2_enabled()):
comparison_fn = self.assertAllClose
if in_graph_mode:
comparison_fn(outputs_static, outputs_dynamic)