m3 app files (int4 ONNX, tokenizer, lexicon, SymSpell index, quantized trigram, FORMAT.md)
Browse files- FORMAT.md +127 -0
- m3.decoder.int4.onnx +3 -0
- m3.encoder.int4.onnx +3 -0
- m3.manifest.json +95 -0
- m3_lexicon.tsv +0 -0
- m3_ngram.bin +3 -0
- m3_symspell.bin +3 -0
- vocab_m3.json +0 -0
FORMAT.md
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# m3 app files
|
| 2 |
+
|
| 3 |
+
This folder holds the files that the Keylime apps download for the m3 model. The algorithm that uses them is in
|
| 4 |
+
`m3dict_cpt/PORT_SPEC.md` (the m3dict decoder). The reference code is `m3dict_cpt/onnx_backend.py` (`M3Dict`).
|
| 5 |
+
The test vectors are in `m3dict_cpt/test_vectors.json`.
|
| 6 |
+
|
| 7 |
+
All binary files are little-endian. All offsets are in bytes.
|
| 8 |
+
|
| 9 |
+
## Files
|
| 10 |
+
|
| 11 |
+
| File | Content |
|
| 12 |
+
|---|---|
|
| 13 |
+
| `m3.encoder.int4.onnx` | Encoder. Inputs: `src_ids` int64 [1,S], `taps` float32 [1,S,2] (zeros). Outputs: `memory`, `cross_k0..2`, `cross_v0..2` float32 [1,2,S,64]. |
|
| 14 |
+
| `m3.decoder.int4.onnx` | Decoder with KV cache. See "Decoder" below. |
|
| 15 |
+
| `m3.manifest.json` | Export metadata: config, style ids (casual 256, standard 257, formal 258), EOS 2, `max_src` 384, `max_tgt` 112. |
|
| 16 |
+
| `vocab_m3.json` | SentencePiece BPE vocabulary (32,768 pieces, byte fallback) in the `web/export_vocab.py` format. Byte-identical to `vocab_m2.json`. |
|
| 17 |
+
| `m3_lexicon.tsv` | The 100,000-word shipping lexicon. One `word<TAB>count` per line, sorted by count (high first). Line number = lexicon id. |
|
| 18 |
+
| `m3_symspell.bin` | Delete index for candidate lookup. |
|
| 19 |
+
| `m3_ngram.bin` | Pruned and quantized KN word trigram (the `ng` feature). |
|
| 20 |
+
|
| 21 |
+
The apps derive the other tables from `m3_lexicon.tsv` when they load it:
|
| 22 |
+
|
| 23 |
+
- **Log prior**: `log(count / sum of counts)` (float64).
|
| 24 |
+
- **Letter bag**: 26 letter counts per word (apostrophes not counted), and the letter count.
|
| 25 |
+
- **Prefix table**: for each prefix of a lexicon word, the shortest letter count (apostrophes not counted) of a word
|
| 26 |
+
with that prefix. A sorted word list plus a range-minimum query gives the same values as a full prefix map.
|
| 27 |
+
- **Apostrophe expansions**: the word without apostrophes -> the lexicon words with apostrophes, in lexicon order.
|
| 28 |
+
|
| 29 |
+
## Input to the encoder
|
| 30 |
+
|
| 31 |
+
`src_ids` = `[style id]` + the UTF-8 bytes of the text, cut to 384 ids in total. `taps` = zeros [1,S,2].
|
| 32 |
+
|
| 33 |
+
## Decoder
|
| 34 |
+
|
| 35 |
+
Inputs: `ids` int64 [1,N], `next_ids` int64 [1,N] (zeros when not needed), `pos` int64 [N] (absolute positions),
|
| 36 |
+
`past_mask` bool [P], `src_mask` bool [S] (all true), `past_k0`/`past_v0` float32 [1,2,P,64], `past_c0`/`past_c1`
|
| 37 |
+
float32 [1,6,512], `cross_*` (from the encoder).
|
| 38 |
+
|
| 39 |
+
Outputs: `last_lp` float32 [1,32768] (log-probs after the last id), `tok_lp` float32 [1,N] (log-prob of `next_ids[i]`
|
| 40 |
+
after `ids[..i]`), `present_k0`, `present_v0`, `present_c0`, `present_c1`.
|
| 41 |
+
|
| 42 |
+
The start state: `past_k0`/`past_v0` zeros [1,2,1,64], `past_c*` zeros [1,6,512], `past_mask` = `[false]`, position 0.
|
| 43 |
+
The first id is EOS (2). After a call, the new mask is the old mask + N times `true`, and the position moves by N.
|
| 44 |
+
|
| 45 |
+
## m3_symspell.bin
|
| 46 |
+
|
| 47 |
+
```
|
| 48 |
+
offset 0 char[4] "KLSS"
|
| 49 |
+
offset 4 u32 version (1)
|
| 50 |
+
offset 8 u32 n_words (100000)
|
| 51 |
+
offset 12 u32 n_entries
|
| 52 |
+
offset 16 u32 maxd (2)
|
| 53 |
+
offset 20 u32 short (2)
|
| 54 |
+
offset 24 u32 key[n_entries]
|
| 55 |
+
u32 word_id[n_entries]
|
| 56 |
+
```
|
| 57 |
+
|
| 58 |
+
For each lexicon word w, let s = w without apostrophes. The index has one entry for each string in deletes(s, d)
|
| 59 |
+
(s itself and all strings made by deleting up to d letters), with d = maxd if len(s) > short, else d = 1.
|
| 60 |
+
`key` = the high 32 bits of FNV-1a 64 of the UTF-8 bytes of the delete string. Entries are sorted by (key, word_id).
|
| 61 |
+
|
| 62 |
+
Lookup for a typed word t (lowercase, apostrophes removed, length n): make deletes(t, dq) with dq = 3 if n >= 7,
|
| 63 |
+
dq = 2 if n > 2, else dq = 1. For each delete q, find the entries with key = hash32(q). Accept a word id only if
|
| 64 |
+
q is a subsequence of the word's s and 0 <= len(s) - len(q) <= d (d of that word, as above). This check removes all
|
| 65 |
+
32-bit key collisions, so the result is exactly the set of words that share a delete with t. Return the sorted
|
| 66 |
+
unique ids.
|
| 67 |
+
|
| 68 |
+
## m3_ngram.bin
|
| 69 |
+
|
| 70 |
+
The KN word trigram of the m2 data (`data/stats/ngram_kn.npz`, 508 MB), pruned to trigram count >= 12 and bigram
|
| 71 |
+
continuation count >= 12 (`m3dict_cpt/qkn.py`). The backoff weights are computed again from the full totals, so the
|
| 72 |
+
pruned model is still an interpolated KN model. It is stored in backoff form:
|
| 73 |
+
|
| 74 |
+
```
|
| 75 |
+
log P(w | u v) = T(u,v,w) if the trigram (u,v,w) is stored
|
| 76 |
+
= G3(u,v) + B(v,w) else, if the history (u,v) is stored
|
| 77 |
+
= B(v,w) else
|
| 78 |
+
B(v,w) = Q2(v,w) if the bigram (v,w) is stored
|
| 79 |
+
= G2(v) + P1(w) else
|
| 80 |
+
```
|
| 81 |
+
|
| 82 |
+
Each value x is stored as q = round(-x * 1024), and the value is read back as -q / 1024. All sums of these values are
|
| 83 |
+
exact in float64, so all ports get bit-identical results.
|
| 84 |
+
|
| 85 |
+
```
|
| 86 |
+
offset 0 char[4] "KLQN"
|
| 87 |
+
offset 4 u32 version (1)
|
| 88 |
+
u32 V (vocabulary size)
|
| 89 |
+
u32 n_bi, n_hist, n_tri
|
| 90 |
+
u32 vocab_bytes
|
| 91 |
+
u8 vocab[vocab_bytes] UTF-8, words joined by '\n'; id 0 = <unk>, 1 = <s>, 2 = <num>
|
| 92 |
+
pad to a multiple of 4
|
| 93 |
+
u16 P1[V] q of log P1(w)
|
| 94 |
+
u16 G2[V] q of log G2(v) (0 when v has no bigram statistics)
|
| 95 |
+
u32 bi_off[V+1] bigrams of v: entries bi_off[v] .. bi_off[v+1]-1
|
| 96 |
+
u32 bi[n_bi] (w << 15) | q(Q2(v,w)), w ascending in each v
|
| 97 |
+
u32 hist_off[V+1] histories (u, *): entries hist_off[u] .. hist_off[u+1]-1
|
| 98 |
+
u32 hist[n_hist] (v << 15) | q(G3(u,v)), v ascending in each u
|
| 99 |
+
u32 tri_off[n_hist+1] trigrams of history h: entries tri_off[h] .. tri_off[h+1]-1
|
| 100 |
+
u32 tri[n_tri] (w << 15) | q(T(u,v,w)), w ascending in each history
|
| 101 |
+
```
|
| 102 |
+
|
| 103 |
+
Find an entry with a binary search on `entry >> 15` in its range. The 15-bit fields hold q up to 32767 (log value
|
| 104 |
+
down to -32). The u16 fields hold q up to 65535.
|
| 105 |
+
|
| 106 |
+
Sentence log-prob (`sent_lp`): words = the lowercase SEG words (`[A-Za-z]+(?:'[A-Za-z]+)*`) of the text. The id of a
|
| 107 |
+
word is its vocabulary index, or 0 if it is not in the vocabulary (a word that starts with a digit gets 2). History
|
| 108 |
+
starts as (1, 1). For each word: add -16.0 if its id is 0, else log P(w | u v). Then shift the history (an OOV word
|
| 109 |
+
enters the history as id 0). Add the values from left to right in float64.
|
| 110 |
+
|
| 111 |
+
`ng` of an alternative = sent_lp(the greedy text with the slot replaced) - sent_lp(the greedy text).
|
| 112 |
+
|
| 113 |
+
## Decoder constants
|
| 114 |
+
|
| 115 |
+
Shipping parameters: `m3dict_cpt/params_ship.json` (also in the manifest entry). Constants that are not in that file:
|
| 116 |
+
|
| 117 |
+
| Name | Value |
|
| 118 |
+
|---|---|
|
| 119 |
+
| top-K tokens per greedy step | 8 (ties: log-prob desc, then token id asc) |
|
| 120 |
+
| tau_split | 4.0 |
|
| 121 |
+
| greedy steps | min(112, len(src_ids) // 2 + 24) |
|
| 122 |
+
| candidates per slot (slot plan, injection) | 8 |
|
| 123 |
+
| RARE rank | 10000 (a slot plan uses min(10000, params.rare)) |
|
| 124 |
+
| generator | prior weight 0.5, max hits 4000, qd3 7, short 2, bag top 300 for words of >= 6 letters (ties: score desc, then lexicon id asc) |
|
| 125 |
+
| spatial model | keyboard.SpatialModel defaults (sigma 0.3, key-centre touches), QWERTY eval geometry; splits use space bar x 3.5 .. 8.5 |
|
| 126 |
+
| feature rounding | dm3, sp, pr, ng: Python round(x, 4) |
|
| 127 |
+
| LP_OOV | -16.0 |
|
m3.decoder.int4.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:05c2ba7c3b54bba2499522efc08a1d8cd02b2bf5d58cfa1662a029aeff89a1f8
|
| 3 |
+
size 26871564
|
m3.encoder.int4.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fe57a05e25ef38639ff0ae88930f5cd79004a7d7f5c8538515fb641a04a44085
|
| 3 |
+
size 23968360
|
m3.manifest.json
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "m3",
|
| 3 |
+
"params": 61718400,
|
| 4 |
+
"config": {
|
| 5 |
+
"vocab_size": 32768,
|
| 6 |
+
"d_model": 512,
|
| 7 |
+
"n_layers": 16,
|
| 8 |
+
"attn_layers": [
|
| 9 |
+
2,
|
| 10 |
+
4,
|
| 11 |
+
6,
|
| 12 |
+
9,
|
| 13 |
+
11,
|
| 14 |
+
13
|
| 15 |
+
],
|
| 16 |
+
"n_heads": 8,
|
| 17 |
+
"n_kv_heads": 2,
|
| 18 |
+
"head_dim": 64,
|
| 19 |
+
"conv_kernel": 7,
|
| 20 |
+
"mlp_hidden": 1536,
|
| 21 |
+
"max_seq": 8192,
|
| 22 |
+
"rope_theta": 10000.0,
|
| 23 |
+
"norm_eps": 1e-05,
|
| 24 |
+
"attnres": true,
|
| 25 |
+
"attnres_block": 4,
|
| 26 |
+
"init_std": 0.02,
|
| 27 |
+
"arch": "encdec",
|
| 28 |
+
"enc_layers": 10,
|
| 29 |
+
"enc_attn_layers": [
|
| 30 |
+
2,
|
| 31 |
+
4,
|
| 32 |
+
7
|
| 33 |
+
],
|
| 34 |
+
"dec_layers": 3,
|
| 35 |
+
"dec_attn_layers": [
|
| 36 |
+
1
|
| 37 |
+
],
|
| 38 |
+
"size": "m3_60m",
|
| 39 |
+
"src_vocab": 264,
|
| 40 |
+
"cross_rope": false,
|
| 41 |
+
"src_taps": true,
|
| 42 |
+
"engram_rows": 0,
|
| 43 |
+
"engram_dim": 96,
|
| 44 |
+
"engram_heads": 2,
|
| 45 |
+
"engram_orders": [
|
| 46 |
+
3,
|
| 47 |
+
4,
|
| 48 |
+
5
|
| 49 |
+
],
|
| 50 |
+
"enc_block_causal": true
|
| 51 |
+
},
|
| 52 |
+
"src_vocab": 264,
|
| 53 |
+
"style_ids": {
|
| 54 |
+
"casual": 256,
|
| 55 |
+
"standard": 257,
|
| 56 |
+
"formal": 258
|
| 57 |
+
},
|
| 58 |
+
"eos": 2,
|
| 59 |
+
"encoder_inputs": [
|
| 60 |
+
"src_ids",
|
| 61 |
+
"taps (float32 [1,S,2], per-byte touch minus key centre in key widths; zeros if none)"
|
| 62 |
+
],
|
| 63 |
+
"max_src": 384,
|
| 64 |
+
"max_tgt": 112,
|
| 65 |
+
"state_names": [
|
| 66 |
+
"k0",
|
| 67 |
+
"v0",
|
| 68 |
+
"c0",
|
| 69 |
+
"c1"
|
| 70 |
+
],
|
| 71 |
+
"n_cross": 3,
|
| 72 |
+
"files": {
|
| 73 |
+
"m3.encoder.int4.onnx": 23968360,
|
| 74 |
+
"m3.decoder.int4.onnx": 26871564
|
| 75 |
+
},
|
| 76 |
+
"parity": {
|
| 77 |
+
"n": 50,
|
| 78 |
+
"fp32_vs_torch_fp": 50,
|
| 79 |
+
"int4_vs_torch_qat": 50,
|
| 80 |
+
"int4_acc4_vs_torch_qat": 50,
|
| 81 |
+
"int4_greedy_ms_mean_2threads": 695.0
|
| 82 |
+
},
|
| 83 |
+
"val": {
|
| 84 |
+
"val_fp": 0.06328179687261581,
|
| 85 |
+
"val_qat_fakequant": 0.06331119686365128,
|
| 86 |
+
"val_standard": 0.07250192016363144,
|
| 87 |
+
"val_formal": 0.2008037567138672,
|
| 88 |
+
"val_ctx": 0.028463984340006247,
|
| 89 |
+
"val_ctx_iso": 0.032692755440651616,
|
| 90 |
+
"tokens": 17260216320,
|
| 91 |
+
"steps": 131685,
|
| 92 |
+
"hours": 7.625444640451008,
|
| 93 |
+
"tok_per_s": 629121.2119097591
|
| 94 |
+
}
|
| 95 |
+
}
|
m3_lexicon.tsv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
m3_ngram.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0c2329ec0a7884d6a8a4a85899e27adf077bc5b3187c5d2e654be9cc5aa6665c
|
| 3 |
+
size 24097652
|
m3_symspell.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fb489db418bad113c02ad239c1c584e3cf2c3d77465639365345e3d4e8942386
|
| 3 |
+
size 26841264
|
vocab_m3.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|