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.2-cp312-cp312-win_amd64.whl (351.1 kB view details)

Uploaded CPython 3.12Windows x86-64

nnue_interface-0.2.2-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.2-cp312-cp312-macosx_10_15_universal2.whl (955.3 kB view details)

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

nnue_interface-0.2.2-cp311-cp311-win_amd64.whl (349.6 kB view details)

Uploaded CPython 3.11Windows x86-64

nnue_interface-0.2.2-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.2-cp311-cp311-macosx_10_15_universal2.whl (963.0 kB view details)

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

nnue_interface-0.2.2-cp310-cp310-win_amd64.whl (348.7 kB view details)

Uploaded CPython 3.10Windows x86-64

nnue_interface-0.2.2-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.2-cp310-cp310-macosx_10_15_universal2.whl (960.1 kB view details)

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

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

Uploaded CPython 3.9Windows x86-64

nnue_interface-0.2.2-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.2-cp39-cp39-macosx_10_15_universal2.whl (736.0 kB view details)

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

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

Uploaded CPython 3.8Windows x86-64

nnue_interface-0.2.2-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.2-cp38-cp38-macosx_11_0_universal2.whl (735.8 kB view details)

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

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 459aad9fe072105aaaf065fe6e7ae13685a1b217314a2eae9868a907616d1216
MD5 a52a1892f09ff8a258f864d390253411
BLAKE2b-256 c82d49bbcc6c24e0fc67835e61225b188ad801964b7c90508a22a4d107f01d4b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.2-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d11fc5b258a46f26cb04aa9300c30e23927290a833f4e8c796130a0a01210c69
MD5 3530218c51a321e0ffb8dfcfd7470971
BLAKE2b-256 39cbbfe1fff4ca024d03557e58127f6d8ea86e02e46d4859ce18adbde017105c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.2-cp312-cp312-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 b0fad80696d25de37e657143afa764f961eb5a93a0f24a74c9f492ef7fb060f0
MD5 a4db04651f1c2429d54d65c98f9c2a75
BLAKE2b-256 7e8c3228ff2d015a24db607663325c0bba384f2098774b247f0444d5ce164617

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 5bbb65eb8ca2508e028cc3efaabe1d88b4f238be3ead5c99577fbb63f12cc350
MD5 ffe3d3f11d944382cb153283542304f8
BLAKE2b-256 11956a3d117bab26f702fed2c3d3ef4bed9c4d737350ef42e44c93f2c7466898

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.2-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9cfa536be65777cba4ed9711d432b122334cfbd56d2b729080d801cf0032cae4
MD5 c2f4882d305ce5945dc4086930f00adb
BLAKE2b-256 ea5bf05e17981f544873a4507050c2c3279e37a5199dfcaf86d4e44ab743fb94

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.2-cp311-cp311-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 749bd99e7d48e4e706b07394571498ee09c78ea8d5b23165b22e8ecfdbb4cb59
MD5 ee44453e86f3f190bf30d09cfeaa92e3
BLAKE2b-256 04065ff8d10d7cf9b6018b23813377e90ca78e0a99395863cd9e2dccbe63e70d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 fdf24f140a044fc82ca7df3938861ac3e3ba6961b2ce0650319a7187b180526a
MD5 36394d6807ff3f265a8e3f5bc64e6816
BLAKE2b-256 d6a61bf9e6ee6e6391caf9fada4452dc08e8cdd82c04d0637b063478d581a907

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.2-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 3ba4b95b77963f0de75f570fee02b944fe768c5a729aac40762c244b2012d33e
MD5 49b0a209ae1a4ba84cc7972c6bfbb947
BLAKE2b-256 9810db35056c014f619460002d118429458fbcd6446f2683899ef70860ac0e2e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.2-cp310-cp310-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 9b506d1d2db9b1dc52912fddd7bf4bd3bbe0f24fed6e8ebe19fd7cb5b4c88f5d
MD5 43b4f0a587e6bea7b17956db59c21788
BLAKE2b-256 9e8439eb456f56e0a5e419f684b2afec768a4debb62af08a00394f9e2a6e1d84

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.2-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 9e63bdc323290de315307180adf6c9902e407269bf5e1aedadd1ed552205174e
MD5 821067fd6131e3f570f39da385649ad7
BLAKE2b-256 eaa8d8112459bad39cd9fba95e1ae2c20c601dadeb5cf4679b82bc3d597a276d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.2-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 37635426f528faf45dd40c5cceb3b0ca4b06e395a4ce2d4bd5a95128dd5ceaf8
MD5 63431154ea978bb4e3a45ef1d009f518
BLAKE2b-256 774e83c44c08744e19ccfc63af79026e9547c12c699f85915752f41b024b2a65

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.2-cp39-cp39-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 a58cbc05dce5f337f2bf0bdecec98535c712851d5c0ef2bf6157e5fd9ca6f7ce
MD5 e814fbae1c1174e4b4dda331411de81a
BLAKE2b-256 74f1aa2fb4b01cf86c5db74965f65393e316e4e536de582d4016597848b9a22d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.2-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 80a1f0ef6414ebc78b2a4d7df1c11ba3367f8923e094c0f00d0f2d32149471fc
MD5 aea386f63223bf4be1825c6fa885091c
BLAKE2b-256 e1d330fff9cd7c08967d25b86fb0bf2c1f69f1a1e2a8f5f74f1ff7c1d10616cc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.2-cp38-cp38-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 3e3aa51aec30b77b3fb66fcb790111ed924771cff7aac7c6edddcf3d174a7bae
MD5 98251b35bf3fa29adb010c5c8a7e6fd7
BLAKE2b-256 522dd97b4f0a60a9661f28d7a404ea2fe3fd3642da798a133f4cd83a40e5f867

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.2-cp38-cp38-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 b521de103eb0e05d7b5be1b5d834d15ea64863271cc948184253502ee3a82c88
MD5 40432543dc910636f3a6e78ea9210ceb
BLAKE2b-256 9d9296f5723e5b562f95c6e7eea671164a458d32fac96f59e8f5b6e40958d1cc

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