Spark-X2.6-4B running in the browser on WebGPU (ONNX, official INT4 weights)

#26
by mohdmahmodi - opened

Hi XHToken team, thanks for Spark-X2.6!

I converted Spark-X2.6-4B to ONNX so it runs in the browser on WebGPU, with no server and nothing to install.

The 4-bit weights are your GPTQ codes from Spark-X2.6-4B-INT4, repacked for onnxruntime-web without changing them. The sliding-window and full-attention layers, partial rotary, output gate, thinking modes and tool calling all work, using the official chat template.

I compared it with the original model in transformers on the same prompts (greedy, thinking off):

bf16 official INT4 this build
GSM8K, 1,319 problems 90.8% 91.2% 90.5%
BFCL v3 simple, 200 cases 89.0% 89.5% 89.5%
Needle in a haystack, 1k to 16k tokens 12/12 12/12 12/12

The GSM8K gap to INT4 is within noise (McNemar p = 0.18).

On an RTX 3090 Ti in Chrome it does about 92 tok/s with a short prompt and 69 tok/s at 16k tokens of context. The download is 2.77 GB and the browser caches it.

It also works with Transformers.js 4.3:

import { pipeline } from "@huggingface/transformers";

const generator = await pipeline("text-generation", "mohdmahmodi/Spark-X2.6-4B-ONNX-WebGPU", { device: "webgpu", dtype: "q4f16" });
const out = await generator([{ role: "user", content: "Hello!" }], {
  max_new_tokens: 256, tokenizer_encode_kwargs: { enable_thinking: false },
});
console.log(out[0].generated_text.at(-1).content);

I've only tested it on Windows with an NVIDIA GPU so far. If anyone tries it on a Mac, an AMD or Intel GPU, or a phone, let me know how it goes. And if you think it's useful, I'd be glad to see it linked from the model card next to the GGUF.

SparkLLM org

Thanks for the contribution and detailed validation! We’ve added a WebGPU section to our README linking to your demo, model files, and source code.

Sign up or log in to comment