How tadaoyamaoka/deeplearningshogi works
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).