# tascj/kaggle-lmsys-chatbot-arena architecture

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

- Interactive diagram: https://gitdiagram.com/tascj/kaggle-lmsys-chatbot-arena
- Repository: https://github.com/tascj/kaggle-lmsys-chatbot-arena

## Overview

##### Main Components
- Data Preparation & Artifacts:
- External data (CSV files, training/test splits) is intended to be located in a separate “data” directory (outside the repo) and “artifacts” where processed output is stored.
- scripts/prepare_dataset.py kick-starts the preparation stage.
- Multi-Stage Training Pipeline:
- Stage 1: Represented by configs/stage1. This stage contains initial training scripts (m0.py, m1.py, m3.py).
- Pseudo-label Generation: Located in configs/stage1_generate_pseudo_labels. This stage has variants of training scripts (m0_110k.py, m0_130k.py, etc.) that likely generate additional labels or training data adjustments.
- Stage 2: Found in configs/stage2 with its own README and scripts (e.g., m0.py, m3.py).
- Stage 3: Located in configs/stage3 and includes both training and validation scripts (m0.py, m0val.py, m3.py, m3val.py).
- Human Preference Module:
- The human_pref directory handles model optimizers, data collation, dataset preparation, processors, and both training/inference logic specific to human preference predictions.
- It contains inference submodules for models (modeling_gemma2.py, modeling_llama.py), various custom operations for performance optimizations (flash_attention, fused rotary embedding, etc.), and utility functions.
- Orchestration & Execution:
- main.py acts as the entry point to the system (likely coordinating the overall training/inference flow).
- The scripts subdirectory contains additional helper scripts:
▪ scripts/kapenon and scripts/sakaku serve as parts of the training or evaluation workflows.
▪ Scripts for converting checkpoints for submission (prepare_gemma2_for_submission.py, prepare_llama3_for_submission.py).

##### Mapping the Relationships and Interactions
- Data flows from raw CSV files (data) into the dataset preparation script (scripts/prepare_dataset.py).
- Once prepared, training proceeds via a series of stages:
- Stage 1 training (configs/stage1) produces initial model checkpoints.
- Pseudo-label generation (configs/stage1_generate_pseudo_labels) enhances or refines these predictions.
- Stage 2 (configs/stage2) further refines the model training.
- Stage 3 (configs/stage3) finalizes training and validation.
- The human_pref module provides shared components (data pipelines, model optimizations, custom ops) that are integrated throughout these stages.
- After training, conversion scripts under the scripts folder adjust the model checkpoints for inference and submission (pointing to external Kaggle requirements).

##### Important Architectural Patterns and Design Principles
- Pipeline and Modularization:
- The clear segmentation into multiple training stages (stage1, pseudo-label generation, stage2, stage3) shows a staged pipeline design.
- Separation of concerns: Different folders for configuration, human preference specifics, and orchestration (main.py, scripts).
- Reusability and Extensibility:
- The human_pref directory encapsulates data processing and model inference functionalities separately to allow possible reuse or extension.
- Dependency on External Libraries:
- Critical frameworks include PyTorch (via the NVIDIA container), detectron2, transformers, datasets, and flash-attn which are integrated across various components.

##### Final Element Suggestions
- Data ingestion → Training Stages → Inference conversion.
- Optionally, outline an area for external services or hardware dependencies (e.g., GPU configuration and Docker image nvcr.io/nvidia/pytorch:24.04-py3).

## Diagram (Mermaid)

```mermaid
flowchart TD
    Orchestration["Orchestration & Execution (main.py & scripts)"]:::orchestration
    DataPrep["Data Preparation & Artifacts (scripts/prepare_dataset.py)"]:::data
    Stage1["Stage 1 Training (configs/stage1)"]:::training
    Stage1Pseudo["Stage 1 Pseudo-label Generation (configs/stage1_generate_pseudo_labels)"]:::training
    Stage2["Stage 2 Training (configs/stage2)"]:::training
    Stage3["Stage 3 Training (configs/stage3)"]:::training
    InferenceGemma["Gemma2 Conversion (scripts/prepare_gemma2_for_submission.py)"]:::inference
    InferenceLlama["Llama3 Conversion (scripts/prepare_llama3_for_submission.py)"]:::inference
    HumanPref["Human Preference Module (human_pref)"]:::human
    Dependencies["GPU & Libraries\n(A100 SXM 80G, PyTorch, transformers, detectron2, flash-attn)"]:::orchestration

    %% Main pipeline flow
    Orchestration -->|"initiates"| DataPrep
    DataPrep -->|"feeds data"| Stage1
    Stage1 -->|"checkpoint output"| Stage1Pseudo
    Stage1Pseudo -->|"refinement"| Stage2
    Stage2 -->|"further training"| Stage3
    Stage3 -->|"conversion"| InferenceGemma
    Stage3 -->|"conversion"| InferenceLlama

    %% Human Preference integration (dotted connections)
    HumanPref -.->|"integrates with"| Stage1
    HumanPref -.->|"integrates with"| Stage1Pseudo
    HumanPref -.->|"integrates with"| Stage2
    HumanPref -.->|"integrates with"| Stage3

    %% Orchestration links to Human Preference and External Dependencies
    Orchestration -->|"coordinates"| HumanPref
    Orchestration ---|"utilizes"| Dependencies

    %% Click Events
    click DataPrep "https://github.com/tascj/kaggle-lmsys-chatbot-arena/blob/main/scripts/prepare_dataset.py"
    click Stage1 "https://github.com/tascj/kaggle-lmsys-chatbot-arena/tree/main/configs/stage1"
    click Stage1Pseudo "https://github.com/tascj/kaggle-lmsys-chatbot-arena/tree/main/configs/stage1_generate_pseudo_labels"
    click Stage2 "https://github.com/tascj/kaggle-lmsys-chatbot-arena/tree/main/configs/stage2"
    click Stage3 "https://github.com/tascj/kaggle-lmsys-chatbot-arena/tree/main/configs/stage3"
    click HumanPref "https://github.com/tascj/kaggle-lmsys-chatbot-arena/tree/main/human_pref"
    click Orchestration "https://github.com/tascj/kaggle-lmsys-chatbot-arena/blob/main/main.py"
    click InferenceGemma "https://github.com/tascj/kaggle-lmsys-chatbot-arena/blob/main/scripts/prepare_gemma2_for_submission.py"
    click InferenceLlama "https://github.com/tascj/kaggle-lmsys-chatbot-arena/blob/main/scripts/prepare_llama3_for_submission.py"

    %% Styles
    classDef data fill:#FEEBC8,stroke:#DD6B20,stroke-width:2px;
    classDef training fill:#A1D99B,stroke:#2F855A,stroke-width:2px;
    classDef human fill:#FEBA72,stroke:#D69E2E,stroke-width:2px;
    classDef orchestration fill:#BFD4FF,stroke:#2B6CB0,stroke-width:2px;
    classDef inference fill:#FBB6CE,stroke:#C53030,stroke-width:2px;
```
