@mailwoman/mtf 10.7.0 → 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 +35 -5
- 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/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/tools/build-wasm.run.js +2 -2
- package/out/tools/build-wasm.run.js.map +1 -1
- 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 +15 -3
- package/tools/build-wasm.run.ts +2 -2
- 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
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* Splice locale diacritic pieces into a frozen tokenizer, or grow an exported model onto a spliced tokenizer.
|
|
7
|
+
*
|
|
8
|
+
* `build-tokenizer` reads the locales' OpenAddresses street and city names, trains a locale unigram model on them,
|
|
9
|
+
* appends that model's pieces that hold a code point outside ASCII to the base tokenizer, and checks that English
|
|
10
|
+
* tokenizes as before. With `--trained-samples`, it also writes the code point overlap report beside the spliced
|
|
11
|
+
* tokenizer and fails on an overlap no `--accept-overlap` entry accepts.
|
|
12
|
+
*
|
|
13
|
+
* `onnx-mean-init` grows an exported model's token embedding table, and its int8 twin's, to the spliced vocabulary.
|
|
14
|
+
*
|
|
15
|
+
* Usage:
|
|
16
|
+
*
|
|
17
|
+
* node packages/mtf/tools/tokenizer/splice.run.ts build-tokenizer --oa-root <dir> --base-tokenizer <model>
|
|
18
|
+
* --out-tokenizer <model> [--locales cz,pl,sk,si] [--vocab-size 24000] [--per-file-cap 400000]
|
|
19
|
+
* [--work-dir out/bsplice-work] [--trained-samples fr=<path>,de=<path>] [--accept-overlap fr]
|
|
20
|
+
* [--extra-text <txt>] [--extra-repeat 10] [--spm-train <path>]
|
|
21
|
+
* node packages/mtf/tools/tokenizer/splice.run.ts onnx-mean-init --fp32-onnx <onnx> --base-tokenizer <model>
|
|
22
|
+
* --spliced-tokenizer <model> --out-fp32 <onnx> [--int8-onnx <onnx> --out-int8 <onnx>]
|
|
23
|
+
*
|
|
24
|
+
* A per-file cap that reads a whole small regional file can read only the first departments of a country published
|
|
25
|
+
* as one national file, such as France's. Raise it, or pass a sampled national extract, to keep the corpus spread
|
|
26
|
+
* across the country.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import { makeDirectories } from "@mailwoman/core/fs/writers"
|
|
30
|
+
import { parseArguments, requiredArgument } from "@mailwoman/core/scripting/arguments"
|
|
31
|
+
import { resolvePath } from "path-ts"
|
|
32
|
+
|
|
33
|
+
import { spmTrainBinary } from "./binary.ts"
|
|
34
|
+
import {
|
|
35
|
+
buildSlavicCorpus,
|
|
36
|
+
checkCodepointOverlap,
|
|
37
|
+
meanInitONNXEmbeddings,
|
|
38
|
+
spliceVocabulary,
|
|
39
|
+
trainDiacriticSentencePiece,
|
|
40
|
+
} from "./splice.ts"
|
|
41
|
+
|
|
42
|
+
const { values, positionals } = parseArguments({
|
|
43
|
+
allowPositionals: true,
|
|
44
|
+
options: {
|
|
45
|
+
"oa-root": { type: "string" },
|
|
46
|
+
locales: { type: "string", default: "cz,pl,sk,si" },
|
|
47
|
+
"base-tokenizer": { type: "string" },
|
|
48
|
+
"out-tokenizer": { type: "string" },
|
|
49
|
+
"vocab-size": { type: "string", default: "24000" },
|
|
50
|
+
"per-file-cap": { type: "string", default: "400000" },
|
|
51
|
+
"work-dir": { type: "string", default: "out/bsplice-work" },
|
|
52
|
+
"trained-samples": { type: "string", default: "" },
|
|
53
|
+
"extra-text": { type: "string" },
|
|
54
|
+
"extra-repeat": { type: "string", default: "10" },
|
|
55
|
+
"accept-overlap": { type: "string", default: "" },
|
|
56
|
+
"spm-train": { type: "string" },
|
|
57
|
+
"fp32-onnx": { type: "string" },
|
|
58
|
+
"spliced-tokenizer": { type: "string" },
|
|
59
|
+
"out-fp32": { type: "string" },
|
|
60
|
+
"int8-onnx": { type: "string" },
|
|
61
|
+
"out-int8": { type: "string" },
|
|
62
|
+
},
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
function list(value: string): string[] {
|
|
66
|
+
return value
|
|
67
|
+
.split(",")
|
|
68
|
+
.map((entry) => entry.trim())
|
|
69
|
+
.filter((entry) => entry.length > 0)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const command = positionals[0]
|
|
73
|
+
|
|
74
|
+
if (command === "build-tokenizer") {
|
|
75
|
+
const workDirectory = resolvePath(values["work-dir"])
|
|
76
|
+
const baseTokenizer = resolvePath(requiredArgument(command, "base-tokenizer", values["base-tokenizer"]))
|
|
77
|
+
const outTokenizer = resolvePath(requiredArgument(command, "out-tokenizer", values["out-tokenizer"]))
|
|
78
|
+
const corpus = `${workDirectory}/slavic-corpus.txt`
|
|
79
|
+
|
|
80
|
+
await makeDirectories(workDirectory)
|
|
81
|
+
|
|
82
|
+
const lines = await buildSlavicCorpus({
|
|
83
|
+
oaRoot: resolvePath(requiredArgument(command, "oa-root", values["oa-root"])),
|
|
84
|
+
locales: values.locales.split(","),
|
|
85
|
+
outPath: corpus,
|
|
86
|
+
perFileCap: Number(values["per-file-cap"]),
|
|
87
|
+
extraText: values["extra-text"] ?? null,
|
|
88
|
+
extraRepeat: Number(values["extra-repeat"]),
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
console.log(`corpus: ${lines} lines`)
|
|
92
|
+
|
|
93
|
+
const donor = await trainDiacriticSentencePiece(corpus, `${workDirectory}/slavic-sp`, {
|
|
94
|
+
spmTrain: await spmTrainBinary(values["spm-train"]),
|
|
95
|
+
vocabSize: Number(values["vocab-size"]),
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
const pieces = await spliceVocabulary(baseTokenizer, donor, outTokenizer)
|
|
99
|
+
|
|
100
|
+
console.log(`spliced ${pieces.length} diacritic pieces -> ${outTokenizer}`)
|
|
101
|
+
console.log("English tokenization verified identical (0 differences).")
|
|
102
|
+
|
|
103
|
+
if (values["trained-samples"]) {
|
|
104
|
+
const samples = Object.fromEntries(
|
|
105
|
+
list(values["trained-samples"]).map((pair) => {
|
|
106
|
+
const at = pair.indexOf("=")
|
|
107
|
+
|
|
108
|
+
return [pair.slice(0, at).trim(), pair.slice(at + 1).trim()]
|
|
109
|
+
})
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
const reportPath = outTokenizer.replace(/\.[^./]*$/, "") + ".overlap-report.json"
|
|
113
|
+
const report = await checkCodepointOverlap(pieces, samples, reportPath, new Set(list(values["accept-overlap"])))
|
|
114
|
+
const overlapping = Object.entries(report).flatMap(([locale, codepoints]) => (codepoints.length ? [locale] : []))
|
|
115
|
+
|
|
116
|
+
console.log(
|
|
117
|
+
`overlap check passed; report ${reportPath}; overlapping (accepted): ${overlapping.join(", ") || "none"}`
|
|
118
|
+
)
|
|
119
|
+
}
|
|
120
|
+
} else if (command === "onnx-mean-init") {
|
|
121
|
+
const outFp32 = resolvePath(requiredArgument(command, "out-fp32", values["out-fp32"]))
|
|
122
|
+
|
|
123
|
+
const [oldVocab, newVocab] = await meanInitONNXEmbeddings({
|
|
124
|
+
fp32ONNX: resolvePath(requiredArgument(command, "fp32-onnx", values["fp32-onnx"])),
|
|
125
|
+
baseTokenizer: resolvePath(requiredArgument(command, "base-tokenizer", values["base-tokenizer"])),
|
|
126
|
+
splicedTokenizer: resolvePath(requiredArgument(command, "spliced-tokenizer", values["spliced-tokenizer"])),
|
|
127
|
+
outFp32,
|
|
128
|
+
int8ONNX: values["int8-onnx"] ?? null,
|
|
129
|
+
outInt8: values["out-int8"] ?? null,
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
console.log(`expanded ONNX token_embeddings ${oldVocab} -> ${newVocab}; wrote ${outFp32}`)
|
|
133
|
+
|
|
134
|
+
if (values["out-int8"]) {
|
|
135
|
+
console.log(` int8 twin written ${values["out-int8"]}`)
|
|
136
|
+
}
|
|
137
|
+
} else {
|
|
138
|
+
console.error("usage: splice.run.ts build-tokenizer|onnx-mean-init [options]")
|
|
139
|
+
|
|
140
|
+
process.exitCode = 2
|
|
141
|
+
}
|