Exemplo n.º 1
0
  def federated_secure_sum(arg):
    py_typecheck.check_type(arg, building_blocks.ComputationBuildingBlock)
    summand_arg = building_blocks.Selection(arg, index=0)
    summand_type = summand_arg.type_signature.member
    max_input_arg = building_blocks.Selection(arg, index=1)
    max_input_type = max_input_arg.type_signature

    # Add the max_value as a second value in the zero, so it can be read during
    # `accumulate` to ensure client summands are valid. The value will be
    # later dropped in `report`.
    #
    # While accumulating summands, we'll assert each summand is less than or
    # equal to max_input. Otherwise the comptuation should issue an error.
    summation_zero = building_block_factory.create_generic_constant(
        summand_type, 0)
    aggregation_zero = building_blocks.Struct([summation_zero, max_input_arg],
                                              container_type=tuple)

    def assert_less_equal_max_and_add(summation_and_max_input, summand):
      summation, original_max_input = summation_and_max_input
      max_input = _ensure_structure(original_max_input, max_input_type,
                                    summand_type)

      # Assert that all coordinates in all tensors are less than the secure sum
      # allowed max input value.
      def assert_all_coordinates_less_equal(x, m):
        return tf.Assert(
            tf.reduce_all(
                tf.less_equal(tf.cast(x, tf.int64), tf.cast(m, tf.int64))), [
                    'client value larger than maximum specified for secure sum',
                    x, 'not less than or equal to', m
                ])

      assert_ops = structure.flatten(
          structure.map_structure(assert_all_coordinates_less_equal, summand,
                                  max_input))
      with tf.control_dependencies(assert_ops):
        return structure.map_structure(tf.add, summation,
                                       summand), original_max_input

    assert_less_equal_and_add = building_block_factory.create_tensorflow_binary_operator(
        assert_less_equal_max_and_add,
        operand_type=aggregation_zero.type_signature,
        second_operand_type=summand_type)

    def nested_plus(a, b):
      return structure.map_structure(tf.add, a, b)

    plus_op = building_block_factory.create_tensorflow_binary_operator(
        nested_plus, operand_type=aggregation_zero.type_signature)

    # In the `report` function we take the summation and drop the second element
    # of the struct (which was holding the max_value).
    drop_max_value_op = building_block_factory.create_tensorflow_unary_operator(
        lambda x: type_conversions.type_to_py_container(x[0], summand_type),
        aggregation_zero.type_signature)

    return building_block_factory.create_federated_aggregate(
        summand_arg, aggregation_zero, assert_less_equal_and_add, plus_op,
        drop_max_value_op)
Exemplo n.º 2
0
 def federated_sum(x):
   py_typecheck.check_type(x, building_blocks.ComputationBuildingBlock)
   operand_type = x.type_signature.member
   zero = building_block_factory.create_generic_constant(operand_type, 0)
   plus_op = building_block_factory.create_tensorflow_binary_operator_with_upcast(
       tf.add, computation_types.StructType([operand_type, operand_type]))
   identity = building_block_factory.create_identity(operand_type)
   return building_block_factory.create_federated_aggregate(
       x, zero, plus_op, plus_op, identity)
Exemplo n.º 3
0
 def federated_sum(x):
     zero = value_impl.ValueImpl(
         building_block_factory.create_generic_constant(
             x.type_signature.member, 0), context_stack)
     plus_op = value_impl.ValueImpl(
         building_block_factory.create_binary_operator_with_upcast(
             computation_types.NamedTupleType(
                 [x.type_signature.member, x.type_signature.member]),
             tf.add), context_stack)
     return federated_reduce([x, zero, plus_op])
Exemplo n.º 4
0
 def federated_sum(x):
     operand_type = x.type_signature.member
     zero = value_impl.ValueImpl(
         building_block_factory.create_generic_constant(operand_type, 0),
         context_stack)
     plus_op = value_impl.ValueImpl(
         building_block_factory.
         create_tensorflow_binary_operator_with_upcast(
             computation_types.StructType([operand_type, operand_type]),
             tf.add), context_stack)
     return federated_reduce([x, zero, plus_op])
Exemplo n.º 5
0
 def federated_mean(arg):
     one = value_impl.ValueImpl(
         building_block_factory.create_generic_constant(
             arg.type_signature, 1), context_stack)
     arg = value_impl.to_value([arg, one], None, context_stack)
     return federated_weighted_mean(arg)
 def federated_mean(arg):
     py_typecheck.check_type(arg, building_blocks.ComputationBuildingBlock)
     one = building_block_factory.create_generic_constant(
         arg.type_signature, 1)
     mean_arg = building_blocks.Struct([(None, arg), (None, one)])
     return federated_weighted_mean(mean_arg)