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 https://github.com/github-jimjim/NNUE-Parser
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.1-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.1-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for nnue_parser-0.3.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c9a4a95b233aad25818d11cb317f9a7fc53636b1b9bb43db2468da3a9cad9cc1
MD5 34db42d32b4f96ba604eeb106811b339
BLAKE2b-256 75304f362fd896e19197fd16a2c3305c84fc69dfda601a93ab1a38517c4c527b

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