Upload README.md with huggingface_hub
Browse files
README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Basic Arithmetic Transformer from Scratch
|
| 2 |
+
|
| 3 |
+
A toy ~1.2M parameter Decoder-only Transformer built from scratch using Python, JAX, Flax, and Optax. The model is trained to solve simple arithmetic additions (e.g., predicting the answer string `168` from the input prompt `123+45=`) using character-level tokenization.
|
| 4 |
+
|
| 5 |
+
---
|
| 6 |
+
|
| 7 |
+
## 1. File Structure
|
| 8 |
+
|
| 9 |
+
* `embedding_layer.py`: Combines token character embeddings and learned absolute positional embeddings.
|
| 10 |
+
* `attention.py`: Causal Multi-Head Self-Attention layer with Query, Key, Value projections and masking.
|
| 11 |
+
* `transformer_block.py`: Integrates attention, multi-layer perceptron (MLP), normalization, and residual skip connections.
|
| 12 |
+
* `model.py`: Master model class stacking 6 transformer blocks (Depth = 6) and the final output head.
|
| 13 |
+
* `main.py`: Main script to generate random additions, calculate masked cross-entropy loss, and execute the compiled GPU training loop.
|
| 14 |
+
* `inference.py`: Interactive console script to load saved weights and prompt the model in real-time.
|
| 15 |
+
* `eval.py`: Automated batch evaluation script that runs 100 random additions and prints exact match accuracy.
|
| 16 |
+
* `docs/`:
|
| 17 |
+
* `basic_overview.md`: Simple conceptual guide on how the model functions.
|
| 18 |
+
* `detailed_technical_implementation.md`: Mathematical and systems-level blueprint.
|
| 19 |
+
|
| 20 |
+
---
|
| 21 |
+
|
| 22 |
+
### Important GPU/CUDA Constraint (Windows Users)
|
| 23 |
+
> [!WARNING]
|
| 24 |
+
> **JAX does not support GPU/CUDA acceleration natively on Windows.** Running natively on Windows forces JAX to run in CPU-only mode. To utilize your NVIDIA GPU (like the RTX 4090) for high-speed training, **you must execute the code inside Windows Subsystem for Linux (WSL2)**.
|
| 25 |
+
|
| 26 |
+
---
|
| 27 |
+
|
| 28 |
+
## 2. Installation & Requirements
|
| 29 |
+
|
| 30 |
+
### A. GPU Execution (Recommended - via WSL2 Ubuntu)
|
| 31 |
+
1. Open your WSL2 terminal (Ubuntu).
|
| 32 |
+
2. Clone the repository and navigate to the project directory:
|
| 33 |
+
```bash
|
| 34 |
+
git clone <your-repo-url>
|
| 35 |
+
cd BasicTransformer
|
| 36 |
+
```
|
| 37 |
+
3. Install the packages:
|
| 38 |
+
```bash
|
| 39 |
+
pip install -r requirements.txt
|
| 40 |
+
```
|
| 41 |
+
4. Install JAX with CUDA 12 support (runs directly on your GPU through WSL):
|
| 42 |
+
```bash
|
| 43 |
+
pip install -U "jax[cuda12]"
|
| 44 |
+
```
|
| 45 |
+
|
| 46 |
+
### B. CPU Execution (Fallback - Native Windows)
|
| 47 |
+
1. Open your Windows terminal and navigate to the project directory.
|
| 48 |
+
2. Install requirements (runs on CPU only):
|
| 49 |
+
```bash
|
| 50 |
+
pip install -r requirements.txt
|
| 51 |
+
```
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
---
|
| 55 |
+
|
| 56 |
+
## 3. Usage
|
| 57 |
+
|
| 58 |
+
### A. Training the Model
|
| 59 |
+
To train the model from scratch, execute:
|
| 60 |
+
```bash
|
| 61 |
+
python main.py
|
| 62 |
+
```
|
| 63 |
+
This runs the training loop for 30,000 steps (configurable in the code), logs the progress, and saves the trained weights to `model.safetensors`.
|
| 64 |
+
|
| 65 |
+
### B. Interactive Inference Console
|
| 66 |
+
To prompt your model interactively (without retraining):
|
| 67 |
+
```bash
|
| 68 |
+
python inference.py
|
| 69 |
+
```
|
| 70 |
+
This launches a CLI loop where you can input math equations (e.g., `123+45=`) and view the model's generated answer.
|
| 71 |
+
|
| 72 |
+
### C. Run Accuracy Evaluations
|
| 73 |
+
To check the exact match accuracy of the saved weights on 100 random math questions:
|
| 74 |
+
```bash
|
| 75 |
+
python eval.py
|
| 76 |
+
```
|