# biegehydra/nanogptdotnet architecture

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

- Interactive diagram: https://gitdiagram.com/biegehydra/nanogptdotnet
- Repository: https://github.com/biegehydra/nanogptdotnet

## Overview

##### Main Components Identification
The project consists of three main components:
- NanoGpt (main implementation)
- BigramModel (simpler language model)
- Shared (common utilities and helpers)

**┌─────────────────┐     ┌─────────────────┐**
- NanoGpt      │     │   BigramModel   │
- ┘     └─────────────────┘
- ▲                       ▲

- ┬─────────────┘

- ┌────────────────┐
- Shared     │
- ┘

##### Shared Module (Base Layer)
**┌─────────────── Shared ──────────────┐**

- ┌─────────────┐   ┌────────────┐  │
- TokenEncoder │   │DataSampler │  │
- ┘   └────────────┘  │

- ┌─────────────┐   ┌────────────┐  │
- Enums     │   │  Helpers   │  │
- ┘   └────────────┘  │
- ┘

##### Model Components
**┌─────────── NanoGpt ──────────┐    ┌────── BigramModel ─────┐**

- ┌──────────────────────┐     │    │ ┌─────────────────┐    │
- Neural Network Model │     │    │ │ Bigram Language │    │
- ┘     │    │ │     Model       │    │
- ┘    │
- ┘    └─────────────────────────┘

C. Runtime Configuration:
A section showing the compute options:
**┌──────── Runtime Configuration ───────┐**

- ┌─────────┐            ┌────────┐  │
- CUDA   │  ←─OR─→   │  CPU   │  │
- ┘            └────────┘  │
- ┘

##### Important Elements

- Blue for core model components (NanoGpt, BigramModel)
- Green for shared utilities
- Orange for runtime configuration

- Data Flow Indicators:
- Input text flow from files to models
- Model output generation
- Shared component usage paths

**┌─────── Training Process ──────┐**
- 1. Data Loading              │
- 2. Token Encoding            │
- 3. Model Training            │
- 4. Loss Calculation          │
- 5. Model Output Generation   │
- ┘

##### The relationship between the two model implementations

Keep the abstraction level focused on the architectural components while showing the clear separation between the main model implementations and their shared dependencies. This will help developers understand both the high-level structure and the specific implementation details of the system.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Models
        NanoGpt["NanoGPT Neural Network"]:::model
        Bigram["Bigram Language Model"]:::model
    end

    subgraph SharedUtils["Shared Utilities"]
        TokenEnc["Token Encoder"]:::shared
        DataSamp["Data Sampler"]:::shared
        Enums["Enums"]:::shared
        Helpers["Helper Functions"]:::shared
    end

    subgraph Runtime["Runtime Configuration"]
        CUDA["CUDA Processing"]:::runtime
        CPU["CPU Processing"]:::runtime
        CUDA --- CPU
    end

    subgraph TrainingProcess["Training Process"]
        direction TB
        Step1["1. Data Loading"]:::process
        Step2["2. Token Encoding"]:::process
        Step3["3. Model Training"]:::process
        Step4["4. Loss Calculation"]:::process
        Step5["5. Model Output"]:::process
        
        Step1 --> Step2
        Step2 --> Step3
        Step3 --> Step4
        Step4 --> Step5
    end

    subgraph InputData["Training Data"]
        NanoInput["NanoGPT Input"]:::data
        BigramInput["Bigram Input"]:::data
    end

    NanoGpt --> TokenEnc
    NanoGpt --> DataSamp
    NanoGpt --> Enums
    NanoGpt --> Helpers
    
    Bigram --> TokenEnc
    Bigram --> DataSamp
    Bigram --> Enums
    Bigram --> Helpers

    NanoInput --> NanoGpt
    BigramInput --> Bigram

    Runtime --> NanoGpt
    Runtime --> Bigram

    classDef model fill:#a8d1f0,stroke:#333,stroke-width:2px
    classDef shared fill:#90EE90,stroke:#333,stroke-width:2px
    classDef runtime fill:#FFA500,stroke:#333,stroke-width:2px
    classDef process fill:#F0F8FF,stroke:#333,stroke-width:2px
    classDef data fill:#FFE4B5,stroke:#333,stroke-width:2px

    click NanoGpt "https://github.com/biegehydra/NanoGptDotnet/blob/master/src/NanoGpt/NanoGpt.cs"
    click Bigram "https://github.com/biegehydra/NanoGptDotnet/blob/master/src/BigramModel/BigramModel.cs"
    click TokenEnc "https://github.com/biegehydra/NanoGptDotnet/blob/master/src/Shared/TokenEncoder.cs"
    click DataSamp "https://github.com/biegehydra/NanoGptDotnet/blob/master/src/Shared/DataSampler.cs"
    click Enums "https://github.com/biegehydra/NanoGptDotnet/blob/master/src/Shared/Enums.cs"
    click Helpers "https://github.com/biegehydra/NanoGptDotnet/blob/master/src/Shared/HelperFunctions.cs"
    click NanoInput "https://github.com/biegehydra/NanoGptDotnet/blob/master/src/NanoGpt/input.txt"
    click BigramInput "https://github.com/biegehydra/NanoGptDotnet/blob/master/src/BigramModel/input.txt"
```
