def test_build_encoded_broadcast_raise_warning(self): value = tf.constant(np.random.rand(20)) value_spec = tf.TensorSpec(value.shape, tf.dtypes.as_dtype(value.dtype)) encoder = te.encoders.as_simple_encoder(te.encoders.identity(), value_spec) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') encoding_utils.build_encoded_broadcast(value, encoder) self.assertLen(w, 1)
def test_build_encoded_broadcast_raise_warning(self): value = tf.constant(np.random.rand(20)) value_spec = tf.TensorSpec(value.shape, tf.dtypes.as_dtype(value.dtype)) encoder = te.encoders.as_simple_encoder(te.encoders.identity(), value_spec) with warnings.catch_warnings(record=True): warnings.simplefilter('error', DeprecationWarning) with self.assertRaisesRegex(DeprecationWarning, 'tff.utils.build_encoded_broadcast()'): encoding_utils.build_encoded_broadcast(value, encoder)
def test_build_encoded_broadcast(self, value_constructor, encoder_constructor): value = value_constructor(np.random.rand(20)) value_spec = tf.TensorSpec(value.shape, tf.dtypes.as_dtype(value.dtype)) value_type = computation_types.to_type(value_spec) encoder = te.encoders.as_simple_encoder(encoder_constructor(), value_spec) broadcast_fn = encoding_utils.build_encoded_broadcast(value, encoder) state_type = broadcast_fn._initialize_fn.type_signature.result broadcast_signature = computations.federated_computation( broadcast_fn._next_fn, computation_types.FederatedType( broadcast_fn._initialize_fn.type_signature.result, placements.SERVER), computation_types.FederatedType(value_type, placements.SERVER)).type_signature self.assertIsInstance(broadcast_fn, StatefulBroadcastFn) self.assertEqual(state_type, broadcast_signature.result[0].member) self.assertEqual(placements.SERVER, broadcast_signature.result[0].placement) self.assertEqual(value_type, broadcast_signature.result[1].member) self.assertEqual(placements.CLIENTS, broadcast_signature.result[1].placement)
def build_encoded_broadcast_from_model( model_fn: _ModelConstructor, encoder_fn: _EncoderConstructor) -> computation_utils.StatefulBroadcastFn: """Builds `StatefulBroadcastFn` for weights of model returned by `model_fn`. This method creates a `SimpleEncoder` for every weight of model created by `model_fn`, as returned by `encoder_fn`. Args: model_fn: A Python callable with no arguments function that returns a `tff.learning.Model`. encoder_fn: A Python callable with a single argument, which is expected to be a `tf.Tensor` of shape and dtype to be encoded. The function must return a `tensor_encoding.core.SimpleEncoder`, which expects a `tf.Tensor` with compatible type as the input to its `encode` method. Returns: A `StatefulBroadcastFn` for encoding and broadcasting the weights of model created by `model_fn`. Raises: TypeError: If `model_fn` or `encoder_fn` are not callable objects. """ warnings.warn( 'Deprecation warning: ' 'tff.learning.framework.build_encoded_broadcast_from_model() is ' 'deprecated, use ' 'tff.learning.framework.build_encoded_broadcast_process_from_model() ' 'instead.', DeprecationWarning) py_typecheck.check_callable(model_fn) py_typecheck.check_callable(encoder_fn) weights = _weights_from_model_fn(model_fn) encoders = tf.nest.map_structure(encoder_fn, weights) return encoding_utils.build_encoded_broadcast(weights, encoders)
def test_build_encoded_broadcast(self, value_constructor, encoder_constructor): value = value_constructor(np.random.rand(20)) value_spec = tf.TensorSpec(value.shape, tf.as_dtype(value.dtype)) value_type = tff.to_type(value_spec) encoder = te.core.SimpleEncoder(encoder_constructor(), value_spec) broadcast_fn = encoding_utils.build_encoded_broadcast(value, encoder) broadcast_signature = broadcast_fn._next_fn.type_signature self.assertIsInstance(broadcast_fn, StatefulBroadcastFn) self.assertEqual(value_type, broadcast_signature.parameter[1].member) self.assertEqual(value_type, broadcast_signature.result[1].member) self.assertEqual(broadcast_signature.parameter[1].placement, tff.SERVER) self.assertEqual(broadcast_signature.result[1].placement, tff.CLIENTS)
def test_build_encoded_broadcast_raises_bad_structure(self): value = [tf.constant([0.0, 1.0]), tf.constant([0.0, 1.0])] encoder = te.encoders.as_simple_encoder(te.encoders.identity(), tf.TensorSpec((2,))) with self.assertRaises(ValueError): encoding_utils.build_encoded_broadcast(value, encoder)
def test_build_encoded_broadcast_raises_incompatible_encoder(self): value = tf.constant([0.0, 1.0]) incompatible_encoder = te.encoders.as_simple_encoder( te.encoders.identity(), tf.TensorSpec((3,))) with self.assertRaises(TypeError): encoding_utils.build_encoded_broadcast(value, incompatible_encoder)
def test_build_encoded_broadcast_raises_bad_encoder(self, bad_encoder): value = tf.constant([0.0, 1.0]) with self.assertRaises(TypeError): encoding_utils.build_encoded_broadcast(value, bad_encoder)