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

Uploaded CPython 3.12Windows x86-64

nnue_interface-0.2.4-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.4-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.4-cp311-cp311-win_amd64.whl (349.6 kB view details)

Uploaded CPython 3.11Windows x86-64

nnue_interface-0.2.4-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.4-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.4-cp310-cp310-win_amd64.whl (348.7 kB view details)

Uploaded CPython 3.10Windows x86-64

nnue_interface-0.2.4-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.4-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.4-cp39-cp39-win_amd64.whl (123.6 kB view details)

Uploaded CPython 3.9Windows x86-64

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

Uploaded CPython 3.8Windows x86-64

nnue_interface-0.2.4-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.4-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.4-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for nnue_interface-0.2.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 75214f95ba0ee8e70e602b3aaeb598176c4acd25b5fc901b0c92914a7465d061
MD5 0ef846c8381139c1a908d45781d50e14
BLAKE2b-256 7937911e37d538355cccb55747c297ba38a208456aedb880128fab4e0173040f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.4-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b87ecf11db411e5723c92a31aeb63192e2f49d4287393f9651f3b44d9ebceb41
MD5 7e5ed07f5db91336aa188daee85a9016
BLAKE2b-256 a28f25b9e0350c9526ae05fe238c683432781e70050e26292395fdc5e66eda65

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.4-cp312-cp312-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 a353d2f04b58c170f2fd6d13a6156753e33f54a95479ff909504fb17b7acb962
MD5 fc07dc723b80e6a8e91e8b798a9d38db
BLAKE2b-256 3ca120d591ffad19fd20b3b136fcb95be2b7368dba5a81abea78a5e12191a20c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 02ee6d0a3b29d2f77efcf90a4db8f64bc0483ff856625635b564f8b4443795d6
MD5 d37dc2614e35ea31e92bd0448e36f6df
BLAKE2b-256 c72ac344f67b0d3d22c1cb6366684bd75f6a6112af03d4b67298a862e6fdb12b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.4-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5ed206726ddab31700c19b0c4b6d7f68ccba2556a954ebd6bdb6907dce8e3294
MD5 81b933c4f3b3c5ef467f59e6c1a05356
BLAKE2b-256 40ee94452320a060245c2b03b46643a258755a5c8321673f1b60f28728077d95

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.4-cp311-cp311-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 142a1e2e052ef24a262b77f2e869ede9c6ca384f108e66a1e606bbf5fae8bb01
MD5 fd870daf4fedeaa19cab6847ff7983d1
BLAKE2b-256 6159cf5fc808f35c0e360afb1330aceddc4c3f26df8bd8a1f1709884c3b1f2f2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.4-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 28e313f369d078d37169afc8c14376f70bcb905f92f1c3b4ee13b36d76eb591b
MD5 7d43f87b3bc9ae64b0293788d44a19aa
BLAKE2b-256 5f4e332c47bad1dd9700c145e1c8f270f727644e9cd7a8802cab4b220258c14a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.4-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0fe1e94980394e44e23aec83fca4966d0f7c42a0c149ddcc82c9cf7a13d36ccb
MD5 72c0e37ad5d6d9591d1b8c14c51b802e
BLAKE2b-256 c94c4b3e6a9a816fb91a1d647a91868e86613bf1e3656bd2c83926e7c741bdab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.4-cp310-cp310-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 a68f7d2bcd2144299ba87a7a6097ca5da1bbf09a1e5d816b8e6e1e35af7ff26c
MD5 d3e5c773a8a18c89d5a3e7580f1b97e8
BLAKE2b-256 c26318a3adf935552fd89db998e4f0abb00ecff89eb196d1249e0e8907a72079

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.4-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 f554bbaf7d965ca5c78e622e3112fd1f89b71bb4287278cabe47a3064f0d9b17
MD5 f1e029a9ac35ae3077d02d9301b32ff7
BLAKE2b-256 4847516079e0bc1f617ebe25285826507c851e373675c50c1fac0338290166ac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.4-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0864b0d603aecb95e067abf320ec89704b73eb4e8baa1ff2d3fcb8c9d0772925
MD5 90111b7371baed08532a5ce9a403f9e7
BLAKE2b-256 2517152147bf1964f4d01278293e54e247ecfce91a62fa03d5579941ac2f0d07

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.4-cp39-cp39-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 dc53170b67069b3159210a5c6c35e2aabd135749d3443d4bdfa76ad64d8b97ab
MD5 b3cacb26aa2cdd939ddeb0afba46e644
BLAKE2b-256 a8b72261ec67b3f4e9e10a723f16e6561b1e53557b5d71c7a6840197ac1874fb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.4-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 9bf8ac3bf656cbc539bb484c22a683c6a7ea405f22f26a111b9de574999ac5e9
MD5 108515dfcfe69952efa5fd6c36049cf3
BLAKE2b-256 51d59050b42d9c5cf34995fdf86799f2ae84e6e2cd0df7a33cc24c0ae7afd521

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.4-cp38-cp38-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 fee711e6390bc017bc88cdcf42f49849c60ab80474a6d4ff578054a04f6b26a0
MD5 de0df7a6e54645f4984c445285df19e7
BLAKE2b-256 a456fa15b48d813e2c96bcfe10021562d9c4ac6c98a592b9767cbe4817cc885b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nnue_interface-0.2.4-cp38-cp38-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 23739258f082a31e7a77723cfbb8082279e4e611fafb613613f8c41f86ddfea8
MD5 7bc546cc92d5edd7e131176bf82b357f
BLAKE2b-256 036d5ece357731c11133c9b46ae10d707bc6021fe040d2353226e7475eb992b8

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