firdavsus commited on
Commit
73c78ab
ยท
verified ยท
1 Parent(s): 6d269f4

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +133 -3
README.md CHANGED
@@ -1,3 +1,133 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ ---
4
+ # LLM\_D3: A Sparse 350M Architecture Trained on 50B Tokens
5
+
6
+ This repository contains the implementation of **LLM\_D3**, a decoder-only Large Language Model trained from scratch on 50 billion tokens of the C4 English-only dataset. It features a modern, high-performance architecture optimized for efficiency, combining **Mixture of Experts (MoE)**, **Multi-head Latent Attention (MLA)**, and **Rotary Positional Embeddings (RoPE)**.
7
+
8
+ Designed for genuine generalization over rote memorization, the model was trained using a single-epoch pass, achieving a **33% zero-shot HellaSwag** score. Following instruction fine-tuning, it serves as a capable assistant with strong general reasoning and factual recall.
9
+
10
+ -----
11
+
12
+ ## ๐Ÿ“Š Model Statistics
13
+
14
+ | Metric | Value |
15
+ | :--- | :--- |
16
+ | **Total Parameters** | 358.74M |
17
+ | **Active Parameters** | 171.96M |
18
+ | **Sparsity Ratio** | 52.06% |
19
+ | **Training Data** | 50B Tokens (C4 English) |
20
+ | **Architecture** | MLA + Sparse MoE + RoPE |
21
+
22
+ -----
23
+
24
+ ## for the scipt
25
+ **github: firdavsus/LLM_D3**
26
+
27
+ ## ๐Ÿ—๏ธ Architecture Details
28
+
29
+ The model utilizes a custom GPT implementation (`LLM_2.py`) with several key architectural innovations focused on compute efficiency and memory optimization.
30
+
31
+ ### Multi-head Latent Attention (MLA)
32
+
33
+ To solve the memory bottleneck of the KV cache, LLM\_D3 implements **Multi-head Latent Attention**.
34
+
35
+ * **Latent Compression**: Query and KV states are compressed into a lower-dimensional latent space before being up-projected for attention calculations.
36
+ * **Throughput**: This reduces the memory footprint of the KV cache during inference while maintaining the performance of standard Multi-Head Attention.
37
+
38
+ ### Sparse Mixture of Experts (MoE)
39
+
40
+ LLM\_D3 uses a sparse MoE architecture for 19 out of its 24 layers.
41
+
42
+ * **Expert Configuration**: Each MoE layer contains **6 experts**, with a **Top-2** routing mechanism active for every token.
43
+ * **Hybrid Stability Sandwich**: For improved training stability, the **first 3 layers** and **last 2 layers** are initialized as standard dense MLP blocks rather than MoE layers.
44
+ * **Routing**: Uses a noisy Top-K router with auxiliary load-balancing and router z-loss to prevent expert collapse and ensure balanced utilization across the 19 MoE blocks.
45
+
46
+ ### Positional Encoding
47
+
48
+ * **RoPE**: Rotary Positional Embeddings are applied to ensure better handling of long-range dependencies and superior sequence positioning compared to traditional learned embeddings.
49
+
50
+ -----
51
+
52
+ ## ๐Ÿ“ˆ Training & Evaluation
53
+
54
+ ### Pre-training Setup
55
+
56
+ * **Policy**: Single-epoch pass on 50B tokens (no repetition) to prioritize feature extraction and generalization.
57
+ * **Batch Size**: 1M tokens effective batch size for high gradient stability.
58
+ * **Schedule**: Warmup-Stable-Decay (WSD) / Stepped Cosine Decay with a 1,000-step warmup.
59
+ * **Optimizer**: AdamW with hardware-optimized settings.
60
+
61
+ ### Benchmarks
62
+
63
+ | Benchmark | Setting | Score |
64
+ | :--- | :--- | :--- |
65
+ | **HellaSwag** | Zero-shot | **33%** |
66
+
67
+ ### Fine-tuning
68
+
69
+ Fine-tuned on the `alpaca-cleaned` dataset using an Instruction-Input-Response format.
70
+
71
+ * **Strengths**: Strong general reasoning, factual consistency, and instruction adherence.
72
+ * **Known Limitations**: The model currently struggles with complex arithmetic. Additionally, an initialization anomaly in the final 2 layers resulted in a signal spike at the end of the network; while the model remains functional and capable, this is a known area for future refinement.
73
+
74
+ -----
75
+
76
+ ## ๐Ÿ–ผ๏ธ Visualizations
77
+
78
+ ### Pre-Training Curves
79
+ ![Pre-Training](images/training_curves_with_eval.png)
80
+
81
+ *50k steps on a 50B token corpus with 1M token effective batch size.*
82
+
83
+ ### Diagnostics & Utilization
84
+ ![Model-analysis](images/full_diagnostics.png)
85
+ ![Model-analysis](images/weight_histograms.png)
86
+ *Visualizing weight distribution and expert utilization. Current routing shows healthy balance with utilization under 33%.*
87
+
88
+ -----
89
+
90
+ ## ๐Ÿ› ๏ธ Usage
91
+
92
+ ### Inference
93
+
94
+ Interact with the model using the `test.py` script, which includes Top-K, Top-P, and repetition penalty sampling.
95
+
96
+ ```bash
97
+ python test.py
98
+ ```
99
+
100
+ ### Fine-tuning
101
+
102
+ To replicate the instruction tuning on your own dataset:
103
+
104
+ 1. Format your data following the Alpaca template in `fine_tune.py`.
105
+ 2. Execute:
106
+
107
+ <!-- end list -->
108
+
109
+ ```bash
110
+ python fine_tune.py
111
+ ```
112
+
113
+ -----
114
+
115
+ ## ๐Ÿ“‚ Repository Structure
116
+
117
+ * `LLM_2.py`: Core architecture (MLA, MoE, RoPE).
118
+ * `train.py`: Pre-training logic and WSD scheduler.
119
+ * `fine_tune.py`: Instruction tuning implementation.
120
+ * `manager.py`: MoE auxiliary loss tracking.
121
+ * `check_params.py`: Active vs. total parameter counter.
122
+ * `eval.py`: HellaSwag evaluation suite.
123
+ * `analysis.py` / `show.py`: Diagnostic and visualization tools.
124
+
125
+ -----
126
+
127
+ *Note: This model was developed as a research exploration into efficient sparse architectures. Verify all mathematical outputs manually.*
128
+
129
+ ### References
130
+
131
+ * [nanoMoE Implementation](https://www.google.com/search?q=https://github.com/avm-avm/nanoMoE)
132
+ * [MLA Implementation Guide](https://medium.com/@atulit23/implementing-multi-head-latent-attention-from-scratch-in-python-1e14d03fbc91)
133
+ * [DeepSeek-V3 Research (MoE/MLA Foundations)](https://arxiv.org/abs/2412.19437)