# cxcscmu/craw4llm architecture

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

- Interactive diagram: https://gitdiagram.com/cxcscmu/craw4llm
- Repository: https://github.com/cxcscmu/craw4llm

## Overview

##### Project type and purpose
- Craw4LLM is an open‑source Python tool designed for efficient web crawling of the ClueWeb22 dataset to produce document collections for LLM pretraining. It provides multiple crawling strategies (e.g., DCLM‑fastText scoring, random, in‑degree) and supports downstream fetching of raw document text.

##### File structure
- Top‑level CLI entry scripts:
- crawl.py       – orchestrates a crawl run
- fetch_docs.py  – retrieves document texts after crawling
- access_data.py – ad hoc inspection of ClueWeb22 docs
- Core modules:
- crawler.py          – implements the crawler engine, queue management, visited set
- corpus_interface.py – abstracts access to the ClueWeb22 content store
- document_rater.py   – defines rating methods (length, fastText_score, etc.)
- normalizer.py       – cleans/normalizes URLs or document representations
- utils.py            – shared utilities (e.g., file I/O, YAML loader)
- wandb_logger.py     – wraps Weights & Biases logging
- Other scripts:
- crawl.py calls into crawler.py, document_rater.py, wandb_logger.py
- fetch_docs.py uses corpus_interface.py & utils.py
- Config folder (user‑created) “configs/” holds YAML files specifying:
- Paths (cw22_root_path, output_dir)
- Crawler parameters (seed_docs_file, num_workers, selection_method, rating_methods)
- Data files:
- seed.txt          – initial seed document IDs
- seed/ output_dir  – crawl results: lists of document IDs per iteration

##### README for additional insights
- Emphasis on placing ClueWeb22 on SSD for performance
- wandb integration for logging metrics per iteration
- Instructions for multiple crawling strategies (baseline random, indegree)
- Post‑crawl step to fetch raw texts for downstream LLM pretraining via DCLM framework

1. CLI Layer
- crawl.py
- fetch_docs.py
- access_data.py
2. Config Loader
- YAML parser (utils.py) reads “configs/*.yaml”
3. Crawler Controller
- CrawlerEngine (crawler.py)
- QueueManager & VisitedSet
4. Rating Engine
- DocumentRater (document_rater.py)
- Scoring methods: length, fasttext_score, inlink_count, random
5. Corpus Interface
- ClueWeb22 Data Store (on SSD)
- corpus_interface.py
6. Persistence & I/O
- Output: crawl_results/ (lists of doc IDs per iteration)
- State checkpoint (queue & visited set if enabled)
7. Fetch Module
- fetch_docs.py reads IDs + corpus_interface → raw texts
8. Logging & Monitoring
- wandb_logger.py → Weights & Biases
9. External Dependencies
- fastText models (fasttext_scorers/)
- Python libraries: numpy, tqdm, pyyaml, wandb

- CLI “crawl.py” → Config Loader → Crawler Controller
- Crawler Controller ⇄ QueueManager & VisitedSet (pop / push doc IDs)
- Crawler Controller → Corpus Interface → read seed docs / outlinks
- Documents fetched from Corpus → Rating Engine → scores
- Rated docs → Controller → selection_method → new docs enqueued
- Controller → Persistence → write crawl_results/ & optional state file
- Controller → Logging → wandb_logger → wandb backend
- CLI “fetch_docs.py” → reads crawl_results/ → fetch via Corpus Interface → writes raw texts

c. Architectural Patterns & Principles
- Modular separation: CLI / Controller / Rating / I/O
- Pluggable strategies: rating methods and selection_method via configuration
- Producer‑consumer model: multi‑worker document fetching & scoring
- Checkpointing for fault tolerance (save_state_every)
- Externalized config (YAML) for reproducibility

d. Technologies & Frameworks to annotate
- Python 3.10+
- fastText (HuggingFace models)
- Weights & Biases (wandb)
- ClueWeb22 dataset (SSD)

##### Detailed Capture of Essential Elements
- Break out the “Corpus Interface” and “Rating Engine” as separate components, not lumped into one
- Worker pool (num_workers) inside CrawlerEngine for concurrency

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "CLI Layer"
        CrawlCLI["crawl.py"]:::module
        FetchCLI["fetch_docs.py"]:::module
        AccessCLI["access_data.py"]:::module
    end

    subgraph "Config Loader"
        ConfigLoader["YAML Parser\n(utils.py)"]:::module
    end

    subgraph "Crawler Controller"
        Controller["CrawlerEngine\n(crawler.py)\nWorker Pool\nQueueManager\nVisitedSet"]:::core
    end

    subgraph "Rating Engine"
        Rater["DocumentRater\n(document_rater.py)\n[length, fastText_score,\ninlink_count, random]"]:::core
    end

    subgraph "Corpus Interface"
        Corpus["ClueWeb22 Data Store\n(corpus_interface.py)"]:::data
    end

    subgraph "Persistence & I/O"
        Results["crawl_results/\n(seed.txt output)"]:::data
    end

    subgraph "Logging & Monitoring"
        Logger["wandb_logger.py\n(Weights & Biases)"]:::external
    end

    subgraph "External Dependencies"
        FastText["fastText models"]:::external
        PyLibs["Python libs\n(numpy, tqdm,\npyyaml, wandb)"]:::external
    end

    CrawlCLI -->|load config| ConfigLoader
    ConfigLoader -->|initialize| Controller
    Controller -->|fetch seed IDs| Corpus
    Corpus -->|return docs & outlinks| Controller
    Controller -->|send docs| Rater
    Rater -->|scores| Controller
    Controller -->|enqueue selected| Controller
    Controller -->|write results| Results
    Controller -->|log metrics| Logger

    FetchCLI -->|read IDs| Results
    FetchCLI -->|fetch texts| Corpus
    FetchCLI -->|write raw texts| Results

    AccessCLI -->|inspect docs| Corpus

    classDef module fill:#ccf,stroke:#000,stroke-width:1px
    classDef core fill:#cfc,stroke:#000,stroke-width:1px
    classDef data fill:#9cf,stroke:#000,stroke-width:1px
    classDef external fill:#fdd,stroke:#000,stroke-width:1px

    click CrawlCLI "https://github.com/cxcscmu/craw4llm/blob/main/crawl.py"
    click FetchCLI "https://github.com/cxcscmu/craw4llm/blob/main/fetch_docs.py"
    click AccessCLI "https://github.com/cxcscmu/craw4llm/blob/main/access_data.py"
    click ConfigLoader "https://github.com/cxcscmu/craw4llm/blob/main/utils.py"
    click Controller "https://github.com/cxcscmu/craw4llm/blob/main/crawler.py"
    click Rater "https://github.com/cxcscmu/craw4llm/blob/main/document_rater.py"
    click Corpus "https://github.com/cxcscmu/craw4llm/blob/main/corpus_interface.py"
    click Logger "https://github.com/cxcscmu/craw4llm/blob/main/wandb_logger.py"
    click Results "https://github.com/cxcscmu/craw4llm/blob/main/seed.txt"
```
