@mailwoman/mtf 0.0.0-stage → 10.7.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 +191 -2
- package/THIRD_PARTY_NOTICES.md +31 -0
- package/lib/index.ts +266 -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/package.json +95 -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/wasm/mtf.wasm +0 -0
package/README.md
CHANGED
|
@@ -1,3 +1,192 @@
|
|
|
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/
|
|
56
|
+
Cargo.toml Cargo workspace for the crates below
|
|
57
|
+
crates/mtf-core/ Model representation, normalization, unigram encode, decode, errors
|
|
58
|
+
crates/mtf-wasm/ Thin wasm-bindgen adapter around mtf-core
|
|
59
|
+
crates/mtf-tools/ Offline model importer and compiler
|
|
60
|
+
lib/ TypeScript loader and result conversion
|
|
61
|
+
wasm/ Committed WASM binary and its build metadata
|
|
62
|
+
test/fixtures/ Reference outputs from the deployed runtime
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
The WASM binary is committed, as `sentencepiece-wasm`'s is, so an ordinary install needs no Rust
|
|
66
|
+
toolchain. A repository check will rebuild it and compare hashes.
|
|
67
|
+
|
|
68
|
+
### Runtime interface
|
|
69
|
+
|
|
70
|
+
- The caller initializes the runtime explicitly, and may supply a compiled `WebAssembly.Module` for
|
|
71
|
+
Workers. The package's import has no side effects. The WASM fetch and instantiation happen in the
|
|
72
|
+
initialization call.
|
|
73
|
+
- A loaded model is a `Disposable` processor object. Its encode and decode methods are synchronous.
|
|
74
|
+
- Results are ordinary JavaScript values. The caller never handles WASM vectors or pointers.
|
|
75
|
+
- A model file records its format version, normalization policy, special IDs and vocabulary hash. The
|
|
76
|
+
loader refuses a model whose semantics it does not implement.
|
|
77
|
+
|
|
78
|
+
### Risks to test early
|
|
79
|
+
|
|
80
|
+
- **Normalization and alignment.** The shipped model uses `nmt_nfkc` with a compiled character map.
|
|
81
|
+
Generic Unicode NFKC is a different function. The Rust encoder must reproduce the compiled map and the
|
|
82
|
+
original-input alignment.
|
|
83
|
+
- **Segmentation ties.** Scores, floating-point precision, tie-breaking and user-defined symbols can move
|
|
84
|
+
token IDs. The fixtures detect such changes.
|
|
85
|
+
- **Byte fallback.** Several byte pieces can represent one character, and only the last owns the
|
|
86
|
+
character's span.
|
|
87
|
+
- **Model loading.** The loader validates lengths, counts and versions before allocating, and bounds input
|
|
88
|
+
length and token count.
|
|
89
|
+
- **Downstream consumers.** `packages/neural/lib/tokenizer.ts` converts byte offsets to UTF-16 ranges and
|
|
90
|
+
trims leading whitespace. `corpus-python/src/mailwoman_train/tokenizer/` converts them to Python
|
|
91
|
+
character positions for label projection. A change to `▁` or to word boundaries changes both.
|
|
92
|
+
|
|
93
|
+
## Stage 1 baseline
|
|
94
|
+
|
|
95
|
+
Measured on 2026-10-24 on an AMD Ryzen 9 8945HS with Node v26.2.0.
|
|
96
|
+
|
|
97
|
+
### Shipped model
|
|
98
|
+
|
|
99
|
+
All nine Latin weights packages (`de-de`, `en-au`, `en-gb`, `en-in`, `en-nz`, `en-us`, `es-es`, `fr-fr`,
|
|
100
|
+
`it-it`) ship the same `tokenizer.model`.
|
|
101
|
+
|
|
102
|
+
| Property | Value |
|
|
103
|
+
| ----------------- | ------------------------------------------------------------------ |
|
|
104
|
+
| SHA-256 | `4e9344a8995972d138ed1e3f90a07b55edf0287611eee1ebc8724b45a1eb4b23` |
|
|
105
|
+
| Size | 1,632,289 bytes |
|
|
106
|
+
| Tokenizer version | `0.9.0-multisplice` (model card `tokenizer_version`) |
|
|
107
|
+
| Algorithm | unigram, `byte_fallback=true`, vocabulary 73,143 |
|
|
108
|
+
|
|
109
|
+
The CJK family uses a character encoder and no SentencePiece model.
|
|
110
|
+
|
|
111
|
+
### Consumers of sentencepiece-wasm
|
|
112
|
+
|
|
113
|
+
- `packages/neural/lib/tokenizer.ts` (`MailwomanTokenizer`)
|
|
114
|
+
- `packages/neural/lib/native-offsets.integration.test.ts`
|
|
115
|
+
- `packages/neural/lib/web/browser-slo.integration.test.ts`
|
|
116
|
+
- `packages/mailwoman/tools/dev-tools/man-page.ts`
|
|
117
|
+
|
|
118
|
+
### Current runtime cost
|
|
119
|
+
|
|
120
|
+
The input set is the 1,264 distinct `input` strings in the gauntlet board's two-letter country
|
|
121
|
+
directories (46,721 UTF-8 bytes). Timings come from one process after three warmup passes.
|
|
122
|
+
|
|
123
|
+
| Measure | Value |
|
|
124
|
+
| ------------------------------------------ | ------------------------------------- |
|
|
125
|
+
| `sentencepiece.wasm` / `sentencepiece.mjs` | 625,993 / 57,585 bytes |
|
|
126
|
+
| WASM instantiate | 7.4 ms |
|
|
127
|
+
| Model load (`loadFromSerializedProto`) | 92.0 ms |
|
|
128
|
+
| Full pass, median of 10 | 47.4 ms (26,700 addresses per second) |
|
|
129
|
+
| Per-address encode p50 / p95 | 27.1 µs / 67.8 µs |
|
|
130
|
+
| Tokens | 23,123 (18.3 per address) |
|
|
131
|
+
| Byte-fallback pieces | 48 |
|
|
132
|
+
| Decode differs from input | 10 of 1,264 rows, all from `nmt_nfkc` |
|
|
133
|
+
|
|
134
|
+
Model load costs about twice a full pass over the board. Load time and model size are the first targets for
|
|
135
|
+
the compiled model format.
|
|
136
|
+
|
|
137
|
+
All 10 decode differences come from `nmt_nfkc` normalization: `ª`→`a`, `¾`→`3⁄4`, `½`→`1⁄2`, `№`→`No`,
|
|
138
|
+
`´`→` ́`, and runs of spaces collapsed to one space. The Rust decoder must reproduce them.
|
|
139
|
+
|
|
140
|
+
### Reference fixtures
|
|
141
|
+
|
|
142
|
+
`test/fixtures/board-reference-v0.2.2.json` holds, for each board input, the IDs, pieces, UTF-8 `begins`
|
|
143
|
+
and `ends`, and decode output from the deployed runtime on the shipped model. The deployed WASM is built
|
|
144
|
+
from Google's v0.2.2 tag (`e0cce7d37b065b5140349dbe12c6bcf6192fdd78`), so these outputs are the v0.2.2
|
|
145
|
+
reference for this model. The file is 545,495 bytes with SHA-256
|
|
146
|
+
`440cffd8908a289caebb1df5c36c027a13e54c93002740972cfccd6c8e8fff3e`.
|
|
147
|
+
|
|
148
|
+
## Status
|
|
149
|
+
|
|
150
|
+
| Step | State |
|
|
151
|
+
| -------------------------------------------------------------------------------- | --------------------------------------------------------------- |
|
|
152
|
+
| Workspace registered, plan and baseline recorded | Done |
|
|
153
|
+
| v0.2.2 reference fixtures | Captured |
|
|
154
|
+
| `cargo` admitted by the Bash hook | Done |
|
|
155
|
+
| `crates/mtf-core`: model loader, normalizer, unigram encode with offsets, decode | All 1,264 fixture rows match in IDs, pieces, offsets and decode |
|
|
156
|
+
| WASM adapter and TypeScript loader | All 1,264 fixture rows match through `wasm/mtf.wasm` in Node |
|
|
157
|
+
| `MailwomanTokenizer` on `@mailwoman/mtf` | Neural, mailwoman and browser suites pass |
|
|
158
|
+
| Flat piece index and borrowed piece text in the protobuf loader | WASM load 31.1 ms, from 43.4 ms |
|
|
159
|
+
| Compiled model format | Not started |
|
|
160
|
+
|
|
161
|
+
### Runtime comparison
|
|
162
|
+
|
|
163
|
+
Measured on 2026-10-24 on an AMD Ryzen 9 8945HS with Node v26.2.0, on the shipped `tokenizer.model` and
|
|
164
|
+
the 1,264 board inputs. Each value is the median of seven rounds. Each round ran each runtime in its own
|
|
165
|
+
process, alternating.
|
|
166
|
+
|
|
167
|
+
| Measure | `@mailwoman/mtf` | `@mailwoman/sentencepiece-wasm` |
|
|
168
|
+
| ---------------------------- | ---------------- | --------------------------------------- |
|
|
169
|
+
| WASM binary | 79,874 bytes | 625,993 bytes plus a 57,585-byte loader |
|
|
170
|
+
| WASM instantiate | 0.41 ms | 5.37 ms |
|
|
171
|
+
| Model load | 31.1 ms | 91.9 ms |
|
|
172
|
+
| Full board pass | 8.7 ms | 45.0 ms |
|
|
173
|
+
| Per-address encode p50 / p95 | 6.1 / 15.5 µs | 32.7 / 73.5 µs |
|
|
174
|
+
|
|
175
|
+
Natively, the same load takes 15.2 ms (`cargo run --release --example load_profile`).
|
|
176
|
+
|
|
177
|
+
Run the parity test from this directory with
|
|
178
|
+
`MTF_REFERENCE_MODEL=<path to the shipped tokenizer.model> cargo test --release`. Without the variable it
|
|
179
|
+
reads `../neural-weights-en-us/tokenizer.model` and fails if that file is absent.
|
|
180
|
+
|
|
181
|
+
## License
|
|
182
|
+
|
|
183
|
+
`@mailwoman/mtf` is licensed AGPL-3.0-only, with a commercial license available, like the rest of
|
|
184
|
+
Mailwoman.
|
|
185
|
+
|
|
186
|
+
Six modules in `crates/mtf-core/src` port upstream code: five from Google SentencePiece v0.2.2
|
|
187
|
+
(Apache-2.0) and `darts.rs` from darts-clone (BSD 2-clause). Each ported file keeps the upstream
|
|
188
|
+
copyright notice in its header and states how it was modified. `third-party/` holds both license texts
|
|
189
|
+
unchanged, and `THIRD_PARTY_NOTICES.md` maps each ported file to its upstream source. A later port from
|
|
190
|
+
either project follows the same three steps: keep the upstream notice in the file header, state the
|
|
191
|
+
modification, and add the file to `THIRD_PARTY_NOTICES.md`. Code written here without an upstream
|
|
192
|
+
source carries 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` |
|
package/lib/index.ts
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* The TypeScript loader for the Multilingual Token Framework's WebAssembly runtime.
|
|
7
|
+
*
|
|
8
|
+
* The module's import has no side effects: the WASM fetch and instantiation happen in the caller.
|
|
9
|
+
* The caller obtains the WASM bytes or a compiled `WebAssembly.Module` (Workers supply the latter)
|
|
10
|
+
* and passes it to {@linkcode MTFRuntime.instantiate}.
|
|
11
|
+
* `MTF_WASM_URL` locates the committed binary beside this package.
|
|
12
|
+
* A loaded model's encode and decode methods are synchronous.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The location of the committed `wasm/mtf.wasm`, resolved from this module.
|
|
17
|
+
*
|
|
18
|
+
* The same relative path holds from `lib/` in the repository and from `out/` in a tarball.
|
|
19
|
+
*/
|
|
20
|
+
export const MTF_WASM_URL = new URL("../wasm/mtf.wasm", import.meta.url)
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A compiled `WebAssembly.Module`, as a Worker runtime supplies it.
|
|
24
|
+
*
|
|
25
|
+
* The type is opaque because the Node type preset omits the DOM library that declares
|
|
26
|
+
* `WebAssembly`, and this package's declarations must type-check without it.
|
|
27
|
+
*/
|
|
28
|
+
export interface MTFCompiledModule {
|
|
29
|
+
readonly [Symbol.toStringTag]: string
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The bytes or compiled module the runtime instantiates from.
|
|
34
|
+
*/
|
|
35
|
+
export type MTFWasmSource = MTFCompiledModule | ArrayBuffer | Uint8Array<ArrayBuffer>
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The members of the global `WebAssembly` object this loader calls.
|
|
39
|
+
*/
|
|
40
|
+
interface WebAssemblyAPI {
|
|
41
|
+
// Bytes come first: an `ArrayBuffer` also satisfies the opaque module type.
|
|
42
|
+
instantiate(
|
|
43
|
+
bytes: ArrayBuffer | Uint8Array<ArrayBuffer>,
|
|
44
|
+
imports: object
|
|
45
|
+
): Promise<{ instance: { exports: MTFExports } }>
|
|
46
|
+
instantiate(module: MTFCompiledModule, imports: object): Promise<{ exports: MTFExports }>
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The runtime's global, typed to the members above.
|
|
51
|
+
*
|
|
52
|
+
* Every JavaScript runtime this package targets defines it.
|
|
53
|
+
*/
|
|
54
|
+
declare const WebAssembly: WebAssemblyAPI
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The pieces of one encoded input.
|
|
58
|
+
*
|
|
59
|
+
* `begins[i]` and `ends[i]` are UTF-8 byte offsets into the input as `TextEncoder` encodes it.
|
|
60
|
+
* A byte-fallback piece other than the last for its character has `begins[i] === ends[i]`.
|
|
61
|
+
*/
|
|
62
|
+
export interface MTFEncodeResult {
|
|
63
|
+
ids: number[]
|
|
64
|
+
pieces: string[]
|
|
65
|
+
begins: number[]
|
|
66
|
+
ends: number[]
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The C ABI that `crates/mtf-wasm` exports.
|
|
71
|
+
*/
|
|
72
|
+
interface MTFExports {
|
|
73
|
+
memory: { readonly buffer: ArrayBuffer }
|
|
74
|
+
mtf_alloc(len: number): number
|
|
75
|
+
mtf_dealloc(ptr: number, len: number): void
|
|
76
|
+
mtf_error_ptr(): number
|
|
77
|
+
mtf_error_len(): number
|
|
78
|
+
mtf_model_load(ptr: number, len: number): number
|
|
79
|
+
mtf_model_free(handle: number): void
|
|
80
|
+
mtf_vocab_size(handle: number): number
|
|
81
|
+
mtf_encode(handle: number, ptr: number, len: number): number
|
|
82
|
+
mtf_decode(handle: number, ids: number, count: number): number
|
|
83
|
+
mtf_words_ptr(handle: number): number
|
|
84
|
+
mtf_bytes_ptr(handle: number): number
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Words per piece in the encode output buffer: id, begin, end, piece offset, piece length.
|
|
89
|
+
*/
|
|
90
|
+
const PIECE_WORDS = 5
|
|
91
|
+
|
|
92
|
+
const encoder = new TextEncoder()
|
|
93
|
+
const decoder = new TextDecoder("utf-8", { fatal: true })
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* One instantiated WebAssembly runtime.
|
|
97
|
+
*
|
|
98
|
+
* A runtime may hold several loaded models at once.
|
|
99
|
+
*/
|
|
100
|
+
export class MTFRuntime {
|
|
101
|
+
readonly #exports: MTFExports
|
|
102
|
+
|
|
103
|
+
private constructor(exports: MTFExports) {
|
|
104
|
+
this.#exports = exports
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Instantiates the runtime from WASM bytes or a compiled module.
|
|
109
|
+
*/
|
|
110
|
+
static async instantiate(source: MTFWasmSource): Promise<MTFRuntime> {
|
|
111
|
+
const instance =
|
|
112
|
+
source instanceof ArrayBuffer || ArrayBuffer.isView(source)
|
|
113
|
+
? (await WebAssembly.instantiate(source, {})).instance
|
|
114
|
+
: await WebAssembly.instantiate(source, {})
|
|
115
|
+
|
|
116
|
+
return new MTFRuntime(instance.exports)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Loads a serialized SentencePiece unigram model.
|
|
121
|
+
*
|
|
122
|
+
* @throws When the bytes are malformed or the model uses a feature the runtime does not implement.
|
|
123
|
+
*/
|
|
124
|
+
loadModel(bytes: Uint8Array): MTFModel {
|
|
125
|
+
const handle = this.#withInput(bytes, (ptr, len) => this.#exports.mtf_model_load(ptr, len))
|
|
126
|
+
|
|
127
|
+
if (handle === 0) throw new Error(`mtf: model failed to load: ${this.lastError()}`)
|
|
128
|
+
|
|
129
|
+
return new MTFModel(this, this.#exports, handle)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Reads the message the last failed call recorded.
|
|
134
|
+
*
|
|
135
|
+
* @internal
|
|
136
|
+
*/
|
|
137
|
+
lastError(): string {
|
|
138
|
+
const { mtf_error_ptr, mtf_error_len, memory } = this.#exports
|
|
139
|
+
|
|
140
|
+
return decoder.decode(new Uint8Array(memory.buffer, mtf_error_ptr(), mtf_error_len()))
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Copies `bytes` into WASM memory for the duration of `call`.
|
|
145
|
+
*
|
|
146
|
+
* @internal
|
|
147
|
+
*/
|
|
148
|
+
withInput<T>(bytes: Uint8Array, call: (ptr: number, len: number) => T): T {
|
|
149
|
+
return this.#withInput(bytes, call)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
#withInput<T>(bytes: Uint8Array, call: (ptr: number, len: number) => T): T {
|
|
153
|
+
const { mtf_alloc, mtf_dealloc, memory } = this.#exports
|
|
154
|
+
const ptr = mtf_alloc(bytes.byteLength)
|
|
155
|
+
|
|
156
|
+
if (ptr === 0) throw new Error(`mtf: could not allocate ${bytes.byteLength} bytes`)
|
|
157
|
+
|
|
158
|
+
try {
|
|
159
|
+
new Uint8Array(memory.buffer, ptr, bytes.byteLength).set(bytes)
|
|
160
|
+
|
|
161
|
+
return call(ptr, bytes.byteLength)
|
|
162
|
+
} finally {
|
|
163
|
+
mtf_dealloc(ptr, bytes.byteLength)
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* A loaded model.
|
|
170
|
+
*
|
|
171
|
+
* Dispose it with `using` or {@linkcode MTFModel.dispose} to release its WASM memory.
|
|
172
|
+
*/
|
|
173
|
+
export class MTFModel implements Disposable {
|
|
174
|
+
readonly #runtime: MTFRuntime
|
|
175
|
+
readonly #exports: MTFExports
|
|
176
|
+
#handle: number
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* @internal Construct through {@linkcode MTFRuntime.loadModel}.
|
|
180
|
+
*/
|
|
181
|
+
constructor(runtime: MTFRuntime, exports: MTFExports, handle: number) {
|
|
182
|
+
this.#runtime = runtime
|
|
183
|
+
this.#exports = exports
|
|
184
|
+
this.#handle = handle
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
#live(): number {
|
|
188
|
+
if (this.#handle === 0) throw new Error("mtf: the model was disposed")
|
|
189
|
+
|
|
190
|
+
return this.#handle
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* The number of vocabulary rows.
|
|
195
|
+
*/
|
|
196
|
+
get vocabSize(): number {
|
|
197
|
+
return this.#exports.mtf_vocab_size(this.#live())
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Encodes `text` into pieces with UTF-8 byte offsets.
|
|
202
|
+
*/
|
|
203
|
+
encode(text: string): MTFEncodeResult {
|
|
204
|
+
const handle = this.#live()
|
|
205
|
+
const { mtf_encode, mtf_words_ptr, mtf_bytes_ptr, memory } = this.#exports
|
|
206
|
+
const count = this.#runtime.withInput(encoder.encode(text), (ptr, len) => mtf_encode(handle, ptr, len))
|
|
207
|
+
|
|
208
|
+
if (count < 0) throw new Error(`mtf: encode failed: ${this.#runtime.lastError()}`)
|
|
209
|
+
|
|
210
|
+
const words = new Uint32Array(memory.buffer, mtf_words_ptr(handle), count * PIECE_WORDS)
|
|
211
|
+
const bytesPtr = mtf_bytes_ptr(handle)
|
|
212
|
+
const result: MTFEncodeResult = { ids: [], pieces: [], begins: [], ends: [] }
|
|
213
|
+
|
|
214
|
+
for (let i = 0; i < count; i++) {
|
|
215
|
+
const w = i * PIECE_WORDS
|
|
216
|
+
|
|
217
|
+
result.ids.push(words[w]!)
|
|
218
|
+
result.begins.push(words[w + 1]!)
|
|
219
|
+
result.ends.push(words[w + 2]!)
|
|
220
|
+
result.pieces.push(decoder.decode(new Uint8Array(memory.buffer, bytesPtr + words[w + 3]!, words[w + 4]!)))
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return result
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Decodes vocabulary IDs into text.
|
|
228
|
+
*
|
|
229
|
+
* @throws On an ID outside the vocabulary.
|
|
230
|
+
*/
|
|
231
|
+
decode(ids: ArrayLike<number>): string {
|
|
232
|
+
const handle = this.#live()
|
|
233
|
+
const { mtf_decode, mtf_bytes_ptr, memory } = this.#exports
|
|
234
|
+
const packed = new Uint32Array(ids.length)
|
|
235
|
+
|
|
236
|
+
for (let i = 0; i < ids.length; i++) {
|
|
237
|
+
const id = ids[i]!
|
|
238
|
+
|
|
239
|
+
if (!Number.isInteger(id) || id < 0) throw new Error(`mtf: invalid id: ${id}`)
|
|
240
|
+
|
|
241
|
+
packed[i] = id
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const bytes = new Uint8Array(packed.buffer)
|
|
245
|
+
const length = this.#runtime.withInput(bytes, (ptr) => mtf_decode(handle, ptr, packed.length))
|
|
246
|
+
|
|
247
|
+
if (length < 0) throw new Error(`mtf: decode failed: ${this.#runtime.lastError()}`)
|
|
248
|
+
|
|
249
|
+
return decoder.decode(new Uint8Array(memory.buffer, mtf_bytes_ptr(handle), length))
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Releases the model's WASM memory.
|
|
254
|
+
* Later calls on this model throw.
|
|
255
|
+
*/
|
|
256
|
+
dispose(): void {
|
|
257
|
+
if (this.#handle === 0) return
|
|
258
|
+
|
|
259
|
+
this.#exports.mtf_model_free(this.#handle)
|
|
260
|
+
this.#handle = 0
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
[Symbol.dispose](): void {
|
|
264
|
+
this.dispose()
|
|
265
|
+
}
|
|
266
|
+
}
|
package/out/index.d.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* The TypeScript loader for the Multilingual Token Framework's WebAssembly runtime.
|
|
7
|
+
*
|
|
8
|
+
* The module's import has no side effects: the WASM fetch and instantiation happen in the caller.
|
|
9
|
+
* The caller obtains the WASM bytes or a compiled `WebAssembly.Module` (Workers supply the latter)
|
|
10
|
+
* and passes it to {@linkcode MTFRuntime.instantiate}.
|
|
11
|
+
* `MTF_WASM_URL` locates the committed binary beside this package.
|
|
12
|
+
* A loaded model's encode and decode methods are synchronous.
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* The location of the committed `wasm/mtf.wasm`, resolved from this module.
|
|
16
|
+
*
|
|
17
|
+
* The same relative path holds from `lib/` in the repository and from `out/` in a tarball.
|
|
18
|
+
*/
|
|
19
|
+
export declare const MTF_WASM_URL: import("node:url").URL;
|
|
20
|
+
/**
|
|
21
|
+
* A compiled `WebAssembly.Module`, as a Worker runtime supplies it.
|
|
22
|
+
*
|
|
23
|
+
* The type is opaque because the Node type preset omits the DOM library that declares
|
|
24
|
+
* `WebAssembly`, and this package's declarations must type-check without it.
|
|
25
|
+
*/
|
|
26
|
+
export interface MTFCompiledModule {
|
|
27
|
+
readonly [Symbol.toStringTag]: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The bytes or compiled module the runtime instantiates from.
|
|
31
|
+
*/
|
|
32
|
+
export type MTFWasmSource = MTFCompiledModule | ArrayBuffer | Uint8Array<ArrayBuffer>;
|
|
33
|
+
/**
|
|
34
|
+
* The pieces of one encoded input.
|
|
35
|
+
*
|
|
36
|
+
* `begins[i]` and `ends[i]` are UTF-8 byte offsets into the input as `TextEncoder` encodes it.
|
|
37
|
+
* A byte-fallback piece other than the last for its character has `begins[i] === ends[i]`.
|
|
38
|
+
*/
|
|
39
|
+
export interface MTFEncodeResult {
|
|
40
|
+
ids: number[];
|
|
41
|
+
pieces: string[];
|
|
42
|
+
begins: number[];
|
|
43
|
+
ends: number[];
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The C ABI that `crates/mtf-wasm` exports.
|
|
47
|
+
*/
|
|
48
|
+
interface MTFExports {
|
|
49
|
+
memory: {
|
|
50
|
+
readonly buffer: ArrayBuffer;
|
|
51
|
+
};
|
|
52
|
+
mtf_alloc(len: number): number;
|
|
53
|
+
mtf_dealloc(ptr: number, len: number): void;
|
|
54
|
+
mtf_error_ptr(): number;
|
|
55
|
+
mtf_error_len(): number;
|
|
56
|
+
mtf_model_load(ptr: number, len: number): number;
|
|
57
|
+
mtf_model_free(handle: number): void;
|
|
58
|
+
mtf_vocab_size(handle: number): number;
|
|
59
|
+
mtf_encode(handle: number, ptr: number, len: number): number;
|
|
60
|
+
mtf_decode(handle: number, ids: number, count: number): number;
|
|
61
|
+
mtf_words_ptr(handle: number): number;
|
|
62
|
+
mtf_bytes_ptr(handle: number): number;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* One instantiated WebAssembly runtime.
|
|
66
|
+
*
|
|
67
|
+
* A runtime may hold several loaded models at once.
|
|
68
|
+
*/
|
|
69
|
+
export declare class MTFRuntime {
|
|
70
|
+
#private;
|
|
71
|
+
private constructor();
|
|
72
|
+
/**
|
|
73
|
+
* Instantiates the runtime from WASM bytes or a compiled module.
|
|
74
|
+
*/
|
|
75
|
+
static instantiate(source: MTFWasmSource): Promise<MTFRuntime>;
|
|
76
|
+
/**
|
|
77
|
+
* Loads a serialized SentencePiece unigram model.
|
|
78
|
+
*
|
|
79
|
+
* @throws When the bytes are malformed or the model uses a feature the runtime does not implement.
|
|
80
|
+
*/
|
|
81
|
+
loadModel(bytes: Uint8Array): MTFModel;
|
|
82
|
+
/**
|
|
83
|
+
* Reads the message the last failed call recorded.
|
|
84
|
+
*
|
|
85
|
+
* @internal
|
|
86
|
+
*/
|
|
87
|
+
lastError(): string;
|
|
88
|
+
/**
|
|
89
|
+
* Copies `bytes` into WASM memory for the duration of `call`.
|
|
90
|
+
*
|
|
91
|
+
* @internal
|
|
92
|
+
*/
|
|
93
|
+
withInput<T>(bytes: Uint8Array, call: (ptr: number, len: number) => T): T;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* A loaded model.
|
|
97
|
+
*
|
|
98
|
+
* Dispose it with `using` or {@linkcode MTFModel.dispose} to release its WASM memory.
|
|
99
|
+
*/
|
|
100
|
+
export declare class MTFModel implements Disposable {
|
|
101
|
+
#private;
|
|
102
|
+
/**
|
|
103
|
+
* @internal Construct through {@linkcode MTFRuntime.loadModel}.
|
|
104
|
+
*/
|
|
105
|
+
constructor(runtime: MTFRuntime, exports: MTFExports, handle: number);
|
|
106
|
+
/**
|
|
107
|
+
* The number of vocabulary rows.
|
|
108
|
+
*/
|
|
109
|
+
get vocabSize(): number;
|
|
110
|
+
/**
|
|
111
|
+
* Encodes `text` into pieces with UTF-8 byte offsets.
|
|
112
|
+
*/
|
|
113
|
+
encode(text: string): MTFEncodeResult;
|
|
114
|
+
/**
|
|
115
|
+
* Decodes vocabulary IDs into text.
|
|
116
|
+
*
|
|
117
|
+
* @throws On an ID outside the vocabulary.
|
|
118
|
+
*/
|
|
119
|
+
decode(ids: ArrayLike<number>): string;
|
|
120
|
+
/**
|
|
121
|
+
* Releases the model's WASM memory.
|
|
122
|
+
* Later calls on this model throw.
|
|
123
|
+
*/
|
|
124
|
+
dispose(): void;
|
|
125
|
+
[Symbol.dispose](): void;
|
|
126
|
+
}
|
|
127
|
+
export {};
|
|
128
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH;;;;GAIG;AACH,eAAO,MAAM,YAAY,wBAA+C,CAAA;AAExE;;;;;GAKG;AACH,MAAM,WAAW,iBAAiB;IACjC,QAAQ,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,EAAE,MAAM,CAAA;CACrC;AAED;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG,iBAAiB,GAAG,WAAW,GAAG,UAAU,CAAC,WAAW,CAAC,CAAA;AAqBrF;;;;;GAKG;AACH,MAAM,WAAW,eAAe;IAC/B,GAAG,EAAE,MAAM,EAAE,CAAA;IACb,MAAM,EAAE,MAAM,EAAE,CAAA;IAChB,MAAM,EAAE,MAAM,EAAE,CAAA;IAChB,IAAI,EAAE,MAAM,EAAE,CAAA;CACd;AAED;;GAEG;AACH,UAAU,UAAU;IACnB,MAAM,EAAE;QAAE,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAA;KAAE,CAAA;IACxC,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;IAC9B,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI,CAAA;IAC3C,aAAa,IAAI,MAAM,CAAA;IACvB,aAAa,IAAI,MAAM,CAAA;IACvB,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;IAChD,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAA;IACpC,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAA;IACtC,UAAU,CAAC,MAAM,EAAE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|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* The TypeScript loader for the Multilingual Token Framework's WebAssembly runtime.
|
|
7
|
+
*
|
|
8
|
+
* The module's import has no side effects: the WASM fetch and instantiation happen in the caller.
|
|
9
|
+
* The caller obtains the WASM bytes or a compiled `WebAssembly.Module` (Workers supply the latter)
|
|
10
|
+
* and passes it to {@linkcode MTFRuntime.instantiate}.
|
|
11
|
+
* `MTF_WASM_URL` locates the committed binary beside this package.
|
|
12
|
+
* A loaded model's encode and decode methods are synchronous.
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* The location of the committed `wasm/mtf.wasm`, resolved from this module.
|
|
16
|
+
*
|
|
17
|
+
* The same relative path holds from `lib/` in the repository and from `out/` in a tarball.
|
|
18
|
+
*/
|
|
19
|
+
export const MTF_WASM_URL = new URL("../wasm/mtf.wasm", import.meta.url);
|
|
20
|
+
/**
|
|
21
|
+
* Words per piece in the encode output buffer: id, begin, end, piece offset, piece length.
|
|
22
|
+
*/
|
|
23
|
+
const PIECE_WORDS = 5;
|
|
24
|
+
const encoder = new TextEncoder();
|
|
25
|
+
const decoder = new TextDecoder("utf-8", { fatal: true });
|
|
26
|
+
/**
|
|
27
|
+
* One instantiated WebAssembly runtime.
|
|
28
|
+
*
|
|
29
|
+
* A runtime may hold several loaded models at once.
|
|
30
|
+
*/
|
|
31
|
+
export class MTFRuntime {
|
|
32
|
+
#exports;
|
|
33
|
+
constructor(exports) {
|
|
34
|
+
this.#exports = exports;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Instantiates the runtime from WASM bytes or a compiled module.
|
|
38
|
+
*/
|
|
39
|
+
static async instantiate(source) {
|
|
40
|
+
const instance = source instanceof ArrayBuffer || ArrayBuffer.isView(source)
|
|
41
|
+
? (await WebAssembly.instantiate(source, {})).instance
|
|
42
|
+
: await WebAssembly.instantiate(source, {});
|
|
43
|
+
return new MTFRuntime(instance.exports);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Loads a serialized SentencePiece unigram model.
|
|
47
|
+
*
|
|
48
|
+
* @throws When the bytes are malformed or the model uses a feature the runtime does not implement.
|
|
49
|
+
*/
|
|
50
|
+
loadModel(bytes) {
|
|
51
|
+
const handle = this.#withInput(bytes, (ptr, len) => this.#exports.mtf_model_load(ptr, len));
|
|
52
|
+
if (handle === 0)
|
|
53
|
+
throw new Error(`mtf: model failed to load: ${this.lastError()}`);
|
|
54
|
+
return new MTFModel(this, this.#exports, handle);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Reads the message the last failed call recorded.
|
|
58
|
+
*
|
|
59
|
+
* @internal
|
|
60
|
+
*/
|
|
61
|
+
lastError() {
|
|
62
|
+
const { mtf_error_ptr, mtf_error_len, memory } = this.#exports;
|
|
63
|
+
return decoder.decode(new Uint8Array(memory.buffer, mtf_error_ptr(), mtf_error_len()));
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Copies `bytes` into WASM memory for the duration of `call`.
|
|
67
|
+
*
|
|
68
|
+
* @internal
|
|
69
|
+
*/
|
|
70
|
+
withInput(bytes, call) {
|
|
71
|
+
return this.#withInput(bytes, call);
|
|
72
|
+
}
|
|
73
|
+
#withInput(bytes, call) {
|
|
74
|
+
const { mtf_alloc, mtf_dealloc, memory } = this.#exports;
|
|
75
|
+
const ptr = mtf_alloc(bytes.byteLength);
|
|
76
|
+
if (ptr === 0)
|
|
77
|
+
throw new Error(`mtf: could not allocate ${bytes.byteLength} bytes`);
|
|
78
|
+
try {
|
|
79
|
+
new Uint8Array(memory.buffer, ptr, bytes.byteLength).set(bytes);
|
|
80
|
+
return call(ptr, bytes.byteLength);
|
|
81
|
+
}
|
|
82
|
+
finally {
|
|
83
|
+
mtf_dealloc(ptr, bytes.byteLength);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* A loaded model.
|
|
89
|
+
*
|
|
90
|
+
* Dispose it with `using` or {@linkcode MTFModel.dispose} to release its WASM memory.
|
|
91
|
+
*/
|
|
92
|
+
export class MTFModel {
|
|
93
|
+
#runtime;
|
|
94
|
+
#exports;
|
|
95
|
+
#handle;
|
|
96
|
+
/**
|
|
97
|
+
* @internal Construct through {@linkcode MTFRuntime.loadModel}.
|
|
98
|
+
*/
|
|
99
|
+
constructor(runtime, exports, handle) {
|
|
100
|
+
this.#runtime = runtime;
|
|
101
|
+
this.#exports = exports;
|
|
102
|
+
this.#handle = handle;
|
|
103
|
+
}
|
|
104
|
+
#live() {
|
|
105
|
+
if (this.#handle === 0)
|
|
106
|
+
throw new Error("mtf: the model was disposed");
|
|
107
|
+
return this.#handle;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* The number of vocabulary rows.
|
|
111
|
+
*/
|
|
112
|
+
get vocabSize() {
|
|
113
|
+
return this.#exports.mtf_vocab_size(this.#live());
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Encodes `text` into pieces with UTF-8 byte offsets.
|
|
117
|
+
*/
|
|
118
|
+
encode(text) {
|
|
119
|
+
const handle = this.#live();
|
|
120
|
+
const { mtf_encode, mtf_words_ptr, mtf_bytes_ptr, memory } = this.#exports;
|
|
121
|
+
const count = this.#runtime.withInput(encoder.encode(text), (ptr, len) => mtf_encode(handle, ptr, len));
|
|
122
|
+
if (count < 0)
|
|
123
|
+
throw new Error(`mtf: encode failed: ${this.#runtime.lastError()}`);
|
|
124
|
+
const words = new Uint32Array(memory.buffer, mtf_words_ptr(handle), count * PIECE_WORDS);
|
|
125
|
+
const bytesPtr = mtf_bytes_ptr(handle);
|
|
126
|
+
const result = { ids: [], pieces: [], begins: [], ends: [] };
|
|
127
|
+
for (let i = 0; i < count; i++) {
|
|
128
|
+
const w = i * PIECE_WORDS;
|
|
129
|
+
result.ids.push(words[w]);
|
|
130
|
+
result.begins.push(words[w + 1]);
|
|
131
|
+
result.ends.push(words[w + 2]);
|
|
132
|
+
result.pieces.push(decoder.decode(new Uint8Array(memory.buffer, bytesPtr + words[w + 3], words[w + 4])));
|
|
133
|
+
}
|
|
134
|
+
return result;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Decodes vocabulary IDs into text.
|
|
138
|
+
*
|
|
139
|
+
* @throws On an ID outside the vocabulary.
|
|
140
|
+
*/
|
|
141
|
+
decode(ids) {
|
|
142
|
+
const handle = this.#live();
|
|
143
|
+
const { mtf_decode, mtf_bytes_ptr, memory } = this.#exports;
|
|
144
|
+
const packed = new Uint32Array(ids.length);
|
|
145
|
+
for (let i = 0; i < ids.length; i++) {
|
|
146
|
+
const id = ids[i];
|
|
147
|
+
if (!Number.isInteger(id) || id < 0)
|
|
148
|
+
throw new Error(`mtf: invalid id: ${id}`);
|
|
149
|
+
packed[i] = id;
|
|
150
|
+
}
|
|
151
|
+
const bytes = new Uint8Array(packed.buffer);
|
|
152
|
+
const length = this.#runtime.withInput(bytes, (ptr) => mtf_decode(handle, ptr, packed.length));
|
|
153
|
+
if (length < 0)
|
|
154
|
+
throw new Error(`mtf: decode failed: ${this.#runtime.lastError()}`);
|
|
155
|
+
return decoder.decode(new Uint8Array(memory.buffer, mtf_bytes_ptr(handle), length));
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Releases the model's WASM memory.
|
|
159
|
+
* Later calls on this model throw.
|
|
160
|
+
*/
|
|
161
|
+
dispose() {
|
|
162
|
+
if (this.#handle === 0)
|
|
163
|
+
return;
|
|
164
|
+
this.#exports.mtf_model_free(this.#handle);
|
|
165
|
+
this.#handle = 0;
|
|
166
|
+
}
|
|
167
|
+
[Symbol.dispose]() {
|
|
168
|
+
this.dispose();
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
//# sourceMappingURL=index.js.map
|
package/out/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":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AoB,EAAE,EAAE,CAAC,CAAA;YAE9E,MAAM,CAAC,CAAC,CAAC,GAAG,EAAE,CAAA;QACf,CAAC;QAED,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;QAC3C,MAAM,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,UAAU,CAAC,MAAM,EAAE,GAAG,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,CAAA;QAE9F,IAAI,MAAM,GAAG,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,uBAAuB,IAAI,CAAC,QAAQ,CAAC,SAAS,EAAE,EAAE,CAAC,CAAA;QAEnF,OAAO,OAAO,CAAC,MAAM,CAAC,IAAI,UAAU,CAAC,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC,CAAA;IACpF,CAAC;IAED;;;OAGG;IACH,OAAO;QACN,IAAI,IAAI,CAAC,OAAO,KAAK,CAAC;YAAE,OAAM;QAE9B,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;QAC1C,IAAI,CAAC,OAAO,GAAG,CAAC,CAAA;IACjB,CAAC;IAED,CAAC,MAAM,CAAC,OAAO,CAAC;QACf,IAAI,CAAC,OAAO,EAAE,CAAA;IACf,CAAC;CACD"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* Builds `crates/mtf-wasm` for `wasm32-unknown-unknown` and writes the result to `wasm/mtf.wasm`.
|
|
7
|
+
*
|
|
8
|
+
* With `--check`, the tool builds without writing.
|
|
9
|
+
* It exits non-zero when the build's SHA-256 differs from the committed binary's.
|
|
10
|
+
*
|
|
11
|
+
* Usage: `node packages/mtf/tools/build-wasm.run.ts [--check]`
|
|
12
|
+
*/
|
|
13
|
+
export {};
|
|
14
|
+
//# sourceMappingURL=build-wasm.run.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build-wasm.run.d.ts","sourceRoot":"","sources":["../../tools/build-wasm.run.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* Builds `crates/mtf-wasm` for `wasm32-unknown-unknown` and writes the result to `wasm/mtf.wasm`.
|
|
7
|
+
*
|
|
8
|
+
* With `--check`, the tool builds without writing.
|
|
9
|
+
* It exits non-zero when the build's SHA-256 differs from the committed binary's.
|
|
10
|
+
*
|
|
11
|
+
* Usage: `node packages/mtf/tools/build-wasm.run.ts [--check]`
|
|
12
|
+
*/
|
|
13
|
+
import { statPath } from "@mailwoman/core/fs/readers/stat";
|
|
14
|
+
import { replaceFileAtomically } from "@mailwoman/core/fs/writers";
|
|
15
|
+
import { sha256File } from "@mailwoman/core/hash";
|
|
16
|
+
import { workspacePath } from "@mailwoman/core/paths";
|
|
17
|
+
import { runFile } from "@mailwoman/core/process";
|
|
18
|
+
import { parseArguments } from "@mailwoman/core/scripting/arguments";
|
|
19
|
+
const { values } = parseArguments({ options: { check: { type: "boolean", default: false } } });
|
|
20
|
+
const workspace = workspacePath("mtf");
|
|
21
|
+
const built = workspacePath("mtf", "target", "wasm32-unknown-unknown", "release", "mtf_wasm.wasm");
|
|
22
|
+
const committed = workspacePath("mtf", "wasm", "mtf.wasm");
|
|
23
|
+
await runFile("cargo", ["build", "--release", "--locked", "--target", "wasm32-unknown-unknown", "-p", "mtf-wasm"], {
|
|
24
|
+
cwd: workspace,
|
|
25
|
+
});
|
|
26
|
+
const builtDigest = await sha256File(built);
|
|
27
|
+
const { size } = await statPath(built);
|
|
28
|
+
if (values.check) {
|
|
29
|
+
const committedDigest = await sha256File(committed);
|
|
30
|
+
if (committedDigest === builtDigest) {
|
|
31
|
+
console.log(`wasm/mtf.wasm matches the build: ${builtDigest} (${size} bytes)`);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
console.error(`wasm/mtf.wasm is stale: committed ${committedDigest}, built ${builtDigest} (${size} bytes)`);
|
|
35
|
+
process.exitCode = 1;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
await replaceFileAtomically(built, committed);
|
|
40
|
+
console.log(`wrote wasm/mtf.wasm: ${builtDigest} (${size} bytes)`);
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=build-wasm.run.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build-wasm.run.js","sourceRoot":"","sources":["../../tools/build-wasm.run.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,iCAAiC,CAAA;AAC1D,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAA;AAClE,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AACjD,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAA;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAA;AACjD,OAAO,EAAE,cAAc,EAAE,MAAM,qCAAqC,CAAA;AAEpE,MAAM,EAAE,MAAM,EAAE,GAAG,cAAc,CAAC,EAAE,OAAO,EAAE,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,CAAA;AAE9F,MAAM,SAAS,GAAG,aAAa,CAAC,KAAK,CAAC,CAAA;AACtC,MAAM,KAAK,GAAG,aAAa,CAAC,KAAK,EAAE,QAAQ,EAAE,wBAAwB,EAAE,SAAS,EAAE,eAAe,CAAC,CAAA;AAClG,MAAM,SAAS,GAAG,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,CAAC,CAAA;AAE1D,MAAM,OAAO,CAAC,OAAO,EAAE,CAAC,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,UAAU,EAAE,wBAAwB,EAAE,IAAI,EAAE,UAAU,CAAC,EAAE;IAClH,GAAG,EAAE,SAAS;CACd,CAAC,CAAA;AAEF,MAAM,WAAW,GAAG,MAAM,UAAU,CAAC,KAAK,CAAC,CAAA;AAC3C,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,QAAQ,CAAC,KAAK,CAAC,CAAA;AAEtC,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;IAClB,MAAM,eAAe,GAAG,MAAM,UAAU,CAAC,SAAS,CAAC,CAAA;IAEnD,IAAI,eAAe,KAAK,WAAW,EAAE,CAAC;QACrC,OAAO,CAAC,GAAG,CAAC,oCAAoC,WAAW,KAAK,IAAI,SAAS,CAAC,CAAA;IAC/E,CAAC;SAAM,CAAC;QACP,OAAO,CAAC,KAAK,CAAC,qCAAqC,eAAe,WAAW,WAAW,KAAK,IAAI,SAAS,CAAC,CAAA;QAE3G,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAA;IACrB,CAAC;AACF,CAAC;KAAM,CAAC;IACP,MAAM,qBAAqB,CAAC,KAAK,EAAE,SAAS,CAAC,CAAA;IAE7C,OAAO,CAAC,GAAG,CAAC,wBAAwB,WAAW,KAAK,IAAI,SAAS,CAAC,CAAA;AACnE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,98 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mailwoman/mtf",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "10.7.0",
|
|
4
|
+
"description": "Multilingual Token Framework: Mailwoman's subword tokenizer, a Rust core compiled to WASM with a TypeScript loader. It reproduces SentencePiece v0.2.2's pieces and offsets on the shipped unigram model and is the tokenizer runtime for @mailwoman/neural.",
|
|
5
|
+
"license": "AGPL-3.0-only OR LicenseRef-Commercial",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/sister-software/mailwoman.git",
|
|
9
|
+
"directory": "packages/mtf"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"out/**/*.js",
|
|
13
|
+
"out/**/*.js.map",
|
|
14
|
+
"out/**/*.d.ts",
|
|
15
|
+
"out/**/*.d.ts.map",
|
|
16
|
+
"lib/**/*.ts",
|
|
17
|
+
"tools/**/*.ts",
|
|
18
|
+
"wasm/mtf.wasm",
|
|
19
|
+
"README.md",
|
|
20
|
+
"THIRD_PARTY_NOTICES.md",
|
|
21
|
+
"third-party/**",
|
|
22
|
+
"!**/*.test.ts",
|
|
23
|
+
"!test/**"
|
|
24
|
+
],
|
|
25
|
+
"type": "module",
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"imports": {
|
|
28
|
+
"#tools/*": {
|
|
29
|
+
"types": "./out/tools/*.d.ts",
|
|
30
|
+
"node": "./out/tools/*.js",
|
|
31
|
+
"default": "./out/tools/*.js"
|
|
32
|
+
},
|
|
33
|
+
"#*": {
|
|
34
|
+
"types": "./out/*.d.ts",
|
|
35
|
+
"node": "./out/*.js",
|
|
36
|
+
"default": "./out/*.js"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"exports": {
|
|
40
|
+
"./package.json": "./package.json",
|
|
41
|
+
"./wasm/mtf.wasm": "./wasm/mtf.wasm",
|
|
42
|
+
".": {
|
|
43
|
+
"types": "./out/index.d.ts",
|
|
44
|
+
"node": "./out/index.js",
|
|
45
|
+
"default": "./out/index.js"
|
|
46
|
+
},
|
|
47
|
+
"./tools/*": {
|
|
48
|
+
"types": "./out/tools/*.d.ts",
|
|
49
|
+
"node": "./out/tools/*.js",
|
|
50
|
+
"default": "./out/tools/*.js"
|
|
51
|
+
},
|
|
52
|
+
"./*": {
|
|
53
|
+
"types": "./out/*.d.ts",
|
|
54
|
+
"node": "./out/*.js",
|
|
55
|
+
"default": "./out/*.js"
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"publishConfig": {
|
|
59
|
+
"access": "public",
|
|
60
|
+
"exports": {
|
|
61
|
+
"./package.json": "./package.json",
|
|
62
|
+
"./wasm/mtf.wasm": "./wasm/mtf.wasm",
|
|
63
|
+
".": {
|
|
64
|
+
"types": "./out/index.d.ts",
|
|
65
|
+
"node": "./out/index.js",
|
|
66
|
+
"default": "./out/index.js"
|
|
67
|
+
},
|
|
68
|
+
"./tools/*": {
|
|
69
|
+
"types": "./out/tools/*.d.ts",
|
|
70
|
+
"node": "./out/tools/*.js",
|
|
71
|
+
"default": "./out/tools/*.js"
|
|
72
|
+
},
|
|
73
|
+
"./*": {
|
|
74
|
+
"types": "./out/*.d.ts",
|
|
75
|
+
"node": "./out/*.js",
|
|
76
|
+
"default": "./out/*.js"
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
"imports": {
|
|
80
|
+
"#tools/*": {
|
|
81
|
+
"types": "./out/tools/*.d.ts",
|
|
82
|
+
"node": "./out/tools/*.js",
|
|
83
|
+
"default": "./out/tools/*.js"
|
|
84
|
+
},
|
|
85
|
+
"#*": {
|
|
86
|
+
"types": "./out/*.d.ts",
|
|
87
|
+
"node": "./out/*.js",
|
|
88
|
+
"default": "./out/*.js"
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
"dependencies": {
|
|
93
|
+
"@mailwoman/core": "10.7.0"
|
|
94
|
+
},
|
|
95
|
+
"devDependencies": {
|
|
96
|
+
"@types/node": "^26.6.5"
|
|
97
|
+
}
|
|
6
98
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Copyright (c) 2008-2011, Susumu Yata
|
|
2
|
+
All rights reserved.
|
|
3
|
+
|
|
4
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
5
|
+
|
|
6
|
+
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
7
|
+
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
8
|
+
- Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
9
|
+
|
|
10
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* Builds `crates/mtf-wasm` for `wasm32-unknown-unknown` and writes the result to `wasm/mtf.wasm`.
|
|
7
|
+
*
|
|
8
|
+
* With `--check`, the tool builds without writing.
|
|
9
|
+
* It exits non-zero when the build's SHA-256 differs from the committed binary's.
|
|
10
|
+
*
|
|
11
|
+
* Usage: `node packages/mtf/tools/build-wasm.run.ts [--check]`
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { statPath } from "@mailwoman/core/fs/readers/stat"
|
|
15
|
+
import { replaceFileAtomically } from "@mailwoman/core/fs/writers"
|
|
16
|
+
import { sha256File } from "@mailwoman/core/hash"
|
|
17
|
+
import { workspacePath } from "@mailwoman/core/paths"
|
|
18
|
+
import { runFile } from "@mailwoman/core/process"
|
|
19
|
+
import { parseArguments } from "@mailwoman/core/scripting/arguments"
|
|
20
|
+
|
|
21
|
+
const { values } = parseArguments({ options: { check: { type: "boolean", default: false } } })
|
|
22
|
+
|
|
23
|
+
const workspace = workspacePath("mtf")
|
|
24
|
+
const built = workspacePath("mtf", "target", "wasm32-unknown-unknown", "release", "mtf_wasm.wasm")
|
|
25
|
+
const committed = workspacePath("mtf", "wasm", "mtf.wasm")
|
|
26
|
+
|
|
27
|
+
await runFile("cargo", ["build", "--release", "--locked", "--target", "wasm32-unknown-unknown", "-p", "mtf-wasm"], {
|
|
28
|
+
cwd: workspace,
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
const builtDigest = await sha256File(built)
|
|
32
|
+
const { size } = await statPath(built)
|
|
33
|
+
|
|
34
|
+
if (values.check) {
|
|
35
|
+
const committedDigest = await sha256File(committed)
|
|
36
|
+
|
|
37
|
+
if (committedDigest === builtDigest) {
|
|
38
|
+
console.log(`wasm/mtf.wasm matches the build: ${builtDigest} (${size} bytes)`)
|
|
39
|
+
} else {
|
|
40
|
+
console.error(`wasm/mtf.wasm is stale: committed ${committedDigest}, built ${builtDigest} (${size} bytes)`)
|
|
41
|
+
|
|
42
|
+
process.exitCode = 1
|
|
43
|
+
}
|
|
44
|
+
} else {
|
|
45
|
+
await replaceFileAtomically(built, committed)
|
|
46
|
+
|
|
47
|
+
console.log(`wrote wasm/mtf.wasm: ${builtDigest} (${size} bytes)`)
|
|
48
|
+
}
|
package/wasm/mtf.wasm
ADDED
|
Binary file
|