FLUX.2-klein-4B — LiteRT (on-device text-to-image and image editing)
Black Forest Labs FLUX.2 [klein] 4B
(Apache-2.0) converted to LiteRT CompiledModel int8 graphs, generating and
editing images fully on a phone GPU. The upstream model card says klein "runs on consumer GPUs, with
as little as 13 GB VRAM". These graphs run it on a Pixel 8a's Mali-G715, which has no
dedicated VRAM at all.
[Image blocked: generated on a Pixel 8a Mali GPU]
Prompt: "a red apple on a wooden table, studio lighting". 4 steps, 256×256, generated
end-to-end on a Pixel 8a Mali GPU in 306 s. Matches the fp32 diffusers pipeline at
PSNR 36.8 dB / corr 0.9987.
[Image blocked: edited on a Pixel 8a Mali GPU]
Editing: source, the fp32 diffusers edit, and the same edit on a Pixel 8a. Prompt:
"turn the apple into a green apple". 4 steps, 256×256, PSNR 44.3 dB / SSIM 0.9998
against the fp32 pipeline, in 328–369 s.
klein is step-wise distilled, so the sampling loop is unusually plain: 4 steps, no
classifier-free guidance (one DiT pass per step, not two), no sign flip — just a
flow-matching Euler update latents += dsigma[step] * noise_pred.
What's here
The pipeline (Qwen3-4B text encoder → rectified-flow DiT → VAE) is exported as
INTEGER-int8 LiteRT graphs — int×int compute, the path the GPU delegate actually
runs; weight-only-FLOAT quantization hangs the GPU compile. The 4B DiT and the 4B
encoder each exceed both LiteRT's 2 GB flatbuffer load limit and a phone's GPU budget,
so they are split into chunks that are resident one at a time: peak footprint is a
single ~912 MB graph rather than the 6.2 GB total.
The text encoder is included because klein conditions on Qwen3-4B hidden states from
layers 9 / 18 / 27, interleaved to 7680 channels — not on a pooled embedding, so there
is no smaller drop-in replacement.
Tensors are raw float32, little-endian, row-major. Tokenization, embed_tokens, the
causal+padding mask, both rotary tables, the scheduler and the two tail permutations run
on the host.
Typing the prompt
The prompt is not baked in — only two of the staged tensors depend on the words
(inputs_embeds and enc_mask), so the app encodes a typed prompt itself. Stage the
tokenizer/ folder here (qwen_vocab.txt, qwen_merges.txt, qwen_special.txt and the
778 MB qwen_embed_fp16.bin) and the app shows editable prompt fields.
It carries a faithful Qwen2Tokenizer port, fixture-tested byte-for-byte against Python
(tokenizer_fixture.txt), and looks token rows up in the memory-mapped fp16 embedding
table — a GATHER over 151936 rows is not a GPU op, and the gathered row is the graph's
input anyway. The fp16 table is within 3e-8 of the fp32 weights because the checkpoint is
bf16. Tokenizing and embedding a prompt takes about 1 s; a typed "a blue ceramic teapot on
a marble counter, morning light" generates the teapot, not the baked apple.
Image editing
Flux2KleinPipeline.__call__ takes image as its first argument: klein is natively
an editing model and text-to-image is the image=None case. Editing VAE-encodes the
reference and appends its latent tokens to the noise tokens before every step,
with the reference separated from the noise on the rotary T axis (noise T = 0, the
i-th reference T = 10 + 10·i). Afterwards only the noise half is kept:
noise_pred[:, :latents.size(1)].
At 256×256 that grows the image sequence 256 → 512 tokens, so the joint sequence goes
768 → 1024. The weights do not change, so kce_* are the same tensors re-exported at
the longer shape and every chunk is byte-identical in size to its kc_* twin. Measured
on a Pixel 8a: peak RSS +2 %, per-step GPU time 1.6×, shader compile 1.0–1.2×
— and since compilation dominates, editing costs about +7 % end-to-end.
The VAE encoder needed one rewrite: DiagonalGaussianDistribution.mode() chunks the
64-channel moments in half, which lowers to the banned SPLIT. The mode is the mean,
so slicing the first 32 channels directly is bit-exact and GPU-clean.
Caveats. 256×256, one reference image, and the prompt is baked into the staged host
tensors. More references or a different resolution means a re-export, not a redesign.
Report SSIM next to PSNR: the residual error is sparse. On a moonlit-snow edit the
worst 0.5 % of pixels carry 76 % of the squared error (specular speckles flipping between
near-white and dark blue), which drags PSNR to 27.9 dB while SSIM stays at 0.989 and the
image is perceptually identical to the fp32 reference.
Two things the graphs assume
Both come from the GPU delegate (ML Drift), and neither is visible to a desktop op check.
- The attention mask is pre-expanded across heads: pass
[1, 32, 512, 512], not
[1, 1, 512, 512]. A broadcast ADD whose left operand is a BATCH_MATMUL result is
silently miscomputed — the probabilities still sum to 1 and still honour the causal and
padding masks, but the logits are wrong.
- Compute must be FP32:
GpuOptions(precision = FP32). The modulated (adaLN) blocks
overflow fp16 and return NaN.
Also: create one Environment and share it across every CompiledModel (a null
environment leaks the OpenCL context), and close every TensorBuffer after each run.
Usage — Python (reproduces the exact device loop)
Usage — Kotlin (Android, LiteRT GPU)
Conversion
Quantization is litert_torch full_dynamic_recipe(weight_dtype=INT8, granularity=CHANNELWISE).
The conversion scripts (build_klein_enc.py, chunked_export_klein.py,
vae_deploy_klein.py) and the host-prep / verification reference
(gen_prep_klein.py, gen_verify_klein.py) ship alongside the LiteRT sample app for
this model. Three rewrites are required for a GPU-clean graph, all exact:
- RoPE without
GATHER_ND — bake the even/odd de-interleave into the rows of
to_q / to_k and the fused to_qkv_mlp_proj, turning it into a contiguous
half-split rotation. q · k is invariant to a permutation applied to both.
- GQA
repeat_kv as a CONCATENATION — the stock expand is rank-5 and lowers
to BROADCAST_TO, which the GPU delegate rejects outright.
- Safe RMSNorm / LayerNorm (max-normalized) and
ManualGroupNormND in the VAE.
Note that the desktop int8 path is a pessimistic proxy: the same graphs score
36.4 dB through the host CPU int8 kernels and 44.1 dB on the device. Weights are never
redistributed here — the graphs are produced from the original Apache-2.0 checkpoint
with those scripts.
Performance
Measured on a Pixel 8a (Tensor G3, Android 16) with the standard TFLite benchmark_model tool — 5 warm-up runs then 20 timed runs, reported as the tool's mean.
This pipeline ships 21 graphs; the 6 below are the representative ones — the shards that dominate a step plus the small head and tail graphs. The repeated shards (kc_single1..3 and friends) are the same shape as the one measured here.
These are the classic TFLite OpenCL delegate, not LiteRT's own accelerator. The Kotlin usage above runs through LiteRT CompiledModel, which is a different GPU implementation; any figure quoted elsewhere on this card came from that path and is not comparable to this table. Read this table as a reproducible floor anyone can re-measure with a public tool.
Two graphs do not run on this delegate at all. kv_vae fails with Failed to create Image2D from Buffer (clCreateImage): Invalid image size, and ke_enc0 with an OpenCL program build error; both then fall back to OpenGL, which rejects them with Batch size mismatch, expected 1 but got 32.
The small head and tail graphs are faster on the CPU here — kc_final.tflite (7 ms on CPU against 58 ms on GPU), kc_prep.tflite (44 ms on CPU against 72 ms on GPU) — so a host loop that sends every graph to the GPU is leaving time on the table.
Snapdragon NPU (Hexagon)
This repo publishes 21 graphs and the sweep measured each one separately. On all 18 where both accelerators ran, the GPU is faster. The NPU loads faster on every one of them. Per-file rows are below.
ke_enc0.tflite — the GPU runs it at 339.8 ms. The NPU does not — the graph compiles and then fails to run (LiteRtException: Failed to invoke the compiled model).
ke_enc1.tflite — the GPU runs it at 340.0 ms. The NPU does not — the run reported no result line.
ke_enc2.tflite — the GPU runs it at 339.2 ms. The NPU does not — the run reported no result line.
Measured on a Samsung Galaxy S26 (Snapdragon 8 Elite Gen 5 / SM8850, Hexagon v81, Android 16) with LiteRT CompiledModel 2.2.0, one accelerator per process, 5 warm-up runs then N=50 timed runs, median reported. Every run held thermal status NONE throughout. Headroom 0.58–0.83, where 1.0 is the throttling threshold.
The NPU rows marked on-device JIT ran the published file unchanged. LiteRT compiled it for the Hexagon on the device at first load. Those first compiles took 664 ms to 107 s here. The load column above is the cached load every later run pays. Recipe and the runtime libraries it needs: NPU guide.
The NPU rows marked AOT ran an artifact compiled ahead of time for SM8850 (ai-edge-litert 2.2.0 + QAIRT 2.47.0), not the published file. That artifact is not distributed here; the compile is one command in the NPU guide.
GPU wiring: GPU guide.
License
Apache-2.0, inherited from black-forest-labs/FLUX.2-klein-4B (weights and text encoder).