# Lint as: python3
# Copyright 2018, The TensorFlow Federated Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Example showing how to run a multi-machine simulation.

In order to run this example, you must have a running instance of the
Executor Service, either locally or on Kubernetes.

The model trains EMNIST for a small number of rounds, but uses a RemoteExecutor
to distribute the work to the ExecutorService.
"""

import collections
import warnings

from absl import app
from absl import flags
import grpc
import numpy as np
import tensorflow as tf
import tensorflow_federated as tff

tf.compat.v1.enable_v2_behavior()

FLAGS = flags.FLAGS

flags.DEFINE_string('host', None, 'The host to connect to.')
flags.mark_flag_as_required('host')
flags.DEFINE_string('port', '8000', 'The port to connect to.')
flags.DEFINE_integer('n_clients', 10, 'Number of clients.')


def preprocess(dataset):

  def element_fn(element):
    return collections.OrderedDict([
        ('x', tf.reshape(element['pixels'], [-1])),
        ('y', tf.reshape(element['label'], [1])),
    ])

  return dataset.repeat(NUM_EPOCHS).map(element_fn).batch(BATCH_SIZE)


def make_federated_data(client_data, client_ids):
  return [
      preprocess(client_data.create_tf_dataset_for_client(x))
      for x in client_ids
  ]


def create_compiled_keras_model():
  """Create compiled Keras model."""
  model = tf.keras.models.Sequential([
      tf.keras.layers.Dense(
          10,
          activation=tf.nn.softmax,
          kernel_initializer='zeros',
          input_shape=(784,))
  ])

  model.compile(
      loss=tf.keras.losses.SparseCategoricalCrossentropy(),
      optimizer=tf.keras.optimizers.SGD(learning_rate=0.02),
      metrics=[tf.keras.metrics.SparseCategoricalAccuracy()])
  return model


NUM_EPOCHS = 10
BATCH_SIZE = 20
N_ROUNDS = 3


def make_remote_executor(inferred_cardinalities):
  """Make remote executor."""

  def create_worker_stack_on(ex):
    return tff.framework.LambdaExecutor(tff.framework.ConcurrentExecutor(ex))

  client_ex = []
  num_clients = inferred_cardinalities.get(tff.CLIENTS, None)
  if num_clients:
    print('Inferred that there are {} clients'.format(num_clients))
  else:
    print('No CLIENTS placement provided')

  for _ in range(num_clients or 0):
    channel = grpc.insecure_channel('{}:{}'.format(FLAGS.host, FLAGS.port))
    client_ex.append(
        create_worker_stack_on(
            tff.framework.RemoteExecutor(channel, rpc_mode='STREAMING')))

  federated_ex = tff.framework.FederatedExecutor({
      None: create_worker_stack_on(tff.framework.EagerExecutor()),
      tff.SERVER: create_worker_stack_on(tff.framework.EagerExecutor()),
      tff.CLIENTS: client_ex,
  })

  return tff.framework.LambdaExecutor(federated_ex)


def main(argv):
  if len(argv) > 1:
    raise app.UsageError('Too many command-line arguments.')

  warnings.simplefilter('ignore')

  np.random.seed(0)

  emnist_train, _ = tff.simulation.datasets.emnist.load_data()

  sample_clients = emnist_train.client_ids[0:FLAGS.n_clients]

  federated_train_data = make_federated_data(emnist_train, sample_clients)

  example_dataset = emnist_train.create_tf_dataset_for_client(
      emnist_train.client_ids[0])

  preprocessed_example_dataset = preprocess(example_dataset)

  sample_batch = tf.nest.map_structure(
      lambda x: x.numpy(),
      iter(preprocessed_example_dataset).next())

  def model_fn():
    keras_model = create_compiled_keras_model()
    return tff.learning.from_compiled_keras_model(keras_model, sample_batch)

  iterative_process = tff.learning.build_federated_averaging_process(model_fn)

  # Set the default executor to be a RemoteExecutor
  tff.framework.set_default_executor(make_remote_executor)

  state = iterative_process.initialize()

  state, metrics = iterative_process.next(state, federated_train_data)
  print('round  1, metrics={}'.format(metrics))

  for round_num in range(2, N_ROUNDS + 1):
    state, metrics = iterative_process.next(state, federated_train_data)
    print('round {:2d}, metrics={}'.format(round_num, metrics))


if __name__ == '__main__':
  app.run(main)

tensorflow.compat.v1.enable_v2_behavior numpy.random.seed tensorflow.keras.losses.SparseCategoricalCrossentropy tensorflow.keras.layers.Dense tensorflow.reshape tensorflow.keras.metrics.SparseCategoricalAccuracy tensorflow.keras.optimizers.SGD
[(34, 'tensorflow.compat.v1.enable_v2_behavior', 'tf.compat.v1.enable_v2_behavior', ([], {}), True, 'import tensorflow as tf\n'), (38, 'absl.flags.DEFINE_string', 'flags.DEFINE_string', (['"""host"""', 'None', '"""The host to connect to."""'], {}), False, 'from absl import flags\n'), (39, 'absl.flags.mark_flag_as_required', 'flags.mark_flag_as_required', (['"""host"""'], {}), False, 'from absl import flags\n'), (40, 'absl.flags.DEFINE_string', 'flags.DEFINE_string', (['"""port"""', '"""8000"""', '"""The port to connect to."""'], {}), False, 'from absl import flags\n'), (41, 'absl.flags.DEFINE_integer', 'flags.DEFINE_integer', (['"""n_clients"""', '(10)', '"""Number of clients."""'], {}), False, 'from absl import flags\n'), (109, 'tensorflow_federated.framework.LambdaExecutor', 'tff.framework.LambdaExecutor', (['federated_ex'], {}), True, 'import tensorflow_federated as tff\n'), (116, 'warnings.simplefilter', 'warnings.simplefilter', (['"""ignore"""'], {}), False, 'import warnings\n'), (118, 'numpy.random.seed', 'np.random.seed', (['(0)'], {}), True, 'import numpy as np\n'), (120, 'tensorflow_federated.simulation.datasets.emnist.load_data', 'tff.simulation.datasets.emnist.load_data', ([], {}), True, 'import tensorflow_federated as tff\n'), (139, 'tensorflow_federated.learning.build_federated_averaging_process', 'tff.learning.build_federated_averaging_process', (['model_fn'], {}), True, 'import tensorflow_federated as tff\n'), (142, 'tensorflow_federated.framework.set_default_executor', 'tff.framework.set_default_executor', (['make_remote_executor'], {}), True, 'import tensorflow_federated as tff\n'), (155, 'absl.app.run', 'app.run', (['main'], {}), False, 'from absl import app\n'), (114, 'absl.app.UsageError', 'app.UsageError', (['"""Too many command-line arguments."""'], {}), False, 'from absl import app\n'), (137, 'tensorflow_federated.learning.from_compiled_keras_model', 'tff.learning.from_compiled_keras_model', (['keras_model', 'sample_batch'], {}), True, 'import tensorflow_federated as tff\n'), (65, 'tensorflow.keras.layers.Dense', 'tf.keras.layers.Dense', (['(10)'], {'activation': 'tf.nn.softmax', 'kernel_initializer': '"""zeros"""', 'input_shape': '(784,)'}), True, 'import tensorflow as tf\n'), (73, 'tensorflow.keras.losses.SparseCategoricalCrossentropy', 'tf.keras.losses.SparseCategoricalCrossentropy', ([], {}), True, 'import tensorflow as tf\n'), (74, 'tensorflow.keras.optimizers.SGD', 'tf.keras.optimizers.SGD', ([], {'learning_rate': '(0.02)'}), True, 'import tensorflow as tf\n'), (88, 'tensorflow_federated.framework.ConcurrentExecutor', 'tff.framework.ConcurrentExecutor', (['ex'], {}), True, 'import tensorflow_federated as tff\n'), (75, 'tensorflow.keras.metrics.SparseCategoricalAccuracy', 'tf.keras.metrics.SparseCategoricalAccuracy', ([], {}), True, 'import tensorflow as tf\n'), (101, 'tensorflow_federated.framework.RemoteExecutor', 'tff.framework.RemoteExecutor', (['channel'], {'rpc_mode': '"""STREAMING"""'}), True, 'import tensorflow_federated as tff\n'), (104, 'tensorflow_federated.framework.EagerExecutor', 'tff.framework.EagerExecutor', ([], {}), True, 'import tensorflow_federated as tff\n'), (105, 'tensorflow_federated.framework.EagerExecutor', 'tff.framework.EagerExecutor', ([], {}), True, 'import tensorflow_federated as tff\n'), (48, 'tensorflow.reshape', 'tf.reshape', (["element['pixels']", '[-1]'], {}), True, 'import tensorflow as tf\n'), (49, 'tensorflow.reshape', 'tf.reshape', (["element['label']", '[1]'], {}), True, 'import tensorflow as tf\n')]