Skip to main content

McTorch — Monte Carlo Tree Search framework with PyTorch and C++ backends

Project description

McTorch

A Monte Carlo Tree Search framework built on Python and PyTorch, with a C++ backend for performance-critical workloads.


Overview

McTorch provides a clean, environment-agnostic MCTS implementation. The design separates the search engine, environment interface, and execution backend so that each piece can be developed and tested independently.

Two backends are supported:

  • TorchBackend — pure Python reference implementation, correct by design.
  • CppBackend — C++ accelerated backend, built via Cython bindings.

Repository layout

implement/            source code — package, tests, examples, benchmarks
docs/                 all documentation
management/           project management artifacts
scripts/              build and test helper scripts
assets/               diagrams and images

Installation

Requires Python 3.10+, PyTorch 2.0+, and CMake 3.18+.

The C++ extension links against PyTorch at build time, so PyTorch must be installed before building the package. Use --no-build-isolation so the build step can find it:

# 1. Install PyTorch and build tools
pip install torch numpy wheel pybind11

# 2. Build and install mctorch (core only)
pip install -e . --no-build-isolation

# 3. Optional: include pgx game environments + JAX/mctx reference backend
pip install -e ".[pgx]" --no-build-isolation

# 4. Optional: development tools (pytest, ruff, mypy)
pip install -e ".[dev]" --no-build-isolation

Why --no-build-isolation?
pip normally builds in a sandboxed environment that only has the packages listed under [build-system] in pyproject.toml. PyTorch is too large to list there, so the sandbox would never have it. --no-build-isolation tells pip to build in your current environment instead, where torch is already present.


Quick start

import torch
import torch.nn as nn
from mctorch import alphazero_policy, RootFnOutput, RecurrentFnOutput

# Minimal two-headed network (policy + value)
class Net(nn.Module):
    def forward(self, obs):
        ...  # return policy_logits [B, A], value [B]

net = Net()
board = torch.zeros(16, 9)          # 16 games, 9-cell TicTacToe boards

with torch.no_grad():
    logits, values = net(board)

root = RootFnOutput(prior_logits=logits, value=values, embedding=board)

def recurrent_fn(params, actions, embedding):
    new_board, reward, discount = your_step_fn(embedding, actions)
    with torch.no_grad():
        new_logits, new_values = params(new_board)
    return RecurrentFnOutput(reward=reward, discount=discount,
                             prior_logits=new_logits, value=new_values), new_board

out = alphazero_policy(net, root, recurrent_fn, num_simulations=200)
print(out.action)        # [B] best action per game

See implement/examples/ for complete runnable scripts.


Running tests

bash scripts/run_tests.sh

or directly:

pytest implement/tests/ -v

Running benchmarks

# mctorch C++ arena vs raw baseline
python implement/benchmarks/bench_mctorch_native.py

# mctx JAX reference backend
python implement/benchmarks/bench_mctx.py

# All backends side-by-side
python implement/benchmarks/bench_all_backends.py

# pgx game environments (requires .[pgx] extras)
python implement/benchmarks/bench_pgx.py

Documentation


License

MIT

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

mctorch_mcts-0.1.0.tar.gz (39.1 kB view details)

Uploaded Source

Built Distribution

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

mctorch_mcts-0.1.0-cp311-cp311-win_amd64.whl (339.2 kB view details)

Uploaded CPython 3.11Windows x86-64

File details

Details for the file mctorch_mcts-0.1.0.tar.gz.

File metadata

  • Download URL: mctorch_mcts-0.1.0.tar.gz
  • Upload date:
  • Size: 39.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.3

File hashes

Hashes for mctorch_mcts-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5841ee150c058c656cb30c5a1f2850bb3f7181487e6735f1944d67a84c4f56bd
MD5 889863f4a042517b06c8248d00facd3f
BLAKE2b-256 ddcde316db66b29345a9ff9e8b011b054230e395b5654bb001b1d4cd1cb57dfc

See more details on using hashes here.

File details

Details for the file mctorch_mcts-0.1.0-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for mctorch_mcts-0.1.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 79bf9b7221686966dc37f55ce431085b1553f6085d13fb78f16644617b0f70b7
MD5 b88626477df0adac2f3bcd6af63e0f43
BLAKE2b-256 1f2b2171bccf731769e2df238d0faeae5d684917bd2c2e4a4677ded41b8b0a95

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