Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
238 changes: 238 additions & 0 deletions test_scripts/litert_cli.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
{
"cells": [
{
"id": "90c3deda",
"cell_type": "markdown",
"source": [
"# LiteRT CLI Demo\n",
"\n",
"This notebook demonstrates how to use the `litert-cli` tool to convert a PyTorch model, quantize it and run it."
],
"metadata": {}
},
{
"id": "19270446",
"cell_type": "markdown",
"source": [
"## \ud83d\udee0\ufe0f 1. Environment Setup & Installation"
],
"metadata": {}
},
{
"id": "d34739df",
"cell_type": "code",
"source": [
"# Install required packages\n",
"!pip install torch torchvision\n",
"\n",
"# Install litert-cli\n",
"!pip install -q -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple litert-cli==0.1.1.dev23\n",
"\n",
"# 'litert compile' depends on Clang, and below make sure your Clang has version `18.x.x` or above\n",
"!wget https://apt.llvm.org/llvm.sh\n",
"!chmod +x llvm.sh\n",
"!sudo ./llvm.sh 18 all"
],
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"id": "1dd1eb34",
"cell_type": "markdown",
"source": [
"## \ud83d\udcdd 2. Prepare PyTorch Model Script"
],
"metadata": {}
},
{
"id": "5e40f66c",
"cell_type": "code",
"source": [
"%%writefile resnet18.py\n",
"import torch\n",
"import torchvision\n",
"\n",
"def get_model() -> torch.nn.Module:\n",
" model = torchvision.models.resnet18(\n",
" weights=torchvision.models.ResNet18_Weights.IMAGENET1K_V1\n",
" )\n",
" model.eval()\n",
" return model\n",
"\n",
"def get_args() -> tuple[torch.Tensor, ...]:\n",
" return (torch.randn(1, 3, 224, 224),)"
],
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"id": "427dca8a",
"cell_type": "markdown",
"source": [
"## \ud83d\udd04 3. Model Conversion (PyTorch -> LiteRT)"
],
"metadata": {}
},
{
"id": "578cc653",
"cell_type": "code",
"source": [
"# Convert PyTorch ResNet18 model to LiteRT\n",
"!litert convert resnet18.py --output resnet18"
],
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"id": "606efd4f",
"cell_type": "markdown",
"source": [
"## \ud83d\udcc9 4. Model Quantization"
],
"metadata": {}
},
{
"id": "c589c711",
"cell_type": "code",
"source": [
"# Quantize the ResNet18 model\n",
"!litert quantize resnet18/resnet18.tflite --type int8_dynamic --output resnet18/resnet18_int8_dynamic.tflite\n",
"!litert quantize resnet18/resnet18.tflite --type int8_weight_only --output resnet18/resnet18_int8_weight_only.tflite"
],
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"id": "b3ae9ddc",
"cell_type": "markdown",
"source": [
"## \ud83d\ude80 5. Run Inference\n",
"### \ud83d\udda5\ufe0f 5.1 CPU Inference"
],
"metadata": {}
},
{
"id": "ea93a1db",
"cell_type": "code",
"source": [
"# Run Inference on Desktop (Colab VM CPU)\n",
"!litert run resnet18/resnet18.tflite --desktop --cpu --iterations 1\n",
"!litert run resnet18/resnet18_int8_dynamic.tflite --desktop --cpu --iterations 1"
],
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"id": "9d748475",
"cell_type": "markdown",
"source": [
"### \ud83c\udfae 5.2 GPU Inference"
],
"metadata": {}
},
{
"id": "00b026b2",
"cell_type": "code",
"source": [
"# Run Inference on Desktop (Colab VM GPU)\n",
"!litert run resnet18/resnet18.tflite --desktop --gpu --iterations 1\n",
"!litert run resnet18/resnet18_int8_dynamic.tflite --desktop --gpu --iterations 1"
],
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"id": "eaa972bc",
"cell_type": "markdown",
"source": [
"## \u2699\ufe0f 6. NPU Offline Compilation (AOT)"
],
"metadata": {}
},
{
"id": "d0a61181",
"cell_type": "code",
"source": [
"# Compile the model for qualcomm NPU: SM8750\n",
"# TIP: Only support running on Linux and it might take a few minutes, given download large SDKs from SOCs.\n",
"# TIP: It depends on Clang, and please make sure your Clang has version `18.x.x` or above when running\n",
"# `clang --version`. To update, choose one of below: \n",
"# a) `sudo apt update && sudo apt upgrade -y`\n",
"# b) wget https://apt.llvm.org/llvm.sh\n",
"# chmod +x llvm.sh\n",
"# sudo ./llvm.sh 18 all\n",
"#\n",
"!litert compile resnet18/resnet18.tflite --target sm8750"
],
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"id": "2da953c9",
"cell_type": "markdown",
"source": [
"## \ud83c\udfc1 7. Benchmark in Google AI Edge Portal"
],
"metadata": {}
},
{
"id": "a9a19e48",
"cell_type": "code",
"source": [
"# Please login into Google Cloud and make sure you have joined the EAP for Google AI Edge Portal\n",
"# Check: https://ai.google.dev/edge/ai-edge-portal\n",
"!pip install gcloud\n",
"!gcloud auth login\n",
"\n",
"# Benchmark on Google AI Edge Portal\n",
"# Please specify you own GCP project: --gcp-project <your-own-gcp-project-id>\n",
"# Or set a default environment variable: LITERT_GCP_PROJECT\n",
"!litert benchmark model.tflite --gcp --device \"pixel 7\" --cpu --gcp-project aep-e2e-test\n",
"!litert benchmark model.tflite --gcp --device \"pixel 7\" --gpu --gcp-project aep-e2e-test\n",
"!litert benchmark model.tflite --gcp --devices \"pixel 7, sm-s931u1\" --gpu"
],
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"id": "2685b076",
"cell_type": "markdown",
"source": [
"## Advanced Usage\n",
"\n",
"### Android Deployment\n",
"To run or benchmark on Android, you need an ADB connected device. These commands typically look like:\n",
"```bash\n",
"# Run on Android\n",
"!litert run resnet18/resnet18.tflite --android --cpu\n",
"\n",
"# Benchmark on Android\n",
"!litert benchmark resnet18/resnet18.tflite --android\n",
"```\n",
"These are not executed in this notebook as Colab does not have access to a physical Android device by default."
],
"metadata": {}
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.13.12"
}
},
"nbformat_minor": 5,
"nbformat": 4
}
5 changes: 2 additions & 3 deletions test_scripts/models/efficientnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,8 @@ fi


# --- 5. Compile (AOT Compilation) ---
# TODO: Add this back when we fix the NPU compile issue.
# run_case "Compile: EfficientNet FP32 for Qualcomm sm8750 NPU" \
# litert compile "$EFFICIENTNET_TFLITE" --target sm8750 --output-dir "$MODEL_DIR/efficientnet"
run_case "Compile: EfficientNet FP32 for Qualcomm sm8750 NPU" \
litert compile "$EFFICIENTNET_TFLITE" --target sm8750 --output-dir "$MODEL_DIR/efficientnet"

# --- 6. Visualize (Model Explorer) ---
run_case "Visualize: Launch Model Explorer in the background" \
Expand Down
5 changes: 2 additions & 3 deletions test_scripts/models/resnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,8 @@ fi


# --- 5. Compile (AOT Compilation) ---
# TODO: Add this back when we fix the NPU compile issue.
# run_case "Compile: ResNet18 FP32 for Qualcomm sm8750 NPU" \
# litert compile "$RESNET_TFLITE" --target sm8750 --output-dir "$MODEL_DIR/resnet18"
run_case "Compile: ResNet18 FP32 for Qualcomm sm8750 NPU" \
litert compile "$RESNET_TFLITE" --target sm8750 --output-dir "$MODEL_DIR/resnet18"

# --- 6. Visualize (Model Explorer) ---
run_case "Visualize: Launch Model Explorer in the background" \
Expand Down
Loading