Gpu Clean Conversion logo

Gpu Clean Conversion

Organization
google-ai-edge
gpu-clean-conversion

Convert a PyTorch or Hugging Face model into a LiteRT model that runs fully on the GPU via the CompiledModel API with verified-correct output, and lay it out as a model recipe. Use when converting a new model, or when a converted model is rejected by the GPU, falls back to CPU, or returns wrong numbers on device.

Overview

Publishergoogle-ai-edge
Repositorylitert-samples
Skill namegpu-clean-conversion
Stars
470
Forks
124
Bundled files
Instructions only
LicenseApache-2.0
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • Self-contained

    Everything the model needs lives in the instructions — no extra files to sync.

  • Open source

    Published by google-ai-edge on GitHub. Read the source before you install it.

Installation

Install the Gpu Clean Conversion AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/google-ai-edge/litert-samples/tree/main/skills/gpu-clean-conversion
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/google-ai-edge/litert-samples.git /tmp/litert-samples
mkdir -p .claude/skills
cp -r /tmp/litert-samples/skills/gpu-clean-conversion .claude/skills/gpu-clean-conversion
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Gpu Clean Conversion in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Gpu Clean Conversion on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Gpu Clean Conversion is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

GPU-clean conversion

A conversion is done when three things hold, in this order:

  1. it converts,
  2. every node runs on the GPU,
  3. the on-device output matches the source model.

Step 3 is not implied by step 2. The delegate can report full residency and still return wrong numbers — that is the failure mode most of the rewrites below exist for. Never call a model done without a numerical check against CPU or PyTorch on the actual device.

Loop

1. Convert plain first. No patches. This tells you what the model actually needs rather than what you assumed.

2. Verify through the CompiledModel API before touching a device.

python
from litert_gpu_toolkit import check_gpu_compatibility, print_report
print_report(check_gpu_compatibility("model.tflite"))

It compiles the model for the GPU accelerator, runs every signature on random inputs, and compares the outputs against a CPU-compiled reference. A failed GPU compile names the offending op — route it through the table below. A pass with a CPU-fallback warning means some ops fell back to the CPU; treat them the same way if you need full residency. This exercises the host GPU, so step 5 on the actual device is still required.

3. Map each symptom to a rewrite. The rewrites live in utilities/litert_gpu_toolkit — plain Python, no build step. Outside this repo, clone litert-samples and put utilities/ on PYTHONPATH, or vendor the directory:

What you seeRewrite
GATHER_ND named in the compile errorFind its source. Stride-2 slicing (x[:, ::2], Focus stems, patch merging), grid_sample, MaxPool padding, bicubic interpolate, and reflect-mode F.pad all lower to it. patch_grid_sample, patch_maxpool_zeropad, patch_interpolate, patch_patch_merging
A rank-5+ tensor named in the compile errorPixelShuffle (rank-6 reshape), windowed attention, einops.rearrange, packed-QKV attention head splits. pixelshuffle_to_conv_transpose, patch_window_attention, patch_einops
TRANSPOSE_CONV rejectedVersion skew, not a missing op. ZeroStuffConvT1d / ZeroStuffConvT2d — zero-stuff plus a plain conv, exact to ~1e-7
SELECT / SELECT_V2PReLU, ELU, in-place index assignment, and torch.where masking. Replace with arithmetic: x*(1-m) + v*m. Keep v finite in fp16: −1e9 is −inf there, and (1 − m) × −1e9 is 0 × −inf = NaN on every valid position; −30000 is exact in fp16 and gives the same softmax in fp32 when the logits are bounded
BROADCAST_TOTwo cases. On a compile-time constant: an outer product or .expand that did not fold — bake the result as a constant at its target shape. On a runtime tensor the GPU delegate rejects it outright, even at rank 4 — the canonical case is GQA's repeat_kv (x[:,:,None].expand(...), which is also rank-5, so two walls in one line). Exact rewrite: torch.cat([x[:, i:i+1].expand(b, n_rep, s, d) for i in range(n_kv)], dim=1) — same head order, bit-exact. Tracked upstream: google-ai-edge/LiteRT#9191
Masked attention wrong only on device: token 0 bit-exact, every later token wrongBroadcast ADD whose LHS is a BATCH_MATMUL result (the scores + mask[1,1,S,S] idiom) silently miscomputed on ai-edge-litert 2.1.3 and 2.1.5 and is fixed in 2.1.6 (head-axis size-1 broadcast only; google-ai-edge/LiteRT#8593, closed as fixed; the 2.1.6 release notes list GPU broadcast fixes). The signature mimics broken RoPE — tap the rope output before blaming it. Rewrites on the older runtimes: pre-expand the mask to [1,H,S,S], or materialize the BMM as a second output
An ADD result that is both a graph output and consumed downstream comes back wrongOutput aliasing: the returned tensor holds an operand, not the sum — ADD with two runtime operands (SUB/MUL exact, x + 1.0 exact). This is the shape of every explicit state update in a streaming/recurrent graph. Workaround: emit acc * one where one is a runtime input holding 1.0 — a constant folds straight back into the pattern. Tracked upstream: google-ai-edge/LiteRT#8599
RELU_0_TO_1 rejected by the GPU delegateEmitted by hard-sigmoid / nn.Hardtanh(0,1). Accepted in litert 2.1.3, rejected from 2.1.5 on — a model at full residency on an older runtime hard-fails CompiledModel creation after an upgrade. Rewrite: relu(x) - relu(x-1), exact. Tracked upstream: google-ai-edge/LiteRT#8598
DIV: No support of few identical inputs / Expected 1 const input tensor(s), device onlyThe delegate declines an op whose two inputs are the same tensor, and ops whose inputs are all constants — together these split a perceiver-style block (softmax over a length-1 axis of a constant latent bank) into several partitions. Fixes: special-case the degenerate axis (a softmax over a length-1 axis is identically 1), or make one input non-constant. Note the sibling LayerNorm-over-a-constant pattern no longer reaches the delegate at all — the converter folds it to a single MUL. Tracked upstream: google-ai-edge/LiteRT#9192
NHWC node rewriter not found: amaxx.amax(...)/x.max(dim) in stable-softmax, adaptive norms, qk-norm. Rewrite channel reduce-max as max_pool2d(x.reshape(N,1,C,H*W), kernel=(C,1)) — numerically identical — or drop the norm to 3D
Lowering not found: aten._fft_r2c / aten.complextorch.stft/istft and complex views have no lowering (fails before any GPU check). A DFT is a fixed linear map: windowed-DFT as Conv1d with the cos/sin basis baked into kernels (stride = hop), iSTFT as inverse-DFT matmul + overlap-add via zero-stuffed conv-transpose — exact. Model-selection corollary: prefer time-domain vocoder branches over iSTFT-based ones. ⚠ Library STFT-as-conv stacks (torchlibrosa-style) have numerically mis-converted (corr 0.83) while the op check looks clean — verify the spectrogram numerically or compute log-mel host-side
Compiles, runs, output is wrong or NaNThe fp16 reduction family — and the trigger is the fp16 accumulator passing 65504, so a plain single-axis mean/sum over enough elements overflows just like variance does. patch_safe_layernorm, patch_rmsnorm, patch_instance_norm, hierarchical_mean. Caveats: at extreme magnitudes (|x| in the thousands) even the adaptive safe-LN form overflows when it reconstructs the large variance — the robust form stays entirely in the down-scaled domain (xs = x/S, normalize xs, never multiply the variance back by S²); hierarchical_mean is exact only for power-of-two spatial dims (for arbitrary dims, cascade /2 avg-pools with ceil_mode so each stage averages ≤~49 elements). A static form keeps fp32 bit-exact and puts no reduce-max in the graph: scale each norm's input by a power of two 2^-k chosen per site from a calibration over the whole check set (headroom for the row's sum of squares and for the largest single x² — both overflowed in the case on record, the mean of squares did not), and eps by 4^-k. When eps·4^-k drops out of fp16's range (eps 1e-6 at k = 3), an all-zero row (padding) becomes rsqrt(0) = inf and 0 × inf = NaN: clamp the rsqrt at 2^k/√eps — the zero row's own value, never reached in fp32. Diagnostic split: all-zero/all-blank output = an overflow in one block; a result that starts near-correct and degrades with depth = precision compounding, which no overflow patch (and no fp32-precision flag) fixes
Head outputs exactly zeroRMSNorm Σx² overflowed fp16 to inf. patch_rmsnorm
GPU compile fails only at fp16 precision with a kernel-source error (write_imageh on Adreno and Mali OpenCL, write on Metal, at the same source line), while fp32 precision compilesA boolean value on the way to a graph output. In the case on record an output mask computed as (mask > 0.5) and cast back to float made all three GPUs fail; the same output computed from the float input mask by float ops (a strided slice) compiled at fp16 everywhere and stayed bit-exact in fp32. Small graphs with the same GREATER → CAST pair compiled, so bisect on the real graph. Keep masks float (1 = valid) from input to output

4. Re-convert and re-verify. Repeat 2–3 until the check passes.

5. Verify on device, numerically. Run the same input through the source model and the on-device graph and compare. Correlation on the output tensor, plus a task-level check where one exists (argmax match, IoU, mask foreground count). Record the device and the residency line (N/N nodes) alongside the number — a result without both is not reproducible.

If residency is full and the output is wrong, bisect by intermediate: dump tensors at block boundaries and find the first one that diverges. The cause is usually a reduction, not the op you suspect.

Watch for

  • fp16 is used even for an fp32 graph. The delegate reduces in fp16 regardless of tensor dtype by default (the GPU options carry a precision setting — CompiledModel.GpuOptions(precision = …) in Kotlin — that can request fp32 at a speed cost). Anything that sums many large values — variance, Σx², multi-axis mean — is a candidate.
  • Approximation choices are not free. Substituting a GELU or Swish approximation changes numerics. A head with a wide output range can lose real accuracy to the coarser form.
  • inplace=True activations mutate a residual. out = self.act(x) then x + out adds relu(x), not x. Swapping the activation silently changes the math.
  • A silent CPU fallback looks like success. If the GPU output is bit-identical to CPU fp32, it probably did not run on the GPU. Genuine fp16 execution drifts in the last digits. The runtime's own count is the line N operations will run on the GPU, and the remaining M operations will run on the CPU.; a node the delegate declines while building (TopK requires src tensor C dimension to be divisible by 4) leaves only that log behind — the checker's report can read fully-on-GPU while the op ran on the CPU (ai-edge-litert 2.2.0 and 2.3.0).
  • Reflect padding routes through F.pad even at padding=0 — every conv with padding_mode='reflect' is affected, not just padded ones. Slice+concat reflect pads are cheap for small pad widths.
  • Channel-attention through a Linear confuses layout handling at convert time (tfl.mul operands don't have broadcast-compatible shapes) — express channel attention as a 1×1 conv.
  • .chunk() lowers to SPLIT (GPU-rejected) — slice directly instead; bit-exact.
  • Constant folding can explode the file: a frozen-param × constant product materializes at full size per block (fp16 casting skips non-weight constants, so it cannot rescue it). Feed one factor as a runtime input — param × input never folds.

Output layout

Ship the result as a model recipe, not a one-off script:

models/<family>/<model>/
  README.md                 what the model is, what was produced
  converted/
    README.md               environments, pipeline, verification results
    export_*.py             one script per graph
    dump_*_ref.py           reference dumps from the source model
    verify_*.py             parity check against those references

Keep the export, the reference dump, and the verification separate so each can be re-run alone. State the verification numbers in the README with the device they came from. Weights are not committed.

The tree above is this repo's convention. In your own project the directory names matter less than the split — keep the same three separately-runnable pieces wherever your models live.

Do the model first. A demo can follow, and reusable inference code matters more in it than a full UI.

Frequently asked questions

What does the Gpu Clean Conversion AI skill do?

Convert a PyTorch or Hugging Face model into a LiteRT model that runs fully on the GPU via the CompiledModel API with verified-correct output, and lay it out as a model recipe. Use when converting a new model, or when a converted model is rejected by the GPU, falls back to CPU, or returns wrong numbers on device.

Why use Gpu Clean Conversion on TypingMind?

Because you install it once and use it with any model. Gpu Clean Conversion is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Gpu Clean Conversion in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/google-ai-edge/litert-samples/tree/main/skills/gpu-clean-conversion. TypingMind reads its SKILL.md and installs it as a skill you can enable per chat.

Which AI models can use Gpu Clean Conversion?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Gpu Clean Conversion?

As many as you like. As long as a model supports skills, you can use Gpu Clean Conversion with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Gpu Clean Conversion AI skill free?

Yes. It is published on GitHub by google-ai-edge under the Apache-2.0 license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇