Skip to main content

A fast NNUE-Parser for my NNUE implementation

Project description

NNUE Parser Python Module

Overview

The NNUE Parser Python Module is a high-performance library for evaluating chess positions using an Efficiently Updatable Neural Network (NNUE). Written in Rust and exposed to Python via PyO3 and maturin, this module is optimized for speed and efficiency—allowing you to use it entirely from Python.

Features

  • High Performance: Leverages SIMD optimizations for fast NNUE evaluations.
  • Python-Only Interface: Use the NNUE parser directly from Python without any Rust code exposure.
  • Easy to Use: Simple API for model initialization and evaluation using FEN strings.
  • Lightweight: Designed for efficient memory and computational usage.

Installation

Ensure you have Rust installed along with Python (3.7 or later). Then, install maturin:

pip install maturin

Clone the repository and build the module:

git clone <repository-url>
cd nnue-parser
maturin develop

This command compiles the Rust code and installs the Python module in your current environment.

Usage

Once installed, you can initialize the NNUE model and evaluate chess positions using FEN strings directly from Python:

import nnue_parser

# Initialize the NNUE model (adjust the path to your NNUE file)
nnue_parser.init_nnue_py("path/to/nnue_file")

# Example FEN string for the starting chess position
fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"

# Evaluate the position in centipawns
evaluation = nnue_parser.eval_nnue_py(fen)
print("Evaluation in centipawns:", evaluation)

Benchmarking

Measure the Nodes per Second (NPS) by running the following benchmark test for one second:

import time
import nnue_parser

def benchmark_nnue_nps():
    nnue_file_path = "path/to/nnue_file"

    print("Initializing NNUE model...")
    nnue_parser.init_nnue_py(nnue_file_path)

    fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
    
    # Warm-up phase
    print("Warming up the model...")
    for _ in range(10):
        _ = nnue_parser.eval_nnue_py(fen)

    print("Starting benchmark for 1 second...")
    count = 0
    start_time = time.perf_counter()
    end_time = start_time + 1.0  # Run for 1 second
    while time.perf_counter() < end_time:
        _ = nnue_parser.eval_nnue_py(fen)
        count += 1
    total_time = time.perf_counter() - start_time

    print(f"Evaluations in {total_time:.6f} seconds: {count}")
    nps = count / total_time
    print(f"NPS (Nodes per Second): {nps:.2f}")

if __name__ == "__main__":
    benchmark_nnue_nps()

Project Structure

  • src/lib.rs: Contains the NNUE parser implementation and PyO3 bindings.
  • Cargo.toml: Configures Rust dependencies and the build process for PyO3 and maturin.
  • README.md: This file.

Contributing

Contributions are welcome! Please open issues or submit pull requests if you have suggestions or improvements.

License

This project is licensed under the MIT License.

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 Distribution

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

nnue_parser-0.3.0-cp311-cp311-win_amd64.whl (129.4 kB view details)

Uploaded CPython 3.11Windows x86-64

File details

Details for the file nnue_parser-0.3.0-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for nnue_parser-0.3.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 3f1277e28be36ed4ca6766afe9983546ad4ef075cc6d405912ad2d5aeff369ba
MD5 d245b91ffaf4d07ff5d8bbcf24c7df39
BLAKE2b-256 8fc6c0c216ab0b411a9978f2f6dbd6ad9103093e779bd5d09a97fa65fad08def

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