# ml-gsai/llada architecture

> Architecture diagram of the GitHub repository ml-gsai/llada, 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/ml-gsai/llada
- Repository: https://github.com/ml-gsai/llada

## Overview

##### Project Type & Purpose
- The project implements a diffusion-based language model called LLaDA, designed to explore masked language modeling using diffusion techniques. It supports different modes of inference including multi-turn chat (via chat.py), a Gradio-based demo (via app.py), and evaluation through likelihood estimation and generation.
- In essence, it serves as both an inference framework (demonstrating interactive conversation and demo interfaces) and an evaluation suite for large language diffusion models.

##### Analyzing the File Structure
- Top-level files:
– README.md and GUIDELINES.md contain essential documentation and theoretical guidelines.
– LICENSE indicates open-source usage.
– Python scripts (app.py, chat.py, generate.py, get_log_likelihood.py) implement the main functionality.
- Subdirectory "evaluation":
– Contains scripts (eval.sh, eval_llada.py) to run evaluation metrics using libraries like lm-evaluation-harness.
- Subdirectory "visualization":
– Offers additional tools (e.g., visualization_paper.py, html_to_png.py) to generate visual outputs and supplementary content.
- The overall structure suggests a modular design that differentiates between interactive inference, evaluation, and visualization.

##### Main System Components & Their Relationships
- Core Model Integration:
- This is where the LLaDA model is loaded using Hugging Face’s transformers library. Components such as get_log_likelihood.py and generate.py provide functions for conditional likelihood evaluation and generation.
- Inference Modules:
- chat.py: Enables multi-turn conversational interactions with the model.
- app.py: Implements a Gradio demo for a user-friendly frontend.
- Evaluation Suite:
- Located in the evaluation folder, these scripts (eval.sh, eval_llada.py) orchestrate conditional likelihood and generation evaluations using external libraries (e.g., lm-evaluation-harness).
- Visualization Tools:
- The visualization directory comprises scripts to generate graphical representations (e.g., converting HTML to PNG, drawing process flows) and might be used to illustrate how the diffusion process works or to display sampling results.
- Documentation & Guidelines:
- GUIDELINES.md and the README provide theoretical context, training tips, and usage instructions, which are critical for understanding the system’s design and functionality.

- Processing Layer: Enclose core model logic and evaluation scripts.
- Visualization & Reporting: Include modules that generate visual outputs.
- External Dependencies: Display libraries and external datasets/repositories (e.g., Hugging Face, lm-evaluation-harness).
- Key Elements to Include:
- Clear labels on each component such as “Chat Interface (chat.py)”, “Gradio Demo (app.py)”, “Core Model & Generation (get_log_likelihood.py, generate.py)”, “Evaluation Engine (evaluation/)”, and “Visualization Tools (visualization/)”.
- Rectangles for main modules.
- Ellipses for external dependencies and libraries.

##### Summary
- Components:
- User Interfaces: Chat interface (chat.py) and Gradio demo (app.py)
- Core Model Functions: get_log_likelihood.py and generate.py using Hugging Face Transformers.
- Evaluation Modules: Scripts and shell commands in the evaluation directory.
- Visualization Tools: Scripts in the visualization folder, complementing the experimental results with graphical outputs.
- Documentation & Guidelines: References to GUIDELINES.md and the README for context.
- Data Flow:
- User submits input → Input processed by Inference Modules (chat/app) → Forwarded to the Core Model → Model produces outputs → Outputs are evaluated (via evaluation scripts) → Optionally visualized (via visualization tools) → Feedback returns to the user.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interface / Inference Layer
    subgraph "User Interface / Inference Layer"
        UIChat["Chat Interface (chat.py)"]
        UIGradio["Gradio Demo (app.py)"]
    end

    %% Aggregator for User Input
    UserInput["User Input"]

    %% Core Model Functions Layer
    subgraph "Core Model Functions"
        Generation["Generation (generate.py)"]
        LogLikelihood["Conditional Likelihood (get_log_likelihood.py)"]
    end

    %% Aggregator for Model Output
    ModelOutput["Model Output"]

    %% Evaluation Engine Layer
    subgraph "Evaluation Engine"
        EvalShell["Evaluation Script (evaluation/eval.sh)"]
        EvalLLada["Evaluation Script (evaluation/eval_llada.py)"]
    end

    %% Visualization & Reporting Layer
    subgraph "Visualization & Reporting"
        Visualization["Visualization Tools (visualization)"]
    end

    %% Documentation Layer
    subgraph "Documentation & Guidelines"
        Readme["Documentation (README.md)"]
        Guidelines["Guidelines (GUIDELINES.md)"]
    end

    %% Connections: User Interface to User Input
    UIChat -->|"UserQuery"| UserInput
    UIGradio -->|"UserQuery"| UserInput

    %% User Input to Core Model Functions
    UserInput -->|"Process"| Generation
    UserInput -->|"Process"| LogLikelihood

    %% Core Model Functions to Model Output
    Generation -->|"Output"| ModelOutput
    LogLikelihood -->|"Output"| ModelOutput

    %% Model Output to Evaluation Engine and Visualization
    ModelOutput -->|"Evaluate"| EvalShell
    ModelOutput -->|"Evaluate"| EvalLLada
    ModelOutput -->|"Visualize"| Visualization

    %% Cross-link: Core Model references Documentation
    Generation ---|"Ref"| Readme
    LogLikelihood ---|"Ref"| Guidelines

    %% Click Events
    click UIChat "https://github.com/ml-gsai/llada/blob/main/chat.py"
    click UIGradio "https://github.com/ml-gsai/llada/blob/main/app.py"
    click Generation "https://github.com/ml-gsai/llada/blob/main/generate.py"
    click LogLikelihood "https://github.com/ml-gsai/llada/blob/main/get_log_likelihood.py"
    click EvalShell "https://github.com/ml-gsai/llada/blob/main/evaluation/eval.sh"
    click EvalLLada "https://github.com/ml-gsai/llada/blob/main/evaluation/eval_llada.py"
    click Visualization "https://github.com/ml-gsai/llada/tree/main/visualization/"
    click Readme "https://github.com/ml-gsai/llada/blob/main/README.md"
    click Guidelines "https://github.com/ml-gsai/llada/blob/main/GUIDELINES.md"

    %% Styles
    classDef ui fill:#F4A261,stroke:#E76F51,stroke-width:2px;
    classDef core fill:#2A9D8F,stroke:#264653,stroke-width:2px;
    classDef eval fill:#E9C46A,stroke:#F4A261,stroke-width:2px;
    classDef viz fill:#E76F51,stroke:#264653,stroke-width:2px;
    classDef docs fill:#A8DADC,stroke:#457B9D,stroke-width:2px;

    %% Apply Classes
    class UIChat,UIGradio,UserInput ui;
    class Generation,LogLikelihood,ModelOutput core;
    class EvalShell,EvalLLada eval;
    class Visualization viz;
    class Readme,Guidelines docs;
```
