Esempio n. 1
0
 def testSimpleConstantsExplicitCPU(self):
   config.set_device_policy('explicit')
   with ops.device('CPU:1'):
     self.assertAllClose(1., array_ops.ones([]))
     self.assertAllClose(0., array_ops.zeros([]))
     self.assertAllClose([1.], array_ops.fill([1], 1.))
     self.assertAllClose(2., constant_op.constant(1.) * 2.)
Esempio n. 2
0
 def testIdentityCanCopy(self):
   config.set_device_policy('explicit')
   with ops.device('CPU:0'):
     x = constant_op.constant(1.0)
     self.assertIn('CPU:0', x.device)
     self.assertIn('CPU:0', x.backing_device)
   with ops.device('GPU:0'):
     y = array_ops.identity(x)
     self.assertIn('GPU:0', y.device)
     self.assertIn('GPU:0', y.backing_device)
Esempio n. 3
0
 def testSilentCopy(self):
     # Temporarily replace the context
     # pylint: disable=protected-access
     old_context = context.context()
     context._set_context(context.Context())
     try:
         config.set_device_policy('silent')
         cpu_tensor = constant_op.constant(1.0)
         gpu_tensor = cpu_tensor.gpu()
         self.assertAllEqual(cpu_tensor + gpu_tensor, 2.0)
     finally:
         context._set_context(old_context)
Esempio n. 4
0
 def testSilentCopy(self):
   # Temporarily replace the context
   # pylint: disable=protected-access
   del context._context
   context._context = context.Context()
   try:
     config.set_device_policy('silent')
     cpu_tensor = constant_op.constant(1.0)
     gpu_tensor = cpu_tensor.gpu()
     self.assertAllEqual(cpu_tensor + gpu_tensor, 2.0)
   finally:
     del context._context
     context._context = context.Context()
Esempio n. 5
0
 def testSoftPlacement(self):
     # Temporarily replace the context
     # pylint: disable=protected-access
     old_context = context.context()
     context._set_context(context.Context())
     try:
         config.set_device_policy('silent')
         config.set_soft_device_placement(True)
         cpu_tensor = constant_op.constant(1.0)
         result = cpu_tensor + cpu_tensor
         self.assertEqual(result.device,
                          '/job:localhost/replica:0/task:0/device:GPU:0')
     finally:
         context._set_context(old_context)
Esempio n. 6
0
 def testSilentCopy(self):
   if not context.context().num_gpus():
     self.skipTest('No GPUs found')
   # Temporarily replace the context
   # pylint: disable=protected-access
   del context._context
   context._context = context.Context()
   try:
     config.set_device_policy('silent')
     cpu_tensor = constant_op.constant(1.0)
     gpu_tensor = cpu_tensor.gpu()
     self.assertAllEqual(cpu_tensor + gpu_tensor, 2.0)
   finally:
     del context._context
     context._context = context.Context()
Esempio n. 7
0
 def testSoftPlacement(self):
   # Temporarily replace the context
   # pylint: disable=protected-access
   del context._context
   context._context = context.Context()
   try:
     config.set_device_policy('silent')
     config.set_soft_device_placement(True)
     cpu_tensor = constant_op.constant(1.0)
     result = cpu_tensor + cpu_tensor
     self.assertEqual(result.device,
                      '/job:localhost/replica:0/task:0/device:GPU:0')
   finally:
     del context._context
     context._context = context.Context()
Esempio n. 8
0
 def testSoftPlacement(self):
     # Temporarily replace the context
     # pylint: disable=protected-access
     old_context = context.context()
     context._set_context(context.Context())
     try:
         config.set_device_policy('silent')
         config.set_soft_device_placement(True)
         # Avoid the TensorHandle cache hit.
         # TODO(b/169790439): include Context to the TensorHandle cache.
         cpu_tensor = constant_op.constant(1.1)
         result = cpu_tensor + cpu_tensor
         self.assertEqual(result.device,
                          '/job:localhost/replica:0/task:0/device:GPU:0')
     finally:
         context._set_context(old_context)
Esempio n. 9
0
 def testSoftPlacement(self):
   if not context.context().num_gpus():
     self.skipTest('No GPUs found')
   # Temporarily replace the context
   # pylint: disable=protected-access
   del context._context
   context._context = context.Context()
   try:
     config.set_device_policy('silent')
     config.set_soft_device_placement(True)
     cpu_tensor = constant_op.constant(1.0)
     result = cpu_tensor + cpu_tensor
     self.assertEqual(result.device,
                      '/job:localhost/replica:0/task:0/device:GPU:0')
   finally:
     del context._context
     context._context = context.Context()
Esempio n. 10
0
    def testDevicePolicy(self):
        self.assertEqual(context.DEVICE_PLACEMENT_SILENT,
                         context.context().device_policy)

        # If no op has been executed we should be able to set the device policy as
        # well as any init-time configs.
        config.set_intra_op_parallelism_threads(1)
        config.set_device_policy('silent')
        config.set_intra_op_parallelism_threads(2)

        context.ensure_initialized()

        def copy_tensor(dtype=dtypes.int32):
            with ops.device('CPU:0'):
                cpu_tensor = constant_op.constant(1, dtype=dtype)
            gpu_tensor = cpu_tensor.gpu()
            self.assertAllEqual(cpu_tensor + gpu_tensor, 2.0)

        config.set_device_policy('silent')
        self.assertEqual(config.get_device_policy(), 'silent')
        self.assertEqual(context.DEVICE_PLACEMENT_SILENT,
                         context.context().device_policy)
        copy_tensor()

        config.set_device_policy('silent_for_int32')
        self.assertEqual(config.get_device_policy(), 'silent_for_int32')
        self.assertEqual(context.DEVICE_PLACEMENT_SILENT_FOR_INT32,
                         context.context().device_policy)
        with self.assertRaisesRegex(errors.InvalidArgumentError,
                                    'Tensors on conflicting devices'):
            copy_tensor(dtypes.float32)
        copy_tensor()

        config.set_device_policy('warn')
        self.assertEqual(config.get_device_policy(), 'warn')
        self.assertEqual(context.DEVICE_PLACEMENT_WARN,
                         context.context().device_policy)
        copy_tensor()

        config.set_device_policy('explicit')
        self.assertEqual(config.get_device_policy(), 'explicit')
        self.assertEqual(context.DEVICE_PLACEMENT_EXPLICIT,
                         context.context().device_policy)
        with self.assertRaisesRegex(errors.InvalidArgumentError,
                                    'Tensors on conflicting devices'):
            copy_tensor()

        config.set_device_policy(None)
        self.assertEqual(config.get_device_policy(), 'silent')
Esempio n. 11
0
  def testDevicePolicy(self):
    self.assertEqual(context.DEVICE_PLACEMENT_SILENT,
                     context.context().device_policy)

    # If no op has been executed we should be able to set the device policy as
    # well as any init-time configs.
    config.set_intra_op_parallelism_threads(1)
    config.set_device_policy('silent')
    config.set_intra_op_parallelism_threads(2)

    # Excute a dummy op to ensure that the context has been initialized
    constant_op.constant(1)

    def copy_tensor(dtype=dtypes.int32):
      cpu_tensor = constant_op.constant(1, dtype=dtype)
      gpu_tensor = cpu_tensor.gpu()
      self.assertAllEqual(cpu_tensor + gpu_tensor, 2.0)

    config.set_device_policy('silent')
    self.assertEqual(config.get_device_policy(), 'silent')
    self.assertEqual(context.DEVICE_PLACEMENT_SILENT,
                     context.context().device_policy)
    copy_tensor()

    config.set_device_policy('silent_for_int32')
    self.assertEqual(config.get_device_policy(), 'silent_for_int32')
    self.assertEqual(context.DEVICE_PLACEMENT_SILENT_FOR_INT32,
                     context.context().device_policy)
    with self.assertRaisesRegexp(errors.InvalidArgumentError,
                                 'Tensors on conflicting devices'):
      copy_tensor(dtypes.float32)
    copy_tensor()

    config.set_device_policy('warn')
    self.assertEqual(config.get_device_policy(), 'warn')
    self.assertEqual(context.DEVICE_PLACEMENT_WARN,
                     context.context().device_policy)
    copy_tensor()

    config.set_device_policy('explicit')
    self.assertEqual(config.get_device_policy(), 'explicit')
    self.assertEqual(context.DEVICE_PLACEMENT_EXPLICIT,
                     context.context().device_policy)
    with self.assertRaisesRegexp(errors.InvalidArgumentError,
                                 'Tensors on conflicting devices'):
      copy_tensor()

    config.set_device_policy(None)
    self.assertEqual(config.get_device_policy(), 'silent')