예제 #1
0
def main(unused_argv):
    base_config = config_util.get_config(urdf_root=FLAGS.urdf_data_path)
    config = config_util.generate_config(
        base_config, current_time_string=FLAGS.current_time_string)
    servers = []
    server_creds = loas2.loas2_server_credentials()
    port = FLAGS.port
    if not config.run_on_borg:
        port = 20000 + FLAGS.server_id
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=100),
                         ports=(port, ))

    blackbox_object = config.blackbox_object_fn()

    np.random.seed(FLAGS.server_id)

    if config.algorithm == "zero_order":
        if FLAGS.server_id < config.test_workers:
            worker_mode = "Test"
            task_ids = range(config.train_set_size,
                             config.train_set_size + config.test_set_size)

        else:
            worker_mode = "Train"
            task_ids = range(config.train_set_size)

        servicer = blackbox_maml_objects.GeneralMAMLBlackboxWorker(
            worker_id=FLAGS.server_id,
            blackbox_object=blackbox_object,
            task_ids=task_ids,
            task_batch_size=config.task_batch_size,
            worker_mode=worker_mode)
        zero_order_pb2_grpc.add_EvaluationServicer_to_server(servicer, server)

    elif config.algorithm == "first_order":
        tasks = [
            config.make_task_fn(s)
            for s in range(config.train_set_size + config.test_set_size)
        ]
        servicer = blackbox_maml_objects.GradientMAMLWorker(
            FLAGS.server_id, blackbox_object=blackbox_object, tasks=tasks)
        first_order_pb2_grpc.add_EvaluationServicer_to_server(servicer, server)

    server.add_secure_port("[::]:{}".format(port), server_creds)
    servers.append(server)
    server.start()
    print("Start server {}".format(FLAGS.server_id))

    # prevent the main thread from exiting
    try:
        while True:
            time.sleep(_ONE_DAY_IN_SECONDS)
    except KeyboardInterrupt:
        for server in servers:
            server.stop(0)
def main(unused_argv):
    base_config = config_util.get_config()
    config = config_util.generate_config(
        base_config, current_time_string=FLAGS.current_time_string)
    blackbox_object = config.blackbox_object_fn()
    init_current_input = blackbox_object.get_initial()
    init_best_input = []
    init_best_core_hyperparameters = []
    init_best_value = -float("inf")
    init_iteration = 0
    np.random.seed(0)
    # ------------------ OPTIMIZERS ----------------------------------------------
    num_servers = config.num_servers
    logging.info("Number of Servers: %d", num_servers)

    if not config.run_locally:
        servers = [
            "{}.{}".format(i, FLAGS.server_address) for i in range(num_servers)
        ]
    else:
        servers = [
            "127.0.0.1:{}".format(20000 + i) for i in range(num_servers)
        ]
    logging.info("Running servers:")
    logging.info(servers)
    stubs = []
    for server in servers:
        channel = grpc.insecure_channel(server)
        grpc.channel_ready_future(channel).result()
        if config.algorithm == "zero_order":
            stubs.append(zero_order_pb2_grpc.EvaluationStub(channel))
        elif config.algorithm == "first_order":
            stubs.append(first_order_pb2_grpc.EvaluationStub(channel))

    tf.gfile.MakeDirs(config.global_logfoldername)
    logging.info("LOGGING FOLDER: %s", config.global_logfoldername)
    tf.gfile.MakeDirs(config.test_mamlpt_parallel_vals_folder)
    if config.log_states:
        tf.gfile.MakeDirs(config.states_folder)
    if config.recording:
        tf.gfile.MakeDirs(config.video_folder)
    with tf.gfile.Open(config.hparams_file, "w") as hparams_file:
        json.dump(config.json_hparams, hparams_file)
    # Runs main client's procedure responsible for optimization.

    if config.algorithm == "zero_order":
        es_blackbox_optimizer = config.es_blackbox_optimizer_fn(
            blackbox_object.get_metaparams())
        zero_order_maml_learner_grpc.run_blackbox(
            config,
            es_blackbox_optimizer,
            init_current_input,
            init_best_input,
            init_best_core_hyperparameters,
            init_best_value,
            init_iteration,
            stubs=stubs,
            log_bool=True)

    elif config.algorithm == "first_order":
        train_tasks = {
            "object": blackbox_object,
            "tasks":
            [config.make_task_fn(t) for t in range(config.train_set_size)],
            "ids": range(config.train_set_size)
        }

        test_tasks = {
            "object":
            blackbox_object,
            "tasks": [
                config.make_task_fn(t)
                for t in range(config.train_set_size, config.train_set_size +
                               config.test_set_size)
            ],
            "ids":
            range(config.train_set_size,
                  config.train_set_size + config.test_set_size)
        }
        first_order_maml_learner_grpc.run_blackbox(config, train_tasks,
                                                   test_tasks,
                                                   init_current_input, stubs)