bantamtrain 0.1.0__tar.gz → 0.3.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.
- bantamtrain-0.3.0/MANIFEST.in +4 -0
- bantamtrain-0.3.0/PKG-INFO +326 -0
- bantamtrain-0.3.0/README.md +939 -0
- bantamtrain-0.3.0/bantamtrain/__init__.py +27 -0
- bantamtrain-0.3.0/bantamtrain/__main__.py +5 -0
- bantamtrain-0.3.0/bantamtrain/cli/__init__.py +111 -0
- bantamtrain-0.3.0/bantamtrain/cli/chat.py +245 -0
- bantamtrain-0.3.0/bantamtrain/cli/doctor.py +177 -0
- bantamtrain-0.3.0/bantamtrain/cli/generate.py +90 -0
- bantamtrain-0.3.0/bantamtrain/cli/pack_era5.py +65 -0
- bantamtrain-0.3.0/bantamtrain/cli/pack_text.py +162 -0
- bantamtrain-0.3.0/bantamtrain/cli/prepare_era5.py +145 -0
- bantamtrain-0.3.0/bantamtrain/cli/prepare_text.py +408 -0
- bantamtrain-0.3.0/bantamtrain/cli/pretrain.py +765 -0
- bantamtrain-0.3.0/bantamtrain/cli/recipes.py +63 -0
- bantamtrain-0.3.0/bantamtrain/cli/weather.py +769 -0
- bantamtrain-0.3.0/bantamtrain/data/__init__.py +31 -0
- bantamtrain-0.3.0/bantamtrain/data/dedup.py +369 -0
- bantamtrain-0.3.0/bantamtrain/data/gridpack.py +311 -0
- bantamtrain-0.3.0/bantamtrain/data/tokpack.py +634 -0
- {bantamtrain-0.1.0 → bantamtrain-0.3.0}/bantamtrain/device.py +107 -28
- bantamtrain-0.3.0/bantamtrain/finetune.py +1330 -0
- bantamtrain-0.3.0/bantamtrain/lora.py +359 -0
- bantamtrain-0.3.0/bantamtrain/losses.py +65 -0
- bantamtrain-0.3.0/bantamtrain/lowmem.py +268 -0
- bantamtrain-0.3.0/bantamtrain/memory.py +391 -0
- bantamtrain-0.3.0/bantamtrain/models/gpt.py +498 -0
- {bantamtrain-0.1.0 → bantamtrain-0.3.0}/bantamtrain/models/vit_weather.py +41 -4
- bantamtrain-0.3.0/bantamtrain/optim.py +689 -0
- bantamtrain-0.3.0/bantamtrain/qweights.py +682 -0
- bantamtrain-0.3.0/bantamtrain/weightpack.py +303 -0
- {bantamtrain-0.1.0 → bantamtrain-0.3.0}/bantamtrain/zoom.py +69 -17
- bantamtrain-0.3.0/bantamtrain.egg-info/PKG-INFO +326 -0
- {bantamtrain-0.1.0 → bantamtrain-0.3.0}/bantamtrain.egg-info/SOURCES.txt +20 -2
- bantamtrain-0.3.0/bantamtrain.egg-info/entry_points.txt +3 -0
- bantamtrain-0.3.0/bantamtrain.egg-info/requires.txt +30 -0
- bantamtrain-0.3.0/packaging/README.md +281 -0
- bantamtrain-0.3.0/pyproject.toml +44 -0
- bantamtrain-0.1.0/PKG-INFO +0 -107
- bantamtrain-0.1.0/README.md +0 -74
- bantamtrain-0.1.0/bantamtrain/__init__.py +0 -12
- bantamtrain-0.1.0/bantamtrain/data/__init__.py +0 -1
- bantamtrain-0.1.0/bantamtrain/data/dedup.py +0 -149
- bantamtrain-0.1.0/bantamtrain/data/gridpack.py +0 -124
- bantamtrain-0.1.0/bantamtrain/data/tokpack.py +0 -220
- bantamtrain-0.1.0/bantamtrain/lowmem.py +0 -127
- bantamtrain-0.1.0/bantamtrain/memory.py +0 -170
- bantamtrain-0.1.0/bantamtrain/models/gpt.py +0 -177
- bantamtrain-0.1.0/bantamtrain/optim.py +0 -221
- bantamtrain-0.1.0/bantamtrain/qweights.py +0 -130
- bantamtrain-0.1.0/bantamtrain/weightpack.py +0 -154
- bantamtrain-0.1.0/bantamtrain.egg-info/PKG-INFO +0 -107
- bantamtrain-0.1.0/bantamtrain.egg-info/requires.txt +0 -17
- bantamtrain-0.1.0/pyproject.toml +0 -34
- {bantamtrain-0.1.0 → bantamtrain-0.3.0}/bantamtrain/models/__init__.py +0 -0
- {bantamtrain-0.1.0 → bantamtrain-0.3.0}/bantamtrain.egg-info/dependency_links.txt +0 -0
- {bantamtrain-0.1.0 → bantamtrain-0.3.0}/bantamtrain.egg-info/top_level.txt +0 -0
- {bantamtrain-0.1.0 → bantamtrain-0.3.0/packaging}/LICENSE +0 -0
- {bantamtrain-0.1.0 → bantamtrain-0.3.0}/setup.cfg +0 -0
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: bantamtrain
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: Train and fine-tune small language and weather models on limited hardware: Zoom training, Muon, 8-bit/bf16 low-memory training, LoRA/QLoRA/DoRA, int8/int4/NF4 weights, compressed data stores
|
|
5
|
+
Author: Nathan
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Source, https://github.com/Wisk-inc/AboxRebuildcode
|
|
8
|
+
Keywords: llm,training,fine-tuning,lora,qlora,efficiency,muon,optimizer,quantization,weather,era5,pytorch
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Science/Research
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
13
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Requires-Python: >=3.10
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: packaging/LICENSE
|
|
18
|
+
Requires-Dist: torch>=2.4
|
|
19
|
+
Requires-Dist: numpy>=1.24
|
|
20
|
+
Requires-Dist: zstandard>=0.21
|
|
21
|
+
Provides-Extra: data
|
|
22
|
+
Requires-Dist: tokenizers; extra == "data"
|
|
23
|
+
Requires-Dist: pyarrow; extra == "data"
|
|
24
|
+
Requires-Dist: huggingface_hub; extra == "data"
|
|
25
|
+
Provides-Extra: finetune
|
|
26
|
+
Requires-Dist: transformers>=4.56; extra == "finetune"
|
|
27
|
+
Requires-Dist: safetensors; extra == "finetune"
|
|
28
|
+
Requires-Dist: datasets; extra == "finetune"
|
|
29
|
+
Requires-Dist: sentencepiece; extra == "finetune"
|
|
30
|
+
Provides-Extra: weather
|
|
31
|
+
Requires-Dist: xarray; extra == "weather"
|
|
32
|
+
Requires-Dist: zarr; extra == "weather"
|
|
33
|
+
Requires-Dist: fsspec; extra == "weather"
|
|
34
|
+
Requires-Dist: aiohttp; extra == "weather"
|
|
35
|
+
Provides-Extra: test
|
|
36
|
+
Requires-Dist: pytest>=7; extra == "test"
|
|
37
|
+
Requires-Dist: transformers>=4.56; extra == "test"
|
|
38
|
+
Requires-Dist: peft; extra == "test"
|
|
39
|
+
Requires-Dist: safetensors; extra == "test"
|
|
40
|
+
Requires-Dist: tokenizers; extra == "test"
|
|
41
|
+
Requires-Dist: pyarrow; extra == "test"
|
|
42
|
+
Requires-Dist: sentencepiece; extra == "test"
|
|
43
|
+
Requires-Dist: matplotlib; extra == "test"
|
|
44
|
+
Dynamic: license-file
|
|
45
|
+
|
|
46
|
+
# Bantam (`bantamtrain`)
|
|
47
|
+
|
|
48
|
+
**Train and fine-tune small language models and small weather models on limited
|
|
49
|
+
compute, in plain PyTorch.**
|
|
50
|
+
|
|
51
|
+
Made by Nathan from Jamaica. MIT license. Version 0.3.0.
|
|
52
|
+
|
|
53
|
+
Bantam is a small, readable toolkit. The core needs only PyTorch, NumPy and
|
|
54
|
+
zstandard. One command, `bantam`, covers the whole path:
|
|
55
|
+
|
|
56
|
+
- **pretrain** a small GPT on a Hugging Face text dataset (or local parquet files) at
|
|
57
|
+
a fixed compute budget;
|
|
58
|
+
- **fine-tune** a Hugging Face causal language model with LoRA, QLoRA (a 4-bit NF4
|
|
59
|
+
base in pure PyTorch, no bitsandbytes) or DoRA, and export a standard PEFT adapter;
|
|
60
|
+
- **forecast weather**: train a small 6-hour ERA5 forecaster;
|
|
61
|
+
- **plan**: estimate GPU memory (`bantam memory`) and print an environment report
|
|
62
|
+
(`bantam doctor`).
|
|
63
|
+
|
|
64
|
+
It is meant for learning how training works, for testing training ideas at small
|
|
65
|
+
scale, and for a CPU or a single GPU. It is not a replacement for Unsloth, Axolotl,
|
|
66
|
+
TRL + PEFT or LLaMA-Factory on GPUs: Bantam has no fused GPU kernels, no multi-node
|
|
67
|
+
training, no RL/DPO, and no GPU benchmarks yet.
|
|
68
|
+
|
|
69
|
+
**Where the numbers come from.** Every result below was measured in one CPU-only
|
|
70
|
+
sandbox (4 cores, 16 GB RAM, PyTorch 2.15 CPU build). **Nothing has been run on a GPU
|
|
71
|
+
yet.** `n` is the number of seeds; `±` is the sample standard deviation.
|
|
72
|
+
|
|
73
|
+
## Install
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
pip install bantamtrain # core: torch, numpy, zstandard
|
|
77
|
+
pip install 'bantamtrain[data]' # prepare-text, generate (tokenizers, pyarrow, huggingface_hub)
|
|
78
|
+
pip install 'bantamtrain[finetune]' # finetune, chat (transformers, safetensors, datasets, sentencepiece)
|
|
79
|
+
pip install 'bantamtrain[weather]' # prepare-era5 (xarray, zarr, fsspec, aiohttp)
|
|
80
|
+
pip install 'bantamtrain[test]' # pytest and what the test suite imports
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
The quotes keep zsh from treating the brackets as a pattern. Python 3.10+ and
|
|
84
|
+
PyTorch 2.4+. `bantam --help` lists the 11 commands; `bantam <command> --help` lists
|
|
85
|
+
a command's options. `python -m bantamtrain <command>` works the same way.
|
|
86
|
+
|
|
87
|
+
## Results at a glance
|
|
88
|
+
|
|
89
|
+
**LLM pretraining.** A 5.8M-parameter GPT (d=256, 6 layers, context 256) on
|
|
90
|
+
TinyStories (4,096-token BPE, about 117M training tokens), every run at the same
|
|
91
|
+
budget of 6.8e13 model FLOPs (Muon's optimizer FLOPs not charged). Validation loss
|
|
92
|
+
in nats per token on 2,048 held-out windows (lower is better). "Compute-to-target"
|
|
93
|
+
is how much more compute the plain Muon baseline needs to reach the same loss (read
|
|
94
|
+
off baseline runs at 1x, 1.25x, 1.5x and 2x the budget; the runs above 1x are single
|
|
95
|
+
seeds).
|
|
96
|
+
|
|
97
|
+
| recipe | val loss | n | compute-to-target |
|
|
98
|
+
|---|---|---|---|
|
|
99
|
+
| AdamW baseline (tuned LR) | 3.332 | 1 | |
|
|
100
|
+
| Muon baseline | 2.737 ± 0.012 | 3 | 1.00x |
|
|
101
|
+
| Zoom: k=2 for the first 50 % of compute, batch x2 | 2.685 ± 0.004 | 3 | 1.12x |
|
|
102
|
+
| QK-norm (a known trick) | 2.639 ± 0.003 | 2 | 1.24x |
|
|
103
|
+
| QK-norm + Zoom (k=2 for 33 %, batch x2) | 2.589 ± 0.006 | 2 | **1.41x** (about 29 % less compute for that loss) |
|
|
104
|
+
|
|
105
|
+
`bantam pretrain --recipe best` sets the last row's flags (Muon at LR 1.6e-2, QK-norm,
|
|
106
|
+
Zoom k=2 for 33 % with batch x2); that evidence is the 2 seeds above, at this one size,
|
|
107
|
+
on a CPU.
|
|
108
|
+
|
|
109
|
+
Muon is the biggest single win at this size (one caveat: the AdamW-side parameters
|
|
110
|
+
in Muon runs get 16x the tuned AdamW learning rate, and the AdamW baseline is one
|
|
111
|
+
seed). Zoom, which merges pairs of neighbouring tokens for the first part of
|
|
112
|
+
training, gives a small gain that repeats across seeds. It is a learned-gate variant
|
|
113
|
+
of patch-level training (Shao et al., ICLR 2025, which reports about half the
|
|
114
|
+
training cost at 370M-2.7B parameters). QK-norm alone beat Zoom alone, and the two
|
|
115
|
+
stack. Several upgrades did not help (one seed each: WSD schedule, ReLU², Polar
|
|
116
|
+
Express, k=4 Zoom schedules), and the original 4→2→1 Zoom schedule lost on all 3
|
|
117
|
+
seeds (2.874).
|
|
118
|
+
|
|
119
|
+
**Weather.** ERA5 at 5.625°, a 1.26M-parameter ViT, 5.5e13 FLOPs per run, trained on
|
|
120
|
+
2010-2017, normalized 6-hour forecast MSE on 2018 (the learning rates were also
|
|
121
|
+
chosen on 2018; new runs can hold out a validation year).
|
|
122
|
+
|
|
123
|
+
| recipe | val MSE | n |
|
|
124
|
+
|---|---|---|
|
|
125
|
+
| AdamW baseline | 0.229 | 1 |
|
|
126
|
+
| Muon baseline | 0.124 ± 0.001 | 2 |
|
|
127
|
+
| Muon + Zoom merge (k=4 for 60 %) | 0.159 ± 0.001: **worse** | 2 |
|
|
128
|
+
| Muon + low-resolution warm-up (progressive resizing, a known method) | 0.112 | 1 |
|
|
129
|
+
|
|
130
|
+
**Against published models.** 1,000 TinyStories validation stories that no compared
|
|
131
|
+
model trained on, in bits per byte, each model family in its own text format. Bantam
|
|
132
|
+
(one CPU run of the plain Muon baseline recipe, 5.8M parameters, 6.4e13 FLOPs)
|
|
133
|
+
scores 1.029. That is **behind every TinyStories model**: TinyStories-1M scores 0.822
|
|
134
|
+
with about 3,350x more compute, and TinyStories-33M 0.542. It beats llama2.c
|
|
135
|
+
stories260K (1.097) with at least 157x less compute, but that is one run against a
|
|
136
|
+
22x smaller model.
|
|
137
|
+
|
|
138
|
+
**Low memory.** 8-bit Muon state + bf16 weights with stochastic rounding: loss 2.728
|
|
139
|
+
(n = 1, scored with 0.2's bf16 arithmetic, about ±0.001) vs 2.737 (n = 3), weights +
|
|
140
|
+
optimizer state 50.4 MB → 18.5 MB (2.7x smaller). On this CPU it ran 1.6x slower in
|
|
141
|
+
wall-clock.
|
|
142
|
+
|
|
143
|
+
**Quantized bases.** On SmolLM2-135M before any training (88 held-out Dolly
|
|
144
|
+
examples, one run each, 0.2 code, which packed eval examples without a
|
|
145
|
+
block-diagonal mask): int8 costs +0.003 loss, NF4 with int8 embeddings +0.18. On a
|
|
146
|
+
model this small, 4-bit costs real quality.
|
|
147
|
+
|
|
148
|
+
**5B on one 96 GB GPU.** About 6 bytes per parameter of model state with bf16
|
|
149
|
+
weights and 8-bit state, against 16-18 for standard mixed-precision AdamW. This is
|
|
150
|
+
arithmetic from `bantam memory`, **not measured**.
|
|
151
|
+
|
|
152
|
+
**Fine-tuning race** (Hugging Face Trainer + PEFT against `bantam finetune` on
|
|
153
|
+
SmolLM2-135M, fp32 on the same 4-core CPU: one epoch over 800 Dolly examples with
|
|
154
|
+
the same LoRA modules, learning rate and schedule in every arm; speed, peak memory,
|
|
155
|
+
and held-out loss on 200 examples judged the same way for every arm):
|
|
156
|
+
|
|
157
|
+
| arm | train min | real tok/s | peak RSS GB | held-out loss |
|
|
158
|
+
|---|---:|---:|---:|---:|
|
|
159
|
+
| Hugging Face Trainer + PEFT (AdamW) | 13.2 | 151 | 4.17 | 2.260 |
|
|
160
|
+
| Bantam, the same AdamW, one packed window per step | 9.7 | 206 | 2.57 | 2.259 |
|
|
161
|
+
| Bantam, QLoRA NF4 base, AdamW | 9.4 | 212 | 2.12 | 2.368 |
|
|
162
|
+
|
|
163
|
+
With the same optimizer and a comparable number of optimizer steps, Bantam matched
|
|
164
|
+
the held-out loss 1.36x faster with 38 % less peak RAM, mainly because packing
|
|
165
|
+
computes no padding. Packing also means fewer, larger optimizer steps at a given
|
|
166
|
+
batch setting: at 2 x 2 packed windows per step Bantam took 58 steps instead of 200
|
|
167
|
+
and lost 0.056. Muon on the LoRA matrices did not beat AdamW. One run per arm (wall
|
|
168
|
+
time varies by about 9 %), fp32 on a 4-core CPU; GPU tools with fused kernels are not
|
|
169
|
+
in this comparison. The race ran on the code before 0.3.0's full epochs (its "1 epoch"
|
|
170
|
+
left out the 3 windows that did not fill a last 2 x 2 step; `--drop_last 1` reproduces
|
|
171
|
+
that). `--examples_per_step N`, added since, picks the gradient accumulation for about
|
|
172
|
+
N examples per optimizer step; it has not been raced.
|
|
173
|
+
|
|
174
|
+
## Quickstart
|
|
175
|
+
|
|
176
|
+
### Pretrain a small language model
|
|
177
|
+
|
|
178
|
+
```bash
|
|
179
|
+
pip install 'bantamtrain[data]'
|
|
180
|
+
bantam prepare-text --hf roneneldan/TinyStories --out data/tinystories --vocab 4096
|
|
181
|
+
bantam pack-text --data data/tinystories # dedup + compressed token store
|
|
182
|
+
bantam pretrain --data data/tinystories --budget 6.8e13 --recipe best \
|
|
183
|
+
--name qk_zoom --out runs/tinystories --save runs/tinystories/qk_zoom.pt
|
|
184
|
+
bantam generate --model runs/tinystories/qk_zoom_pretrained --prompt "Once upon a time"
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
`--recipe best` is `--optim muon --lr 1.6e-2 --qk_norm 1 --zoom 2:0.33,1:0.67
|
|
188
|
+
--batch_scale 2` (the QK-norm + Zoom row above); any of these you pass yourself wins.
|
|
189
|
+
Without it, pretrain uses AdamW at LR 1e-3.
|
|
190
|
+
|
|
191
|
+
Other Hub text datasets work too (`--column` names the text field; repos without
|
|
192
|
+
parquet files are read through the Hub's parquet conversion), as do local parquet
|
|
193
|
+
files (`--train ... --val ...`) and an existing tokenizer (`--tokenizer gpt2`).
|
|
194
|
+
|
|
195
|
+
### Fine-tune a Hugging Face model
|
|
196
|
+
|
|
197
|
+
```bash
|
|
198
|
+
pip install 'bantamtrain[finetune]'
|
|
199
|
+
|
|
200
|
+
# LoRA r=16 on every linear layer, 8-bit AdamW (the defaults)
|
|
201
|
+
bantam finetune --model HuggingFaceTB/SmolLM2-135M \
|
|
202
|
+
--dataset databricks/databricks-dolly-15k --max_examples 2000 \
|
|
203
|
+
--out runs/dolly
|
|
204
|
+
|
|
205
|
+
# QLoRA (NF4 base) or DoRA: add --quant nf4 --low_ram 1 --grad_ckpt 1, or --dora 1
|
|
206
|
+
bantam chat --model HuggingFaceTB/SmolLM2-135M --adapter runs/dolly \
|
|
207
|
+
--prompt "Give me three tips for staying healthy."
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
Defaults: 8-bit optimizer state, examples packed into 512-token windows with
|
|
211
|
+
block-diagonal masks (sliding-window layers keep their window), every window once per
|
|
212
|
+
epoch, loss on response tokens only, chunked cross-entropy. A packed window holds
|
|
213
|
+
several examples, so an optimizer step sees more examples than `--batch x --accum`;
|
|
214
|
+
`log.json` reports `examples_per_step`, and `--examples_per_step N` (with a small
|
|
215
|
+
enough `--batch`) sets the accumulation for about N. `--optim muon` puts the LoRA
|
|
216
|
+
matrices on Muon; in the race it did not beat AdamW. Data can
|
|
217
|
+
be a Hub dataset or a JSONL file with `prompt`/`response`,
|
|
218
|
+
`instruction`/`context`/`response`, `messages` or `conversations` rows. The output
|
|
219
|
+
directory holds a standard PEFT adapter (`adapter_config.json` +
|
|
220
|
+
`adapter_model.safetensors`) and the tokenizer. Loading it in PEFT needs
|
|
221
|
+
`pip install peft`:
|
|
222
|
+
|
|
223
|
+
```python
|
|
224
|
+
import torch
|
|
225
|
+
from transformers import AutoModelForCausalLM
|
|
226
|
+
from peft import PeftModel
|
|
227
|
+
base = AutoModelForCausalLM.from_pretrained("HuggingFaceTB/SmolLM2-135M", dtype=torch.float32)
|
|
228
|
+
model = PeftModel.from_pretrained(base, "runs/dolly")
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
Load the base in fp32 as above: transformers 5 otherwise loads SmolLM2 in its stored
|
|
232
|
+
bf16, and the logits then no longer match Bantam's.
|
|
233
|
+
|
|
234
|
+
### Train a small weather model
|
|
235
|
+
|
|
236
|
+
```bash
|
|
237
|
+
pip install 'bantamtrain[weather]'
|
|
238
|
+
bantam prepare-era5 --out data/era5 # WeatherBench2 ERA5, 5.625 deg, 2010-2018
|
|
239
|
+
bantam pack-era5 --data data/era5 # optional error-bounded compressed copy
|
|
240
|
+
# Muon + low-resolution warm-up, the best measured weather run (1 seed; = --recipe best)
|
|
241
|
+
bantam weather --data data/era5 --val_year 2017 --budget 5.5e13 \
|
|
242
|
+
--optim muon --lr 1.6e-2 --zoom 4:0.6,1:0.4 --coarse_mode lowres \
|
|
243
|
+
--name lowres --out runs/era5 --save runs/era5/lowres.pt
|
|
244
|
+
bantam weather --forecast --load runs/era5/lowres.pt --init 2018-03-01T00 --steps 12
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### Check your setup and plan memory
|
|
248
|
+
|
|
249
|
+
```bash
|
|
250
|
+
bantam doctor # versions, CUDA, threads, RAM, extras
|
|
251
|
+
bantam memory --params 1e9 --gpu_gb 24 # memory / time estimate for one GPU
|
|
252
|
+
bantam memory --finetune --params 7e9 --gpu_gb 24 # full / LoRA / QLoRA fine-tuning
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### Stop and resume
|
|
256
|
+
|
|
257
|
+
`bantam pretrain` and `bantam weather` take `--ckpt_every N` and `--resume PATH`;
|
|
258
|
+
`bantam finetune` takes `--save_every N` and `--resume OUT_DIR`. On CPU a resumed run
|
|
259
|
+
matches an uninterrupted one bit for bit (tested).
|
|
260
|
+
|
|
261
|
+
## Use it from Python
|
|
262
|
+
|
|
263
|
+
```python
|
|
264
|
+
import torch
|
|
265
|
+
from bantamtrain.models import ZoomGPT
|
|
266
|
+
from bantamtrain.optim import build_optimizer
|
|
267
|
+
from bantamtrain.zoom import ZoomSchedule, lr_at
|
|
268
|
+
|
|
269
|
+
model = ZoomGPT(vocab=4096, d=256, n_layer=6, n_head=4, zoom_ks=[2], qk_norm=True)
|
|
270
|
+
opt = build_optimizer(model, "muon", lr=1.6e-2, weight_decay=0.1, state_bits=8)
|
|
271
|
+
sched = ZoomSchedule.parse("2:0.33,1:0.67") # k=2 for the first 33% of the budget
|
|
272
|
+
|
|
273
|
+
x = torch.randint(0, 4096, (8, 258)) # stand-in for real token windows
|
|
274
|
+
for frac in (0.1, 0.5): # fraction of the compute budget spent
|
|
275
|
+
k = sched.k_at(frac)
|
|
276
|
+
for g in opt.param_groups:
|
|
277
|
+
g["lr"] = lr_at(frac, 1.6e-2) * g.get("lr_scale", 1.0)
|
|
278
|
+
loss = model(x[:, :256], x[:, k:256 + k], k=k) # targets: the inputs shifted by k
|
|
279
|
+
opt.zero_grad(); loss.backward(); opt.step()
|
|
280
|
+
|
|
281
|
+
model.save_pretrained("my_model") # zoom gates stripped: a plain k=1 model
|
|
282
|
+
tokens = ZoomGPT.from_pretrained("my_model").generate(torch.tensor([[0]]), 50)
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
Also importable: `bantamtrain.lora` (LoRA, DoRA, PEFT save/load), `bantamtrain.qweights`
|
|
286
|
+
(int8 / int4 / NF4 weight-only layers), `bantamtrain.weightpack` (compressed
|
|
287
|
+
checkpoints), `bantamtrain.losses` (chunked cross-entropy), `bantamtrain.data`
|
|
288
|
+
(TokPack, MinHash dedup, GridPack).
|
|
289
|
+
|
|
290
|
+
## On a GPU (should work, untested)
|
|
291
|
+
|
|
292
|
+
The CUDA paths exist but have only been tested with CPU stand-ins: bf16 autocast on
|
|
293
|
+
Ampere and newer (fp16 with a gradient scaler before), TF32, `--compile`,
|
|
294
|
+
`bantam pretrain --auto_batch`, and multi-GPU DDP through `torchrun` for pretrain and
|
|
295
|
+
finetune (tested only with 2 CPU processes). There is no
|
|
296
|
+
FSDP, no FP8 and no fused kernel; packed fine-tuning uses a dense block-diagonal mask,
|
|
297
|
+
so expect it to be slower than padding-free packing with FlashAttention.
|
|
298
|
+
|
|
299
|
+
## Limitations
|
|
300
|
+
|
|
301
|
+
- No GPU measurements of speed or memory.
|
|
302
|
+
- One small model per task (5.8M pretraining, 135M fine-tuning, 1.26M weather), and
|
|
303
|
+
2-3 seeds at most. Gains at larger scale are not measured.
|
|
304
|
+
- Comparisons are matched in model FLOPs, not wall-clock. Muon's own optimizer FLOPs
|
|
305
|
+
(about 20 % of the model FLOPs at this size) are not charged to the budget.
|
|
306
|
+
- The committed weather runs had no separate validation year (their learning rates
|
|
307
|
+
were chosen on the test year), and the scores are not WeatherBench scorecards.
|
|
308
|
+
- The low-resolution weather run is one seed, and the quantized-base costs above
|
|
309
|
+
were measured with 0.2 code.
|
|
310
|
+
- The fine-tuning race is one run per arm on one 135M model. Packing changes the
|
|
311
|
+
number of optimizer steps, so check `steps_per_epoch` in `log.json`.
|
|
312
|
+
- Importing the pretrain and weather command modules turns on flush-to-zero for the
|
|
313
|
+
whole process (`bantam finetune` and `bantam chat` set it with `--ftz`, default on).
|
|
314
|
+
|
|
315
|
+
## Credit
|
|
316
|
+
|
|
317
|
+
Most of Bantam is other people's ideas, re-implemented in plain PyTorch: Muon (Keller
|
|
318
|
+
Jordan et al.), Polar Express (Amsel et al.), QK-norm (Henry et al.; Dehghani et
|
|
319
|
+
al.), 8-bit optimizer state (Dettmers et al.), bf16 stochastic rounding (Zamirai et
|
|
320
|
+
al.), LoRA, QLoRA / NF4, DoRA, LoRA+ and rsLoRA, the PEFT adapter format, and
|
|
321
|
+
progressive resizing. Zoom is a learned-gate variant of patch-level training
|
|
322
|
+
([Shao et al., ICLR 2025](https://arxiv.org/abs/2407.12665)), related to Hourglass,
|
|
323
|
+
Funnel-Transformer and sequence-length warm-up. Data: TinyStories (Eldan & Li),
|
|
324
|
+
llama2.c models (Karpathy), WeatherBench 2 (Rasp et al.).
|
|
325
|
+
|
|
326
|
+
Made by Nathan from Jamaica. MIT license.
|