-
Notifications
You must be signed in to change notification settings - Fork 99
Add @wllama/wllama-compat for Safari compatibility #223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7b7dd8d
add compat package
ngxson 79ed69e
update examples
ngxson a7295e0
v3.2.0
ngxson 1e4605c
change semantic of mode
ngxson b1a4296
unused var
ngxson 955499f
fix more bugs
ngxson 6433ebb
fix build vite
ngxson 165fde6
fix build (2)
ngxson 5460b97
nits
ngxson 57fef54
also allow publish compat package
ngxson b031520
correct deployment
ngxson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,8 @@ | |
| /src/wasm-from-cdn.ts | ||
| /src/glue/messages.ts | ||
|
|
||
| /compat/wasm | ||
|
|
||
| *.md | ||
| *.mdx | ||
| *.json | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| # @wllama/wllama-compat | ||
|
|
||
| Optional package that provides compatibility WASM assets for `@wllama/wllama` on browsers that lack [JSPI](https://github.com/WebAssembly/js-promise-integration) or [MEMORY64](https://github.com/WebAssembly/memory64) support - most notably Safari and older browsers. | ||
|
|
||
| ## Why this package exists | ||
|
|
||
| The default `@wllama/wllama` build relies on two modern WebAssembly features: [JSPI](https://github.com/WebAssembly/js-promise-integration) and [MEMORY64](https://github.com/WebAssembly/memory64) | ||
|
|
||
| When either feature is absent, wllama automatically falls back to **compat mode**: a separate WASM build that uses [Asyncify](https://emscripten.org/docs/porting/asyncify.html) instead of JSPI, and drops MEMORY64. | ||
|
|
||
| > **Note:** Compat mode has significantly lower performance than the default build. Use it only as a fallback. | ||
|
|
||
| ## Browser compatibility | ||
|
|
||
| | | Chromium | Firefox | Safari | | ||
| |---|---|---|---| | ||
| | Auto-compat (default, recommended) | ✅ | 🟡 (no WebGPU) | 🟡 (supports WebGPU) | | ||
| | Force-compat | ✅ | 🔴 (supports WebGPU) | 🟡 (supports WebGPU) | | ||
| | Non-compat mode | ✅ | 🟡 (no WebGPU) | ❌ | | ||
|
|
||
| - ✅: Good speed | ||
| - 🟡: Acceptable speed | ||
| - 🔴: Runs but slow, not usable | ||
| - ❌: Does not run at all | ||
|
|
||
| ### Default behaviour | ||
|
|
||
| Out of the box, wllama fetches the compat assets from jsDelivr CDN when compat mode is needed. If you want to self-host the assets (no external CDN dependency), install this package (see new section.) | ||
|
|
||
| ### Recommended preset | ||
|
|
||
| By default (`mode = 'safari'`), compat is disabled on Firefox because WebGPU via compat mode is extremely slow there. This is the recommended behaviour: | ||
|
|
||
| ```js | ||
| wllama.setCompat('default'); | ||
| ``` | ||
|
|
||
| If you also want compat on Firefox (e.g. to reach users without JSPI enabled), pass `'firefox_safari'`: | ||
|
|
||
| ```js | ||
| wllama.setCompat('default', 'firefox_safari'); | ||
| ``` | ||
|
|
||
| ## Disabling compat mode | ||
|
|
||
| To opt out of compat mode completely (e.g. you don't target Safari): | ||
|
|
||
| ```ts | ||
| wllama.setCompat(null); | ||
| ``` | ||
|
|
||
| ## Using this package | ||
|
|
||
| **You only need to install package if you want to store compat assets locally**. By default, assets are pulled from CDN. | ||
|
|
||
| ```bash | ||
| npm install @wllama/wllama-compat | ||
| ``` | ||
|
|
||
| Then copy the assets from `node_modules/@wllama/wllama-compat/wasm/` to your public directory and call `setCompat()` with the URLs pointing to those files: | ||
|
|
||
| ```ts | ||
| import { Wllama } from '@wllama/wllama'; | ||
|
|
||
| const wllama = new Wllama({ default: '/wasm/wllama.wasm' }); | ||
|
|
||
| wllama.setCompat({ | ||
| wasm: '/wllama-compat/wasm/wllama.wasm', | ||
| worker: '/wllama-compat/wasm/wllama.js', | ||
| }); | ||
| ``` | ||
|
|
||
| **IMPORTANT**: for Vite, you will need to import the JS as `?raw` | ||
|
|
||
| ```ts | ||
| import compatWasm from '@wllama/wllama-compat/wasm/wllama.wasm?url'; | ||
| import compatWorker from '@wllama/wllama-compat/wasm/wllama.js?raw'; // IMPORTANT: ?raw, NOT ?url | ||
|
|
||
| export const WLLAMA_COMPAT_CONFIG = { | ||
| wasm: compatWasm, | ||
| worker: { | ||
| code: compatWorker, | ||
| }, | ||
| }; | ||
|
|
||
| const instance = new Wllama(WLLAMA_CONFIG_PATHS, { logger: DebugLogger }); | ||
| instance.setCompat(WLLAMA_COMPAT_CONFIG); | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| // refer to README.md for more |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| { | ||
| "name": "@wllama/wllama-compat", | ||
| "version": "3.2.2", | ||
| "description": "Optional package providing compatibility with older browsers for @wllama/wllama", | ||
| "main": "index.js", | ||
| "type": "module", | ||
| "scripts": { | ||
| "upload": "npm publish --access public" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/ngxson/wllama.git" | ||
| }, | ||
| "keywords": [ | ||
| "wasm", | ||
| "webassembly", | ||
| "llama", | ||
| "llm", | ||
| "ai", | ||
| "rag", | ||
| "embeddings", | ||
| "generation" | ||
| ], | ||
| "author": "Xuan Son NGUYEN <contact@ngxson.com>", | ||
| "license": "MIT", | ||
| "bugs": { | ||
| "url": "https://github.com/ngxson/wllama/issues" | ||
| }, | ||
| "homepage": "https://github.com/ngxson/wllama#readme" | ||
| } | ||
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.