File size: 3,859 Bytes
2617e05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
---
license: mit
library_name: tiny-dlms
pipeline_tag: reinforcement-learning
tags:
- tiny-dlms
- decision-model
- from-scratch
- pytorch
- imitation-learning
- vision-transformer
- CarRacing-v3
---

# tiny-dlms-melon-car: Mēlon 4x128 (car)

Mēlon (μῆλον, the golden apple) is a tiny decision model that looks at an **image** and picks one option written in words. This is Mēlon driving CarRacing from pixels. The options are scored all at once and a softmax gives one probability each; nothing is generated.

Written and trained from scratch in PyTorch (no Hugging Face `transformers`): tokenizer, Transformer, training.
Code, all the other checkpoints and the full benchmark: [GitHub](https://github.com/benisalla/tiny-dlms) · [project page](https://benisalla.github.io/tiny-dlms/).
A learning project, not a production model.

## Usage

```bash
pip install "tiny-dlm[hub] @ git+https://github.com/benisalla/tiny-dlms" "gymnasium[box2d]"
```

```python
import torch
from tiny_dlm.car import OPTION_ACTIONS, make_env, preprocess
from tiny_dlm.hub import from_pretrained
from tiny_dlm.vision import encode_options

model, tokenizer, config = from_pretrained("liamsi/tiny-dlms-melon-car")
idx = encode_options(config["option_texts"], tokenizer)[None]          # the 15 driving options
mask = torch.ones(1, idx.size(1), dtype=torch.bool)
env = make_env()                                                     # CarRacing-v3
obs, _ = env.reset(seed=0)
for _ in range(1000):
    choice, probs = model.decide(preprocess(obs)[None], idx, mask)  # 96x96 frame -> 48x48 -> one option
    obs, reward, done, truncated, _ = env.step(OPTION_ACTIONS[choice.item()])
    if done or truncated:
        break
```

Or with the repo: `python scripts/car_drive.py --ckpt <checkpoint>` records a video.

## Model

| | |
|---|---|
| Layers x width | 4 x 128 (4 heads) + 1 layer where the options look at each other |
| Input | 48x48 colour image, 36 patches of 8x8 |
| Blocks | RMSNorm, RoPE, SwiGLU |
| Weights | 1.34 M |
| Training steps | 4,000 |
| Options | steer full left and accelerate · steer full left and coast · steer medium left and accelerate · steer medium left and coast · steer slight left and accelerate · steer slight left and coast · go straight and accelerate · go straight and coast · steer slight right and accelerate · steer slight right and coast · steer medium right and accelerate · steer medium right and coast · steer full right and accelerate · steer full right and coast · brake |

## Training

Imitation: it copies a pure-pursuit teacher that drove 45 tracks (frames recorded with `scripts/car_record.py`).

## Results

| Driver (5 unseen tracks, CarRacing-v3) | Mean score |
|---|---|
| **this model** | **913** |
| the teacher (pure pursuit) | 913 |
| always straight | -26 |
| random | -40 |

## The tiny-dlms models

| Model | |
|---|---|
| [tiny-dlms-rhoa](https://huggingface.co/liamsi/tiny-dlms-rhoa) | the best Rhoa: pre-trained on 2 GB of web text, then fine-tuned |
| [tiny-dlms-rhoa-small](https://huggingface.co/liamsi/tiny-dlms-rhoa-small) | the small Rhoa, pre-trained on 2 GB of web text, then fine-tuned |
| [tiny-dlms-rhoa-gpt2](https://huggingface.co/liamsi/tiny-dlms-rhoa-gpt2) | Rhoa with GPT-2 blocks (LayerNorm, learned positions, GELU), pre-trained on 2 GB of web text |
| [tiny-dlms-rhoa-base](https://huggingface.co/liamsi/tiny-dlms-rhoa-base) | the pre-trained Rhoa before fine-tuning: masked-token prediction on 2 GB of web text |
| [tiny-dlms-melon-car](https://huggingface.co/liamsi/tiny-dlms-melon-car) | Mēlon driving CarRacing from pixels |
| [tiny-dlms-melon-car-small](https://huggingface.co/liamsi/tiny-dlms-melon-car-small) | the small Mēlon driving CarRacing from pixels |
| [tiny-dlms-melon-maze](https://huggingface.co/liamsi/tiny-dlms-melon-maze) | Mēlon solving mazes from pixels |