gulding commited on
Commit
2fe0ff8
Β·
verified Β·
1 Parent(s): 1fc66ec

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +106 -117
README.md CHANGED
@@ -1,117 +1,106 @@
1
- ---
2
- license: mit
3
- pipeline_tag: text-generation
4
- language:
5
- - en
6
- tags:
7
- - gguf
8
- - llama.cpp
9
- - ollama
10
- - from-scratch
11
- - educational
12
- - pytorch
13
- ---
14
-
15
- # EmsyAI πŸš€
16
-
17
- EmsyAI is a small, decoder-only transformer language model built entirely from scratch in pure PyTorch β€” no `transformers` library, no pre-built blocks. It's an educational project: the goal is to demystify how models like Llama 3, Qwen 2.5, and DeepSeek work internally by actually building every piece β€” tokenizer, attention, RoPE, KV cache, LoRA β€” by hand.
18
-
19
- Full training code, data pipeline, and developer docs live on [GitHub](https://github.com/gulding/EmsyAI). This page is about the model artifact itself: what it is, how to run it, and what to expect.
20
-
21
- > [!WARNING]
22
- > **Read this before you run it:** EmsyAI is a small model (V3 Titan is 154M parameters) trained on a small, narrow dataset. It is not a general-purpose assistant and won't behave like one β€” treat it as a working demonstration of a from-scratch LLM pipeline, not a production model. See [Limitations](#limitations).
23
-
24
- ### Built from scratch:
25
- - 🧩 **Hand-rolled byte-pair encoding (BPE)** tokenizer
26
- - πŸ”„ **Rotary position embeddings (RoPE)** via complex tensor rotation
27
- - 🎯 **Grouped-query attention (GQA)**, fused via `F.scaled_dot_product_attention`
28
- - 🧠 **SwiGLU** feed-forward layers
29
- - πŸ“ **RMSNorm** pre-normalization
30
- - πŸ’Ύ **KV caching** for autoregressive inference
31
- - 🎨 **LoRA fine-tuning** across all seven linear projections per block
32
-
33
- ## Quickstart
34
-
35
- By default, we serve the latest **V3 Titan** architecture (154M parameters, 4,096-token context window).
36
-
37
- ```bash
38
- ollama run hf.co/gulding/EmsyAI
39
- ```
40
-
41
- or with `llama.cpp`:
42
- ```bash
43
- llama-cli -hf gulding/EmsyAI:F32
44
- ```
45
-
46
- > **Note:** If you are looking for the older **V2** architecture (88M params, 1,024-token context), the `emsyai-v2-instruct-f32.gguf` file is still available in the Files tab.
47
-
48
- ## Prompt format
49
-
50
- ```text
51
- [USER]
52
- Write a Python function to check if a number is prime.
53
- [MODEL]
54
- ```
55
-
56
- ## What's in this repo
57
-
58
- | File | What it is | Do you need it? |
59
- |---|---|---|
60
- | `emsyai-v3-titan-instruct-f32.gguf` | **v3 Titan** base model + LoRA adapter, merged and exported to GGUF | **Yes.** This is what Ollama / llama.cpp actually run by default |
61
- | `emsyai-v2-instruct-f32.gguf` | **v2** base model + LoRA adapter, merged and exported to GGUF | Optional. Legacy 88M architecture |
62
- | `base_model_154M_step_5000.pt` | Raw PyTorch inference weights | Only if loading it back into the [training code](https://github.com/gulding/EmsyAI) |
63
- | `tokenizer.json` | The trained 16k-vocab BPE tokenizer | Only needed for the PyTorch path, not GGUF |
64
- | `Modelfile` | Ollama configuration β€” prompt template, stop token, temperature | Used automatically by `ollama run` |
65
-
66
- *(GGUF export maps the model's tensors onto llama.cpp's naming convention for Ollama/llama.cpp compatibility β€” that's why the architecture shows as `llama` in the metadata above. The training-time implementation itself is fully custom, not derived from Llama's code.)*
67
-
68
- ## Model Details
69
-
70
- | Specification | EmsyAI-v3 "Titan" (Latest) | EmsyAI-v2 (Legacy) |
71
- |---|---|---|
72
- | Parameters | 153.8M (tied embeddings) | 88.1M (tied embeddings) |
73
- | Layers | 16 | 12 |
74
- | Hidden dim | 896 | 768 |
75
- | Attention heads | 14 Query / 2 KV (GQA 7:1) | 12 Query / 4 KV (GQA 3:1) |
76
- | FFN dim | 2,560 (SwiGLU) | 2,048 (SwiGLU) |
77
- | Context window | 4,096 tokens | 1,024 tokens |
78
- | Vocab size | 16,000 (custom BPE) | 16,000 (custom BPE) |
79
- | Position encoding | RoPE | RoPE |
80
-
81
- **Training data (V3 Titan):** a ~150M-token hybrid of Cosmopedia v2 (synthetic educational text) and a Python code subset, tokenized with the project's own BPE tokenizer. Strict 10-gram decontamination was run against HumanEval and MBPP. Instruction-tuned with LoRA (rank 8, alpha 16) on ~20k CodeAlpaca examples with masked prompt loss.
82
-
83
- ## Project status
84
-
85
- **V3 Titan is complete.** The model successfully converged during 5,000 steps of pretraining on the 150M-token corpus, followed by 10,000 steps of LoRA instruction tuning. Prompt loss masking (-100) successfully eliminated base-model "topic drift", resulting in a model that strictly outputs the desired python format.
86
-
87
- ## Limitations
88
-
89
- - **Small model, small data.** 154M active parameters trained on ~150M tokens β€” closer in scale to early GPT-2-class experiments than to a modern assistant model.
90
- - **No RLHF or preference tuning** β€” only supervised LoRA fine-tuning on instruction-following examples. Expect repetitive, sometimes factually wrong, or off-topic output, especially past a few sentences.
91
- - **Code it writes may not run.** This project's own benchmark only checks that generated Python parses, not that it's logically correct β€” don't assume otherwise.
92
- - **English only**, and narrow in domain (educational text and Python code).
93
-
94
- ## License
95
- MIT β€” see [LICENSE](https://github.com/gulding/EmsyAI/blob/main/LICENSE).
96
-
97
- ## Links
98
- - πŸ’» [Source code, training pipeline, and full docs](https://github.com/gulding/EmsyAI)
99
- - πŸ’¬ [Issues / discussions](https://github.com/gulding/EmsyAI/issues)
100
-
101
-
102
- Prompt loss masking successfully eliminated base-model "topic drift" (the model attempts to output Python blocks rather than continuing the prompt), but the underlying logic is still heavily limited by the 154M parameter count.
103
-
104
- Here is a raw, unedited transcript from the model demonstrating its capabilities (and limitations):
105
-
106
- ```python
107
- [USER]
108
- Write the function def is_prime(n):
109
- [MODEL]
110
- def is_prime(n):
111
- if n <= 1:
112
- return False
113
-
114
- for i in range(2, int(n-1)+"):
115
- print("Fizz")
116
- ```
117
- *Note: As seen above, while the model attempts to generate Python, it frequently hallucinates syntax errors or drifts into unrelated coding topics (like FizzBuzz).*
 
1
+ # EmsyAI 🧠
2
+
3
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4
+ [![Python: 3.12](https://img.shields.io/badge/Python-3.12-blue.svg)](https://www.python.org/)
5
+ [![PyTorch: 2.5.1+cu121](https://img.shields.io/badge/PyTorch-CUDA%2012.1-red.svg)](https://pytorch.org/)
6
+ [![Hugging Face: EmsyAI](https://img.shields.io/badge/HuggingFace-gulding%2FEmsyAI-orange.svg)](https://huggingface.co/gulding/EmsyAI)
7
+
8
+ 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.
9
+
10
+ This is **not** a wrapper around the `transformers` library. **Every single component was built from scratch:**
11
+
12
+ - βœ‚οΈ **Hand-rolled Byte-Pair Encoding (BPE)** Tokenizer with native special token isolation (`<|eos|>`, `<|bos|>`, `<|pad|>`)
13
+ - πŸ”„ **Rotary Positional Embeddings (RoPE)** via complex polar tensor rotations
14
+ - ⚑ **Grouped Query Attention (GQA)** with fused FlashAttention-2 (PyTorch SDPA)
15
+ - πŸšͺ **SwiGLU Feed-Forward Networks** (3-matrix gated layout: $W_1, W_2, W_3$)
16
+ - βš–οΈ **RMSNorm Pre-Normalization** with numerical float32 upcasting
17
+ - πŸ’Ύ **Step-by-Step KV Caching** for high-throughput autoregressive inference
18
+ - 🎯 **Multi-Layer Low-Rank Adaptation (LoRA)** fine-tuning across all 7 linear projections
19
+
20
+ ---
21
+
22
+ ## πŸ—οΈ Architecture Specifications
23
+
24
+ | Specification | EmsyAI-v3 Titan | EmsyAI-v4 (Current Training) |
25
+ |---|---|---|
26
+ | **Parameters** | 153.8 Million (Tied) | 196 Million (Tied) |
27
+ | **Layers ($L$)** | 16 | 16 |
28
+ | **Hidden Dimension ($d_{\text{model}}$)** | 896 | 1024 |
29
+ | **Attention Heads ($H_q / H_{kv}$)** | 14 Query / 2 KV (GQA 7:1) | 16 Query / 4 KV (GQA 4:1) |
30
+ | **FFN Dimension** | 2,560 (SwiGLU) | 2,816 (SwiGLU) |
31
+ | **Context Window ($T$)** | 4,096 tokens | 4,096 tokens |
32
+ | **Vocabulary Size** | 16,000 (Hybrid BPE) | 16,000 (Hybrid BPE) |
33
+ | **Attention Backend** | FlashAttention-2 | FlashAttention-2 (`is_causal=True`) |
34
+
35
+ ---
36
+
37
+ ## πŸš€ Quickstart: Run in 5 Seconds via Ollama
38
+
39
+ You can run EmsyAI natively without installing PyTorch or GPU drivers:
40
+
41
+ ```bash
42
+ ollama run hf.co/gulding/EmsyAI
43
+ ```
44
+
45
+ ## πŸ’» Developer & Training Pipeline
46
+
47
+ This project uses [uv](https://github.com/astral-sh/uv) for fast dependency management.
48
+
49
+ ```bash
50
+ # 1. Clone the repository
51
+ git clone https://github.com/gulding/EmsyAI.git
52
+ cd EmsyAI
53
+
54
+ # 2. Install dependencies (PyTorch with CUDA 12.1)
55
+ uv sync
56
+ ```
57
+
58
+ ### 1. Data Engine & Strict Decontamination
59
+ EmsyAI-v4 is trained on a strictly verified, mathematically sterile dataset. We explicitly filter out exact 13-gram overlap sequences against standard coding benchmarks (e.g., HumanEval) at the token level during ingestion.
60
+ ```bash
61
+ # Stream and decontaminate 500M tokens from Cosmopedia v2
62
+ uv run python scripts/prepare_v4_data.py
63
+
64
+ # Verify the dataset is 100% sterile
65
+ uv run python scripts/audit_contamination.py
66
+ ```
67
+
68
+ ### 2. Pretraining
69
+ ```bash
70
+ # Pretrain EmsyAI-v4 (196M Model @ 4,096 Context)
71
+ uv run python -m emsyai.training.train_v4
72
+ ```
73
+
74
+ ### 3. LoRA Instruction Fine-Tuning (SFT)
75
+ ```bash
76
+ # Download 20k CodeAlpaca instruction dataset
77
+ uv run python -m emsyai.training.download_alpaca
78
+
79
+ # Train LoRA adapters across all linear layers
80
+ uv run python -m emsyai.training.finetune --steps 10000
81
+ ```
82
+
83
+ ### 4. Interactive Chat REPL
84
+ ```bash
85
+ uv run python -m emsyai.chat \
86
+ --checkpoint checkpoints_v4/model_step_15000.pt \
87
+ --version v4
88
+ ```
89
+
90
+ ## πŸ“ Prompt Template
91
+
92
+ When querying the instruction model, use the exact role tags:
93
+
94
+ ```text
95
+ [USER]
96
+ Write a Python function to check if a number is prime.
97
+ [MODEL]
98
+ ```
99
+
100
+ ## πŸ”¬ Mathematical Verification Suite
101
+
102
+ Run our verification suite to mathematically validate attention causality and KV-cache consistency:
103
+
104
+ ```bash
105
+ uv run python scripts/verify_model.py
106
+ ```