KataGo icon indicating copy to clipboard operation
KataGo copied to clipboard

model_pytorch.py is too messy and not easy to read.

Open Nightbringers opened this issue 2 years ago • 5 comments

I only want see the best model b18c384nbt's architecture. But there are too many different config in model_pytorch.py. Various kinds of structures of different model. Make it very very hard to know b18c384nbt's architecture. Could you give me a model.py just for best model b18c384nbt. Thanks very much.

Nightbringers avatar Oct 03 '23 07:10 Nightbringers

It would be messy to duplicate a new model file for every different possible model and have many versions of the code floating around that could then get out of sync with each other, so no this is not happening.

Perhaps the problem is not model_pytorch.py, but rather that you might not know how to look at the model file? You can see the full config for the model if you download a model file and load it in pytorch and look at the config:

data = torch.load(model_path,map_location="cpu")
print(data["config"])

The config contains a list of all the blocks in the model, in order, as well as the channel parameters and other settings. Every listed block in the model, like "bottlenest2", corresponds exactly to 1 "structure" in model_pytorch.py, which you can find if you search for that string in the code. So that should solve the problem with there being "various kinds of structures" - the config will let you determine precisely which ones are used. You can also iterate through the list of all the tensors in data["model"] to print their key and their shape, and see exactly the parameters.

lightvector avatar Oct 03 '23 15:10 lightvector

I'm trying to use b18c384nbt in my own python go_game. But i don't know what's the input of model. I first think it's same as AZ. But then i found it's total different. katago has two inputs, input_spatial and input_global. Is there a guide or example or something about the inputs?

Nightbringers avatar Oct 04 '23 02:10 Nightbringers

If you are simply trying to run KataGo from python because you want its suggested moves or analysis or ownership predictions, I recommend you ignore all of these details about the models and inputs and architectures. Just use query C++ analysis engine through subprocess:

Here is example code for doing that: https://github.com/lightvector/KataGo/blob/master/python/query_analysis_engine_example.py

lightvector avatar Oct 04 '23 02:10 lightvector

I'm not just simply run KataGo from python. I want continue train model in python, use a different envs and MCTS. So i need to know the input of the model.

Nightbringers avatar Oct 04 '23 02:10 Nightbringers

Okay cool. In that case, take a look at https://github.com/lightvector/KataGo/blob/master/python/play.py - this is working code to load a neural net from python and query it based on a board position. It uses sgfmill for the board, and implements a basic GTP interface (https://www.lysator.liu.se/~gunnar/gtp/gtp2-spec-draft2/gtp2-spec.html) that will let you play on the command line with the bot which makes moves using the raw neural net (no MCTS).

The query to the neural net happens with the get_outputs function, where gs is a GameState (class defined right above that). The reason for having two different input tensors is that one holds spatial features (like where the stones are), and one holds global features that don't depend on the board position (like what the rules and komi are). These are computed by features.fill_row_features.

This is sort of old sandbox code that is mostly written a long time ago, so it's not very well commented or documented, so you'll have to do some work to trace the source code, but this should show you from start to end what the inference side of loading and running the net might look like purely from python.

Note that KataGo has no python MCTS implementation, so you're on your own there. This script only queries the raw net to make moves via the policy head directly. A lot of the graphical commands you see are designed for GoGUI, which is a particular gui you can try, which can view various outputs with graphical colors when this script is configured as the GTP engine for the gui.

lightvector avatar Oct 04 '23 03:10 lightvector