--- license: mit library_name: litert pipeline_tag: image-segmentation tags: - litert - tflite - android - on-device - gpu - lane-detection - adas - autonomous-driving - real-time --- LiteRT is Google's on-device runtime, the new name for TensorFlow Lite (Android: `com.google.ai.edge.litert:litert`), and `litert-torch`, the renamed `ai-edge-torch`, is its PyTorch converter: a PyTorch model converted unmodified with `litert_torch.convert` matched the original to 4e-7 on a Galaxy S26 ([measured](https://github.com/john-rocky/apple-silicon-llm-bench/blob/main/results/android/tinyhybridnet-three-runtimes.md), LiteRT 2.2.0, Android 16, 2026-09-05). Measured on device (edge-compat): Raspberry Pi 5 · LiteRT 2.2.0.dev20260804 · CPU/XNNPACK, 4 threads · 151 ms p50 (2026-08-31); browser · Chromium 151 on M4 Max · LiteRT.js 2.5.3 · WebGPU · 7.46 ms p50 · output matches CPU (2026-08-11). Record: https://github.com/john-rocky/edge-compat/blob/main/cards/ultra-fast-lane-detection/CARD.md # Ultra-Fast-Lane-Detection (ResNet18, CULane) — LiteRT GPU On-device **lane detection** running **fully on the LiteRT `CompiledModel` GPU** delegate (no CPU fallback). [Ultra-Fast-Lane-Detection](https://github.com/cfzd/Ultra-Fast-Lane-Detection) (ECCV 2020) reformulates lane detection as fast **row-wise classification** — the network runs on the GPU, and a tiny host-side arg/expectation decode turns the grid into lane points. ~20 ms/frame on a Pixel 8a. - **Architecture:** ResNet18 backbone + row-anchor classification head — pure CNN. - **Weights:** [cfzd/Ultra-Fast-Lane-Detection](https://github.com/cfzd/Ultra-Fast-Lane-Detection) (CULane, ResNet18) · MIT. - **Size:** 178 MB. ![Ultra-Fast-Lane-Detection](hero.png) *Detected ego-lane on a dashcam highway frame. Source: Wikimedia Commons (Public Domain).* ## I/O - **Input:** `[1, 3, 288, 800]` NCHW, RGB, `x/255` then ImageNet-normalized (mean `[0.485,0.456,0.406]`, std `[0.229,0.224,0.225]`). - **Output:** `[1, 201, 18, 4]` = `(griding+1, row_anchors, lanes)` — per-lane, per-row classification logits over 200 horizontal grid cells (+1 "no lane"). ## Host-side decode For each of the 4 lanes and 18 row anchors: softmax over the 200 grid cells, take the expectation → column; if the argmax over all 201 is the last index (200 = "no lane"), drop it. Map the column to an x-pixel via `linspace(0, 799, 200)` (scaled to the image width) and the row anchor to a y-pixel (CULane row anchors, scaled from 288). ## GPU conversion UFLD is a pure CNN. It converts fully GPU-compatible (**41/41 nodes on the delegate, 1 partition**; device corr 0.999982, ~20 ms) with **one patch**: the ResNet18 stem `MaxPool2d(padding=1)` lowers to a `-inf` PADV2 (rejected by Mali), replaced by a 0-pad + unpadded maxpool (exact post-ReLU). CPU-exact vs PyTorch (corr 0.9999999999996). ## Minimal usage ### Kotlin (Android, LiteRT CompiledModel GPU) ```kotlin val options = CompiledModel.Options(Accelerator.GPU) val model = CompiledModel.create(context.assets, "ufld.tflite", options, null) val inBufs = model.createInputBuffers() val outBufs = model.createOutputBuffers() inBufs[0].writeFloat(inputNCHW) // [1,3,288,800] RGB, x/255 then ImageNet-norm model.run(inBufs, outBufs) val out = outBufs[0].readFloat() // [201*18*4], layout (griding+1, rows, lanes) // decode: per (lane,row) softmax over the first 200 cells, take the expectation -> column; // skip if argmax == 200 (no lane). See LaneDetector.kt for the full decode. ``` ### Python (LiteRT / ai-edge-litert) ```python import numpy as np from ai_edge_litert.interpreter import Interpreter it = Interpreter(model_path="ufld.tflite"); it.allocate_tensors() inp, out = it.get_input_details(), it.get_output_details() it.set_tensor(inp[0]["index"], x) # [1,3,288,800] float32, RGB /255, ImageNet-norm it.invoke() o = it.get_tensor(out[0]["index"])[0] # [201,18,4] o = o[:, ::-1, :] prob = np.exp(o[:-1]) / np.exp(o[:-1]).sum(0, keepdims=True) loc = (prob * (np.arange(200) + 1).reshape(-1, 1, 1)).sum(0) # [18,4] columns loc[np.argmax(o, 0) == 200] = 0 # 0 = no lane ``` ## Conversion Converted with **litert-torch** (`build_ufld.py`): loads the ResNet18 CULane weights and exports the row-classification graph. ## 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 | |---|---|---|---| | LiteRT `CompiledModel` (`LITERT_CL`) | GPU | 41 / 41 | ~20 ms | | TFLite `benchmark_model` (`TfLiteGpuDelegateV2`) | GPU (OpenCL) | 41 / 41 | 25.3 ms | | TFLite `benchmark_model` | CPU (XNNPACK, 4 threads) | — | 252.3 ms | **The two GPU rows are different runtimes, not a contradiction.** The `LITERT_CL` figure is the one recorded when this model shipped, taken through LiteRT's own `CompiledModel` accelerator — the path the Kotlin sample app and the LiteRT API use. The `TfLiteGpuDelegateV2` figure is the classic TFLite OpenCL delegate, measured with a tool anyone can download and re-run. They agree on how much of the graph the GPU takes; they disagree on speed, and the classic delegate is the slower of the two here. Read the `TfLiteGpuDelegateV2` row as a reproducible floor, not as this model's speed on LiteRT. ## Snapdragon NPU (Hexagon) The NPU is **2.20x faster** than the GPU (2.96 ms against 6.51 ms) and loads 7.44x faster (113 ms against 840 ms). | backend | inference (median / min) | load | |---|---:|---:| | NPU (Hexagon v81) | 2.96 ms / 2.92 ms | 113 ms | | GPU (Adreno) | 6.51 ms / 6.22 ms | 840 ms | Measured on a **Samsung Galaxy S26** (Snapdragon 8 Elite Gen 5 / SM8850, Hexagon v81, Android 16), 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.66-0.67, where 1.0 is the throttling threshold. The NPU rows here ran artifacts compiled ahead of time for SM8850 with QAIRT 2.47.0; the GPU rows ran the published files as they are. LiteRT can also compile for the NPU on the device at first load, which is what lets you ship the published file unchanged — that path and the ten runtime libraries it needs are in the [NPU recipe](https://github.com/john-rocky/hf-to-litertlm/blob/main/docs/android-npu.md), and we did not measure it here. GPU wiring is in the [GPU recipe](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 | |---|---:|---:|---:|---:| | `ufld.tflite` | 150.8 ms | 150.4–163.7 ms | 150 | 417 MB | ## License MIT (Ultra-Fast-Lane-Detection / cfzd). Trained on CULane.