Esempio n. 1
0
 async def create_selection(self, source, index=None, name=None):
   py_typecheck.check_type(source, FederatingExecutorValue)
   py_typecheck.check_type(source.type_signature,
                           computation_types.NamedTupleType)
   if name is not None:
     name_to_index = dict((n, i) for i, (
         n,
         t) in enumerate(anonymous_tuple.to_elements(source.type_signature)))
     index = name_to_index[name]
   if isinstance(source.internal_representation,
                 anonymous_tuple.AnonymousTuple):
     val = source.internal_representation
     selected = val[index]
     return FederatingExecutorValue(selected, source.type_signature[index])
   elif isinstance(source.internal_representation,
                   executor_value_base.ExecutorValue):
     if type_utils.type_tree_contains_types(source.type_signature,
                                            computation_types.FederatedType):
       raise ValueError(
           'FederatingExecutorValue {} has violated its contract; '
           'it is embedded in another executor and yet its type '
           'has placement. The embedded value is {}, with type '
           'signature {}.'.format(source, source.internal_representation,
                                  source.type_signature))
     val = source.internal_representation
     child = self._target_executors[None][0]
     return FederatingExecutorValue(
         await child.create_selection(val, index=index),
         source.type_signature[index])
   else:
     raise ValueError('Unexpected internal representation while creating '
                      'selection. Expected one of `AnonymousTuple` or value '
                      'embedded in target executor, received {}'.format(
                          source.internal_representation))
Esempio n. 2
0
  def federated_value(self, value, placement):
    """Implements `federated_value` as defined in `api/intrinsics.py`."""
    value = value_impl.to_value(value, None, self._context_stack)
    if type_utils.type_tree_contains_types(value.type_signature,
                                           computation_types.FederatedType):
      raise TypeError('Cannt place value {} containing federated types at '
                      'another placement; requested to be placed at {}.'.format(
                          value, placement))

    value = value_impl.ValueImpl.get_comp(value)
    comp = building_block_factory.create_federated_value(value, placement)
    return value_impl.ValueImpl(comp, self._context_stack)