Skip to main content

An amazing sample package for maia2.

Project description

Maia2: A Unified Model for Human-AI Alignment in Chess

[!IMPORTANT] Maia-3 is now available and is recommended for new projects. It is the latest generation of our human chess modeling work, built on the Chessformer architecture. See the code, pre-trained models, paper, and website.

The official implementation of the NeurIPS 2024 paper Maia-2 [paper]. This work was led by CSSLab at the University of Toronto.

Abstract

There are an increasing number of domains in which artificial intelligence (AI) systems both surpass human ability and accurately model human behavior. This introduces the possibility of algorithmically-informed teaching in these domains through more relatable AI partners and deeper insights into human decision-making. Critical to achieving this goal, however, is coherently modeling human behavior at various skill levels. Chess is an ideal model system for conducting research into this kind of human-AI alignment, with its rich history as a pivotal testbed for AI research, mature superhuman AI systems like AlphaZero, and precise measurements of skill via chess rating systems. Previous work in modeling human decision-making in chess uses completely independent models to capture human style at different skill levels, meaning they lack coherence in their ability to adapt to the full spectrum of human improvement and are ultimately limited in their effectiveness as AI partners and teaching tools. In this work, we propose a unified modeling approach for human-AI alignment in chess that coherently captures human style across different skill levels and directly captures how people improve. Recognizing the complex, non-linear nature of human learning, we introduce a skill-aware attention mechanism to dynamically integrate players’ strengths with encoded chess positions, enabling our model to be sensitive to evolving player skill. Our experimental results demonstrate that this unified framework significantly enhances the alignment between AI and human players across a diverse range of expertise levels, paving the way for deeper insights into human decision-making and AI-guided teaching tools.

Requirements

chess==1.10.0
einops==0.8.0
gdown==5.2.0
numpy==2.1.3
pandas==2.2.3
pyzstd==0.15.9
Requests==2.32.4
torch==2.8.0
tqdm==4.66.3

The version requirements may not be very strict, but the above configuration should work.

Installation

pip install maia2

Quick Start: Batch Inference

from maia2 import model, dataset, inference

You can load a model for "rapid" or "blitz" games on CUDA, Apple Silicon MPS, or CPU. The default "auto" setting selects CUDA first, then MPS, and finally CPU.

maia2_model = model.from_pretrained(type="rapid", device="auto")

Set device explicitly to "cuda", "mps", or "cpu" when needed. The older "gpu" value remains supported as an alias for "cuda".

Load a pre-defined example test dataset for demonstration.

data = dataset.load_example_test_dataset()

Batch Inference

  • batch_size=1024: Set the batch size for inference.
  • num_workers=4: Use multiple worker threads for data loading and processing.
  • verbose=1: Show the progress bar during the inference process.
data, acc = inference.inference_batch(data, maia2_model, verbose=1, batch_size=1024, num_workers=4)
print(acc)

data will be updated in-place to include inference results.

Position-wise Inference

We use the same example test dataset for demonstration.

prepared = inference.prepare()

Once the prepapration is done, you can easily run inference position by position:

for fen, move, elo_self, elo_oppo, _, _ in data.values[:10]:
    move_probs, win_prob = inference.inference_each(maia2_model, prepared, fen, elo_self, elo_oppo)
    print(f"Move: {move}, Predicted: {move_probs}, Win Prob: {win_prob}")
    print(f"Correct: {max(move_probs, key=move_probs.get) == move}")

Try to tweak the skill level (ELO) of the activce player elo_self and opponent play elo_oppo! You may find it insightful for some positions.

Training

Download data from Lichess Database

Please download the game data of the time period you would like to train on in .pgn.zst format. Data decompressing is handled by maia2, so you don't need to decompress these files before training.

Training with our default settings

Please modify data_root in the config file to indicate where you stored the downloaded lichess data. It will take around 1 week to finish training 1 epoch with 2*A100 and 16*CPUs.

from maia2 import train, utils
cfg = utils.parse_args(cfg_file_path="./maia2_models/config.yaml")
train.run(cfg, device="auto")

The training device can be set to "cuda", "mps" (Apple Silicon), or "cpu". The default "auto" setting selects CUDA first, then MPS, and finally CPU. CPU training is supported but is likely to be much slower; reduce the configured batch size and number of workers when training on a laptop.

train.run creates a new model unless checkpoint restoration is enabled in the configuration. A model returned by model.from_pretrained is intended for inference and is not used automatically by train.run.

If you would like to restore training from a checkpoint, please modify the from_checkpoint, checkpoint_year, and checkpoint_month to indicate the initialization you need.

Interpretability and Concept Probing

For follow-up work on interpreting Maia-2's skill-aware representations, see maia2-skill-adaptation. It includes code for extracting intermediate activations and training Elo-conditioned linear probes over 172 formally defined chess concepts, including bishop-pair and queen-capture concepts. This is an extension of the concept analysis in the Maia-2 paper rather than an exact reproduction of every measurement in the paper's chess-concept figure.

Citation

@inproceedings{
tang2024maia,
title={Maia-2: A Unified Model for Human-{AI} Alignment in Chess},
author={Zhenwei Tang and Difan Jiao and Reid McIlroy-Young and Jon Kleinberg and Siddhartha Sen and Ashton Anderson},
booktitle={The Thirty-eighth Annual Conference on Neural Information Processing Systems},
year={2024},
url={https://openreview.net/forum?id=XWlkhRn14K}
}
@inproceedings{monroe2026chessformer,
title={Chessformer: A Unified Architecture for Chess Modeling},
author={Daniel Monroe and George Eilender and Philip Chalmers and Zhenwei Tang and Ashton Anderson},
booktitle={The Fourteenth International Conference on Learning Representations},
year={2026},
url={https://openreview.net/forum?id=2ltBRzEHyd}
}

If you find these projects helpful, please consider citing both papers and starring both repos.

Contact

If you have any questions or suggestions, please feel free to contact us via email: josephtang@cs.toronto.edu.

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

maia2-0.10.0.tar.gz (20.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

maia2-0.10.0-py2.py3-none-any.whl (19.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file maia2-0.10.0.tar.gz.

File metadata

  • Download URL: maia2-0.10.0.tar.gz
  • Upload date:
  • Size: 20.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.13 {"installer":{"name":"uv","version":"0.11.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for maia2-0.10.0.tar.gz
Algorithm Hash digest
SHA256 8def7c52dc81853b52b34f6d48a6b114d1ec69da5985483bdb9641e4f44b61ec
MD5 10a7a0d84592004becd7d929dd87875b
BLAKE2b-256 b195795424eee31abac81db6906611ea427471d7bae67585c820e76277a10ec8

See more details on using hashes here.

File details

Details for the file maia2-0.10.0-py2.py3-none-any.whl.

File metadata

  • Download URL: maia2-0.10.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 19.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.13 {"installer":{"name":"uv","version":"0.11.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for maia2-0.10.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4ca51e693ea7922efed3aafb052f8f57f0b5398e2c248d226062d2aa67d3ab99
MD5 769c08ce42fc90f2c92f3e715bf23203
BLAKE2b-256 45183a9b3d6df169a35474b16460715d214ae60e59cd770614bdcb7d403d55a4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page