예제 #1
0
  def federated_secure_modular_sum(arg):
    py_typecheck.check_type(arg, building_blocks.ComputationBuildingBlock)
    arg.type_signature.check_struct()
    if arg.type_signature.is_struct_with_python():
      container_type = arg.type_signature.python_container
    else:
      container_type = None
    summand_arg = building_blocks.Selection(arg, index=0)
    raw_summed_values = building_block_factory.create_federated_sum(summand_arg)

    unplaced_modulus = building_blocks.Selection(arg, index=1)
    placed_modulus = building_block_factory.create_federated_value(
        unplaced_modulus, placements.SERVER)
    modulus_arg = building_block_factory.create_federated_zip(
        building_blocks.Struct([raw_summed_values, placed_modulus],
                               container_type=container_type))

    def map_structure_mod(summed_values, modulus):
      modulus = _ensure_structure(modulus, unplaced_modulus.type_signature,
                                  raw_summed_values.type_signature.member)
      return structure.map_structure(tf.math.mod, summed_values, modulus)

    modulus_fn = building_block_factory.create_tensorflow_binary_operator(
        map_structure_mod,
        operand_type=raw_summed_values.type_signature.member,
        second_operand_type=placed_modulus.type_signature.member)
    modulus_computed = building_block_factory.create_federated_apply(
        modulus_fn, modulus_arg)

    return modulus_computed
예제 #2
0
 def federated_sum(self, value):
   """Implements `federated_sum` as defined in `api/intrinsics.py`."""
   value = value_impl.to_value(value, None, self._context_stack)
   value = value_utils.ensure_federated_value(value, placements.CLIENTS,
                                              'value to be summed')
   type_utils.check_is_sum_compatible(value.type_signature)
   value = value_impl.ValueImpl.get_comp(value)
   comp = building_block_factory.create_federated_sum(value)
   return value_impl.ValueImpl(comp, self._context_stack)
예제 #3
0
 def test_finds_aggregate_dependent_on_aggregate(self):
     aggregate = building_block_test_utils.create_whimsy_called_federated_aggregate(
     )
     broadcasted_aggregate = building_block_factory.create_federated_broadcast(
         aggregate)
     second_aggregate = building_block_factory.create_federated_sum(
         broadcasted_aggregate)
     with self.assertRaises(ValueError):
         tree_analysis.check_aggregate_not_dependent_on_aggregate(
             second_aggregate)
예제 #4
0
def create_whimsy_called_federated_sum(value_type=tf.int32):
    r"""Returns a whimsy called federated sum.

                Call
               /    \
  federated_sum      data

  Args:
    value_type: The type of the value.
  """
    federated_type = computation_types.FederatedType(value_type,
                                                     placements.CLIENTS)
    value = building_blocks.Data('data', federated_type)
    return building_block_factory.create_federated_sum(value)
예제 #5
0
    def federated_sum(self, value):
        """Implements `federated_sum` as defined in `api/intrinsics.py`."""
        value = value_impl.to_value(value, None, self._context_stack)
        value = value_utils.ensure_federated_value(value, placements.CLIENTS,
                                                   'value to be summed')

        if not type_utils.is_sum_compatible(value.type_signature):
            raise TypeError(
                'The value type {} is not compatible with the sum operator.'.
                format(value.type_signature))

        value = value_impl.ValueImpl.get_comp(value)
        comp = building_block_factory.create_federated_sum(value)
        return value_impl.ValueImpl(comp, self._context_stack)
예제 #6
0
def federated_sum(value):
    """Computes a sum at `tff.SERVER` of a `value` placed on the `tff.CLIENTS`.

  To sum integer values with stronger privacy properties, consider using
  `tff.federated_secure_sum_bitwidth`.

  Args:
    value: A value of a TFF federated type placed at the `tff.CLIENTS`.

  Returns:
    A representation of the sum of the member constituents of `value` placed
    on the `tff.SERVER`.

  Raises:
    TypeError: If the argument is not a federated TFF value placed at
      `tff.CLIENTS`.
  """
    value = value_impl.to_value(value, None)
    value = value_utils.ensure_federated_value(value, placements.CLIENTS,
                                               'value to be summed')
    type_analysis.check_is_sum_compatible(value.type_signature)
    comp = building_block_factory.create_federated_sum(value.comp)
    comp = _bind_comp_as_reference(comp)
    return value_impl.Value(comp)