webslm 0.4.0 → 0.4.1
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 +5 -3
- package/dist/cache/models.d.ts +26 -3
- package/dist/cache/models.js +95 -11
- package/dist/cache/node-fs.d.ts +26 -3
- package/dist/cache/node-fs.js +43 -9
- package/dist/cache-management.d.ts +1 -1
- package/dist/download.d.ts +4 -0
- package/dist/download.js +29 -4
- package/dist/models.d.ts +3 -2
- package/dist/models.js +3 -2
- package/dist/simple.d.ts +2 -2
- package/dist/simple.js +2 -2
- package/dist/types.d.ts +4 -2
- package/native-manifest.json +29 -29
- package/package.json +1 -1
- package/wasm/relaxed/webslm_wasm_bg.wasm +0 -0
- package/wasm/simd/webslm_wasm_bg.wasm +0 -0
- package/wasm/threads/webslm_wasm_bg.wasm +0 -0
package/README.md
CHANGED
|
@@ -363,7 +363,9 @@ The first load writes the file as it converts (0.1 to 1.8 s more, once). Each we
|
|
|
363
363
|
|
|
364
364
|
## Node, Bun and Deno
|
|
365
365
|
|
|
366
|
-
The same API works in Node 20+ and Bun, as a plain ESM script with no bundler, on the [native engine](#native-engine-in-node) when it is available, else on WebAssembly. The cache
|
|
366
|
+
The same API works in Node 20+ and Bun, as a plain ESM script with no bundler, on the [native engine](#native-engine-in-node) when it is available, else on WebAssembly. The cache is the [command-line tool's](https://github.com/maxgfr/webslm#cache-and-hugging-face-tokens): `WEBSLM_CACHE_DIR`, else `~/Library/Caches/webslm` on macOS, `%LOCALAPPDATA%\webslm\cache` on Windows, `$XDG_CACHE_HOME/webslm` or `~/.cache/webslm` on Linux (`cacheDir` in the options overrides it). It has the same layout, so a model pulled with `webslm pull` loads in Node without a download, and a model downloaded in Node runs with `webslm run`. `WEBSLM_OFFLINE=1` (or `HF_HUB_OFFLINE=1`) forbids network access: models already in the cache load, anything else rejects with `NETWORK`. `HF_ENDPOINT` replaces `https://huggingface.co` (a mirror, or a local stand-in in tests) unless a source gives its own `hubUrl`. The engine runs in a `worker_threads` worker, which does not keep the process alive between calls; pass `worker: false` to run it on the calling thread. Deno 2 runs Hub models as is; for a `files` source of a few hundred megabytes, pass `worker: false` (Deno's `worker_threads` cannot copy buffers that large).
|
|
367
|
+
|
|
368
|
+
Earlier versions kept the Node cache in `~/.cache/webslm` on macOS and in `%USERPROFILE%\.cache\webslm` on Windows (or `$XDG_CACHE_HOME/webslm` when set), apart from the command-line tool's. webslm does not touch that directory: models in it download again into the new one. To keep them, move the directory when the new one does not exist yet (`mv ~/.cache/webslm ~/Library/Caches/webslm` on macOS), or merge it into the new one and delete it (`rsync -a --ignore-existing ~/.cache/webslm/ ~/Library/Caches/webslm/ && rm -rf ~/.cache/webslm`); or just delete it (`rm -rf ~/.cache/webslm`). Its `native/` directory, the native engine, is downloaded again when needed. With `WEBSLM_CACHE_DIR` set nothing changes.
|
|
367
369
|
|
|
368
370
|
```ts
|
|
369
371
|
import { readFile } from "node:fs/promises";
|
|
@@ -388,7 +390,7 @@ Each release attaches to its [GitHub Release](https://github.com/maxgfr/webslm/r
|
|
|
388
390
|
- the native CPU kernels (NEON with dotprod and i8mm, AVX2, AVX-512 VNNI) on every core, capped by the container's CPU quota, instead of WebAssembly SIMD on at most 8 threads;
|
|
389
391
|
- the GPU through Metal (macOS), Vulkan or DX12 (Linux, Windows), with the device rules of the command-line tool: `device: "auto"` runs models above ~100M parameters on a hardware GPU that holds them (software rasterizers such as llvmpipe are skipped; the model is sized against the GPU's memory before anything is uploaded), the CPU otherwise; `device: "gpu"` fails with `UNSUPPORTED_DEVICE` when there is no GPU;
|
|
390
392
|
- `quant: "auto"` decided against the machine's memory and the GPU's (measured with [sift](https://github.com/maxgfr/sift)), not the 4 GiB of WebAssembly;
|
|
391
|
-
- the converted-weight cache in the same file as the command-line tool (`<repo>/<sha>/quantized/<format>/weights.webslmq`), so `webslm pull --quant q8` and the package share it
|
|
393
|
+
- the converted-weight cache in the same file as the command-line tool (`<repo>/<sha>/quantized/<format>/weights.webslmq`), in the same cache directory, so `webslm pull --quant q8` and the package share it.
|
|
392
394
|
|
|
393
395
|
The API is the same (`load`, `ask`, `stream`, `loadModel`, sessions with `score`, `step`, `fork`, LoRA adapters, the AI SDK provider), and so are the results on the CPU: the quantized kernels are bit-identical across builds, so `q8` gives the WebAssembly engine's greedy tokens, and `f32` the `transformers` goldens (both checked in CI).
|
|
394
396
|
|
|
@@ -426,7 +428,7 @@ export default defineConfig({
|
|
|
426
428
|
});
|
|
427
429
|
```
|
|
428
430
|
|
|
429
|
-
Server code that imports webslm must not be bundled: webslm runs from its own files in `node_modules` (Next.js: `serverExternalPackages: ["webslm"]`). Without a bundler, a page can import it from jsDelivr (`https://cdn.jsdelivr.net/npm/webslm@0.4.
|
|
431
|
+
Server code that imports webslm must not be bundled: webslm runs from its own files in `node_modules` (Next.js: `serverExternalPackages: ["webslm"]`). Without a bundler, a page can import it from jsDelivr (`https://cdn.jsdelivr.net/npm/webslm@0.4.1/dist/index.js`); webslm starts its workers through same-origin `blob:` workers when its files come from another origin. Guides for Next.js, Vite, webpack, Node servers, Bun, Deno and CDNs, each with a tested example: <https://github.com/maxgfr/webslm#frameworks-bundlers-and-runtimes>.
|
|
430
432
|
|
|
431
433
|
## Errors
|
|
432
434
|
|
package/dist/cache/models.d.ts
CHANGED
|
@@ -2,10 +2,18 @@
|
|
|
2
2
|
* Layout of cached checkpoints inside a {@link CacheStore}:
|
|
3
3
|
*
|
|
4
4
|
* ```text
|
|
5
|
-
* {repo}/refs/{revision}
|
|
6
|
-
* {repo}/{sha}/.webslm.json
|
|
7
|
-
* {repo}/{sha}
|
|
5
|
+
* {repo}/refs/{revision} commit sha a revision resolved to
|
|
6
|
+
* {repo}/{sha}/.webslm.json manifest: files, sizes, hashes, completeness
|
|
7
|
+
* {repo}/{sha}/.webslm-manifest.json the command-line tool's manifest (complete snapshots)
|
|
8
|
+
* {repo}/{sha}/{file} checkpoint files
|
|
9
|
+
* {repo}/{sha}/quantized/{format}/weights.webslmq converted weights
|
|
8
10
|
* ```
|
|
11
|
+
*
|
|
12
|
+
* In Node this is the command-line tool's cache layout
|
|
13
|
+
* (crates/webslm-hub/src/cache.rs) in the same directory, so a model pulled
|
|
14
|
+
* by `webslm pull` loads without a download and the reverse: each side reads
|
|
15
|
+
* the other's manifest, and a completed download in a directory store writes
|
|
16
|
+
* the command-line tool's manifest too.
|
|
9
17
|
*/
|
|
10
18
|
import { type CacheStore } from "./store.js";
|
|
11
19
|
/** Manifest of one cached revision. */
|
|
@@ -49,6 +57,8 @@ export interface CachedModel {
|
|
|
49
57
|
readonly storage: string;
|
|
50
58
|
}
|
|
51
59
|
export declare const MANIFEST_FILE = ".webslm.json";
|
|
60
|
+
/** The command-line tool's manifest of a complete snapshot (`MANIFEST_FILE` in crates/webslm-hub/src/cache.rs). */
|
|
61
|
+
export declare const CLI_MANIFEST_FILE = ".webslm-manifest.json";
|
|
52
62
|
/** Checks a repository id and returns it normalized. */
|
|
53
63
|
export declare function checkRepoId(repo: string): string;
|
|
54
64
|
export declare function refPath(repo: string, revision: string): string;
|
|
@@ -61,10 +71,23 @@ export declare function filePath(repo: string, sha: string, file: string): strin
|
|
|
61
71
|
*/
|
|
62
72
|
export declare function weightCachePath(repo: string, sha: string, quant: string): string;
|
|
63
73
|
export declare function manifestPath(repo: string, sha: string): string;
|
|
74
|
+
export declare function cliManifestPath(repo: string, sha: string): string;
|
|
64
75
|
export declare function readRef(store: CacheStore, repo: string, revision: string): Promise<string | undefined>;
|
|
65
76
|
export declare function writeRef(store: CacheStore, repo: string, revision: string, sha: string): Promise<void>;
|
|
66
77
|
export declare function readManifest(store: CacheStore, repo: string, sha: string): Promise<CacheManifest | undefined>;
|
|
67
78
|
export declare function writeManifest(store: CacheStore, manifest: CacheManifest): Promise<void>;
|
|
79
|
+
/**
|
|
80
|
+
* The snapshot `repo@sha` the command-line tool pulled, as a complete
|
|
81
|
+
* {@link CacheManifest}: `undefined` without its manifest or when a file of
|
|
82
|
+
* it is missing or has another size.
|
|
83
|
+
*/
|
|
84
|
+
export declare function readCliManifest(store: CacheStore, repo: string, sha: string): Promise<CacheManifest | undefined>;
|
|
85
|
+
/**
|
|
86
|
+
* Writes the command-line tool's manifest of a complete snapshot, unless it
|
|
87
|
+
* has one, so that `webslm` (the command-line tool) uses the files without
|
|
88
|
+
* pulling them again.
|
|
89
|
+
*/
|
|
90
|
+
export declare function writeCliManifest(store: CacheStore, manifest: CacheManifest, revision: string): Promise<void>;
|
|
68
91
|
/** Every cached repository of `store`. */
|
|
69
92
|
export declare function listCachedModelsIn(store: CacheStore): Promise<CachedModel[]>;
|
|
70
93
|
/** Removes a cached repository (every revision) from `store`. */
|
package/dist/cache/models.js
CHANGED
|
@@ -2,13 +2,23 @@
|
|
|
2
2
|
* Layout of cached checkpoints inside a {@link CacheStore}:
|
|
3
3
|
*
|
|
4
4
|
* ```text
|
|
5
|
-
* {repo}/refs/{revision}
|
|
6
|
-
* {repo}/{sha}/.webslm.json
|
|
7
|
-
* {repo}/{sha}
|
|
5
|
+
* {repo}/refs/{revision} commit sha a revision resolved to
|
|
6
|
+
* {repo}/{sha}/.webslm.json manifest: files, sizes, hashes, completeness
|
|
7
|
+
* {repo}/{sha}/.webslm-manifest.json the command-line tool's manifest (complete snapshots)
|
|
8
|
+
* {repo}/{sha}/{file} checkpoint files
|
|
9
|
+
* {repo}/{sha}/quantized/{format}/weights.webslmq converted weights
|
|
8
10
|
* ```
|
|
11
|
+
*
|
|
12
|
+
* In Node this is the command-line tool's cache layout
|
|
13
|
+
* (crates/webslm-hub/src/cache.rs) in the same directory, so a model pulled
|
|
14
|
+
* by `webslm pull` loads without a download and the reverse: each side reads
|
|
15
|
+
* the other's manifest, and a completed download in a directory store writes
|
|
16
|
+
* the command-line tool's manifest too.
|
|
9
17
|
*/
|
|
10
18
|
import { joinPath, readText, writeText } from "./store.js";
|
|
11
19
|
export const MANIFEST_FILE = ".webslm.json";
|
|
20
|
+
/** The command-line tool's manifest of a complete snapshot (`MANIFEST_FILE` in crates/webslm-hub/src/cache.rs). */
|
|
21
|
+
export const CLI_MANIFEST_FILE = ".webslm-manifest.json";
|
|
12
22
|
/** Checks a repository id and returns it normalized. */
|
|
13
23
|
export function checkRepoId(repo) {
|
|
14
24
|
const trimmed = repo.trim().replace(/^\/+|\/+$/g, "");
|
|
@@ -20,6 +30,10 @@ export function checkRepoId(repo) {
|
|
|
20
30
|
export function refPath(repo, revision) {
|
|
21
31
|
return joinPath(repo, "refs", encodeURIComponent(revision));
|
|
22
32
|
}
|
|
33
|
+
/** Where the command-line tool keeps a ref: one directory per `/` of the revision. */
|
|
34
|
+
function cliRefPath(repo, revision) {
|
|
35
|
+
return joinPath(repo, "refs", ...revision.split("/").map(encodeURIComponent));
|
|
36
|
+
}
|
|
23
37
|
export function revisionDir(repo, sha) {
|
|
24
38
|
return joinPath(repo, sha);
|
|
25
39
|
}
|
|
@@ -40,8 +54,15 @@ export function weightCachePath(repo, sha, quant) {
|
|
|
40
54
|
export function manifestPath(repo, sha) {
|
|
41
55
|
return joinPath(repo, sha, MANIFEST_FILE);
|
|
42
56
|
}
|
|
57
|
+
export function cliManifestPath(repo, sha) {
|
|
58
|
+
return joinPath(repo, sha, CLI_MANIFEST_FILE);
|
|
59
|
+
}
|
|
43
60
|
export async function readRef(store, repo, revision) {
|
|
44
|
-
|
|
61
|
+
let text = await readText(store, refPath(repo, revision));
|
|
62
|
+
// A branch with `/` pulled by the command-line tool.
|
|
63
|
+
if (text === undefined && revision.includes("/"))
|
|
64
|
+
text = await readText(store, cliRefPath(repo, revision));
|
|
65
|
+
const sha = text?.trim();
|
|
45
66
|
return sha === undefined || sha === "" ? undefined : sha;
|
|
46
67
|
}
|
|
47
68
|
export async function writeRef(store, repo, revision, sha) {
|
|
@@ -62,21 +83,84 @@ export async function readManifest(store, repo, sha) {
|
|
|
62
83
|
export async function writeManifest(store, manifest) {
|
|
63
84
|
await writeText(store, manifestPath(manifest.repo, manifest.sha), JSON.stringify(manifest, null, 2));
|
|
64
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* The snapshot `repo@sha` the command-line tool pulled, as a complete
|
|
88
|
+
* {@link CacheManifest}: `undefined` without its manifest or when a file of
|
|
89
|
+
* it is missing or has another size.
|
|
90
|
+
*/
|
|
91
|
+
export async function readCliManifest(store, repo, sha) {
|
|
92
|
+
const text = await readText(store, cliManifestPath(repo, sha));
|
|
93
|
+
if (text === undefined)
|
|
94
|
+
return undefined;
|
|
95
|
+
let parsed;
|
|
96
|
+
try {
|
|
97
|
+
parsed = JSON.parse(text);
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
if (parsed?.sha !== sha || !Array.isArray(parsed.files))
|
|
103
|
+
return undefined;
|
|
104
|
+
const files = {};
|
|
105
|
+
for (const file of parsed.files) {
|
|
106
|
+
if (typeof file?.name !== "string")
|
|
107
|
+
return undefined;
|
|
108
|
+
let size;
|
|
109
|
+
try {
|
|
110
|
+
size = await store.size(filePath(repo, sha, file.name));
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
return undefined; // a name the store refuses
|
|
114
|
+
}
|
|
115
|
+
if (size === undefined || (typeof file.size === "number" && file.size !== size))
|
|
116
|
+
return undefined;
|
|
117
|
+
files[file.name] =
|
|
118
|
+
typeof file.sha256 === "string" ? { size, sha256: file.sha256, complete: true } : { size, complete: true };
|
|
119
|
+
}
|
|
120
|
+
const created = typeof parsed.created_unix === "number" ? parsed.created_unix * 1000 : 0;
|
|
121
|
+
return { version: 1, repo, sha, files, complete: true, updatedAt: new Date(created).toISOString() };
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Writes the command-line tool's manifest of a complete snapshot, unless it
|
|
125
|
+
* has one, so that `webslm` (the command-line tool) uses the files without
|
|
126
|
+
* pulling them again.
|
|
127
|
+
*/
|
|
128
|
+
export async function writeCliManifest(store, manifest, revision) {
|
|
129
|
+
const path = cliManifestPath(manifest.repo, manifest.sha);
|
|
130
|
+
if (!manifest.complete || (await store.size(path)) !== undefined)
|
|
131
|
+
return;
|
|
132
|
+
const cli = {
|
|
133
|
+
repo: manifest.repo,
|
|
134
|
+
revision,
|
|
135
|
+
sha: manifest.sha,
|
|
136
|
+
files: Object.entries(manifest.files).map(([name, info]) => ({
|
|
137
|
+
name,
|
|
138
|
+
size: info.size,
|
|
139
|
+
sha256: info.sha256 ?? null,
|
|
140
|
+
blob_id: null,
|
|
141
|
+
})),
|
|
142
|
+
created_unix: Math.floor(Date.now() / 1000),
|
|
143
|
+
};
|
|
144
|
+
await writeText(store, path, JSON.stringify(cli, null, 2));
|
|
145
|
+
}
|
|
65
146
|
/** Every cached repository of `store`. */
|
|
66
147
|
export async function listCachedModelsIn(store) {
|
|
67
148
|
const entries = await store.list("");
|
|
68
|
-
const
|
|
149
|
+
const isManifest = (path) => path.endsWith(`/${MANIFEST_FILE}`) || path.endsWith(`/${CLI_MANIFEST_FILE}`);
|
|
150
|
+
// Revisions with either manifest (ours, or the command-line tool's), once each.
|
|
151
|
+
const revisionDirs = new Set(entries.filter((e) => isManifest(e.path)).map((e) => e.path.slice(0, e.path.lastIndexOf("/"))));
|
|
69
152
|
const byRepo = new Map();
|
|
70
|
-
for (const
|
|
71
|
-
const parts =
|
|
72
|
-
const sha = parts[parts.length -
|
|
73
|
-
const repo = parts.slice(0, -
|
|
153
|
+
for (const revision of [...revisionDirs].sort()) {
|
|
154
|
+
const parts = revision.split("/");
|
|
155
|
+
const sha = parts[parts.length - 1];
|
|
156
|
+
const repo = parts.slice(0, -1).join("/");
|
|
74
157
|
if (sha === undefined || repo === "")
|
|
75
158
|
continue;
|
|
76
159
|
const manifest = await readManifest(store, repo, sha);
|
|
160
|
+
const complete = manifest?.complete === true || (await readCliManifest(store, repo, sha)) !== undefined;
|
|
77
161
|
const dir = `${revisionDir(repo, sha)}/`;
|
|
78
162
|
const files = entries
|
|
79
|
-
.filter((e) => e.path.startsWith(dir) && !e.path
|
|
163
|
+
.filter((e) => e.path.startsWith(dir) && !isManifest(e.path))
|
|
80
164
|
.map((e) => ({ name: e.path.slice(dir.length), sizeBytes: e.size }));
|
|
81
165
|
const refs = [];
|
|
82
166
|
const refDir = `${repo}/refs/`;
|
|
@@ -91,7 +175,7 @@ export async function listCachedModelsIn(store) {
|
|
|
91
175
|
refs,
|
|
92
176
|
files,
|
|
93
177
|
sizeBytes: files.reduce((n, f) => n + f.sizeBytes, 0),
|
|
94
|
-
complete
|
|
178
|
+
complete,
|
|
95
179
|
});
|
|
96
180
|
byRepo.set(repo, revisions);
|
|
97
181
|
}
|
package/dist/cache/node-fs.d.ts
CHANGED
|
@@ -1,9 +1,31 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Directory store for Node.js
|
|
3
|
-
* (
|
|
2
|
+
* Directory store for Node.js, in the command-line tool's cache directory
|
|
3
|
+
* (see {@link resolveCacheDir}).
|
|
4
4
|
*/
|
|
5
5
|
import { type CacheEntry, type CacheStore, type CacheWriter } from "./store.js";
|
|
6
|
-
|
|
6
|
+
interface PathModule {
|
|
7
|
+
join(...parts: string[]): string;
|
|
8
|
+
dirname(path: string): string;
|
|
9
|
+
isAbsolute(path: string): boolean;
|
|
10
|
+
}
|
|
11
|
+
/** What the default cache directory depends on. */
|
|
12
|
+
export interface CacheDirEnvironment {
|
|
13
|
+
/** `process.platform`: `"darwin"`, `"win32"`, `"linux"`... */
|
|
14
|
+
readonly platform: string;
|
|
15
|
+
readonly env: Readonly<Record<string, string | undefined>>;
|
|
16
|
+
/** The home directory; `undefined` or `""` when unknown. */
|
|
17
|
+
readonly homedir: string | undefined;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The cache directory, by the command-line tool's rules (`Cache::from_env`
|
|
21
|
+
* in crates/webslm-hub/src/cache.rs), so that both share one cache:
|
|
22
|
+
* `$WEBSLM_CACHE_DIR`, else `~/Library/Caches/webslm` on macOS,
|
|
23
|
+
* `%LOCALAPPDATA%\webslm\cache` on Windows, `$XDG_CACHE_HOME/webslm` (an
|
|
24
|
+
* absolute `XDG_CACHE_HOME`) or `~/.cache/webslm` elsewhere. `path` is the
|
|
25
|
+
* platform's path module (`node:path`, or its `win32`/`posix` flavour).
|
|
26
|
+
*/
|
|
27
|
+
export declare function resolveCacheDir(environment: CacheDirEnvironment, path: Pick<PathModule, "join" | "isAbsolute">): string;
|
|
28
|
+
/** The default cache directory in Node (see {@link resolveCacheDir}). */
|
|
7
29
|
export declare function defaultCacheDir(): Promise<string>;
|
|
8
30
|
export declare class NodeFsStore implements CacheStore {
|
|
9
31
|
#private;
|
|
@@ -19,3 +41,4 @@ export declare class NodeFsStore implements CacheStore {
|
|
|
19
41
|
list(prefix: string): Promise<CacheEntry[]>;
|
|
20
42
|
remove(path: string): Promise<void>;
|
|
21
43
|
}
|
|
44
|
+
export {};
|
package/dist/cache/node-fs.js
CHANGED
|
@@ -1,20 +1,54 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Directory store for Node.js
|
|
3
|
-
* (
|
|
2
|
+
* Directory store for Node.js, in the command-line tool's cache directory
|
|
3
|
+
* (see {@link resolveCacheDir}).
|
|
4
4
|
*/
|
|
5
5
|
import { importNode } from "../runtime.js";
|
|
6
6
|
import { pathSegments, resolveRange, } from "./store.js";
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* The cache directory, by the command-line tool's rules (`Cache::from_env`
|
|
9
|
+
* in crates/webslm-hub/src/cache.rs), so that both share one cache:
|
|
10
|
+
* `$WEBSLM_CACHE_DIR`, else `~/Library/Caches/webslm` on macOS,
|
|
11
|
+
* `%LOCALAPPDATA%\webslm\cache` on Windows, `$XDG_CACHE_HOME/webslm` (an
|
|
12
|
+
* absolute `XDG_CACHE_HOME`) or `~/.cache/webslm` elsewhere. `path` is the
|
|
13
|
+
* platform's path module (`node:path`, or its `win32`/`posix` flavour).
|
|
14
|
+
*/
|
|
15
|
+
export function resolveCacheDir(environment, path) {
|
|
16
|
+
const { platform, env } = environment;
|
|
17
|
+
const custom = env.WEBSLM_CACHE_DIR;
|
|
18
|
+
if (custom)
|
|
19
|
+
return custom;
|
|
20
|
+
const home = environment.homedir || undefined;
|
|
21
|
+
const noHome = () => new Error("cannot find the home directory; set WEBSLM_CACHE_DIR");
|
|
22
|
+
if (platform === "darwin") {
|
|
23
|
+
if (home === undefined)
|
|
24
|
+
throw noHome();
|
|
25
|
+
return path.join(home, "Library", "Caches", "webslm");
|
|
26
|
+
}
|
|
27
|
+
const localAppData = env.LOCALAPPDATA;
|
|
28
|
+
if (platform === "win32" && localAppData)
|
|
29
|
+
return path.join(localAppData, "webslm", "cache");
|
|
30
|
+
const xdg = env.XDG_CACHE_HOME;
|
|
31
|
+
if (xdg && path.isAbsolute(xdg))
|
|
32
|
+
return path.join(xdg, "webslm");
|
|
33
|
+
if (home === undefined)
|
|
34
|
+
throw noHome();
|
|
35
|
+
return path.join(home, ".cache", "webslm");
|
|
36
|
+
}
|
|
37
|
+
/** The default cache directory in Node (see {@link resolveCacheDir}). */
|
|
8
38
|
export async function defaultCacheDir() {
|
|
9
|
-
const env = globalThis.process
|
|
10
|
-
?.env;
|
|
39
|
+
const env = globalThis.process?.env ?? {};
|
|
11
40
|
const path = await importNode("path");
|
|
12
|
-
if (env
|
|
41
|
+
if (env.WEBSLM_CACHE_DIR)
|
|
13
42
|
return env.WEBSLM_CACHE_DIR;
|
|
14
|
-
if (env?.XDG_CACHE_HOME)
|
|
15
|
-
return path.join(env.XDG_CACHE_HOME, "webslm");
|
|
16
43
|
const os = await importNode("os");
|
|
17
|
-
|
|
44
|
+
let homedir;
|
|
45
|
+
try {
|
|
46
|
+
homedir = os.homedir();
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
homedir = undefined;
|
|
50
|
+
}
|
|
51
|
+
return resolveCacheDir({ platform: os.platform(), env, homedir }, path);
|
|
18
52
|
}
|
|
19
53
|
export class NodeFsStore {
|
|
20
54
|
kind = "fs";
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { type CachedModel } from "./cache/models.js";
|
|
3
3
|
export type { CachedModel, CachedRevision } from "./cache/models.js";
|
|
4
4
|
export interface CacheOptions {
|
|
5
|
-
/** Cache directory in Node (default `WEBSLM_CACHE_DIR`
|
|
5
|
+
/** Cache directory in Node (default `WEBSLM_CACHE_DIR`, else the command-line tool's: `~/Library/Caches/webslm` on macOS, `~/.cache/webslm` on Linux). */
|
|
6
6
|
readonly cacheDir?: string;
|
|
7
7
|
}
|
|
8
8
|
/** Models downloaded into the cache (OPFS and Cache API in browsers, the cache directory in Node). */
|
package/dist/download.d.ts
CHANGED
|
@@ -7,6 +7,10 @@
|
|
|
7
7
|
* read from the cache without any network request: a revision given as a
|
|
8
8
|
* commit sha, or a branch already resolved once (the cached ref is reused;
|
|
9
9
|
* delete the cached model to pick up new commits).
|
|
10
|
+
*
|
|
11
|
+
* In a directory store (Node) a snapshot the command-line tool pulled is used
|
|
12
|
+
* as is, and a completed download also writes the command-line tool's
|
|
13
|
+
* manifest, so `webslm pull` and the package share the files.
|
|
10
14
|
*/
|
|
11
15
|
import { type CachedFileInfo } from "./cache/models.js";
|
|
12
16
|
import type { CacheStore } from "./cache/store.js";
|
package/dist/download.js
CHANGED
|
@@ -7,8 +7,12 @@
|
|
|
7
7
|
* read from the cache without any network request: a revision given as a
|
|
8
8
|
* commit sha, or a branch already resolved once (the cached ref is reused;
|
|
9
9
|
* delete the cached model to pick up new commits).
|
|
10
|
+
*
|
|
11
|
+
* In a directory store (Node) a snapshot the command-line tool pulled is used
|
|
12
|
+
* as is, and a completed download also writes the command-line tool's
|
|
13
|
+
* manifest, so `webslm pull` and the package share the files.
|
|
10
14
|
*/
|
|
11
|
-
import { checkRepoId, filePath, readManifest, readRef, writeManifest, writeRef, } from "./cache/models.js";
|
|
15
|
+
import { checkRepoId, filePath, readCliManifest, readManifest, readRef, writeCliManifest, writeManifest, writeRef, } from "./cache/models.js";
|
|
12
16
|
import { WebslmError, abortError, isAborted, toWebslmError } from "./errors.js";
|
|
13
17
|
import { SAFETENSORS_INDEX, fetchRevision, fileUrl, hubFetch, isAdapterListing, selectCheckpointFiles, } from "./hub.js";
|
|
14
18
|
const SHA_PATTERN = /^[0-9a-f]{40}$/;
|
|
@@ -21,8 +25,12 @@ export async function prepareHubCheckpoint(repoId, revision, options) {
|
|
|
21
25
|
// Offline path: a known sha whose files are all complete.
|
|
22
26
|
const knownSha = SHA_PATTERN.test(rev) ? rev : await readRef(store, repo, rev);
|
|
23
27
|
if (knownSha !== undefined) {
|
|
24
|
-
const
|
|
25
|
-
|
|
28
|
+
const ours = await readManifest(store, repo, knownSha);
|
|
29
|
+
const manifest = ours?.complete === true && (await filesPresent(store, ours))
|
|
30
|
+
? ours
|
|
31
|
+
: await readCliManifest(store, repo, knownSha);
|
|
32
|
+
if (manifest !== undefined) {
|
|
33
|
+
await shareWithCli(store, manifest, rev);
|
|
26
34
|
emitCachedProgress(manifest, emit);
|
|
27
35
|
return reader(store, manifest, true);
|
|
28
36
|
}
|
|
@@ -34,7 +42,9 @@ export async function prepareHubCheckpoint(repoId, revision, options) {
|
|
|
34
42
|
const names = revisionInfo.files.map((f) => f.name);
|
|
35
43
|
return withLock(`webslm:${repo}@${sha}`, async () => {
|
|
36
44
|
const previous = await readManifest(store, repo, sha);
|
|
37
|
-
|
|
45
|
+
// Files the command-line tool pulled count as downloaded.
|
|
46
|
+
const pulled = await readCliManifest(store, repo, sha);
|
|
47
|
+
const done = { ...previous?.files, ...pulled?.files };
|
|
38
48
|
let downloaded = false;
|
|
39
49
|
const download = async (file, progress) => {
|
|
40
50
|
const hubFile = hubFiles.get(file);
|
|
@@ -119,12 +129,27 @@ export async function prepareHubCheckpoint(repoId, revision, options) {
|
|
|
119
129
|
await writeManifest(store, manifest);
|
|
120
130
|
if (rev !== sha)
|
|
121
131
|
await writeRef(store, repo, rev, sha);
|
|
132
|
+
await shareWithCli(store, manifest, rev);
|
|
122
133
|
return reader(store, manifest, !downloaded);
|
|
123
134
|
});
|
|
124
135
|
}
|
|
125
136
|
function manifestOf(repo, sha, files, complete) {
|
|
126
137
|
return { version: 1, repo, sha, files, complete, updatedAt: new Date().toISOString() };
|
|
127
138
|
}
|
|
139
|
+
/**
|
|
140
|
+
* Writes the command-line tool's manifest of a complete snapshot in a
|
|
141
|
+
* directory store (where the tool may share the cache); best effort.
|
|
142
|
+
*/
|
|
143
|
+
async function shareWithCli(store, manifest, revision) {
|
|
144
|
+
if (store.kind !== "fs")
|
|
145
|
+
return;
|
|
146
|
+
try {
|
|
147
|
+
await writeCliManifest(store, manifest, revision);
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
// A read-only cache: the command-line tool will pull the files itself.
|
|
151
|
+
}
|
|
152
|
+
}
|
|
128
153
|
async function filesPresent(store, manifest) {
|
|
129
154
|
for (const [file, info] of Object.entries(manifest.files)) {
|
|
130
155
|
if (!info.complete)
|
package/dist/models.d.ts
CHANGED
|
@@ -74,8 +74,9 @@ export declare function normalizeAdapterSource(source: AdapterSource, revision?:
|
|
|
74
74
|
* for await (const delta of model.chat([{ role: "user", content: "Hi!" }])) console.log(delta);
|
|
75
75
|
* ```
|
|
76
76
|
*
|
|
77
|
-
* Hub checkpoints are downloaded once into the cache (OPFS in browsers,
|
|
78
|
-
*
|
|
77
|
+
* Hub checkpoints are downloaded once into the cache (OPFS in browsers, the
|
|
78
|
+
* command-line tool's cache directory in Node) and read from it afterwards,
|
|
79
|
+
* without network.
|
|
79
80
|
*/
|
|
80
81
|
export declare function loadModel(source: string | ModelSource, options?: LoadModelOptions): Promise<Model>;
|
|
81
82
|
/**
|
package/dist/models.js
CHANGED
|
@@ -186,8 +186,9 @@ function requestPersistentStorage() {
|
|
|
186
186
|
* for await (const delta of model.chat([{ role: "user", content: "Hi!" }])) console.log(delta);
|
|
187
187
|
* ```
|
|
188
188
|
*
|
|
189
|
-
* Hub checkpoints are downloaded once into the cache (OPFS in browsers,
|
|
190
|
-
*
|
|
189
|
+
* Hub checkpoints are downloaded once into the cache (OPFS in browsers, the
|
|
190
|
+
* command-line tool's cache directory in Node) and read from it afterwards,
|
|
191
|
+
* without network.
|
|
191
192
|
*/
|
|
192
193
|
export async function loadModel(source, options = {}) {
|
|
193
194
|
const device = resolveDevice(options.device);
|
package/dist/simple.d.ts
CHANGED
|
@@ -265,8 +265,8 @@ export declare function probeTemplateTraits(model: Pick<Model, "info" | "renderC
|
|
|
265
265
|
* offline cache).
|
|
266
266
|
*
|
|
267
267
|
* The first load downloads the checkpoint from the Hugging Face Hub into the
|
|
268
|
-
* cache (OPFS in browsers,
|
|
269
|
-
* from there without network.
|
|
268
|
+
* cache (OPFS in browsers, the command-line tool's cache directory in Node);
|
|
269
|
+
* later loads read it from there without network.
|
|
270
270
|
*
|
|
271
271
|
* ```ts
|
|
272
272
|
* import { load } from "webslm";
|
package/dist/simple.js
CHANGED
|
@@ -493,8 +493,8 @@ class ChatImpl {
|
|
|
493
493
|
* offline cache).
|
|
494
494
|
*
|
|
495
495
|
* The first load downloads the checkpoint from the Hugging Face Hub into the
|
|
496
|
-
* cache (OPFS in browsers,
|
|
497
|
-
* from there without network.
|
|
496
|
+
* cache (OPFS in browsers, the command-line tool's cache directory in Node);
|
|
497
|
+
* later loads read it from there without network.
|
|
498
498
|
*
|
|
499
499
|
* ```ts
|
|
500
500
|
* import { load } from "webslm";
|
package/dist/types.d.ts
CHANGED
|
@@ -192,8 +192,10 @@ export interface LoadModelOptions {
|
|
|
192
192
|
*/
|
|
193
193
|
readonly engine?: EngineHandle;
|
|
194
194
|
/**
|
|
195
|
-
* Where the cache lives in Node (default `WEBSLM_CACHE_DIR
|
|
196
|
-
*
|
|
195
|
+
* Where the cache lives in Node (default `WEBSLM_CACHE_DIR`, else the
|
|
196
|
+
* command-line tool's cache directory: `~/Library/Caches/webslm` on macOS,
|
|
197
|
+
* `%LOCALAPPDATA%\webslm\cache` on Windows, `$XDG_CACHE_HOME/webslm` or
|
|
198
|
+
* `~/.cache/webslm` on Linux). Ignored in browsers, which use OPFS.
|
|
197
199
|
*/
|
|
198
200
|
readonly cacheDir?: string;
|
|
199
201
|
/**
|
package/native-manifest.json
CHANGED
|
@@ -1,47 +1,47 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.4.
|
|
2
|
+
"version": "0.4.1",
|
|
3
3
|
"assets": {
|
|
4
4
|
"darwin-arm64": {
|
|
5
|
-
"file": "webslm-node-0.4.
|
|
6
|
-
"size":
|
|
7
|
-
"sha256": "
|
|
8
|
-
"nodeSha256": "
|
|
5
|
+
"file": "webslm-node-0.4.1-darwin-arm64.node.gz",
|
|
6
|
+
"size": 4528290,
|
|
7
|
+
"sha256": "fef370b0bc05f3190cd91c963bec7d05a48e1c32528ee394c2a38fe599af1e02",
|
|
8
|
+
"nodeSha256": "11c00c4af4394be95076c45cb14a39bf6486cd22c59e93a0c6745bc1c0f8fa6c"
|
|
9
9
|
},
|
|
10
10
|
"darwin-x64": {
|
|
11
|
-
"file": "webslm-node-0.4.
|
|
12
|
-
"size":
|
|
13
|
-
"sha256": "
|
|
14
|
-
"nodeSha256": "
|
|
11
|
+
"file": "webslm-node-0.4.1-darwin-x64.node.gz",
|
|
12
|
+
"size": 4781839,
|
|
13
|
+
"sha256": "b1e11294b083e8b7f7db9f3100193d2aa9642796e9791feb7582803ac2831b03",
|
|
14
|
+
"nodeSha256": "25b7f086532e91f0c3fca8ad709f7c4a7000eaff00d9a93970d808b13ba0b923"
|
|
15
15
|
},
|
|
16
16
|
"linux-x64-gnu": {
|
|
17
|
-
"file": "webslm-node-0.4.
|
|
18
|
-
"size":
|
|
19
|
-
"sha256": "
|
|
20
|
-
"nodeSha256": "
|
|
17
|
+
"file": "webslm-node-0.4.1-linux-x64-gnu.node.gz",
|
|
18
|
+
"size": 4811944,
|
|
19
|
+
"sha256": "147528671faa5e52485eb409207b72b5bc225175713194e99c783c7da2b70d4a",
|
|
20
|
+
"nodeSha256": "80a1cbcc204ed65d8799c13d2c8a5248646a66f72f0ea18aaa7f629f37e1f695"
|
|
21
21
|
},
|
|
22
22
|
"linux-arm64-gnu": {
|
|
23
|
-
"file": "webslm-node-0.4.
|
|
24
|
-
"size":
|
|
25
|
-
"sha256": "
|
|
26
|
-
"nodeSha256": "
|
|
23
|
+
"file": "webslm-node-0.4.1-linux-arm64-gnu.node.gz",
|
|
24
|
+
"size": 4531590,
|
|
25
|
+
"sha256": "cc9bb90288fc968f92f462f31dd7929054243d39fc03401525ad0ddc04b1f505",
|
|
26
|
+
"nodeSha256": "3f5189ce553fb76f7439568dc492ce464849e11f31f82da865fa3079e5176407"
|
|
27
27
|
},
|
|
28
28
|
"linux-x64-musl": {
|
|
29
|
-
"file": "webslm-node-0.4.
|
|
30
|
-
"size":
|
|
31
|
-
"sha256": "
|
|
32
|
-
"nodeSha256": "
|
|
29
|
+
"file": "webslm-node-0.4.1-linux-x64-musl.node.gz",
|
|
30
|
+
"size": 5174150,
|
|
31
|
+
"sha256": "dfced45eaaccaaaf90ceb7b3685198d137181dda270956164a462af847ebc341",
|
|
32
|
+
"nodeSha256": "bf54d6bf59c205f2f03ce4da64e5a015ccf510b92013fc1a6bc7a5c33cd752d1"
|
|
33
33
|
},
|
|
34
34
|
"linux-arm64-musl": {
|
|
35
|
-
"file": "webslm-node-0.4.
|
|
36
|
-
"size":
|
|
37
|
-
"sha256": "
|
|
38
|
-
"nodeSha256": "
|
|
35
|
+
"file": "webslm-node-0.4.1-linux-arm64-musl.node.gz",
|
|
36
|
+
"size": 5012588,
|
|
37
|
+
"sha256": "e4293a066377971e4e900256fcb182a12371b3907c9f2b6dbe5bc7a01a3f7053",
|
|
38
|
+
"nodeSha256": "e6dd20315c5354a8c21ffebb59476bdc8c993ae21e9a3c00d573ae8a0adc7928"
|
|
39
39
|
},
|
|
40
40
|
"win32-x64-msvc": {
|
|
41
|
-
"file": "webslm-node-0.4.
|
|
42
|
-
"size":
|
|
43
|
-
"sha256": "
|
|
44
|
-
"nodeSha256": "
|
|
41
|
+
"file": "webslm-node-0.4.1-win32-x64-msvc.node.gz",
|
|
42
|
+
"size": 5247323,
|
|
43
|
+
"sha256": "570e537e95223ccc2a25c650ae7cd96f2999ce5f08b059add1e98846074a506d",
|
|
44
|
+
"nodeSha256": "a1cd8bf4a3c77f01bdc848f783daa1aeae7786d1aa28f8d7a262915d7f6b3152"
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "webslm",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Run small language models in the browser and in Node, straight from their original Hugging Face checkpoints: a Rust engine compiled to WebAssembly, in a worker, with an offline cache.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Maxime Golfier",
|
|
Binary file
|
|
Binary file
|
|
Binary file
|