Beispiel #1
0
def task_wait_for_version(port, model_name, version_id, timeout):
    """ Waits until the TF serving is done loading a version in memory
        :param port: The port used by the TensorFlow Serving server.
        :param model_name: The model name that needs to be loaded
        :param version_id: The version that should be loaded
        :param timeout: The maximum number of seconds to wait. Logs an error if reached.
    """
    GRPCDataset.wait_for_model_to_load('localhost',
                                       port,
                                       model_name,
                                       version_id,
                                       timeout=timeout + 30)
Beispiel #2
0
def get_train_supervised_players(adapter_ctor, dataset_builder_ctor,
                                 tf_serving_port, cluster_config, hparams):
    """ Builds 1 RL player vs 6 supervised opponent
        :param adapter_ctor: The constructor to build the adapter to query orders, values and policy details
        :param dataset_builder_ctor: The constructor of `BaseBuilder` to set the required proto fields
        :param tf_serving_port: The port to connect to the TF serving server
        :param cluster_config: The cluster configuration to use for distributed training
        :param hparams: A dictionary of hyper-parameters.
        :return: A list of players
        :type adapter_ctor: diplomacy_research.models.policy.base_policy_adapter.BasePolicyAdapter.__class__
        :type dataset_builder_ctor: diplomacy_research.models.datasets.base_builder.BaseBuilder.__class__
        :type cluster_config: diplomacy_research.utils.cluster.ClusterConfig
    """
    player_dataset = GRPCDataset(hostname='localhost',
                                 port=tf_serving_port,
                                 model_name='player',
                                 signature=adapter_ctor.get_signature(),
                                 dataset_builder=dataset_builder_ctor(),
                                 cluster_config=cluster_config)
    opponent_dataset = GRPCDataset(hostname='localhost',
                                   port=tf_serving_port,
                                   model_name='opponent',
                                   signature=adapter_ctor.get_signature(),
                                   dataset_builder=dataset_builder_ctor(),
                                   cluster_config=cluster_config)

    # Adapters
    player_adapter = adapter_ctor(player_dataset)
    opponent_adapter = adapter_ctor(opponent_dataset)

    # Creating players
    players = []
    players += [
        ModelBasedPlayer(policy_adapter=player_adapter,
                         temperature=DEFAULT_TEMPERATURE,
                         noise=DEFAULT_NOISE,
                         dropout_rate=hparams['dropout_rate'],
                         use_beam=hparams['use_beam'])
    ]
    for _ in range(NB_POWERS - 1):
        players += [
            ModelBasedPlayer(policy_adapter=opponent_adapter,
                             temperature=DEFAULT_TEMPERATURE,
                             noise=DEFAULT_NOISE,
                             dropout_rate=hparams['dropout_rate'],
                             use_beam=hparams['use_beam'])
        ]
    return players
Beispiel #3
0
    def __init__(self, temperature=0.1, use_beam=False, port=9502, name=None):
        """ Constructor
            :param temperature: The temperature to apply to the logits.
            :param use_beam: Boolean that indicates that we want to use a beam search.
            :param port: The port to use for the tf serving to query the model.
            :param name: Optional. The name of this player.
        """
        model_url = 'https://f002.backblazeb2.com/file/ppaquette-public/benchmarks/neurips2019-rl_model.zip'

        # Creating serving if port is not open
        if not is_port_opened(port):
            launch_serving(model_url, port)

        # Creating adapter
        grpc_dataset = GRPCDataset(
            hostname='localhost',
            port=port,
            model_name='player',
            signature=rl_neurips2019.PolicyAdapter.get_signature(),
            dataset_builder=rl_neurips2019.BaseDatasetBuilder())
        policy_adapter = rl_neurips2019.PolicyAdapter(grpc_dataset)

        # Building benchmark model
        super(DipNetRLPlayer, self).__init__(policy_adapter=policy_adapter,
                                             temperature=temperature,
                                             use_beam=use_beam,
                                             name=name)
Beispiel #4
0
def create_model_based_player(model_name, adapter_ctor, dataset_builder_ctor):
    """ Function to connect to TF Serving server and query orders """
    serving_port = PORTS_POOL.pop(0)
    OPEN_PORTS.append(serving_port)

    # Start TF Serving
    launch_serving(model_name, serving_port)

    # Creating player
    grpc_dataset = GRPCDataset(hostname='localhost',
                               port=serving_port,
                               model_name='player',
                               signature=adapter_ctor.get_signature(),
                               dataset_builder=dataset_builder_ctor())
    adapter = adapter_ctor(grpc_dataset)
    player = ModelBasedPlayer(adapter,
                              noise=NOISE,
                              temperature=TEMPERATURE,
                              dropout_rate=DROPOUT_RATE,
                              use_beam=USE_BEAM)

    # Validating openings
    yield player.check_openings()

    # Returning player
    return player
Beispiel #5
0
def task_get_player_models(port, config, adapter_ctor, dataset_builder_ctor,
                           cluster_config):
    """ Gets the players to use for monitor the serving server
        :param port: Integer. The port to use for the TF serving server.
        :param config: The configuration to set on the serving on launch (None to set no config on launch)
        :param adapter_ctor: The constructor to build the adapter to query orders, values and policy details
        :param dataset_builder_ctor: The constructor of `BaseBuilder` to set the required proto fields
        :param cluster_config: The cluster configuration used for distributed training.
        :return: A list of tuples (model_name, player)

        :type adapter_ctor: diplomacy_research.models.policy.base_policy_adapter.BasePolicyAdapter.__class__
        :type dataset_builder_ctor: diplomacy_research.models.datasets.base_builder.BaseBuilder.__class__
        :type cluster_config: diplomacy_research.utils.cluster.ClusterConfig
    """
    player_models = []
    if config is None:
        return player_models
    for model_config in config:
        player_dataset = GRPCDataset(hostname='localhost',
                                     port=port,
                                     model_name=model_config.name,
                                     signature=adapter_ctor.get_signature(),
                                     dataset_builder=dataset_builder_ctor(),
                                     cluster_config=cluster_config)
        player_adapter = adapter_ctor(player_dataset)
        player = ModelBasedPlayer(player_adapter)
        player_models += [(model_config.name, player)]
    return player_models
Beispiel #6
0
def task_set_config(port, config, nb_retries=30):
    """ Sets a new configuration on the serving server
        :param port: Integer. The port to use for the TF serving server.
        :param config: A ModelConfig named tuple or a list of ModelConfig named tuple
        :param nb_retries: The number of times to retry setting the configuration.
    """
    if config is None:
        return
    for _ in range(nb_retries):
        if GRPCDataset.set_config('localhost', port, config):
            break
        yield gen.sleep(1.)
    else:
        LOGGER.error(
            'Unable to set the configuration on the TF serving server. Tried %d times. Skipping.',
            nb_retries)
Beispiel #7
0
    def make(self, clean_dir=False):
        """ Function to download the latest model and create a player """
        bot_directory = os.path.join(WORKING_DIR, 'data', 'bot_%s' % self.name)
        bot_model = os.path.join(bot_directory, '%s.zip' % self.name)
        if clean_dir:
            shutil.rmtree(bot_directory, ignore_errors=True)
        os.makedirs(bot_directory, exist_ok=True)

        model_url = self.url

        # Downloading model
        download_file(model_url, bot_model, force=clean_dir)

        # Unzipping file
        zip_ref = zipfile.ZipFile(bot_model, 'r')
        zip_ref.extractall(bot_directory)
        zip_ref.close()

        serving_port = PORTS_POOL.pop(0)
        OPEN_PORTS.append(serving_port)

        # Start TF Serving
        launch_serving(self.name, serving_port)

        # Creating player
        grpc_dataset = GRPCDataset(
            hostname='localhost',
            port=serving_port,
            model_name='player',
            signature=self.policy_adapter.get_signature(),
            dataset_builder=self.base_dataset_builder())
        adapter = self.policy_adapter(grpc_dataset)
        player = ModelBasedPlayer(adapter,
                                  noise=NOISE,
                                  temperature=self.temperature,
                                  dropout_rate=DROPOUT_RATE,
                                  use_beam=self.use_beam,
                                  name=self.name)

        # Validating openings
        yield player.check_openings()

        # Returning
        return player
Beispiel #8
0
def launch_serving(model_name, serving_port):
    """ Launches or relaunches the TF Serving process """
    # Stop all serving child processes
    if is_port_opened(serving_port):
        kill_processes_using_port(serving_port)

    # Launching a new process
    log_file_path = os.path.join(WORKING_DIR, 'data',
                                 'log_serving_%d.txt' % serving_port)
    serving_process = Process(target=start_tf_serving,
                              args=(serving_port, WORKING_DIR),
                              kwargs={
                                  'force_cpu': True,
                                  'log_file_path': log_file_path
                              })
    serving_process.start()
    kill_subprocesses_on_exit()

    # Waiting for port to be opened.
    for attempt_idx in range(30):
        time.sleep(10)
        if is_port_opened(serving_port):
            break
        LOGGER.info('Waiting for TF Serving to come online. - Attempt %d / %d',
                    attempt_idx + 1, 30)
    else:
        LOGGER.error('TF Serving is not online after 5 minutes. Aborting.')
        raise RuntimeError()

    # Setting configuration
    new_config = ModelConfig(name='player',
                             base_path='/work_dir/data/bot_%s' % model_name,
                             version_policy=None)
    for _ in range(30):
        if GRPCDataset.set_config('localhost', serving_port, new_config):
            LOGGER.info('Configuration set successfully.')
            break
        time.sleep(5.)
    else:
        LOGGER.error('Unable to set the configuration file.')
Beispiel #9
0
def launch_serving(model_url, serving_port, first_launch=True):
    """ Launches or relaunches the TF Serving process
        :param model_url: The URL to use to download the model
        :param serving_port: The port to use for TF serving
        :param first_launch: Boolean that indicates if this is the first launch or a relaunch
    """
    model_url = model_url or ''
    bot_filename = model_url.split('/')[-1]
    bot_name = bot_filename.split('.')[0]
    bot_directory = os.path.join(WORKING_DIR, 'data', 'bot_%s' % bot_name)
    bot_model = os.path.join(bot_directory, bot_filename)

    # If first launch, downloading the model
    if first_launch:
        shutil.rmtree(bot_directory, ignore_errors=True)
        os.makedirs(bot_directory, exist_ok=True)

        # Downloading model
        download_file(model_url, bot_model, force=True)

        # Unzipping file
        zip_ref = zipfile.ZipFile(bot_model, 'r')
        zip_ref.extractall(bot_directory)
        zip_ref.close()

    # Otherwise, restarting the serving
    elif is_port_opened(serving_port):
        kill_processes_using_port(serving_port)

    # Launching a new process
    log_file_path = os.path.join(WORKING_DIR, 'data',
                                 'log_serving_%d.txt' % serving_port)
    serving_process = Process(target=start_tf_serving,
                              args=(serving_port, WORKING_DIR),
                              kwargs={
                                  'force_cpu': True,
                                  'log_file_path': log_file_path
                              })
    serving_process.start()
    kill_subprocesses_on_exit()

    # Waiting for port to be opened.
    for attempt_ix in range(90):
        time.sleep(10)
        if is_port_opened(serving_port):
            break
        LOGGER.info('Waiting for TF Serving to come online. - Attempt %d / %d',
                    attempt_ix + 1, 90)
    else:
        LOGGER.error('TF Serving is not online after 15 minutes. Aborting.')
        raise RuntimeError()

    # Setting configuration
    new_config = ModelConfig(name='player',
                             base_path='/work_dir/data/bot_%s' % bot_name,
                             version_policy=None)
    for _ in range(30):
        if GRPCDataset.set_config('localhost', serving_port, new_config):
            LOGGER.info('Configuration set successfully.')
            break
        time.sleep(5.)
    else:
        LOGGER.error('Unable to set the configuration file.')