TensorRT : High-performance deep learning inference

딥러닝 모델을 가속화 하는 TensorRT에 대해 소개하려고 합니다.

Release Notes :: NVIDIA Deep Learning TensorRT Documentation
NVIDIA TensorRT is a C++ library that facilitates high performance inference on NVIDIA GPUs. It is designed to work in connection with deep learning frameworks that are commonly used for training. TensorRT focuses specifically on running an already trained network quickly and efficiently on a GPU fo…

위 링크에 접속한 후에 사용하고자 하는 TensorRT 의 버전을 클릭하면 필요한 cuda 버전이 나옵니다.

목차는…

1. TensorRT ?
2. Exporting a model from pytorch to ONNX
3. TensorRT backend for ONNX


1. TensorRT ?

TensorRT 는 엔비디아에서 개발한 추론에 최적화 되어있는 SDK 입니다. 자신이 개발한 모델을 TensorRT 로 추론해보면 엄청난 속도 향상을 가져올 수 있습니다.

그럼 어떻게 TensorRT 는 빠르게 추론이 가능할까요? 추론하고자 하는 모델에 대해 TensorRT 는 아래와 같이 작업 합니다.

  1. Elimination of layers whose outputs are not used.
  2. Elimination of operations which are equivalent to no-op
  3. The fusion of convolution, bias and ReLU operations
  4. Aggregation of operations with sufficiently similar parameters and the same source tensor (for example, the 1x1 convolutions in GoogleNet v5’s inception module)
  5. Merging of concatenation layers by directing layer outputs to the correct eventual destination.

TensorRT 를 설치하기 이전에 Pytorch 버전과 관련된 이슈가 많은데요. 제가 추천드리는 환경은 아래와 같습니다! 저는 Pytorch 1.3 / TensorRT 7 입니다. NGC 에 TensorRT 나 Pytorch 도커 이미지를 사용하는 것을 권장합니다.

  • Pytorch 1.3 버전 이상일 경우는 TensorRT 7 버전
  • Pytorch 1.2 버전 이하일 경우는 TensorRT 6 버전

2. Exporting a model from pytorch to ONNX

Pytorch 를 곧바로 TensorRT 로 바꾸는 일은 너무 힘든 일입니다. 그렇기 때문에 ONNX 로 추출한 뒤 파싱하여 자동으로 바꾸겠습니다.

(ONNX ?: https://onnx.ai/)

  1. 먼저 아주 간단한 MNIST 분류 모델을 Pytorch 로 만들어 보겠습니다.

tqdm 을 이용하면 초당 이터레이션이 얼마나 도는지 확인할 수 있습니다.

제 경우에는 2640.29it/s 가 걸리는군요!

2. 이제 ONNX 로 추출 해봅시다! ONNX 는 파이토치 튜토리얼에 나와있습니다.

3. TensorRT backend for ONNX

이번 포스트에서는 onnx 나 tensorrt 에서 지원하지 않는 op, layer 에 대해서는 다루지 않습니다. 그럼 2번에서 추출한 onnx 파일로 TensorRT 추론하기에 앞서 다음 코드를 봅시다.

해당 코드는 tensorrt sample 예제에 공통으로 쓰이는 것 입니다. cuda 에 관련된 것들이 많은데 주석을 보고 이런 기능을 하는 구나 정도로만 이해하고 넘어가면 될 것 같습니다.

우선 위에 필요한 클래스와 함수를 선언하고 아래를 저와 같이 맞춰 주세요. builder 에 관련된 것은 링크에서 확인할 수 있습니다. 그 외에는 위 출력문을 통해 어떤 기능을 하는 것인지 알 수 있습니다.

해당 코드를 실행하기 위해서 필요한 것은 아까 exporting 했던 onnx 파일 뿐 입니다! 실행해봅시다!

대략 5~6 배가 빨라졌습니다. 이렇게 TensorRT 는 딥러닝 모델을 리얼타임 서비스에 적용하려면 필수적인 요소입니다. 다음 포스트에서는 영상 처리의 대표적인 모델들을 가지고 지원하지 않는 op, layer 에 대해 구성하고 TensorRT 로 추론하도록 하겠습니다.

You've successfully subscribed to Digitalize offline space with AI
Great! Next, complete checkout to get full access to all premium content.
Error! Could not sign up. invalid link.
Welcome back! You've successfully signed in.
Error! Could not sign in. Please try again.
Success! Your account is fully activated, you now have access to all content.
Error! Stripe checkout failed.
Success! Your billing info is updated.
Error! Billing info update failed.