|
1 | 1 | { |
2 | 2 | "cells": [ |
| 3 | + { |
| 4 | + "id": "cdbfc46c", |
| 5 | + "cell_type": "raw", |
| 6 | + "source": [ |
| 7 | + "# Copyright 2026 The LiteRT CLI Authors.\n", |
| 8 | + "#\n", |
| 9 | + "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", |
| 10 | + "# you may not use this file except in compliance with the License.\n", |
| 11 | + "# You may obtain a copy of the License at\n", |
| 12 | + "#\n", |
| 13 | + "# http://www.apache.org/licenses/LICENSE-2.0\n", |
| 14 | + "#\n", |
| 15 | + "# Unless required by applicable law or agreed to in writing, software\n", |
| 16 | + "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", |
| 17 | + "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", |
| 18 | + "# See the License for the specific language governing permissions and\n", |
| 19 | + "# limitations under the License.\n", |
| 20 | + "# ==============================================================================" |
| 21 | + ], |
| 22 | + "metadata": {}, |
| 23 | + "execution_count": null |
| 24 | + }, |
3 | 25 | { |
4 | 26 | "id": "90c3deda", |
5 | 27 | "cell_type": "markdown", |
|
8 | 30 | "\n", |
9 | 31 | "This notebook demonstrates how to use the `litert-cli` tool to convert a PyTorch model, quantize it and run it." |
10 | 32 | ], |
11 | | - "metadata": {} |
| 33 | + "metadata": {}, |
| 34 | + "execution_count": null |
12 | 35 | }, |
13 | 36 | { |
14 | 37 | "id": "19270446", |
15 | 38 | "cell_type": "markdown", |
16 | 39 | "source": [ |
17 | | - "## \ud83d\udee0\ufe0f 1. Environment Setup & Installation" |
| 40 | + "## 🛠️ 1. Environment Setup \u0026 Installation" |
18 | 41 | ], |
19 | | - "metadata": {} |
| 42 | + "metadata": {}, |
| 43 | + "execution_count": null |
20 | 44 | }, |
21 | 45 | { |
22 | 46 | "id": "d34739df", |
|
34 | 58 | "!sudo ./llvm.sh 18 all" |
35 | 59 | ], |
36 | 60 | "metadata": {}, |
37 | | - "execution_count": null, |
38 | | - "outputs": [] |
| 61 | + "execution_count": null |
39 | 62 | }, |
40 | 63 | { |
41 | 64 | "id": "1dd1eb34", |
42 | 65 | "cell_type": "markdown", |
43 | 66 | "source": [ |
44 | | - "## \ud83d\udcdd 2. Prepare PyTorch Model Script" |
| 67 | + "## 📝 2. Prepare PyTorch Model Script" |
45 | 68 | ], |
46 | | - "metadata": {} |
| 69 | + "metadata": {}, |
| 70 | + "execution_count": null |
47 | 71 | }, |
48 | 72 | { |
49 | 73 | "id": "5e40f66c", |
|
53 | 77 | "import torch\n", |
54 | 78 | "import torchvision\n", |
55 | 79 | "\n", |
56 | | - "def get_model() -> torch.nn.Module:\n", |
| 80 | + "def get_model() -\u003e torch.nn.Module:\n", |
57 | 81 | " model = torchvision.models.resnet18(\n", |
58 | 82 | " weights=torchvision.models.ResNet18_Weights.IMAGENET1K_V1\n", |
59 | 83 | " )\n", |
60 | 84 | " model.eval()\n", |
61 | 85 | " return model\n", |
62 | 86 | "\n", |
63 | | - "def get_args() -> tuple[torch.Tensor, ...]:\n", |
| 87 | + "def get_args() -\u003e tuple[torch.Tensor, ...]:\n", |
64 | 88 | " return (torch.randn(1, 3, 224, 224),)" |
65 | 89 | ], |
66 | 90 | "metadata": {}, |
67 | | - "execution_count": null, |
68 | | - "outputs": [] |
| 91 | + "execution_count": null |
69 | 92 | }, |
70 | 93 | { |
71 | 94 | "id": "427dca8a", |
72 | 95 | "cell_type": "markdown", |
73 | 96 | "source": [ |
74 | | - "## \ud83d\udd04 3. Model Conversion (PyTorch -> LiteRT)" |
| 97 | + "## 🔄 3. Model Conversion (PyTorch -\u003e LiteRT)" |
75 | 98 | ], |
76 | | - "metadata": {} |
| 99 | + "metadata": {}, |
| 100 | + "execution_count": null |
77 | 101 | }, |
78 | 102 | { |
79 | 103 | "id": "578cc653", |
|
83 | 107 | "!litert convert resnet18.py --output resnet18" |
84 | 108 | ], |
85 | 109 | "metadata": {}, |
86 | | - "execution_count": null, |
87 | | - "outputs": [] |
| 110 | + "execution_count": null |
88 | 111 | }, |
89 | 112 | { |
90 | 113 | "id": "606efd4f", |
91 | 114 | "cell_type": "markdown", |
92 | 115 | "source": [ |
93 | | - "## \ud83d\udcc9 4. Model Quantization" |
| 116 | + "## 📉 4. Model Quantization" |
94 | 117 | ], |
95 | | - "metadata": {} |
| 118 | + "metadata": {}, |
| 119 | + "execution_count": null |
96 | 120 | }, |
97 | 121 | { |
98 | 122 | "id": "c589c711", |
|
103 | 127 | "!litert quantize resnet18/resnet18.tflite --type int8_weight_only --output resnet18/resnet18_int8_weight_only.tflite" |
104 | 128 | ], |
105 | 129 | "metadata": {}, |
106 | | - "execution_count": null, |
107 | | - "outputs": [] |
| 130 | + "execution_count": null |
108 | 131 | }, |
109 | 132 | { |
110 | 133 | "id": "b3ae9ddc", |
111 | 134 | "cell_type": "markdown", |
112 | 135 | "source": [ |
113 | | - "## \ud83d\ude80 5. Run Inference\n", |
114 | | - "### \ud83d\udda5\ufe0f 5.1 CPU Inference" |
| 136 | + "## 🚀 5. Run Inference\n", |
| 137 | + "### 🖥️ 5.1 CPU Inference" |
115 | 138 | ], |
116 | | - "metadata": {} |
| 139 | + "metadata": {}, |
| 140 | + "execution_count": null |
117 | 141 | }, |
118 | 142 | { |
119 | 143 | "id": "ea93a1db", |
|
124 | 148 | "!litert run resnet18/resnet18_int8_dynamic.tflite --desktop --cpu --iterations 1" |
125 | 149 | ], |
126 | 150 | "metadata": {}, |
127 | | - "execution_count": null, |
128 | | - "outputs": [] |
| 151 | + "execution_count": null |
129 | 152 | }, |
130 | 153 | { |
131 | 154 | "id": "9d748475", |
132 | 155 | "cell_type": "markdown", |
133 | 156 | "source": [ |
134 | | - "### \ud83c\udfae 5.2 GPU Inference" |
| 157 | + "### 🎮 5.2 GPU Inference" |
135 | 158 | ], |
136 | | - "metadata": {} |
| 159 | + "metadata": {}, |
| 160 | + "execution_count": null |
137 | 161 | }, |
138 | 162 | { |
139 | 163 | "id": "00b026b2", |
|
144 | 168 | "!litert run resnet18/resnet18_int8_dynamic.tflite --desktop --gpu --iterations 1" |
145 | 169 | ], |
146 | 170 | "metadata": {}, |
147 | | - "execution_count": null, |
148 | | - "outputs": [] |
| 171 | + "execution_count": null |
149 | 172 | }, |
150 | 173 | { |
151 | 174 | "id": "eaa972bc", |
152 | 175 | "cell_type": "markdown", |
153 | 176 | "source": [ |
154 | | - "## \u2699\ufe0f 6. NPU Offline Compilation (AOT)" |
| 177 | + "## ⚙️ 6. NPU Offline Compilation (AOT)" |
155 | 178 | ], |
156 | | - "metadata": {} |
| 179 | + "metadata": {}, |
| 180 | + "execution_count": null |
157 | 181 | }, |
158 | 182 | { |
159 | 183 | "id": "d0a61181", |
|
163 | 187 | "# TIP: Only support running on Linux and it might take a few minutes, given download large SDKs from SOCs.\n", |
164 | 188 | "# TIP: It depends on Clang, and please make sure your Clang has version `18.x.x` or above when running\n", |
165 | 189 | "# `clang --version`. To update, choose one of below: \n", |
166 | | - "# a) `sudo apt update && sudo apt upgrade -y`\n", |
| 190 | + "# a) `sudo apt update \u0026\u0026 sudo apt upgrade -y`\n", |
167 | 191 | "# b) wget https://apt.llvm.org/llvm.sh\n", |
168 | 192 | "# chmod +x llvm.sh\n", |
169 | 193 | "# sudo ./llvm.sh 18 all\n", |
170 | 194 | "#\n", |
171 | 195 | "!litert compile resnet18/resnet18.tflite --target sm8750" |
172 | 196 | ], |
173 | 197 | "metadata": {}, |
174 | | - "execution_count": null, |
175 | | - "outputs": [] |
| 198 | + "execution_count": null |
176 | 199 | }, |
177 | 200 | { |
178 | 201 | "id": "2da953c9", |
179 | 202 | "cell_type": "markdown", |
180 | 203 | "source": [ |
181 | | - "## \ud83c\udfc1 7. Benchmark in Google AI Edge Portal" |
| 204 | + "## 🏁 7. Benchmark in Google AI Edge Portal" |
182 | 205 | ], |
183 | | - "metadata": {} |
| 206 | + "metadata": {}, |
| 207 | + "execution_count": null |
184 | 208 | }, |
185 | 209 | { |
186 | 210 | "id": "a9a19e48", |
|
192 | 216 | "!gcloud auth login\n", |
193 | 217 | "\n", |
194 | 218 | "# Benchmark on Google AI Edge Portal\n", |
195 | | - "# Please specify you own GCP project: --gcp-project <your-own-gcp-project-id>\n", |
| 219 | + "# Please specify you own GCP project: --gcp-project \u003cyour-own-gcp-project-id\u003e\n", |
196 | 220 | "# Or set a default environment variable: LITERT_GCP_PROJECT\n", |
197 | 221 | "!litert benchmark model.tflite --gcp --device \"pixel 7\" --cpu --gcp-project aep-e2e-test\n", |
198 | 222 | "!litert benchmark model.tflite --gcp --device \"pixel 7\" --gpu --gcp-project aep-e2e-test\n", |
199 | 223 | "!litert benchmark model.tflite --gcp --devices \"pixel 7, sm-s931u1\" --gpu" |
200 | 224 | ], |
201 | 225 | "metadata": {}, |
202 | | - "execution_count": null, |
203 | | - "outputs": [] |
| 226 | + "execution_count": null |
204 | 227 | }, |
205 | 228 | { |
206 | 229 | "id": "2685b076", |
|
219 | 242 | "```\n", |
220 | 243 | "These are not executed in this notebook as Colab does not have access to a physical Android device by default." |
221 | 244 | ], |
222 | | - "metadata": {} |
| 245 | + "metadata": {}, |
| 246 | + "execution_count": null |
223 | 247 | } |
224 | 248 | ], |
225 | 249 | "metadata": { |
|
0 commit comments