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]inpyproject.toml. PyTorch is too large to list there, so the sandbox would never have it.--no-build-isolationtells 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
Metadata
Release files for mctorch-mcts 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mctorch_mcts-0.1.2-cp311-cp311-win_amd64.whl | CPython 3.11 | CPython 3.11 | Windows x86-64 | Details for mctorch_mcts-0.1.2-cp311-cp311-win_amd64.whl |
Release files / mctorch_mcts-0.1.2-cp311-cp311-win_amd64.whl
| Download URL | mctorch_mcts-0.1.2-cp311-cp311-win_amd64.whl |
|---|---|
| Size | 348.1 kB |
| Tags | CPython 3.11 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
53af72dddf429d8ef75f4475dab75ea29c1b9eb5b8e4554e382255b773221e3f
|
|
BLAKE2b-256 checksum How to use checksums |
fb51243748ebf26ece0f3e179bb979f079808e0bf94f86ded74aaba179222ecc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.3
|
Security for mctorch-mcts 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.