# tadaoyamaoka/deeplearningshogi architecture

> Architecture diagram of the GitHub repository tadaoyamaoka/deeplearningshogi, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tadaoyamaoka/deeplearningshogi
- Repository: https://github.com/tadaoyamaoka/deeplearningshogi

## Overview

##### Project Type and Purpose
- This project is a specialized deep learning and game engine framework for Shogi, inspired by AlphaGo/AlphaZero. Its purpose is to allow experiments with deep reinforcement learning for Shogi – handling board management, move generation, search (MCTS/self-play), and neural network training/inference.
- The README clearly separates the codebase into several domains: a C++ board and engine library (cppshogi), the deep learning aspects in Python (dlshogi), self-play modules, and USI engines for interfacing with shogi GUIs/clients.

##### Core Components
- C++ Shogi Library (cppshogi)
- Implements board management, move generation, and basic game logic.
- Provides input feature generation used by the neural network.
- Deep Learning Engine (dlshogi)
- Contains network definitions (multiple network variants in dlshogi/network).
- Handles training scripts (train.py, ptl.py) and preprocessing of game data.
- Includes utility scripts (in dlshogi/utils) for model conversion, logging, and data manipulations.
- Self-Play Module (selfplay)
- Implements MCTS-based search and self-play engine that leverages both the C++ game engine and NN evaluations.
- USI Engine Layers (usi, usi_onnxruntime, usi_multiponder)
- Implements USI protocol-based engines to interact with external Shogi interfaces.
- usi_onnxruntime integrates ONNX Runtime for neural network inference.
- usi_multiponder provides extended search capabilities (pondering with multiprocessor support).
- Build and Conversion Tools
- build_onnx directory: Contains scripts and build files for converting models to ONNX format.
- Docker files and various project files (Visual Studio, Makefiles) for different target platforms.
- Test and Auxiliary Modules
- The “test” folder is used for benchmark and validation codes for the engine and network performance.

##### Relationships and Data Flow
- At the core is the C++ library (cppshogi) which provides board state management and move generation; this is utilized by multiple components.
- The deep learning module (dlshogi) trains the neural networks. Once trained, model files are integrated into the USI engines for move evaluation. The conversion scripts in both dlshogi and build_onnx help ensure models are in the proper ONNX format for fast runtime use.
- The selfplay module ties together both classical search (MCTS) and NN evaluations. It interacts with the board logic and neural network inference services (via the USI engine modules) to perform self-play games for further training or analysis.
- USI engine components (usi, usi_onnxruntime, usi_multiponder) serve as the interface that supports shogi engine protocols and connects to external clients, using either the native C++ implementations or accelerated ONNX runtime.
- The utility scripts across the project help pre-process data (e.g., converting CSA, KIF, PGN files), manage training checkpoints, and merge or evaluate game records.

##### Architectural Patterns and Technology Stack
- The project demonstrates a separation of concerns by splitting modules based on functionality (game logic, search, NN training/inference, and client interfacing).
- It follows a layered architecture:
- Base layer: Fundamental game logic (cppshogi) with underlying core algorithms.
- Middle layers: Search (selfplay and USI engines) that rely on both classical and neural components.
- Top layer: Deep learning training/inference (dlshogi) and model conversion utilities.
- Technologies/Frameworks:
- C++ (Visual Studio projects, Makefiles) for performance-critical components.
- Python (for deep learning, preprocessing, and training, using PyTorch).
- ONNX Runtime integration for efficient NN inference.
- CUDA, cuDNN, and TensorRT for GPU acceleration.
- USI protocol for engine-to-interface communication.

- Refine it gradually by breaking down each main component into sub-components (e.g., within dlshogi, break out network variants and utility scripts).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Core Layer
    subgraph "Core Game Logic"
        cp["C++ Shogi Library (cppshogi)"]
    end

    %% Self-Play Layer
    subgraph "Self-Play & Search"
        sp["Self-Play Module (MCTS/Self-Play)"]
    end

    %% Deep Learning Layer
    subgraph "Deep Learning"
        dl["Deep Learning Engine (dlshogi)"]
    end

    %% USI Engine Interfaces
    subgraph "USI Engine Interfaces"
        usiNative["USI Engine – Native"]
        usiOnnx["USI Engine – ONNX Runtime"]
        usiMulti["USI Engine – Multiponder"]
    end

    %% Auxiliary Tools
    subgraph "Auxiliary Tools"
        build["Build & Conversion Tools"]
        test["Test & Auxiliary Modules"]
    end

    %% Data Flow
    cp -->|"provides_board_state"| sp
    sp -->|"requests_NN_evaluation"| dl
    dl -->|"returns_NN_value"| sp

    dl -->|"trained_model"| usiNative
    dl -->|"trained_model"| usiOnnx
    dl -->|"trained_model"| usiMulti

    sp -->|"interfaces_with"| usiNative

    dl -->|"conversion"| build
    build -->|"converted_ONNX_model"| usiOnnx

    test -->|"benchmarks_validation"| cp
    test -->|"benchmarks_validation"| dl

    %% Click Events
    click cp "https://github.com/tadaoyamaoka/deeplearningshogi/tree/master/cppshogi/"
    click dl "https://github.com/tadaoyamaoka/deeplearningshogi/tree/master/dlshogi/"
    click sp "https://github.com/tadaoyamaoka/deeplearningshogi/tree/master/selfplay/"
    click usiNative "https://github.com/tadaoyamaoka/deeplearningshogi/tree/master/usi/"
    click usiOnnx "https://github.com/tadaoyamaoka/deeplearningshogi/tree/master/usi_onnxruntime/"
    click usiMulti "https://github.com/tadaoyamaoka/deeplearningshogi/tree/master/usi_multiponder/"
    click build "https://github.com/tadaoyamaoka/deeplearningshogi/tree/master/build_onnx/"
    click test "https://github.com/tadaoyamaoka/deeplearningshogi/tree/master/test/"

    %% Styling
    classDef core fill:#FFEDCC,stroke:#CC6600,stroke-width:2px;
    classDef dl fill:#CCFFCC,stroke:#008000,stroke-width:2px;
    classDef selfp fill:#CCE5FF,stroke:#0056b3,stroke-width:2px;
    classDef usi fill:#FFCCCC,stroke:#CC0000,stroke-width:2px;
    classDef aux fill:#E6CCFF,stroke:#6600CC,stroke-width:2px;

    class cp core;
    class dl dl;
    class sp selfp;
    class usiNative usi;
    class usiOnnx usi;
    class usiMulti usi;
    class build aux;
    class test aux;
```
