gulding commited on
Commit
dd5c6df
Β·
verified Β·
1 Parent(s): 4d6213a

Update Model Card

Browse files
Files changed (1) hide show
  1. README.md +78 -56
README.md CHANGED
@@ -1,99 +1,121 @@
1
  ---
 
 
2
  license: mit
3
- language:
4
- - en
5
  tags:
6
- - from-scratch
7
  - pytorch
8
- - educational
9
- - lora
10
- - code-generation
11
  ---
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.
99
 
 
 
 
 
1
  ---
2
+ base_model: gulding/EmsyAI
3
+ library_name: emsyai
4
  license: mit
 
 
5
  tags:
6
+ - emsyai
7
  - pytorch
8
+ - custom-architecture
 
 
9
  ---
 
10
  # EmsyAI 🧠
11
 
12
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
13
+ [![Python: 3.12](https://img.shields.io/badge/Python-3.12-blue.svg)](https://www.python.org/)
14
+ [![PyTorch: 2.5.1+cu121](https://img.shields.io/badge/PyTorch-CUDA%2012.1-red.svg)](https://pytorch.org/)
15
+ [![Hugging Face: EmsyAI](https://img.shields.io/badge/HuggingFace-gulding%2FEmsyAI-orange.svg)](https://huggingface.co/gulding/EmsyAI)
16
+
17
+ EmsyAI is a complete, educational, decoder-only Transformer Language Model built entirely from mathematical first principles in pure PyTorch. The goal of this project is to demystify how modern LLMs (like Llama 3, Qwen 2.5, and DeepSeek) operate under the hood.
18
+
19
+ This is **not** a wrapper around the `transformers` library. **Every single component was built from scratch:**
20
+
21
+ - βœ‚οΈ **Hand-rolled Byte-Pair Encoding (BPE)** Tokenizer with native special token isolation (`<|eos|>`, `<|bos|>`, `<|pad|>`)
22
+ - πŸ”„ **Rotary Positional Embeddings (RoPE)** via complex polar tensor rotations
23
+ - ⚑ **Grouped Query Attention (GQA)** with fused FlashAttention-2 (PyTorch SDPA)
24
+ - πŸšͺ **SwiGLU Feed-Forward Networks** (3-matrix gated layout: $W_1, W_2, W_3$)
25
+ - βš–οΈ **RMSNorm Pre-Normalization** with numerical float32 upcasting
26
+ - πŸ’Ύ **Step-by-Step KV Caching** for high-throughput autoregressive inference
27
+ - 🎯 **Multi-Layer Low-Rank Adaptation (LoRA)** fine-tuning across all 7 linear projections
28
+
29
+ ---
30
 
31
+ ## πŸ—οΈ Architecture Specifications
 
 
 
 
 
 
 
32
 
33
+ | Specification | EmsyAI-v2 (Current) | EmsyAI-v3 Titan (Active) |
34
+ |---|---|---|
35
+ | **Parameters** | 88.1 Million (Tied) | 178.5 Million (Tied) |
36
+ | **Layers ($L$)** | 12 | 16 |
37
+ | **Hidden Dimension ($d_{\text{model}}$)** | 768 | 896 |
38
+ | **Attention Heads ($H_q / H_{kv}$)** | 12 Query / 4 KV (GQA 3:1) | 14 Query / 2 KV (GQA 7:1) |
39
+ | **FFN Dimension** | 2,048 (SwiGLU) | 2,560 (SwiGLU) |
40
+ | **Context Window ($T$)** | 1,024 tokens | 4,096 tokens |
41
+ | **Vocabulary Size** | 16,000 (Hybrid BPE) | 16,000 (Hybrid BPE) |
42
+ | **Attention Backend** | FlashAttention-2 (`F.scaled_dot_product_attention`) | FlashAttention-2 |
43
 
44
+ ---
 
 
 
 
 
 
 
 
45
 
46
+ ## πŸš€ Quickstart: Run in 5 Seconds via Ollama
47
 
48
+ You can run EmsyAI natively without installing PyTorch or GPU drivers:
49
 
50
  ```bash
51
+ ollama run hf.co/gulding/EmsyAI
 
52
  ```
53
 
54
+ ## πŸ’» Developer & Training Pipeline
55
+
56
+ This project uses [uv](https://github.com/astral-sh/uv) for fast dependency management.
57
 
58
  ```bash
59
+ # 1. Clone the repository
60
+ git clone https://github.com/gulding/EmsyAI.git
61
+ cd EmsyAI
62
+
63
+ # 2. Install dependencies (PyTorch with CUDA 12.1)
64
+ uv sync
65
  ```
66
 
67
+ ### 1. Data Engine & Tokenizer
 
68
  ```bash
69
+ # Stream 100k hybrid samples (Cosmopedia v2 + Python-25k)
70
  uv run python data/download_v2.py
71
 
72
+ # Or stream the 150M Token Titan Dataset with Decontamination
73
+ uv run python data/download_v3.py
74
+
75
+ # Train the 16k BPE Tokenizer
76
  uv run python scripts/train_tokenizer.py
77
  ```
78
 
79
+ ### 2. Pretraining
 
 
80
  ```bash
81
+ # Pretrain EmsyAI-v2 (88M Model)
82
  uv run python -m emsyai.training.train
 
83
 
84
+ # Pretrain EmsyAI-v3 Titan (180M Model @ 4,096 Context)
85
+ uv run python -m emsyai.training.train_v3
86
+ ```
87
 
88
+ ### 3. LoRA Instruction Fine-Tuning (SFT)
89
  ```bash
90
+ # Download 20k CodeAlpaca instruction dataset
91
  uv run python -m emsyai.training.download_alpaca
92
 
93
+ # Train LoRA adapters across all linear layers
94
  uv run python -m emsyai.training.finetune --steps 10000
95
  ```
96
 
97
+ ### 4. Interactive Chat REPL
 
 
98
  ```bash
99
+ uv run python -m emsyai.chat_instruct \
100
+ --base_checkpoint checkpoints_v2/model_step_5000.pt \
101
+ --lora_checkpoint checkpoints_v2/lora/instruct_lora_step_10000.pt \
102
+ --tokenizer dataset/tokenizer_v2.json
103
  ```
104
 
105
+ ## πŸ“ Prompt Template
 
106
 
107
+ When querying the instruction model, use the exact role tags:
108
+
109
+ ```text
110
+ [USER]
111
+ Write a Python function to check if a number is prime.
112
+ [MODEL]
113
  ```
114
 
115
+ ## πŸ”¬ Mathematical Verification Suite
116
+
117
+ Run our verification suite to mathematically validate attention causality and KV-cache consistency:
118
 
119
+ ```bash
120
+ uv run python scripts/verify_model.py
121
+ ```