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| 8 |
+
- causal-lm
|
| 9 |
+
- from-scratch
|
| 10 |
+
- gqa
|
| 11 |
+
- rope
|
| 12 |
+
- swiglu
|
| 13 |
+
- fim
|
| 14 |
+
- qk-norm
|
| 15 |
+
datasets:
|
| 16 |
+
- nampdn-ai/tiny-codes
|
| 17 |
+
- ise-uiuc/Magicoder-OSS-Instruct-75K
|
| 18 |
+
- iamtarun/python_code_instructions_18k_alpaca
|
| 19 |
+
- bigcode/the-stack-smol
|
| 20 |
+
- flytech/python-codes-25k
|
| 21 |
+
- iamtarun/code_instructions_120k_alpaca
|
| 22 |
+
---
|
| 23 |
+
|
| 24 |
+
# PyCraft-1: A 55M Python Code LLM Trained From Scratch on Consumer Hardware
|
| 25 |
+
|
| 26 |
+
## Model Description
|
| 27 |
+
|
| 28 |
+
PyCraft-1 is a 55.3M parameter decoder-only transformer trained
|
| 29 |
+
**entirely from scratch** on Python code using a single NVIDIA RTX 3050
|
| 30 |
+
laptop GPU (4GB VRAM). It demonstrates that a domain-specific code LLM
|
| 31 |
+
can be trained without cloud compute, following a quality-first data
|
| 32 |
+
curriculum inspired by the Phi-1 "Textbooks Are All You Need" approach.
|
| 33 |
+
|
| 34 |
+
## Architecture
|
| 35 |
+
|
| 36 |
+
| Component | Choice |
|
| 37 |
+
|---|---|
|
| 38 |
+
| Architecture | Decoder-only transformer |
|
| 39 |
+
| Parameters | 55.3M |
|
| 40 |
+
| Attention | Grouped Query Attention (8Q / 2KV heads) |
|
| 41 |
+
| Positional encoding | RoPE |
|
| 42 |
+
| QK-Norm | RMSNorm on Q and K (OLMo 2 / Qwen 3, 2025) |
|
| 43 |
+
| FFN | SwiGLU |
|
| 44 |
+
| Normalisation | RMSNorm pre-norm |
|
| 45 |
+
| Training objective | Causal LM + Fill-in-the-Middle (FIM, 50%) |
|
| 46 |
+
| Context window | 1024 tokens |
|
| 47 |
+
| Vocabulary | 32,000 (custom BPE, Python-tuned) |
|
| 48 |
+
|
| 49 |
+
## Training
|
| 50 |
+
|
| 51 |
+
**Pretraining:**
|
| 52 |
+
- 309,221 curated Python examples from 6 open sources
|
| 53 |
+
- Quality curriculum: scored on 5 heuristics, ordered best-first
|
| 54 |
+
- 4,000 steps | 1.05B tokens | Loss 1.16 | PPL 3.2
|
| 55 |
+
|
| 56 |
+
**Supervised Fine-Tuning:**
|
| 57 |
+
- Magicoder-OSS-Instruct-75K (Python subset, 40k examples)
|
| 58 |
+
- 400 steps | Loss 1.15 | PPL 3.15
|
| 59 |
+
|
| 60 |
+
**Hardware:** NVIDIA RTX 3050 Laptop GPU 4GB, Ryzen 7 6000, 16GB RAM
|
| 61 |
+
**Total training time:** ~22 hours
|
| 62 |
+
|
| 63 |
+
## Novel Contributions
|
| 64 |
+
|
| 65 |
+
1. **Quality-first data curriculum** — 309k Python examples scored and
|
| 66 |
+
ordered by educational value (docstrings, type hints, comments,
|
| 67 |
+
naming conventions, length). Validates Phi-1 hypothesis at the
|
| 68 |
+
resource-constrained regime.
|
| 69 |
+
|
| 70 |
+
2. **QK-Norm** — RMSNorm applied to Q and K before RoPE, adopted from
|
| 71 |
+
OLMo 2 and Qwen 3 (2025), improving training stability in small models.
|
| 72 |
+
|
| 73 |
+
3. **FIM pretraining on 4GB VRAM** — Fill-in-the-Middle objective using
|
| 74 |
+
PSM format trained on a consumer GPU via gradient checkpointing and
|
| 75 |
+
BF16 mixed precision.
|
| 76 |
+
|
| 77 |
+
4. **Full reproducibility** — complete open-source pipeline runnable on
|
| 78 |
+
consumer hardware in under one week.
|
| 79 |
+
|
| 80 |
+
## Evaluation
|
| 81 |
+
|
| 82 |
+
| Metric | Value |
|
| 83 |
+
|---|---|
|
| 84 |
+
| Pretraining loss | 1.16 |
|
| 85 |
+
| Pretraining PPL | 3.20 |
|
| 86 |
+
| SFT loss | 1.15 |
|
| 87 |
+
| SFT PPL | 3.15 |
|
| 88 |
+
| Held-out PPL (binary search) | 1.4 |
|
| 89 |
+
| Held-out PPL (Stack class) | 1.7 |
|
| 90 |
+
| Held-out PPL (average, 5 samples) | 2.16 |
|
| 91 |
+
|
| 92 |
+
## Example Usage
|
| 93 |
+
|
| 94 |
+
```python
|
| 95 |
+
# Clone the repo and install dependencies first
|
| 96 |
+
# pip install torch safetensors tokenizers datasets
|
| 97 |
+
|
| 98 |
+
import torch
|
| 99 |
+
from safetensors.torch import load_file
|
| 100 |
+
from model.config import get_config_120m
|
| 101 |
+
from model.pycraft_model import PyCraftModel
|
| 102 |
+
from tokenizer.tokenizer_utils import PyCraftTokenizer
|
| 103 |
+
|
| 104 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 105 |
+
tokenizer = PyCraftTokenizer("tokenizer/vocab/tokenizer.json")
|
| 106 |
+
|
| 107 |
+
cfg = get_config_120m()
|
| 108 |
+
cfg.vocab_size = 32000
|
| 109 |
+
cfg.dropout = 0.0
|
| 110 |
+
|
| 111 |
+
model = PyCraftModel(cfg).to(device)
|
| 112 |
+
model.load_state_dict(load_file("model.safetensors", device=device))
|
| 113 |
+
model.eval()
|
| 114 |
+
|
| 115 |
+
prompt = "def is_palindrome(s: str) -> bool:\n "
|
| 116 |
+
ids = tokenizer.encode(prompt)
|
| 117 |
+
inp = torch.tensor(ids, dtype=torch.long).unsqueeze(0).to(device)
|
| 118 |
+
|
| 119 |
+
with torch.no_grad():
|
| 120 |
+
out = model.generate(inp, max_new_tokens=80, temperature=0.7, top_k=40)
|
| 121 |
+
|
| 122 |
+
print(tokenizer.decode(out[0].tolist()))
|
| 123 |
+
```
|
| 124 |
+
|
| 125 |
+
## Limitations
|
| 126 |
+
|
| 127 |
+
- 55M parameters: generates plausible Python but may produce logical errors
|
| 128 |
+
- Context window: 1024 tokens
|
| 129 |
+
- Best on standard algorithms, data structures, and common library patterns
|
| 130 |
+
- Not a conversational assistant
|
| 131 |
+
|
| 132 |
+
## Citation
|
| 133 |
+
|
| 134 |
+
```bibtex
|
| 135 |
+
@misc{inamdar2026pycraft,
|
| 136 |
+
title={PyCraft-1: Training a Python Code LLM From Scratch on Consumer Hardware},
|
| 137 |
+
author={Inamdar, Rohan},
|
| 138 |
+
year={2026},
|
| 139 |
+
institution={University of Manchester, MSc Artificial Intelligence},
|
| 140 |
+
}
|
| 141 |
+
```
|
| 142 |
+
|
| 143 |
+
## Links
|
| 144 |
+
|
| 145 |
+
- GitHub: https://github.com/irohan0/pycraft-llm
|
| 146 |
+
- Author: https://www.linkedin.com/in/rohan-inamdar-47aa4b251
|