@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.
Files changed (92) hide show
  1. package/README.md +35 -5
  2. package/crates/mtf-channels/tests/fixtures/README.md +13 -0
  3. package/crates/mtf-sampler/tests/fixtures/README.md +120 -0
  4. package/lib/code-units.ts +45 -0
  5. package/out/code-units.d.ts +24 -0
  6. package/out/code-units.d.ts.map +1 -0
  7. package/out/code-units.js +36 -0
  8. package/out/code-units.js.map +1 -0
  9. package/out/tools/build-wasm.run.js +2 -2
  10. package/out/tools/build-wasm.run.js.map +1 -1
  11. package/out/tools/sampler/native.d.ts +314 -0
  12. package/out/tools/sampler/native.d.ts.map +1 -0
  13. package/out/tools/sampler/native.js +114 -0
  14. package/out/tools/sampler/native.js.map +1 -0
  15. package/out/tools/tokenizer/binary.d.ts +38 -0
  16. package/out/tools/tokenizer/binary.d.ts.map +1 -0
  17. package/out/tools/tokenizer/binary.js +94 -0
  18. package/out/tools/tokenizer/binary.js.map +1 -0
  19. package/out/tools/tokenizer/byte-fallback.d.ts +39 -0
  20. package/out/tools/tokenizer/byte-fallback.d.ts.map +1 -0
  21. package/out/tools/tokenizer/byte-fallback.js +49 -0
  22. package/out/tools/tokenizer/byte-fallback.js.map +1 -0
  23. package/out/tools/tokenizer/model.d.ts +70 -0
  24. package/out/tools/tokenizer/model.d.ts.map +1 -0
  25. package/out/tools/tokenizer/model.js +56 -0
  26. package/out/tools/tokenizer/model.js.map +1 -0
  27. package/out/tools/tokenizer/prune.d.ts +69 -0
  28. package/out/tools/tokenizer/prune.d.ts.map +1 -0
  29. package/out/tools/tokenizer/prune.js +126 -0
  30. package/out/tools/tokenizer/prune.js.map +1 -0
  31. package/out/tools/tokenizer/prune.run.d.ts +16 -0
  32. package/out/tools/tokenizer/prune.run.d.ts.map +1 -0
  33. package/out/tools/tokenizer/prune.run.js +34 -0
  34. package/out/tools/tokenizer/prune.run.js.map +1 -0
  35. package/out/tools/tokenizer/sentencepiece.d.ts +63 -0
  36. package/out/tools/tokenizer/sentencepiece.d.ts.map +1 -0
  37. package/out/tools/tokenizer/sentencepiece.js +90 -0
  38. package/out/tools/tokenizer/sentencepiece.js.map +1 -0
  39. package/out/tools/tokenizer/splice.d.ts +117 -0
  40. package/out/tools/tokenizer/splice.d.ts.map +1 -0
  41. package/out/tools/tokenizer/splice.js +398 -0
  42. package/out/tools/tokenizer/splice.js.map +1 -0
  43. package/out/tools/tokenizer/splice.run.d.ts +29 -0
  44. package/out/tools/tokenizer/splice.run.d.ts.map +1 -0
  45. package/out/tools/tokenizer/splice.run.js +114 -0
  46. package/out/tools/tokenizer/splice.run.js.map +1 -0
  47. package/out/tools/tokenizer/symbols.d.ts +42 -0
  48. package/out/tools/tokenizer/symbols.d.ts.map +1 -0
  49. package/out/tools/tokenizer/symbols.js +113 -0
  50. package/out/tools/tokenizer/symbols.js.map +1 -0
  51. package/out/tools/tokenizer/train/text.run.d.ts +16 -0
  52. package/out/tools/tokenizer/train/text.run.d.ts.map +1 -0
  53. package/out/tools/tokenizer/train/text.run.js +67 -0
  54. package/out/tools/tokenizer/train/text.run.js.map +1 -0
  55. package/out/tools/tokenizer/train.d.ts +113 -0
  56. package/out/tools/tokenizer/train.d.ts.map +1 -0
  57. package/out/tools/tokenizer/train.js +162 -0
  58. package/out/tools/tokenizer/train.js.map +1 -0
  59. package/out/tools/tokenizer/train.run.d.ts +24 -0
  60. package/out/tools/tokenizer/train.run.d.ts.map +1 -0
  61. package/out/tools/tokenizer/train.run.js +131 -0
  62. package/out/tools/tokenizer/train.run.js.map +1 -0
  63. package/out/tools/tokenizer/utilization.run.d.ts +17 -0
  64. package/out/tools/tokenizer/utilization.run.d.ts.map +1 -0
  65. package/out/tools/tokenizer/utilization.run.js +80 -0
  66. package/out/tools/tokenizer/utilization.run.js.map +1 -0
  67. package/out/tools/tokenizer/verify.d.ts +94 -0
  68. package/out/tools/tokenizer/verify.d.ts.map +1 -0
  69. package/out/tools/tokenizer/verify.js +193 -0
  70. package/out/tools/tokenizer/verify.js.map +1 -0
  71. package/out/tools/tokenizer/verify.run.d.ts +22 -0
  72. package/out/tools/tokenizer/verify.run.d.ts.map +1 -0
  73. package/out/tools/tokenizer/verify.run.js +71 -0
  74. package/out/tools/tokenizer/verify.run.js.map +1 -0
  75. package/package.json +15 -3
  76. package/tools/build-wasm.run.ts +2 -2
  77. package/tools/sampler/native.ts +433 -0
  78. package/tools/tokenizer/binary.ts +118 -0
  79. package/tools/tokenizer/byte-fallback.ts +80 -0
  80. package/tools/tokenizer/model.ts +110 -0
  81. package/tools/tokenizer/prune.run.ts +37 -0
  82. package/tools/tokenizer/prune.ts +195 -0
  83. package/tools/tokenizer/sentencepiece.ts +142 -0
  84. package/tools/tokenizer/splice.run.ts +141 -0
  85. package/tools/tokenizer/splice.ts +592 -0
  86. package/tools/tokenizer/symbols.ts +119 -0
  87. package/tools/tokenizer/train/text.run.ts +80 -0
  88. package/tools/tokenizer/train.run.ts +151 -0
  89. package/tools/tokenizer/train.ts +266 -0
  90. package/tools/tokenizer/utilization.run.ts +96 -0
  91. package/tools/tokenizer/verify.run.ts +77 -0
  92. package/tools/tokenizer/verify.ts +313 -0
@@ -0,0 +1,126 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * Prune a tokenizer's unused pieces and the matching rows of an int8 model's embedding table.
7
+ *
8
+ * A piece that never won a Viterbi path contributes no score to any other path, so segmentation is unchanged for
9
+ * every input whose best path avoided the pruned pieces. The kept pieces stay in their order. The embedding rows
10
+ * are gathered by the old-to-new id map with the scale and zero point untouched, so each kept row stays the same
11
+ * bytes.
12
+ */
13
+ import { readLocalBuffer, readLocalJSONFile } from "@mailwoman/core/fs/readers";
14
+ import { readFileSize } from "@mailwoman/core/fs/readers/stat";
15
+ import { temporaryDirectory } from "@mailwoman/core/fs/temporary";
16
+ import { makeDirectories, writeLocalBuffer, writeLocalJSONFile, writeLocalTextFile } from "@mailwoman/core/fs/writers";
17
+ import { prettyJSON } from "@mailwoman/core/json";
18
+ import { runMTFTokenizer } from "./binary.js";
19
+ import { editModel, PieceType, readModelPieces } from "./model.js";
20
+ /**
21
+ * The quantized embedding table of the BIO token classifier.
22
+ */
23
+ export const QUANTIZED_EMBEDDING = "inner.token_embeddings.weight_quantized";
24
+ /**
25
+ * The pieces to keep: every piece the training corpus or the evaluation surface used, every
26
+ * piece that is not a normal piece (control, unknown, user-defined, unused and byte pieces),
27
+ * and every piece of at most one code point once a leading `▁` is removed.
28
+ * This keeps every character reachable.
29
+ */
30
+ export function keepSet(vocabulary, counts, evalFired) {
31
+ if (counts.length !== vocabulary.length) {
32
+ throw new Error(`the counts cover ${counts.length} pieces and the tokenizer has ${vocabulary.length}`);
33
+ }
34
+ const keep = counts.map((n) => n > 0);
35
+ for (const id of evalFired) {
36
+ if (id < 0 || id >= keep.length)
37
+ throw new Error(`eval-fired id ${id} is outside the vocabulary`);
38
+ keep[id] = true;
39
+ }
40
+ let singleCodepoint = 0;
41
+ vocabulary.forEach(([piece, , type], id) => {
42
+ if (type !== PieceType.Normal) {
43
+ keep[id] = true;
44
+ return;
45
+ }
46
+ const literal = piece.startsWith("▁") ? piece.slice(1) : piece;
47
+ if ([...literal].length <= 1) {
48
+ keep[id] = true;
49
+ singleCodepoint++;
50
+ }
51
+ });
52
+ return { keep, singleCodepoint };
53
+ }
54
+ /**
55
+ * Write the pruned `tokenizer.model`, `model.onnx`, `id-map.json`
56
+ * and `prune-report.json` under the output directory.
57
+ */
58
+ export async function prunePieces(options) {
59
+ const vocabulary = await readModelPieces(options.tokenizer);
60
+ const { counts } = await readLocalJSONFile(options.trainCounts);
61
+ const { fired_ids: evalFired } = await readLocalJSONFile(options.evalFired);
62
+ const { keep, singleCodepoint } = keepSet(vocabulary, counts, evalFired);
63
+ const keptIDs = keep.flatMap((kept, id) => (kept ? [id] : []));
64
+ const oldToNew = new Array(vocabulary.length).fill(-1);
65
+ keptIDs.forEach((id, i) => {
66
+ oldToNew[id] = i;
67
+ });
68
+ const trainFired = counts.filter((n) => n > 0).length;
69
+ const evalCount = new Set(evalFired).size;
70
+ console.info(`[prune] vocab ${vocabulary.length} → keep ${keptIDs.length} (${((100 * keptIDs.length) / vocabulary.length).toFixed(1)}%)`);
71
+ console.info(`[prune] train-fired ${trainFired}, eval-fired ${evalCount}, single-codepoint kept ${singleCodepoint}`);
72
+ await makeDirectories(options.outDirectory);
73
+ const tokenizerOut = `${options.outDirectory}/tokenizer.model`;
74
+ await editModel(options.tokenizer, tokenizerOut, { keep: keptIDs });
75
+ await using scratch = await temporaryDirectory("mtf-prune-");
76
+ const table = scratch.path("table.bin").toString();
77
+ const gathered = scratch.path("gathered.bin").toString();
78
+ const onnxOut = `${options.outDirectory}/model.onnx`;
79
+ const meta = await runMTFTokenizer([
80
+ "onnx-read",
81
+ "--model",
82
+ options.onnx,
83
+ "--name",
84
+ QUANTIZED_EMBEDDING,
85
+ "--raw-out",
86
+ table,
87
+ ]);
88
+ if (meta.dims[0] !== vocabulary.length) {
89
+ throw new Error(`embedding rows ${meta.dims[0]} != vocab ${vocabulary.length}`);
90
+ }
91
+ const rowBytes = (await readFileSize(table)) / vocabulary.length;
92
+ const rows = await readLocalBuffer(table);
93
+ const out = Buffer.alloc(keptIDs.length * rowBytes);
94
+ keptIDs.forEach((id, i) => rows.copy(out, i * rowBytes, id * rowBytes, (id + 1) * rowBytes));
95
+ await writeLocalBuffer(out, gathered);
96
+ await runMTFTokenizer([
97
+ "onnx-write",
98
+ "--model",
99
+ options.onnx,
100
+ "--name",
101
+ QUANTIZED_EMBEDDING,
102
+ "--dims",
103
+ [keptIDs.length, ...meta.dims.slice(1)].join(","),
104
+ "--data-type",
105
+ String(meta.data_type),
106
+ "--raw",
107
+ gathered,
108
+ "--out",
109
+ onnxOut,
110
+ ]);
111
+ const idMap = { kept_ids: keptIDs, old_to_new: oldToNew };
112
+ await writeLocalJSONFile(idMap, `${options.outDirectory}/id-map.json`);
113
+ const report = {
114
+ vocab_before: vocabulary.length,
115
+ vocab_after: keptIDs.length,
116
+ train_fired: trainFired,
117
+ eval_fired: evalCount,
118
+ single_codepoint_kept: singleCodepoint,
119
+ tokenizer_bytes: { before: await readFileSize(options.tokenizer), after: await readFileSize(tokenizerOut) },
120
+ onnx_bytes: { before: await readFileSize(options.onnx), after: await readFileSize(onnxOut) },
121
+ };
122
+ await writeLocalTextFile(prettyJSON(report, true, 1), `${options.outDirectory}/prune-report.json`);
123
+ console.info(prettyJSON(report, false, 1));
124
+ return report;
125
+ }
126
+ //# sourceMappingURL=prune.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prune.js","sourceRoot":"","sources":["../../../tools/tokenizer/prune.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAA;AAC/E,OAAO,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAA;AAC9D,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAA;AACjE,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAA;AACtH,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AAEjD,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAC7C,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAElE;;GAEG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,yCAAyC,CAAA;AAoC5E;;;;;GAKG;AACH,MAAM,UAAU,OAAO,CACtB,UAA0D,EAC1D,MAAyB,EACzB,SAA4B;IAE5B,IAAI,MAAM,CAAC,MAAM,KAAK,UAAU,CAAC,MAAM,EAAE,CAAC;QACzC,MAAM,IAAI,KAAK,CAAC,oBAAoB,MAAM,CAAC,MA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IAElG,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,CAAA;IAE1C,OAAO,MAAM,CAAA;AACd,CAAC"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * Prune a tokenizer's unused pieces and the int8 model's matching embedding rows.
7
+ *
8
+ * `--train-counts` is the per-piece count `utilization.run.ts` writes; `--eval-fired` is a JSON file whose
9
+ * `fired_ids` lists the pieces the evaluation surface uses. The tool writes `tokenizer.model`, `model.onnx`,
10
+ * `id-map.json` and `prune-report.json` under `--out-dir`. `verify.run.ts prune` then checks the result.
11
+ *
12
+ * Usage: `node packages/mtf/tools/tokenizer/prune.run.ts --tokenizer <model> --onnx <int8 onnx>
13
+ * --train-counts <json> --eval-fired <json> --out-dir <dir>`
14
+ */
15
+ export {};
16
+ //# sourceMappingURL=prune.run.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prune.run.d.ts","sourceRoot":"","sources":["../../../tools/tokenizer/prune.run.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG"}
@@ -0,0 +1,34 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * Prune a tokenizer's unused pieces and the int8 model's matching embedding rows.
7
+ *
8
+ * `--train-counts` is the per-piece count `utilization.run.ts` writes; `--eval-fired` is a JSON file whose
9
+ * `fired_ids` lists the pieces the evaluation surface uses. The tool writes `tokenizer.model`, `model.onnx`,
10
+ * `id-map.json` and `prune-report.json` under `--out-dir`. `verify.run.ts prune` then checks the result.
11
+ *
12
+ * Usage: `node packages/mtf/tools/tokenizer/prune.run.ts --tokenizer <model> --onnx <int8 onnx>
13
+ * --train-counts <json> --eval-fired <json> --out-dir <dir>`
14
+ */
15
+ import { parseArguments, requiredArgument } from "@mailwoman/core/scripting/arguments";
16
+ import { resolvePath } from "path-ts";
17
+ import { prunePieces } from "./prune.js";
18
+ const { values } = parseArguments({
19
+ options: {
20
+ tokenizer: { type: "string" },
21
+ onnx: { type: "string" },
22
+ "train-counts": { type: "string" },
23
+ "eval-fired": { type: "string" },
24
+ "out-dir": { type: "string" },
25
+ },
26
+ });
27
+ await prunePieces({
28
+ tokenizer: resolvePath(requiredArgument("prune", "tokenizer", values.tokenizer)),
29
+ onnx: resolvePath(requiredArgument("prune", "onnx", values.onnx)),
30
+ trainCounts: resolvePath(requiredArgument("prune", "train-counts", values["train-counts"])),
31
+ evalFired: resolvePath(requiredArgument("prune", "eval-fired", values["eval-fired"])),
32
+ outDirectory: resolvePath(requiredArgument("prune", "out-dir", values["out-dir"])),
33
+ });
34
+ //# sourceMappingURL=prune.run.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prune.run.js","sourceRoot":"","sources":["../../../tools/tokenizer/prune.run.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,qCAAqC,CAAA;AACtF,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAErC,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAExC,MAAM,EAAE,MAAM,EAAE,GAAG,cAAc,CAAC;IACjC,OAAO,EAAE;QACR,SAAS,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QAC7B,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QACxB,cAAc,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QAClC,YAAY,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QAChC,SAAS,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;KAC7B;CACD,CAAC,CAAA;AAEF,MAAM,WAAW,CAAC;IACjB,SAAS,EAAE,WAAW,CAAC,gBAAgB,CAAC,OAAO,EAAE,WAAW,EAAE,MAAM,CAAC,SAAS,CAAC,CAAC;IAChF,IAAI,EAAE,WAAW,CAAC,gBAAgB,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;IACjE,WAAW,EAAE,WAAW,CAAC,gBAAgB,CAAC,OAAO,EAAE,cAAc,EAAE,MAAM,CAAC,cAAc,CAAC,CAAC,CAAC;IAC3F,SAAS,EAAE,WAAW,CAAC,gBAAgB,CAAC,OAAO,EAAE,YAAY,EAAE,MAAM,CAAC,YAAY,CAAC,CAAC,CAAC;IACrF,YAAY,EAAE,WAAW,CAAC,gBAAgB,CAAC,OAAO,EAAE,SAAS,EAAE,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC;CAClF,CAAC,CAAA"}
@@ -0,0 +1,63 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * One SentencePiece training call, made with native `spm_train` and written in the Python wheel's model form.
7
+ *
8
+ * The flags are the keyword arguments a `SentencePieceTrainer.train(**flags)` call took, in the same order and with
9
+ * the same values. Two lists go through files rather than the command line, because the CLI splits an inline list
10
+ * on commas and a user-defined symbol may itself be a comma. `minloglevel` sets only the wheel's log level and has
11
+ * no CLI flag, so it is left off the command line.
12
+ *
13
+ * `spm_train` sets every trainer flag on its spec, so the model records defaults the caller did not supply.
14
+ * The wheel's spec holds only fields supplied in the call. After training, the rewrite drops trainer and normalizer
15
+ * fields absent from the flags. This produces the same bytes the wheel produces for that call.
16
+ */
17
+ import { type ModelEditSummary } from "./model.ts";
18
+ /**
19
+ * One training argument's value.
20
+ */
21
+ export type TrainerFlagValue = string | number | boolean | readonly string[];
22
+ /**
23
+ * The keyword arguments of one training call, in call order.
24
+ */
25
+ export type TrainerFlags = Readonly<Record<string, TrainerFlagValue>>;
26
+ /**
27
+ * This function serializes a list as one CSV row, matching the format Python's
28
+ * `csv.writer` produces for a list flag.
29
+ */
30
+ export declare function csvRow(values: readonly string[]): string;
31
+ export interface TrainedModel {
32
+ model: string;
33
+ vocab: string;
34
+ /**
35
+ * The command line `spm_train` ran with.
36
+ */
37
+ argv: string[];
38
+ /**
39
+ * The spec fields the wheel's form dropped.
40
+ */
41
+ dropped: ModelEditSummary["dropped_fields"];
42
+ }
43
+ export interface TrainOptions {
44
+ spmTrain: string;
45
+ /**
46
+ * Where the list files and the trainer's log go.
47
+ */
48
+ workDirectory: string;
49
+ /**
50
+ * The directory where `spm_train` runs.
51
+ *
52
+ * Relative `input` and `model_prefix` values resolve from this directory.
53
+ *
54
+ * The model records both paths as given, so a recipe written with relative paths
55
+ * trains to the same bytes in any directory.
56
+ */
57
+ cwd?: string;
58
+ }
59
+ /**
60
+ * Train with `flags` containing `model_prefix`, then rewrite `<model_prefix>.model` in the wheel's form.
61
+ */
62
+ export declare function trainSentencePiece(flags: TrainerFlags, options: TrainOptions): Promise<TrainedModel>;
63
+ //# sourceMappingURL=sentencepiece.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sentencepiece.d.ts","sourceRoot":"","sources":["../../../tools/tokenizer/sentencepiece.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAOH,OAAO,EAAa,KAAK,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAE7D;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,MAAM,EAAE,CAAA;AAE5E;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC,CAAA;AAerE;;;GAGG;AACH,wBAAgB,MAAM,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAMxD;AAWD,MAAM,WAAW,YAAY;IAC5B,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE,MAAM,CAAA;IACb;;OAEG;IACH,IAAI,EAAE,MAAM,EAAE,CAAA;IACd;;OAEG;IACH,OAAO,EAAE,gBAAgB,CAAC,gBAAgB,CAAC,CAAA;CAC3C;AAED,MAAM,WAAW,YAAY;IAC5B,QAAQ,EAAE,MAAM,CAAA;IAChB;;OAEG;IACH,aAAa,EAAE,MAAM,CAAA;IACrB;;;;;;;OAOG;IACH,GAAG,CAAC,EAAE,MAAM,CAAA;CACZ;AAED;;GAEG;AACH,wBAAsB,kBAAkB,CAAC,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC,CAwC1G"}
@@ -0,0 +1,90 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * One SentencePiece training call, made with native `spm_train` and written in the Python wheel's model form.
7
+ *
8
+ * The flags are the keyword arguments a `SentencePieceTrainer.train(**flags)` call took, in the same order and with
9
+ * the same values. Two lists go through files rather than the command line, because the CLI splits an inline list
10
+ * on commas and a user-defined symbol may itself be a comma. `minloglevel` sets only the wheel's log level and has
11
+ * no CLI flag, so it is left off the command line.
12
+ *
13
+ * `spm_train` sets every trainer flag on its spec, so the model records defaults the caller did not supply.
14
+ * The wheel's spec holds only fields supplied in the call. After training, the rewrite drops trainer and normalizer
15
+ * fields absent from the flags. This produces the same bytes the wheel produces for that call.
16
+ */
17
+ import { writeLocalTextFile } from "@mailwoman/core/fs/writers";
18
+ import { stringifyJSON } from "@mailwoman/core/json";
19
+ import { runFile } from "@mailwoman/core/process";
20
+ import { resolvePath } from "path-ts";
21
+ import { editModel } from "./model.js";
22
+ /**
23
+ * The list flags `spm_train` reads one entry per line from a file.
24
+ */
25
+ const FILE_LIST_FLAGS = {
26
+ user_defined_symbols: "user_defined_symbols_file",
27
+ control_symbols: "control_symbols_file",
28
+ };
29
+ /**
30
+ * The flags only the wheel reads.
31
+ */
32
+ const WHEEL_ONLY_FLAGS = new Set(["minloglevel"]);
33
+ /**
34
+ * This function serializes a list as one CSV row, matching the format Python's
35
+ * `csv.writer` produces for a list flag.
36
+ */
37
+ export function csvRow(values) {
38
+ return values
39
+ .map((value) => /[",\r\n]/.test(value) || (values.length === 1 && value === "") ? `"${value.replaceAll('"', '""')}"` : value)
40
+ .join(",");
41
+ }
42
+ /**
43
+ * A scalar flag as the wheel's `str(value)` renders it.
44
+ */
45
+ function scalar(value) {
46
+ if (typeof value === "boolean")
47
+ return value ? "true" : "false";
48
+ return String(value);
49
+ }
50
+ /**
51
+ * Train with `flags` containing `model_prefix`, then rewrite `<model_prefix>.model` in the wheel's form.
52
+ */
53
+ export async function trainSentencePiece(flags, options) {
54
+ const prefix = flags["model_prefix"];
55
+ if (typeof prefix !== "string")
56
+ throw new Error("a training call needs a model_prefix");
57
+ const argv = [];
58
+ for (const [name, value] of Object.entries(flags)) {
59
+ if (WHEEL_ONLY_FLAGS.has(name))
60
+ continue;
61
+ if (Array.isArray(value)) {
62
+ const listed = value;
63
+ const fileFlag = FILE_LIST_FLAGS[name];
64
+ if (fileFlag) {
65
+ if (!listed.length)
66
+ continue;
67
+ for (const entry of listed) {
68
+ if (/[\r\n]/.test(entry))
69
+ throw new Error(`${name} entry ${stringifyJSON(entry)} holds a line break`);
70
+ }
71
+ const file = `${options.workDirectory}/${fileFlag}.txt`;
72
+ await writeLocalTextFile(listed, file);
73
+ argv.push(`--${fileFlag}=${file}`);
74
+ }
75
+ else {
76
+ argv.push(`--${name}=${csvRow(listed)}`);
77
+ }
78
+ }
79
+ else {
80
+ argv.push(`--${name}=${scalar(value)}`);
81
+ }
82
+ }
83
+ const { stderr } = await runFile(options.spmTrain, argv, { maxBuffer: 1 << 30, cwd: options.cwd });
84
+ await writeLocalTextFile(stderr, `${options.workDirectory}/spm_train.log`);
85
+ const base = resolvePath(options.cwd ?? ".", prefix);
86
+ const model = `${base}.model`;
87
+ const { dropped_fields } = await editModel(model, model, { wheelFlags: Object.keys(flags) });
88
+ return { model, vocab: `${base}.vocab`, argv, dropped: dropped_fields };
89
+ }
90
+ //# sourceMappingURL=sentencepiece.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AC,EAAE,CAAC,CAAA;QACrE,CAAC;IACF,CAAC;IAED,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,QAAQ,EAAE,IAAI,EAAE,EAAE,SAAS,EAAE,CAAC,IAAI,EAAE,EAAE,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE,CAAC,CAAA;IAElG,MAAM,kBAAkB,CAAC,MAAM,EAAE,GAAG,OAAO,CAAC,aAAa,gBAAgB,CAAC,CAAA;IAC1E,MAAM,IAAI,GAAG,WAAW,CAAC,OAAO,CAAC,GAAG,IAAI,GAAG,EAAE,MAAM,CAAC,CAAA;IACpD,MAAM,KAAK,GAAG,GAAG,IAAI,QAAQ,CAAA;IAC7B,MAAM,EAAE,cAAc,EAAE,GAAG,MAAM,SAAS,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,UAAU,EAAE,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,CAAA;IAE5F,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,IAAI,QAAQ,EAAE,IAAI,EAAE,OAAO,EAAE,cAAc,EAAE,CAAA;AACxE,CAAC"}
@@ -0,0 +1,117 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * Splice the diacritic pieces of a locale tokenizer into a frozen unigram vocabulary, and grow an exported model's
7
+ * embedding table to match.
8
+ *
9
+ * An appended piece contains a code point outside ASCII, so it cannot match any span of an ASCII string, and ASCII
10
+ * text tokenizes exactly as before. `verifySourceIdentical` checks that on a probe rather than assuming it. A
11
+ * trained locale that shares a code point with the new pieces can re-tokenize. `checkCodepointOverlap`
12
+ * reports and refuses unless the locale is accepted. The method does not scale to CJK.
13
+ */
14
+ /**
15
+ * ASCII address lines, the default probe of the English-identity check.
16
+ */
17
+ export declare const ENGLISH_PROBE: readonly string[];
18
+ export interface SlavicCorpusOptions {
19
+ oaRoot: string;
20
+ locales: readonly string[];
21
+ outPath: string;
22
+ /**
23
+ * Rows read per OpenAddresses CSV.
24
+ *
25
+ * The read admits two rows past the cap, as the Python reader did.
26
+ */
27
+ perFileCap?: number;
28
+ /**
29
+ * Extra corpus lines, such as gazetteer alias names, appended after the sample.
30
+ */
31
+ extraText?: string | null;
32
+ /**
33
+ * The repetition count for the extra lines.
34
+ *
35
+ * A list of single names needs enough unigram mass to win vocabulary slots.
36
+ */
37
+ extraRepeat?: number;
38
+ }
39
+ /**
40
+ * The distinct `STREET`, `CITY` and `STREET CITY` lines of each locale's OpenAddresses CSVs,
41
+ * under 80 code points, shuffled and capped at 350,000, then the extra lines.
42
+ * Returns the number of lines written.
43
+ *
44
+ * The files of a locale are read in name order and the lines kept in first-seen order,
45
+ * so the corpus is the same on every run.
46
+ */
47
+ export declare function buildSlavicCorpus(options: SlavicCorpusOptions): Promise<number>;
48
+ /**
49
+ * Train the locale unigram model the splice draws its pieces from, and return its `.model` path.
50
+ */
51
+ export declare function trainDiacriticSentencePiece(corpusPath: string, outPrefix: string, options: {
52
+ spmTrain: string;
53
+ vocabSize?: number;
54
+ }): Promise<string>;
55
+ /**
56
+ * Raise when `probe` tokenizes differently under the spliced vocabulary than under the base one.
57
+ */
58
+ export declare function verifySourceIdentical(baseTokenizer: string, splicedTokenizer: string, probe?: readonly string[]): Promise<void>;
59
+ /**
60
+ * Append the pieces of `donor` whose core holds a code point outside ASCII and that `base`
61
+ * lacks, with their scores, as normal pieces, then check the English identity.
62
+ * Returns the appended pieces.
63
+ */
64
+ export declare function spliceVocabulary(base: string, donor: string, out: string): Promise<string[]>;
65
+ /**
66
+ * The code points outside ASCII in the first `capBytes` of a locale sample,
67
+ * read as UTF-8 with undecodable bytes ignored.
68
+ * A character split by the byte cap is left out.
69
+ *
70
+ * The sample is read as raw text rather than parsed, because the check needs a locale's
71
+ * character inventory and not its records, and any sample format then works.
72
+ */
73
+ export declare function collectSampleCodepoints(samplePath: string, capBytes?: number): Promise<Set<string>>;
74
+ export interface OverlapReport {
75
+ new_piece_codepoints: string[];
76
+ per_locale_overlap: Record<string, string[]>;
77
+ accepted_overlap: string[];
78
+ }
79
+ /**
80
+ * Report the code points outside ASCII that the new pieces share with each trained locale's
81
+ * sample, write the report, and raise when an overlapping locale is not in `acceptedOverlap`.
82
+ *
83
+ * The evaluation for an accepted locale must meet a registered per-locale non-inferiority
84
+ * comparison for it before the first measurement.
85
+ */
86
+ export declare function checkCodepointOverlap(newPieces: readonly string[], localeSamples: Readonly<Record<string, string>>, reportPath: string, acceptedOverlap?: ReadonlySet<string>): Promise<Record<string, string[]>>;
87
+ /**
88
+ * Refuses a table initializer whose dims or byte length differ from `[rows, columns]` at `bytesPerValue`.
89
+ *
90
+ * @throws Naming the initializer, its dims and byte length, and the shape it was expected to have.
91
+ */
92
+ export declare function assertTableShape(label: string, tensor: {
93
+ dims: readonly number[];
94
+ bytes: Uint8Array;
95
+ }, [rows, columns]: readonly [number, number], bytesPerValue: number): void;
96
+ export interface ONNXMeanInitOptions {
97
+ fp32ONNX: string;
98
+ baseTokenizer: string;
99
+ splicedTokenizer: string;
100
+ outFp32: string;
101
+ int8ONNX?: string | null;
102
+ outInt8?: string | null;
103
+ embeddingName?: string;
104
+ }
105
+ /**
106
+ * Grow an exported model's token embedding table to the spliced vocabulary.
107
+ *
108
+ * Each new row is the mean of the base tokenizer's pieces for the new piece's surface (FVT),
109
+ * or the mean of every row when the surface has no piece inside the old vocabulary.
110
+ * The embedding is the only vocabulary-sized tensor of the token classifier,
111
+ * so the rest of the graph is unchanged.
112
+ *
113
+ * For an int8 twin the new rows are quantized with the table's own scale and zero point,
114
+ * after checking that those parameters reproduce the table's last row.
115
+ */
116
+ export declare function meanInitONNXEmbeddings(options: ONNXMeanInitOptions): Promise<[number, number]>;
117
+ //# sourceMappingURL=splice.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"splice.d.ts","sourceRoot":"","sources":["../../../tools/tokenizer/splice.ts"],"names":[],"mappings":"AAAA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