major-chat 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- major_chat-0.1.0/PKG-INFO +34 -0
- major_chat-0.1.0/README.md +204 -0
- major_chat-0.1.0/major_chat/README.md +19 -0
- major_chat-0.1.0/major_chat/__init__.py +2 -0
- major_chat-0.1.0/major_chat/__main__.py +3 -0
- major_chat-0.1.0/major_chat/app.py +499 -0
- major_chat-0.1.0/major_chat/cli.py +104 -0
- major_chat-0.1.0/major_chat/client.py +165 -0
- major_chat-0.1.0/major_chat/config.py +82 -0
- major_chat-0.1.0/major_chat/themes.py +24 -0
- major_chat-0.1.0/major_chat.egg-info/PKG-INFO +34 -0
- major_chat-0.1.0/major_chat.egg-info/SOURCES.txt +23 -0
- major_chat-0.1.0/major_chat.egg-info/dependency_links.txt +1 -0
- major_chat-0.1.0/major_chat.egg-info/entry_points.txt +2 -0
- major_chat-0.1.0/major_chat.egg-info/requires.txt +1 -0
- major_chat-0.1.0/major_chat.egg-info/top_level.txt +1 -0
- major_chat-0.1.0/pyproject.toml +28 -0
- major_chat-0.1.0/setup.cfg +4 -0
- major_chat-0.1.0/tests/test_accounts.py +61 -0
- major_chat-0.1.0/tests/test_chat_feed.py +35 -0
- major_chat-0.1.0/tests/test_chat_parity.py +51 -0
- major_chat-0.1.0/tests/test_layers.py +98 -0
- major_chat-0.1.0/tests/test_overfit.py +19 -0
- major_chat-0.1.0/tests/test_quant_export.py +83 -0
- major_chat-0.1.0/tests/test_resume.py +93 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: major-chat
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Full-screen terminal chat for Major, a tiny language model (sign-in required).
|
|
5
|
+
License-Expression: GPL-3.0-or-later
|
|
6
|
+
Project-URL: Repository, https://github.com/yixuanxu-ai/major-1
|
|
7
|
+
Keywords: llm,chat,tui,terminal,tiny-model
|
|
8
|
+
Classifier: Environment :: Console
|
|
9
|
+
Classifier: Intended Audience :: End Users/Desktop
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Topic :: Communications :: Chat
|
|
12
|
+
Requires-Python: >=3.9
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
Requires-Dist: prompt_toolkit>=3.0.40
|
|
15
|
+
|
|
16
|
+
# major-chat
|
|
17
|
+
|
|
18
|
+
A full-screen terminal chat for **Major**, a very small language model.
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
pip install major-chat
|
|
22
|
+
major-chat
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
You need an account to use it. On first run it asks you to sign in or create one (username and password, no email), then opens the chat.
|
|
26
|
+
Chats are saved to your account, so `/resume` brings them back, including on another machine.
|
|
27
|
+
|
|
28
|
+
- Type `/` to see the commands; use the arrow keys to choose, Enter to run.
|
|
29
|
+
- `/model` choose the model (one today: `major-1`), `/theme` six colour themes (default `noir`), `/temp` and `/length` reply settings, `/resume` saved chats, `/new`, `/whoami`, `/logout`, `/quit`.
|
|
30
|
+
- Up and down scroll the conversation; `ctrl+p` and `ctrl+n` bring back earlier prompts; `ctrl+c` stops a reply.
|
|
31
|
+
- The window can be resized at any time.
|
|
32
|
+
|
|
33
|
+
The chat runs on a Major server. `major-chat` finds the current server link on its own; use `--api https://…` to point at a different one,
|
|
34
|
+
and `major-chat logout` to forget the saved sign-in. Major is a 43M-parameter model and gets facts wrong. It is an AI and can make mistakes.
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
# Major: a tiny chat model for tiny hardware
|
|
2
|
+
|
|
3
|
+
Major is a 43M-parameter English chat model with the exact **Needle 2** architecture (Hadamard MLP, GQA + RoPE, engram memory, 4-lane
|
|
4
|
+
hyper-connections, tied 8192-token embedding, 27 x 512), trained from random initialisation with quantisation-aware training. At 2 bits per weight
|
|
5
|
+
the file is 13.2 MB, so it runs on an ESP32-S3: weights memory-mapped from flash, KV cache in PSRAM (7.65 MB of a 7.86 MB budget with the 512-token window).
|
|
6
|
+
|
|
7
|
+
It is good at short friendly chat and weak at facts and arithmetic. Read **`docs/WHITEPAPER.md`** (also on the website at `/paper/`) for what it can and cannot do, with every number and its caveats.
|
|
8
|
+
|
|
9
|
+
| | |
|
|
10
|
+
|---|---|
|
|
11
|
+
| Chat in a terminal | `pip install major-chat` then `major-chat` (account required), or `./install_tui.sh` from this checkout |
|
|
12
|
+
| Website | landing page, chat with accounts, API docs, paper: `web/` (plain HTML/CSS/JS), served by `web/server.py` |
|
|
13
|
+
| API | OpenAI-compatible `POST /api/v1/chat/completions`, `GET /api/v1/models` (docs at `/api/`) |
|
|
14
|
+
| Model files | not published yet: `models/README.md` |
|
|
15
|
+
| Notes and every experiment | `docs/NOTES.md`, `docs/EXPERIMENTS.md`; fixed prompts, tests and grades in `evals/` |
|
|
16
|
+
|
|
17
|
+
## Layout
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
major/ PyTorch model, CQ quantisation, .cact I/O, data, training, eval, export
|
|
21
|
+
model.py Needle 2 SAN (op-for-op with the C engine)
|
|
22
|
+
cq.py Cactus-Quants (bit-exact with the exporter) + QAT
|
|
23
|
+
cact.py .cact v3 reader/writer
|
|
24
|
+
data.py streaming prep, filters, dedup, hold-out, resumable loaders
|
|
25
|
+
train.py pretrain / SFT loop, QAT, WSD/cosine, atomic checkpoints, resume
|
|
26
|
+
sample.py chat generation mirroring the engine's sampler
|
|
27
|
+
evals.py val loss per source (FP + Q2), 100 prompts, repetition rate
|
|
28
|
+
export.py checkpoint -> .cact, then parity + board-budget verification
|
|
29
|
+
engine/ C99 engine (from needle-2-esp32) + nd_chat.c free-text chat mode
|
|
30
|
+
host/ host CLIs: nd_dump (logits), nd_chat (chat + ESP32-S3 budget harness)
|
|
31
|
+
firmware/major_chat/ ESP-IDF app for the board (serial chat console)
|
|
32
|
+
sim/run_qemu.sh real firmware in Espressif QEMU (16 MB flash, 8 MB octal PSRAM)
|
|
33
|
+
tools/ parity vs released Needle 2, board client, flash script, references
|
|
34
|
+
modal_app.py Modal: data prep (CPU), benchmark, training, evaluation (GPU)
|
|
35
|
+
tests/ unit, quantisation/export, engine parity, chat parity, resume, overfit
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Setup
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
python3 -m venv --without-pip --system-site-packages .venv # or a normal venv
|
|
42
|
+
.venv/bin/python -m pip install torch numpy sentencepiece datasets zstandard pytest cmake ninja modal
|
|
43
|
+
# released Needle 2 assets (tokenizer is reused; weights only for the parity test)
|
|
44
|
+
mkdir -p assets/needle2/tokenizer && for f in needle2.cact config.json LICENSE tokenizer/tokenizer.model tokenizer/tokenizer.vocab; do
|
|
45
|
+
curl -sL -o assets/needle2/$f https://huggingface.co/Cactus-Compute/needle2/resolve/main/$f; done
|
|
46
|
+
export PATH=$PWD/.venv/bin:$PATH
|
|
47
|
+
cmake -S host -B build/host -DCMAKE_BUILD_TYPE=Release && cmake --build build/host -j
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Test (local, no GPU)
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
.venv/bin/python -m pytest -q tests # 19 tests
|
|
54
|
+
.venv/bin/python tools/parity_needle2.py assets/needle2/needle2.cact build/host/nd_dump
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Covers:
|
|
58
|
+
- every layer
|
|
59
|
+
- CQ bit-exactness against the released codebook
|
|
60
|
+
- tokenizer against the engine
|
|
61
|
+
- the `.cact` round trip
|
|
62
|
+
- PyTorch Q2 forward against the C engine
|
|
63
|
+
- PyTorch chat generation against the engine chat mode (greedy and sampled)
|
|
64
|
+
- kill/resume, which is bit-exact
|
|
65
|
+
- single-batch overfit
|
|
66
|
+
|
|
67
|
+
## Data (Modal CPU workers, streamed, tokenized ahead into Volume shards)
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
.venv/bin/python -m modal run modal_app.py::prep_all --total-tokens 3e9
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Train
|
|
74
|
+
|
|
75
|
+
All runs resume automatically from `latest.pt` on Modal timeout, preemption
|
|
76
|
+
or retry. Checkpoints go to the `major-vol` Volume: `latest.pt` every 20 min,
|
|
77
|
+
`epoch_NNN.pt` every `--epoch-tokens`, `best.pt`, and `predecay.pt` (before
|
|
78
|
+
the WSD cooldown).
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
# throughput / cost benchmark
|
|
82
|
+
.venv/bin/python -m modal run modal_app.py::bench_run --gpu H100 --preset needle2 --batch 128 --compile
|
|
83
|
+
|
|
84
|
+
# pretraining (exact command used: runs/m45_pt.args)
|
|
85
|
+
.venv/bin/python -m modal run --detach modal_app.py::train_run --gpu H100 --timeout-h 3 \
|
|
86
|
+
--args "$(cat runs/m45_pt.args)"
|
|
87
|
+
|
|
88
|
+
# resume explicitly
|
|
89
|
+
... --args "$(cat runs/m45_pt.args) --resume latest" # or --resume epoch=2 / best / predecay path
|
|
90
|
+
|
|
91
|
+
# continue with more budget: resume from before the cooldown with a larger total
|
|
92
|
+
... --args "$(cat runs/m45_pt.args | sed 's/--total-tokens 1.1e9/--total-tokens 3e9/') \
|
|
93
|
+
--resume /vol/runs/m45_pt/predecay.pt --override-total"
|
|
94
|
+
|
|
95
|
+
# chat SFT (exact command: runs/m45_sft.args)
|
|
96
|
+
.venv/bin/python -m modal run --detach modal_app.py::train_run --gpu H100 --args "$(cat runs/m45_sft.args)"
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Local CPU training works the same way, e.g. `python -m major.train --preset tiny --data runs/data_local --run runs/x --total-tokens 3e4 --batch 4 --seq-len 128 --dtype fp32`.
|
|
100
|
+
|
|
101
|
+
## Evaluate
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
.venv/bin/python -m modal run modal_app.py::eval_run --run m45_sft --ckpt latest --gpu H100
|
|
105
|
+
# or locally: python -m major.evals --run runs/m45_sft --ckpt latest --data runs/data_local
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Writes `val.json` (per source, FP and Q2), `replies.jsonl`/`replies.md` (100
|
|
109
|
+
prompts, FP and Q2 replies) and `summary.json` (repetition rate, runaway rate).
|
|
110
|
+
|
|
111
|
+
## Export
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
modal volume get major-vol runs/m45_sft/latest.pt runs/m45_sft/latest.pt
|
|
115
|
+
.venv/bin/python -m major.export --ckpt runs/m45_sft/latest.pt --out major.cact
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
The export fails unless all of these hold:
|
|
119
|
+
- the file fits the 0xD20000-byte model partition
|
|
120
|
+
- engine logits match the PyTorch Q2 forward (relative error < 2e-3, identical argmax)
|
|
121
|
+
- a chat turn runs within the board budgets
|
|
122
|
+
|
|
123
|
+
## Chat on the host (same engine as the board)
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
./build/host/nd_chat major.cact --board # interactive; /reset
|
|
127
|
+
./build/host/nd_chat major.cact --board --prompts evals/prompts_100.txt --out runs/engine_replies.jsonl
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
`--board` enforces the XIAO budgets and exits non-zero on any overrun:
|
|
131
|
+
- flash 0xD20000 (13.76 MB)
|
|
132
|
+
- PSRAM 7.5 MB (8 MB minus 512 KB for IDF)
|
|
133
|
+
- internal SRAM 320 KB
|
|
134
|
+
- 16 KB stack, with a guard page
|
|
135
|
+
|
|
136
|
+
It reports peak PSRAM, SRAM and stack.
|
|
137
|
+
|
|
138
|
+
## Simulate (real firmware in QEMU)
|
|
139
|
+
|
|
140
|
+
```bash
|
|
141
|
+
. ~/esp/esp-idf/export.sh # ESP-IDF v5.5.6
|
|
142
|
+
cd firmware/major_chat && idf.py set-target esp32s3 build && cd ../..
|
|
143
|
+
sim/run_qemu.sh major.cact & # serial console on localhost:5555
|
|
144
|
+
.venv/bin/python tools/board.py --tcp localhost:5555 --max-new 16 --prompts evals/prompts_100.txt --limit 2 --out runs/qemu.json
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
QEMU is not cycle-accurate. Its timings are **not** device speed; see
|
|
148
|
+
`NOTES.md` for what is and isn't emulated.
|
|
149
|
+
|
|
150
|
+
## Flash and benchmark the real board
|
|
151
|
+
|
|
152
|
+
```bash
|
|
153
|
+
. ~/esp/esp-idf/export.sh
|
|
154
|
+
tools/flash_board.sh /dev/ttyACM0 major.cact # build, flash app + model, run benchmark
|
|
155
|
+
.venv/bin/python tools/board.py --port /dev/ttyACM0 # then chat in any serial terminal at 115200
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
The benchmark writes `runs/board_bench.json`: measured ms/token, heap and
|
|
159
|
+
stack high-water marks, and replies to the first 10 prompts.
|
|
160
|
+
|
|
161
|
+
**Estimated** device speed (not measured): ~530 ms/token. The architecture,
|
|
162
|
+
tensor shapes and bit widths are identical to Needle 2, which the reference
|
|
163
|
+
port measured at 534 ms/token on this chip at 240 MHz. A 20-token prompt plus a
|
|
164
|
+
40-token reply is therefore about 30 s.
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
## Website and server
|
|
168
|
+
|
|
169
|
+
`web/` is a static site (landing page, chat, API docs, paper) and `web/server.py` (FastAPI) serves it and streams replies from the C engine
|
|
170
|
+
(`nd_serve --threads 2`, one generation at a time). Run `python web/server.py` (127.0.0.1:8088). All URLs in the pages are relative, so it also works behind a reverse
|
|
171
|
+
proxy under a path prefix (the prefix may be stripped). The server speaks plain HTTP: put it behind your own TLS and access control before exposing it.
|
|
172
|
+
|
|
173
|
+
- **Models.** `major-1` (the chip model, 512-token window) and `king-1-preview` (same architecture, 1,024-token window, too big for the chip, so server only; it appears only if `models/king-1-preview.cact` exists on the server). Older ids (`major-v2` and so on) are accepted and mapped to `major-1`. Replies are capped at 320 tokens (default 256).
|
|
174
|
+
- **Accounts.** Sign up with a username and password (no email, no recovery). Chats sync to the account; guests keep chats in the browser. Accounts live in
|
|
175
|
+
a SQLite file (`var/accounts.db`, git-ignored): scrypt password hashes, bearer tokens stored only as hashes, failed logins rate-limited, chats size-capped.
|
|
176
|
+
- **The current link.** `web/config.json` holds the backend URL. The static site (`tools/build_site.py` builds `site/` for GitHub Pages; the workflow in `.github/workflows/pages.yml` stays dormant until
|
|
177
|
+
you set the repo variable `PAGES_ENABLED=true`) and the terminal chat both read it, so changing one file re-points them. A static site can only chat if the backend at that link is reachable from the visitor's browser. CORS allows `https://yixuanxu-ai.github.io` (set `MAJOR_CORS` to change it).
|
|
178
|
+
- **API.** OpenAI-compatible and stateless, streaming or not. Replies that show their working carry a `reasoning` field next to `content`.
|
|
179
|
+
|
|
180
|
+
```
|
|
181
|
+
curl $BASE/api/v1/chat/completions -H 'Content-Type: application/json' \
|
|
182
|
+
-d '{"model":"major-1","messages":[{"role":"user","content":"whats the capital of france"}]}'
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
Tests: `pytest tests/test_accounts.py` (accounts) plus the model tests above. The logo set is traced from `brand/major-1.png` (`tools/trace_logo.py`, `tools/render_icons.py`).
|
|
186
|
+
|
|
187
|
+
## Terminal chat (`major-chat`)
|
|
188
|
+
|
|
189
|
+
A full-screen app (it redraws correctly when the window is resized) that needs an account.
|
|
190
|
+
|
|
191
|
+
```
|
|
192
|
+
pip install major-chat # hosted server only
|
|
193
|
+
major-chat # first run: sign in or create an account
|
|
194
|
+
./install_tui.sh # from a checkout; with models/major-1.cact present, `major-chat --local` runs the engine offline
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
Type `/` for the command palette and use the arrow keys: `/model` (`major-1`, and the King-1 Preview when the server has it), `/theme` (six colour themes, default `noir`), `/temp` and `/length` (reply settings), `/resume` (saved chats), `/new`, `/whoami`, `/logout`, `/quit`. Up and down scroll the chat, `ctrl+p` and `ctrl+n` recall earlier prompts, `ctrl+c` stops a reply. Major has no reasoning-effort control, so there is no `/effort`.
|
|
198
|
+
Replies that show their working are drawn with a dim "reasoning" line above the answer. Publish a new release: `python -m build && python -m twine upload dist/*`
|
|
199
|
+
(the package is `major_chat/`, config in `pyproject.toml`).
|
|
200
|
+
|
|
201
|
+
## Benchmarks against other small models
|
|
202
|
+
|
|
203
|
+
`tools/bench_lm.py` scores any model zero-shot on ARC-Easy, ARC-Challenge, PIQA and HellaSwag with one protocol (answer log-likelihood, length-normalised, full test sets);
|
|
204
|
+
`modal_app.py::bench_models` runs it on a GPU and `tools/make_chart.py` draws the chart in the paper. Major is scored with its deployed numerics (2-bit weights, int8 KV).
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# major-chat
|
|
2
|
+
|
|
3
|
+
A full-screen terminal chat for **Major**, a very small language model.
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
pip install major-chat
|
|
7
|
+
major-chat
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
You need an account to use it. On first run it asks you to sign in or create one (username and password, no email), then opens the chat.
|
|
11
|
+
Chats are saved to your account, so `/resume` brings them back, including on another machine.
|
|
12
|
+
|
|
13
|
+
- Type `/` to see the commands; use the arrow keys to choose, Enter to run.
|
|
14
|
+
- `/model` choose the model (one today: `major-1`), `/theme` six colour themes (default `noir`), `/temp` and `/length` reply settings, `/resume` saved chats, `/new`, `/whoami`, `/logout`, `/quit`.
|
|
15
|
+
- Up and down scroll the conversation; `ctrl+p` and `ctrl+n` bring back earlier prompts; `ctrl+c` stops a reply.
|
|
16
|
+
- The window can be resized at any time.
|
|
17
|
+
|
|
18
|
+
The chat runs on a Major server. `major-chat` finds the current server link on its own; use `--api https://…` to point at a different one,
|
|
19
|
+
and `major-chat logout` to forget the saved sign-in. Major is a 43M-parameter model and gets facts wrong. It is an AI and can make mistakes.
|