@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
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
export {};
|
|
29
|
+
//# sourceMappingURL=splice.run.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"splice.run.d.ts","sourceRoot":"","sources":["../../../tools/tokenizer/splice.run.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG"}
|
|
@@ -0,0 +1,114 @@
|
|
|
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
|
+
import { makeDirectories } from "@mailwoman/core/fs/writers";
|
|
29
|
+
import { parseArguments, requiredArgument } from "@mailwoman/core/scripting/arguments";
|
|
30
|
+
import { resolvePath } from "path-ts";
|
|
31
|
+
import { spmTrainBinary } from "./binary.js";
|
|
32
|
+
import { buildSlavicCorpus, checkCodepointOverlap, meanInitONNXEmbeddings, spliceVocabulary, trainDiacriticSentencePiece, } from "./splice.js";
|
|
33
|
+
const { values, positionals } = parseArguments({
|
|
34
|
+
allowPositionals: true,
|
|
35
|
+
options: {
|
|
36
|
+
"oa-root": { type: "string" },
|
|
37
|
+
locales: { type: "string", default: "cz,pl,sk,si" },
|
|
38
|
+
"base-tokenizer": { type: "string" },
|
|
39
|
+
"out-tokenizer": { type: "string" },
|
|
40
|
+
"vocab-size": { type: "string", default: "24000" },
|
|
41
|
+
"per-file-cap": { type: "string", default: "400000" },
|
|
42
|
+
"work-dir": { type: "string", default: "out/bsplice-work" },
|
|
43
|
+
"trained-samples": { type: "string", default: "" },
|
|
44
|
+
"extra-text": { type: "string" },
|
|
45
|
+
"extra-repeat": { type: "string", default: "10" },
|
|
46
|
+
"accept-overlap": { type: "string", default: "" },
|
|
47
|
+
"spm-train": { type: "string" },
|
|
48
|
+
"fp32-onnx": { type: "string" },
|
|
49
|
+
"spliced-tokenizer": { type: "string" },
|
|
50
|
+
"out-fp32": { type: "string" },
|
|
51
|
+
"int8-onnx": { type: "string" },
|
|
52
|
+
"out-int8": { type: "string" },
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
function list(value) {
|
|
56
|
+
return value
|
|
57
|
+
.split(",")
|
|
58
|
+
.map((entry) => entry.trim())
|
|
59
|
+
.filter((entry) => entry.length > 0);
|
|
60
|
+
}
|
|
61
|
+
const command = positionals[0];
|
|
62
|
+
if (command === "build-tokenizer") {
|
|
63
|
+
const workDirectory = resolvePath(values["work-dir"]);
|
|
64
|
+
const baseTokenizer = resolvePath(requiredArgument(command, "base-tokenizer", values["base-tokenizer"]));
|
|
65
|
+
const outTokenizer = resolvePath(requiredArgument(command, "out-tokenizer", values["out-tokenizer"]));
|
|
66
|
+
const corpus = `${workDirectory}/slavic-corpus.txt`;
|
|
67
|
+
await makeDirectories(workDirectory);
|
|
68
|
+
const lines = await buildSlavicCorpus({
|
|
69
|
+
oaRoot: resolvePath(requiredArgument(command, "oa-root", values["oa-root"])),
|
|
70
|
+
locales: values.locales.split(","),
|
|
71
|
+
outPath: corpus,
|
|
72
|
+
perFileCap: Number(values["per-file-cap"]),
|
|
73
|
+
extraText: values["extra-text"] ?? null,
|
|
74
|
+
extraRepeat: Number(values["extra-repeat"]),
|
|
75
|
+
});
|
|
76
|
+
console.log(`corpus: ${lines} lines`);
|
|
77
|
+
const donor = await trainDiacriticSentencePiece(corpus, `${workDirectory}/slavic-sp`, {
|
|
78
|
+
spmTrain: await spmTrainBinary(values["spm-train"]),
|
|
79
|
+
vocabSize: Number(values["vocab-size"]),
|
|
80
|
+
});
|
|
81
|
+
const pieces = await spliceVocabulary(baseTokenizer, donor, outTokenizer);
|
|
82
|
+
console.log(`spliced ${pieces.length} diacritic pieces -> ${outTokenizer}`);
|
|
83
|
+
console.log("English tokenization verified identical (0 differences).");
|
|
84
|
+
if (values["trained-samples"]) {
|
|
85
|
+
const samples = Object.fromEntries(list(values["trained-samples"]).map((pair) => {
|
|
86
|
+
const at = pair.indexOf("=");
|
|
87
|
+
return [pair.slice(0, at).trim(), pair.slice(at + 1).trim()];
|
|
88
|
+
}));
|
|
89
|
+
const reportPath = outTokenizer.replace(/\.[^./]*$/, "") + ".overlap-report.json";
|
|
90
|
+
const report = await checkCodepointOverlap(pieces, samples, reportPath, new Set(list(values["accept-overlap"])));
|
|
91
|
+
const overlapping = Object.entries(report).flatMap(([locale, codepoints]) => (codepoints.length ? [locale] : []));
|
|
92
|
+
console.log(`overlap check passed; report ${reportPath}; overlapping (accepted): ${overlapping.join(", ") || "none"}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
else if (command === "onnx-mean-init") {
|
|
96
|
+
const outFp32 = resolvePath(requiredArgument(command, "out-fp32", values["out-fp32"]));
|
|
97
|
+
const [oldVocab, newVocab] = await meanInitONNXEmbeddings({
|
|
98
|
+
fp32ONNX: resolvePath(requiredArgument(command, "fp32-onnx", values["fp32-onnx"])),
|
|
99
|
+
baseTokenizer: resolvePath(requiredArgument(command, "base-tokenizer", values["base-tokenizer"])),
|
|
100
|
+
splicedTokenizer: resolvePath(requiredArgument(command, "spliced-tokenizer", values["spliced-tokenizer"])),
|
|
101
|
+
outFp32,
|
|
102
|
+
int8ONNX: values["int8-onnx"] ?? null,
|
|
103
|
+
outInt8: values["out-int8"] ?? null,
|
|
104
|
+
});
|
|
105
|
+
console.log(`expanded ONNX token_embeddings ${oldVocab} -> ${newVocab}; wrote ${outFp32}`);
|
|
106
|
+
if (values["out-int8"]) {
|
|
107
|
+
console.log(` int8 twin written ${values["out-int8"]}`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
console.error("usage: splice.run.ts build-tokenizer|onnx-mean-init [options]");
|
|
112
|
+
process.exitCode = 2;
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=splice.run.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"splice.run.js","sourceRoot":"","sources":["../../../tools/tokenizer/splice.run.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAA;AAC5D,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,qCAAqC,CAAA;AACtF,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAErC,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAC5C,OAAO,EACN,iBAAiB,EACjB,qBAAqB,EACrB,sBAAsB,EACtB,gBAAgB,EAChB,2BAA2B,GAC3B,MAAM,aAAa,CAAA;AAEpB,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,cAAc,CAAC;IAC9C,gBAAgB,EAAE,IAAI;IACtB,OAAO,EAAE;QACR,SAAS,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QAC7B,OAAO,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,aAAa,EAAE;QACnD,gBAAgB,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QACpC,eAAe,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QACnC,YAAY,EAAE,EAAE,IAAI,EAAE,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,EAAE,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAA;IAEtF,MAAM,CAAC,QAAQ,EAAE,QAAQ,CAAC,GAAG,MAAM,sBAAsB,CAAC;QACzD,QAAQ,EAAE,WAAW,CAAC,gBAAgB,CAAC,OAAO,EAAE,WAAW,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC;QAClF,aAAa,EAAE,WAAW,CAAC,gBAAgB,CAAC,OAAO,EAAE,gBAAgB,EAAE,MAAM,CAAC,gBAAgB,CAAC,CAAC,CAAC;QACjG,gBAAgB,EAAE,WAAW,CAAC,gBAAgB,CAAC,OAAO,EAAE,mBAAmB,EAAE,MAAM,CAAC,mBAAmB,CAAC,CAAC,CAAC;QAC1G,OAAO;QACP,QAAQ,EAAE,MAAM,CAAC,WAAW,CAAC,IAAI,IAAI;QACrC,OAAO,EAAE,MAAM,CAAC,UAAU,CAAC,IAAI,IAAI;KACnC,CAAC,CAAA;IAEF,OAAO,CAAC,GAAG,CAAC,kCAAkC,QAAQ,OAAO,QAAQ,WAAW,OAAO,EAAE,CAAC,CAAA;IAE1F,IAAI,MAAM,CAAC,UAAU,CAAC,EAAE,CAAC;QACxB,OAAO,CAAC,GAAG,CAAC,uBAAuB,MAAM,CAAC,UAAU,CAAC,EAAE,CAAC,CAAA;IACzD,CAAC;AACF,CAAC;KAAM,CAAC;IACP,OAAO,CAAC,KAAK,CAAC,+DAA+D,CAAC,CAAA;IAE9E,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAA;AACrB,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* The strings a trained tokenizer keeps whole, and how they reach SentencePiece.
|
|
7
|
+
*
|
|
8
|
+
* A user-defined symbol bypasses unigram inference and is always emitted as one piece. The literal a caller writes
|
|
9
|
+
* is not the literal SentencePiece matches: it turns whitespace into `▁` before matching, so `PO Box` is passed as
|
|
10
|
+
* `PO▁Box`. The symbols also compete with the vocabulary budget, so a list longer than a quarter of the vocabulary
|
|
11
|
+
* is truncated.
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* The default symbols: US state and territory codes, country abbreviations
|
|
15
|
+
* and names, and postal-form anchors.
|
|
16
|
+
*
|
|
17
|
+
* A state code beside a postcode (`NY 10001`) stays one piece, so the region and postcode stay adjacent.
|
|
18
|
+
* Postal markers (`PO Box`, `Cedex`, `BP`) are fixed surface forms that are
|
|
19
|
+
* cheaper to add once than to learn.
|
|
20
|
+
*
|
|
21
|
+
* A hyphen is not listed because it already tokenizes as one piece.
|
|
22
|
+
* Whole Japanese postcodes come from mined postcode literals.
|
|
23
|
+
*
|
|
24
|
+
* The list keeps its duplicates (`CA`, `DE`), which the trainer removes.
|
|
25
|
+
*/
|
|
26
|
+
export declare const DEFAULT_USER_DEFINED_SYMBOLS: readonly string[];
|
|
27
|
+
/**
|
|
28
|
+
* One literal per line.
|
|
29
|
+
*
|
|
30
|
+
* A blank line or a `#` comment is skipped, and only line ends are trimmed,
|
|
31
|
+
* because a symbol may hold a space (`PO Box`).
|
|
32
|
+
*/
|
|
33
|
+
export declare function readUserDefinedSymbolsFile(path: string): Promise<string[]>;
|
|
34
|
+
/**
|
|
35
|
+
* A symbol with each ASCII space replaced by SentencePiece's whitespace placeholder, `▁`.
|
|
36
|
+
*/
|
|
37
|
+
export declare function sentencePieceSymbol(symbol: string): string;
|
|
38
|
+
/**
|
|
39
|
+
* The non-empty entries, first occurrences only, in order.
|
|
40
|
+
*/
|
|
41
|
+
export declare function dedupeKeepOrder(items: Iterable<string>): string[];
|
|
42
|
+
//# sourceMappingURL=symbols.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"symbols.d.ts","sourceRoot":"","sources":["../../../tools/tokenizer/symbols.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAKH;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,4BAA4B,EAAE,SAAS,MAAM,EA+DzD,CAAA;AAED;;;;;GAKG;AACH,wBAAsB,0BAA0B,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAIhF;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAE1D;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,GAAG,MAAM,EAAE,CAEjE"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* The strings a trained tokenizer keeps whole, and how they reach SentencePiece.
|
|
7
|
+
*
|
|
8
|
+
* A user-defined symbol bypasses unigram inference and is always emitted as one piece. The literal a caller writes
|
|
9
|
+
* is not the literal SentencePiece matches: it turns whitespace into `▁` before matching, so `PO Box` is passed as
|
|
10
|
+
* `PO▁Box`. The symbols also compete with the vocabulary budget, so a list longer than a quarter of the vocabulary
|
|
11
|
+
* is truncated.
|
|
12
|
+
*/
|
|
13
|
+
import { readLocalTextFile } from "@mailwoman/core/fs/readers";
|
|
14
|
+
import { splitLines } from "@mailwoman/core/strings/unicode";
|
|
15
|
+
/**
|
|
16
|
+
* The default symbols: US state and territory codes, country abbreviations
|
|
17
|
+
* and names, and postal-form anchors.
|
|
18
|
+
*
|
|
19
|
+
* A state code beside a postcode (`NY 10001`) stays one piece, so the region and postcode stay adjacent.
|
|
20
|
+
* Postal markers (`PO Box`, `Cedex`, `BP`) are fixed surface forms that are
|
|
21
|
+
* cheaper to add once than to learn.
|
|
22
|
+
*
|
|
23
|
+
* A hyphen is not listed because it already tokenizes as one piece.
|
|
24
|
+
* Whole Japanese postcodes come from mined postcode literals.
|
|
25
|
+
*
|
|
26
|
+
* The list keeps its duplicates (`CA`, `DE`), which the trainer removes.
|
|
27
|
+
*/
|
|
28
|
+
export const DEFAULT_USER_DEFINED_SYMBOLS = [
|
|
29
|
+
// US states and territories.
|
|
30
|
+
..."AL AK AZ AR CA CO CT DE FL GA HI ID IL IN IA KS KY LA ME MD MA MI MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA RI SC SD TN TX UT VT VA WA WV WI WY DC PR VI GU AS MP".split(" "),
|
|
31
|
+
// Country abbreviations and names.
|
|
32
|
+
"USA",
|
|
33
|
+
"US",
|
|
34
|
+
"U.S.",
|
|
35
|
+
"U.S.A.",
|
|
36
|
+
"FR",
|
|
37
|
+
"FRA",
|
|
38
|
+
"France",
|
|
39
|
+
"JP",
|
|
40
|
+
"JPN",
|
|
41
|
+
"Japan",
|
|
42
|
+
"GB",
|
|
43
|
+
"UK",
|
|
44
|
+
"U.K.",
|
|
45
|
+
"DE",
|
|
46
|
+
"DEU",
|
|
47
|
+
"Germany",
|
|
48
|
+
"IT",
|
|
49
|
+
"ITA",
|
|
50
|
+
"Italy",
|
|
51
|
+
"ES",
|
|
52
|
+
"ESP",
|
|
53
|
+
"Spain",
|
|
54
|
+
"NL",
|
|
55
|
+
"NLD",
|
|
56
|
+
"Netherlands",
|
|
57
|
+
"CA",
|
|
58
|
+
"CAN",
|
|
59
|
+
"Canada",
|
|
60
|
+
"AU",
|
|
61
|
+
"AUS",
|
|
62
|
+
"Australia",
|
|
63
|
+
"CH",
|
|
64
|
+
"CHE",
|
|
65
|
+
"Switzerland",
|
|
66
|
+
"BE",
|
|
67
|
+
"BEL",
|
|
68
|
+
"Belgium",
|
|
69
|
+
"AT",
|
|
70
|
+
"AUT",
|
|
71
|
+
"Austria",
|
|
72
|
+
"SE",
|
|
73
|
+
"SWE",
|
|
74
|
+
"Sweden",
|
|
75
|
+
"RU",
|
|
76
|
+
"RUS",
|
|
77
|
+
// Postal-form anchors.
|
|
78
|
+
"PO Box",
|
|
79
|
+
"P.O. Box",
|
|
80
|
+
"P.O.Box",
|
|
81
|
+
"POB",
|
|
82
|
+
"Apt",
|
|
83
|
+
"Apt.",
|
|
84
|
+
"Suite",
|
|
85
|
+
"Ste",
|
|
86
|
+
"Cedex",
|
|
87
|
+
"CEDEX",
|
|
88
|
+
"BP",
|
|
89
|
+
];
|
|
90
|
+
/**
|
|
91
|
+
* One literal per line.
|
|
92
|
+
*
|
|
93
|
+
* A blank line or a `#` comment is skipped, and only line ends are trimmed,
|
|
94
|
+
* because a symbol may hold a space (`PO Box`).
|
|
95
|
+
*/
|
|
96
|
+
export async function readUserDefinedSymbolsFile(path) {
|
|
97
|
+
return splitLines(await readLocalTextFile(path))
|
|
98
|
+
.map((line) => line.trimEnd())
|
|
99
|
+
.filter((line) => line && !line.trim().startsWith("#"));
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* A symbol with each ASCII space replaced by SentencePiece's whitespace placeholder, `▁`.
|
|
103
|
+
*/
|
|
104
|
+
export function sentencePieceSymbol(symbol) {
|
|
105
|
+
return symbol.replaceAll(" ", "▁");
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* The non-empty entries, first occurrences only, in order.
|
|
109
|
+
*/
|
|
110
|
+
export function dedupeKeepOrder(items) {
|
|
111
|
+
return [...new Set([...items].filter((item) => item.length > 0))];
|
|
112
|
+
}
|
|
113
|
+
//# sourceMappingURL=symbols.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"symbols.js","sourceRoot":"","sources":["../../../tools/tokenizer/symbols.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAA;AAC9D,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAE5D;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,MAAM,4BAA4B,GAAsB;IAC9D,6BAA6B;IAC7B,GAAG,yKAAyK,CAA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,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;AAClE,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* Train the first-generation tokenizer recipe on a text file of raw addresses, one per line.
|
|
7
|
+
*
|
|
8
|
+
* The recipe is fixed: a 16,000-piece unigram vocabulary, 0.9995 character coverage, byte fallback, the four special
|
|
9
|
+
* ids, and no user-defined symbols. It writes `tokenizer.model`, `tokenizer.vocab` and `META.json` under `--output`.
|
|
10
|
+
* `--version` must match the corpus version the tokenizer ships with.
|
|
11
|
+
*
|
|
12
|
+
* Usage: `node packages/mtf/tools/tokenizer/train/text.run.ts --input <txt> --output <dir> --version <v>
|
|
13
|
+
* [--spm-train <path>]`
|
|
14
|
+
*/
|
|
15
|
+
export {};
|
|
16
|
+
//# sourceMappingURL=text.run.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text.run.d.ts","sourceRoot":"","sources":["../../../../tools/tokenizer/train/text.run.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* Train the first-generation tokenizer recipe on a text file of raw addresses, one per line.
|
|
7
|
+
*
|
|
8
|
+
* The recipe is fixed: a 16,000-piece unigram vocabulary, 0.9995 character coverage, byte fallback, the four special
|
|
9
|
+
* ids, and no user-defined symbols. It writes `tokenizer.model`, `tokenizer.vocab` and `META.json` under `--output`.
|
|
10
|
+
* `--version` must match the corpus version the tokenizer ships with.
|
|
11
|
+
*
|
|
12
|
+
* Usage: `node packages/mtf/tools/tokenizer/train/text.run.ts --input <txt> --output <dir> --version <v>
|
|
13
|
+
* [--spm-train <path>]`
|
|
14
|
+
*/
|
|
15
|
+
import { readLocalTextFile } from "@mailwoman/core/fs/readers";
|
|
16
|
+
import { temporaryDirectory } from "@mailwoman/core/fs/temporary";
|
|
17
|
+
import { makeDirectories, writeLocalJSONFile } from "@mailwoman/core/fs/writers";
|
|
18
|
+
import { gitHead } from "@mailwoman/core/git";
|
|
19
|
+
import { sha256File } from "@mailwoman/core/hash";
|
|
20
|
+
import { prettyJSON } from "@mailwoman/core/json";
|
|
21
|
+
import { parseArguments, requiredArgument } from "@mailwoman/core/scripting/arguments";
|
|
22
|
+
import { resolvePath } from "path-ts";
|
|
23
|
+
import { spmTrainBinary } from "../binary.js";
|
|
24
|
+
import { trainSentencePiece } from "../sentencepiece.js";
|
|
25
|
+
const RECIPE = {
|
|
26
|
+
vocab_size: 16_000,
|
|
27
|
+
character_coverage: 0.9995,
|
|
28
|
+
model_type: "unigram",
|
|
29
|
+
byte_fallback: true,
|
|
30
|
+
};
|
|
31
|
+
const { values } = parseArguments({
|
|
32
|
+
options: {
|
|
33
|
+
input: { type: "string" },
|
|
34
|
+
output: { type: "string" },
|
|
35
|
+
version: { type: "string" },
|
|
36
|
+
"spm-train": { type: "string" },
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
const input = resolvePath(requiredArgument("train-text", "input", values.input));
|
|
40
|
+
const output = resolvePath(requiredArgument("train-text", "output", values.output));
|
|
41
|
+
const version = requiredArgument("train-text", "version", values.version);
|
|
42
|
+
await makeDirectories(output);
|
|
43
|
+
await using scratch = await temporaryDirectory("mtf-train-text-");
|
|
44
|
+
const trained = await trainSentencePiece({
|
|
45
|
+
input,
|
|
46
|
+
model_prefix: `${output}/tokenizer`,
|
|
47
|
+
...RECIPE,
|
|
48
|
+
pad_id: 0,
|
|
49
|
+
unk_id: 1,
|
|
50
|
+
bos_id: 2,
|
|
51
|
+
eos_id: 3,
|
|
52
|
+
user_defined_symbols: [],
|
|
53
|
+
}, { spmTrain: await spmTrainBinary(values["spm-train"]), workDirectory: scratch.path.toString() });
|
|
54
|
+
const text = await readLocalTextFile(input);
|
|
55
|
+
const meta = {
|
|
56
|
+
tokenizer_version: version,
|
|
57
|
+
...RECIPE,
|
|
58
|
+
// Every line break ends a line, and a final line without one still counts.
|
|
59
|
+
training_lines: (text.match(/\n/g)?.length ?? 0) + (text && !text.endsWith("\n") ? 1 : 0),
|
|
60
|
+
git_commit: await gitHead().catch(() => null),
|
|
61
|
+
model_sha256: await sha256File(trained.model),
|
|
62
|
+
model_path: trained.model,
|
|
63
|
+
vocab_path: trained.vocab,
|
|
64
|
+
};
|
|
65
|
+
await writeLocalJSONFile(meta, `${output}/META.json`);
|
|
66
|
+
console.log(prettyJSON(meta, false, 2));
|
|
67
|
+
//# sourceMappingURL=text.run.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text.run.js","sourceRoot":"","sources":["../../../../tools/tokenizer/train/text.run.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAA;AAC9D,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAA;AACjE,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAA;AAChF,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AACjD,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,qCAAqC,CAAA;AACtF,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAErC,OAAO,EAAE,cAAc,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AE,aAAa,EAAE,OAAO,CAAC,IAAI,CAAC,QAAQ,EAAE,EAAE,CAC/F,CAAA;AAED,MAAM,IAAI,GAAG,MAAM,iBAAiB,CAAC,KAAK,CAAC,CAAA;AAE3C,MAAM,IAAI,GAAG;IACZ,iBAAiB,EAAE,OAAO;IAC1B,GAAG,MAAM;IACT,2EAA2E;IAC3E,cAAc,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,MAAM,IAAI,CAAC,CAAC,GAAG,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACzF,UAAU,EAAE,MAAM,OAAO,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC;IAC7C,YAAY,EAAE,MAAM,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC;IAC7C,UAAU,EAAE,OAAO,CAAC,KAAK;IACzB,UAAU,EAAE,OAAO,CAAC,KAAK;CACzB,CAAA;AAED,MAAM,kBAAkB,CAAC,IAAI,EAAE,GAAG,MAAM,YAAY,CAAC,CAAA;AAErD,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* Train a versioned SentencePiece tokenizer from a corpus's train split, and write its model card.
|
|
7
|
+
*
|
|
8
|
+
* The training lines are a balanced sample: a reservoir of `perCountrySample` raws per country, drawn with CPython's
|
|
9
|
+
* generator in the order `tokenizer/train.py` drew, then shuffled. The card records what reproduces the model: the
|
|
10
|
+
* corpus files read and their order, the SHA-256 of the training input and of the symbol list, the SentencePiece
|
|
11
|
+
* release, every flag, and the model's SHA-256.
|
|
12
|
+
*/
|
|
13
|
+
import { SENTENCEPIECE } from "./binary.ts";
|
|
14
|
+
import { type ByteFallbackReport } from "./byte-fallback.ts";
|
|
15
|
+
import { type TrainerFlags, type TrainerFlagValue } from "./sentencepiece.ts";
|
|
16
|
+
export interface TokenizerTrainingConfig {
|
|
17
|
+
corpusDirectory: string;
|
|
18
|
+
outputDirectory: string;
|
|
19
|
+
corpusVersion: string;
|
|
20
|
+
vocabSize: number;
|
|
21
|
+
characterCoverage: number;
|
|
22
|
+
modelType: string;
|
|
23
|
+
byteFallback: boolean;
|
|
24
|
+
splitDigits: boolean;
|
|
25
|
+
allowWhitespaceOnlyPieces: boolean;
|
|
26
|
+
perCountrySample: number;
|
|
27
|
+
countries: readonly string[];
|
|
28
|
+
minePostcodeLiterals: number;
|
|
29
|
+
userDefinedSymbols: readonly string[];
|
|
30
|
+
evalFixture: string | null;
|
|
31
|
+
seed: number;
|
|
32
|
+
/**
|
|
33
|
+
* Further trainer arguments, appended after the standard ones in this order.
|
|
34
|
+
*/
|
|
35
|
+
extraFlags: Readonly<Record<string, TrainerFlagValue>>;
|
|
36
|
+
/**
|
|
37
|
+
* The `spm_train` binary.
|
|
38
|
+
*/
|
|
39
|
+
spmTrain: string;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The defaults the Python tokenizer trainer used.
|
|
43
|
+
*/
|
|
44
|
+
export declare const TOKENIZER_TRAINING_DEFAULTS: {
|
|
45
|
+
readonly vocabSize: 48000;
|
|
46
|
+
readonly characterCoverage: 0.9999;
|
|
47
|
+
readonly modelType: "unigram";
|
|
48
|
+
readonly byteFallback: true;
|
|
49
|
+
readonly splitDigits: false;
|
|
50
|
+
readonly allowWhitespaceOnlyPieces: false;
|
|
51
|
+
readonly perCountrySample: 500000;
|
|
52
|
+
readonly countries: readonly ["US", "FR"];
|
|
53
|
+
readonly minePostcodeLiterals: 0;
|
|
54
|
+
readonly seed: 42;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* The symbols to keep whole, as written and as passed to SentencePiece: the configured list,
|
|
58
|
+
* then any mined postcode literals, first occurrences only, truncated to a quarter of the vocabulary.
|
|
59
|
+
*/
|
|
60
|
+
export declare function resolveUserDefinedSymbols(config: Pick<TokenizerTrainingConfig, "userDefinedSymbols" | "minePostcodeLiterals" | "corpusDirectory" | "countries" | "vocabSize">): Promise<{
|
|
61
|
+
symbols: string[];
|
|
62
|
+
passed: string[];
|
|
63
|
+
}>;
|
|
64
|
+
/**
|
|
65
|
+
* The trainer flags of a configuration, in the order the Python trainer passed them.
|
|
66
|
+
*/
|
|
67
|
+
export declare function trainerFlags(config: TokenizerTrainingConfig, input: string, modelPrefix: string, symbols: readonly string[]): TrainerFlags;
|
|
68
|
+
/**
|
|
69
|
+
* A train file read, in read order, and the SHA-256 its corpus manifest records, when it records one.
|
|
70
|
+
*/
|
|
71
|
+
interface CorpusFileRecord {
|
|
72
|
+
path: string;
|
|
73
|
+
sha256: string | null;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The card `trainTokenizer` writes as `model_card.json` and `META.json`.
|
|
77
|
+
*/
|
|
78
|
+
export interface TokenizerModelCard {
|
|
79
|
+
tokenizer_version: string;
|
|
80
|
+
corpus_version: string;
|
|
81
|
+
vocab_size: number;
|
|
82
|
+
training_lines: number;
|
|
83
|
+
training_duration_seconds: number;
|
|
84
|
+
generated_at: string;
|
|
85
|
+
git_commit: string | null;
|
|
86
|
+
model_sha256: string;
|
|
87
|
+
model_path: string;
|
|
88
|
+
vocab_path: string;
|
|
89
|
+
sentencepiece: typeof SENTENCEPIECE & {
|
|
90
|
+
trainer: "spm_train";
|
|
91
|
+
};
|
|
92
|
+
sentencepiece_flags: Record<string, unknown>;
|
|
93
|
+
training_input: {
|
|
94
|
+
sha256: string;
|
|
95
|
+
lines: number;
|
|
96
|
+
corpus_files: CorpusFileRecord[];
|
|
97
|
+
user_defined_symbols_sha256: string;
|
|
98
|
+
};
|
|
99
|
+
sampling: {
|
|
100
|
+
countries: string[];
|
|
101
|
+
per_country: number;
|
|
102
|
+
seed: number;
|
|
103
|
+
per_country_lines: Record<string, number>;
|
|
104
|
+
};
|
|
105
|
+
byte_fallback_eval: ByteFallbackReport | null;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Sample, train and write `tokenizer.model`, `tokenizer.vocab`, `model_card.json`
|
|
109
|
+
* and `META.json` under the output directory.
|
|
110
|
+
*/
|
|
111
|
+
export declare function trainTokenizer(config: TokenizerTrainingConfig): Promise<TokenizerModelCard>;
|
|
112
|
+
export {};
|
|
113
|
+
//# sourceMappingURL=train.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"train.d.ts","sourceRoot":"","sources":["../../../tools/tokenizer/train.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAQH,OAAO,EAAmB,aAAa,EAAE,MAAM,aAAa,CAAA;AAC5D,OAAO,EAAE,KAAK,kBAAkB,EAAyC,MAAM,oBAAoB,CAAA;AAEnG,OAAO,EAAE,KAAK,YAAY,EAAE,KAAK,gBAAgB,EAAsB,MAAM,oBAAoB,CAAA;AAGjG,MAAM,WAAW,uBAAuB;IACvC,eAAe,EAAE,MAAM,CAAA;IACvB,eAAe,EAAE,MAAM,CAAA;IACvB,aAAa,EAAE,MAAM,CAAA;IACrB,SAAS,EAAE,MAAM,CAAA;IACjB,iBAAiB,EAAE,MAAM,CAAA;IACzB,SAAS,EAAE,MAAM,CAAA;IACjB,YAAY,EAAE,OAAO,CAAA;IACrB,WAAW,EAAE,OAAO,CAAA;IACpB,yBAAyB,EAAE,OAAO,CAAA;IAClC,gBAAgB,EAAE,MAAM,CAAA;IACxB,SAAS,EAAE,SAAS,MAAM,EAAE,CAAA;IAC5B,oBAAoB,EAAE,MAAM,CAAA;IAC5B,kBAAkB,EAAE,SAAS,MAAM,EAAE,CAAA;IACrC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAA;IAC1B,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