김지훈

tensorRT ipynb

1 +{
2 + "nbformat": 4,
3 + "nbformat_minor": 0,
4 + "metadata": {
5 + "colab": {
6 + "name": "Untitled0.ipynb",
7 + "provenance": [],
8 + "private_outputs": true,
9 + "collapsed_sections": []
10 + },
11 + "kernelspec": {
12 + "name": "python3",
13 + "display_name": "Python 3"
14 + },
15 + "accelerator": "GPU"
16 + },
17 + "cells": [
18 + {
19 + "cell_type": "markdown",
20 + "metadata": {
21 + "id": "8k6IDrTDGZ5W"
22 + },
23 + "source": [
24 + "## Pytorch 추론시간 단축을 위한 정밀도 감소 - TensorRT / torch2trt library\n",
25 + "\n",
26 + "### 환경설정\n",
27 + "\n",
28 + "- [Nvidia developer portal](https://developer.nvidia.com/nvidia-tensorrt-7x-download) 회원가입\n",
29 + "- TensorRT 7.0.0.11 for Ubuntu 1804 and CUDA 10.0 DEB local repo package 다운로드 후 colab 업로드\n",
30 + "\n",
31 + "### TensorRT 7.0 을 colab 에 설치하는 방법"
32 + ]
33 + },
34 + {
35 + "cell_type": "code",
36 + "metadata": {
37 + "id": "ApojKcRcGf2j"
38 + },
39 + "source": [
40 + "!nvcc --version"
41 + ],
42 + "execution_count": null,
43 + "outputs": []
44 + },
45 + {
46 + "cell_type": "markdown",
47 + "metadata": {
48 + "id": "NZD-bw7YIlHL"
49 + },
50 + "source": [
51 + "### colab CUDA 10.1 삭제"
52 + ]
53 + },
54 + {
55 + "cell_type": "code",
56 + "metadata": {
57 + "id": "jJQrjrcfIm8u"
58 + },
59 + "source": [
60 + "# remove CUDA 10.1\n",
61 + "\n",
62 + "!sudo apt-get --purge remove cuda nvidia* libnvidia-*\n",
63 + "!sudo dpkg -l | grep cuda- | awk '{print $2}' | xargs -n1 dpkg --purge\n",
64 + "!sudo apt-get remove cuda-*\n",
65 + "!sudo apt autoremove\n",
66 + "!sudo apt-get update"
67 + ],
68 + "execution_count": null,
69 + "outputs": []
70 + },
71 + {
72 + "cell_type": "markdown",
73 + "metadata": {
74 + "id": "tRhvY4fZIygS"
75 + },
76 + "source": [
77 + "### CUDA 10.0 설치 - 입력 세번 필요 (Y, 31, 1)"
78 + ]
79 + },
80 + {
81 + "cell_type": "code",
82 + "metadata": {
83 + "id": "73tZNq2WI41u"
84 + },
85 + "source": [
86 + "# Installing CUDA 10.0\n",
87 + "\n",
88 + "!wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-repo-ubuntu1804_10.0.130-1_amd64.deb\n",
89 + "!sudo dpkg -i cuda-repo-ubuntu1804_10.0.130-1_amd64.deb\n",
90 + "!sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub\n",
91 + "!sudo apt-get update\n",
92 + "!wget http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/nvidia-machine-learning-repo-ubuntu1804_1.0.0-1_amd64.deb\n",
93 + "!sudo apt install -y ./nvidia-machine-learning-repo-ubuntu1804_1.0.0-1_amd64.deb\n",
94 + "!sudo apt-get update\n",
95 + "\n",
96 + "# Install NVIDIA driver\n",
97 + "!sudo apt-get -y installnvidia-driver-418\n",
98 + "\n",
99 + "# Install development and runtime libraries (~4GB)\n",
100 + "!sudo apt-get install -y \\\n",
101 + " cuda-10-0 \\\n",
102 + " libcudnn7=7.6.2.24-1+cuda10.0 \\\n",
103 + " libcudnn7-dev=7.6.2.24-1+cuda10.0 --allow-change-held-packages"
104 + ],
105 + "execution_count": null,
106 + "outputs": []
107 + },
108 + {
109 + "cell_type": "code",
110 + "metadata": {
111 + "id": "5HAS_SQ7LBgj"
112 + },
113 + "source": [
114 + "!nvcc --version"
115 + ],
116 + "execution_count": null,
117 + "outputs": []
118 + },
119 + {
120 + "cell_type": "markdown",
121 + "metadata": {
122 + "id": "emv-dfsgLEE9"
123 + },
124 + "source": [
125 + "### TensorRT 설치 - colab .deb 파일 경로 확인"
126 + ]
127 + },
128 + {
129 + "cell_type": "code",
130 + "metadata": {
131 + "id": "YI96JETCLJ3I"
132 + },
133 + "source": [
134 + "# install tensorrt\n",
135 + "!sudo dpkg -i \"/content/drive/My Drive/capstone1/CAN/nv-tensorrt-repo-ubuntu1804-cuda10.0-trt7.0.0.11-ga-20191216_1-1_amd64.deb\"\n",
136 + "!sudo apt-key add /var/nv-tensorrt-repo-cuda10.0-trt7.0.0.11-ga-20191216/7fa2af80.pub\n",
137 + "\n",
138 + "!sudo apt-get update\n",
139 + "\n",
140 + "!sudo apt-get install libnvinfer7=7.0.0-1+cuda10.0 libnvonnxparsers7=7.0.0-1+cuda10.0 libnvparsers7=7.0.0-1+cuda10.0 libnvinfer-plugin7=7.0.0-1+cuda10.0 libnvinfer-dev=7.0.0-1+cuda10.0 libnvonnxparsers-dev=7.0.0-1+cuda10.0 libnvparsers-dev=7.0.0-1+cuda10.0 libnvinfer-plugin-dev=7.0.0-1+cuda10.0 python-libnvinfer=7.0.0-1+cuda10.0 python3-libnvinfer=7.0.0-1+cuda10.0\n",
141 + "\n",
142 + "!sudo apt-mark hold libnvinfer7 libnvonnxparsers7 libnvparsers7 libnvinfer-plugin7 libnvinfer-dev libnvonnxparsers-dev libnvparsers-dev libnvinfer-plugin-dev python-libnvinfer python3-libnvinfer\n",
143 + "\n",
144 + "!sudo apt-get install tensorrt"
145 + ],
146 + "execution_count": null,
147 + "outputs": []
148 + },
149 + {
150 + "cell_type": "markdown",
151 + "metadata": {
152 + "id": "DcpWP5snL2yy"
153 + },
154 + "source": [
155 + "### TensorRT 설치 확인"
156 + ]
157 + },
158 + {
159 + "cell_type": "code",
160 + "metadata": {
161 + "id": "uOW85nnxLdSB"
162 + },
163 + "source": [
164 + "!dpkg -l | grep TensorRT"
165 + ],
166 + "execution_count": null,
167 + "outputs": []
168 + },
169 + {
170 + "cell_type": "markdown",
171 + "metadata": {
172 + "id": "k7xHrhZuNb0U"
173 + },
174 + "source": [
175 + "### 런타임 다시시작하기"
176 + ]
177 + },
178 + {
179 + "cell_type": "markdown",
180 + "metadata": {
181 + "id": "8EQ__AkjMH9G"
182 + },
183 + "source": [
184 + "### [torch2trt](https://github.com/NVIDIA-AI-IOT/torch2trt) 라이브러리 설치"
185 + ]
186 + },
187 + {
188 + "cell_type": "code",
189 + "metadata": {
190 + "id": "k7IztAyvMGlP"
191 + },
192 + "source": [
193 + "cd /content/drive/My\\ Drive/capstone1/CAN"
194 + ],
195 + "execution_count": null,
196 + "outputs": []
197 + },
198 + {
199 + "cell_type": "code",
200 + "metadata": {
201 + "id": "qR0V215wMHXF"
202 + },
203 + "source": [
204 + "!pip3 install pycuda\n",
205 + "!git clone https://github.com/NVIDIA-AI-IOT/torch2trt"
206 + ],
207 + "execution_count": null,
208 + "outputs": []
209 + },
210 + {
211 + "cell_type": "code",
212 + "metadata": {
213 + "id": "fG9UtL6sNAgM"
214 + },
215 + "source": [
216 + "cd /content/drive/My\\ Drive/capstone1/CAN/torch2trt"
217 + ],
218 + "execution_count": null,
219 + "outputs": []
220 + },
221 + {
222 + "cell_type": "code",
223 + "metadata": {
224 + "id": "iMEc08ilNDW8"
225 + },
226 + "source": [
227 + "!python setup.py install"
228 + ],
229 + "execution_count": null,
230 + "outputs": []
231 + },
232 + {
233 + "cell_type": "code",
234 + "metadata": {
235 + "id": "LpK7xRFdNVbr"
236 + },
237 + "source": [
238 + "import tensorrt\n",
239 + "import torch2trt"
240 + ],
241 + "execution_count": null,
242 + "outputs": []
243 + },
244 + {
245 + "cell_type": "markdown",
246 + "metadata": {
247 + "id": "M1DDuNL4Npn-"
248 + },
249 + "source": [
250 + "### 추론시간 비교하기"
251 + ]
252 + },
253 + {
254 + "cell_type": "code",
255 + "metadata": {
256 + "id": "9WddajL5Pj6E"
257 + },
258 + "source": [
259 + "cd /content/drive/My\\ Drive/capstone1/CAN"
260 + ],
261 + "execution_count": null,
262 + "outputs": []
263 + },
264 + {
265 + "cell_type": "code",
266 + "metadata": {
267 + "id": "PgKGWmSqNr4z"
268 + },
269 + "source": [
270 + "import utils\n",
271 + "import numpy as np\n",
272 + "import importlib\n",
273 + "importlib.reload(utils)\n",
274 + "\n",
275 + "utils.run_benchmark('./weights/fed_avg_50_0.9688.pth')"
276 + ],
277 + "execution_count": null,
278 + "outputs": []
279 + },
280 + {
281 + "cell_type": "markdown",
282 + "metadata": {
283 + "id": "qh7_UaFsRykh"
284 + },
285 + "source": [
286 + "- torch: 464 msc / 96.87 % / 0.05931\n",
287 + "- trt: 354 msc / 96.87 % / 0.05931\n",
288 + "- trt float 16: 323 msc / 96.87 % / 0.05931\n",
289 + "- trt int8 strict: 401 msc / 96.86 % / 06405\n",
290 + "\n",
291 + "\n",
292 + "#### int8 양자화 실패한 이유 추측\n",
293 + "- input 이 0.0 ~ 1.0 이기 때문에 계산이 전부 float 연산\n",
294 + "- nvidia tensorRT 가 정확도가 너무 낮아질 경우 양자화 무시\n",
295 + "- 사용법 미숙, 모델이 너무 간단함\n"
296 + ]
297 + }
298 + ]
299 +}
...\ No newline at end of file ...\ No newline at end of file