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

Uploaded CPython 3.12Windows x86-64

nnue_interface-0.1.0-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.1.0-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.1.0-cp311-cp311-win_amd64.whl (348.2 kB view details)

Uploaded CPython 3.11Windows x86-64

nnue_interface-0.1.0-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.1.0-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.1.0-cp310-cp310-win_amd64.whl (347.3 kB view details)

Uploaded CPython 3.10Windows x86-64

nnue_interface-0.1.0-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.1.0-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.1.0-cp39-cp39-win_amd64.whl (123.5 kB view details)

Uploaded CPython 3.9Windows x86-64

nnue_interface-0.1.0-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.1.0-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.1.0-cp38-cp38-win_amd64.whl (122.4 kB view details)

Uploaded CPython 3.8Windows x86-64

nnue_interface-0.1.0-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.1.0-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.1.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for nnue_interface-0.1.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 d0e7b7d087f567bdd1f7e3a085033b4adaf490f1956dd37f06209791ccad6530
MD5 10aeb737380b815f89f16f78507e2aa8
BLAKE2b-256 9f945856c02f6407aa2ada5bbff6afa46c162eda52b4bca2e54ee338f4baa2eb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.1.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 919f09e8fc5fb3493496b6ecc45b78b5156aa91a973bedc7a0830ed13a1bb7ee
MD5 6e076c435ff5c57e199c42c097874486
BLAKE2b-256 d5c4a7bec93b07f4051ae7832764557d6302dbd06107b0e6296a4057f342d437

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.1.0-cp312-cp312-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 0639547201c6bfc0baa295d082d5903bc553c533299bc00463b17e9589fc9924
MD5 336c6dd59fae117d9edf1331a8adb442
BLAKE2b-256 285517582babc76c97b58e1f76bda9ca941fd53ef792189a8e34a8a988ea77ab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.1.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 7892150d63fb6c7633acef3deadcc43ef1046c59d53e99f07ce71b475e184047
MD5 96286bb211f6ed420745e3b4a98448a0
BLAKE2b-256 4a99325e7471cb7bd541e62c44a5dee4cd3fd3859736c90b6b709d503027b042

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.1.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d3c3ec19db9d8309a636130912353b5a04be8d3cbe3cebbe51475d23f0e36900
MD5 6ee5f6013155eacc77847e140f443dd0
BLAKE2b-256 f3351a225e7a0702d4953da68cb880d76636b2f36c3a7d5681e3df7eb7125da7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.1.0-cp311-cp311-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 83b8326c65f12d9354fb0f2231e2dcdc5b240486eee7165758de1151ee340a9e
MD5 1890dfafd882c2ff1d66fd75f5116bbb
BLAKE2b-256 a0421d2be2d1a1f62b2ffe6e05bf2f0223c9bc2bf0bbcffdac313d1f695993e6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.1.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 8a99c78316b67dde93229ff4a39cb43f5c7c84d3fae8ae1f781635654106792e
MD5 21ff1df219c331cc6c5df88d3285e38e
BLAKE2b-256 a3fd1fa32301ad4705e99a9df4d560ea7077455384bdeba0fbd149bc3eba1681

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.1.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e0dac340d76abf093f3fe92a6ba11e37f63ea826091c30724de7c5078a9eeed2
MD5 4c03005b972cb33ce11fbaa991b621a4
BLAKE2b-256 949a34cd1e0a3ffa3599d6af7b82f0878b3832840c4e3cafc8405276fe613fa3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.1.0-cp310-cp310-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 6e22511517929ae9b65a673e8f5db1aff0b52b9e66997d58f1549ab35b987cc9
MD5 e9bc957711ce3e115f8444b9f1840967
BLAKE2b-256 5e352ab7d6218190760529a68f0b5d9f9b511fab9e4de4ed7273ebba36b59131

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.1.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 3b2713244679845af59d66cf957a0c5e1cff00c12b7c1f7ee354de0a0d49c565
MD5 e0091ca28ad058d553e700553d64cfbb
BLAKE2b-256 774b2c5705902b97db230119804c8e10089f8ca104f701b482f52713592800b8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.1.0-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 32937366005b4ae701e59ef64977f2cc22f70250ce7c183ac0aa2338b152c292
MD5 21d2ae6d8ac69b3e4d3a111ab068272a
BLAKE2b-256 4761060ff361c6529ced67bebae99d5a8780676884566d8c0d35b1c86fa70475

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.1.0-cp39-cp39-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 907863d249d91ae73526f14b1942ae1d6172aeb93883dc53e149e6afcd4481f7
MD5 8721638c192b62681df4c1968881dedb
BLAKE2b-256 0e72dcc6109fe56c068e129c70549c0d3335e2dfa43cfc949e8b9924e59a50bc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.1.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 a226de50b3136b93a1b312b1c53441b6357d58823ba4683adaf194daa8ca6701
MD5 3b41c056e24cc4dc7a09e2496580875e
BLAKE2b-256 74d9fbdc54e80709087ea17b0f54e06c2959731b42557f99e7910a386a36208f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.1.0-cp38-cp38-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 05bc2fa3c1689bf95b025b7a9009ac2354403ba649db99cb08a8813a59b492ee
MD5 929a3a10021d1b196a0d55c9d0b87845
BLAKE2b-256 59ab95927bdf124de3e77516834e70b886beb1bbd0185931c32c522270218e0d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.1.0-cp38-cp38-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 c0f163db4725750d1efb5c3837fe63fc915fd9fb14586118491ed3406a96ce13
MD5 1457c6250d09f50f7a7c3e4f66ac20d5
BLAKE2b-256 db0a8a4e2faa8f28499bf789672f858425915b6e2caf1cf17e6a6096dec30517

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