Upload README.md with huggingface_hub
Browse files
README.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Binary Looped (Internal-Reasoning) Transformer–Mamba Hybrid
|
| 2 |
+
|
| 3 |
+
A small, live-trained language model built as a demonstrator in the spirit of
|
| 4 |
+
Scott's AGILLM experiments: a **binary-weight, weight-shared looped
|
| 5 |
+
transformer** hybridised with **Mamba-style selective state-space blocks**,
|
| 6 |
+
trained from scratch on text **streamed live from HuggingFace** (no corpus is
|
| 7 |
+
downloaded).
|
| 8 |
+
|
| 9 |
+
- **3,450,240 parameters** total, of which **2,359,296 are binary-latent**
|
| 10 |
+
(the linear layers inside the looped transformer blocks).
|
| 11 |
+
- Byte-level LM: vocabulary = 256 raw UTF-8 bytes, no trained tokenizer.
|
| 12 |
+
- `d_model` 256, sequence length 256, batch 16, AdamW lr 3e-3 (cosine).
|
| 13 |
+
|
| 14 |
+
## Architecture (`model.py`)
|
| 15 |
+
|
| 16 |
+
The stack alternates two block types, 3 of each:
|
| 17 |
+
|
| 18 |
+
- **SSMBlock** — a simplified Mamba-1-style *selective* SSM written in pure
|
| 19 |
+
PyTorch (no `mamba-ssm` package, no custom CUDA kernels). Δ, B and C are
|
| 20 |
+
input-dependent ("selective"); A is a learned diagonal matrix (S4D-style
|
| 21 |
+
init). The recurrence `h_t = a_t · h_{t-1} + u_t` is evaluated **exactly**
|
| 22 |
+
with a parallel associative (prefix) scan. Depthwise causal conv + SiLU
|
| 23 |
+
gating, as in Mamba.
|
| 24 |
+
- **LoopedTransformerBlock** — ONE transformer block (8 heads, MLP ratio 4)
|
| 25 |
+
whose weights are shared and applied **R = 3 times** in a row per stack
|
| 26 |
+
position — the "internal reasoning" loop. Every linear layer inside it is a
|
| 27 |
+
**BinaryLinear**: the forward pass uses `W_eff = α · sign(W)` with per-row
|
| 28 |
+
scale `α = mean(|W|)` (genuinely 1-bit weights plus a scale), while
|
| 29 |
+
gradients reach the latent full-precision weights through a
|
| 30 |
+
straight-through estimator (STE).
|
| 31 |
+
|
| 32 |
+
Embeddings (tied with the LM head), the SSM projections and the output head
|
| 33 |
+
stay full precision.
|
| 34 |
+
|
| 35 |
+
## Files
|
| 36 |
+
|
| 37 |
+
| File | What it is |
|
| 38 |
+
|---|---|
|
| 39 |
+
| `model.py` | The hybrid model: `BinaryLinear`, `SSMBlock`, `LoopedTransformerBlock`, `BinaryLoopHybridLM` |
|
| 40 |
+
| `train.py` | Training loop: HF streaming data, AdamW, fp16 autocast, atomic `state.json` + `history.jsonl` logging, checkpoints every 1,000 steps to `ckpt_last.pt`, clean exit when a `STOP` file appears |
|
| 41 |
+
| `dashboard.py` | The GUI: a stdlib-only web monitor (live loss curve, step / loss / EMA / tokens-per-second / GPU memory, latest greedy sample). Endpoints `/`, `/state`, `/history` |
|
| 42 |
+
| `ckpt_step3000.pt` | Checkpoint snapshot from the live run (see below) |
|
| 43 |
+
|
| 44 |
+
A second GUI variant — a Tkinter desktop monitor that polls the same
|
| 45 |
+
`state.json` — was used when the model first ran on Scott's laptop (MSI GP65
|
| 46 |
+
Leopard); the web dashboard in this repo is the canonical monitor for the
|
| 47 |
+
current run.
|
| 48 |
+
|
| 49 |
+
## Train it
|
| 50 |
+
|
| 51 |
+
```bash
|
| 52 |
+
pip install torch datasets
|
| 53 |
+
python3 train.py
|
| 54 |
+
```
|
| 55 |
+
|
| 56 |
+
Data is streamed with `datasets` `streaming=True`, trying in order:
|
| 57 |
+
`roneneldan/TinyStories`, then `Salesforce/wikitext` (103, then 2) as
|
| 58 |
+
fallbacks. Training runs on CUDA if available, else CPU. Create a file named
|
| 59 |
+
`STOP` in the folder to stop cleanly (a final checkpoint is saved).
|
| 60 |
+
|
| 61 |
+
## Run the monitor (GUI)
|
| 62 |
+
|
| 63 |
+
```bash
|
| 64 |
+
python3 dashboard.py 8080 # then open http://localhost:8080/
|
| 65 |
+
```
|
| 66 |
+
|
| 67 |
+
The dashboard reads `state.json` / `history.jsonl` written by the trainer
|
| 68 |
+
and auto-refreshes every 2 seconds.
|
| 69 |
+
|
| 70 |
+
## Checkpoint
|
| 71 |
+
|
| 72 |
+
`ckpt_step3000.pt` — a snapshot of `ckpt_last.pt` taken from the live run
|
| 73 |
+
without interrupting it (verified: it loads and its SHA-256 matches the
|
| 74 |
+
source file on the training box).
|
| 75 |
+
|
| 76 |
+
- **Step:** 3,000
|
| 77 |
+
- **Loss at step 3,000:** 0.8828 (EMA loss 1.0109)
|
| 78 |
+
- **Size:** 13,819,708 bytes
|
| 79 |
+
- **SHA-256:** `a28a3e1ca81a01f97e8a6f74e37220490dd060f7b5fdef4e0d8a35d26a447c0b`
|
| 80 |
+
- **Contents:** `{"step": 3000, "model": <state_dict>, "cfg": <training config>}`
|
| 81 |
+
- **Run it came from:** live training on an RTX 2080 Ti (Vast.ai instance
|
| 82 |
+
61918457), streaming `roneneldan/TinyStories`, ~23,500 tokens/s. The run
|
| 83 |
+
continued past this snapshot.
|
| 84 |
+
|
| 85 |
+
Load it:
|
| 86 |
+
|
| 87 |
+
```python
|
| 88 |
+
import torch
|
| 89 |
+
from model import BinaryLoopHybridLM
|
| 90 |
+
|
| 91 |
+
ckpt = torch.load("ckpt_step3000.pt", map_location="cpu", weights_only=False)
|
| 92 |
+
cfg = ckpt["cfg"]
|
| 93 |
+
model = BinaryLoopHybridLM(d_model=cfg["d_model"], seq_len=cfg["seq_len"],
|
| 94 |
+
n_ssm=cfg["n_ssm"], n_loop_blocks=cfg["n_loop_blocks"],
|
| 95 |
+
loops=cfg["loops"], n_head=cfg["n_head"],
|
| 96 |
+
d_inner=cfg["d_inner"], d_state=cfg["d_state"])
|
| 97 |
+
model.load_state_dict(ckpt["model"])
|
| 98 |
+
```
|
| 99 |
+
|
| 100 |
+
## Honest scope
|
| 101 |
+
|
| 102 |
+
This is a 3.45M-parameter demonstrator, not a frontier model: it shows the
|
| 103 |
+
binary + looped + SSM-hybrid mechanics training stably end-to-end on streamed
|
| 104 |
+
data, with samples improving from word salad toward simple TinyStories-style
|
| 105 |
+
sentences within a few thousand steps.
|