summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--candle-wasm-examples/llama2-c/lib-example.html12
-rw-r--r--candle-wasm-examples/llama2-c/llama2cWorker.js16
-rw-r--r--candle-wasm-examples/whisper/whisperWorker.js19
-rw-r--r--candle-wasm-examples/yolo/yoloWorker.js17
4 files changed, 44 insertions, 20 deletions
diff --git a/candle-wasm-examples/llama2-c/lib-example.html b/candle-wasm-examples/llama2-c/lib-example.html
index 5995f003..b5033c54 100644
--- a/candle-wasm-examples/llama2-c/lib-example.html
+++ b/candle-wasm-examples/llama2-c/lib-example.html
@@ -38,11 +38,11 @@
},
stories42M: {
url: "stories42M.bin",
- seq_len: 256,
+ seq_len: 1024,
},
stories110M: {
url: "stories110M.bin",
- seq_len: 256,
+ seq_len: 1024,
},
};
@@ -124,9 +124,17 @@
const prompt = document.querySelector("#prompt");
const clearBtn = document.querySelector("#clear-btn");
const runBtn = document.querySelector("#run");
+ const modelSelect = document.querySelector("#model");
let runController = new AbortController();
let isRunning = false;
+ modelSelect.addEventListener("change", (e) => {
+ const model = MODELS[e.target.value];
+ document.querySelector("#max-seq").max = model.seq_len;
+ document.querySelector("#max-seq").nextElementSibling.value =
+ model.seq_len;
+ });
+
form.addEventListener("submit", async (e) => {
e.preventDefault();
if (isRunning) {
diff --git a/candle-wasm-examples/llama2-c/llama2cWorker.js b/candle-wasm-examples/llama2-c/llama2cWorker.js
index e4229055..abaf3401 100644
--- a/candle-wasm-examples/llama2-c/llama2cWorker.js
+++ b/candle-wasm-examples/llama2-c/llama2cWorker.js
@@ -1,13 +1,17 @@
import init, { Model } from "./build/m.js";
async function fetchArrayBuffer(url) {
- const res = await fetch(url, {
- cache: "force-cache",
- });
- const data = await res.arrayBuffer();
- return new Uint8Array(data);
+ const cacheName = "llama2c-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 Llama2C {
static instance = {};
diff --git a/candle-wasm-examples/whisper/whisperWorker.js b/candle-wasm-examples/whisper/whisperWorker.js
index 2598adde..d2ad8e0b 100644
--- a/candle-wasm-examples/whisper/whisperWorker.js
+++ b/candle-wasm-examples/whisper/whisperWorker.js
@@ -2,16 +2,17 @@
import init, { Decoder } from "./build/m.js";
async function fetchArrayBuffer(url) {
- const res = await fetch(url, {
- cache: "force-cache",
- headers: {
- "Cache-Control": "public, max-age=31536000",
- },
- });
- const data = await res.arrayBuffer();
- return new Uint8Array(data);
+ const cacheName = "whisper-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 Whisper {
static instance = {};
// Retrieve the Whisper model. When called for the first time,
diff --git a/candle-wasm-examples/yolo/yoloWorker.js b/candle-wasm-examples/yolo/yoloWorker.js
index 93097372..8b5ef8b9 100644
--- a/candle-wasm-examples/yolo/yoloWorker.js
+++ b/candle-wasm-examples/yolo/yoloWorker.js
@@ -1,6 +1,19 @@
//load the candle yolo wasm module
import init, { Model, ModelPose } from "./build/m.js";
+async function fetchArrayBuffer(url) {
+ const cacheName = "yolo-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 Yolo {
static instance = {};
// Retrieve the YOLO model. When called for the first time,
@@ -11,9 +24,7 @@ class Yolo {
await init();
self.postMessage({ status: `loading model ${modelID}:${modelSize}` });
- const modelRes = await fetch(modelURL);
- const yoloArrayBuffer = await modelRes.arrayBuffer();
- const weightsArrayU8 = new Uint8Array(yoloArrayBuffer);
+ const weightsArrayU8 = await fetchArrayBuffer(modelURL);
if (/pose/.test(modelID)) {
// if pose model, use ModelPose
this.instance[modelID] = new ModelPose(weightsArrayU8, modelSize);