async def compute_federated_secure_sum(
      self, arg: federated_resolving_strategy.FederatedResolvingStrategyValue
  ) -> federated_resolving_strategy.FederatedResolvingStrategyValue:
    py_typecheck.check_type(arg.internal_representation, structure.Struct)
    py_typecheck.check_len(arg.internal_representation, 2)
    logging.warning(
        'The implementation of the `tff.federated_secure_sum` intrinsic '
        'provided by the `tff.backends.test` runtime uses no cryptography.')

    server_ex = self._target_executors[placement_literals.SERVER][0]
    value = federated_resolving_strategy.FederatedResolvingStrategyValue(
        arg.internal_representation[0], arg.type_signature[0])
    bitwidth = await arg.internal_representation[1].compute()
    bitwidth_type = arg.type_signature[1]
    sum_result, mask, fn = await asyncio.gather(
        self.compute_federated_sum(value),
        executor_utils.embed_tf_constant(self._executor, bitwidth_type,
                                         2**bitwidth - 1),
        executor_utils.embed_tf_binary_operator(self._executor, bitwidth_type,
                                                tf.math.mod))
    fn_arg = await server_ex.create_struct([
        sum_result.internal_representation[0],
        mask.internal_representation,
    ])
    fn_arg_type = computation_types.FederatedType(
        fn_arg.type_signature, placement_literals.SERVER, all_equal=True)
    arg = federated_resolving_strategy.FederatedResolvingStrategyValue(
        structure.Struct([
            (None, fn.internal_representation),
            (None, [fn_arg]),
        ]), computation_types.StructType([fn.type_signature, fn_arg_type]))
    return await self.compute_federated_map_all_equal(arg)
  async def _compute_intrinsic_federated_mean(self, arg):
    type_utils.check_federated_type(
        arg.type_signature, placement=placement_literals.CLIENTS)
    member_type = arg.type_signature.member

    async def _compute_total():
      total = await self._compute_intrinsic_federated_sum(arg)
      total = await total.compute()
      return await self._parent_executor.create_value(total, member_type)

    async def _compute_factor():
      cardinalities = await self._get_cardinalities()
      count = sum(cardinalities)
      return await executor_utils.embed_tf_scalar_constant(
          self._parent_executor, member_type, float(1.0 / count))

    async def _compute_multiply_arg():
      total, factor = tuple(await asyncio.gather(_compute_total(),
                                                 _compute_factor()))
      return await self._parent_executor.create_tuple([total, factor])

    multiply_fn, multiply_arg = tuple(await asyncio.gather(
        executor_utils.embed_tf_binary_operator(self._parent_executor,
                                                member_type, tf.multiply),
        _compute_multiply_arg()))
    result = await self._parent_executor.create_call(multiply_fn, multiply_arg)
    type_signature = type_factory.at_server(member_type)
    return CompositeValue(result, type_signature)
    async def compute_federated_mean(
        self, arg: FederatedComposingStrategyValue
    ) -> FederatedComposingStrategyValue:
        type_analysis.check_federated_type(
            arg.type_signature, placement=placement_literals.CLIENTS)
        member_type = arg.type_signature.member

        async def _create_total():
            total = await self.compute_federated_sum(arg)
            total = await total.compute()
            return await self._server_executor.create_value(total, member_type)

        async def _create_factor():
            cardinalities = await self._get_cardinalities()
            count = sum(cardinalities)
            return await executor_utils.embed_tf_constant(
                self._server_executor, member_type, float(1.0 / count))

        async def _create_multiply_arg():
            total, factor = await asyncio.gather(_create_total(),
                                                 _create_factor())
            return await self._server_executor.create_struct([total, factor])

        multiply_fn, multiply_arg = await asyncio.gather(
            executor_utils.embed_tf_binary_operator(self._server_executor,
                                                    member_type, tf.multiply),
            _create_multiply_arg())
        result = await self._server_executor.create_call(
            multiply_fn, multiply_arg)
        type_signature = computation_types.at_server(member_type)
        return FederatedComposingStrategyValue(result, type_signature)
 async def _compute_intrinsic_federated_sum(self, arg):
   type_utils.check_federated_type(
       arg.type_signature, placement=placement_literals.CLIENTS)
   zero, plus, identity = tuple(await asyncio.gather(*[
       executor_utils.embed_tf_scalar_constant(self, arg.type_signature.member,
                                               0),
       executor_utils.embed_tf_binary_operator(self, arg.type_signature.member,
                                               tf.add),
       self.create_value(
           computation_factory.create_lambda_identity(
               arg.type_signature.member),
           type_factory.unary_op(arg.type_signature.member))
   ]))
   aggregate_args = await self.create_tuple([arg, zero, plus, plus, identity])
   return await self._compute_intrinsic_federated_aggregate(aggregate_args)
 async def compute_federated_sum(
     self, arg: FederatedComposingStrategyValue
 ) -> FederatedComposingStrategyValue:
     type_analysis.check_federated_type(
         arg.type_signature, placement=placement_literals.CLIENTS)
     id_comp, id_type = tensorflow_computation_factory.create_identity(
         arg.type_signature.member)
     zero, plus, identity = await asyncio.gather(
         executor_utils.embed_tf_constant(self._executor,
                                          arg.type_signature.member, 0),
         executor_utils.embed_tf_binary_operator(self._executor,
                                                 arg.type_signature.member,
                                                 tf.add),
         self._executor.create_value(id_comp, id_type))
     aggregate_args = await self._executor.create_struct(
         [arg, zero, plus, plus, identity])
     return await self.compute_federated_aggregate(aggregate_args)
Exemple #6
0
 async def _compute_intrinsic_federated_mean(self, arg):
   arg_sum = await self._compute_intrinsic_federated_sum(arg)
   member_type = arg_sum.type_signature.member
   count = float(len(arg.internal_representation))
   if count < 1.0:
     raise RuntimeError('Cannot compute a federated mean over an empty group.')
   child = self._target_executors[placement_literals.SERVER][0]
   factor, multiply = tuple(await asyncio.gather(*[
       executor_utils.embed_tf_scalar_constant(child, member_type,
                                               float(1.0 / count)),
       executor_utils.embed_tf_binary_operator(child, member_type, tf.multiply)
   ]))
   multiply_arg = await child.create_tuple(
       anonymous_tuple.AnonymousTuple([(None,
                                        arg_sum.internal_representation[0]),
                                       (None, factor)]))
   result = await child.create_call(multiply, multiply_arg)
   return FederatingExecutorValue([result], arg_sum.type_signature)
 async def compute_federated_sum(
     self,
     arg: FederatedResolvingStrategyValue) -> FederatedResolvingStrategyValue:
   py_typecheck.check_type(arg.type_signature, computation_types.FederatedType)
   zero, plus = await asyncio.gather(
       executor_utils.embed_tf_constant(self._executor,
                                        arg.type_signature.member, 0),
       executor_utils.embed_tf_binary_operator(self._executor,
                                               arg.type_signature.member,
                                               tf.add))
   return await self.compute_federated_reduce(
       FederatedResolvingStrategyValue(
           structure.Struct([(None, arg.internal_representation),
                             (None, zero.internal_representation),
                             (None, plus.internal_representation)]),
           computation_types.StructType(
               (arg.type_signature, zero.type_signature, plus.type_signature)))
   )
Exemple #8
0
 async def _compute_intrinsic_federated_sum(self, arg):
   py_typecheck.check_type(arg.type_signature, computation_types.FederatedType)
   zero, plus = tuple(await
                      asyncio.gather(*[
                          executor_utils.embed_tf_scalar_constant(
                              self, arg.type_signature.member, 0),
                          executor_utils.embed_tf_binary_operator(
                              self, arg.type_signature.member, tf.add)
                      ]))
   return await self._compute_intrinsic_federated_reduce(
       FederatingExecutorValue(
           anonymous_tuple.AnonymousTuple([
               (None, arg.internal_representation),
               (None, zero.internal_representation),
               (None, plus.internal_representation)
           ]),
           computation_types.NamedTupleType(
               (arg.type_signature, zero.type_signature, plus.type_signature)))
   )
 async def compute_federated_mean(
     self,
     arg: FederatedResolvingStrategyValue) -> FederatedResolvingStrategyValue:
   arg_sum = await self.compute_federated_sum(arg)
   member_type = arg_sum.type_signature.member
   count = float(len(arg.internal_representation))
   if count < 1.0:
     raise RuntimeError('Cannot compute a federated mean over an empty group.')
   child = self._target_executors[placement_literals.SERVER][0]
   factor, multiply = await asyncio.gather(
       executor_utils.embed_tf_constant(child, member_type,
                                        float(1.0 / count)),
       executor_utils.embed_tf_binary_operator(child, member_type,
                                               tf.multiply))
   multiply_arg = await child.create_struct(
       structure.Struct([(None, arg_sum.internal_representation[0]),
                         (None, factor)]))
   result = await child.create_call(multiply, multiply_arg)
   return FederatedResolvingStrategyValue([result], arg_sum.type_signature)
Exemple #10
0
 async def _compute_intrinsic_federated_mean(self, arg):
   member_type = arg.type_signature.member
   ones = await self.create_value(
       1, type_factory.at_clients(member_type, all_equal=True))
   totals = (await self._compute_intrinsic_federated_sum(
       await self._compute_intrinsic_federated_zip_at_clients(
           await self.create_tuple([arg, ones])))).internal_representation
   py_typecheck.check_type(totals, executor_value_base.ExecutorValue)
   fed_sum, count = tuple(await asyncio.gather(
       self._parent_executor.create_selection(totals, index=0),
       self._parent_executor.create_selection(totals, index=1)))
   count_val = await count.compute()
   factor, multiply = tuple(await asyncio.gather(*[
       executor_utils.embed_tf_scalar_constant(
           self._parent_executor, member_type, float(1.0 / count_val)),
       executor_utils.embed_tf_binary_operator(self._parent_executor,
                                               member_type, tf.multiply)
   ]))
   multiply_arg = await self._parent_executor.create_tuple([fed_sum, factor])
   result = await self._parent_executor.create_call(multiply, multiply_arg)
   return CompositeValue(result, type_factory.at_server(member_type))