# nageshram/gptfromscratch architecture

> Architecture diagram of the GitHub repository nageshram/gptfromscratch, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nageshram/gptfromscratch
- Repository: https://github.com/nageshram/gptfromscratch

## Overview

This repository trains a character-level decoder-only GPT on input.txt, evaluates training and validation loss, saves a checkpoint, and can generate text later from that checkpoint. The main path runs from corpus encoding through batched optimization and evaluation to checkpointed inference. The model’s attention, feed-forward, and transformer-block relationships are visible in the excerpts; README statements support the broader training and hardware capabilities. Some training-loop and device-selection details were not sampled, so those connections are documented rather than inferred from unseen code.

Main entry points: [`bigram.py`](https://github.com/nageshram/gptfromscratch/blob/main/bigram.py) (Training and evaluation), [`sample.py`](https://github.com/nageshram/gptfromscratch/blob/main/sample.py) (Checkpoint inference), [`input.txt`](https://github.com/nageshram/gptfromscratch/blob/main/input.txt) (Training corpus)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data_training["Data and training"]
  node_corpus["Training corpus<br/>[input.txt]"]
  node_encoding["Character encoding<br/>[bigram.py]"]
  node_batches["Train and val batches<br/>[bigram.py]"]
  node_training["Training and evaluation<br/>[bigram.py]"]
  node_optimizer["AdamW optimizer<br/>[bigram.py]"]
  node_device["Compute device<br/>[bigram.py]"]
end

subgraph group_transformer["Transformer model"]
  node_model["Language model<br/>[bigram.py]"]
  node_attention["Causal attention<br/>[bigram.py]"]
  node_feedforward["Feed-forward network<br/>[bigram.py]"]
  node_block["Transformer block<br/>[bigram.py]"]
end

subgraph group_generation["Checkpoint and generation"]
  node_checkpoint["Model checkpoint<br/>[bigram.py]"]
  node_inference["Checkpoint inference<br/>[sample.py]"]
  node_generation["Text generation<br/>[bigram.py]"]
  node_generated_text["Generated text"]
end

node_user(("User"))

node_user -->|"starts training"| node_training
node_corpus -->|"provides text"| node_encoding
node_encoding -->|"encodes characters"| node_batches
node_batches -->|"supplies batches"| node_training
node_training -->|"trains and evaluates"| node_model
node_training -->|"updates with"| node_optimizer
node_training -->|"saves weights and metadata"| node_checkpoint
node_model -->|"applies stacked blocks"| node_block
node_block -->|"invokes"| node_attention
node_block -->|"invokes"| node_feedforward
node_training -->|"selects hardware"| node_device
node_user -->|"runs inference"| node_inference
node_inference -->|"loads"| node_checkpoint
node_inference -->|"instantiates and restores"| node_model
node_inference -->|"requests generation"| node_generation
node_generation -->|"calls autoregressively"| node_model
node_generation -->|"decodes characters"| node_generated_text

click node_corpus "https://github.com/nageshram/gptfromscratch/blob/main/input.txt"
click node_encoding "https://github.com/nageshram/gptfromscratch/blob/main/bigram.py"
click node_batches "https://github.com/nageshram/gptfromscratch/blob/main/bigram.py"
click node_training "https://github.com/nageshram/gptfromscratch/blob/main/bigram.py"
click node_optimizer "https://github.com/nageshram/gptfromscratch/blob/main/bigram.py"
click node_model "https://github.com/nageshram/gptfromscratch/blob/main/bigram.py"
click node_attention "https://github.com/nageshram/gptfromscratch/blob/main/bigram.py"
click node_feedforward "https://github.com/nageshram/gptfromscratch/blob/main/bigram.py"
click node_block "https://github.com/nageshram/gptfromscratch/blob/main/bigram.py"
click node_device "https://github.com/nageshram/gptfromscratch/blob/main/bigram.py"
click node_checkpoint "https://github.com/nageshram/gptfromscratch/blob/main/bigram.py"
click node_inference "https://github.com/nageshram/gptfromscratch/blob/main/sample.py"
click node_generation "https://github.com/nageshram/gptfromscratch/blob/main/bigram.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_corpus,node_encoding,node_batches,node_training,node_optimizer,node_device,node_user toneBlue
class node_model,node_attention,node_feedforward,node_block toneAmber
class node_checkpoint,node_inference,node_generation,node_generated_text toneMint
```

## Components

### Data and training

- [Training corpus](https://github.com/nageshram/gptfromscratch/blob/main/input.txt): `input.txt`
- [Character encoding](https://github.com/nageshram/gptfromscratch/blob/main/bigram.py): `bigram.py`
- [Train and val batches](https://github.com/nageshram/gptfromscratch/blob/main/bigram.py): `bigram.py`
- [Training and evaluation](https://github.com/nageshram/gptfromscratch/blob/main/bigram.py): `bigram.py`
- [AdamW optimizer](https://github.com/nageshram/gptfromscratch/blob/main/bigram.py): `bigram.py`
- [Compute device](https://github.com/nageshram/gptfromscratch/blob/main/bigram.py): `bigram.py`

### Transformer model

- [Language model](https://github.com/nageshram/gptfromscratch/blob/main/bigram.py): `bigram.py`
- [Causal attention](https://github.com/nageshram/gptfromscratch/blob/main/bigram.py): `bigram.py`
- [Feed-forward network](https://github.com/nageshram/gptfromscratch/blob/main/bigram.py): `bigram.py`
- [Transformer block](https://github.com/nageshram/gptfromscratch/blob/main/bigram.py): `bigram.py`

### Checkpoint and generation

- [Model checkpoint](https://github.com/nageshram/gptfromscratch/blob/main/bigram.py): `bigram.py`
- [Checkpoint inference](https://github.com/nageshram/gptfromscratch/blob/main/sample.py): `sample.py`
- [Text generation](https://github.com/nageshram/gptfromscratch/blob/main/bigram.py): `bigram.py`
- **Generated text**

### Other components

- **User**

## Connections

- User → Training and evaluation: starts training (evidence: `README.md`)
- Training corpus → Character encoding: provides text (evidence: `bigram.py`)
- Character encoding → Train and val batches: encodes characters (evidence: `README.md`)
- Train and val batches → Training and evaluation: supplies batches (evidence: `README.md`)
- Training and evaluation → Language model: trains and evaluates (evidence: `README.md`)
- Training and evaluation → AdamW optimizer: updates with (evidence: `README.md`)
- Training and evaluation → Model checkpoint: saves weights and metadata (evidence: `bigram.py`)
- Language model → Transformer block: applies stacked blocks (evidence: `bigram.py`)
- Transformer block → Causal attention: invokes (evidence: `bigram.py`)
- Transformer block → Feed-forward network: invokes (evidence: `bigram.py`)
- Training and evaluation → Compute device: selects hardware (evidence: `README.md`)
- User → Checkpoint inference: runs inference (evidence: `README.md`)
- Checkpoint inference → Model checkpoint: loads (evidence: `sample.py`)
- Checkpoint inference → Language model: instantiates and restores (evidence: `sample.py`)
- Checkpoint inference → Text generation: requests generation (evidence: `sample.py`)
- Text generation → Language model: calls autoregressively (evidence: `bigram.py`)
- Text generation → Generated text: decodes characters
