コード例 #1
0
def create_local_executor(num_clients):
  """Constructs an executor to execute computations on the local machine.

  The initial temporary implementation requires that the number of clients be
  specified in advance. This limitation will be removed in the near future.

  NOTE: This function is only available in Python 3.

  Args:
    num_clients: The number of clients.

  Returns:
    An instance of `tff.framework.Executor` for single-machine use only.
  """
  # TODO(b/134543154): We should not have to specif the number of clients; this
  # needs to go away once we flesh out all the remaining bits ad pieces.

  py_typecheck.check_type(num_clients, int)
  bottom_ex = lambda_executor.LambdaExecutor(eager_executor.EagerExecutor())

  def _make(n):
    return [concurrent_executor.ConcurrentExecutor(bottom_ex) for _ in range(n)]

  return lambda_executor.LambdaExecutor(
      federated_executor.FederatedExecutor({
          None: _make(1),
          placement_literals.SERVER: _make(1),
          placement_literals.CLIENTS: _make(num_clients)
      }))
コード例 #2
0
def test_context(rpc_mode='REQUEST_REPLY'):
  port = portpicker.pick_unused_port()
  server_pool = logging_pool.pool(max_workers=1)
  server = grpc.server(server_pool)
  server.add_insecure_port('[::]:{}'.format(port))
  target_executor = eager_executor.EagerExecutor()
  tracer = executor_test_utils.TracingExecutor(target_executor)
  service = executor_service.ExecutorService(tracer)
  executor_pb2_grpc.add_ExecutorServicer_to_server(service, server)
  server.start()
  channel = grpc.insecure_channel('localhost:{}'.format(port))
  remote_exec = remote_executor.RemoteExecutor(channel, rpc_mode)
  executor = lambda_executor.LambdaExecutor(remote_exec)
  set_default_executor.set_default_executor(executor)
  try:
    yield collections.namedtuple('_', 'executor tracer')(executor, tracer)
  finally:
    remote_exec.__del__()
    set_default_executor.set_default_executor()
    try:
      channel.close()
    except AttributeError:
      pass  # Public gRPC channel doesn't support close()
    finally:
      server.stop(None)
コード例 #3
0
    def test_with_federated_map_and_broadcast(self):
        eager_ex = eager_executor.EagerExecutor()
        federated_ex = federated_executor.FederatedExecutor({
            None:
            eager_ex,
            placement_literals.SERVER:
            eager_ex,
            placement_literals.CLIENTS: [eager_ex for _ in range(3)]
        })
        ex = lambda_executor.LambdaExecutor(federated_ex)
        loop = asyncio.get_event_loop()

        @computations.tf_computation(tf.int32)
        def add_one(x):
            return x + 1

        @computations.federated_computation(
            type_constructors.at_server(tf.int32))
        def comp(x):
            return intrinsics.federated_map(add_one,
                                            intrinsics.federated_broadcast(x))

        v1 = loop.run_until_complete(ex.create_value(comp))
        v2 = loop.run_until_complete(
            ex.create_value(10, type_constructors.at_server(tf.int32)))
        v3 = loop.run_until_complete(ex.create_call(v1, v2))
        result = loop.run_until_complete(v3.compute())
        self.assertCountEqual([x.numpy() for x in result], [11, 11, 11])
コード例 #4
0
    def test_with_block(self):
        ex = lambda_executor.LambdaExecutor(eager_executor.EagerExecutor())
        loop = asyncio.get_event_loop()

        f_type = computation_types.FunctionType(tf.int32, tf.int32)
        a = computation_building_blocks.Reference(
            'a',
            computation_types.NamedTupleType([('f', f_type), ('x', tf.int32)]))
        ret = computation_building_blocks.Block(
            [('f', computation_building_blocks.Selection(a, name='f')),
             ('x', computation_building_blocks.Selection(a, name='x'))],
            computation_building_blocks.Call(
                computation_building_blocks.Reference('f', f_type),
                computation_building_blocks.Call(
                    computation_building_blocks.Reference('f', f_type),
                    computation_building_blocks.Reference('x', tf.int32))))
        comp = computation_building_blocks.Lambda(a.name, a.type_signature,
                                                  ret)

        @computations.tf_computation(tf.int32)
        def add_one(x):
            return x + 1

        v1 = loop.run_until_complete(
            ex.create_value(comp.proto, comp.type_signature))
        v2 = loop.run_until_complete(ex.create_value(add_one))
        v3 = loop.run_until_complete(ex.create_value(10, tf.int32))
        v4 = loop.run_until_complete(
            ex.create_tuple(
                anonymous_tuple.AnonymousTuple([('f', v2), ('x', v3)])))
        v5 = loop.run_until_complete(ex.create_call(v1, v4))
        result = loop.run_until_complete(v5.compute())
        self.assertEqual(result.numpy(), 12)
コード例 #5
0
    def test_with_federated_apply(self):
        eager_ex = eager_executor.EagerExecutor()
        federated_ex = federated_executor.FederatedExecutor({
            None:
            eager_ex,
            placement_literals.SERVER:
            eager_ex
        })
        ex = lambda_executor.LambdaExecutor(federated_ex)
        loop = asyncio.get_event_loop()

        @computations.tf_computation(tf.int32)
        def add_one(x):
            return x + 1

        @computations.federated_computation(
            type_constructors.at_server(tf.int32))
        def comp(x):
            return intrinsics.federated_apply(add_one, x)

        v1 = loop.run_until_complete(ex.create_value(comp))
        v2 = loop.run_until_complete(
            ex.create_value(10, type_constructors.at_server(tf.int32)))
        v3 = loop.run_until_complete(ex.create_call(v1, v2))
        result = loop.run_until_complete(v3.compute())
        self.assertEqual(result.numpy(), 11)
コード例 #6
0
def _make_executor_and_tracer_for_test(support_lambdas=False):
    tracer = executor_test_utils.TracingExecutor(
        eager_executor.EagerExecutor())
    ex = caching_executor.CachingExecutor(tracer)
    if support_lambdas:
        ex = lambda_executor.LambdaExecutor(
            caching_executor.CachingExecutor(ex))
    return ex, tracer
コード例 #7
0
def _make_test_executor(num_clients=1, use_lambda_executor=False):
  bottom_ex = eager_executor.EagerExecutor()
  if use_lambda_executor:
    bottom_ex = lambda_executor.LambdaExecutor(bottom_ex)
  return federated_executor.FederatedExecutor({
      placements.SERVER: bottom_ex,
      placements.CLIENTS: [bottom_ex for _ in range(num_clients)],
      None: bottom_ex
  })
コード例 #8
0
    def test_clear_failure_with_mismatched_types_in_create_call(self):
        ex = lambda_executor.LambdaExecutor(eager_executor.EagerExecutor())
        loop = asyncio.get_event_loop()

        @computations.federated_computation(tf.float32)
        def comp(x):
            return x

        v1 = loop.run_until_complete(ex.create_value(comp))
        v2 = loop.run_until_complete(ex.create_value(10, tf.int32))
        with self.assertRaisesRegex(TypeError, 'incompatible'):
            loop.run_until_complete(ex.create_call(v1, v2))
コード例 #9
0
    def test_with_no_arg_tf_comp_in_no_arg_fed_comp(self):
        ex = lambda_executor.LambdaExecutor(eager_executor.EagerExecutor())
        loop = asyncio.get_event_loop()

        @computations.federated_computation
        def comp():
            return 10

        v1 = loop.run_until_complete(ex.create_value(comp))
        v2 = loop.run_until_complete(ex.create_call(v1))
        result = loop.run_until_complete(v2.compute())
        self.assertEqual(result.numpy(), 10)
コード例 #10
0
    def test_create_selection_with_tuples(self):
        ex = lambda_executor.LambdaExecutor(eager_executor.EagerExecutor())
        loop = asyncio.get_event_loop()

        v1 = loop.run_until_complete(ex.create_value(10, tf.int32))
        v2 = loop.run_until_complete(ex.create_value(20, tf.int32))
        v3 = loop.run_until_complete(
            ex.create_tuple(
                anonymous_tuple.AnonymousTuple([(None, v1), (None, v2)])))
        v4 = loop.run_until_complete(ex.create_selection(v3, index=0))
        v5 = loop.run_until_complete(ex.create_selection(v3, index=1))
        result0 = loop.run_until_complete(v4.compute())
        result1 = loop.run_until_complete(v5.compute())
        self.assertEqual(result0.numpy(), 10)
        self.assertEqual(result1.numpy(), 20)
コード例 #11
0
    def test_with_tuples(self):
        ex = lambda_executor.LambdaExecutor(eager_executor.EagerExecutor())
        loop = asyncio.get_event_loop()

        @computations.tf_computation(tf.int32, tf.int32)
        def add_numbers(x, y):
            return x + y

        @computations.federated_computation
        def comp():
            return add_numbers(10, 20)

        v1 = loop.run_until_complete(ex.create_value(comp))
        v2 = loop.run_until_complete(ex.create_call(v1))
        result = loop.run_until_complete(v2.compute())
        self.assertEqual(result.numpy(), 30)
コード例 #12
0
def create_local_executor(num_clients=None):
  """Constructs an executor to execute computations on the local machine.

  The initial temporary implementation requires that the number of clients be
  specified in advance. This limitation will be removed in the near future.

  NOTE: This function is only available in Python 3.

  Args:
    num_clients: The number of clients. If not specified (`None`), then this
      executor is not federated (can only execute unplaced computations).

  Returns:
    An instance of `tff.framework.Executor` for single-machine use only.

  Raises:
    ValueError: If the number of clients is not one or larger.
  """

  def _create_single_worker_stack():
    ex = eager_executor.EagerExecutor()
    ex = concurrent_executor.ConcurrentExecutor(ex)
    ex = caching_executor.CachingExecutor(ex)
    return lambda_executor.LambdaExecutor(ex)

  if num_clients is None:
    return _create_single_worker_stack()
  else:
    # TODO(b/134543154): We shouldn't have to specif the number of clients; this
    # needs to go away once we flesh out all the remaining bits ad pieces.
    py_typecheck.check_type(num_clients, int)
    if num_clients < 1:
      raise ValueError('If the number of clients is present, it must be >= 1.')

    def _create_multiple_worker_stacks(num_workers):
      return [_create_single_worker_stack() for _ in range(num_workers)]

    return lambda_executor.LambdaExecutor(
        caching_executor.CachingExecutor(
            federated_executor.FederatedExecutor({
                None:
                    _create_multiple_worker_stacks(1),
                placement_literals.SERVER:
                    _create_multiple_worker_stacks(1),
                placement_literals.CLIENTS:
                    (_create_multiple_worker_stacks(num_clients))
            })))
コード例 #13
0
    def test_with_one_arg_tf_comp_in_one_arg_fed_comp(self):
        ex = lambda_executor.LambdaExecutor(eager_executor.EagerExecutor())
        loop = asyncio.get_event_loop()

        @computations.tf_computation(tf.int32)
        def add_one(x):
            return x + 1

        @computations.federated_computation(tf.int32)
        def comp(x):
            return add_one(add_one(x))

        v1 = loop.run_until_complete(ex.create_value(comp))
        v2 = loop.run_until_complete(ex.create_value(10, tf.int32))
        v3 = loop.run_until_complete(ex.create_call(v1, v2))
        result = loop.run_until_complete(v3.compute())
        self.assertEqual(result.numpy(), 12)
コード例 #14
0
 def _create_variable_clients_executors(x):
     """Constructs executor stacks from `dict` argument."""
     py_typecheck.check_type(x, dict)
     for k, v in six.iteritems(x):
         py_typecheck.check_type(k, placement_literals.PlacementLiteral)
         if v <= 0:
             raise ValueError(
                 'Cardinality must be at '
                 'least one; you have passed {} for placement {}.'.format(
                     v, k))
     executor_dict = dict([(placement, _create_multiple_worker_stacks(n))
                           for placement, n in six.iteritems(x)])
     executor_dict.update({None: _create_multiple_worker_stacks(1)})
     executor_dict.update(
         {placement_literals.SERVER: _create_multiple_worker_stacks(1)})
     return lambda_executor.LambdaExecutor(
         caching_executor.CachingExecutor(
             federated_executor.FederatedExecutor(executor_dict)))
コード例 #15
0
    def test_with_functional_parameter(self):
        ex = lambda_executor.LambdaExecutor(eager_executor.EagerExecutor())
        loop = asyncio.get_event_loop()

        @computations.tf_computation(tf.int32)
        def add_one(x):
            return x + 1

        @computations.federated_computation(
            computation_types.FunctionType(tf.int32, tf.int32), tf.int32)
        def comp(f, x):
            return f(f(x))

        v1 = loop.run_until_complete(ex.create_value(comp))
        v2 = loop.run_until_complete(ex.create_value(add_one))
        v3 = loop.run_until_complete(ex.create_value(10, tf.int32))
        v4 = loop.run_until_complete(
            ex.create_tuple(
                anonymous_tuple.AnonymousTuple([(None, v2), (None, v3)])))
        v5 = loop.run_until_complete(ex.create_call(v1, v4))
        result = loop.run_until_complete(v5.compute())
        self.assertEqual(result.numpy(), 12)
コード例 #16
0
    def test_with_one_arg_tf_comp_in_two_arg_fed_comp(self):
        ex = lambda_executor.LambdaExecutor(eager_executor.EagerExecutor())
        loop = asyncio.get_event_loop()

        @computations.tf_computation(tf.int32, tf.int32)
        def add_numbers(x, y):
            return x + y

        @computations.federated_computation(tf.int32, tf.int32)
        def comp(x, y):
            return add_numbers(x, x), add_numbers(x, y), add_numbers(y, y)

        v1 = loop.run_until_complete(ex.create_value(comp))
        v2 = loop.run_until_complete(ex.create_value(10, tf.int32))
        v3 = loop.run_until_complete(ex.create_value(20, tf.int32))
        v4 = loop.run_until_complete(
            ex.create_tuple(
                anonymous_tuple.AnonymousTuple([(None, v2), (None, v3)])))
        v5 = loop.run_until_complete(ex.create_call(v1, v4))
        result = loop.run_until_complete(v5.compute())
        self.assertEqual(
            str(anonymous_tuple.map_structure(lambda x: x.numpy(), result)),
            '<20,30,40>')
コード例 #17
0
ファイル: executor_stacks.py プロジェクト: FreJoe/federated
def _complete_stack(ex):
  return lambda_executor.LambdaExecutor(
      caching_executor.CachingExecutor(
          concurrent_executor.ConcurrentExecutor(ex)))
コード例 #18
0
def _create_middle_stack(children):
    return lambda_executor.LambdaExecutor(
        caching_executor.CachingExecutor(
            composite_executor.CompositeExecutor(_create_bottom_stack(),
                                                 children)))
コード例 #19
0
def _create_bottom_stack():
    return lambda_executor.LambdaExecutor(
        caching_executor.CachingExecutor(
            concurrent_executor.ConcurrentExecutor(
                eager_executor.EagerExecutor())))
コード例 #20
0
 def _return_executor(x):
     del x  # Unused
     return lambda_executor.LambdaExecutor(
         caching_executor.CachingExecutor(
             federated_executor.FederatedExecutor(executor_dict)))
コード例 #21
0
 def test_with_mnist_training_example(self):
     executor_test_utils.test_mnist_training(
         self,
         lambda_executor.LambdaExecutor(eager_executor.EagerExecutor()))
コード例 #22
0
 def test_runs_tf(self):
     executor_test_utils.test_runs_tf(
         self,
         lambda_executor.LambdaExecutor(eager_executor.EagerExecutor()))
コード例 #23
0
def _create_single_worker_stack():
    ex = eager_executor.EagerExecutor()
    ex = concurrent_executor.ConcurrentExecutor(ex)
    ex = caching_executor.CachingExecutor(ex)
    return lambda_executor.LambdaExecutor(ex)