# leelachesszero/lc0 architecture

> Architecture diagram of the GitHub repository leelachesszero/lc0, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/leelachesszero/lc0
- Repository: https://github.com/leelachesszero/lc0

## Overview

##### Main Components

a) Core Components:
- Chess Engine Core (src/chess/)
- Neural Network System (src/neural/)
- Search System (src/search/)
- Self-play Component (src/selfplay/)
- UCI Interface Layer (src/chess/uciloop)

b) Neural Network Backends:
- CUDA Backend
- OpenCL Backend
- BLAS Backend
- DirectX Backend
- Metal Backend
- ONNX Backend
- XLA Backend

**┌─────────────────────────────────────────────┐**
- UCI Interface                │
- ┤
- Engine Controller              │
- ┬───────────────┬────────────┤
- Chess Logic  │ Search System │ Self-play  │
- ┴───────────────┴────────────┤
- Neural Network Factory            │
- ┤
- Backend Multiplexer              │
- ┬───┬───┬───┬───┬───┬───┬───────────────┤
- CUD│OCL│BLA│DX │MET│ONN│XLA│  More Backs   │
- ┴───┴───┴───┴───┴───┴───┴───────────────┘

##### Component Relationships

- UCI Interface ↔ Engine Controller
- Engine Controller → Chess Logic
- Engine Controller → Search System
- Search System ↔ Neural Network Factory
- Neural Network Factory → Backend Multiplexer
- Backend Multiplexer → Individual Backends

##### Additional Elements

- Configuration System
- Weight Loading System
- Cache System
- Syzygy Endgame Tables Integration

##### Implementation Details

Classes/modules:
- Board representation
- Evaluation
- Move generation
- Neural network architecture

##### The training data generation pipeline

This architecture should highlight Lc0's unique characteristics as a neural network-based chess engine while showing its modular and extensible nature.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Interface
        UCI[UCI Interface]
        Config[Configuration System]
    end

    subgraph Core
        Engine[Engine Controller]
        Chess[Chess Logic]
        Search[Search System]
        SelfPlay[Self-play Component]
        Training[Training Data System]
    end

    subgraph "Neural Network Layer"
        NNFactory[Neural Network Factory]
        BackendMux[Backend Multiplexer]
    end

    subgraph "Hardware Backends"
        CUDA[CUDA Backend]
        OCL[OpenCL Backend]
        BLAS[BLAS Backend]
        DX[DirectX Backend]
        Metal[Metal Backend]
        ONNX[ONNX Backend]
        XLA[XLA Backend]
    end

    subgraph "Additional Components"
        Syzygy[Syzygy Tables]
    end

    %% Relationships
    UCI --> Engine
    Config --> Engine
    Engine --> Chess
    Engine --> Search
    Engine --> SelfPlay
    Search <--> NNFactory
    SelfPlay --> Training
    Chess --> Syzygy
    NNFactory --> BackendMux
    BackendMux --> CUDA & OCL & BLAS & DX & Metal & ONNX & XLA

    %% Click events
    click UCI "https://github.com/leelachesszero/lc0/blob/master/src/chess/uciloop.h"
    click Chess "https://github.com/leelachesszero/lc0/tree/master/src/chess/"
    click Search "https://github.com/leelachesszero/lc0/tree/master/src/search/"
    click SelfPlay "https://github.com/leelachesszero/lc0/tree/master/src/selfplay/"
    click Training "https://github.com/leelachesszero/lc0/tree/master/src/trainingdata/"
    click NNFactory "https://github.com/leelachesszero/lc0/blob/master/src/neural/factory.h"
    click BackendMux "https://github.com/leelachesszero/lc0/blob/master/src/neural/backends/network_demux.cc"
    click CUDA "https://github.com/leelachesszero/lc0/tree/master/src/neural/backends/cuda/"
    click OCL "https://github.com/leelachesszero/lc0/tree/master/src/neural/backends/opencl/"
    click BLAS "https://github.com/leelachesszero/lc0/tree/master/src/neural/backends/blas/"
    click DX "https://github.com/leelachesszero/lc0/tree/master/src/neural/backends/dx/"
    click Metal "https://github.com/leelachesszero/lc0/tree/master/src/neural/backends/metal/"
    click ONNX "https://github.com/leelachesszero/lc0/tree/master/src/neural/onnx/"
    click XLA "https://github.com/leelachesszero/lc0/tree/master/src/neural/backends/xla/"
    click Config "https://github.com/leelachesszero/lc0/blob/master/src/utils/configfile.h"
    click Syzygy "https://github.com/leelachesszero/lc0/tree/master/src/syzygy/"

    %% Styling
    classDef interface fill:#a8d1ff
    classDef core fill:#90EE90
    classDef neural fill:#FFE5B4
    classDef backend fill:#E6E6FA
    classDef additional fill:#D3D3D3

    class UCI,Config interface
    class Engine,Chess,Search,SelfPlay,Training core
    class NNFactory,BackendMux neural
    class CUDA,OCL,BLAS,DX,Metal,ONNX,XLA backend
    class Syzygy additional
```
