freegen.ai
Your GPU

FreeGen / Blog

One attribute made FLUX almost six times faster in the browser

FreeGen runs FLUX.2 Klein, a 4 billion parameter image model, inside a browser tab. No server draws your picture. The weights download once, the browser keeps them, and your own graphics card does the work through WebGPU.

When we first got it working, a single denoising step at 512 by 512 took 1.6 seconds on an RTX 5090. At 1024 it took about five and a half. The same model in PyTorch on the same card does a step in 81 milliseconds. We expected the browser to be slower. We did not expect twenty times slower, so we went looking for where the time went.

Where the time went

ONNX Runtime has a profiler, and it works in the browser if you can get its output out. Ours said that 95% of every step was spent in a single kind of operation, MatMulNBits, and that it was running at roughly 6 TFLOPS. A 5090 can do a great deal more than that.

MatMulNBits is how ONNX Runtime does matrix multiplication against quantized weights. We ship the denoiser with 8-bit weights: at 4 bits the pictures fell apart (cosine similarity of 0.85 against the original model, against 0.9994 at 8 bits), and diffusion models turn out to be much fussier about this than language models. By default the WebGPU backend takes those 8-bit weights, expands them back to 16-bit floats inside the kernel, and multiplies in fp16. That works, but you are paying for 8-bit storage and getting fp16 arithmetic, on a GPU path that has no access to tensor cores.

The attribute

Every MatMulNBits node has an attribute called accuracy_level. The name makes it sound like a quality dial you would want to leave alone. What it actually sets is the lowest precision the runtime is allowed to use for the activations, the values flowing through the network, as opposed to the weights. Level 4 means int8.

With that permission, the WebGPU backend quantizes each block of activations to 8-bit integers on the fly and multiplies them against the 8-bit weights with packed integer dot products: four multiply-adds in one instruction (DP4A on NVIDIA hardware, dot4I8Packed in WGSL). Both sides of the multiplication are now 8-bit, and the GPU has fast hardware for exactly that.

The change is a short script that walks the graph and sets the attribute on every MatMulNBits node. No weights change, so the new graph reads the same 4 GB weight files as the old one and visitors who already have them download a 5 MB graph and nothing else.

One denoising step, RTX 5090512 px1024 px
Browser, int8 weights, fp16 arithmetic1610 msabout 5500 ms
Browser, int8 weights, int8 arithmetic275 ms1020 ms
PyTorch, fp32, no tensor cores219 ms893 ms
PyTorch, bf16 on tensor cores81 ms281 ms

That is 5.9 times faster at 512 and 5.4 times at 1024. A whole 512 px picture, four steps plus reading the prompt and decoding, now takes about two seconds once the model is loaded. At 1024 it is a little over five.

Did the pictures get worse?

This was the part we were nervous about. Quantizing activations is not free in general, and image models show damage that a benchmark number can hide: banding, colour shifts, mush in fine texture.

We draw the same three prompts with the same seeds before and after every change and compare the pictures pixel by pixel. Against the old fp16 path, the int8 pictures measured 31 to 38 dB PSNR, which in practice means you cannot tell them apart side by side. We also keep pictures made by the original PyTorch pipeline from the same seeds, and against those the two browser paths land within 0.05 dB of each other. Whatever separates our browser build from the reference implementation, the speedup is not it.

How close is that to native?

The fair comparison is the third row of the table. WebGPU in Chrome on Windows cannot reach the tensor cores at all: the subgroup matrix feature that would expose them is not available on the Direct3D 12 backend, and WebNN is not there either, even behind flags. So the browser is competing with PyTorch's plain fp32 path, and it is now within about 25% of it at 512 and 15% at 1024. The bf16 tensor core number is the one we cannot touch from a web page today.

Of what is left in a step, about 69% is the int8 matrix multiplication, running at around 70 trillion operations a second. Most of the rest is attention.

Memory, while we were in there

Speed was not the only problem at 1024 px. A run peaked at 28 GB of GPU memory, which is fine on a 5090 and not fine on almost anything else. Two things caused it.

Attention builds a score matrix for every head, every query against every key, and at 1024 px each one is a gigabyte. We split the queries into eight slices and run the same three operations on each slice. Softmax works one row at a time, so the result is identical to the last bit, and the denoiser stopped growing with the picture. It also got slightly faster, 0.90 seconds a step instead of 0.96.

The image decoder was the other half. It ran at full resolution in fp32 and the runtime held on to the memory afterwards. Decoding in overlapping 512 px tiles, blended at the edges, fixed that with no visible seams. Together the peak went from 28 GB to about 10.

What we took from it

Profile before guessing. We had theories about shader compilation, about the cost of moving data between workers, about attention. None of them mattered next to one operation running at a fraction of what the hardware could do.

And read the attributes on the operators you depend on. The fix was not a new kernel or a clever trick. It was a setting that already existed, with a name that made it look like something you should not touch.

You can try the result at freegen.ai/imagine. The converted model is on Hugging Face as cgb/flux2-klein-4b-onnx-webgpu, under the same Apache 2.0 licence as the original, if you want to run it in your own page.