Load Models#

Open In Colab

Setup#

[ ]:
import importlib.util

DEV = True

if importlib.util.find_spec("google.colab") is not None:
    MODE = "colab-dev" if DEV else "colab"
else:
    MODE = "local"
[2]:
if MODE == "colab":
    %pip install -q lczerolens[hf]
elif MODE == "colab-dev":
    !rm -r lczerolens
    !git clone https://github.com/Xmaster6y/lczerolens -b main
    %pip install -q ./lczerolens --extra hf

Make sure to install the with the hf extra to load models from the Hugging Face Hub.

Load a Model#

Load a leela network from file (already converted to onnx or torch):

[3]:
from huggingface_hub import hf_hub_download
from lczerolens import LczeroModel, LczeroBoard

path = hf_hub_download("lczerolens/maia-1100", "model.onnx")

model = LczeroModel.from_path(path)
model(LczeroBoard())
[3]:
TensorDict(
    fields={
        board: Tensor(shape=torch.Size([1, 112, 8, 8]), device=cpu, dtype=torch.float32, is_shared=False),
        policy: Tensor(shape=torch.Size([1, 1858]), device=cpu, dtype=torch.float32, is_shared=False),
        wdl: Tensor(shape=torch.Size([1, 3]), device=cpu, dtype=torch.float32, is_shared=False)},
    batch_size=torch.Size([1]),
    device=None,
    is_shared=False)

To convert original weights see the section Convert Official Weights.

Directly from the Hub#

You can also load a model directly from the Hugging Face Hub:

[4]:
model = LczeroModel.from_hf("lczerolens/maia-1100")
model(LczeroBoard())
[4]:
TensorDict(
    fields={
        board: Tensor(shape=torch.Size([1, 112, 8, 8]), device=cpu, dtype=torch.float32, is_shared=False),
        policy: Tensor(shape=torch.Size([1, 1858]), device=cpu, dtype=torch.float32, is_shared=False),
        wdl: Tensor(shape=torch.Size([1, 3]), device=cpu, dtype=torch.float32, is_shared=False)},
    batch_size=torch.Size([1]),
    device=None,
    is_shared=False)