Example #1
0
 def test_construct_setattr_named_tuple_type_fails_unknown_name(self):
     good_type = computation_types.NamedTupleType([('a', tf.int32),
                                                   ('b', tf.bool)])
     value_comp = computation_building_blocks.Data('x', tf.int32)
     with self.assertRaises(AttributeError):
         _ = computation_constructing_utils.construct_named_tuple_setattr_lambda(
             good_type, 'c', value_comp)
Example #2
0
 def test_construct_setattr_named_tuple_type_fails_on_bad_type(self):
     bad_type = computation_types.FederatedType([('a', tf.int32)],
                                                placement_literals.CLIENTS)
     value_comp = computation_building_blocks.Data('x', tf.int32)
     with self.assertRaises(TypeError):
         _ = computation_constructing_utils.construct_named_tuple_setattr_lambda(
             bad_type, 'a', value_comp)
Example #3
0
 def test_construct_setattr_named_tuple_type_fails_implicit_type_conversion(
         self):
     good_type = computation_types.NamedTupleType([('a', tf.int32),
                                                   ('b', tf.bool)])
     value_comp = computation_building_blocks.Data('x', tf.int32)
     with self.assertRaisesRegex(TypeError, 'incompatible type'):
         _ = computation_constructing_utils.construct_named_tuple_setattr_lambda(
             good_type, 'b', value_comp)
Example #4
0
 def test_construct_setattr_named_tuple_type_replaces_single_element(self):
     good_type = computation_types.NamedTupleType([('a', tf.int32),
                                                   ('b', tf.bool)])
     value_comp = computation_building_blocks.Data('x', tf.int32)
     lam = computation_constructing_utils.construct_named_tuple_setattr_lambda(
         good_type, 'a', value_comp)
     self.assertEqual(
         lam.tff_repr,
         '(let value_comp_placeholder=x in (lambda_arg -> <a=value_comp_placeholder,b=lambda_arg[1]>))'
     )
Example #5
0
 def test_construct_setattr_named_tuple_type_leaves_type_signature_unchanged(
         self):
     good_type = computation_types.NamedTupleType([('a', tf.int32),
                                                   (None, tf.float32),
                                                   ('b', tf.bool)])
     value_comp = computation_building_blocks.Data('x', tf.int32)
     lam = computation_constructing_utils.construct_named_tuple_setattr_lambda(
         good_type, 'a', value_comp)
     self.assertTrue(
         type_utils.are_equivalent_types(lam.type_signature.parameter,
                                         lam.type_signature.result))
Example #6
0
 def __setattr__(self, name, value):
     py_typecheck.check_type(name, six.string_types)
     value_comp = ValueImpl.get_comp(
         to_value(value, None, self._context_stack))
     if isinstance(self._comp.type_signature,
                   computation_types.FederatedType) and isinstance(
                       self._comp.type_signature.member,
                       computation_types.NamedTupleType):
         new_comp = computation_constructing_utils.construct_federated_setattr_call(
             self._comp, name, value_comp)
         super(ValueImpl, self).__setattr__('_comp', new_comp)
         return
     elif not isinstance(self._comp.type_signature,
                         computation_types.NamedTupleType):
         raise TypeError(
             'Operator setattr() is only supported for named tuples, but the '
             'object on which it has been invoked is of type {}.'.format(
                 str(self._comp.type_signature)))
     named_tuple_setattr_lambda = computation_constructing_utils.construct_named_tuple_setattr_lambda(
         self._comp.type_signature, name, value_comp)
     new_comp = computation_building_blocks.Call(named_tuple_setattr_lambda,
                                                 self._comp)
     super(ValueImpl, self).__setattr__('_comp', new_comp)
Example #7
0
 def test_construct_setattr_named_tuple_type_fails_on_none_value(self):
     good_type = computation_types.NamedTupleType([('a', tf.int32)])
     with self.assertRaises(TypeError):
         _ = computation_constructing_utils.construct_named_tuple_setattr_lambda(
             good_type, 'a', None)
Example #8
0
 def test_construct_setattr_named_tuple_type_fails_on_none_name(self):
     good_type = computation_types.NamedTupleType([('a', tf.int32)])
     value_comp = computation_building_blocks.Data('x', tf.int32)
     with self.assertRaises(TypeError):
         _ = computation_constructing_utils.construct_named_tuple_setattr_lambda(
             good_type, None, value_comp)