--- license: bsd-3-clause library_name: LiteRT pipeline_tag: image-to-image tags: [litert, tflite, on-device, android, gpu, style-transfer, neural-style, fast-neural-style] base_model: pytorch/examples base_model_relation: quantized --- Measured on device (edge-compat, style_candy_fp16): Galaxy S26 · LiteRT 2.2.0 · GPU (ML Drift) · 13.9 ms p50 (2026-08-25); Galaxy S26 · LiteRT 2.2.0 · NPU (QNN/HTP) · 7.41 ms p50 (2026-08-25); Raspberry Pi 5 · LiteRT 2.2.0.dev20260804 · CPU/XNNPACK, 4 threads · 287 ms p50 (2026-08-31); browser · Chromium 151 on M4 Max · LiteRT.js 2.5.3 · WebGPU · 7.34 ms p50 · output differs from CPU (max rel diff 0.05) (2026-08-11). Record: https://github.com/john-rocky/edge-compat/blob/main/cards/fast-neural-style__style_candy_fp16/CARD.md Measured on device (edge-compat, style_mosaic_fp16): Galaxy S26 · LiteRT 2.2.0 · GPU (ML Drift) · 13.8 ms p50 (2026-08-25); Galaxy S26 · LiteRT 2.2.0 · NPU (QNN/HTP) · 7.41 ms p50 (2026-08-25); Raspberry Pi 5 · LiteRT 2.2.0.dev20260804 · CPU/XNNPACK, 4 threads · 288 ms p50 (2026-08-31); browser · Chromium 151 on M4 Max · LiteRT.js 2.5.3 · WebGPU · 7.19 ms p50 · output differs from CPU (max rel diff 0.016) (2026-08-11). Record: https://github.com/john-rocky/edge-compat/blob/main/cards/fast-neural-style__style_mosaic_fp16/CARD.md Measured on device (edge-compat, style_rain_princess_fp16): Galaxy S26 · LiteRT 2.2.0 · GPU (ML Drift) · 13.8 ms p50 (2026-08-25); Galaxy S26 · LiteRT 2.2.0 · NPU (QNN/HTP) · 7.48 ms p50 (2026-08-25); Raspberry Pi 5 · LiteRT 2.2.0.dev20260804 · CPU/XNNPACK, 4 threads · 288 ms p50 (2026-08-31); browser · Chromium 151 on M4 Max · LiteRT.js 2.5.3 · WebGPU · 7.34 ms p50 · output differs from CPU (max rel diff 0.25) (2026-08-11). Record: https://github.com/john-rocky/edge-compat/blob/main/cards/fast-neural-style__style_rain_princess_fp16/CARD.md Measured on device (edge-compat, style_udnie_fp16): Galaxy S26 · LiteRT 2.2.0 · GPU (ML Drift) · 13.8 ms p50 (2026-08-25); Galaxy S26 · LiteRT 2.2.0 · NPU (QNN/HTP) · 7.42 ms p50 (2026-08-25); Raspberry Pi 5 · LiteRT 2.2.0.dev20260804 · CPU/XNNPACK, 4 threads · 288 ms p50 (2026-08-31); browser · Chromium 151 on M4 Max · LiteRT.js 2.5.3 · WebGPU · 7.37 ms p50 · output differs from CPU (max rel diff 0.0041) (2026-08-11). Record: https://github.com/john-rocky/edge-compat/blob/main/cards/fast-neural-style__style_udnie_fp16/CARD.md # Fast Neural Style Transfer — LiteRT (on-device, fully-GPU, 4 styles) Fast neural **style transfer** ([PyTorch examples](https://github.com/pytorch/examples/tree/main/fast_neural_style) `TransformerNet`, Johnson et al.), converted to **LiteRT** and running **fully on the `CompiledModel` GPU** (ML Drift) on Android. Applies an artistic style to a photo — **4 styles** (candy / mosaic / rain_princess / udnie), each a **3.5 MB** fp16 graph. ![Fast Neural Style — content + candy / mosaic / rain / udnie (on-device LiteRT GPU)](samples/sample.png) ## On-device (Pixel 8a, Tensor G3 — verified) | | | |---|---| | nodes on GPU | **350 / 350** LITERT_CL (full residency) | | inference | **~9 ms** (256×256) | | size | 3.5 MB per style (fp16) | | accuracy | device-vs-PyTorch corr **0.9998–0.9999** (all 4 styles) | ``` image[1,3,256,256] (RGB 0-255) →[GPU: TransformerNet]→ stylized[1,3,256,256] (RGB 0-255) ``` ## Minimal usage **Android (Kotlin, CompiledModel GPU)** ```kotlin val model = CompiledModel.create(context.assets, "style_candy_fp16.tflite", CompiledModel.Options(Accelerator.GPU), null) val inputs = model.createInputBuffers() val outputs = model.createOutputBuffers() inputs[0].writeFloat(chw) // [1,3,256,256] RGB 0-255, NCHW model.run(inputs, outputs) val stylized = outputs[0].readFloat() // [1,3,256,256] RGB 0-255 ``` **Python (desktop verification)** ```python import numpy as np from PIL import Image from ai_edge_litert.interpreter import Interpreter img = Image.open("photo.jpg").convert("RGB") w, h = img.size; s = min(w, h) img = img.crop(((w-s)//2, (h-s)//2, (w+s)//2, (h+s)//2)).resize((256, 256)) x = np.asarray(img, np.float32).transpose(2, 0, 1)[None] # 0-255, no normalization # candy / mosaic / rain_princess / udnie it = Interpreter(model_path="style_candy_fp16.tflite"); it.allocate_tensors() it.set_tensor(it.get_input_details()[0]["index"], x); it.invoke() y = it.get_tensor(it.get_output_details()[0]["index"])[0] # [3,256,256] RGB 0-255 Image.fromarray(y.transpose(1, 2, 0).clip(0, 255).astype(np.uint8)).save("stylized.png") ``` ## How it converts (litert-torch) — three numerically-exact re-authorings 1. **`ReflectionPad2d` → zero-pad** (`GATHER_ND` → `PAD`; border-only difference). 2. **Large conv activations → conv-weight scaling.** The conv outputs reach ≈ |5000|, where the Mali delegate's fp16 conv accumulation loses precision → garbage (device corr 0.34 at full residency — *residency ≠ correctness*). Each conv is followed by an `InstanceNorm` (which is **scale-invariant**), so scaling those conv weights down so the output is ≈ |10| is **exact** (IN output unchanged) and keeps the fp16 accumulation precise → corr 1.0. 3. **`InstanceNorm` → SafeInstanceNorm** (down-scaled-domain spatial reduction, fp16-safe; SafeLayerNorm class). Upsample is `interpolate(nearest)` (no transposed conv → no ZeroStuff). Result: banned ops NONE, ≤4D, tflite-vs-torch corr **1.0**, device-vs-torch corr **0.9999**. ## Preprocessing Center-crop to square, resize to 256×256, RGB **0–255** (no normalization), NCHW. Output is 0–255 RGB (clamp). ## Performance Measured on a **Pixel 8a** (Tensor G3, Android 16) with the standard TFLite [`benchmark_model`](https://ai.google.dev/edge/litert/models/measurement) tool — 10 warm-up runs then 50 timed runs, reported as the tool's mean. | Runtime | Backend | Graph on GPU | Latency | |---|---|---|---| | TFLite `benchmark_model` (`TfLiteGpuDelegateV2`) — `style_udnie_fp16.tflite` | GPU (OpenCL) | 350 / 350 | 37.4 ms | | TFLite `benchmark_model` (`TfLiteGpuDelegateV2`) — `style_mosaic_fp16.tflite` | GPU (OpenCL) | 350 / 350 | 37.5 ms | | TFLite `benchmark_model` (`TfLiteGpuDelegateV2`) — `style_rain_princess_fp16.tflite` | GPU (OpenCL) | 350 / 350 | 37.7 ms | | TFLite `benchmark_model` (`TfLiteGpuDelegateV2`) — `style_candy_fp16.tflite` | GPU (OpenCL) | 350 / 350 | 37.7 ms | | TFLite `benchmark_model` — `style_udnie_fp16.tflite` | CPU (XNNPACK, 4 threads) | — | 406.0 ms | | TFLite `benchmark_model` — `style_mosaic_fp16.tflite` | CPU (XNNPACK, 4 threads) | — | 404.3 ms | | TFLite `benchmark_model` — `style_rain_princess_fp16.tflite` | CPU (XNNPACK, 4 threads) | — | 404.5 ms | | TFLite `benchmark_model` — `style_candy_fp16.tflite` | CPU (XNNPACK, 4 threads) | — | 402.5 ms | **Any on-device figure recorded when this model shipped came from a different runtime.** It was taken through LiteRT's own `CompiledModel` accelerator (logcat reports it as `LITERT_CL`), which is the path the Kotlin sample app and the LiteRT API use, and it appears elsewhere on this card. The rows above are the classic TFLite OpenCL delegate, measured with a tool anyone can download and re-run. The two are not comparable, so read the rows above as a reproducible floor rather than as this model's speed on LiteRT. ## Snapdragon NPU (Hexagon) - `style_candy_fp16.tflite` — the NPU is **1.87x faster** than the GPU (7.41 ms against 13.88 ms) and loads 5.85x faster (104 ms against 609 ms). - `style_mosaic_fp16.tflite` — the NPU is **1.87x faster** than the GPU (7.41 ms against 13.85 ms) and loads 5.78x faster (106 ms against 610 ms). - `style_rain_princess_fp16.tflite` — the NPU is **1.85x faster** than the GPU (7.48 ms against 13.85 ms) and loads 5.76x faster (105 ms against 603 ms). - `style_udnie_fp16.tflite` — the NPU is **1.86x faster** than the GPU (7.42 ms against 13.76 ms) and loads 5.78x faster (107 ms against 618 ms). | file | backend | compiled | inference (median / min) | load | |---|---|---|---:|---:| | `style_candy_fp16.tflite` | NPU (Hexagon v81) | on-device JIT | 7.41 ms / 7.35 ms | 104 ms | | `style_candy_fp16.tflite` | GPU (Adreno) | — | 13.88 ms / 13.33 ms | 609 ms | | `style_mosaic_fp16.tflite` | NPU (Hexagon v81) | on-device JIT | 7.41 ms / 7.35 ms | 106 ms | | `style_mosaic_fp16.tflite` | GPU (Adreno) | — | 13.85 ms / 13.52 ms | 610 ms | | `style_rain_princess_fp16.tflite` | NPU (Hexagon v81) | on-device JIT | 7.48 ms / 7.35 ms | 105 ms | | `style_rain_princess_fp16.tflite` | GPU (Adreno) | — | 13.85 ms / 13.48 ms | 603 ms | | `style_udnie_fp16.tflite` | NPU (Hexagon v81) | on-device JIT | 7.42 ms / 7.33 ms | 107 ms | | `style_udnie_fp16.tflite` | GPU (Adreno) | — | 13.76 ms / 13.36 ms | 618 ms | 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.68–0.70, where 1.0 is the throttling threshold. **The NPU rows ran the published file unchanged.** LiteRT compiled it for the Hexagon on the device at first load. Those first compiles took 2.8 s to 2.9 s here. The `load` column above is the cached load every later run pays. Recipe and the runtime libraries it needs: [NPU guide](https://github.com/john-rocky/hf-to-litertlm/blob/main/docs/android-npu.md). GPU wiring: [GPU guide](https://github.com/john-rocky/hf-to-litertlm/blob/main/docs/android-gpu.md). ## Raspberry Pi 5 (CPU) Measured on a Raspberry Pi 5 Model B Rev 1.1 (8 GB, Raspberry Pi OS 64-bit) with the LiteRT [`benchmark_model`](https://ai.google.dev/edge/litert/models/measurement) tool from litert-cli-nightly 0.2.0.dev20260805: CPU inference (XNNPACK, 4 threads), 3 invocations per file of 10 warm-up plus 50 timed runs (the tool caps a phase at 150 s, so very slow graphs run fewer — the Runs column is the actual timed total). The latency is the median across invocations; the spread is the min–max over all timed runs. No thermal throttling occurred during these runs (`vcgencmd get_throttled` stayed `0x0`). | File | Inference (median) | Spread (min–max) | Runs | Peak memory | |---|---:|---:|---:|---:| | `style_candy_fp16.tflite` | 287.5 ms | 285.8–289.9 ms | 150 | 190 MB | | `style_mosaic_fp16.tflite` | 288.0 ms | 285.8–290.6 ms | 150 | 190 MB | | `style_rain_princess_fp16.tflite` | 287.6 ms | 286.2–324.6 ms | 150 | 190 MB | | `style_udnie_fp16.tflite` | 287.5 ms | 286.1–292.1 ms | 150 | 190 MB | ## License [BSD-3-Clause](https://github.com/pytorch/examples/blob/main/LICENSE). Upstream: [pytorch/examples](https://github.com/pytorch/examples/tree/main/fast_neural_style).