How kwai/douzero works
Project Type and Purpose
• DouZero is a reinforcement learning framework for a challenging card game (DouDizhu). Its primary goal is to use self-play and a specialized Deep Monte Carlo (DMC) algorithm to train AI agents.
• The README and file structure indicate that the project covers the entire pipeline from simulation and training to evaluation, including various baseline agents.
File Structure
• The project is organized into several top-level folders:
– The “douzero” directory contains key modules:
▪ “dmc” holds the implementation of the Deep Monte Carlo algorithm (arguments, model definitions, file writing, etc.).
▪ “env” encapsulates the game environment and related utilities (e.g., game logic, move detectors, move generators, and selectors).
▪ “evaluation” contains different evaluation agents (e.g., deep_agent, random_agent, rlcard_agent) and simulation scripts to run self-play.
– Other important files include training and evaluation entry points (train.py, evaluate.py, generate_eval_data.py) and setup/configuration files.
– The “baselines” directory is used for storing pretrained model checkpoints and reference baselines.
README Insights
• The README provides detailed instructions on how training and evaluation are conducted – highlighting the roles of multiple GPU devices, actor processes, and the DMC algorithm.
• It explains how the simulation (self-play) interacts with training and evaluation and includes command-line options that outline core configurable parameters.
Key Components
a. Main Components:
• Game Environment Module:
– Represented by the “douzero/env” folder components (env, game, move_detector, move_generator, move_selector, etc.).
• Deep Monte Carlo (DMC) Module:
– Represented by “douzero/dmc” with algorithms, neural network models, utilities, and logging (file_writer) for training.
• Evaluation Module:
– Represented by “douzero/evaluation” containing various agents (random, deep, RLCard-based) and simulation scripts to evaluate performance.
• Training Entry Point:
– The “train.py” script with configuration arguments for controlling GPU allocation, actor process count, unroll length, checkpoint saving, etc.
• Evaluation Entry Point:
– The “evaluate.py” and “generate_eval_data.py” scripts for data generation and self-play evaluation.
• Pre-Trained Models / Baselines:
– The “baselines” folder where pre-trained weights and model checkpoints are stored.
b. Relationships and Interactions:
• Actors (parallel actor processes) are used to simulate self-play games in the environment. These processes feed gameplay data to the training module.
• The trained model (updated via the DMC algorithm) is then used in the evaluation phase by different agents (random, rule-based, deep agents) to test performance.
• There’s a feedback loop where results from evaluation may influence further training or parameter tuning.
• Scripts like “get_most_recent.sh” assist in checkpoint management, indicating interaction with the file system for model persistence.
c. Architectural Patterns and Design Principles:
• The project follows a modular design with clear separation between game simulation (environment), learning (DMC module), and evaluation.
• A multi-process/multi-GPU setup is used to perform self-play simulations concurrently with the training process.
• It leverages classic reinforcement learning loops enhanced by deep learning models.
• The design emphasizes extensibility (different agents, baseline comparisons) and scalability (GPU and actor process configuration).
d. Relevant Technologies and Frameworks:
• Python is the primary language.
• CUDA/GPU support is integral for training, and there are fallbacks to CPU for certain cases.
• Although not explicitly stated, the use of terms like “torchbeast” suggests the possibility of PyTorch or similar frameworks for deep learning.
• Dependency management via pip (with requirements.txt) and installation scripts (setup.py).