# rcurtis14/microgpt architecture

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

- Interactive diagram: https://gitdiagram.com/rcurtis14/microgpt
- Repository: https://github.com/rcurtis14/microgpt

## Overview

microgpt is a from-scratch, character-level GPT implemented in NumPy. Its main workflow turns Tiny Shakespeare text into character IDs and shifted training batches, then trains embeddings, causal attention, and a transformer block to predict the next character. The inference path loads saved weights and samples characters autoregressively. The graph preserves the documented data-download capability; edges are limited to relationships shown in excerpts, the SOURCE INDEX, or README. Unsampled implementation details are not inferred.

Main entry points: [`train.py`](https://github.com/rcurtis14/microgpt/blob/main/train.py) (Training Loop), [`generate.py`](https://github.com/rcurtis14/microgpt/blob/main/generate.py) (Sampling Loop), [`tokenizer.py`](https://github.com/rcurtis14/microgpt/blob/main/tokenizer.py) (Character Tokenizer), [`get_data.py`](https://github.com/rcurtis14/microgpt/blob/main/get_data.py) (Corpus Download)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Text and Batches"]
  node_data_download["Corpus Download<br/>[get_data.py]"]
  node_corpus["Training Corpus<br/>[data.txt]"]
  node_tokenizer["Character Tokenizer<br/>[tokenizer.py]"]
  node_batching["Shifted Batches<br/>[get_batch.py]"]
end

subgraph group_model["Model Computation"]
  node_autograd["Tensor Autograd<br/>[engine.py]"]
  node_embedding["Token Embeddings<br/>[model.py]"]
  node_attention["Causal Attention<br/>[attention.py]"]
  node_block["Transformer Block<br/>[block.py]"]
  node_head_loss["Output and Loss<br/>[model.py]"]
end

subgraph group_training["Training and Weights"]
  node_trainer["Training Loop<br/>[train.py]"]
  node_weights["Saved Weights<br/>[model_weights.npz]"]
end

subgraph group_inference["Text Generation"]
  node_generator["Sampling Loop<br/>[generate.py]"]
  node_generated_ids["Generated IDs<br/>[generate.py]"]
  node_generated_text["Decoded Text<br/>[tokenizer.py]"]
end

node_user(("User"))
node_data_source(("Tiny Shakespeare"))

node_user -->|"requests corpus"| node_data_download
node_data_source -->|"provides text"| node_data_download
node_data_download -->|"writes corpus"| node_corpus
node_corpus -->|"reads text"| node_tokenizer
node_tokenizer -->|"encodes text"| node_batching
node_batching -->|"provides batches"| node_trainer
node_user -->|"starts training"| node_trainer
node_trainer -->|"embeds IDs"| node_embedding
node_trainer -->|"runs block"| node_block
node_block -->|"calls attention"| node_attention
node_trainer -->|"computes loss"| node_head_loss
node_trainer -->|"backpropagates"| node_autograd
node_trainer -->|"saves weights"| node_weights
node_user -->|"starts generation"| node_generator
node_generator -->|"loads weights"| node_weights
node_generator -->|"embeds context"| node_embedding
node_generator -->|"runs block"| node_block
node_generator -->|"projects logits"| node_head_loss
node_generator -->|"samples IDs"| node_generated_ids
node_generated_ids -->|"decodes IDs"| node_tokenizer
node_tokenizer -->|"returns characters"| node_generated_text

click node_data_download "https://github.com/rcurtis14/microgpt/blob/main/get_data.py"
click node_corpus "https://github.com/rcurtis14/microgpt/blob/main/data.txt"
click node_tokenizer "https://github.com/rcurtis14/microgpt/blob/main/tokenizer.py"
click node_batching "https://github.com/rcurtis14/microgpt/blob/main/get_batch.py"
click node_autograd "https://github.com/rcurtis14/microgpt/blob/main/engine.py"
click node_embedding "https://github.com/rcurtis14/microgpt/blob/main/model.py"
click node_attention "https://github.com/rcurtis14/microgpt/blob/main/attention.py"
click node_block "https://github.com/rcurtis14/microgpt/blob/main/block.py"
click node_head_loss "https://github.com/rcurtis14/microgpt/blob/main/model.py"
click node_trainer "https://github.com/rcurtis14/microgpt/blob/main/train.py"
click node_weights "https://github.com/rcurtis14/microgpt/blob/main/model_weights.npz"
click node_generator "https://github.com/rcurtis14/microgpt/blob/main/generate.py"
click node_generated_ids "https://github.com/rcurtis14/microgpt/blob/main/generate.py"
click node_generated_text "https://github.com/rcurtis14/microgpt/blob/main/tokenizer.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_data_download,node_corpus,node_tokenizer,node_batching,node_user toneBlue
class node_autograd,node_embedding,node_attention,node_block,node_head_loss toneAmber
class node_trainer,node_weights toneMint
class node_generator,node_generated_ids,node_generated_text toneRose
class node_data_source toneIndigo
```

## Components

### Text and Batches

- [Corpus Download](https://github.com/rcurtis14/microgpt/blob/main/get_data.py): `get_data.py`
- [Training Corpus](https://github.com/rcurtis14/microgpt/blob/main/data.txt): `data.txt`
- [Character Tokenizer](https://github.com/rcurtis14/microgpt/blob/main/tokenizer.py): `tokenizer.py`
- [Shifted Batches](https://github.com/rcurtis14/microgpt/blob/main/get_batch.py): `get_batch.py`

### Model Computation

- [Tensor Autograd](https://github.com/rcurtis14/microgpt/blob/main/engine.py): `engine.py`
- [Token Embeddings](https://github.com/rcurtis14/microgpt/blob/main/model.py): `model.py`
- [Causal Attention](https://github.com/rcurtis14/microgpt/blob/main/attention.py): `attention.py`
- [Transformer Block](https://github.com/rcurtis14/microgpt/blob/main/block.py): `block.py`
- [Output and Loss](https://github.com/rcurtis14/microgpt/blob/main/model.py): `model.py`

### Training and Weights

- [Training Loop](https://github.com/rcurtis14/microgpt/blob/main/train.py): `train.py`
- [Saved Weights](https://github.com/rcurtis14/microgpt/blob/main/model_weights.npz): `model_weights.npz`

### Text Generation

- [Sampling Loop](https://github.com/rcurtis14/microgpt/blob/main/generate.py): `generate.py`
- [Generated IDs](https://github.com/rcurtis14/microgpt/blob/main/generate.py): `generate.py`
- [Decoded Text](https://github.com/rcurtis14/microgpt/blob/main/tokenizer.py): `tokenizer.py`

### Other components

- **User**
- **Tiny Shakespeare**

## Connections

- User → Corpus Download: requests corpus (evidence: `README.md`)
- Tiny Shakespeare → Corpus Download: provides text (evidence: `get_data.py`)
- Corpus Download → Training Corpus: writes corpus (evidence: `get_data.py`)
- Training Corpus → Character Tokenizer: reads text (evidence: `tokenizer.py`)
- Character Tokenizer → Shifted Batches: encodes text (evidence: `get_batch.py`)
- Shifted Batches → Training Loop: provides batches (evidence: `train.py`)
- User → Training Loop: starts training (evidence: `README.md`)
- Training Loop → Token Embeddings: embeds IDs (evidence: `train.py`)
- Training Loop → Transformer Block: runs block (evidence: `train.py`)
- Transformer Block → Causal Attention: calls attention (evidence: `block.py`)
- Training Loop → Output and Loss: computes loss (evidence: `train.py`)
- Training Loop → Tensor Autograd: backpropagates (evidence: `README.md`)
- Training Loop → Saved Weights: saves weights (evidence: `train.py`)
- User → Sampling Loop: starts generation (evidence: `README.md`)
- Sampling Loop → Saved Weights: loads weights (evidence: `generate.py`)
- Sampling Loop → Token Embeddings: embeds context (evidence: `generate.py`)
- Sampling Loop → Transformer Block: runs block (evidence: `generate.py`)
- Sampling Loop → Output and Loss: projects logits (evidence: `generate.py`)
- Sampling Loop → Generated IDs: samples IDs (evidence: `generate.py`)
- Generated IDs → Character Tokenizer: decodes IDs (evidence: `generate.py`)
- Character Tokenizer → Decoded Text: returns characters (evidence: `tokenizer.py`)
