Exemple #1
0
 def test_federated_sum_with_server_int(self):
     x = _mock_data_of_type(computation_types.at_server(tf.int32))
     with self.assertRaises(TypeError):
         intrinsics.federated_sum(x)
 def after_merge_with_sum(original_arg, merged_arg):
   del merged_arg  # Unused
   # Second element in original arg is the clients-placed value.
   return intrinsics.federated_sum(original_arg[1])
 def comp():
     value = intrinsics.federated_value(10, placements.CLIENTS)
     return intrinsics.federated_sum(value)
Exemple #4
0
 def test_federated_sum_with_client_int(self):
     x = _mock_data_of_type(computation_types.at_clients(tf.int32))
     val = intrinsics.federated_sum(x)
     self.assert_value(val, 'int32@SERVER')
 def aggregate_metrics(client_metrics):
     return collections.OrderedDict(
         num_over=intrinsics.federated_sum(client_metrics.num_over))
Exemple #6
0
 def next_fn(state, cluster_centers, client_data):
     client_result, stat_output = intrinsics.federated_map(
         client_update, (cluster_centers, client_data))
     stat_metrics = intrinsics.federated_sum(stat_output)
     return measured_process.MeasuredProcessOutput(state, client_result,
                                                   stat_metrics)
 def odict_next_fn(state, val):
   return collections.OrderedDict(
       state=state,
       result=intrinsics.federated_sum(val),
       measurements=server_zero())
Exemple #8
0
 def next_fn(state, client_values):
     metrics = intrinsics.federated_map(sum_sequence, client_values)
     metrics = intrinsics.federated_sum(metrics)
     return LearningProcessOutput(state, metrics)
 def tuple_next_fn(state, val):
   return state, intrinsics.federated_sum(val), server_zero()
 def namedtuple_next_fn(state, val):
   return measured_process_output(state, intrinsics.federated_sum(val),
                                  server_zero())
 def float_next_fn(state, val):
   del state
   return MeasuredProcessOutput(
       intrinsics.federated_value(0.0, placements.SERVER),
       intrinsics.federated_sum(val), server_zero())
Exemple #12
0
 def next_fn(state, client_values, second_state):
   del second_state  # Unused.
   metrics = intrinsics.federated_map(sum_dataset, client_values)
   metrics = intrinsics.federated_sum(metrics)
   return LearningProcessOutput(state, metrics)
 def up_to_merge(server_arg, client_arg):
   del server_arg  # Unused
   return intrinsics.federated_sum(client_arg)
Exemple #14
0
 def odict_next_fn(state, client_values):
     metrics = intrinsics.federated_map(sum_sequence, client_values)
     metrics = intrinsics.federated_sum(metrics)
     return collections.OrderedDict(state=state, metrics=metrics)
 def next_fn(state, val):
   return intrinsics.federated_zip(
       MeasuredProcessOutput(state, intrinsics.federated_sum(val),
                             server_zero()))
Exemple #16
0
 def next_fn(state, client_values, second_state):  # pylint: disable=unused-argument
     metrics = intrinsics.federated_map(sum_sequence, client_values)
     metrics = intrinsics.federated_sum(metrics)
     return LearningProcessOutput(state, metrics)
 def next_fn(state, val):
   return MeasuredProcessOutput(state, intrinsics.federated_sum(val), val)
Exemple #18
0
def unsecure_aggregation():
    data_at_clients = intrinsics.federated_value(1, placements.CLIENTS)
    return intrinsics.federated_sum(data_at_clients)
 def weighted_next_fn(state, value, weight):
   del weight
   return MeasuredProcessOutput(
       state, intrinsics.federated_sum(value),
       intrinsics.federated_value(1, placements.SERVER))
 def next_fn(state, weights, update):
   return MeasuredProcessOutput(
       state, test_finalizer_result(weights,
                                    intrinsics.federated_sum(update)),
       server_zero())
 def next_fn(state, val):
   return MeasuredProcessOutput(
       state, intrinsics.federated_sum(val),
       intrinsics.federated_value(1, placements.SERVER))
Exemple #22
0
 def next_fn(server_state, client_data):
     del server_state  # Unused
     return intrinsics.federated_sum(
         intrinsics.federated_map(reduce_dataset, client_data))
Exemple #23
0
def secure_quantized_sum(client_value, lower_bound, upper_bound):
    """Quantizes and sums values securely.

  Provided `client_value` can be either a Tensor or a nested structure of
  Tensors. If it is a nested structure, `lower_bound` and `upper_bound` must be
  either both scalars, or both have the same structure as `client_value`, with
  each element being a scalar, representing the bounds to be used for each
  corresponding Tensor in `client_value`.

  This method converts each Tensor in provided `client_value` to appropriate
  format and uses the `tff.federated_secure_sum_bitwidth` operator to realize
  the sum.

  The dtype of Tensors in provided `client_value` can be one of `[tf.int32,
  tf.int64, tf.float32, tf.float64]`.

  If the dtype of `client_value` is `tf.int32` or `tf.int64`, the summation is
  possibly exact, depending on `lower_bound` and `upper_bound`: In the case that
  `upper_bound - lower_bound < 2**32`, the summation will be exact. If it is
  not, `client_value` will be quantized to precision of 32 bits, so the worst
  case error introduced for the value of each client will be approximately
  `(upper_bound - lower_bound) / 2**32`. Deterministic rounding to nearest value
  is used in such cases.

  If the dtype of `client_value` is `tf.float32` or `tf.float64`, the summation
  is generally *not* accurate up to full floating point precision. Instead, the
  values are first clipped to the `[lower_bound, upper_bound]` range. These
  values are then uniformly quantized to 32 bit resolution, using deterministic
  rounding to round the values to the quantization points. Rounding happens
  roughly as follows (implementation is a bit more complex to mitigate numerical
  stability issues):

  ```
  values = tf.round(
      (client_value - lower_bound) * ((2**32 - 1) / (upper_bound - lower_bound))
  ```

  After summation, the inverse operation if performed, so the return value
  is of the same dtype as the input `client_value`.

  In terms of accuracy, it is safe to assume accuracy within 7-8 significant
  digits for `tf.float32` inputs, and 8-9 significant digits for `tf.float64`
  inputs, where the significant digits refer to precision relative to the range
  of the provided bounds. Thus, these bounds should not be set extremely wide.
  Accuracy losses arise due to (1) quantization within the given clipping range,
  (2) float precision of final outputs (e.g. `tf.float32` has 23 bits in its
  mantissa), and (3) precision losses that arise in doing math on `tf.float32`
  and `tf.float64` inputs.

  As a concrete example, if the range is `+/- 1000`, errors up to `1e-4` per
  element should be expected for `tf.float32` and up to `1e-5` for `tf.float64`.

  Args:
    client_value: A `tff.Value` placed at `tff.CLIENTS`.
    lower_bound: The smallest possible value for `client_value` (inclusive).
      Values smaller than this bound will be clipped. Must be either a scalar or
      a nested structure of scalars, matching the structure of `client_value`.
      Must be either a Python constant or a `tff.Value` placed at `tff.SERVER`,
      with dtype matching that of `client_value`.
    upper_bound: The largest possible value for `client_value` (inclusive).
      Values greater than this bound will be clipped. Must be either a scalar or
      a nested structure of scalars, matching the structure of `client_value`.
      Must be either a Python constant or a `tff.Value` placed at `tff.SERVER`,
      with dtype matching that of `client_value`.

  Returns:
    Summed `client_value` placed at `tff.SERVER`, of the same dtype as
    `client_value`.

  Raises:
    TypeError (or its subclasses): If input arguments do not satisfy the type
      constraints specified above.
  """

    # Possibly converts Python constants to federated values.
    client_value, lower_bound, upper_bound = _normalize_secure_quantized_sum_args(
        client_value, lower_bound, upper_bound)

    # This object is used during decoration of the `client_shift` method, and the
    # value stored in this mutable container is used during decoration of the
    # `server_shift` method. The reason for this is that we cannot currently get
    # the needed information out of `client_value.type_signature.member` as we
    # need both the `TensorType` information as well as the Python container
    # attached to them.
    temp_box = []

    # These tf_computations assume the inputs were already validated. In
    # particular, that lower_bnd and upper_bnd have the same structure, and if not
    # scalar, the structure matches the structure of value.
    @computations.tf_computation()
    def client_shift(value, lower_bnd, upper_bnd):
        assert not temp_box
        temp_box.append(tf.nest.map_structure(lambda v: v.dtype, value))
        fn = _client_tensor_shift_for_secure_sum
        if tf.is_tensor(lower_bnd):
            return tf.nest.map_structure(lambda v: fn(v, lower_bnd, upper_bnd),
                                         value)
        else:
            return tf.nest.map_structure(fn, value, lower_bnd, upper_bnd)

    @computations.tf_computation()
    def server_shift(value, lower_bnd, upper_bnd, summands):
        fn = _server_tensor_shift_for_secure_sum
        if tf.is_tensor(lower_bnd):
            return tf.nest.map_structure(
                lambda v, dtype: fn(summands, v, lower_bnd, upper_bnd, dtype),
                value, temp_box[0])
        else:
            return tf.nest.map_structure(lambda *args: fn(summands, *args),
                                         value, lower_bnd, upper_bnd,
                                         temp_box[0])

    client_one = intrinsics.federated_value(1, placements.CLIENTS)

    # Orchestration.
    client_lower_bound = intrinsics.federated_broadcast(lower_bound)
    client_upper_bound = intrinsics.federated_broadcast(upper_bound)

    value = intrinsics.federated_map(
        client_shift, (client_value, client_lower_bound, client_upper_bound))
    num_summands = intrinsics.federated_sum(client_one)

    secagg_value_type = value.type_signature.member
    assert secagg_value_type.is_tensor() or secagg_value_type.is_struct()
    if secagg_value_type.is_tensor():
        bitwidths = 32
    else:
        bitwidths = structure.map_structure(lambda t: 32, secagg_value_type)

    value = intrinsics.federated_secure_sum_bitwidth(value, bitwidth=bitwidths)
    value = intrinsics.federated_map(
        server_shift, (value, lower_bound, upper_bound, num_summands))
    return value
Exemple #24
0
 def foo(x):
     return intrinsics.federated_sum(x)
Exemple #25
0
 def tuple_next_fn(state, client_values):
     metrics = intrinsics.federated_map(sum_sequence, client_values)
     metrics = intrinsics.federated_sum(metrics)
     return (state, metrics)
Exemple #26
0
 def test_federated_sum_with_client_string(self):
     x = _mock_data_of_type(computation_types.at_clients(tf.string))
     with self.assertRaises(TypeError):
         intrinsics.federated_sum(x)
Exemple #27
0
 def namedtuple_next_fn(state, client_values):
     metrics = intrinsics.federated_map(sum_sequence, client_values)
     metrics = intrinsics.federated_sum(metrics)
     return learning_process_output(state, metrics)
Exemple #28
0
 def next_fn(state, value):
     summed_value = intrinsics.federated_sum(value)
     empty_measurements = intrinsics.federated_value((),
                                                     placements.SERVER)
     return measured_process.MeasuredProcessOutput(
         state, summed_value, empty_measurements)
 def map_sum(state, values):
   return MeasuredProcessOutput(
       state=intrinsics.federated_map(add_one, state),
       result=intrinsics.federated_sum(values),
       measurements=intrinsics.federated_value(
           collections.OrderedDict(b=2), placements.SERVER))