# lean-dojo/reprover architecture

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

- Interactive diagram: https://gitdiagram.com/lean-dojo/reprover
- Repository: https://github.com/lean-dojo/reprover

## Overview

- generation – for training and running the tactic generator (both with and without retrieval augmentation)
- retrieval – for training and running premise retrievers (including a BM25 baseline and transformer-based models)
- prover – for evaluating the final theorem proving pipeline (e.g., via best-first search tactics and proof search)
- scripts – containing utility scripts for data conversion, data statistics, downloading data, and integration tasks (e.g., converting checkpoints)

##### Main Components
a. Data & Preprocessing:
– A “common” code base (e.g., common.py) for shared functions.
– Scripts for downloading data and converting checkpoints.
b. Premise Retrieval Module:
– Located in the retrieval directory (with submodules for BM25 and transformer-based retrieval).
– Includes YAML configuration files (in retrieval/confs) to control training and evaluation.
– Tasks: indexing the corpus, training the retriever, and retrieving premises for a given proof state.
c. Tactic Generation Module:
– Found in the generation directory.
– Trains a ByT5 encoder-decoder model to generate tactics.
– Supports both standalone and retrieval-augmented versions (the latter combining retrieved premises with the proof state).
d. Proof Search / Evaluation:
– In the prover directory, where the search algorithms and evaluation functions (e.g., best-first search) integrate with the generation module.
– Interacts with external systems such as Lean (and optionally Lean Copilot) when running the final proof search.
e. Training & Logging Infrastructure:
– Leverages PyTorch Lightning and Weights & Biases (W&B) for logging.
– Uses configuration files for hyperparameter management.

##### Relationships and Interactions
a. The system starts with a proof state as an input.
b. The Retrieval Module (premise retriever) processes this proof state to compute embedding vectors and conduct a nearest neighbor search over a pre-indexed corpus (handled with retrieval/index.py).
c. The retrieved premises are then concatenated with the original proof state to form an enriched input.
d. This enriched input is passed to the Tactic Generation Module (in generation) to produce candidate tactics.
e. Finally, the Prover Module uses these generated tactics in a best-first search to interact with the Lean theorem prover, completing the evaluation loop.
f. During training, scripts, YAML configurations, and checkpoint converters ensure that training, evaluation, and deployment pipelines are seamlessly integrated.

##### Architectural Patterns / Design Principles
– Separation of Concerns: Clearly defined modules for retrieval, generation, and evaluation.
– Pipeline Architecture: Data flows from retrieval to generation to final proof search.
– Configurability: Use of YAML config files for both retrieval and generation allows tuning of hyperparameters.
– Modularity & Extensibility: The design supports both direct tactic generation and retrieval-augmented generation, along with seamless integration with external tools (Lean, W&B, Hugging Face).

##### Relevant Technologies and Frameworks
– Python (with PyTorch, PyTorch Lightning)
– Hugging Face Transformers (for model loading and inference)
– Weights & Biases for logging, along with standard CLI tools in Linux (wget, tar)
– Lean theorem prover integration for final evaluation.
– Additionally, configuration management via YAML files.

→ Proof State to Retrieval Module.
→ Retrieval Module outputs Passed along with Proof State to the Generation Module.
→ Tactic Generation outputs to the Prover Module.
– Label connectors (e.g., “embedding computation”, “concatenation”, “beam search generation”, “best-first search”).
– If desired, show how logging and experiment tracking (W&B) integrate with the training components.

## Diagram (Mermaid)

```mermaid
graph TD
    A["Proof State"]:::common

    subgraph "Supporting Utilities"
        B["Data Preprocessing & Scripts"]:::scripts
        C["Common Code Base"]:::common
    end

    subgraph "Retrieval Pipeline"
        D["Premise Retrieval Module"]:::retrieval
        D1["Retrieval Core"]:::retrieval
        D2["Retrieval Configs"]:::retrieval
    end

    subgraph "Generation Pipeline"
        E["Tactic Generation Module"]:::generation
        E1["Generation Core"]:::generation
        E2["Generation Configs"]:::generation
    end

    subgraph "Evaluation"
        F["Proof Search / Evaluation"]:::prover
        F1["Proof Search Core"]:::prover
    end

    G["External Lean Integration"]:::external

    H["Training & Logging (W&B, PT Lightning)"]:::training

    %% Main Data Flow
    A -->|"embedding_computation"| D
    D -->|"concatenation"| E
    E -->|"generated_tactic"| F
    F -->|"Lean_communication"| G

    %% Utility connections
    B ---|"utilities"| D
    B ---|"utilities"| E
    C ---|"common_functions"| D
    C ---|"common_functions"| E

    %% Training and Logging connections
    D ---|"training_logging"| H
    E ---|"training_logging"| H

    %% Internal Retrieval Module connections
    D --> D1
    D --> D2

    %% Internal Generation Module connections
    E --> E1
    E --> E2

    %% Internal Evaluation Module connection
    F --> F1

    %% Click Events
    click B "https://github.com/lean-dojo/reprover/tree/main/scripts"
    click C "https://github.com/lean-dojo/reprover/blob/main/common.py"
    click D "https://github.com/lean-dojo/reprover/tree/main/retrieval"
    click D1 "https://github.com/lean-dojo/reprover/blob/main/retrieval/index.py"
    click D2 "https://github.com/lean-dojo/reprover/tree/main/retrieval/confs"
    click E "https://github.com/lean-dojo/reprover/tree/main/generation"
    click E1 "https://github.com/lean-dojo/reprover/blob/main/generation/main.py"
    click E2 "https://github.com/lean-dojo/reprover/tree/main/generation/confs"
    click F "https://github.com/lean-dojo/reprover/tree/main/prover"
    click F1 "https://github.com/lean-dojo/reprover/blob/main/prover/evaluate.py"

    %% Styles
    classDef scripts fill:#c9e6ff,stroke:#333,stroke-width:2px;
    classDef retrieval fill:#d8f7d8,stroke:#333,stroke-width:2px;
    classDef generation fill:#ffe6cc,stroke:#333,stroke-width:2px;
    classDef prover fill:#e6ccff,stroke:#333,stroke-width:2px;
    classDef external fill:#ffcccc,stroke:#333,stroke-width:2px;
    classDef common fill:#f2f2f2,stroke:#333,stroke-width:2px;
    classDef training fill:#ffffcc,stroke:#333,stroke-width:2px;
```
