Spaces:
Running
Running
Diagnostic switches, CPU-side embedding lookup, python branch link
Browse filesFiles changed (4)
- README.md+5-1
- index.html+1
- llm.js+2-1
- main.js+9-2
26 unmodified lines2728293026 unmodified linesllama.cpp cannot take raw embeddings, so the 32 image vectors are written into 32 reserved rows (`<0x80>`..`<0x9F>`) of the F16 token-embedding table before the model is loaded; the prompt carries those 32 token ids. A new image reloads the model from the browser cache; more questions about the same image do not.Checked in headless Chromium: image preprocessing is bit-exact with torchvision; the ONNX vision graph matches PyTorch (cosine 0.99996); the 8-bit WebGPU kernel matches the CPU one to 2.5e-7; native llama.cpp with the patched table captions the example correctly, and so does the full page on CPU. **Not measured:** speed and memory on a real GPU (this sandbox has none). Vicuna v0 derives from LLaMA-1 (non-commercial research licence).26 unmodified lines272829303132333426 unmodified linesllama.cpp cannot take raw embeddings, so the 32 image vectors are written into 32 reserved rows (`<0x80>`..`<0x9F>`) of the F16 token-embedding table before the model is loaded; the prompt carries those 32 token ids. A new image reloads the model from the browser cache; more questions about the same image do not.**The Python version** (the original Gradio app on ZeroGPU, with the patched `minigpt4` package) is on the [`python` branch](https://huggingface.co/spaces/WeReCooking/MiniGPT4-WebGPU/tree/python).If answers look wrong on your GPU, add `?vgpu=0`, `?llmgpu=0` or `?check=1` to the address (vision on CPU, language model on CPU, compare GPU/CPU image embeddings) to see which part misbehaves.Checked in headless Chromium: image preprocessing is bit-exact with torchvision; the ONNX vision graph matches PyTorch (cosine 0.99996); the 8-bit WebGPU kernel matches the CPU one to 2.5e-7; native llama.cpp with the patched table captions the example correctly, and so does the full page on CPU; on an explicit adult test image both the native run and the page on CPU answer directly, with no refusals. **Not measured:** speed and memory on a real GPU (this sandbox has none). Vicuna v0 derives from LLaMA-1 (non-commercial research licence).
37 unmodified lines38394041424337 unmodified linesA new image therefore reloads the language model from your browser's cache (some seconds); more questions about the same image do not. Text containing those rare byte tokens is not supported.</p><p><b>Limits.</b> About 6 GB of free memory and a GPU with 4 GB+ of video memory are needed; without WebGPU it runs on the CPU, at under one word a second. This is a 2023 model: captions are often wrong in details, and it is uncensored, so it describes whatever you give it.Vicuna v0 is derived from LLaMA-1 (non-commercial research licence).</p><p id="store"></p><button id="wipe" type="button">Delete downloaded models</button></details>37 unmodified lines3839404142434437 unmodified linesA new image therefore reloads the language model from your browser's cache (some seconds); more questions about the same image do not. Text containing those rare byte tokens is not supported.</p><p><b>Limits.</b> About 6 GB of free memory and a GPU with 4 GB+ of video memory are needed; without WebGPU it runs on the CPU, at under one word a second. This is a 2023 model: captions are often wrong in details, and it is uncensored, so it describes whatever you give it.Vicuna v0 is derived from LLaMA-1 (non-commercial research licence).</p><p><b>If answers look wrong or refuse.</b> Add <code>?vgpu=0</code> (vision on the CPU), <code>?llmgpu=0</code> (language model on the CPU) or <code>?check=1</code> (compare the GPU and CPU image embeddings) to the address to find out which part misbehaves on your GPU. The original Python version is on the <a href="https://huggingface.co/spaces/WeReCooking/MiniGPT4-WebGPU/tree/python" target="_blank" rel="noopener"><code>python</code> branch</a>.</p><p id="store"></p><button id="wipe" type="button">Delete downloaded models</button></details>
29 unmodified lines3031323334353629 unmodified lineslet n = nCtx;for (;;) {const w = new Wllama({ default: new URL('./vendor/wllama/wllama.wasm', import.meta.url).href });try { await w.loadModel([blob], { n_ctx: n, n_gpu_layers: gpu ? 999 : 0 }); return { w, nCtx: n }; }catch (e) {console.warn('load failed', e); await w.exit().catch(() => {});if (n <= 1024) throw e;29 unmodified lines303132333435363729 unmodified lineslet n = nCtx;for (;;) {const w = new Wllama({ default: new URL('./vendor/wllama/wllama.wasm', import.meta.url).href });// token_embd_on_gpu: false because the embedding table holds the image: look it up on the CPU copytry { await w.loadModel([blob], { n_ctx: n, n_gpu_layers: gpu ? 999 : 0, token_embd_on_gpu: false }); return { w, nCtx: n }; }catch (e) {console.warn('load failed', e); await w.exit().catch(() => {});if (n <= 1024) throw e;
7 unmodified lines89101112131433 unmodified lines484950515253544 unmodified lines5960616263646566677 unmodified linesconst BASE = local(q.get('model')) || `https://huggingface.co/${REPO}/resolve/main`;const ORT_URL = local(q.get('ort')) || 'https://cdn.jsdelivr.net/npm/onnxruntime-web@1.31.1/dist/ort.webgpu.min.mjs';const N_CTX = Number(q.get('ctx')) || 2048;const USE_GPU = q.get('gpu') !== '0', LLM_GPU = q.get('llmgpu') !== '0'; // llmgpu=0: test hook (vision on WebGPU, language model on CPU)const EXAMPLES = [{ label: 'two cats', image: 'examples/cats.jpg', q: 'Describe this image in detail.',a: 'This image shows two cats lying on a pink blanket. One cat is sleeping with its eyes closed, while the other is lying on its side with its eyes open. There are two remote controls on the blanket next to the cats. The image appears to be taken in a living room or other indoor space.' }];33 unmodified linesconst gpu = USE_GPU && !!navigator.gpu && !!(await navigator.gpu.requestAdapter().catch(() => null));if (!gpu) status('No WebGPU in this browser: running on the CPU (very slow).', 0);status('Looking at the image (vision model)…', 0);const embeds = await encodeImage(ort, files.vision, bitmap, gpu ? 'webgpu' : 'wasm', () => status('The GPU could not run the vision model: using the CPU instead (slower)…', 0));if (my !== generation) return;if (current) { await current.w.exit().catch(() => {}); current = null; }status('Loading the language model with the image (a few seconds from the cache)…', 0);4 unmodified linessetReady(true);if (cached) { addTurn(cached.q, cached.a, 'Precomputed answer (greedy decoding). Ask again to run it on your GPU.'); status('Ready. Ask anything about the image.', 1); }else status('Ready. Ask anything about the image.', 1);} catch (e) { console.error(e); if (my === generation) status(e?.message || String(e), 0, true); }}function addTurn(question, answer, note) {const t = el('div', 'turn'); t.append(el('p', 'q', question)); const a = el('p', 'a', answer); t.append(a);const n = el('p', 'note', note || ''); if (note) t.append(n); ui.log.prepend(t);7 unmodified lines8910111213141533 unmodified lines495051525354555657584 unmodified lines6364656667686970717273747 unmodified linesconst BASE = local(q.get('model')) || `https://huggingface.co/${REPO}/resolve/main`;const ORT_URL = local(q.get('ort')) || 'https://cdn.jsdelivr.net/npm/onnxruntime-web@1.31.1/dist/ort.webgpu.min.mjs';const N_CTX = Number(q.get('ctx')) || 2048;// Switches for telling a GPU problem from a model problem: ?gpu=0 everything on CPU, ?vgpu=0 vision on CPU, ?llmgpu=0 language model on CPU, ?check=1 compare GPU and CPU image embeddings.const USE_GPU = q.get('gpu') !== '0', VISION_GPU = q.get('vgpu') !== '0', LLM_GPU = q.get('llmgpu') !== '0', CHECK = q.get('check') === '1';const EXAMPLES = [{ label: 'two cats', image: 'examples/cats.jpg', q: 'Describe this image in detail.',a: 'This image shows two cats lying on a pink blanket. One cat is sleeping with its eyes closed, while the other is lying on its side with its eyes open. There are two remote controls on the blanket next to the cats. The image appears to be taken in a living room or other indoor space.' }];33 unmodified linesconst gpu = USE_GPU && !!navigator.gpu && !!(await navigator.gpu.requestAdapter().catch(() => null));if (!gpu) status('No WebGPU in this browser: running on the CPU (very slow).', 0);status('Looking at the image (vision model)…', 0);const vgpu = gpu && VISION_GPU;const embeds = await encodeImage(ort, files.vision, bitmap, vgpu ? 'webgpu' : 'wasm', () => status('The GPU could not run the vision model: using the CPU instead (slower)…', 0));let check = '';if (CHECK && vgpu) { status('Checking the GPU result against the CPU (slow)…', 0); check = `GPU vs CPU image embeddings: cosine ${cosine(embeds, await encodeImage(ort, files.vision, bitmap, 'wasm')).toFixed(5)} (1.0 = identical; below 0.99 means the GPU computes the image wrongly)`; }if (my !== generation) return;if (current) { await current.w.exit().catch(() => {}); current = null; }status('Loading the language model with the image (a few seconds from the cache)…', 0);4 unmodified linessetReady(true);if (cached) { addTurn(cached.q, cached.a, 'Precomputed answer (greedy decoding). Ask again to run it on your GPU.'); status('Ready. Ask anything about the image.', 1); }else status('Ready. Ask anything about the image.', 1);if (check) addTurn('Diagnostics', check);else if (CHECK) addTurn('Diagnostics', 'Vision ran on the CPU, so there is nothing to compare.');} catch (e) { console.error(e); if (my === generation) status(e?.message || String(e), 0, true); }}const cosine = (a, b) => { let d = 0, x = 0, y = 0; for (let i = 0; i < a.length; i++) { d += a[i] * b[i]; x += a[i] * a[i]; y += b[i] * b[i]; } return d / Math.sqrt(x * y); };function addTurn(question, answer, note) {const t = el('div', 'turn'); t.append(el('p', 'q', question)); const a = el('p', 'a', answer); t.append(a);const n = el('p', 'note', note || ''); if (note) t.append(n); ui.log.prepend(t);
README.md+5-1
index.html+1
llm.js+2-1
main.js+9-2