1. 程式人生 > >Play Go Against a Champ: Up and Running with Leela Zero

Play Go Against a Champ: Up and Running with Leela Zero

In 2016, the Go world was rocked by AlphaGo’s defeat of one of the world’s top players, Lee Sedol. For years, we had proudly boasted that no computer could beat a top pro. It didn’t stop at Lee either. It played online using the pseudonym “Master” and had a 60/0 win/loss record against pros. Top ranked player (at the time) Ke Jie lost a 3 game match against AlphaGo, prompting the Chinese Go Association to give it a professional 9-dan certificate. Finally, Moore’s law has caught up to us, and we should be celebrating. AlphaGo marks the dawn of a new era of Go. Just look at all the

books, movies, and videos that have come out about it. From here the level of play will only get higher, and it has already brought in a new wave of players to the global community.

How can I play against AlphaGo, you ask? Well, the short answer is you can’t, probably, but luckily for us, there is an open source project called Leela Zero that is designed in the same way, and there is a collective effort going on to rebuild the model weights to make it as strong as AlphaGo.

In this article, I will give the step by step instructions you need to get Leela Zero running on your machine and play a game. We will be using the CPU version of the build. It is slower than using Leela Zero with a dedicated GPU, but many people (or is it just me :D) don’t have a GPU. The instructions will be specific to Linux, and we will be running Leela Zero in a docker container and using Sabaki to play.

Requirements

While you can run Leela Zero on Mac, Windows and Linux, I will be giving instructions here for Linux. What you need before we start:

  • Linux distribution
  • Docker
  • Git

The Plan

In order to play against Leela Zero, we will build Leela Zero inside a docker image, in which we will download the current best weights (this is basically “knowledge” or “experience”) from the project website. Next we will install Sabaki, a Go client that works with the Go Text Protocol, which can be used to communicate with Leela Zero. Finally, we will run the docker container with Leela Zero and play against it with Sabaki.

Building the Leela Zero Image

The first thing we will need to do is clone the Leela Zero repository.

git clone https://github.com/gcp/leela-zerocd leela-zerogit submodule update --init --recursive

IMPORTANT: The repository contains a submodule, so you need to run the update, or cmake will most likely fail.

There are dockerfiles in the repository, so it is pretty simple. First we will build the base image, and then the CPU image.

# Copy the base Dockerfile to the project rootcp Dockerfiles/Dockerfile.base Dockerfile
# Build the base imagedocker build -t leela-zero:base .
# Copy the CPU Dockerfile to the project rootcp Dockerfiles/Dockerfile.cpu Dockerfile

Let’s update the Dockerfile to build Leela Zero and download the current weights. This way, if we want to download new weights, all we have to do is rebuild the image.

# Update the Dockerfile to look like this
FROM leela-zero:base
RUN apt update && apt install -y  curl
# CPU buildRUN CXX=g++ CC=gcc cmake -DUSE_CPU_ONLY=1 ..
RUN cmake --build . --target leelaz --config Release -- -j2
RUN make leelaz
RUN make tests && ./tests
# Do something to keep the container runningCMD sleep 8640

Then we can build it.

# Build the CPU imagedocker build -t leela-zero:cpu .

Get Sabaki

Sabaki is a Go editor (built by Star Wars fans) that can be used to play against programs that use the Go Text Protocol. It is super cool.

You can download the AppImage (32/64bit) from their release page. And run it. You will need to add the executable permission to the file. (NOTE: The version may be different)

chmod +x sabaki-v0.35.1-linux-x64-fixed.AppImage./sabaki-v0.35.1-linux-x64-fixed.AppImage

If you need help with AppImages, you can read the AppImage F.A.Q.

Configure Leela Zero on Sabaki

Once Sabaki is running, we can add Leela Zero as an engine. To do this go to Engines > Manage Engines in the menu. Click the Add button and fill in the form like this.

(Unnamed Engine): Docker LeelaPath: dockerNo arguments: exec -i leela /src/build/leelaz --gtp --weights /src/build/best-network
Configure Leela Zero as an Engine

This will basically call the docker exec command interactively and Sabaki will be able to read/write the Go Text Protocol to communicate with Leela Zero.

Playing the Champ

So lets put it all together.

First we need to run the container with Leela Zero. (NOTE: The name we use is the same one that we have in the engine configuration in Sabaki)

docker run --rm -d --name leela leela-zero:cpu

Now we can open Sabaki and play. All we need to do is select Docker Leela as one of the players and start the game.

Start a new game by going to File > New in the menu, selecting Docker Leela as one of the players, and then clicking the Ok button.

If you want to you can select both players to be Leela Zero, and it will play itself :D.

A New Era of Go

Leela Zero is a part of a new era of Go. One in which players have ready access to a strong opponent any time they feel like a game. Not only are machine learning driven AI’s like Leela Zero strong, they are already changing the way we play. Moves that before were considered reckless, have shown to be effective. I can’t wait to see how the world of Go evolves.

Have fun playing with Leela Zero, and check out the readme for more information (including weights for more “human like” play). If you have any problem with the instructions or find some error, please let me know.