gulding commited on
Commit
cbc87e0
ยท
verified ยท
1 Parent(s): 04573c8

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +75 -36
README.md CHANGED
@@ -12,48 +12,87 @@ tags:
12
 
13
  # EmsyAI ๐Ÿง 
14
 
15
- EmsyAI is a **28 Million Parameter Decoder-only Transformer** built entirely from scratch in pure PyTorch for educational purposes. It is not a wrapper around the `transformers` library, but a fully hand-rolled implementation of a modern LLM architecture (heavily inspired by Llama 3).
16
-
17
- ## ๐Ÿ—๏ธ Model Architecture
18
-
19
- - **Parameters**: ~28 Million
20
- - **Layers**: 8
21
- - **Hidden Dimension**: 512
22
- - **Attention Heads**: 8 Query, 4 KV (Grouped Query Attention)
23
- - **FFN Hidden Dimension**: 1408 (SwiGLU)
24
- - **Context Length**: 512
25
- - **Vocab Size**: 8000
26
- - **Normalization**: Pre-RMSNorm
27
- - **Positional Encoding**: Rotary Positional Embeddings (RoPE)
28
-
29
- ## ๐Ÿ”ง Training Details
30
-
31
- ### Phase 1: Base Pretraining
32
- The base model was pretrained from scratch on ~40MB of Python source code (from the CPython repository) using a hand-rolled Byte-Pair Encoding (BPE) tokenizer.
33
- - **Optimizer**: AdamW with `bfloat16` mixed precision
34
- - **Learning Rate**: Cosine decay with linear warmup
35
- - **Final Perplexity**: ~10.77
36
-
37
- ### Phase 2: Instruction Fine-Tuning (LoRA)
38
- The model was fine-tuned to follow instructions using a custom, from-scratch Low-Rank Adaptation (LoRA) implementation.
39
- - **Dataset**: `CodeAlpaca-20k`
40
- - **LoRA Targets**: All linear layers (Q, K, V, O, w1, w2, w3)
41
- - **Trainable Parameters**: 598,016 (2.11%)
42
- - **Final Loss**: ~1.92
43
-
44
- ## โšก Run Natively with Ollama
 
 
 
45
  Because EmsyAI follows the standard Llama architecture, we exported it to GGUF format! You can run it instantly on your local machine using Ollama without installing any Python dependencies:
46
 
47
  ```bash
48
  ollama run hf.co/gulding/EmsyAI
49
  ```
50
 
51
- ## ๐Ÿ’ป Python Usage
 
 
 
 
52
 
53
- To use this model in PyTorch, you will need the custom inference code from the GitHub repository:
54
- [https://github.com/gulding/EmsyAI](https://github.com/gulding/EmsyAI)
 
55
 
56
- Because the tokenizer and architecture are custom-built, this model is **not directly compatible** with the `transformers` `AutoModel` API. It is designed to be run using the `chat_instruct.py` script provided in the GitHub repo.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
- ## ๐ŸŽ“ Educational Value
59
- This project demonstrates how to build every layer of a modern Large Language Model from mathematical first principles, including the data pipeline, the Transformer core, LoRA adapters, and autoregressive KV-caching generation.
 
 
12
 
13
  # EmsyAI ๐Ÿง 
14
 
15
+ EmsyAI is a complete, educational, decoder-only Transformer Language Model built entirely from scratch in pure PyTorch. The goal of this project is to demystify how modern Large Language Models (like Llama 3, Qwen, and GPT) work under the hood.
16
+
17
+ This is not a wrapper around the `transformers` library. **Every single component was built from scratch:**
18
+ - Hand-rolled Byte-Pair Encoding (BPE) Tokenizer
19
+ - Rotary Positional Embeddings (RoPE)
20
+ - Grouped Query Attention (GQA)
21
+ - SwiGLU Feed-Forward Networks
22
+ - RMSNorm Pre-Normalization
23
+ - KV Caching Autoregressive Inference
24
+ - Low-Rank Adaptation (LoRA) Fine-tuning
25
+
26
+ ## ๐Ÿ—๏ธ Architecture
27
+
28
+ The architecture heavily mirrors modern LLM designs (specifically Llama 3):
29
+ - **Parameters**: ~89 Million (120M class with weight tying)
30
+ - **Layers**: 12
31
+ - **Hidden Dimension**: 768
32
+ - **Attention Heads**: 12 Query, 4 KV (GQA)
33
+ - **FFN Hidden Dimension**: 2048
34
+ - **Context Length**: 1024
35
+ - **Vocab Size**: 16000
36
+ - **Attention Kernel**: PyTorch Native FlashAttention-2 (SDPA)
37
+
38
+ ## ๐Ÿš€ Getting Started
39
+
40
+ This project uses `uv` for lightning-fast dependency management.
41
+
42
+ ```bash
43
+ # Install dependencies
44
+ uv sync
45
+ ```
46
+
47
+ ### โšก Run Natively with Ollama
48
  Because EmsyAI follows the standard Llama architecture, we exported it to GGUF format! You can run it instantly on your local machine using Ollama without installing any Python dependencies:
49
 
50
  ```bash
51
  ollama run hf.co/gulding/EmsyAI
52
  ```
53
 
54
+ ### 1. Tokenizer Training
55
+ The pure-Python BPE tokenizer is trained on the CPython source code.
56
+ ```bash
57
+ # Download CPython source code
58
+ uv run python data/download_v2.py
59
 
60
+ # Train the BPE Tokenizer (creates dataset/tokenizer.json)
61
+ uv run python scripts/train_tokenizer.py
62
+ ```
63
 
64
+ ### 2. Base Model Pretraining
65
+ The training loop features AdamW, Cosine Learning Rate decay with linear warmup, Gradient Accumulation, and `bfloat16` Mixed Precision.
66
+
67
+ ```bash
68
+ uv run python -m emsyai.training.train
69
+ ```
70
+
71
+ ### 3. LoRA Instruction Fine-Tuning
72
+ A custom LoRA implementation targets all linear layers (Q, K, V, O, w1, w2, w3) to fine-tune the base model on a subset of CodeAlpaca.
73
+
74
+ ```bash
75
+ # Download 20k CodeAlpaca instruction pairs
76
+ uv run python -m emsyai.training.download_alpaca
77
+
78
+ # Train the LoRA adapters
79
+ uv run python -m emsyai.training.finetune --steps 10000
80
+ ```
81
+
82
+ ### 4. Exporting to GGUF
83
+ We include a custom `export_gguf.py` script that merges the base weights with the LoRA matrices and exports the full PyTorch model directly to a `.gguf` file compatible with `llama.cpp`.
84
+
85
+ ```bash
86
+ uv run python scripts/export_gguf.py
87
+ ```
88
+
89
+ ### 5. Chat Interface
90
+ You can interact with the instruction-tuned model via a command-line REPL. It implements KV caching, top-k/top-p sampling, and repetition penalties.
91
+
92
+ ```bash
93
+ uv run python -m emsyai.chat_instruct --lora_checkpoint checkpoints_v2/lora/instruct_lora_step_10000.pt
94
+ ```
95
 
96
+ ## ๐Ÿ› ๏ธ Verification & Benchmarking
97
+ - Run `scripts/verify_model.py` to cryptographically prove that the Causal Mask prevents future token leakage, and that the KV Cache accurately matches standard autoregressive generation.
98
+ - Run `uv run python -m emsyai.benchmark` to test the base model's syntax generation capabilities using Python's AST parser.