def test_to_representation_for_type_with_tensor_type(self):
   self.assertEqual(
       reference_executor.to_representation_for_type(10, tf.int32), 10)
   with self.assertRaises(TypeError):
     reference_executor.to_representation_for_type(0.1, tf.int32)
   with self.assertRaises(TypeError):
     reference_executor.to_representation_for_type([], tf.int32)
 def test_to_representation_for_type_with_federated_type(self):
   self.assertEqual(
       reference_executor.to_representation_for_type(
           10,
           computation_types.FederatedType(
               tf.int32, placements.SERVER, all_equal=True)), 10)
   x = [1, 2, 3]
   self.assertEqual(
       reference_executor.to_representation_for_type(
           x,
           computation_types.FederatedType(
               tf.int32, placements.CLIENTS, all_equal=False)), x)
    def test_to_representation_for_type_with_function_type(self):
        def foo(x):
            self.assertIsInstance(x, reference_executor.ComputedValue)
            return reference_executor.ComputedValue(str(x.value), tf.string)

        self.assertIs(
            reference_executor.to_representation_for_type(
                foo, computation_types.FunctionType(tf.int32, tf.string),
                lambda x, t: x), foo)

        with self.assertRaises(TypeError):
            reference_executor.to_representation_for_type(
                foo, computation_types.FunctionType(tf.int32, tf.string))

        with self.assertRaises(TypeError):
            reference_executor.to_representation_for_type(
                10, computation_types.FunctionType(tf.int32, tf.string))
  def test_to_representation_for_type_with_named_tuple_type(self):
    foo = anonymous_tuple.AnonymousTuple([('x', 10), ('y', 20)])
    self.assertEqual(
        reference_executor.to_representation_for_type(foo, [('x', tf.int32),
                                                            ('y', tf.int32)]),
        foo)
    foo = anonymous_tuple.AnonymousTuple([
        ('x', anonymous_tuple.AnonymousTuple([(None, 10), (None, 20)])),
        ('y', 30),
    ])
    self.assertEqual(
        reference_executor.to_representation_for_type(
            foo, [('x', [tf.int32, tf.int32]), ('y', tf.int32)]), foo)
    self.assertEqual(
        reference_executor.to_representation_for_type(
            anonymous_tuple.AnonymousTuple([('x', [10, 20]), ('y', 30)]),
            [('x', [tf.int32, tf.int32]), ('y', tf.int32)]),
        anonymous_tuple.AnonymousTuple([('x',
                                         anonymous_tuple.AnonymousTuple(
                                             [(None, 10), (None, 20)])),
                                        ('y', 30)]))
    with self.assertRaises(TypeError):
      reference_executor.to_representation_for_type(10, [tf.int32, tf.int32])

    unordered_dict = {'a': 10, 'b': 20}
    self.assertEqual(
        str(
            reference_executor.to_representation_for_type(
                unordered_dict, [('a', tf.int32), ('b', tf.int32)])),
        '<a=10,b=20>')
    self.assertEqual(
        str(
            reference_executor.to_representation_for_type(
                unordered_dict, [('b', tf.int32), ('a', tf.int32)])),
        '<b=20,a=10>')
 def test_stamp_computed_value_into_graph_with_tuples_of_tensors(self):
   v_val = anonymous_tuple.AnonymousTuple([('x', 10),
                                           ('y',
                                            anonymous_tuple.AnonymousTuple(
                                                [('z', 0.6)]))])
   v_type = [('x', tf.int32), ('y', [('z', tf.float32)])]
   v = reference_executor.ComputedValue(
       reference_executor.to_representation_for_type(v_val, v_type), v_type)
   with tf.Graph().as_default() as graph:
     stamped_v = reference_executor.stamp_computed_value_into_graph(v, graph)
     with tf.Session(graph=graph) as sess:
       v_val = graph_utils.fetch_value_in_session(sess, stamped_v)
   self.assertEqual(str(v_val), '<x=10,y=<z=0.6>>')
 def test_to_representation_for_type_with_sequence_type(self):
   foo = [1, 2, 3]
   self.assertEqual(
       reference_executor.to_representation_for_type(
           foo, computation_types.SequenceType(tf.int32)), foo)
 def test_to_representation_for_type_with_placement_type(self):
   self.assertIs(
       reference_executor.to_representation_for_type(
           placements.CLIENTS, computation_types.PlacementType()),
       placements.CLIENTS)
 def test_to_representation_for_type_with_abstract_type(self):
   with self.assertRaises(TypeError):
     reference_executor.to_representation_for_type(
         10, computation_types.AbstractType('T'))