myhaiku v2 for the browser (ONNX)
The ONNX version of myhaiku v2 trimmed, a small Gemma 3 model fine-tuned to write haiku in English. It runs in a web page with Transformers.js, on WebGPU or on the CPU through WebAssembly.
Usage
import { pipeline } from "@huggingface/transformers";
const generator = await pipeline("text-generation", "Mr-Corentin/myhaiku-v2-trimmed-onnx", { device: "webgpu" });
const messages = [
{ role: "system", content: "You are a haiku generator. Reply with exactly three short lines, no extra text." },
{ role: "user", content: "a haiku about the first snow" },
];
const output = await generator(messages, { max_new_tokens: 48, do_sample: true, temperature: 1.0, top_p: 0.95, top_k: 64 });
console.log(output[0].generated_text.at(-1).content);
Keep the system prompt: it is the one used during training.
Without WebGPU, use device: "wasm". Transformers.js 4.x loads the "asyncify" build of ONNX Runtime by default, which has no CPU kernel for GatherBlockQuantized. Point it to the plain build before creating the pipeline:
import { env } from "@huggingface/transformers";
const ort = `https://cdn.jsdelivr.net/npm/onnxruntime-web@${env.backends.onnx.versions.web}/dist/`;
env.backends.onnx.wasm.wasmPaths = { mjs: `${ort}ort-wasm-simd-threaded.mjs`, wasm: `${ort}ort-wasm-simd-threaded.wasm` };
What is in the file
One model, onnx/model_quantized.onnx (182 MB), loaded by default:
- the vocabulary is cut from 262k to 89k tokens, keeping English;
- every weight is stored in 8 bits, including the embedding matrix, which is shared between the input lookup and the output head;
- computations stay in float32, because Gemma 3 activations overflow in float16;
- inputs are limited to 2,048 tokens, far more than a haiku request needs, which saves 63 MB of position tables.
On 100 held-out prompts with greedy decoding, 71 haiku come out word for word the same as the float32 model, all keep the three-line format, and the vocabulary diversity is unchanged.
We also tried 4-bit versions (131 to 144 MB). Plain 4-bit rounding broke the three-line format on about one haiku in eight, and HQQ fixed that, but on WebGPU every 4-bit version ran ten times slower than this one with ONNX Runtime Web 1.31, so we kept 8 bits.
| WebGPU | WebAssembly | |
|---|---|---|
| First token | about 0.3 s | about 0.5 s |
| Speed | about 22 tokens/s | about 4.4 tokens/s |
Measured on our test machine with Transformers.js 4.3.1. A haiku is around 20 tokens.
Limitations
- Write your requests in English.
- With 157M parameters, the model gets the form and the imagery right far more often than the meaning.
- About a quarter of the outputs start with a capital letter, because the translators in the training data do not agree on this.