Home | History | Annotate | Download | only in tests
      1 # Copyright 2017 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 """Test cases for XLA devices."""
     16 
     17 from __future__ import absolute_import
     18 from __future__ import division
     19 from __future__ import print_function
     20 
     21 from tensorflow.python.client import session as session_lib
     22 from tensorflow.python.framework import dtypes
     23 from tensorflow.python.framework import ops
     24 from tensorflow.python.ops import array_ops
     25 from tensorflow.python.platform import test
     26 
     27 
     28 class XlaDeviceTest(test.TestCase):
     29 
     30   def testCopies(self):
     31     """Tests that copies between GPU and XLA devices work."""
     32     if not test.is_gpu_available():
     33       return
     34 
     35     with session_lib.Session() as sess:
     36       x = array_ops.placeholder(dtypes.float32, [2])
     37       with ops.device("GPU"):
     38         y = x * 2
     39       with ops.device("device:XLA_CPU:0"):
     40         z = y * y
     41       with ops.device("GPU"):
     42         w = y + z
     43       result = sess.run(w, {x: [1.5, 0.5]})
     44     self.assertAllClose(result, [12., 2.], rtol=1e-3)
     45 
     46 
     47 if __name__ == "__main__":
     48   test.main()
     49