# yueliu1999/guardreasoner architecture

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

- Interactive diagram: https://gitdiagram.com/yueliu1999/guardreasoner
- Repository: https://github.com/yueliu1999/guardreasoner

## Overview

##### Main Components Identification
- Data Storage & Organization:
- Benchmark and test data directories: “data/benchmark” and “data/test” harbor evaluation samples (for various safety tests such as SimpleSafetyTests, AegisSafetyTest, HarmBenchResponse, etc.) organized by model size (e.g., 1B, 3B, 8B).
- Training Pipeline:
- Scripts and tools in the “train” folder include shell scripts (e.g., “HS-DPO_1b.sh”, “R-SFT_8b.sh”), Python modules (e.g., “llm.py”, “chat_template.py”), and data preparation/synthesis scripts.
- This module handles both the reasoning SFT (Supervised Fine-Tuning) and hard sample DPO (Direct Preference Optimization) training methods.
- Evaluation Module:
- Standalone evaluation script “evaluate.py” which tests the safety performance across 13 benchmarks.
- Generation & Deployment:
- “generate.py” uses vLLM (or similar) to generate model responses.
- “deploy.py” enables the live or simulated deployment of the guard model.
- External Integration:
- Dependencies and inspirations include works from “LLaMA Factory”, “WildGuard”, and various LLaMA model releases, suggesting a reliance on pre-trained foundation models.

##### Relationships and Interactions
- Data Flow:
- Benchmark/test data serve as the input for both training and evaluation.
- The training pipeline ingests the GuardReasonerTrain dataset (127K samples) and uses it to fine-tune models.
- The output models (across scales: 1B, 3B, 8B) are then fed into the evaluation and generation/deployment modules.
- Pipeline Integration:
- Training scripts (in “train”) interact with data preparation and hard sample modules; once models are trained, they are evaluated using “evaluate.py”.
- “generate.py” interacts with the inference engine (likely leveraging vLLM) to produce model outputs, which are then optionally deployed via “deploy.py”.
- Architectural Flow:
- Clear sequential flow: Data Collection & Preparation -> Model Training (using both reasoning SFT and hard sample DPO) -> Model Evaluation -> Inference/Generation -> Deployment.

##### Architectural Patterns and Principles
- Modular Design:
- Each functional component (data, training, evaluation, generation, deployment) is separated into its own unit, making it easier to manage and evolve.
- Pipeline Architecture:
- The training and evaluation routines adhere to a pipeline model where data moves sequentially through predefined processing stages.
- Scalability:
- The multiple model sizes (1B, 3B, 8B) indicate that the system should be designed to handle different scales, possibly in parallel or using parameterized workflows.
- Extensibility:
- The organization of benchmarks and the variety of safety tests suggest that new tests or training strategies can be integrated with minimal disruption.

##### Technologies, Frameworks, or Libraries
- Python-based execution (e.g., “evaluate.py”, “generate.py”, “deploy.py”).
- Shell scripts for orchestrating training (e.g., “HS-DPO_x.sh”, “R-SFT_x.sh”).
- Likely integration with vLLM for generation/inference.
- Pre-trained models from LLaMA variants.
- JSON and JSONL for benchmarks and prediction outputs.

##### Detailed Elements
- Each module should include:
- Component name
- The main functionality (e.g., “train/prepare_data_rsft.py – preprocesses data for SFT”)
- Benchmark/test data locations and their role in evaluation.
- In the “Training Pipeline,” show the multiple training methods (R-SFT vs. HS-DPO) as sub-components.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Management
    subgraph "Data Management"
        Bench["Benchmark Data"]:::storage
        TestData["Test Data (1B/3B/8B)"]:::storage
    end

    %% Training Pipeline
    subgraph "Training Pipeline"
        %% Shell Scripts Subgroup
        subgraph "Shell Scripts"
            HS1["HS-DPO_1b.sh"]:::script
            HS3["HS-DPO_3b.sh"]:::script
            HS8["HS-DPO_8b.sh"]:::script
            RS1["R-SFT_1b.sh"]:::script
            RS1d["R-SFT_1b_diversity.sh"]:::script
            RS3["R-SFT_3b.sh"]:::script
            RS3d["R-SFT_3b_diversity.sh"]:::script
            RS8["R-SFT_8b.sh"]:::script
            RS8d["R-SFT_8b_diversity.sh"]:::script
        end
        %% Python Modules Subgroup
        subgraph "Python Modules"
            LLM["llm.py"]:::python
            CT["chat_template.py"]:::python
            Prep["prepare_data_rsft.py"]:::python
            RDS["reasoning_data_synthesis.py"]:::python
            HSM["hard_sample_mining.py"]:::python
            MHS["merge_hard_sample.py"]:::python
            NG["n_generate.py"]:::python
        end
        TP["Trained Models"]:::training
    end

    %% Evaluation Module
    subgraph "Evaluation Module"
        Eval["evaluate.py"]:::script
    end

    %% Generation Module
    subgraph "Generation Module"
        Gen["generate.py"]:::script
    end

    %% Deployment Module
    subgraph "Deployment Module"
        Dep["deploy.py"]:::script
    end

    %% Assets & Documentation
    subgraph "Assets & Documentation"
        Assets["Assets"]:::assets
        Doc["README.md"]:::doc
        TrainDoc["train/README.md"]:::doc
    end

    %% External Dependencies
    subgraph "External Dependencies"
        Ext["Pre-trained Models (LLaMA,WildGuard,etc.)"]:::external
    end

    %% Relationships
    Bench -->|"provides_data"| TP
    TestData -->|"provides_data"| TP
    Ext -->|"informs_training"| TP

    %% Connect Shell Scripts to Trained Models
    HS1 --> TP
    HS3 --> TP
    HS8 --> TP
    RS1 --> TP
    RS1d --> TP
    RS3 --> TP
    RS3d --> TP
    RS8 --> TP
    RS8d --> TP

    %% Connect Python Modules to Trained Models
    LLM --> TP
    CT --> TP
    Prep --> TP
    RDS --> TP
    HSM --> TP
    MHS --> TP
    NG --> TP

    %% Flow from Training to Evaluation to Generation to Deployment
    TP -->|"model_artifacts"| Eval
    Eval -->|"evaluation_results"| Gen
    Gen -->|"deployment_ready"| Dep

    %% Click Events
    click Bench "https://github.com/yueliu1999/guardreasoner/tree/main/data/benchmark/"
    click TestData "https://github.com/yueliu1999/guardreasoner/tree/main/data/test/"
  
    click HS1 "https://github.com/yueliu1999/guardreasoner/blob/main/train/HS-DPO_1b.sh"
    click HS3 "https://github.com/yueliu1999/guardreasoner/blob/main/train/HS-DPO_3b.sh"
    click HS8 "https://github.com/yueliu1999/guardreasoner/blob/main/train/HS-DPO_8b.sh"
    click RS1 "https://github.com/yueliu1999/guardreasoner/blob/main/train/R-SFT_1b.sh"
    click RS1d "https://github.com/yueliu1999/guardreasoner/blob/main/train/R-SFT_1b_diversity.sh"
    click RS3 "https://github.com/yueliu1999/guardreasoner/blob/main/train/R-SFT_3b.sh"
    click RS3d "https://github.com/yueliu1999/guardreasoner/blob/main/train/R-SFT_3b_diversity.sh"
    click RS8 "https://github.com/yueliu1999/guardreasoner/blob/main/train/R-SFT_8b.sh"
    click RS8d "https://github.com/yueliu1999/guardreasoner/blob/main/train/R-SFT_8b_diversity.sh"
  
    click LLM "https://github.com/yueliu1999/guardreasoner/blob/main/train/llm.py"
    click CT "https://github.com/yueliu1999/guardreasoner/blob/main/train/chat_template.py"
    click Prep "https://github.com/yueliu1999/guardreasoner/blob/main/train/prepare_data_rsft.py"
    click RDS "https://github.com/yueliu1999/guardreasoner/blob/main/train/reasoning_data_synthesis.py"
    click HSM "https://github.com/yueliu1999/guardreasoner/blob/main/train/hard_sample_mining.py"
    click MHS "https://github.com/yueliu1999/guardreasoner/blob/main/train/merge_hard_sample.py"
    click NG "https://github.com/yueliu1999/guardreasoner/blob/main/train/n_generate.py"
  
    click Eval "https://github.com/yueliu1999/guardreasoner/blob/main/evaluate.py"
    click Gen "https://github.com/yueliu1999/guardreasoner/blob/main/generate.py"
    click Dep "https://github.com/yueliu1999/guardreasoner/blob/main/deploy.py"
  
    click Assets "https://github.com/yueliu1999/guardreasoner/tree/main/assets/"
    click Doc "https://github.com/yueliu1999/guardreasoner/blob/main/README.md"
    click TrainDoc "https://github.com/yueliu1999/guardreasoner/blob/main/train/README.md"

    %% Styles
    classDef storage fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef script fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef python fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef training fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef external fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
    classDef assets fill:#e2e3e5,stroke:#6c757d,stroke-width:2px;
    classDef doc fill:#fefefe,stroke:#818182,stroke-width:2px;
```
