Home | History | Annotate | Download | only in kernel_tests
      1 # Copyright 2015 The TensorFlow Authors. All Rights Reserved.
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
      4 # you may not use this file except in compliance with the License.
      5 # You may obtain a copy of the License at
      6 #
      7 #     http://www.apache.org/licenses/LICENSE-2.0
      8 #
      9 # Unless required by applicable law or agreed to in writing, software
     10 # distributed under the License is distributed on an "AS IS" BASIS,
     11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 # See the License for the specific language governing permissions and
     13 # limitations under the License.
     14 # ==============================================================================
     15 """Tests for denormal handling."""
     16 
     17 from __future__ import absolute_import
     18 from __future__ import division
     19 from __future__ import print_function
     20 
     21 import numpy as np
     22 import platform
     23 
     24 from tensorflow.python.framework import constant_op
     25 from tensorflow.python.ops import array_ops
     26 from tensorflow.python.platform import test
     27 
     28 
     29 class DenormalTest(test.TestCase):
     30 
     31   def testPythonHasDenormals(self):
     32     """Non-tf numpy code should treat denormals correctly."""
     33     for dtype in np.float32, np.float64:
     34       tiny = np.finfo(dtype).tiny
     35       self.assertEqual(tiny, tiny / 16 * 16)
     36 
     37   def _flushDenormalsTest(self, use_gpu, dtypes):
     38     if platform.machine() == "ppc64le" or platform.machine() == "s390x":
     39       # Disabled denormal_test on power/s390x platform
     40       # Check relevant discussion - https://github.com/tensorflow/tensorflow/issues/11902
     41       return
     42     with self.test_session(use_gpu=use_gpu):
     43       array_ops.identity(7).eval()
     44       for dtype in dtypes:
     45         tiny = np.finfo(dtype).tiny
     46         # Small shape to test main thread, large shape to test thread pool
     47         for shape in (), (1 << 20,):
     48           flush = 0.1 * constant_op.constant(tiny, shape=shape)
     49           self.assertAllEqual(flush.eval(), np.zeros(shape))
     50           # Make sure the flags don't leak out
     51           self.testPythonHasDenormals()
     52 
     53   def testFlushDenormalsCPU(self):
     54     # On CPUs, the processor flags flush for both single and double precision.
     55     self._flushDenormalsTest(use_gpu=False, dtypes=(np.float32, np.float64))
     56 
     57   def testFlushDenormalsGPU(self):
     58     # On GPUs, only single precision can flush to zero.
     59     self._flushDenormalsTest(use_gpu=True, dtypes=(np.float32,))
     60 
     61 
     62 if __name__ == "__main__":
     63   test.main()
     64