-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* get AMPLIFY up and running on Mac // Metal * simplify the AMPLIFY Example code * WASM Example of AMPLIFY * refactor deps to let WASM compilation * being the JS version that pulls the model, the configs, and the tokenizer from the same repo * realize we should follow that same format for the AMPLIFy structs.
- Loading branch information
Showing
18 changed files
with
1,055 additions
and
97 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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 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 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 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 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 @@ | ||
build/ |
This file contains 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,25 @@ | ||
[package] | ||
name = "ferritin-wasm-example-amplify" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
candle-core = { workspace = true } | ||
candle-nn = { workspace = true } | ||
tokenizers = { workspace = true, default-features = false, features = [ | ||
"unstable_wasm", | ||
] } | ||
ferritin-amplify = { path = "../../ferritin-amplify" } | ||
|
||
# for wasm | ||
gloo = "0.11.0" | ||
js-sys = "0.3.74" | ||
wasm-bindgen = "0.2.97" | ||
serde-wasm-bindgen = "0.6.5" | ||
serde_json.workspace = true | ||
console_error_panic_hook = "0.1.7" | ||
getrandom = { version = "0.2.15", features = ["js"] } | ||
|
||
|
||
# [lib] | ||
# crate-type = ["cdylib", "rlib"] |
This file contains 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 @@ | ||
# Amplify WASM Example |
This file contains 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,81 @@ | ||
//load Candle Bert Module wasm module | ||
import init, { Model } from "./build/m.js"; | ||
|
||
async function fetchArrayBuffer(url) { | ||
const cacheName = "amplify-candle-cache"; | ||
const cache = await caches.open(cacheName); | ||
const cachedResponse = await cache.match(url); | ||
if (cachedResponse) { | ||
const data = await cachedResponse.arrayBuffer(); | ||
return new Uint8Array(data); | ||
} | ||
const res = await fetch(url, { cache: "force-cache" }); | ||
cache.put(url, res.clone()); | ||
return new Uint8Array(await res.arrayBuffer()); | ||
} | ||
|
||
class Amplify { | ||
static instance = {}; | ||
|
||
static async getInstance(weightsURL, tokenizerURL, configURL, modelID) { | ||
if (!this.instance[modelID]) { | ||
await init(); | ||
|
||
self.postMessage({ status: "downloaded", message: "Downloaded Model" }); | ||
|
||
const [weightsArrayU8, tokenizerArrayU8, mel_filtersArrayU8] = | ||
await Promise.all([ | ||
fetchArrayBuffer(weightsURL), | ||
fetchArrayBuffer(tokenizerURL), | ||
fetchArrayBuffer(configURL), | ||
]); | ||
|
||
self.postMessage({ status: "loading", message: "Loading Model" }); | ||
|
||
this.instance[modelID] = new Model( | ||
weightsArrayU8, | ||
tokenizerArrayU8, | ||
mel_filtersArrayU8 | ||
); | ||
} else { | ||
self.postMessage({ status: "ready", message: "Model Already Loaded" }); | ||
} | ||
return this.instance[modelID]; | ||
} | ||
} | ||
|
||
self.addEventListener("message", async (event) => { | ||
const { | ||
weightsURL, | ||
tokenizerURL, | ||
configURL, | ||
modelID, | ||
// sentences, | ||
normalize = true, | ||
} = event.data; | ||
try { | ||
self.postMessage({ status: "ready", message: "Starting Bert Model" }); | ||
const model = await Amplify.getInstance( | ||
weightsURL, | ||
tokenizerURL, | ||
configURL, | ||
modelID | ||
); | ||
self.postMessage({ | ||
status: "embedding", | ||
message: "Calculating Embeddings", | ||
}); | ||
const output = model.get_embeddings({ | ||
sentences: sentences, | ||
normalize_embeddings: normalize, | ||
}); | ||
|
||
self.postMessage({ | ||
status: "complete", | ||
message: "complete", | ||
output: output.data, | ||
}); | ||
} catch (e) { | ||
self.postMessage({ error: e }); | ||
} | ||
}); |
This file contains 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,19 @@ | ||
#!/bin/bash | ||
set -e # Exit on error | ||
|
||
# cargo clean | ||
mkdir -p target/wasm32-unknown-unknown/release | ||
cargo update | ||
cargo +nightly build \ | ||
--target wasm32-unknown-unknown \ | ||
--release \ | ||
-Z build-std=std,panic_abort \ | ||
--no-default-features | ||
|
||
|
||
|
||
# cargo build --target wasm32-unknown-unknown --release | ||
# rustup +nightly target add wasm32-unknown-unknown | ||
# cargo +nightly build --target wasm32-unknown-unknown --release | ||
# | ||
wasm-bindgen ../../target/wasm32-unknown-unknown/release/m.wasm --out-dir build --target web |
Oops, something went wrong.