Dan 170M
Dan is a 168M-parameter English chat model built entirely from scratch as a student project: byte-level BPE tokenizer, data pipeline, model code, pretraining and fine-tuning, all written in plain PyTorch. Try it in the Dan Space. Code: github.com/stonbcold/Dan.
It is a small model: it writes fluent English, follows the chat format and often says "I don't know", but it knows few facts and makes mistakes. It is a learning project, not an assistant to rely on.
Architecture
Llama-style decoder-only transformer.
| Parameters | 167.9M (31.5M embedding, 136.4M transformer) |
| Layers | 30 |
| Hidden size | 640 |
| Attention | 10 query heads, 5 key/value heads (GQA), QK-norm |
| Feed-forward | SwiGLU, 1728 |
| Normalization | RMSNorm |
| Positions | RoPE, theta 10000 |
| Context | 2048 tokens |
| Vocabulary | 49,152 (byte-level BPE, one token per digit) |
| Embeddings | tied input/output |
Training
Tokenizer. Byte-level BPE trained on a 100 MB sample of the pretraining mix.
Pretraining. 10B tokens, one run of about 24 hours on a single RTX 5090. Mix: FineWeb-Edu 62%, FineMath-4+ 26%, MegaMath code 12%. Data was deduplicated, filtered and decontaminated against GSM8K, MATH, MATH-500 and HumanEval (13-gram overlap). AdamW (lr 3e-3, betas 0.9/0.95), warmup-stable-decay schedule, batch of 524k tokens, bf16.
Fine-tuning. 2 epochs (about 3 hours on one RTX 5090) on SmolTalk (smol-smoltalk, 458k conversations) plus "I don't know" examples. These were built by asking the pretrained model 20,000 TriviaQA questions 6 times each: questions it answered consistently right became answer examples, questions it got wrong became refusal examples. Loss only on assistant tokens.
Results
| Pretraining validation loss | 2.28 |
| Fine-tuning validation loss (assistant tokens) | 1.18 |
| GSM8K (200 problems, chat, greedy) | 2.5% |
TriviaQA validation, 2,000 unseen questions, greedy:
| Answer | Share |
|---|---|
| Correct | 2.5% |
| Wrong (hallucination) | 28.0% |
| Refusal ("I don't know") | 69.5% |
The refusal behaviour is learned but not well calibrated yet: Dan refuses often, but not only on the questions it does not know.
Use
The weights need the small model code from this repo (model.py, bpe.py, chat.py):
import json, torch
from safetensors.torch import load_file
from model import Dan, DanConfig
from bpe import BPETokenizer
from chat import encode_chat
model = Dan(DanConfig(**json.load(open("config.json"))))
model.load_state_dict(load_file("dan.safetensors"), strict=False)
model.lm_head.weight = model.embed.weight
model.eval()
tok = BPETokenizer()
tok.load("dan_tokenizer.json")
ids, _ = encode_chat(tok, [{"role": "user", "content": "Write a haiku about rain."}], generation=True)
stops = (tok.special["<|endoftext|>"], tok.special["<|im_end|>"])
out = model.generate(torch.tensor([ids]), max_new_tokens=100, temperature=0.3, stop_ids=stops)
print(tok.decode([t for t in out if t not in stops]))
Chat format: <|im_start|>role\n...<|im_end|> with the system prompt
"You are Dan, a small helpful assistant. Answer briefly and honestly."
Limitations
- English only.
- Few facts and frequent hallucinations; check anything important.
- Weak at math (no math fine-tuning yet).
- Not safety-tuned beyond what SmolTalk contains.