Ejemplo n.º 1
0
def bind_single_selection_as_argument_to_lower_level_lambda(comp, index):
  r"""Binds selection from the param of `comp` as param to lower-level lambda.

  The returned pattern is quite important here; given an input lambda `comp`,
  we will return an equivalent structure of the form:


                                    Lambda(x)
                                       |
                                      Call
                                    /      \
                              Lambda        Selection from x

  Args:
    comp: Instance of `tff_framework.Lambda`, whose parameters we wish to rebind
      to a different lambda. This lambda must have unique names.
    index: `int` representing the index to bind as an argument to the
      lower-level lambda.

  Returns:
    An instance of `tff_framework.Lambda`, equivalent to `comp`, satisfying the
    pattern above.
  """
  py_typecheck.check_type(comp, tff_framework.Lambda)
  py_typecheck.check_type(index, int)
  tff_framework.check_has_unique_names(comp)
  comp = _prepare_for_rebinding(comp)
  name_generator = tff_framework.unique_name_generator(comp)
  parameter_name = comp.parameter_name
  new_name = six.next(name_generator)
  new_ref = tff_framework.Reference(new_name,
                                    comp.type_signature.parameter[index])

  def _remove_selection_from_ref(inner_comp):
    if isinstance(inner_comp, tff_framework.Selection) and isinstance(
        inner_comp.source, tff_framework.Reference
    ) and inner_comp.index == index and inner_comp.source.name == parameter_name:
      return new_ref, True
    return inner_comp, False

  references_rebound_in_result, _ = tff_framework.transform_postorder(
      comp.result, _remove_selection_from_ref)
  newly_bound_lambda = tff_framework.Lambda(new_ref.name,
                                            new_ref.type_signature,
                                            references_rebound_in_result)
  _check_for_missed_binding(comp, newly_bound_lambda)
  original_ref = tff_framework.Reference(comp.parameter_name,
                                         comp.parameter_type)
  selection = tff_framework.Selection(original_ref, index=index)
  called_rebound = tff_framework.Call(newly_bound_lambda, selection)
  return tff_framework.Lambda(comp.parameter_name, comp.parameter_type,
                              called_rebound)
Ejemplo n.º 2
0
def _construct_selection_from_federated_tuple(federated_tuple, selected_index,
                                              name_generator):
  """Selects the index `selected_index` from `federated_tuple`.

  Args:
    federated_tuple: Instance of `tff_framework.ComputationBuildingBlock` of
      federated named tuple type from which we wish to select one of the tuple's
      elements.
    selected_index: Integer index we wish to select from `federated_tuple`.
    name_generator: `generator` to generate unique names in the construction.

  Returns:
    An instance of `tff_framework.ComputationBuildingBlock` representing index
    `selected_index` from `federated_tuple`, still federated at the same
    placement.
  """
  py_typecheck.check_type(federated_tuple,
                          tff_framework.ComputationBuildingBlock)
  py_typecheck.check_type(selected_index, int)
  py_typecheck.check_type(federated_tuple.type_signature, tff.FederatedType)
  py_typecheck.check_type(federated_tuple.type_signature.member,
                          tff.NamedTupleType)
  unique_reference_name = six.next(name_generator)
  selection_function_ref = tff_framework.Reference(
      unique_reference_name, federated_tuple.type_signature.member)
  selected_building_block = tff_framework.Selection(
      selection_function_ref, index=selected_index)
  constructed_selection_function = tff_framework.Lambda(
      unique_reference_name, federated_tuple.type_signature.member,
      selected_building_block)
  return tff_framework.create_federated_map_or_apply(
      constructed_selection_function, federated_tuple)
Ejemplo n.º 3
0
 def _normalize_reference_bit(comp):
   if not isinstance(comp.type_signature, tff.FederatedType):
     return comp, False
   return tff_framework.Reference(
       comp.name,
       tff.FederatedType(comp.type_signature.member,
                         comp.type_signature.placement)), True
Ejemplo n.º 4
0
 def _rename_first_function_arg(comp):
   if isinstance(
       comp,
       tff_framework.Reference) and comp.name == first_function.parameter_name:
     if comp.type_signature != second_function.parameter_type:
       raise AssertionError('{}, {}'.format(comp.type_signature,
                                            second_function.parameter_type))
     return tff_framework.Reference(second_function.parameter_name,
                                    comp.type_signature), True
   return comp, False
Ejemplo n.º 5
0
def _extract_intrinsic_as_reference_to_top_level_lambda(comp, uri):
  """Extracts an intrinsic from `comp` as a reference for the given `uri`.

  Args:
    comp: The `tff_framework.Lambda` to transform. The names of lambda
      parameters and block variables in `comp` must be unique.
    uri: A URI of an intrinsic.

  Returns:
    A new computation with the transformation applied or the original `comp`.

  Raises:
    ValueError: If there is more than one intrinsic for the give `uri` or if the
      intrinsic is not exclusively bound by `comp`.
  """
  py_typecheck.check_type(comp, tff_framework.Lambda)
  tff_framework.check_has_unique_names(comp)
  py_typecheck.check_type(uri, six.string_types)
  intrinsics = _get_called_intrinsics(comp, uri)
  length = len(intrinsics)
  if length != 1:
    raise ValueError(
        'Expected a computation with exactly one intrinsic with the uri: {}, '
        'found: {}.'.format(uri, length))
  if not _are_comps_bound_exclusively_by_top_level_lambda(comp, intrinsics):
    raise ValueError(
        'Expected a computation which binds all the references in the '
        'intrinsic with the uri: {}.'.format(uri))
  name_generator = tff_framework.unique_name_generator(comp)
  extracted_intrinsic = intrinsics[0]
  ref_name = six.next(name_generator)
  ref_type = tff.to_type(extracted_intrinsic.type_signature)
  ref = tff_framework.Reference(ref_name, ref_type)

  def _should_transform(comp):
    return tff_framework.is_called_intrinsic(comp, uri)

  def _transform(comp):
    if not _should_transform(comp):
      return comp, False
    return ref, True

  comp, _ = tff_framework.transform_postorder(comp, _transform)
  comp = _insert_comp_in_top_level_lambda(
      comp, name=ref.name, comp_to_insert=extracted_intrinsic)
  return comp, True
Ejemplo n.º 6
0
def select_output_from_lambda(comp, indices):
  """Constructs a new function with result of selecting `indices` from `comp`.

  Args:
    comp: Instance of `tff_framework.Lambda` of result type `tff.NamedTupleType`
      from which we wish to select `indices`. Notice that this named tuple type
      must have elements of federated type.
    indices: Instance of `int`, `list`, or `tuple`, specifying the indices we
      wish to select from the result of `comp`. If `indices` is an `int`, the
      result of the returned `comp` will be of type at index `indices` in
      `comp.type_signature.result`. If `indices` is a `list` or `tuple`, the
      result type will be a `tff.NamedTupleType` wrapping the specified
      selections.

  Returns:
    A transformed version of `comp` with result value the selection from the
    result of `comp` specified by `indices`.
  """
  py_typecheck.check_type(comp, tff_framework.Lambda)
  py_typecheck.check_type(comp.type_signature.result, tff.NamedTupleType)
  py_typecheck.check_type(indices, (int, tuple, list))
  result_tuple = comp.result
  name_generator = tff_framework.unique_name_generator(comp)
  new_name = six.next(name_generator)
  ref_to_result_tuple = tff_framework.Reference(new_name,
                                                result_tuple.type_signature)
  if isinstance(indices, (tuple, list)):
    if not all(isinstance(x, int) for x in indices):
      raise TypeError('Must select by index in `select_output_from_lambda`.')
    selected_output = [
        tff_framework.Selection(ref_to_result_tuple, index=x) for x in indices
    ]
    tuple_of_selected_output = tff_framework.Tuple(selected_output)
    result = tff_framework.Block([(new_name, result_tuple)],
                                 tuple_of_selected_output)
  else:
    selected_output = tff_framework.Selection(
        ref_to_result_tuple, index=indices)
    result = tff_framework.Block([(new_name, result_tuple)], selected_output)
  return tff_framework.Lambda(comp.parameter_name, comp.parameter_type, result)
Ejemplo n.º 7
0
def zip_selection_as_argument_to_lower_level_lambda(comp, selected_index_lists):
  r"""Binds selections from the param of `comp` as params to lower-level lambda.

  Notice that `comp` must be a `tff_framework.Lambda`.

  The returned pattern is quite important here; given an input lambda `Comp`,
  we will return an equivalent structure of the form:


                                    Lambda(x)
                                       |
                                      Call
                                    /      \
                              Lambda        <Selections from x>

  Where <Selections from x> represents a tuple of selections from the parameter
  `x`, as specified by `selected_index_lists`. This transform is necessary in
  order to isolate spurious dependence on arguments that are not in fact used,
  for example after we have separated processing on the server from that which
  happens on the clients, but the server-processing still declares some
  parameters placed at the clients.

  `selected_index_lists` must be a list of lists. Each list represents
  a sequence of selections to the parameter of `comp`. For example, if `var`
  is the parameter of `comp`, the list `[0, 1, 0]` would represent the
  selection `x[0][1][0]`. The elements of these inner lists must be integers;
  that is, the selections must be positional. Notice we do not allow for tuples
  due to automatic unwrapping.

  Args:
    comp: Instance of `tff_framework.Lambda`, whose parameters we wish to rebind
      to a different lambda.
    selected_index_lists: 2-d list of `int`s, specifying the parameters of
      `comp` which we wish to rebind as the parameter to a lower-level lambda.

  Returns:
    An instance of `tff_framework.Lambda`, equivalent to `comp`, satisfying the
    pattern above.
  """
  py_typecheck.check_type(comp, tff_framework.Lambda)
  py_typecheck.check_type(selected_index_lists, list)
  for selection_list in selected_index_lists:
    py_typecheck.check_type(selection_list, list)
    for selected_element in selection_list:
      py_typecheck.check_type(selected_element, int)
  original_comp = comp
  comp = _prepare_for_rebinding(comp)

  top_level_parameter_type = comp.type_signature.parameter
  name_generator = tff_framework.unique_name_generator(comp)
  top_level_parameter_name = comp.parameter_name
  top_level_parameter_reference = tff_framework.Reference(
      top_level_parameter_name, comp.parameter_type)

  type_list = []
  for selection_list in selected_index_lists:
    try:
      selected_type = top_level_parameter_type
      for selection in selection_list:
        selected_type = selected_type[selection]
      type_list.append(selected_type)
    except TypeError:
      six.reraise(
          TypeError,
          TypeError(
              'You have tried to bind a variable to a nonexistent index in your '
              'lambda parameter type; the selection defined by {} is '
              'inadmissible for the lambda parameter type {}, in the comp {}.'
              .format(selection_list, top_level_parameter_type, original_comp)),
          sys.exc_info()[2])

  if not all(isinstance(x, tff.FederatedType) for x in type_list):
    raise TypeError(
        'All selected arguments should be of federated type; your selections '
        'have resulted in the list of types {}'.format(type_list))
  placement = type_list[0].placement
  if not all(x.placement is placement for x in type_list):
    raise ValueError(
        'In order to zip the argument to the lower-level lambda together, all '
        'selected arguments should be at the same placement. Your selections '
        'have resulted in the list of types {}'.format(type_list))

  arg_to_lower_level_lambda_list = []
  for selection_tuple in selected_index_lists:
    selected_comp = top_level_parameter_reference
    for selection in selection_tuple:
      selected_comp = tff_framework.Selection(selected_comp, index=selection)
    arg_to_lower_level_lambda_list.append(selected_comp)
  zip_arg = tff_framework.create_federated_zip(
      tff_framework.Tuple(arg_to_lower_level_lambda_list))

  zip_type = tff.FederatedType([x.member for x in type_list],
                               placement=placement)
  ref_to_zip = tff_framework.Reference(six.next(name_generator), zip_type)

  selections_from_zip = [
      _construct_selection_from_federated_tuple(ref_to_zip, x, name_generator)
      for x in range(len(selected_index_lists))
  ]

  def _replace_selections_with_new_bindings(inner_comp):
    """Identifies selection pattern and replaces with new binding.

    Detecting this pattern is the most brittle part of this rebinding function.
    It relies on pattern-matching, and right now we cannot guarantee that this
    pattern is present in every situation we wish to replace with a new
    binding.

    Args:
      inner_comp: Instance of `tff_framework.ComputationBuildingBlock` in which
        we wish to replace the selections specified by `selected_index_lists`
        with the parallel new bindings from `selections_from_zip`.

    Returns:
      A possibly transformed version of `inner_comp` with nodes matching the
      selection patterns replaced by their new bindings.
    """
    # TODO(b/135541729): Either come up with a preprocessing way to enforce
    # this is sufficient, or rework the should_transform predicate.
    for idx, tup in enumerate(selected_index_lists):
      selection = inner_comp  # Empty selection
      tuple_pattern_matched = True
      for selected_index in tup[::-1]:
        if isinstance(
            selection,
            tff_framework.Selection) and selection.index == selected_index:
          selection = selection.source
        else:
          tuple_pattern_matched = False
          break
      if tuple_pattern_matched:
        if isinstance(selection, tff_framework.Reference
                     ) and selection.name == top_level_parameter_name:
          return selections_from_zip[idx], True
    return inner_comp, False

  variables_rebound_in_result, _ = tff_framework.transform_postorder(
      comp.result, _replace_selections_with_new_bindings)
  lambda_with_zipped_param = tff_framework.Lambda(ref_to_zip.name,
                                                  ref_to_zip.type_signature,
                                                  variables_rebound_in_result)
  _check_for_missed_binding(comp, lambda_with_zipped_param)

  zipped_lambda_called = tff_framework.Call(lambda_with_zipped_param, zip_arg)
  constructed_lambda = tff_framework.Lambda(comp.parameter_name,
                                            comp.parameter_type,
                                            zipped_lambda_called)
  names_uniquified, _ = tff_framework.uniquify_reference_names(
      constructed_lambda)
  return names_uniquified
Ejemplo n.º 8
0
def _split_by_intrinsic(comp, uri):
  """Splits `comp` into `before` and `after` the intrinsic for the given `uri`.

  This function finds the intrinsic for the given `uri` in `comp`; splits `comp`
  into two computations `before` and `after` the intrinsic; and returns a Python
  tuple representing the pair of `before` and `after` computations.

  NOTE: This function is generally safe to call on computations that do not fit
  into canonical form. It is left to the caller to determine if the resulting
  computations are expected.

  Args:
    comp: The `tff_framework.Lambda` to transform.
    uri: A URI of an intrinsic.

  Returns:
    A pair of `tff_framework.ComputationBuildingBlock`s representing the
    computations `before` and `after` the intrinsic.

  Raises:
    ValueError: If `comp` is not a `tff_framework.Lambda` referencing a
      `tff_framework.Block` referencing a collections of variables containing an
      intrinsic with the given `uri` or if there is more than one intrinsic with
      the given `uri` in `comp`.
  """
  py_typecheck.check_type(comp, tff_framework.Lambda)
  py_typecheck.check_type(uri, six.string_types)
  py_typecheck.check_type(comp.result, tff_framework.Block)

  def _get_called_intrinsic_from_block_variables(variables, uri):
    for index, (name, variable) in enumerate(variables):
      if tff_framework.is_called_intrinsic(variable, uri):
        return index, name, variable
    raise ValueError(
        'Expected a lambda referencing a block referencing a collection of '
        'variables containing an intrinsic with the uri: {}, found None.'
        .format(uri))

  index, name, variable = _get_called_intrinsic_from_block_variables(
      comp.result.locals, uri)
  intrinsics = _get_called_intrinsics(comp, uri)
  length = len(intrinsics)
  if length != 1:
    raise ValueError(
        'Expected a computation with exactly one intrinsic with the uri: {}, '
        'found: {}.'.format(uri, length))
  name_generator = tff_framework.unique_name_generator(comp)
  before = tff_framework.Lambda(comp.parameter_name, comp.parameter_type,
                                variable.argument)
  parameter_type = tff.NamedTupleType(
      (comp.parameter_type, variable.type_signature))
  ref_name = six.next(name_generator)
  ref = tff_framework.Reference(ref_name, parameter_type)
  sel_0 = tff_framework.Selection(ref, index=0)
  sel_1 = tff_framework.Selection(ref, index=1)
  variables = comp.result.locals
  variables[index] = (name, sel_1)
  variables.insert(0, (comp.parameter_name, sel_0))
  block = tff_framework.Block(variables, comp.result.result)
  after = tff_framework.Lambda(ref.name, ref.type_signature, block)
  return before, after