def test_to_representation_for_tf_variable(self): value = tf.Variable(10, dtype=tf.int32) type_signature = computation_types.TensorType(tf.int32) v = eager_tf_executor.to_representation_for_type(value, {}, type_signature) self.assertIsInstance(v, tf.Tensor) self.assertEqual(v, 10) self.assertEqual(v.dtype, tf.int32)
def test_to_representation_for_type_with_int_on_specific_device(self): v = eager_tf_executor.to_representation_for_type( 10, {}, tf.int32, '/CPU:0') self.assertIsInstance(v, tf.Tensor) self.assertEqual(v.numpy(), 10) self.assertEqual(v.dtype, tf.int32) self.assertTrue(v.device.endswith('CPU:0'))
def test_to_representation_for_tf_variable(self): v = eager_tf_executor.to_representation_for_type( tf.Variable(10, dtype=tf.int32), {}, type_spec=computation_types.TensorType(tf.int32)) self.assertIsInstance(v, tf.Tensor) self.assertEqual(v.numpy(), 10) self.assertEqual(v.dtype, tf.int32)
def test_to_representation_for_type_with_int(self): value = 10 type_signature = computation_types.TensorType(tf.int32) v = eager_tf_executor.to_representation_for_type(value, {}, type_signature) self.assertIsInstance(v, tf.Tensor) self.assertEqual(v.numpy(), 10) self.assertEqual(v.dtype, tf.int32)
def test_eager_value_constructor_with_int_constant(self): int_tensor_type = computation_types.TensorType(dtype=tf.int32, shape=[]) normalized_value = eager_tf_executor.to_representation_for_type( 10, {}, int_tensor_type) v = eager_tf_executor.EagerValue(normalized_value, int_tensor_type) self.assertEqual(str(v.type_signature), 'int32') self.assertIsInstance(v.internal_representation, tf.Tensor) self.assertEqual(v.internal_representation, 10)
def test_to_representation_for_type_with_int_on_specific_device(self): value = 10 type_signature = computation_types.TensorType(tf.int32) v = eager_tf_executor.to_representation_for_type( value, {}, type_signature, '/CPU:0') self.assertIsInstance(v, tf.Tensor) self.assertEqual(v.numpy(), 10) self.assertEqual(v.dtype, tf.int32) self.assertTrue(v.device.endswith('CPU:0'))
def _get_to_representation_for_type_succeeds_on_device(self, device): @computations.tf_computation(tf.int32) def comp(x): return tf.add(x, 1) comp_proto = computation_impl.ComputationImpl.get_proto(comp) fn = eager_tf_executor.to_representation_for_type( comp_proto, {}, comp.type_signature, device='/{}'.format(device)) result = fn(tf.constant(20)) return result
def test_to_representation_for_type_succeeds_on_all_devices(self, device): @computations.tf_computation(tf.int32) def comp(x): return tf.add(x, 1) comp_proto = computation_impl.ComputationImpl.get_proto(comp) fn = eager_tf_executor.to_representation_for_type( comp_proto, comp.type_signature, device='/{}'.format(device)) result = fn(tf.constant(20)) self.assertTrue(result.device.endswith(device))
def test_to_representation_for_type_with_int(self): v = eager_tf_executor.to_representation_for_type(10, {}, tf.int32) self.assertIsInstance(v, tf.Tensor) self.assertEqual(v.numpy(), 10) self.assertEqual(v.dtype, tf.int32)