# kwai/douzero architecture

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

- Interactive diagram: https://gitdiagram.com/kwai/douzero
- Repository: https://github.com/kwai/douzero

## Overview

##### 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).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Configuration
    Config["Configurations/CLI Args"]:::config

    %% Simulation Layer
    subgraph "Simulation Layer"
        SimActors["Self-Play Actors"]:::simulation
        Env["Game Environment Module"]:::simulation
    end

    %% Training Layer
    subgraph "Training Layer"
        TrainCtrl["Training Controller (train.py)"]:::training
        DMC["Deep Monte Carlo Module"]:::training
    end

    %% Evaluation Layer
    subgraph "Evaluation Layer"
        EvalModule["Evaluation Module (douzero/evaluation)"]:::evaluation
        EvalScript1["evaluate.py"]:::evaluation
        EvalScript2["generate_eval_data.py"]:::evaluation
    end

    %% Model Storage Layer
    subgraph "Model Storage"
        Baselines["Pre-Trained Models/Baselines"]:::storage
        Checkpoint["Checkpoint Utility (get_most_recent.sh)"]:::storage
    end

    %% Connections
    Config -->|"configures"| TrainCtrl
    Config -->|"configures"| EvalScript1
    Config -->|"configures"| EvalScript2

    SimActors -->|"simulates games"| Env
    SimActors -->|"sends experience data"| TrainCtrl

    TrainCtrl -->|"executes training"| DMC
    DMC -->|"returns policy updates"| TrainCtrl

    TrainCtrl -->|"sends updated model"| EvalModule
    Baselines -->|"supplies pretrained models"| EvalModule
    Checkpoint -->|"manages checkpoints"| TrainCtrl

    EvalScript1 -->|"initiates evaluation"| EvalModule
    EvalScript2 -->|"generates evaluation data"| EvalModule

    %% Click Events
    click Env "https://github.com/kwai/douzero/tree/main/douzero/env"
    click DMC "https://github.com/kwai/douzero/tree/main/douzero/dmc"
    click EvalModule "https://github.com/kwai/douzero/tree/main/douzero/evaluation"
    click TrainCtrl "https://github.com/kwai/douzero/blob/main/train.py"
    click EvalScript1 "https://github.com/kwai/douzero/blob/main/evaluate.py"
    click EvalScript2 "https://github.com/kwai/douzero/blob/main/generate_eval_data.py"
    click Baselines "https://github.com/kwai/douzero/tree/main/baselines"
    click Checkpoint "https://github.com/kwai/douzero/blob/main/get_most_recent.sh"

    %% Styles
    classDef config fill:#FFEC99,stroke:#cc9900,stroke-width:2px;
    classDef simulation fill:#C6F7C3,stroke:#2E8B57,stroke-width:2px;
    classDef training fill:#C3DDF7,stroke:#1E90FF,stroke-width:2px;
    classDef evaluation fill:#F7C3DDF,stroke:#C71585,stroke-width:2px;
    classDef storage fill:#FFF2CC,stroke:#DAA520,stroke-width:2px;
```
