# fsoft-ai4code/repohyper architecture

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

- Interactive diagram: https://gitdiagram.com/fsoft-ai4code/repohyper
- Repository: https://github.com/fsoft-ai4code/repohyper

## Overview

##### Main Components

a. Data Preparation/Acquisition:
- Cloning and downloading repositories (using the Repobench dataset).
- Call graph generation from repositories (see scripts/data/download_repos.py and generate_call_graph scripts).

b. Graph Construction & Embedding:
- Construction of Code Property Graphs (repository-level semantic graphs) from the source code.
- Generation of embeddings for nodes and creation of adjacency matrices (scripts/data/repo_to_embeddings.py, src/repo_graph/repo_to_graph.py).

c. Search and Expand Stage:
- Implementation of the KNN search and expansion algorithm on the semantic graph to retrieve and refine context (see scripts/data/matching_repobench_graphs.py and parts of src/repo_graph/search_policy).

d. GNN Link Predictor Training:
- A dedicated training module for the Graph Neural Network (GNN) using various architectures (GraphSage, GCN variants) as seen in src/models/ and training scripts (train_gnn.py, train_gnn_he.py, scripts/trainer/train.py).
- Utilization of configuration via ds_config.json and training orchestration through Slurm scripts in src/train.

e. Evaluation & Integration with LLM:
- Evaluation pipeline that ranks or feeds refined contexts into a Code LLM for next line prediction (scripts/evaluate_llm.py).
- This component verifies the performance of both the graph-based retrieval and the Code LLM integration.

##### Relationships and Data Flow

- Flow to Graph Construction where raw source code is parsed to build Code Property Graphs.
- Feed the refined graphs into the GNN training pipeline for the link prediction task.
- Finally, channel the output from the GNN (retrieved and reranked contexts) into the Code LLM model for code completion.

##### Architectural Patterns & Technologies

- Modular Design: Separate directories for data processing (src/data), evaluation (src/eval), models (src/models), and repository graph construction (src/repo_graph) indicate a modular structure.
- Two-Stage Pipeline: Distinct separation between the retrieval/refinement stage (graph-based techniques) and the completion stage (LLM-based code prediction).
- Distributed/Parallel Processing: Utilization of multi-process calls (e.g., generating call graphs using 60 processes) and deepspeed for GNN model training.
- Use of Graph Neural Networks (GNNs): Different variants (GraphSage, GCN, etc.) are implemented, showing the importance of graph-based learning.
- Integration with modern Code Language Models: The system is designed to work with models such as GPT-3.5.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Preparation/Acquisition
    subgraph "Data Preparation/Acquisition"
        DP1["Download Repos"] 
        DP2["Generate Call Graphs"]
    end

    %% Graph Construction & Embedding
    subgraph "Graph Construction & Embedding"
        GE1["Repo to Embeddings"]
        GE2["Repo to Graph"]
    end

    %% Search and Expand Module
    subgraph "Search & Expand Module"
        SE1["Matching Repobench Graphs"]
        SE2["Search Policy"]
    end

    %% GNN Link Predictor Training
    subgraph "GNN Link Predictor Training"
        GNN1["Train GNN"]
        GNN2["Train GNN HE"]
        GNN3["Trainer"]
        GNN4["GNN Models"]
    end

    %% Evaluation & Integration with LLM
    subgraph "Evaluation & Integration with LLM"
        LL1["Evaluate LLM"]
        LL2["Code LLM"]
    end

    %% Connections between components for vertical data flow
    DP1 --> DP2
    DP2 --> GE1
    GE1 --> GE2
    GE2 --> SE1
    SE1 --> SE2
    SE2 --> GNN1
    GNN1 --> GNN2
    GNN2 --> GNN3
    GNN3 --> GNN4
    GNN4 --> LL1
    LL1 --> LL2

    %% Click Events for Data Preparation/Acquisition
    click DP1 "https://github.com/fsoft-ai4code/repohyper/blob/main/scripts/data/download_repos.py"
    click DP2 "https://github.com/fsoft-ai4code/repohyper/blob/main/scripts/data/generate_call_graphs.py"

    %% Click Events for Graph Construction & Embedding
    click GE1 "https://github.com/fsoft-ai4code/repohyper/blob/main/scripts/data/repo_to_embeddings.py"
    click GE2 "https://github.com/fsoft-ai4code/repohyper/blob/main/src/repo_graph/repo_to_graph.py"

    %% Click Events for Search and Expand Module
    click SE1 "https://github.com/fsoft-ai4code/repohyper/blob/main/scripts/data/matching_repobench_graphs.py"
    click SE2 "https://github.com/fsoft-ai4code/repohyper/tree/main/src/repo_graph/search_policy"

    %% Click Events for GNN Link Predictor Training
    click GNN1 "https://github.com/fsoft-ai4code/repohyper/blob/main/train_gnn.py"
    click GNN2 "https://github.com/fsoft-ai4code/repohyper/blob/main/train_gnn_he.py"
    click GNN3 "https://github.com/fsoft-ai4code/repohyper/blob/main/scripts/trainer/train.py"
    click GNN4 "https://github.com/fsoft-ai4code/repohyper/tree/main/src/models/*"

    %% Click Events for Evaluation & Integration with LLM
    click LL1 "https://github.com/fsoft-ai4code/repohyper/blob/main/scripts/evaluate_llm.py"
    click LL2 "https://github.com/fsoft-ai4code/repohyper/blob/main/src/llm.py"

    %% Define Styles
    classDef dataPrep fill:#bbdefb,stroke:#0d47a1,stroke-width:2px;
    classDef graphEmbed fill:#c8e6c9,stroke:#1b5e20,stroke-width:2px;
    classDef searchExpand fill:#fff9c4,stroke:#f57f17,stroke-width:2px;
    classDef gnn fill:#ffcdd2,stroke:#b71c1c,stroke-width:2px;
    classDef llm fill:#d1c4e9,stroke:#4a148c,stroke-width:2px;
    
    %% Apply Styles 
    class DP1,DP2 dataPrep;
    class GE1,GE2 graphEmbed;
    class SE1,SE2 searchExpand;
    class GNN1,GNN2,GNN3,GNN4 gnn;
    class LL1,LL2 llm;
```
