@mailwoman/mtf 0.0.0-stage → 10.8.0
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.
- package/README.md +221 -2
- package/THIRD_PARTY_NOTICES.md +31 -0
- package/crates/mtf-channels/tests/fixtures/README.md +13 -0
- package/crates/mtf-sampler/tests/fixtures/README.md +120 -0
- package/lib/code-units.ts +45 -0
- package/lib/index.ts +266 -0
- package/out/code-units.d.ts +24 -0
- package/out/code-units.d.ts.map +1 -0
- package/out/code-units.js +36 -0
- package/out/code-units.js.map +1 -0
- package/out/index.d.ts +128 -0
- package/out/index.d.ts.map +1 -0
- package/out/index.js +171 -0
- package/out/index.js.map +1 -0
- package/out/tools/build-wasm.run.d.ts +14 -0
- package/out/tools/build-wasm.run.d.ts.map +1 -0
- package/out/tools/build-wasm.run.js +42 -0
- package/out/tools/build-wasm.run.js.map +1 -0
- package/out/tools/sampler/native.d.ts +314 -0
- package/out/tools/sampler/native.d.ts.map +1 -0
- package/out/tools/sampler/native.js +114 -0
- package/out/tools/sampler/native.js.map +1 -0
- package/out/tools/tokenizer/binary.d.ts +38 -0
- package/out/tools/tokenizer/binary.d.ts.map +1 -0
- package/out/tools/tokenizer/binary.js +94 -0
- package/out/tools/tokenizer/binary.js.map +1 -0
- package/out/tools/tokenizer/byte-fallback.d.ts +39 -0
- package/out/tools/tokenizer/byte-fallback.d.ts.map +1 -0
- package/out/tools/tokenizer/byte-fallback.js +49 -0
- package/out/tools/tokenizer/byte-fallback.js.map +1 -0
- package/out/tools/tokenizer/model.d.ts +70 -0
- package/out/tools/tokenizer/model.d.ts.map +1 -0
- package/out/tools/tokenizer/model.js +56 -0
- package/out/tools/tokenizer/model.js.map +1 -0
- package/out/tools/tokenizer/prune.d.ts +69 -0
- package/out/tools/tokenizer/prune.d.ts.map +1 -0
- package/out/tools/tokenizer/prune.js +126 -0
- package/out/tools/tokenizer/prune.js.map +1 -0
- package/out/tools/tokenizer/prune.run.d.ts +16 -0
- package/out/tools/tokenizer/prune.run.d.ts.map +1 -0
- package/out/tools/tokenizer/prune.run.js +34 -0
- package/out/tools/tokenizer/prune.run.js.map +1 -0
- package/out/tools/tokenizer/sentencepiece.d.ts +63 -0
- package/out/tools/tokenizer/sentencepiece.d.ts.map +1 -0
- package/out/tools/tokenizer/sentencepiece.js +90 -0
- package/out/tools/tokenizer/sentencepiece.js.map +1 -0
- package/out/tools/tokenizer/splice.d.ts +117 -0
- package/out/tools/tokenizer/splice.d.ts.map +1 -0
- package/out/tools/tokenizer/splice.js +398 -0
- package/out/tools/tokenizer/splice.js.map +1 -0
- package/out/tools/tokenizer/splice.run.d.ts +29 -0
- package/out/tools/tokenizer/splice.run.d.ts.map +1 -0
- package/out/tools/tokenizer/splice.run.js +114 -0
- package/out/tools/tokenizer/splice.run.js.map +1 -0
- package/out/tools/tokenizer/symbols.d.ts +42 -0
- package/out/tools/tokenizer/symbols.d.ts.map +1 -0
- package/out/tools/tokenizer/symbols.js +113 -0
- package/out/tools/tokenizer/symbols.js.map +1 -0
- package/out/tools/tokenizer/train/text.run.d.ts +16 -0
- package/out/tools/tokenizer/train/text.run.d.ts.map +1 -0
- package/out/tools/tokenizer/train/text.run.js +67 -0
- package/out/tools/tokenizer/train/text.run.js.map +1 -0
- package/out/tools/tokenizer/train.d.ts +113 -0
- package/out/tools/tokenizer/train.d.ts.map +1 -0
- package/out/tools/tokenizer/train.js +162 -0
- package/out/tools/tokenizer/train.js.map +1 -0
- package/out/tools/tokenizer/train.run.d.ts +24 -0
- package/out/tools/tokenizer/train.run.d.ts.map +1 -0
- package/out/tools/tokenizer/train.run.js +131 -0
- package/out/tools/tokenizer/train.run.js.map +1 -0
- package/out/tools/tokenizer/utilization.run.d.ts +17 -0
- package/out/tools/tokenizer/utilization.run.d.ts.map +1 -0
- package/out/tools/tokenizer/utilization.run.js +80 -0
- package/out/tools/tokenizer/utilization.run.js.map +1 -0
- package/out/tools/tokenizer/verify.d.ts +94 -0
- package/out/tools/tokenizer/verify.d.ts.map +1 -0
- package/out/tools/tokenizer/verify.js +193 -0
- package/out/tools/tokenizer/verify.js.map +1 -0
- package/out/tools/tokenizer/verify.run.d.ts +22 -0
- package/out/tools/tokenizer/verify.run.d.ts.map +1 -0
- package/out/tools/tokenizer/verify.run.js +71 -0
- package/out/tools/tokenizer/verify.run.js.map +1 -0
- package/package.json +107 -3
- package/third-party/darts-clone/LICENSE +10 -0
- package/third-party/sentencepiece/LICENSE +202 -0
- package/tools/build-wasm.run.ts +48 -0
- package/tools/sampler/native.ts +433 -0
- package/tools/tokenizer/binary.ts +118 -0
- package/tools/tokenizer/byte-fallback.ts +80 -0
- package/tools/tokenizer/model.ts +110 -0
- package/tools/tokenizer/prune.run.ts +37 -0
- package/tools/tokenizer/prune.ts +195 -0
- package/tools/tokenizer/sentencepiece.ts +142 -0
- package/tools/tokenizer/splice.run.ts +141 -0
- package/tools/tokenizer/splice.ts +592 -0
- package/tools/tokenizer/symbols.ts +119 -0
- package/tools/tokenizer/train/text.run.ts +80 -0
- package/tools/tokenizer/train.run.ts +151 -0
- package/tools/tokenizer/train.ts +266 -0
- package/tools/tokenizer/utilization.run.ts +96 -0
- package/tools/tokenizer/verify.run.ts +77 -0
- package/tools/tokenizer/verify.ts +313 -0
- package/wasm/mtf.wasm +0 -0
package/README.md
CHANGED
|
@@ -1,3 +1,222 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @mailwoman/mtf
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The Multilingual Token Framework is Mailwoman's subword tokenizer. It is a Rust core compiled to WASM,
|
|
4
|
+
with a TypeScript loader for Node, browsers and Workers. `@mailwoman/neural`'s `MailwomanTokenizer` runs on
|
|
5
|
+
it in place of `@mailwoman/sentencepiece-wasm`, which is Google SentencePiece v0.2.2 built with
|
|
6
|
+
Emscripten. Issues #2843 and #2844 track the work.
|
|
7
|
+
|
|
8
|
+
The package publishes to npm with the rest of the release. The old package stays in the tree as the
|
|
9
|
+
rollback runtime.
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { MTF_WASM_URL, MTFRuntime } from "@mailwoman/mtf"
|
|
15
|
+
|
|
16
|
+
// Node reads the committed binary from disk. A browser fetches the URL its bundler resolved.
|
|
17
|
+
const runtime = await MTFRuntime.instantiate(new Uint8Array(await readFile(MTF_WASM_URL)))
|
|
18
|
+
using model = runtime.loadModel(tokenizerModelBytes)
|
|
19
|
+
|
|
20
|
+
const { ids, pieces, begins, ends } = model.encode("10 Downing Street, London SW1A 2AA")
|
|
21
|
+
const text = model.decode(ids)
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
The package's import has no side effects, and `instantiate` performs the WASM instantiation. It also accepts a compiled
|
|
25
|
+
`WebAssembly.Module`, as a Worker runtime supplies it. `begins` and `ends` are UTF-8 byte offsets into
|
|
26
|
+
the input. Each result is copied out of WASM memory, so it stays valid after later calls.
|
|
27
|
+
|
|
28
|
+
Rebuild the committed `wasm/mtf.wasm` with `node packages/mtf/tools/build-wasm.run.ts` from the
|
|
29
|
+
repository root. With `--check`, the tool exits non-zero when a fresh build's SHA-256 differs from the
|
|
30
|
+
committed file's.
|
|
31
|
+
|
|
32
|
+
## Plan
|
|
33
|
+
|
|
34
|
+
Mailwoman's address parsing quality and operational cost define success. Google's C++ implementation
|
|
35
|
+
supplies the reference outputs and algorithmic guidance. Parity with it is required while the shipped
|
|
36
|
+
model is in use, and it stops being the acceptance criterion once a redesign ships with matched weights.
|
|
37
|
+
|
|
38
|
+
The work proceeds in two phases.
|
|
39
|
+
|
|
40
|
+
1. **Runtime replacement.** A Rust encoder loads the shipped `tokenizer.model` and reproduces Google
|
|
41
|
+
v0.2.2's IDs, pieces, UTF-8 byte offsets and decode output on every row of
|
|
42
|
+
`test/fixtures/board-reference-v0.2.2.json`. An offline converter may compile the protobuf model into
|
|
43
|
+
a compact runtime format, so the shipped runtime need not parse protobuf. This phase changes no model
|
|
44
|
+
behavior and ships on exact fixture equality plus measured load, latency, memory and size.
|
|
45
|
+
2. **Mailwoman-specific tokenization.** Vocabulary, normalization, boundary and symbol-handling changes are
|
|
46
|
+
experiments. Each one needs a matched training run, regenerated labels and the release verdict that
|
|
47
|
+
`docs/engineering/CONTRIBUTING_MODEL_WORK.mdx` requires. A changed tokenizer is never paired with old
|
|
48
|
+
weights. `docs/records/research/2026-10-09-sentencepiece-vocabulary-audit.md` lists the candidate
|
|
49
|
+
changes: fragmentation of uppercase words and numbers, user-defined symbols that match inside words,
|
|
50
|
+
unused vocabulary rows, and the embedding table's size.
|
|
51
|
+
|
|
52
|
+
### Layout
|
|
53
|
+
|
|
54
|
+
```text
|
|
55
|
+
packages/mtf/ (the crates are members of the repository root's cargo workspace)
|
|
56
|
+
crates/mtf-core/ Model representation, normalization, unigram encode, decode, errors
|
|
57
|
+
crates/mtf-wasm/ Thin wasm-bindgen adapter around mtf-core
|
|
58
|
+
crates/mtf-channels/ The training-row encoder: label projection, channel painters, packing
|
|
59
|
+
crates/mtf-sampler/ The training-row sampler: corpus listing, source mixture, augmentation, relabel, char path
|
|
60
|
+
crates/mtf-node/ The Node addon over mtf-sampler and mtf-channels, for the TypeScript audits and tools
|
|
61
|
+
crates/mtf-tools/ The `mtf-tokenizer` binary behind tools/tokenizer: model edits, corpus sampling, ONNX surgery
|
|
62
|
+
lib/ TypeScript loader and result conversion
|
|
63
|
+
tools/tokenizer/ Tokenizer training, splicing, pruning and checks
|
|
64
|
+
tools/sampler/ The loader for the mtf-node addon, built from source on first use
|
|
65
|
+
wasm/ Committed WASM binary and its build metadata
|
|
66
|
+
test/fixtures/ Reference outputs from the deployed runtime
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
The WASM binary is committed, as `sentencepiece-wasm`'s is, so an ordinary install needs no Rust
|
|
70
|
+
toolchain. A repository check will rebuild it and compare hashes.
|
|
71
|
+
|
|
72
|
+
### Runtime interface
|
|
73
|
+
|
|
74
|
+
- The caller initializes the runtime explicitly, and may supply a compiled `WebAssembly.Module` for
|
|
75
|
+
Workers. The package's import has no side effects. The WASM fetch and instantiation happen in the
|
|
76
|
+
initialization call.
|
|
77
|
+
- A loaded model is a `Disposable` processor object. Its encode and decode methods are synchronous.
|
|
78
|
+
- Results are ordinary JavaScript values. The caller never handles WASM vectors or pointers.
|
|
79
|
+
- A model file records its format version, normalization policy, special IDs and vocabulary hash. The
|
|
80
|
+
loader refuses a model whose semantics it does not implement.
|
|
81
|
+
|
|
82
|
+
### Risks to test early
|
|
83
|
+
|
|
84
|
+
- **Normalization and alignment.** The shipped model uses `nmt_nfkc` with a compiled character map.
|
|
85
|
+
Generic Unicode NFKC is a different function. The Rust encoder must reproduce the compiled map and the
|
|
86
|
+
original-input alignment.
|
|
87
|
+
- **Segmentation ties.** Scores, floating-point precision, tie-breaking and user-defined symbols can move
|
|
88
|
+
token IDs. The fixtures detect such changes.
|
|
89
|
+
- **Byte fallback.** Several byte pieces can represent one character, and only the last owns the
|
|
90
|
+
character's span.
|
|
91
|
+
- **Model loading.** The loader validates lengths, counts and versions before allocating, and bounds input
|
|
92
|
+
length and token count.
|
|
93
|
+
- **Downstream consumers.** `packages/neural/lib/tokenizer.ts` converts byte offsets to UTF-16 ranges and
|
|
94
|
+
trims leading whitespace. The deleted Python tokenizer converted them to Python
|
|
95
|
+
character positions for label projection. A change to `▁` or to word boundaries changes both.
|
|
96
|
+
|
|
97
|
+
## Stage 1 baseline
|
|
98
|
+
|
|
99
|
+
Measured on 2026-10-24 on an AMD Ryzen 9 8945HS with Node v26.2.0.
|
|
100
|
+
|
|
101
|
+
### Shipped model
|
|
102
|
+
|
|
103
|
+
All nine Latin weights packages (`de-de`, `en-au`, `en-gb`, `en-in`, `en-nz`, `en-us`, `es-es`, `fr-fr`,
|
|
104
|
+
`it-it`) ship the same `tokenizer.model`.
|
|
105
|
+
|
|
106
|
+
| Property | Value |
|
|
107
|
+
| ----------------- | ------------------------------------------------------------------ |
|
|
108
|
+
| SHA-256 | `4e9344a8995972d138ed1e3f90a07b55edf0287611eee1ebc8724b45a1eb4b23` |
|
|
109
|
+
| Size | 1,632,289 bytes |
|
|
110
|
+
| Tokenizer version | `0.9.0-multisplice` (model card `tokenizer_version`) |
|
|
111
|
+
| Algorithm | unigram, `byte_fallback=true`, vocabulary 73,143 |
|
|
112
|
+
|
|
113
|
+
The CJK family uses a character encoder and no SentencePiece model.
|
|
114
|
+
|
|
115
|
+
### Consumers of sentencepiece-wasm
|
|
116
|
+
|
|
117
|
+
- `packages/neural/lib/tokenizer.ts` (`MailwomanTokenizer`)
|
|
118
|
+
- `packages/neural/lib/native-offsets.integration.test.ts`
|
|
119
|
+
- `packages/neural/lib/web/browser-slo.integration.test.ts`
|
|
120
|
+
- `packages/mailwoman/tools/dev-tools/man-page.ts`
|
|
121
|
+
|
|
122
|
+
### Current runtime cost
|
|
123
|
+
|
|
124
|
+
The input set is the 1,264 distinct `input` strings in the gauntlet board's two-letter country
|
|
125
|
+
directories (46,721 UTF-8 bytes). Timings come from one process after three warmup passes.
|
|
126
|
+
|
|
127
|
+
| Measure | Value |
|
|
128
|
+
| ------------------------------------------ | ------------------------------------- |
|
|
129
|
+
| `sentencepiece.wasm` / `sentencepiece.mjs` | 625,993 / 57,585 bytes |
|
|
130
|
+
| WASM instantiate | 7.4 ms |
|
|
131
|
+
| Model load (`loadFromSerializedProto`) | 92.0 ms |
|
|
132
|
+
| Full pass, median of 10 | 47.4 ms (26,700 addresses per second) |
|
|
133
|
+
| Per-address encode p50 / p95 | 27.1 µs / 67.8 µs |
|
|
134
|
+
| Tokens | 23,123 (18.3 per address) |
|
|
135
|
+
| Byte-fallback pieces | 48 |
|
|
136
|
+
| Decode differs from input | 10 of 1,264 rows, all from `nmt_nfkc` |
|
|
137
|
+
|
|
138
|
+
Model load costs about twice a full pass over the board. Load time and model size are the first targets for
|
|
139
|
+
the compiled model format.
|
|
140
|
+
|
|
141
|
+
All 10 decode differences come from `nmt_nfkc` normalization: `ª`→`a`, `¾`→`3⁄4`, `½`→`1⁄2`, `№`→`No`,
|
|
142
|
+
`´`→` ́`, and runs of spaces collapsed to one space. The Rust decoder must reproduce them.
|
|
143
|
+
|
|
144
|
+
### Reference fixtures
|
|
145
|
+
|
|
146
|
+
`test/fixtures/board-reference-v0.2.2.json` holds, for each board input, the IDs, pieces, UTF-8 `begins`
|
|
147
|
+
and `ends`, and decode output from the deployed runtime on the shipped model. The deployed WASM is built
|
|
148
|
+
from Google's v0.2.2 tag (`e0cce7d37b065b5140349dbe12c6bcf6192fdd78`), so these outputs are the v0.2.2
|
|
149
|
+
reference for this model. The file is 545,495 bytes with SHA-256
|
|
150
|
+
`440cffd8908a289caebb1df5c36c027a13e54c93002740972cfccd6c8e8fff3e`.
|
|
151
|
+
|
|
152
|
+
## Status
|
|
153
|
+
|
|
154
|
+
| Step | State |
|
|
155
|
+
| -------------------------------------------------------------------------------- | --------------------------------------------------------------- |
|
|
156
|
+
| Workspace registered, plan and baseline recorded | Done |
|
|
157
|
+
| v0.2.2 reference fixtures | Captured |
|
|
158
|
+
| `cargo` admitted by the Bash hook | Done |
|
|
159
|
+
| `crates/mtf-core`: model loader, normalizer, unigram encode with offsets, decode | All 1,264 fixture rows match in IDs, pieces, offsets and decode |
|
|
160
|
+
| WASM adapter and TypeScript loader | All 1,264 fixture rows match through `wasm/mtf.wasm` in Node |
|
|
161
|
+
| `MailwomanTokenizer` on `@mailwoman/mtf` | Neural, mailwoman and browser suites pass |
|
|
162
|
+
| Flat piece index and borrowed piece text in the protobuf loader | WASM load 31.1 ms, from 43.4 ms |
|
|
163
|
+
| Compiled model format | Not started |
|
|
164
|
+
|
|
165
|
+
### Runtime comparison
|
|
166
|
+
|
|
167
|
+
Measured on 2026-10-24 on an AMD Ryzen 9 8945HS with Node v26.2.0, on the shipped `tokenizer.model` and
|
|
168
|
+
the 1,264 board inputs. Each value is the median of seven rounds. Each round ran each runtime in its own
|
|
169
|
+
process, alternating.
|
|
170
|
+
|
|
171
|
+
| Measure | `@mailwoman/mtf` | `@mailwoman/sentencepiece-wasm` |
|
|
172
|
+
| ---------------------------- | ---------------- | --------------------------------------- |
|
|
173
|
+
| WASM binary | 79,874 bytes | 625,993 bytes plus a 57,585-byte loader |
|
|
174
|
+
| WASM instantiate | 0.41 ms | 5.37 ms |
|
|
175
|
+
| Model load | 31.1 ms | 91.9 ms |
|
|
176
|
+
| Full board pass | 8.7 ms | 45.0 ms |
|
|
177
|
+
| Per-address encode p50 / p95 | 6.1 / 15.5 µs | 32.7 / 73.5 µs |
|
|
178
|
+
|
|
179
|
+
Natively, the same load takes 15.2 ms (`cargo run --release --example load_profile`).
|
|
180
|
+
|
|
181
|
+
Run the parity test from this directory with
|
|
182
|
+
`MTF_REFERENCE_MODEL=<path to the shipped tokenizer.model> cargo test --release`. Without the variable it
|
|
183
|
+
reads `../neural-weights-en-us/tokenizer.model` and fails if that file is absent.
|
|
184
|
+
|
|
185
|
+
## Tokenizer tools
|
|
186
|
+
|
|
187
|
+
`tools/tokenizer/` trains, splices, prunes and checks tokenizers. Each entry point is a `.run.ts` file, run from the
|
|
188
|
+
repository root with `node`:
|
|
189
|
+
|
|
190
|
+
| Tool | What it does |
|
|
191
|
+
| ------------------------------- | --------------------------------------------------------------------------------------------- |
|
|
192
|
+
| `train.run.ts` | Samples a corpus's train split per country, trains with `spm_train`, writes the model card |
|
|
193
|
+
| `train-text.run.ts` | Trains the 16,000-piece first-generation recipe on a text file |
|
|
194
|
+
| `splice.run.ts build-tokenizer` | Appends a locale model's diacritic pieces to a frozen tokenizer and checks code point overlap |
|
|
195
|
+
| `splice.run.ts onnx-mean-init` | Grows an exported model's embedding table, and its int8 twin's, onto a spliced tokenizer |
|
|
196
|
+
| `utilization.run.ts` | Counts each piece's use over a corpus's train split |
|
|
197
|
+
| `prune.run.ts` | Drops unused pieces from a tokenizer and the matching rows from the int8 model |
|
|
198
|
+
| `verify.run.ts prune` | Checks a pruned tokenizer and model for segmentation identity and bit-equal logits |
|
|
199
|
+
| `verify.run.ts byte-fallback` | Reports a tokenizer's byte-fallback rate on a fixture, overall and per script |
|
|
200
|
+
|
|
201
|
+
Training runs native `spm_train`, built from google/sentencepiece `v0.2.2` under the cache root on first use. It
|
|
202
|
+
reproduces the Python wheel's model byte for byte: the CLI records every trainer default it set, and the tool drops
|
|
203
|
+
the spec fields the training call did not name. That is the form the wheel writes. `tools/tokenizer/sentencepiece.integration.test.ts`
|
|
204
|
+
checks five recorded wheel trainings against `test/fixtures/spm-recipes/`.
|
|
205
|
+
|
|
206
|
+
The proto edits, parquet reads, CPython-compatible sampling, the byte-fallback count and the ONNX initializer
|
|
207
|
+
rewrite run in `mtf-tokenizer`, which Cargo builds under the `tools` profile on first use. It is a binary rather
|
|
208
|
+
than a Node addon: each tool calls it a few times over files, and a binary needs no addon toolchain or
|
|
209
|
+
per-platform artifact.
|
|
210
|
+
|
|
211
|
+
## License
|
|
212
|
+
|
|
213
|
+
`@mailwoman/mtf` is licensed AGPL-3.0-only, with a commercial license available, like the rest of
|
|
214
|
+
Mailwoman.
|
|
215
|
+
|
|
216
|
+
Six modules in `crates/mtf-core/src` port upstream code: five from Google SentencePiece v0.2.2
|
|
217
|
+
(Apache-2.0) and `darts.rs` from darts-clone (BSD 2-clause). Each ported file keeps the upstream
|
|
218
|
+
copyright notice in its header and states how it was modified. `third-party/` holds both license texts
|
|
219
|
+
unchanged, and `THIRD_PARTY_NOTICES.md` maps each ported file to its upstream source. A later port from
|
|
220
|
+
either project follows the same three steps: keep the upstream notice in the file header, state the
|
|
221
|
+
modification, and add the file to `THIRD_PARTY_NOTICES.md`. Code written here without an upstream
|
|
222
|
+
source has only the AGPL header.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Third-Party Notices
|
|
2
|
+
|
|
3
|
+
`@mailwoman/mtf` is distributed under AGPL-3.0-only, with a commercial license available from Sister
|
|
4
|
+
Software. Its Rust core ports code from the two projects below. Each ported file keeps the upstream
|
|
5
|
+
copyright notice in its header and states that it was modified.
|
|
6
|
+
|
|
7
|
+
## Google SentencePiece — Apache-2.0
|
|
8
|
+
|
|
9
|
+
Source: [google/sentencepiece](https://github.com/google/sentencepiece) v0.2.2, commit
|
|
10
|
+
`e0cce7d37b065b5140349dbe12c6bcf6192fdd78`. Copyright 2016 Google Inc. The full license text is
|
|
11
|
+
`third-party/sentencepiece/LICENSE`. SentencePiece ships no `NOTICE` file.
|
|
12
|
+
|
|
13
|
+
| Ported file | Upstream source |
|
|
14
|
+
| ----------------------------------- | ---------------------------------------------------------- |
|
|
15
|
+
| `crates/mtf-core/src/normalizer.rs` | `src/normalizer.cc` |
|
|
16
|
+
| `crates/mtf-core/src/unigram.rs` | `src/unigram_model.cc` |
|
|
17
|
+
| `crates/mtf-core/src/processor.rs` | `src/sentencepiece_processor.cc`, `src/model_interface.cc` |
|
|
18
|
+
| `crates/mtf-core/src/proto.rs` | `src/sentencepiece_model.proto` |
|
|
19
|
+
| `crates/mtf-core/src/utf8.rs` | `src/util.h`, `src/util.cc` |
|
|
20
|
+
|
|
21
|
+
The name SentencePiece identifies the upstream project. Apache-2.0 §6 grants no right to use it as
|
|
22
|
+
this package's name, and this package does not.
|
|
23
|
+
|
|
24
|
+
## darts-clone — BSD 2-clause
|
|
25
|
+
|
|
26
|
+
Source: darts-clone as vendored in SentencePiece v0.2.2 at `third_party/darts_clone/`. Copyright (c)
|
|
27
|
+
2008-2011, Susumu Yata. The full license text is `third-party/darts-clone/LICENSE`.
|
|
28
|
+
|
|
29
|
+
| Ported file | Upstream source |
|
|
30
|
+
| ------------------------------ | --------------- |
|
|
31
|
+
| `crates/mtf-core/src/darts.rs` | `darts.h` |
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# mtf-channels test fixtures
|
|
2
|
+
|
|
3
|
+
Both files are records of the training encoder this crate replaced (#2870). They were written from
|
|
4
|
+
that implementation before its deletion, so they are not regenerated. Each file states the source revision
|
|
5
|
+
it was recorded at.
|
|
6
|
+
|
|
7
|
+
- `encode-row-reference.json` holds one row encoded by the Python `encode_row` with every soft-feed
|
|
8
|
+
channel on, under both label sources, with the shaped anchor and with no channels. It also holds every
|
|
9
|
+
input the encoder read: pieces, lexicons, the anchor table and the label table.
|
|
10
|
+
- `text-reference.json` holds the postcode-shape matches, code units with acceptance counts, and
|
|
11
|
+
`str.lower`, `str.upper` and `str.strip` results for a list of adversarial texts. For each of 17
|
|
12
|
+
templates, it groups every Unicode scalar value by the answer that value gives when placed in the
|
|
13
|
+
template.
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# mtf-sampler fixtures
|
|
2
|
+
|
|
3
|
+
`cpython-random.json` records CPython 3.12.3's `random.Random` outputs for the methods the training sampler and the
|
|
4
|
+
corpus builders call. `tests/pyrandom_cpython.rs` replays every case through `PyRandom` and requires equal values.
|
|
5
|
+
|
|
6
|
+
The file covers eight seeds: the integers `0`, `42`, `2**31 - 1`, `2**40 + 7` and `-5`, and the strings
|
|
7
|
+
`"20260601:corpus"`, `"42-postcode"` and `""`. For each seed, it records 50 consecutive calls of `random`,
|
|
8
|
+
`getrandbits` at 1, 32 and 33 bits, `randrange(7)`, `randrange(10, 2**33)`, `randint(1, 6)` and `choice` over 1,000
|
|
9
|
+
items, and 40 calls of `getrandbits(100)`. It also records `choices` uniform, with `weights` and with `cum_weights`,
|
|
10
|
+
`sample` on both of CPython's paths (a pool for a small population, a selected set for a large one), ten shuffles of
|
|
11
|
+
50 items, and a 140-step sequence that interleaves seven methods on one generator.
|
|
12
|
+
|
|
13
|
+
A population argument is stored as its size `n`, and its items are `list(range(n))`. An integer of 2**63 or more is
|
|
14
|
+
stored as a decimal string.
|
|
15
|
+
|
|
16
|
+
## Regeneration
|
|
17
|
+
|
|
18
|
+
The file was written on 2026-10-29 by a generator run under CPython 3.12.3. The generator is archived in this README
|
|
19
|
+
as text, so the repository tracks no Python file for it. To regenerate, save the block below as `cpython_random_fixture.py` outside the
|
|
20
|
+
repository and run `python3.12 -I cpython_random_fixture.py packages/mtf/crates/mtf-sampler/tests/fixtures/cpython-random.json`.
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
import json, platform, random, sys
|
|
24
|
+
|
|
25
|
+
SEEDS = [0, 42, 2**31 - 1, 2**40 + 7, -5, "20260601:corpus", "42-postcode", ""]
|
|
26
|
+
N = 50
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def case(seed, method, args, calls):
|
|
30
|
+
rng = random.Random(seed)
|
|
31
|
+
out = []
|
|
32
|
+
for _ in range(calls):
|
|
33
|
+
if method == "choices":
|
|
34
|
+
n, weights, cum_weights, k = args
|
|
35
|
+
out.append(rng.choices(list(range(n)), weights=weights, cum_weights=cum_weights, k=k))
|
|
36
|
+
elif method in ("choice", "sample"):
|
|
37
|
+
out.append(getattr(rng, method)(list(range(args[0])), *args[1:]))
|
|
38
|
+
else:
|
|
39
|
+
value = getattr(rng, method)(*args)
|
|
40
|
+
out.append(str(value) if isinstance(value, int) and value >= 2**63 else value)
|
|
41
|
+
return {"seed": seed, "method": method, "args": args, "outputs": out}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def shuffled(seed, n, rounds):
|
|
45
|
+
rng = random.Random(seed)
|
|
46
|
+
out = []
|
|
47
|
+
for _ in range(rounds):
|
|
48
|
+
items = list(range(n))
|
|
49
|
+
rng.shuffle(items)
|
|
50
|
+
out.append(items)
|
|
51
|
+
return {"seed": seed, "method": "shuffle", "args": [n], "outputs": out}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def mixed(seed):
|
|
55
|
+
rng = random.Random(seed)
|
|
56
|
+
weights = [0.5, 1, 2, 0, 3.25, 1, 1, 0.1, 7]
|
|
57
|
+
trace = []
|
|
58
|
+
for i in range(140):
|
|
59
|
+
kind = i % 7
|
|
60
|
+
if kind == 0:
|
|
61
|
+
trace.append(["random", [], rng.random()])
|
|
62
|
+
elif kind == 1:
|
|
63
|
+
trace.append(["randrange", [3, 1000], rng.randrange(3, 1000)])
|
|
64
|
+
elif kind == 2:
|
|
65
|
+
trace.append(["choices", [list(range(9)), weights, 4], rng.choices(list(range(9)), weights=weights, k=4)])
|
|
66
|
+
elif kind == 3:
|
|
67
|
+
items = list(range(17))
|
|
68
|
+
rng.shuffle(items)
|
|
69
|
+
trace.append(["shuffle", [17], items])
|
|
70
|
+
elif kind == 4:
|
|
71
|
+
trace.append(["sample", [40, 6], rng.sample(range(40), 6)])
|
|
72
|
+
elif kind == 5:
|
|
73
|
+
trace.append(["randint", [-3, 3], rng.randint(-3, 3)])
|
|
74
|
+
else:
|
|
75
|
+
trace.append(["choice", [11], rng.choice(range(11))])
|
|
76
|
+
return {"seed": seed, "method": "mixed", "args": [], "outputs": trace}
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
cases = []
|
|
80
|
+
for seed in SEEDS:
|
|
81
|
+
cases.append(case(seed, "random", [], N))
|
|
82
|
+
cases.append(case(seed, "getrandbits", [1], N))
|
|
83
|
+
cases.append(case(seed, "getrandbits", [32], N))
|
|
84
|
+
cases.append(case(seed, "getrandbits", [33], N))
|
|
85
|
+
cases.append(case(seed, "getrandbits", [100], 40))
|
|
86
|
+
cases.append(case(seed, "randrange", [7], N))
|
|
87
|
+
cases.append(case(seed, "randrange", [10, 2**33], N))
|
|
88
|
+
cases.append(case(seed, "randint", [1, 6], N))
|
|
89
|
+
cases.append(case(seed, "choice", [1000], N))
|
|
90
|
+
cases.append(case(seed, "choices", [50, None, None, 25], 8))
|
|
91
|
+
cases.append(case(seed, "choices", [5, [1, 0, 2.5, 0.001, 9], None, 25], 8))
|
|
92
|
+
cases.append(case(seed, "choices", [4, None, [1, 3, 3, 10], 25], 8))
|
|
93
|
+
cases.append(case(seed, "sample", [30, 10], 20))
|
|
94
|
+
cases.append(case(seed, "sample", [10000, 50], 10))
|
|
95
|
+
cases.append(case(seed, "sample", [500, 5], 20))
|
|
96
|
+
cases.append(shuffled(seed, 50, 10))
|
|
97
|
+
cases.append(mixed(seed))
|
|
98
|
+
|
|
99
|
+
doc = {
|
|
100
|
+
"generator": "cpython_random_fixture.py (archived in the fixture README)",
|
|
101
|
+
"python": sys.version.split()[0],
|
|
102
|
+
"implementation": platform.python_implementation(),
|
|
103
|
+
"cases": cases,
|
|
104
|
+
}
|
|
105
|
+
with open(sys.argv[1], "w", encoding="utf-8") as out:
|
|
106
|
+
json.dump(doc, out, separators=(",", ":"))
|
|
107
|
+
out.write("\n")
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## The loader-split reference
|
|
111
|
+
|
|
112
|
+
`loader-split-reference.json` records what the Python loader drew from the corpus `build_reference_corpus` in
|
|
113
|
+
`tests/common/mod.rs` writes: two sources, three countries with unequal country and source weights, several row groups
|
|
114
|
+
per file, a mixed-source validation file, and every augmentation before the postcode move on. `rows` is one pass of
|
|
115
|
+
the train stream with seed 11 and a shuffle buffer of 6, as `source|country|raw`; `val_rows` is the same over the
|
|
116
|
+
validation split; `encoded` is one pass of the char path in word mode with the default shuffle buffer, as each row's
|
|
117
|
+
labels, attention and first three units of char ids; `source_row_counts` is the rows per source. The Python loader
|
|
118
|
+
wrote it before the Rust sampler replaced it, and `tests/loader_split_reference.rs` requires the Rust sampler to
|
|
119
|
+
reproduce every value in order. It is a record of the Python sampler's output and is not regenerated from the Rust
|
|
120
|
+
sampler.
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
* @file Code units: each digit run or alphanumeric code in an address, segmented as one unit (#2845).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { TextSpan } from "@mailwoman/query-shape/types"
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* One code unit and its half-open UTF-16 offsets into the text.
|
|
12
|
+
*/
|
|
13
|
+
export type CodeUnit = TextSpan
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A run of ASCII letters and digits holding at least one digit, with a hyphen
|
|
17
|
+
* allowed between two digits (`12345-6789`, `100-0005`).
|
|
18
|
+
*
|
|
19
|
+
* The run must not touch another ASCII letter or digit on either side, so `B2B` and `SW1A` are whole units.
|
|
20
|
+
* A Han or Kana character next to a run is a boundary, because Japanese and Chinese
|
|
21
|
+
* addresses write a block number straight after its place name (`丸の内1-9-1`).
|
|
22
|
+
*/
|
|
23
|
+
const CODE_UNIT = /(?<![A-Za-z0-9])(?=[A-Za-z0-9-]*\d)[A-Za-z0-9]+(?:-(?=\d)[A-Za-z0-9]+)*(?![A-Za-z0-9])/gu
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Every code unit in `text`, left to right.
|
|
27
|
+
*
|
|
28
|
+
* A word-piece vocabulary makes a postcode one piece only by memorizing that code.
|
|
29
|
+
* A code unit keeps the whole code together and describes it by its shape instead.
|
|
30
|
+
*/
|
|
31
|
+
export function segmentCodeUnits(text: string): CodeUnit[] {
|
|
32
|
+
return [...text.matchAll(CODE_UNIT)].map((match) => ({
|
|
33
|
+
text: match[0],
|
|
34
|
+
start: match.index,
|
|
35
|
+
end: match.index + match[0].length,
|
|
36
|
+
}))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The unit's character pattern: `9` per digit, `A` per letter, other characters
|
|
41
|
+
* kept (`SW1A` → `AA9A`, `12345-6789` → `99999-9999`).
|
|
42
|
+
*/
|
|
43
|
+
export function codeUnitPattern(unit: string): string {
|
|
44
|
+
return unit.replaceAll(/\d/g, "9").replaceAll(/[A-Za-z]/g, "A")
|
|
45
|
+
}
|