Edge ai

From j4flmao/agent-skills · View interactive version

Edge ai is an AI agent skill from the GitHub repository j4flmao/agent-skills. Use when the user asks about edge AI, on-device inference, TensorRT on Jetson, ONNX Runtime, LiteRT (TFLite), TinyML, int8 quantization, Coral, NPUs or ExecuTorch. Produces export, quantization and on-device benchmarking plans. Not for model training (use ml-deep-learning) or cloud LLM serving (use

How to use this skill

Skill file

Edge AI Deployment

Purpose

A model that works in a notebook still has to fit the device's memory, latency, power and operator support. This skill picks the runtime for the target hardware, converts and quantizes the model without silently losing accuracy, and measures it on the device.

Agent Protocol

Trigger

"edge AI", "on-device inference", "TensorRT", "ONNX Runtime", "TFLite", "LiteRT", "TinyML", "int8 quantization", "Jetson", "Coral", "NPU", "ExecuTorch".

Input Context

Output Artifact

Export/convert/quantize scripts, runtime integration code, benchmark plan. No file unless requested.

Response Format

Target: {hardware + OS}
Path: {framework -> format -> runtime/EP}
Precision: {FP32 | FP16 | INT8 (PTQ/QAT) | INT4}
Calibration: {dataset, size}
Result: {latency p50/p99, memory, accuracy delta vs FP32}
Risks: {unsupported ops, fallback to CPU, accuracy drop}

1. Choose the Runtime by Hardware

| Hardware | Typical runtime | Notes |

|----------|-----------------|-------|

| NVIDIA Jetson / dGPU | TensorRT (or ONNX Runtime with TensorRT/CUDA EP) | Engines are built per GPU + TensorRT version; rebuild on upgrade |

| Android phones | LiteRT (formerly TensorFlow Lite) with GPU/NNAPI delegates, or vendor SDKs (Qualcomm AI Engine Direct/QNN) | NNAPI is deprecated in Android 15; prefer GPU delegate or vendor SDK |

| iOS / Apple silicon | Core ML (convert with coremltools) | ANE used automatically for supported layers |

| Linux Cortex-A CPU | ONNX Runtime (CPU EP / XNNPACK), LiteRT + XNNPACK | NEON-optimised kernels |

| Microcontrollers | LiteRT for Microcontrollers (TFLM), CMSIS-NN kernels, vendor tools (STM32Cube.AI, Edge Impulse) | INT8 only in practice; static tensor arena |

| Dedicated NPUs (Hailo, Coral Edge TPU, NXP eIQ, Rockchip) | Vendor compiler + runtime | Coral needs fully INT8 models compiled with edgetpu_compiler |

| PyTorch-first mobile/embedded | ExecuTorch | Backends: XNNPACK, Core ML, Vulkan, Qualcomm, Arm Ethos-U |

ONNX is the usual interchange format: one export, then hardware-specific runtimes or Execution Providers (EPs).

2. Export

import torch

model.eval()
example = torch.randn(1, 3, 224, 224)
torch.onnx.export(
    model, (example,), "model.onnx",
    input_names=["input"], output_names=["logits"],
    dynamic_axes={"input": {0: "batch"}, "logits": {0: "batch"}},
    opset_version=17,
)

3. Quantization

| Method | Effort | Accuracy | Use |

|--------|--------|----------|-----|

| FP16 | none | ~unchanged | GPUs/NPUs with FP16 |

| Dynamic INT8 (weights only, activations at runtime) | low | small drop | CPU, transformer/LSTM layers |

| Static PTQ INT8 (calibrated activations) | medium | small drop if calibrated well | CNNs on NPUs/MCUs/TensorRT |

| QAT (quantization-aware training) | high | best INT8 | when PTQ loses too much |

| INT4 weight-only | medium | model dependent | LLMs/large models on capable runtimes |

ONNX Runtime static quantization sketch:

from onnxruntime.quantization import quantize_static, CalibrationDataReader, QuantFormat, QuantType

class Reader(CalibrationDataReader):
    def __init__(self, batches): self._it = iter(batches)
    def get_next(self): return next(self._it, None)   # dict {"input": np.ndarray} or None

quantize_static("model.onnx", "model.int8.onnx", Reader(calib_batches),
                quant_format=QuantFormat.QDQ,
                activation_type=QuantType.QInt8, weight_type=QuantType.QInt8,
                per_channel=True)

4. TensorRT Build

trtexec --onnx=model.onnx --saveEngine=model.fp16.plan --fp16
# INT8 needs calibration data or a QDQ (explicitly quantized) ONNX model
trtexec --onnx=model.int8.onnx --saveEngine=model.int8.plan --int8

TensorRT fuses layers (conv+bias+activation), selects kernels per GPU and can use FP16/INT8. Engines are not portable across GPU models or TensorRT versions. On Jetson use the JetPack-matched TensorRT and power mode (nvpmodel, jetson_clocks) when benchmarking.

5. Microcontrollers (TinyML)

6. Benchmarking on Device

7. Anti-Patterns

8. Checklist

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.