# hazyresearch/lolcats architecture

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

- Interactive diagram: https://gitdiagram.com/hazyresearch/lolcats
- Repository: https://github.com/hazyresearch/lolcats

## Overview

##### Project Type & Overall Purpose
- LoLCATs is a research-driven project aimed at converting standard Transformer models (like Llama and Mistral) into “linearized” subquadratic LLMs. It does this by two main steps: first training linear attention layers to emulate the softmax attention (Attention Transfer), and then using low-rank adaptation to correct any approximation errors.
- The project has a clear focus on experiments, along with model conversion, fine-tuning, evaluation demos, and GPU-accelerated extensions (via custom CUDA kernels and Flash Attention 2 integrations).

##### And Label Main Components
– Configuration & Experiment Management:
- Configs Directory: Contains experiment configs (datasets, training hyperparameters in configs/experiment) and model configs (initial setup for pretrained models, linear attention architectures in configs/model).
- These files drive every training run and provide a blueprint for setting up experiments.

– C++/CUDA Extensions & Optimizations:
- csrc Directory: Contains C++ and CUDA source code for efficient computation of causal attention kernels. This part is critical since it holds the “causal_attention” implementations and a setup.py for installation.

– Core Python Modules:
- src Directory: Contains subdirectories for dataloaders, model definition/conversion, trainer logic, and utility functions.
⁃ The “src/trainer” modules include scripts for running different phases, such as distill_attention with MSE or cross-entropy losses and fine-tuning routines.
- This layer forms the heart of the attention transfer and low-rank linearizing workflow.

– Entry Points & Orchestration:
- The top-level scripts such as app.py, distill_llama.py, and demo scripts in demos/ serve different purposes:
⁃ app.py might be used to initialize/run a demo or web service.
⁃ distill_llama.py is the core command-line interface that coordinates distillation, low-rank training, and ultimately checkpoint creation.
⁃ Demo scripts (in demos/) and LM Evaluation Harness (in lm_eval_harness/) are used for inferencing and benchmarking.

– External Integration & Deployments:
- Hugging Face integrations are evident; both for model download and possibly as a hosting interface in a Hugging Face Space.
- LM Eval Harness (in lm_eval_harness) is incorporated for evaluation tasks, so include a connection to an external evaluation service.

##### Relationships & Interactions
– Configurations (YAML files) feed parameters into the distillation and fine-tuning scripts.
– The distillation process uses dataloaders (from src/dataloaders) to prepare the dataset (using the Alpaca dataset) and then passes data into model conversion routines (in src/model) that make use of the C++/CUDA optimized kernels (via csrc) to compute “ground-truth” softmax attentions.
– After distillation, the generated checkpoints (attn distillation “_distill” and LoRA fine-tuned “_ft” weights) are used in inference demos (demos/ and demo_lolcats_llm.py) and later evaluated through LM Eval Harness.
– External GPU tools such as Flash Attention 2 or ThunderKittens frameworks are invoked, either via Python wrappers or at the environment setup step.

##### Architectural Patterns & Design Principles
– Modular Design: Code separation is clear (configs, src, csrc, demos, evaluation) supporting a modular architecture where each functionality (data, model, trainer) is compartmentalized.
– Extensibility: The use of YAML config files and clearly separated modules (e.g., separate linear attention variants) indicates support for easy extension to new models or experiments.
– Performance Optimization: Introduction of custom CUDA kernels and integration with external libraries (FlashAttention2, ThunderKittens) denotes a design aimed at high performance.

##### Final Elements
- Each component should have a clear, descriptive label.

Happy diagramming!

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Configuration Management
    subgraph "Configuration Management"
        confExp["Experiment Configs"]:::config
        confModel["Model Configs"]:::config
    end

    %% C++/CUDA Extensions & Optimizations
    subgraph "C++/CUDA Extensions & Optimizations"
        cppSources["C++/CUDA Sources"]:::cpp
    end

    %% Data Pipeline
    subgraph "Data Pipeline"
        dataLoad["Dataset Loaders"]:::data
        dataUtils["Data Utility Scripts"]:::data
    end

    %% Model Conversion & Training Modules
    subgraph "Model Conversion & Training"
        modelConv["Model Conversion & Loading"]:::model
        linearAttn["Linear Attention"]:::model
        modelDefs["Model Definitions"]:::model
        trainer["Trainer Logic"]:::model
        lowRank["Low-Rank Adaptation"]:::model
        cpGen["Checkpoint Generation"]:::model
    end

    %% Entry Points & Evaluation
    subgraph "Entry Points & Demos"
        mainApp["Main Application"]:::entry
        demoEval["Demo & Evaluation"]:::entry
    end

    %% External Integrations
    externalHF["Hugging Face Integration"]:::external

    %% Connections
    confExp -->|"feeds"| dataLoad
    confModel -->|"configures"| modelConv

    dataLoad -->|"supplies data"| modelConv
    dataUtils -->|"supports"| dataLoad

    cppSources -->|"accelerates"| modelConv
    cppSources -->|"supports"| linearAttn

    modelConv -->|"initiates"| trainer
    linearAttn -->|"integrates with"| trainer
    modelDefs -->|"provides models to"| trainer
    lowRank -->|"augments"| trainer

    trainer -->|"produces"| cpGen
    cpGen -->|"drives"| demoEval
    mainApp -->|"orchestrates"| demoEval

    demoEval -->|"utilizes"| externalHF

    %% Click Events
    click confExp "https://github.com/hazyresearch/lolcats/tree/main/configs/experiment/"
    click confModel "https://github.com/hazyresearch/lolcats/tree/main/configs/model/"
    click cppSources "https://github.com/hazyresearch/lolcats/tree/main/csrc/"
    click dataLoad "https://github.com/hazyresearch/lolcats/tree/main/src/dataloaders/"
    click dataUtils "https://github.com/hazyresearch/lolcats/tree/main/src/dataloaders/utils/"
    click modelConv "https://github.com/hazyresearch/lolcats/tree/main/src/model/"
    click linearAttn "https://github.com/hazyresearch/lolcats/tree/main/src/model/linear_attention/"
    click modelDefs "https://github.com/hazyresearch/lolcats/blob/main/src/model/modeling_llama.py"
    click trainer "https://github.com/hazyresearch/lolcats/tree/main/src/trainer/"
    click lowRank "https://github.com/hazyresearch/lolcats/blob/main/src/model/peft.py"
    click mainApp "https://github.com/hazyresearch/lolcats/blob/main/app.py"
    click demoEval "https://github.com/hazyresearch/lolcats/tree/main/demos/"
    click externalHF "https://github.com/hazyresearch/lolcats/blob/main/lm_eval_harness/models_huggingface.py"

    %% Styles
    classDef config fill:#bbdefb,stroke:#1a237e,stroke-width:2px;
    classDef cpp fill:#ffcdd2,stroke:#b71c1c,stroke-width:2px;
    classDef data fill:#c8e6c9,stroke:#1b5e20,stroke-width:2px;
    classDef model fill:#fff9c4,stroke:#f57f17,stroke-width:2px;
    classDef entry fill:#d1c4e9,stroke:#4a148c,stroke-width:2px;
    classDef external fill:#ffe0b2,stroke:#e65100,stroke-width:2px;
```
