fechyy commited on
Commit
227b0bb
·
verified ·
1 Parent(s): 5924abf

Upload folder using huggingface_hub

Browse files
Files changed (6) hide show
  1. README.md +75 -0
  2. hot_rows.npy +3 -0
  3. meta.json +22 -0
  4. rest.pt +3 -0
  5. scales_q4.bin +3 -0
  6. values_q4.bin +3 -0
README.md ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ language:
4
+ - en
5
+ library_name: pytorch
6
+ pipeline_tag: text-generation
7
+ datasets:
8
+ - wikimedia/wikipedia
9
+ tags:
10
+ - product-key-memory
11
+ - memory-layers
12
+ - rocm
13
+ ---
14
+
15
+ # sparse-memory-lm B-16M
16
+
17
+ A small Llama-style language model with a product-key memory: a table of 16.8 million learned vectors (6.4B
18
+ parameters), of which the model reads a few hundred per token. Per token it uses 33M parameters. It's the B-16M
19
+ model from [github.com/re133/sparse-memory-lm](https://github.com/re133/sparse-memory-lm), where the code, the
20
+ training setup and all the measurements are.
21
+
22
+ The point of uploading it: the table doesn't have to be in GPU memory. On my Radeon RX 9070 the model writes about
23
+ 140 tokens/s with the table memory-mapped from an NVMe SSD, using 0.42 GiB of VRAM.
24
+
25
+ ## Files
26
+
27
+ | File | What | Size |
28
+ |---|---|---|
29
+ | `values_q4.bin` | the table, 4 bit, 16,777,216 rows x 384, two values per byte | 3.2 GB |
30
+ | `scales_q4.bin` | one fp16 scale per row | 34 MB |
31
+ | `rest.pt` | everything else (PyTorch state dict + config), fp32 | 284 MB |
32
+ | `hot_rows.npy` | rows sorted by how often they were read in training, for the RAM cache | 134 MB |
33
+ | `meta.json` | shapes, the 4-bit format, sha256 of every file | |
34
+
35
+ The full fp32 table isn't uploaded (25.8 GB). The 4-bit table loses almost nothing: validation PPL 19.98 against
36
+ 19.96.
37
+
38
+ ## Running it
39
+
40
+ ```bash
41
+ git clone https://github.com/re133/sparse-memory-lm.git && cd sparse-memory-lm
42
+ python3 -m venv .venv
43
+ .venv/bin/pip install torch --index-url https://download.pytorch.org/whl/rocm7.2 # or the CUDA wheel
44
+ .venv/bin/pip install -r requirements.txt huggingface_hub
45
+ .venv/bin/python scripts/demo_generate.py --download --table nvme # or ram / vram, -i for your own prompts
46
+ ```
47
+
48
+ `--table nvme` reads the table from the SSD, `ram` keeps it in RAM, `vram` on the GPU (~200 tokens/s on the
49
+ RX 9070). All three write the same text (with Triton 3.5 the logits are even bit-identical; with Triton 3.8 they can
50
+ differ in the last bits). It needs the Triton kernels from the repo, so it doesn't load with
51
+ `transformers`.
52
+
53
+ ## Training
54
+
55
+ - **Data:** 500M tokens of English Wikipedia (`wikimedia/wikipedia`, 20231101.en), GPT-2 tokenizer, every article
56
+ as `Title\n\nText`.
57
+ - **Model:** d=384, 12 layers, 6 heads, SwiGLU, RoPE, 1024-token context. Layers 3, 7 and 11 have a memory layer
58
+ instead of the FFN (4 heads, top 32 of 4096² product keys, one table shared by the three layers).
59
+ - **Hardware:** one NVIDIA H200, ~3 hours, about 101 GB of GPU memory.
60
+
61
+ ## Results
62
+
63
+ | | Val PPL (Wikipedia) |
64
+ |---|---|
65
+ | Same model without the table (21M) | 25.67 |
66
+ | B-16M | 19.96 (fp32 table) / 19.98 (this 4-bit table) |
67
+
68
+ That's about as good as a dense model with ~114M parameters trained on the same data (between 106M and 123M).
69
+
70
+ ## Limitations
71
+
72
+ - **Small model:** it writes fluent Wikipedia-style English, but most facts in it are made up. It's a research
73
+ model for measuring what such a table costs and brings, not for actually using it.
74
+ - **No instruction tuning, no safety tuning.** It just continues Wikipedia articles.
75
+ - **Training data:** Wikipedia text, licensed CC BY-SA 4.0. The weights themselves are Apache 2.0.
hot_rows.npy ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7380ad9608603d05289e3698259a69472056723c7a35bb0fe925833b3e109fb5
3
+ size 134217856
meta.json ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model": "B-16M-sparse (github.com/re133/sparse-memory-lm)",
3
+ "rows": 16777216,
4
+ "dim": 384,
5
+ "table_keys": [
6
+ "layers.2.ffn.values.weight",
7
+ "layers.6.ffn.values.weight",
8
+ "layers.10.ffn.values.weight"
9
+ ],
10
+ "q4": "kernels.quantize_q4: per row d = fp16(max-abs value with sign / -8), code = clamp(round(w/d),-8,7)+8, low nibble = dims 0..dim/2-1, high nibble = dims dim/2..dim-1",
11
+ "cpu_vs_gpu_q4_check": {
12
+ "rows": 1048576,
13
+ "packed_equal": true,
14
+ "scales_equal": true
15
+ },
16
+ "sha256": {
17
+ "hot_rows.npy": "7380ad9608603d05289e3698259a69472056723c7a35bb0fe925833b3e109fb5",
18
+ "rest.pt": "c7ff78fa2b2afe3eaaab29e993d2b988628a858436e8d629364161cca242dbc8",
19
+ "scales_q4.bin": "3a260537dc23ef4c7d73cfd358f9bd4dd9bd95ce89aede4d48ae73a683271294",
20
+ "values_q4.bin": "67f8900ba65e34a2b6d93116f61636fd9ce43d13c96d5ed870f955bd6c54916c"
21
+ }
22
+ }
rest.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c7ff78fa2b2afe3eaaab29e993d2b988628a858436e8d629364161cca242dbc8
3
+ size 284037408
scales_q4.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3a260537dc23ef4c7d73cfd358f9bd4dd9bd95ce89aede4d48ae73a683271294
3
+ size 33554432
values_q4.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:67f8900ba65e34a2b6d93116f61636fd9ce43d13c96d5ed870f955bd6c54916c
3
+ size 3221225472