webslm 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +373 -2
- package/dist/ai-sdk/index.d.ts +81 -0
- package/dist/ai-sdk/index.js +90 -0
- package/dist/ai-sdk/language-model.d.ts +77 -0
- package/dist/ai-sdk/language-model.js +422 -0
- package/dist/ai-sdk/loader.d.ts +54 -0
- package/dist/ai-sdk/loader.js +170 -0
- package/dist/ai-sdk/output.d.ts +43 -0
- package/dist/ai-sdk/output.js +152 -0
- package/dist/ai-sdk/prompt.d.ts +21 -0
- package/dist/ai-sdk/prompt.js +111 -0
- package/dist/async-queue.d.ts +22 -0
- package/dist/async-queue.js +70 -0
- package/dist/cache/cache-api.d.ts +29 -0
- package/dist/cache/cache-api.js +115 -0
- package/dist/cache/index.d.ts +21 -0
- package/dist/cache/index.js +42 -0
- package/dist/cache/memory.d.ts +12 -0
- package/dist/cache/memory.js +61 -0
- package/dist/cache/models.d.ts +67 -0
- package/dist/cache/models.js +107 -0
- package/dist/cache/node-fs.d.ts +21 -0
- package/dist/cache/node-fs.js +147 -0
- package/dist/cache/opfs.d.ts +69 -0
- package/dist/cache/opfs.js +181 -0
- package/dist/cache/store.d.ts +53 -0
- package/dist/cache/store.js +64 -0
- package/dist/cache-management.d.ts +23 -0
- package/dist/cache-management.js +58 -0
- package/dist/capabilities.d.ts +67 -0
- package/dist/capabilities.js +55 -0
- package/dist/client.d.ts +61 -0
- package/dist/client.js +256 -0
- package/dist/curated.d.ts +22 -0
- package/dist/curated.js +53 -0
- package/dist/download.d.ts +51 -0
- package/dist/download.js +289 -0
- package/dist/engine.d.ts +54 -0
- package/dist/engine.js +510 -0
- package/dist/errors.d.ts +81 -0
- package/dist/errors.js +89 -0
- package/dist/hub.d.ts +185 -0
- package/dist/hub.js +409 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +41 -0
- package/dist/models.d.ts +225 -0
- package/dist/models.js +730 -0
- package/dist/node-pool-worker.d.ts +6 -0
- package/dist/node-pool-worker.js +32 -0
- package/dist/node-web-worker.d.ts +23 -0
- package/dist/node-web-worker.js +84 -0
- package/dist/node-worker.d.ts +2 -0
- package/dist/node-worker.js +9 -0
- package/dist/pool-worker.d.ts +36 -0
- package/dist/pool-worker.js +30 -0
- package/dist/pool.d.ts +17 -0
- package/dist/pool.js +41 -0
- package/dist/protocol.d.ts +246 -0
- package/dist/protocol.js +19 -0
- package/dist/runtime.d.ts +21 -0
- package/dist/runtime.js +53 -0
- package/dist/safetensors.d.ts +20 -0
- package/dist/safetensors.js +2 -0
- package/dist/simple.d.ts +310 -0
- package/dist/simple.js +553 -0
- package/dist/types.d.ts +412 -0
- package/dist/types.js +2 -0
- package/dist/wasm-flavour.d.ts +85 -0
- package/dist/wasm-flavour.js +169 -0
- package/dist/wasm.d.ts +34 -0
- package/dist/wasm.js +38 -0
- package/dist/worker.d.ts +6 -0
- package/dist/worker.js +11 -0
- package/package.json +92 -3
- package/wasm/relaxed/webslm_wasm.d.ts +325 -0
- package/wasm/relaxed/webslm_wasm.js +1899 -0
- package/wasm/relaxed/webslm_wasm_bg.wasm +0 -0
- package/wasm/relaxed/webslm_wasm_bg.wasm.d.ts +56 -0
- package/wasm/simd/webslm_wasm.d.ts +325 -0
- package/wasm/simd/webslm_wasm.js +1899 -0
- package/wasm/simd/webslm_wasm_bg.wasm +0 -0
- package/wasm/simd/webslm_wasm_bg.wasm.d.ts +56 -0
- package/wasm/threads/snippets/wasm-bindgen-rayon-38edf6e439f6d70d/src/workerHelpers.js +108 -0
- package/wasm/threads/webslm_wasm.d.ts +357 -0
- package/wasm/threads/webslm_wasm.js +2100 -0
- package/wasm/threads/webslm_wasm_bg.wasm +0 -0
- package/wasm/threads/webslm_wasm_bg.wasm.d.ts +66 -0
|
Binary file
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
export const memory: WebAssembly.Memory;
|
|
4
|
+
export const __wbg_jssource_free: (a: number, b: number) => void;
|
|
5
|
+
export const __wbg_sha256hasher_free: (a: number, b: number) => void;
|
|
6
|
+
export const __wbg_wasmmodel_free: (a: number, b: number) => void;
|
|
7
|
+
export const __wbg_wasmsession_free: (a: number, b: number) => void;
|
|
8
|
+
export const buildInfo: () => [number, number, number];
|
|
9
|
+
export const jssource_new: (a: any, b: number) => number;
|
|
10
|
+
export const parseSafetensorsHeader: (a: number, b: number) => [number, number, number];
|
|
11
|
+
export const sha256hasher_finish: (a: number) => [number, number];
|
|
12
|
+
export const sha256hasher_new: () => number;
|
|
13
|
+
export const sha256hasher_update: (a: number, b: number, c: number) => void;
|
|
14
|
+
export const supportedModelTypes: () => [number, number];
|
|
15
|
+
export const version: () => [number, number];
|
|
16
|
+
export const wasmmodel_applyAdapter: (a: number, b: number, c: number, d: number) => any;
|
|
17
|
+
export const wasmmodel_chat: (a: number, b: number, c: number, d: number, e: number, f: any, g: number) => any;
|
|
18
|
+
export const wasmmodel_createSession: (a: number) => [number, number, number];
|
|
19
|
+
export const wasmmodel_detokenize: (a: number, b: number, c: number, d: number) => [number, number, number, number];
|
|
20
|
+
export const wasmmodel_generate: (a: number, b: number, c: number, d: number, e: number, f: any, g: number) => any;
|
|
21
|
+
export const wasmmodel_info: (a: number) => [number, number, number];
|
|
22
|
+
export const wasmmodel_load: (a: number, b: number, c: number, d: number) => any;
|
|
23
|
+
export const wasmmodel_removeAdapter: (a: number) => any;
|
|
24
|
+
export const wasmmodel_renderChat: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number];
|
|
25
|
+
export const wasmmodel_reset: (a: number) => [number, number];
|
|
26
|
+
export const wasmmodel_tokenize: (a: number, b: number, c: number, d: number) => [number, number, number, number];
|
|
27
|
+
export const wasmsession_capacity: (a: number) => [number, number, number];
|
|
28
|
+
export const wasmsession_chat: (a: number, b: number, c: number, d: number, e: number, f: any, g: number) => any;
|
|
29
|
+
export const wasmsession_computed: (a: number) => [number, number, number];
|
|
30
|
+
export const wasmsession_fork: (a: number) => [number, number, number];
|
|
31
|
+
export const wasmsession_generate: (a: number, b: number, c: number, d: number, e: number, f: any, g: number) => any;
|
|
32
|
+
export const wasmsession_length: (a: number) => number;
|
|
33
|
+
export const wasmsession_reset: (a: number) => [number, number];
|
|
34
|
+
export const wasmsession_score: (a: number, b: number, c: number, d: number, e: number) => any;
|
|
35
|
+
export const wasmsession_scoreContinuations: (a: number, b: number, c: number, d: number, e: number) => any;
|
|
36
|
+
export const wasmsession_shiftContext: (a: number, b: number, c: number) => any;
|
|
37
|
+
export const wasmsession_step: (a: number, b: number, c: number, d: number) => any;
|
|
38
|
+
export const wasmsession_tokens: (a: number) => [number, number, number, number];
|
|
39
|
+
export const wasmsession_truncate: (a: number, b: number) => [number, number];
|
|
40
|
+
export const wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___js_sys_56365fc49d784db6___Function_fn_wasm_bindgen_8cf186fa7724d16d___JsValue_____wasm_bindgen_8cf186fa7724d16d___sys__Undefined___js_sys_56365fc49d784db6___Function_fn_wasm_bindgen_8cf186fa7724d16d___JsValue_____wasm_bindgen_8cf186fa7724d16d___sys__Undefined_______true_: (a: number, b: number, c: any, d: any) => void;
|
|
41
|
+
export const wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___wasm_bindgen_8cf186fa7724d16d___JsValue__core_608f92abc48d28da___result__Result_____wasm_bindgen_8cf186fa7724d16d___JsError___true_: (a: number, b: number, c: any) => [number, number];
|
|
42
|
+
export const wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___wasm_bindgen_8cf186fa7724d16d___sys__JsNullable_wgpu_54917290532eaf6b___backend__webgpu__webgpu_sys__gen_GpuError__GpuError___core_608f92abc48d28da___result__Result_____wasm_bindgen_8cf186fa7724d16d___JsError___true_: (a: number, b: number, c: any) => [number, number];
|
|
43
|
+
export const wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___wasm_bindgen_8cf186fa7724d16d___sys__JsNullable_wgpu_54917290532eaf6b___backend__webgpu__webgpu_sys__gen_GpuError__GpuError___core_608f92abc48d28da___result__Result_____wasm_bindgen_8cf186fa7724d16d___JsError___true__37: (a: number, b: number, c: any) => [number, number];
|
|
44
|
+
export const wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___wasm_bindgen_8cf186fa7724d16d___sys__JsNullable_wgpu_54917290532eaf6b___backend__webgpu__webgpu_sys__gen_GpuError__GpuError___core_608f92abc48d28da___result__Result_____wasm_bindgen_8cf186fa7724d16d___JsError___true__38: (a: number, b: number, c: any) => [number, number];
|
|
45
|
+
export const wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___wgpu_54917290532eaf6b___backend__webgpu__webgpu_sys__gen_GpuDeviceLostInfo__GpuDeviceLostInfo______true_: (a: number, b: number, c: any) => void;
|
|
46
|
+
export const wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___wgpu_54917290532eaf6b___backend__webgpu__webgpu_sys__gen_GpuDeviceLostInfo__GpuDeviceLostInfo______true__36: (a: number, b: number, c: any) => void;
|
|
47
|
+
export const __wbindgen_malloc: (a: number, b: number) => number;
|
|
48
|
+
export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
49
|
+
export const __wbindgen_exn_store: (a: number) => void;
|
|
50
|
+
export const __externref_table_alloc: () => number;
|
|
51
|
+
export const __wbindgen_externrefs: WebAssembly.Table;
|
|
52
|
+
export const __wbindgen_destroy_closure: (a: number, b: number) => void;
|
|
53
|
+
export const __externref_table_dealloc: (a: number) => void;
|
|
54
|
+
export const __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
55
|
+
export const __externref_drop_slice: (a: number, b: number) => void;
|
|
56
|
+
export const __wbindgen_start: () => void;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2022 Google Inc. All Rights Reserved.
|
|
3
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License.
|
|
5
|
+
* You may obtain a copy of the License at
|
|
6
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
8
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
9
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
10
|
+
* See the License for the specific language governing permissions and
|
|
11
|
+
* limitations under the License.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
// Note: we use `wasm_bindgen_worker_`-prefixed message types to make sure
|
|
15
|
+
// we can handle bundling into other files, which might happen to have their
|
|
16
|
+
// own `postMessage`/`onmessage` communication channels.
|
|
17
|
+
//
|
|
18
|
+
// If we didn't take that into the account, we could send much simpler signals
|
|
19
|
+
// like just `0` or whatever, but the code would be less resilient.
|
|
20
|
+
|
|
21
|
+
function waitForMsgType(target, type) {
|
|
22
|
+
return new Promise(resolve => {
|
|
23
|
+
target.addEventListener('message', function onMsg({ data }) {
|
|
24
|
+
if (data?.type !== type) return;
|
|
25
|
+
target.removeEventListener('message', onMsg);
|
|
26
|
+
resolve(data);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
waitForMsgType(self, 'wasm_bindgen_worker_init').then(async ({ init, receiver }) => {
|
|
32
|
+
// # Note 1
|
|
33
|
+
// Our JS should have been generated in
|
|
34
|
+
// `[out-dir]/snippets/wasm-bindgen-rayon-[hash]/workerHelpers.js`,
|
|
35
|
+
// resolve the main module via `../../..`.
|
|
36
|
+
//
|
|
37
|
+
// This might need updating if the generated structure changes on wasm-bindgen
|
|
38
|
+
// side ever in the future, but works well with bundlers today. The whole
|
|
39
|
+
// point of this crate, after all, is to abstract away unstable features
|
|
40
|
+
// and temporary bugs so that you don't need to deal with them in your code.
|
|
41
|
+
//
|
|
42
|
+
// # Note 2
|
|
43
|
+
// This could be a regular import, but then some bundlers complain about
|
|
44
|
+
// circular deps.
|
|
45
|
+
//
|
|
46
|
+
// Dynamic import could be cheap if this file was inlined into the parent,
|
|
47
|
+
// which would require us just using `../../..` in `new Worker` below,
|
|
48
|
+
// but that doesn't work because wasm-pack unconditionally adds
|
|
49
|
+
// "sideEffects":false (see below).
|
|
50
|
+
//
|
|
51
|
+
// OTOH, even though it can't be inlined, it should be still reasonably
|
|
52
|
+
// cheap since the requested file is already in cache (it was loaded by
|
|
53
|
+
// the main thread).
|
|
54
|
+
const pkg = await import('../../../webslm_wasm.js');
|
|
55
|
+
await pkg.default(init);
|
|
56
|
+
postMessage({ type: 'wasm_bindgen_worker_ready' });
|
|
57
|
+
pkg.wbg_rayon_start_worker(receiver);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// Note: this is never used, but necessary to prevent a bug in Firefox
|
|
61
|
+
// (https://bugzilla.mozilla.org/show_bug.cgi?id=1702191) where it collects
|
|
62
|
+
// Web Workers that have a shared WebAssembly memory with the main thread,
|
|
63
|
+
// but are not explicitly rooted via a `Worker` instance.
|
|
64
|
+
//
|
|
65
|
+
// By storing them in a variable, we can keep `Worker` objects around and
|
|
66
|
+
// prevent them from getting GC-d.
|
|
67
|
+
let _workers;
|
|
68
|
+
|
|
69
|
+
export async function startWorkers(module, memory, builder) {
|
|
70
|
+
if (builder.numThreads() === 0) {
|
|
71
|
+
throw new Error(`num_threads must be > 0.`);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const workerInit = {
|
|
75
|
+
type: 'wasm_bindgen_worker_init',
|
|
76
|
+
init: { module_or_path: module, memory },
|
|
77
|
+
receiver: builder.receiver()
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
_workers = await Promise.all(
|
|
81
|
+
Array.from({ length: builder.numThreads() }, async () => {
|
|
82
|
+
// Self-spawn into a new Worker.
|
|
83
|
+
//
|
|
84
|
+
// TODO: while `new URL('...', import.meta.url) becomes a semi-standard
|
|
85
|
+
// way to get asset URLs relative to the module across various bundlers
|
|
86
|
+
// and browser, ideally we should switch to `import.meta.resolve`
|
|
87
|
+
// once it becomes a standard.
|
|
88
|
+
//
|
|
89
|
+
// Note: we could use `../../..` as the URL here to inline workerHelpers.js
|
|
90
|
+
// into the parent entry instead of creating another split point -
|
|
91
|
+
// this would be preferable from optimization perspective -
|
|
92
|
+
// however, Webpack then eliminates all message handler code
|
|
93
|
+
// because wasm-pack produces "sideEffects":false in package.json
|
|
94
|
+
// unconditionally.
|
|
95
|
+
//
|
|
96
|
+
// The only way to work around that is to have side effect code
|
|
97
|
+
// in an entry point such as Worker file itself.
|
|
98
|
+
// webslm: pool workers run webslm's pool-worker.js (see pool.ts in the
|
|
99
|
+
// webslm package): a worker script starting copies of itself cannot be
|
|
100
|
+
// built by every bundler (Turbopack hangs on it).
|
|
101
|
+
const worker = globalThis.__webslmStartPoolWorker();
|
|
102
|
+
worker.postMessage(workerInit);
|
|
103
|
+
await waitForMsgType(worker, 'wasm_bindgen_worker_ready');
|
|
104
|
+
return worker;
|
|
105
|
+
})
|
|
106
|
+
);
|
|
107
|
+
builder.build();
|
|
108
|
+
}
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Checkpoint files served by JavaScript.
|
|
6
|
+
*
|
|
7
|
+
* `read(path, start?, end?)` returns (a promise of) a `Uint8Array` or an
|
|
8
|
+
* `ArrayBuffer` holding the whole file, or bytes `start..end` (end
|
|
9
|
+
* exclusive) when a range is given. A missing file is reported by
|
|
10
|
+
* resolving to `null`/`undefined` or by rejecting with an error whose
|
|
11
|
+
* `code` is `"NOT_FOUND"` (or whose `name` is `"NotFoundError"`, as OPFS
|
|
12
|
+
* throws). The optional `list()` returns the file names of the checkpoint.
|
|
13
|
+
*/
|
|
14
|
+
export class JsSource {
|
|
15
|
+
free(): void;
|
|
16
|
+
[Symbol.dispose](): void;
|
|
17
|
+
constructor(read: Function, list?: Function | null);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Incremental SHA-256.
|
|
22
|
+
*/
|
|
23
|
+
export class Sha256Hasher {
|
|
24
|
+
free(): void;
|
|
25
|
+
[Symbol.dispose](): void;
|
|
26
|
+
/**
|
|
27
|
+
* Lowercase hex digest of everything fed so far; resets the hasher.
|
|
28
|
+
*/
|
|
29
|
+
finish(): string;
|
|
30
|
+
constructor();
|
|
31
|
+
/**
|
|
32
|
+
* Feeds bytes.
|
|
33
|
+
*/
|
|
34
|
+
update(bytes: Uint8Array): void;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A loaded checkpoint, with a default session used by `chat` and
|
|
39
|
+
* `generate`.
|
|
40
|
+
*/
|
|
41
|
+
export class WasmModel {
|
|
42
|
+
private constructor();
|
|
43
|
+
free(): void;
|
|
44
|
+
[Symbol.dispose](): void;
|
|
45
|
+
/**
|
|
46
|
+
* Merges a PEFT LoRA adapter served by `lora` into the weights,
|
|
47
|
+
* replacing the adapter applied before (if any). `options_json`:
|
|
48
|
+
* `{ scale?: number }` (default 1). Resolves to the adapter's
|
|
49
|
+
* description (see [`LoraInfo`]); rejects with `INVALID_ADAPTER`,
|
|
50
|
+
* leaving the model unchanged, when the adapter does not fit the
|
|
51
|
+
* model. Sessions start over at their next call.
|
|
52
|
+
*/
|
|
53
|
+
applyAdapter(lora: JsSource, options_json: string): Promise<any>;
|
|
54
|
+
/**
|
|
55
|
+
* Answers a conversation in the default session; see
|
|
56
|
+
* [`WasmSession::chat`].
|
|
57
|
+
*/
|
|
58
|
+
chat(messages_json: string, options_json: string, on_delta: Function, on_prefill?: Function | null): Promise<any>;
|
|
59
|
+
/**
|
|
60
|
+
* Starts a new, empty session over the same weights.
|
|
61
|
+
*/
|
|
62
|
+
createSession(): WasmSession;
|
|
63
|
+
/**
|
|
64
|
+
* Decodes token ids into text.
|
|
65
|
+
*/
|
|
66
|
+
detokenize(ids: Uint32Array, skip_special_tokens: boolean): string;
|
|
67
|
+
/**
|
|
68
|
+
* Completes raw text in the default session; see
|
|
69
|
+
* [`WasmSession::generate`].
|
|
70
|
+
*/
|
|
71
|
+
generate(prompt: string, options_json: string, on_delta: Function, on_prefill?: Function | null): Promise<any>;
|
|
72
|
+
/**
|
|
73
|
+
* Describes the model; see [`ModelInfo`].
|
|
74
|
+
*/
|
|
75
|
+
info(): any;
|
|
76
|
+
/**
|
|
77
|
+
* Loads a checkpoint from `source`. `options_json` is a JSON object:
|
|
78
|
+
* `{ format?: "q8" | "q4" | "q4_0" | "f32" | "f16" | "bf16",
|
|
79
|
+
* contextLength?: number, deterministic?: boolean,
|
|
80
|
+
* activations?: "quantized" | "f32", device?: "cpu" | "webgpu" | "auto" }`.
|
|
81
|
+
*
|
|
82
|
+
* `device` defaults to `"cpu"`. `"auto"` picks WebGPU when an adapter
|
|
83
|
+
* is available and the model has more than
|
|
84
|
+
* [`AUTO_GPU_MIN_PARAMETERS`] parameters. When WebGPU is wanted but
|
|
85
|
+
* the device cannot be created, the model loads on the CPU and
|
|
86
|
+
* `info().deviceNote` says why. `deterministic` runs on the CPU: GPU
|
|
87
|
+
* results depend on the adapter.
|
|
88
|
+
*
|
|
89
|
+
* `lora`, when given, serves a PEFT LoRA adapter (`adapter_config.json`
|
|
90
|
+
* and `adapter_model.safetensors`) merged into the weights as they
|
|
91
|
+
* load, at `adapterScale` (default 1).
|
|
92
|
+
*/
|
|
93
|
+
static load(source: JsSource, options_json: string, lora?: JsSource | null): Promise<WasmModel>;
|
|
94
|
+
/**
|
|
95
|
+
* Removes the adapter, restoring the base weights exactly. Resolves to
|
|
96
|
+
* the description of the adapter removed, or `null` when there was
|
|
97
|
+
* none.
|
|
98
|
+
*/
|
|
99
|
+
removeAdapter(): Promise<any>;
|
|
100
|
+
/**
|
|
101
|
+
* Renders a conversation (JSON array of `{ role, content }`) with the
|
|
102
|
+
* chat template, ending with the header of a new assistant turn.
|
|
103
|
+
* `kwargs_json` holds extra template variables, e.g.
|
|
104
|
+
* `{"enable_thinking": false}`.
|
|
105
|
+
*/
|
|
106
|
+
renderChat(messages_json: string, kwargs_json: string): string;
|
|
107
|
+
/**
|
|
108
|
+
* Clears the default session.
|
|
109
|
+
*/
|
|
110
|
+
reset(): void;
|
|
111
|
+
/**
|
|
112
|
+
* Encodes text into token ids.
|
|
113
|
+
*/
|
|
114
|
+
tokenize(text: string, add_special_tokens: boolean): Uint32Array;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* An independent conversation (KV cache) over a model.
|
|
119
|
+
*/
|
|
120
|
+
export class WasmSession {
|
|
121
|
+
private constructor();
|
|
122
|
+
free(): void;
|
|
123
|
+
[Symbol.dispose](): void;
|
|
124
|
+
/**
|
|
125
|
+
* Answers a conversation: `messages_json` is a JSON array of
|
|
126
|
+
* `{ role, content }`, `options_json` a JSON object of generation
|
|
127
|
+
* options (`maxTokens`, `temperature`, `topK`, `topP`, `minP`,
|
|
128
|
+
* `repetitionPenalty`, `seed`, `stop`, `templateKwargs`,
|
|
129
|
+
* `ignoreStopTokens`). Turns already in the cache are not recomputed.
|
|
130
|
+
*
|
|
131
|
+
* Generation controls: `presencePenalty`, `frequencyPenalty`,
|
|
132
|
+
* `stopTokenIds`, `logprobs` (number of alternatives), `json`, `schema`,
|
|
133
|
+
* `thinkingBudget`, `loopDetection`, `contextShift`.
|
|
134
|
+
*
|
|
135
|
+
* `on_delta(text, tokenId, info)` runs after every token; `info` is
|
|
136
|
+
* `undefined` or `{ logprob, topLogprobs: [id, logprob][], forced }`.
|
|
137
|
+
* Returning `false` (or a promise resolving to `false`) stops the
|
|
138
|
+
* generation before the next token. `on_prefill(done, total)`, when
|
|
139
|
+
* given, runs after every piece of the prompt (128 tokens) and may stop
|
|
140
|
+
* it the same way. Resolves to `{ text, tokens, finishReason,
|
|
141
|
+
* promptTokens, reusedTokens, generatedTokens, droppedTokens, length }`;
|
|
142
|
+
* `finishReason` is `"stop"`, `"stop_string"`, `"length"`,
|
|
143
|
+
* `"context_full"`, `"loop"` or `"aborted"`.
|
|
144
|
+
*/
|
|
145
|
+
chat(messages_json: string, options_json: string, on_delta: Function, on_prefill?: Function | null): Promise<any>;
|
|
146
|
+
/**
|
|
147
|
+
* A new session holding a copy of this one's cache, to continue it in
|
|
148
|
+
* several directions without recomputing it. Fails on backends that
|
|
149
|
+
* cannot copy caches.
|
|
150
|
+
*/
|
|
151
|
+
fork(): WasmSession;
|
|
152
|
+
/**
|
|
153
|
+
* Completes raw text, without the chat template (`addSpecialTokens`
|
|
154
|
+
* in the options, default `true`, adds the tokenizer's BOS). Same
|
|
155
|
+
* callback and result as [`WasmSession::chat`].
|
|
156
|
+
*/
|
|
157
|
+
generate(prompt: string, options_json: string, on_delta: Function, on_prefill?: Function | null): Promise<any>;
|
|
158
|
+
/**
|
|
159
|
+
* Empties the cache.
|
|
160
|
+
*/
|
|
161
|
+
reset(): void;
|
|
162
|
+
/**
|
|
163
|
+
* Log-probabilities of several continuations of one prefix, the prefix
|
|
164
|
+
* computed once. `continuations_json` is a JSON array of token-id
|
|
165
|
+
* arrays. Resolves to an array of `Float32Array`, one log-probability
|
|
166
|
+
* per continuation token; the session then holds the prefix.
|
|
167
|
+
*/
|
|
168
|
+
scoreContinuations(prefix: Uint32Array, continuations_json: string): Promise<any>;
|
|
169
|
+
/**
|
|
170
|
+
* Teacher-forced pass: appends `ids` to the cache. `options_json`:
|
|
171
|
+
* `{ logits?: "all" | "last" | "none", hiddenStates?: number[] }`
|
|
172
|
+
* (default `"last"`). Hidden state `i` is the residual stream after
|
|
173
|
+
* layer `i` counted from 1, `0` the embedding output, all before the
|
|
174
|
+
* final norm (the `output_hidden_states` indexing of `transformers`).
|
|
175
|
+
*
|
|
176
|
+
* Resolves to `{ logits?: Float32Array, hidden: { [i]: Float32Array },
|
|
177
|
+
* positions, vocabSize, hiddenSize, length }`; arrays are row-major,
|
|
178
|
+
* one row per position.
|
|
179
|
+
*/
|
|
180
|
+
score(ids: Uint32Array, options_json: string): Promise<any>;
|
|
181
|
+
/**
|
|
182
|
+
* Context shift: drops `drop` cached positions after the first `keep`
|
|
183
|
+
* and recomputes the rest. Resolves to the new length.
|
|
184
|
+
*/
|
|
185
|
+
shiftContext(keep: number, drop: number): Promise<any>;
|
|
186
|
+
/**
|
|
187
|
+
* One decode step with a token chosen by the caller. `options_json`:
|
|
188
|
+
* `{ logits?: boolean, hiddenStates?: number[] }` (logits by default).
|
|
189
|
+
*/
|
|
190
|
+
step(id: number, options_json: string): Promise<any>;
|
|
191
|
+
/**
|
|
192
|
+
* The token ids in the cache.
|
|
193
|
+
*/
|
|
194
|
+
tokens(): Uint32Array;
|
|
195
|
+
/**
|
|
196
|
+
* Keeps the first `len` positions. Models with recurrent layers (LFM2
|
|
197
|
+
* and LFM3 convolutions, Qwen3.5 Gated DeltaNet, sliding-window ring
|
|
198
|
+
* buffers) recompute their state for those positions (a prefill of
|
|
199
|
+
* `len` tokens).
|
|
200
|
+
*/
|
|
201
|
+
truncate(len: number): void;
|
|
202
|
+
/**
|
|
203
|
+
* Largest number of positions the cache holds.
|
|
204
|
+
*/
|
|
205
|
+
readonly capacity: number;
|
|
206
|
+
/**
|
|
207
|
+
* Positions run through the network since the session was created
|
|
208
|
+
* (what prefix reuse and forking save).
|
|
209
|
+
*/
|
|
210
|
+
readonly computed: number;
|
|
211
|
+
/**
|
|
212
|
+
* Number of positions in the cache.
|
|
213
|
+
*/
|
|
214
|
+
readonly length: number;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* How this module was built: `{ flavour, kernels, threads }`. `flavour`
|
|
219
|
+
* is `"simd"`, `"relaxed"` (SIMD128 and relaxed SIMD) or `"threads"`
|
|
220
|
+
* (relaxed SIMD with atomics and a Web Worker pool); `kernels` the CPU
|
|
221
|
+
* kernel set; `threads` the size of the pool (1 until `startThreadPool`).
|
|
222
|
+
*/
|
|
223
|
+
export function buildInfo(): any;
|
|
224
|
+
|
|
225
|
+
export function initThreadPool(num_threads: number): Promise<any>;
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Parses the header of a safetensors file.
|
|
229
|
+
*
|
|
230
|
+
* `bytes` must hold at least the 8-byte length prefix and the whole JSON
|
|
231
|
+
* header. Returns a plain object
|
|
232
|
+
* `{ headerLen, dataStart, tensors: { [name]: { name, dtype, shape, dataOffsets } } }`
|
|
233
|
+
* and throws an `Error` if the header is malformed.
|
|
234
|
+
*/
|
|
235
|
+
export function parseSafetensorsHeader(bytes: Uint8Array): any;
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Starts a pool of `threads` Web Workers sharing this module's memory and
|
|
239
|
+
* makes the CPU kernels split their work across it (the `threads` build
|
|
240
|
+
* only). Must run in a worker (or Node), before any model runs.
|
|
241
|
+
*/
|
|
242
|
+
export function startThreadPool(threads: number): Promise<void>;
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* `model_type` values (from `config.json`) of the architectures this build
|
|
246
|
+
* can run.
|
|
247
|
+
*/
|
|
248
|
+
export function supportedModelTypes(): string[];
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Version of the Rust engine, as declared in `Cargo.toml`.
|
|
252
|
+
*/
|
|
253
|
+
export function version(): string;
|
|
254
|
+
|
|
255
|
+
export class wbg_rayon_PoolBuilder {
|
|
256
|
+
private constructor();
|
|
257
|
+
free(): void;
|
|
258
|
+
[Symbol.dispose](): void;
|
|
259
|
+
build(): void;
|
|
260
|
+
numThreads(): number;
|
|
261
|
+
receiver(): number;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export function wbg_rayon_start_worker(receiver: number): void;
|
|
265
|
+
|
|
266
|
+
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
|
|
267
|
+
|
|
268
|
+
export interface InitOutput {
|
|
269
|
+
readonly __wbg_jssource_free: (a: number, b: number) => void;
|
|
270
|
+
readonly __wbg_sha256hasher_free: (a: number, b: number) => void;
|
|
271
|
+
readonly __wbg_wasmmodel_free: (a: number, b: number) => void;
|
|
272
|
+
readonly __wbg_wasmsession_free: (a: number, b: number) => void;
|
|
273
|
+
readonly __wbg_wbg_rayon_poolbuilder_free: (a: number, b: number) => void;
|
|
274
|
+
readonly buildInfo: () => [number, number, number];
|
|
275
|
+
readonly initThreadPool: (a: number) => any;
|
|
276
|
+
readonly jssource_new: (a: any, b: number) => number;
|
|
277
|
+
readonly parseSafetensorsHeader: (a: number, b: number) => [number, number, number];
|
|
278
|
+
readonly sha256hasher_finish: (a: number) => [number, number];
|
|
279
|
+
readonly sha256hasher_new: () => number;
|
|
280
|
+
readonly sha256hasher_update: (a: number, b: number, c: number) => void;
|
|
281
|
+
readonly startThreadPool: (a: number) => any;
|
|
282
|
+
readonly supportedModelTypes: () => [number, number];
|
|
283
|
+
readonly version: () => [number, number];
|
|
284
|
+
readonly wasmmodel_applyAdapter: (a: number, b: number, c: number, d: number) => any;
|
|
285
|
+
readonly wasmmodel_chat: (a: number, b: number, c: number, d: number, e: number, f: any, g: number) => any;
|
|
286
|
+
readonly wasmmodel_createSession: (a: number) => [number, number, number];
|
|
287
|
+
readonly wasmmodel_detokenize: (a: number, b: number, c: number, d: number) => [number, number, number, number];
|
|
288
|
+
readonly wasmmodel_generate: (a: number, b: number, c: number, d: number, e: number, f: any, g: number) => any;
|
|
289
|
+
readonly wasmmodel_info: (a: number) => [number, number, number];
|
|
290
|
+
readonly wasmmodel_load: (a: number, b: number, c: number, d: number) => any;
|
|
291
|
+
readonly wasmmodel_removeAdapter: (a: number) => any;
|
|
292
|
+
readonly wasmmodel_renderChat: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number];
|
|
293
|
+
readonly wasmmodel_reset: (a: number) => [number, number];
|
|
294
|
+
readonly wasmmodel_tokenize: (a: number, b: number, c: number, d: number) => [number, number, number, number];
|
|
295
|
+
readonly wasmsession_capacity: (a: number) => [number, number, number];
|
|
296
|
+
readonly wasmsession_chat: (a: number, b: number, c: number, d: number, e: number, f: any, g: number) => any;
|
|
297
|
+
readonly wasmsession_computed: (a: number) => [number, number, number];
|
|
298
|
+
readonly wasmsession_fork: (a: number) => [number, number, number];
|
|
299
|
+
readonly wasmsession_generate: (a: number, b: number, c: number, d: number, e: number, f: any, g: number) => any;
|
|
300
|
+
readonly wasmsession_length: (a: number) => number;
|
|
301
|
+
readonly wasmsession_reset: (a: number) => [number, number];
|
|
302
|
+
readonly wasmsession_score: (a: number, b: number, c: number, d: number, e: number) => any;
|
|
303
|
+
readonly wasmsession_scoreContinuations: (a: number, b: number, c: number, d: number, e: number) => any;
|
|
304
|
+
readonly wasmsession_shiftContext: (a: number, b: number, c: number) => any;
|
|
305
|
+
readonly wasmsession_step: (a: number, b: number, c: number, d: number) => any;
|
|
306
|
+
readonly wasmsession_tokens: (a: number) => [number, number, number, number];
|
|
307
|
+
readonly wasmsession_truncate: (a: number, b: number) => [number, number];
|
|
308
|
+
readonly wbg_rayon_poolbuilder_build: (a: number) => void;
|
|
309
|
+
readonly wbg_rayon_poolbuilder_numThreads: (a: number) => number;
|
|
310
|
+
readonly wbg_rayon_poolbuilder_receiver: (a: number) => number;
|
|
311
|
+
readonly wbg_rayon_start_worker: (a: number) => void;
|
|
312
|
+
readonly wasm_bindgen_3c612a990bfab728___convert__closures_____invoke___js_sys_b608be85a12bc1d0___Function_fn_wasm_bindgen_3c612a990bfab728___JsValue_____wasm_bindgen_3c612a990bfab728___sys__Undefined___js_sys_b608be85a12bc1d0___Function_fn_wasm_bindgen_3c612a990bfab728___JsValue_____wasm_bindgen_3c612a990bfab728___sys__Undefined_______true_: (a: number, b: number, c: any, d: any) => void;
|
|
313
|
+
readonly wasm_bindgen_3c612a990bfab728___convert__closures_____invoke___wasm_bindgen_3c612a990bfab728___JsValue__core_2b10d7c4e784534e___result__Result_____wasm_bindgen_3c612a990bfab728___JsError___true_: (a: number, b: number, c: any) => [number, number];
|
|
314
|
+
readonly wasm_bindgen_3c612a990bfab728___convert__closures_____invoke___wasm_bindgen_3c612a990bfab728___sys__JsNullable_wgpu_4ecf802b864e9046___backend__webgpu__webgpu_sys__gen_GpuError__GpuError___core_2b10d7c4e784534e___result__Result_____wasm_bindgen_3c612a990bfab728___JsError___true_: (a: number, b: number, c: any) => [number, number];
|
|
315
|
+
readonly wasm_bindgen_3c612a990bfab728___convert__closures_____invoke___wasm_bindgen_3c612a990bfab728___sys__JsNullable_wgpu_4ecf802b864e9046___backend__webgpu__webgpu_sys__gen_GpuError__GpuError___core_2b10d7c4e784534e___result__Result_____wasm_bindgen_3c612a990bfab728___JsError___true__45: (a: number, b: number, c: any) => [number, number];
|
|
316
|
+
readonly wasm_bindgen_3c612a990bfab728___convert__closures_____invoke___wasm_bindgen_3c612a990bfab728___sys__JsNullable_wgpu_4ecf802b864e9046___backend__webgpu__webgpu_sys__gen_GpuError__GpuError___core_2b10d7c4e784534e___result__Result_____wasm_bindgen_3c612a990bfab728___JsError___true__46: (a: number, b: number, c: any) => [number, number];
|
|
317
|
+
readonly wasm_bindgen_3c612a990bfab728___convert__closures_____invoke___js_sys_b608be85a12bc1d0___futures__task__wait_async_polyfill__MessageEvent______true_: (a: number, b: number, c: any) => void;
|
|
318
|
+
readonly wasm_bindgen_3c612a990bfab728___convert__closures_____invoke___wasm_bindgen_3c612a990bfab728___JsValue______true_: (a: number, b: number, c: any) => void;
|
|
319
|
+
readonly wasm_bindgen_3c612a990bfab728___convert__closures_____invoke___wasm_bindgen_3c612a990bfab728___JsValue______true__43: (a: number, b: number, c: any) => void;
|
|
320
|
+
readonly wasm_bindgen_3c612a990bfab728___convert__closures_____invoke___wasm_bindgen_3c612a990bfab728___JsValue______true__44: (a: number, b: number, c: any) => void;
|
|
321
|
+
readonly memory: WebAssembly.Memory;
|
|
322
|
+
readonly __wbindgen_malloc: (a: number, b: number) => number;
|
|
323
|
+
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
324
|
+
readonly __wbindgen_exn_store: (a: number) => void;
|
|
325
|
+
readonly __externref_table_alloc: () => number;
|
|
326
|
+
readonly __wbindgen_externrefs: WebAssembly.Table;
|
|
327
|
+
readonly __wbindgen_destroy_closure: (a: number, b: number) => void;
|
|
328
|
+
readonly __externref_table_dealloc: (a: number) => void;
|
|
329
|
+
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
330
|
+
readonly __externref_drop_slice: (a: number, b: number) => void;
|
|
331
|
+
readonly __wbindgen_thread_destroy: (a?: number, b?: number, c?: number) => void;
|
|
332
|
+
readonly __wbindgen_start: (a: number) => void;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export type SyncInitInput = BufferSource | WebAssembly.Module;
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Instantiates the given `module`, which can either be bytes or
|
|
339
|
+
* a precompiled `WebAssembly.Module`.
|
|
340
|
+
*
|
|
341
|
+
* @param {{ module: SyncInitInput, memory?: WebAssembly.Memory, thread_stack_size?: number }} module - Passing `SyncInitInput` directly is deprecated.
|
|
342
|
+
* @param {WebAssembly.Memory} memory - Deprecated.
|
|
343
|
+
*
|
|
344
|
+
* @returns {InitOutput}
|
|
345
|
+
*/
|
|
346
|
+
export function initSync(module: { module: SyncInitInput, memory?: WebAssembly.Memory, thread_stack_size?: number } | SyncInitInput, memory?: WebAssembly.Memory): InitOutput;
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
|
|
350
|
+
* for everything else, calls `WebAssembly.instantiate` directly.
|
|
351
|
+
*
|
|
352
|
+
* @param {{ module_or_path: InitInput | Promise<InitInput>, memory?: WebAssembly.Memory, thread_stack_size?: number }} module_or_path - Passing `InitInput` directly is deprecated.
|
|
353
|
+
* @param {WebAssembly.Memory} memory - Deprecated.
|
|
354
|
+
*
|
|
355
|
+
* @returns {Promise<InitOutput>}
|
|
356
|
+
*/
|
|
357
|
+
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput>, memory?: WebAssembly.Memory, thread_stack_size?: number } | InitInput | Promise<InitInput>, memory?: WebAssembly.Memory): Promise<InitOutput>;
|