Skip to main content

Python bindings for extracting Stockfish NNUE neural network activations and evaluations

Project description

nnue-interface

Python bindings for extracting Stockfish NNUE neural network activations and evaluations. Perfect for machine learning applications, chess analysis, and fine-tuning neural networks.

Features

Key Capabilities:

  • Extract NNUE accumulator activations (hidden layer 0): 3072 dimensions (Big network) or 128 dimensions (Small network)
  • Extract intermediate layer activations (layers 1-2): For deep network analysis
  • Extract PSQT values: Piece-square table contributions
  • Get final evaluations in centipawns
  • Cross-platform: Works on Linux, macOS, and Windows
  • Fast: Compiled C++ extension via pybind11
  • ML-Ready: All outputs as float32 numpy arrays

Installation

From PyPI (recommended)

pip install nnue-interface

From Source

git clone https://github.com/yourusername/nnue-interface.git
cd nnue-interface
pip install -e .

Requirements:

  • Python 3.8+
  • C++17 compatible compiler (GCC, Clang, MSVC)
  • CMake 3.15+
  • NumPy 1.19+

Quick Start

import nnue_interface
import numpy as np

# Extract all NNUE activations and evaluation for a position
fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"

acc_white, acc_black, psqt, layer1, layer2, eval_final, eval_psqt = \
    nnue_interface.get_activations_and_eval(fen)

print(f"Evaluation: {eval_final:.2f} cp")
print(f"Accumulator shape: {acc_white.shape}")  # (3072,) for Big network
print(f"Layer 1 shape: {layer1.shape}")         # (30,) 
print(f"Layer 2 shape: {layer2.shape}")         # (32,)

API Reference

get_activations_and_eval(fen: str) -> tuple

Extract all NNUE activations and evaluation for a given position.

Parameters:

  • fen (str): FEN notation of the chess position

Returns:

  • acc_white (ndarray): White perspective accumulator, shape (3072,) or (128,)
  • acc_black (ndarray): Black perspective accumulator, shape (3072,) or (128,)
  • psqt (ndarray): PSQT values, shape (2, 8)
  • layer1 (ndarray): First hidden layer activations, shape (30,) or (15×2)
  • layer2 (ndarray): Second hidden layer activations, shape (32,)
  • eval_final (float): Final evaluation in centipawns
  • eval_psqt (float): PSQT-only evaluation in centipawns

get_evaluation(fen: str) -> float

Get only the final evaluation for a position (faster if you don't need activations).

Parameters:

  • fen (str): FEN notation

Returns:

  • Evaluation in centipawns (float)

get_network_info() -> dict

Get information about the NNUE network architecture.

Returns:

{
    'TransformedFeatureDimensionsBig': 3072,
    'TransformedFeatureDimensionsSmall': 128,
    'L2Big': 15,
    'L3Big': 32,
    'L2Small': 15,
    'L3Small': 32,
    'PSQTBuckets': 8,
}

Examples

Using Activations for Machine Learning

import nnue_interface
import numpy as np

# Collect training data
positions = [
    "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
    "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1",
    # ... more positions
]

X_acc = []  # Accumulator features
X_layers = []  # Intermediate layer features
y = []  # Target evaluations

for fen in positions:
    acc_w, acc_b, psqt, layer1, layer2, eval_final, _ = \
        nnue_interface.get_activations_and_eval(fen)
    
    # Combine features
    features = np.concatenate([acc_w, acc_b, psqt.flatten(), layer1, layer2])
    
    X_layers.append(features)
    y.append(eval_final)

X = np.array(X_layers)
y = np.array(y)

# Now use X and y for ML training (scikit-learn, PyTorch, TensorFlow, etc.)

Analyzing Network Activations

import nnue_interface

fen = "r1bqkb1r/pppp1ppp/2n2n2/1B2p3/4P3/5N2/PPPP1PPP/RNBQK2R w KQkq - 4 4"

acc_w, acc_b, psqt, layer1, layer2, eval_final, _ = \
    nnue_interface.get_activations_and_eval(fen)

print(f"Position evaluation: {eval_final:.2f} cp")
print(f"\nAccumulator statistics (White):")
print(f"  Mean: {acc_w.mean():.2f}")
print(f"  Std:  {acc_w.std():.2f}")
print(f"  Min:  {acc_w.min():.2f}")
print(f"  Max:  {acc_w.max():.2f}")

print(f"\nLayer 1 sparsity: {(layer1 == 0).sum() / layer1.size * 100:.1f}%")
print(f"Layer 2 sparsity: {(layer2 == 0).sum() / layer2.size * 100:.1f}%")

Architecture

The NNUE network consists of:

  1. Input Layer (Accumulator): 3072 or 128 dimensions

    • Efficiently updatable representation of board state
    • Updated incrementally as moves are made
  2. Layer 1:

    • FC layer (sparse input) + SqrClippedReLU + ClippedReLU
    • Output: 15 dims × 2 (concatenated)
  3. Layer 2:

    • FC layer + ClippedReLU
    • Output: 32 dims
  4. Output Layer:

    • FC layer → single scalar evaluation
    • Also uses PSQT values for final output

All intermediate activations use int8/uint8 quantization for efficiency, converted to float32 for Python.

Performance

  • Speed: ~50-200 µs per position (depending on CPU)
  • Memory: ~2 MB for network weights
  • No dependencies: Only NumPy required at runtime

Building from Source

Linux / macOS

git clone https://github.com/yourusername/nnue-interface.git
cd nnue-interface
pip install -e .

Windows (MSYS2 UCRT64)

git clone https://github.com/yourusername/nnue-interface.git
cd nnue-interface
pip install -e .

Build Documentation

CMake is used for cross-platform builds. To manually build:

cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build

Testing

pip install pytest numpy
pytest tests/

License

GPL-3.0-or-later (same as Stockfish)

Citation

If you use this in research, please cite Stockfish:

@software{stockfish,
  title = {Stockfish},
  url = {https://stockfishchess.org/},
  author = {Tord Romstad and Marco Costalba and Joona Kiiski and Gary Linscott},
}

Contributing

Contributions welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Run tests: pytest tests/
  5. Submit a pull request

Troubleshooting

Build fails on Linux with missing dependencies

# Ubuntu/Debian
sudo apt-get install build-essential cmake python3-dev

# Fedora
sudo dnf install gcc gcc-c++ cmake python3-devel

Import error on Windows

Make sure you're using the correct Python version (64-bit). The wheel must match your Python installation:

python -c "import struct; print('64-bit' if struct.calcsize('P') == 8 else '32-bit')"

Resources

Authors

  • Created with Stockfish source code
  • Python bindings by Rushil Saraf

Have questions? Open an issue on GitHub!

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

nnue_interface-0.2.1-cp312-cp312-win_amd64.whl (349.7 kB view details)

Uploaded CPython 3.12Windows x86-64

nnue_interface-0.2.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (6.3 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

nnue_interface-0.2.1-cp312-cp312-macosx_10_15_universal2.whl (953.9 kB view details)

Uploaded CPython 3.12macOS 10.15+ universal2 (ARM64, x86-64)

nnue_interface-0.2.1-cp311-cp311-win_amd64.whl (348.2 kB view details)

Uploaded CPython 3.11Windows x86-64

nnue_interface-0.2.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (6.2 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

nnue_interface-0.2.1-cp311-cp311-macosx_10_15_universal2.whl (961.6 kB view details)

Uploaded CPython 3.11macOS 10.15+ universal2 (ARM64, x86-64)

nnue_interface-0.2.1-cp310-cp310-win_amd64.whl (347.3 kB view details)

Uploaded CPython 3.10Windows x86-64

nnue_interface-0.2.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (6.2 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

nnue_interface-0.2.1-cp310-cp310-macosx_10_15_universal2.whl (958.7 kB view details)

Uploaded CPython 3.10macOS 10.15+ universal2 (ARM64, x86-64)

nnue_interface-0.2.1-cp39-cp39-win_amd64.whl (123.5 kB view details)

Uploaded CPython 3.9Windows x86-64

nnue_interface-0.2.1-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (6.2 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

nnue_interface-0.2.1-cp39-cp39-macosx_10_15_universal2.whl (735.9 kB view details)

Uploaded CPython 3.9macOS 10.15+ universal2 (ARM64, x86-64)

nnue_interface-0.2.1-cp38-cp38-win_amd64.whl (122.4 kB view details)

Uploaded CPython 3.8Windows x86-64

nnue_interface-0.2.1-cp38-cp38-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (6.2 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

nnue_interface-0.2.1-cp38-cp38-macosx_11_0_universal2.whl (735.7 kB view details)

Uploaded CPython 3.8macOS 11.0+ universal2 (ARM64, x86-64)

File details

Details for the file nnue_interface-0.2.1-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for nnue_interface-0.2.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 8d446f7565651f43627060f2d26172fe8461b04a8cd611529364af8de2e0fc06
MD5 dd019be67840fb9cad19e90d7c718569
BLAKE2b-256 43ee634fb4b481e84c70868551208f7fca772d84e2721e0f7b63ed928261385c

See more details on using hashes here.

File details

Details for the file nnue_interface-0.2.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for nnue_interface-0.2.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b089f15131607e95b2a64bc616876aee4e36f26ac87e4600b299c2dddda8aca5
MD5 138094c40d01abf4e975331ad4339d85
BLAKE2b-256 bb988f2e10a0da706c7c527cf9edf2a2b282fd9d78282732e38d631b8193a5bb

See more details on using hashes here.

File details

Details for the file nnue_interface-0.2.1-cp312-cp312-macosx_10_15_universal2.whl.

File metadata

File hashes

Hashes for nnue_interface-0.2.1-cp312-cp312-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 d177b82cfd4eae4a06483bbeaeff4b8459b69e384976775d7a01b792ac6afff6
MD5 f1659b2676256c3aa89b2d05d7dbdd0c
BLAKE2b-256 f23c759764c92fcf8b86d03301ba3b561e712e84151ffccf7b28cd5b0b860c3a

See more details on using hashes here.

File details

Details for the file nnue_interface-0.2.1-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for nnue_interface-0.2.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 d0f7df0e4c405fe9bc6e7481d282ff74d07fe55fb44c96b164e3b4802ef9864a
MD5 8bbf2c5b3dc549c6d2c1b14a1706b744
BLAKE2b-256 cf20b09e906ca5035db9fccda7c35520d480fb42a5dd2389012715a1a4d5083a

See more details on using hashes here.

File details

Details for the file nnue_interface-0.2.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for nnue_interface-0.2.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 eeaf4cde05dcb8edce5e69f10b0178744033a4fd4cc9b2fd5257fc345ad1e4a2
MD5 da5e9a00837ab1442593eaa01c31dece
BLAKE2b-256 1361c46ed9f3e3fd40c8ff1224df8f611075706890ab95ba10e141b2605f92e1

See more details on using hashes here.

File details

Details for the file nnue_interface-0.2.1-cp311-cp311-macosx_10_15_universal2.whl.

File metadata

File hashes

Hashes for nnue_interface-0.2.1-cp311-cp311-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 1591bb6f49f0a39ffe3c3cd479c1490a1a76b392f764e4449a965572250c71fe
MD5 93ee2235e8b8878ca403d1523f85a9d8
BLAKE2b-256 8d2f7b21638060a8bac432d16cce4de75e341dbc54e9cfa89398730c7b820d70

See more details on using hashes here.

File details

Details for the file nnue_interface-0.2.1-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for nnue_interface-0.2.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 60d5438b6a1de34999170631ffcaa4a2d0951bc1ef02ceb68b6d1b58c960a790
MD5 67c618a591c939eb78c3e1fc9dd449b4
BLAKE2b-256 7568bcabe7292b16194e2a0c688eb187d88b4c6dfcf1bc0e7ae29cfd6d7d5446

See more details on using hashes here.

File details

Details for the file nnue_interface-0.2.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for nnue_interface-0.2.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c4ed57df1ebb48cf1f93710cb5d49104adeb5ca67496307a762c8816388f544f
MD5 e36417801199970d2ad212b5b8ea6f57
BLAKE2b-256 06405c9ae6a046e2275857c208b03419838980b0e7623ab8915bfa06394f2fa1

See more details on using hashes here.

File details

Details for the file nnue_interface-0.2.1-cp310-cp310-macosx_10_15_universal2.whl.

File metadata

File hashes

Hashes for nnue_interface-0.2.1-cp310-cp310-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 f0c02361dc41140588f9a107af4750ab9708d2d1e20015391947de3c27fa5f7f
MD5 ca4c30363e4bf8469c20a5aa26ac318a
BLAKE2b-256 3fcd3f426b887a21b3c765caee421ee6fb58921c3bc9d1d84d34f6e5b07a2a7b

See more details on using hashes here.

File details

Details for the file nnue_interface-0.2.1-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for nnue_interface-0.2.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 068e3c1b6b70207225dafdc5c0b0eb982ced25a2beb7aa86319cc9cb01d016e2
MD5 ef2ab80e1dd7c08de69cf5f9cc8a02b2
BLAKE2b-256 c9f6368b1a2a7b182ca876a1b88bac32959e9f26cd9d08448bfa85bc9f08602e

See more details on using hashes here.

File details

Details for the file nnue_interface-0.2.1-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for nnue_interface-0.2.1-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 25624e9f156a597aae2856dfdbc4c3cb519a41d80ae1024f995fbec5056a41ea
MD5 fbbca6b3d02d65d7036a785f4f85e481
BLAKE2b-256 36909bafd812983e2bcf2cabf8de507cb66e750d09058ee5c3722d08d154fdaa

See more details on using hashes here.

File details

Details for the file nnue_interface-0.2.1-cp39-cp39-macosx_10_15_universal2.whl.

File metadata

File hashes

Hashes for nnue_interface-0.2.1-cp39-cp39-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 0537ca3a78ded5876ca56b201b6218a90ef808a1c42772969aafe8ea5fb4e5d8
MD5 5315d926c261e7fa44a561fb0e443a66
BLAKE2b-256 df2feb46b2c152f03cb60e15862c615a1d49b87e93f8be0672c4f054c1ed7d99

See more details on using hashes here.

File details

Details for the file nnue_interface-0.2.1-cp38-cp38-win_amd64.whl.

File metadata

File hashes

Hashes for nnue_interface-0.2.1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 1f7c59487bd8e7deb06b91853a08572e9b10a15696f3facee589df99a0c82de2
MD5 0f4d4b37bdfc5ed79084b7758a9a06a2
BLAKE2b-256 cd1f2cddb7084e5a575091eca97e335b34fdc35d036bf3646b1addaffabffbd0

See more details on using hashes here.

File details

Details for the file nnue_interface-0.2.1-cp38-cp38-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for nnue_interface-0.2.1-cp38-cp38-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 109c83881f1c9645021d0655fd6d9d655d3bdbd12f7376204803a1400d6bfb6c
MD5 971127d32f5d28b3eaf08eafaff02995
BLAKE2b-256 d3c922793ea196a4f759fef8dd043d473332eb2d51ebac86b3afb1eb3467ab3a

See more details on using hashes here.

File details

Details for the file nnue_interface-0.2.1-cp38-cp38-macosx_11_0_universal2.whl.

File metadata

File hashes

Hashes for nnue_interface-0.2.1-cp38-cp38-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 f010b39c85cf3dffd1637288f748b559f215de08d61bc47f3b540f73f06527c5
MD5 e8599bdc4ee168af8dd23322824ca2c3
BLAKE2b-256 f98123eddb8701b1b1a4f88492f2783700c6f20b54fb73b5142bca64193a2382

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