@mailwoman/sentencepiece-wasm 10.0.0 → 10.1.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/binding.cpp +2 -2
- package/build.sh +11 -11
- package/index.d.ts +30 -15
- package/package.json +1 -15
package/binding.cpp
CHANGED
|
@@ -58,7 +58,7 @@ public:
|
|
|
58
58
|
/**
|
|
59
59
|
* Encode with native offsets. Returns a JS object:
|
|
60
60
|
* { pieces: string[], ids: number[], begins: number[], ends: number[] } — begins/ends are byte
|
|
61
|
-
* offsets into the UTF-8 encoding of `text` (the exact bytes embind
|
|
61
|
+
* offsets into the UTF-8 encoding of `text` (the exact bytes embind marshaled in).
|
|
62
62
|
*/
|
|
63
63
|
emscripten::val encodeWithOffsets(const std::string& text) const {
|
|
64
64
|
sentencepiece::SentencePieceText spt;
|
|
@@ -94,7 +94,7 @@ public:
|
|
|
94
94
|
|
|
95
95
|
/**
|
|
96
96
|
* Detokenize ids back to text. Invalid ids surface as the empty string plus the error field on
|
|
97
|
-
* the wrapper's
|
|
97
|
+
* the wrapper's interface being violated upstream — the TS layer validates lengths.
|
|
98
98
|
*/
|
|
99
99
|
std::string decodeIDs(const std::vector<int>& ids) const {
|
|
100
100
|
std::string out;
|
package/build.sh
CHANGED
|
@@ -4,17 +4,17 @@
|
|
|
4
4
|
# @license AGPL-3.0
|
|
5
5
|
# @author Teffen Ellis, et al.
|
|
6
6
|
#
|
|
7
|
-
# Build the committed `sentencepiece.mjs` artifact: google/sentencepiece at the
|
|
8
|
-
# compiled to
|
|
7
|
+
# Build the committed `sentencepiece.mjs` artifact: google/sentencepiece at the pinned tag below,
|
|
8
|
+
# compiled to wasm with emscripten and linked against binding.cpp (the embind wrapper exposing
|
|
9
9
|
# native per-piece byte offsets — task #26).
|
|
10
10
|
#
|
|
11
|
-
# The artifact is
|
|
11
|
+
# The artifact is committed (like core/data's reference blobs): consumers import
|
|
12
12
|
# `@mailwoman/sentencepiece-wasm` as a plain dependency and never need emscripten. Re-run this
|
|
13
13
|
# script only to bump the sentencepiece tag or change the binding; commit the regenerated .mjs in
|
|
14
14
|
# the same PR and note the tag bump in the header of index.d.ts.
|
|
15
15
|
#
|
|
16
16
|
# Prereqs: emsdk activated (`source ~/tools/emsdk/emsdk_env.sh`), cmake, git. The sentencepiece
|
|
17
|
-
# checkout is cached
|
|
17
|
+
# checkout is cached outside the repo (a toolchain input, not a repo artifact).
|
|
18
18
|
#
|
|
19
19
|
# Single-file output on purpose: -sSINGLE_FILE=1 embeds the wasm as base64 in the .mjs, matching
|
|
20
20
|
# how the previous runtime (@sctg/sentencepiece-js) shipped — no separate .wasm delivery path to
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
|
|
24
24
|
set -euo pipefail
|
|
25
25
|
|
|
26
|
-
# The pin. corpus-python trains with `sentencepiece>=0.2.2` — the runtime
|
|
26
|
+
# The pin. corpus-python trains with `sentencepiece>=0.2.2` — the runtime must tokenize
|
|
27
27
|
# byte-identically to the trainer, so the tags travel together (see project-tokenizer-mismatch).
|
|
28
28
|
SP_TAG="v0.2.2"
|
|
29
29
|
|
|
@@ -43,8 +43,8 @@ fi
|
|
|
43
43
|
git -C "$SP_SRC" fetch --tags
|
|
44
44
|
git -C "$SP_SRC" checkout "$SP_TAG"
|
|
45
45
|
|
|
46
|
-
# Static library build. SPM_ENABLE_SHARED=
|
|
47
|
-
# meaningless in
|
|
46
|
+
# Static library build. SPM_ENABLE_SHARED=off is required under emscripten; the TCMalloc probe is
|
|
47
|
+
# meaningless in wasm.
|
|
48
48
|
emcmake cmake -S "$SP_SRC" -B "$BUILD_DIR" \
|
|
49
49
|
-DCMAKE_BUILD_TYPE=Release \
|
|
50
50
|
-DSPM_ENABLE_SHARED=OFF \
|
|
@@ -59,13 +59,13 @@ cmake --build "$BUILD_DIR" -j "$(nproc)" > /dev/null
|
|
|
59
59
|
ABSL_LIBS=$(find "$BUILD_DIR/third_party/abseil-cpp" -name "*.a" | sort)
|
|
60
60
|
|
|
61
61
|
# Link the binding. Notes on the flags:
|
|
62
|
-
# -
|
|
62
|
+
# - modularize + EXPORT_ES6: the artifact default-exports an async factory (createSentencePiece).
|
|
63
63
|
# - SINGLE_FILE: wasm embedded as base64 (see header).
|
|
64
|
-
# -
|
|
64
|
+
# - environment=web,webview,worker,node: every consumer surface (browser demo, workers, Node CLI).
|
|
65
65
|
# - ALLOW_MEMORY_GROWTH: tokenizer.model is ~1.6MB and encode allocations scale with input length.
|
|
66
66
|
# - DISABLE_EXCEPTION_CATCHING=0: protobuf-lite + sentencepiece throw in cold error paths; without
|
|
67
|
-
# catching enabled a bad model file
|
|
68
|
-
# -
|
|
67
|
+
# catching enabled a bad model file aborts the runtime instead of returning a status.
|
|
68
|
+
# - filesystem=0: only LoadFromSerializedProto is bound — no FS use anywhere.
|
|
69
69
|
emcc "$SCRIPT_DIR/binding.cpp" \
|
|
70
70
|
-I "$SP_SRC/src" -I "$SP_SRC" -I "$SP_SRC/third_party/abseil-cpp" -I "$SP_SRC/third_party/protobuf-lite" -I "$SP_SRC/src/builtin_pb" -I "$BUILD_DIR" \
|
|
71
71
|
"$BUILD_DIR/src/libsentencepiece.a" \
|
package/index.d.ts
CHANGED
|
@@ -3,15 +3,19 @@
|
|
|
3
3
|
* @license AGPL-3.0
|
|
4
4
|
* @author Teffen Ellis, et al.
|
|
5
5
|
*
|
|
6
|
-
* Type surface of the committed `sentencepiece.mjs` artifact
|
|
7
|
-
* compiled to
|
|
8
|
-
*
|
|
6
|
+
* Type surface of the committed `sentencepiece.mjs` artifact, google/sentencepiece **v0.2.2**
|
|
7
|
+
* compiled to wasm with the native-offsets embind wrapper (`binding.cpp`). Rebuilt only by
|
|
8
|
+
* `build.sh`. Bump the tag there and note it here.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
* One encode result.
|
|
13
|
-
*
|
|
14
|
-
*
|
|
12
|
+
* One encode result.
|
|
13
|
+
*
|
|
14
|
+
* `begins` and `ends` are UTF-8 byte offsets into the encoded input.
|
|
15
|
+
* The upstream invariant is `utf8(text).slice(begins[i], ends[i])` equals the piece's surface.
|
|
16
|
+
*
|
|
17
|
+
* Consecutive pieces also occupy contiguous byte ranges.
|
|
18
|
+
* The TS tokenizer layer owns byte→UTF-16 conversion.
|
|
15
19
|
*/
|
|
16
20
|
export interface EncodeWithOffsetsResult {
|
|
17
21
|
pieces: string[]
|
|
@@ -19,33 +23,43 @@ export interface EncodeWithOffsetsResult {
|
|
|
19
23
|
begins: number[]
|
|
20
24
|
ends: number[]
|
|
21
25
|
/**
|
|
22
|
-
* Present
|
|
26
|
+
* Present instead of the arrays when encoding failed (errors cross the boundary as values).
|
|
23
27
|
*/
|
|
24
28
|
error?: string
|
|
25
29
|
}
|
|
26
30
|
|
|
27
31
|
/**
|
|
28
|
-
* The bound processor.
|
|
32
|
+
* The bound processor.
|
|
33
|
+
*
|
|
34
|
+
* Construct via the module factory, then `loadFromSerializedProto` once.
|
|
29
35
|
*/
|
|
30
36
|
export declare class SentencePieceProcessor {
|
|
31
37
|
constructor()
|
|
32
38
|
/**
|
|
33
|
-
* Load a `tokenizer.model` from its serialized-proto bytes.
|
|
34
|
-
*
|
|
35
|
-
*
|
|
39
|
+
* Load a `tokenizer.model` from its serialized-proto bytes.
|
|
40
|
+
*
|
|
41
|
+
* Takes serialized model bytes as a `Uint8Array`.
|
|
42
|
+
* Embind marshals JS strings to `std::string` as UTF-8, corrupting arbitrary binary bytes.
|
|
43
|
+
*
|
|
44
|
+
* Returns `""` on success, the sentencepiece status message on failure.
|
|
36
45
|
*/
|
|
37
46
|
loadFromSerializedProto(serialized: Uint8Array): string
|
|
38
47
|
encodeWithOffsets(text: string): EncodeWithOffsetsResult
|
|
39
48
|
decodeIDs(ids: IntVector): string
|
|
40
49
|
/**
|
|
41
|
-
* Embind object lifetime
|
|
42
|
-
*
|
|
50
|
+
* Embind object lifetime.
|
|
51
|
+
*
|
|
52
|
+
* The processor owns wasm-heap memory, so call `delete()` when done.
|
|
53
|
+
* Long-lived singletons in practice never do.
|
|
43
54
|
*/
|
|
44
55
|
delete(): void
|
|
45
56
|
}
|
|
46
57
|
|
|
47
58
|
/**
|
|
48
|
-
* Embind-registered `std::vector<int
|
|
59
|
+
* Embind-registered `std::vector<int>`.
|
|
60
|
+
*
|
|
61
|
+
* Build with `module.IntVector` and add ids with `push_back`.
|
|
62
|
+
* Call `delete()` after use.
|
|
49
63
|
*/
|
|
50
64
|
export declare class IntVector {
|
|
51
65
|
constructor()
|
|
@@ -61,7 +75,8 @@ export interface SentencePieceModule {
|
|
|
61
75
|
}
|
|
62
76
|
|
|
63
77
|
/**
|
|
64
|
-
* The emscripten
|
|
78
|
+
* The emscripten modularize factory.
|
|
79
|
+
* It resolves once the embedded wasm is instantiated.
|
|
65
80
|
*/
|
|
66
81
|
declare function createSentencePiece(): Promise<SentencePieceModule>
|
|
67
82
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mailwoman/sentencepiece-wasm",
|
|
3
|
-
"version": "10.
|
|
3
|
+
"version": "10.1.0",
|
|
4
4
|
"description": "google/sentencepiece v0.2.2 compiled to WASM with a native-offsets binding — the tokenizer core for @mailwoman/neural. Single-file ESM (wasm embedded), no runtime deps; per-piece byte offsets come from SentencePiece itself instead of TS-side reconstruction.",
|
|
5
5
|
"license": "AGPL-3.0-only OR LicenseRef-Commercial",
|
|
6
6
|
"repository": {
|
|
@@ -16,13 +16,6 @@
|
|
|
16
16
|
"!test/**"
|
|
17
17
|
],
|
|
18
18
|
"type": "module",
|
|
19
|
-
"imports": {
|
|
20
|
-
"#*": {
|
|
21
|
-
"types": "./out/*.d.ts",
|
|
22
|
-
"node": "./out/*.js",
|
|
23
|
-
"default": "./out/*.js"
|
|
24
|
-
}
|
|
25
|
-
},
|
|
26
19
|
"exports": {
|
|
27
20
|
"./package.json": "./package.json",
|
|
28
21
|
".": {
|
|
@@ -37,13 +30,6 @@
|
|
|
37
30
|
"types": "./index.d.ts",
|
|
38
31
|
"default": "./sentencepiece.mjs"
|
|
39
32
|
}
|
|
40
|
-
},
|
|
41
|
-
"imports": {
|
|
42
|
-
"#*": {
|
|
43
|
-
"types": "./out/*.d.ts",
|
|
44
|
-
"node": "./out/*.js",
|
|
45
|
-
"default": "./out/*.js"
|
|
46
|
-
}
|
|
47
33
|
}
|
|
48
34
|
}
|
|
49
35
|
}
|