AlignmentResearch / polygames

MIT License
0 stars 1 forks source link

Polygames

This repository is a fork of https://github.com/facebookarchive/Polygames.

Installation

To get the code up and running, see the dockerfile and instructions in https://github.com/AlignmentResearch/polygames-scripts.

Checkpoints

The following URL contains a list of download links for checkpoints of different models on different games: http://dl.fbaipublicfiles.com/polygames/checkpoints/list.txt.

In order to run an evaluation of one of the checkpoints, run

python -m pypolygames eval --checkpoint <path_to_checkpoint>

In order to load from a checkpoint and keep training, run

python -m pypolygames train --init_checkpoint <path_to_checkpoint>

For more information, add the --help flag to your call to see how to use the different commands.

Content

The repo contains mostly the following folders:

How to use the application

The application is launched from the pypolygames python package, in either of the following modes:ar

When a training is launched, it creates a game_GAMENAME_model_MODELNAME_feat_FEATURIZATION_GMT_YYYYMMDDHHMMSS within the save_dir where it will log relevant files:

This directory will be the checkpoint_save_dir directory used by evaluation to retrieve the checkpoints to perform eval computation.

Parameters

The list of parameters for each mode is available with

python -m pypolygames {train,eval,traineval,human} --help

Threads

In train (resp. eval) mode, num_game * num_actor (resp. num_game * num_actor_eval * num_actor_opponent) is the total number of threads. The more num_actor (and num_actor_eval, num_actor_opponent), the larger the MCTS is for a given player.

In human mode, since num_game is set to one, for leveraging the computing power available on the platform, a rule-of-thumb is to set num_actor to 5 times the number of CPUs available (it is platform-dependent though, and performance tests should be done).

Model zoo

All models can be found in pypolygames/model_zoo. They come with a set of sensible parameters that can be customized as well as default games.

Usually models come in pair: MODELNAMEFCLogitModel and MODELNAMEConvLogitModel:

So far the models being implemented are the folling:

Depending on the actual model chosen, some parameters might not have any use.

Featurization

--out_features=True: the input to the NN includes a channel with 1 on the frontier.
--turn_features=True: the input to the NN includes a channel with the player index broadcasted.
--geometric_features=True: the input to the NN includes 4 geometric channels representing the position on the board.
--random_features=4: the input to the NN includes 4 random features.
--one_feature=True: the input to the NN includes a channel with 1 everywhere.
--history=3: the representation from the last 3 steps is added in the featurization.

Examples

Run the following command before running the code

export OMP_NUM_THREADS=1

Examples for the training mode

Note that any checkpoint can serve as a pretrained model

In this case cuda:0 will be used for training the model while cuda:1, cuda:2 and cuda:3 will be used for generating games. If there is only one device specified, it will be used for both purposes.

Notes:

Examples for the evaluation mode

Notes:

Examples for the training+evaluation mode

Examples for the human mode

Notes:

Examples for converting models

Saved checkpoints of models also store details about the game for which they were trained, and can only be used directly for the game in which they were trained. This is why eval runs do not require the --game_name to be specified; this is inferred from the model. The pypolygames convert command can be used to convert models to different games.

python -m pypolygames convert \
    --init_checkpoint "/checkpoints/checkpoint_600.pt.gz" \
    --game_name="LudiiGomoku.lud" \
    --out="/checkpoints/converted/XToGomoku.pt.gz"

This takes the previously-trained model stored in "/checkpoints/checkpoint_600.pt.gz", modifies it such that it can be used to play the Ludii implementation of Gomoku, and stores this modified version of the model in the new file "/checkpoints/converted/XToGomoku.pt.gz".

This works best when using neural network architectures that are compatible with arbitrary board shapes (such as ResConvConvLogitPoolModel), and source and target games that have identical numbers of channels for state and move tensors, as well as identical semantics for those channels. For instance, the Ludii implementation of Yavalath has the same number of channels with identical semantics (in the same order) as Gomoku. Therefore, if the source model in "/checkpoints/checkpoint_600.pt.gz" was trained using --model_name=ResConvConvLogitPoolModel and --game_name="LudiiYavalath.lud", this conversion can be performed directly without having to delete any parameters or add any new parameters.

python -m pypolygames convert \
    --init_checkpoint "/checkpoints/checkpoint_600.pt.gz" \
    --game_options="Board Size/19x19" \
    --out="/checkpoints/converted/Gomoku/15x15_to_19x19.pt.gz"

This example will convert the source checkpoint "/checkpoints/checkpoint_600.pt.gz" into a model that can be used in a game loaded with the additional --game_options="Board Size/19x19" argument. For example, --game_name=LudiiGomoku.lud is by default played on a 15x15 board, but can be played on a larger 19x19 board with the --game_options="Board Size/19x19" argument.

Note that the convert command only takes game options into account if some form of --game_options is explicitly provided among the command line arguments. This means that, if a model was first trained for --game_options=Board Size/19x19, and the goal is to convert it into one for the default board size of 15x15, it is still necessary to provide either --game_options (without any values after it) or --game_options=Board Size/15x15 to the convert script. This tells it that the goal is indeed to revert to default options, rather than just leaving whichever options were baked into the source model.

Examples for generating figures of models

If the optional graphviz and torchviz dependencies are installed, we can use torchviz to automatically generate figures of our models. This can be done using draw_model script:

python -m pypolygames draw_model \
    --game_name="Hex5pie" \
    --model_name="ResConvConvLogitPoolModelV2" \
    --out="/private/home/$USER/ImageName"

This command will generate an image of the ResConvConvLogitPoolModelV2 architecture when playing Hex5pie, and save it to /private/home/$USER/ImageName.png (note that the .png extension will be automatically appended).

Any arguments that can be used to modify the game, or any aspect of the Neural Network architecture, can be used in this command.

Running games through Ludii

See detailed documentation on the Ludii integration here.

Contributing

We welcome contributions! Please check basic instructions here

Initial contributors

Contributors to the early version of Polygames (before open source release) include:

Tristan Cazenave, Univ. Dauphine; Yen-Chi Chen, National Taiwan Normal University; Guan-Wei Chen, National Dong Hwa University; Shi-Yu Chen, National Dong Hwa University; Xian-Dong Chiu, National Dong Hwa University; Julien Dehos, Univ. Littoral Cote d’Opale; Maria Elsa, National Dong Hwa University; Qucheng Gong, Facebook AI Research; Hengyuan Hu, Facebook AI Research; Vasil Khalidov, Facebook AI Research; Chen-Ling Li, National Dong Hwa University; Hsin-I Lin, National Dong Hwa University; Yu-Jin Lin, National Dong Hwa University; Xavier Martinet, Facebook AI Research; Vegard Mella, Facebook AI Research; Jeremy Rapin, Facebook AI Research; Baptiste Roziere, Facebook AI Research; Gabriel Synnaeve, Facebook AI Research; Fabien Teytaud, Univ. Littoral Cote d’Opale; Olivier Teytaud, Facebook AI Research; Shi-Cheng Ye, National Dong Hwa University; Yi-Jun Ye, National Dong Hwa University; Shi-Jim Yen, National Dong Hwa University; Sergey Zagoruyko, Facebook AI Research

License

polygames is released under the MIT license. See LICENSE for additional details about it. Third-party libraries are also included under their own license.