GPU 지원

OpenGL ES 지원

MediaPipe는 OpenGL ES를 Android/Linux에서 버전 3.2까지, iOS에서 ES 3.0까지 지원합니다. 또한 MediaPipe는 iOS에서 Metal도 지원합니다.

머신러닝 추론 계산기와 그래프를 실행하려면 OpenGL ES 3.1 이상 (Android/Linux 시스템)이 필요합니다.

OpenGL ES 지원 사용 중지

기본적으로 MediaPipe를 빌드하면 (특별한 bazel 플래그 없이) OpenGL ES (및 iOS의 경우 Metal) 라이브러리를 컴파일하고 링크하려고 합니다.

OpenGL ES를 사용할 수 없는 플랫폼 (Linux 데스크톱에서 OpenGL ES 설정 참고)에서는 다음을 사용하여 OpenGL ES 지원을 사용 중지해야 합니다.

$ bazel build --define MEDIAPIPE_DISABLE_GPU=1 <my-target>

Linux 데스크톱에서 OpenGL ES 설정

OpenGL ES 3.1 이상을 지원하는 비디오 카드가 있는 Linux 데스크톱에서 MediaPipe는 GPU 컴퓨팅 및 렌더링을 실행하고 GPU에서 TFLite 추론을 실행할 수 있습니다.

Linux 데스크톱 GPU가 OpenGL ES에서 MediaPipe를 실행할 수 있는지 확인하려면 다음 단계를 따르세요.

$ sudo apt-get install mesa-common-dev libegl1-mesa-dev libgles2-mesa-dev
$ sudo apt-get install mesa-utils
$ glxinfo | grep -i opengl

예를 들어 다음을 출력할 수 있습니다.

$ glxinfo | grep -i opengl
...
OpenGL ES profile version string: OpenGL ES 3.2 NVIDIA 430.50
OpenGL ES profile shading language version string: OpenGL ES GLSL ES 3.20
OpenGL ES profile extensions:

SSH를 통해 컴퓨터에 연결했고 GPU 정보를 프로브하면 다음과 같은 출력이 표시됩니다.

glxinfo | grep -i opengl
Error: unable to open display

-X 옵션을 사용하여 SSH 연결을 다시 설정한 후 다시 시도하세요. 예를 들면 다음과 같습니다.

ssh -X <user>@<host>

위의 ES 3.20 텍스트를 확인하세요.

MediaPipe의 GPU에서 TFLite 추론을 실행하려면 ES 3.1 이상이 인쇄되어야 합니다. 이 설정에서 다음을 사용하여 빌드합니다.

$ bazel build --copt -DMESA_EGL_NO_X11_HEADERS --copt -DEGL_NO_X11 <my-target>

ES 3.0 이하만 지원되는 경우에도 다음을 사용하여 GPU에서 TFLite 추론이 필요하지 않은 MediaPipe 타겟을 빌드할 수 있습니다.

$ bazel build --copt -DMESA_EGL_NO_X11_HEADERS --copt -DEGL_NO_X11 --copt -DMEDIAPIPE_DISABLE_GL_COMPUTE <my-target>

Linux 데스크톱에서 TensorFlow CUDA 지원 및 설정

MediaPipe 프레임워크는 GPU 컴퓨팅 및 렌더링에 CUDA가 필요하지 않습니다. 하지만 MediaPipe는 TensorFlow와 함께 작동하여 CUDA를 지원하는 비디오 카드에서 GPU 추론을 실행할 수 있습니다.

MediaPipe에서 TensorFlow GPU 추론을 사용 설정하려면 먼저 TensorFlow GPU 문서에 따라 Linux 데스크톱에 필요한 NVIDIA 소프트웨어를 설치해야 합니다.

설치 후 $PATH$LD_LIBRARY_PATH를 업데이트하고 다음으로 ldconfig를 실행합니다.

$ export PATH=/usr/local/cuda-10.1/bin${PATH:+:${PATH}}
$ export LD_LIBRARY_PATH=/usr/local/cuda/extras/CUPTI/lib64,/usr/local/cuda-10.1/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
$ sudo ldconfig

CUPTI, CUDA, CuDNN, NVCC 설치를 확인하는 것이 좋습니다.

$ ls /usr/local/cuda/extras/CUPTI
/lib64
libcupti.so       libcupti.so.10.1.208  libnvperf_host.so        libnvperf_target.so
libcupti.so.10.1  libcupti_static.a     libnvperf_host_static.a

$ ls /usr/local/cuda-10.1
LICENSE  bin  extras   lib64      libnvvp           nvml  samples  src      tools
README   doc  include  libnsight  nsightee_plugins  nvvm  share    targets  version.txt

$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Sun_Jul_28_19:07:16_PDT_2019
Cuda compilation tools, release 10.1, V10.1.243

$ ls /usr/lib/x86_64-linux-gnu/ | grep libcudnn.so
libcudnn.so
libcudnn.so.7
libcudnn.so.7.6.4

$TF_CUDA_PATHS를 설정하면 CUDA 라이브러리 위치를 선언할 수 있습니다. 다음 코드 스니펫은 cudablas 및 libcudnn의 $TF_CUDA_PATHS/usr/lib/x86_64-linux-gnu/usr/include도 추가합니다.

$ export TF_CUDA_PATHS=/usr/local/cuda-10.1,/usr/lib/x86_64-linux-gnu,/usr/include

MediaPipe가 TensorFlow의 CUDA 설정을 가져오도록 하려면 TensorFlow의 .bazelrc를 찾아 build:using_cudabuild:cuda 섹션을 MediaPipe의 .bazelrc 파일에 복사합니다. 예를 들어 2020년 4월 23일 기준 TensorFlow의 CUDA 설정은 다음과 같습니다.

# This config refers to building with CUDA available. It does not necessarily
# mean that we build CUDA op kernels.
build:using_cuda --define=using_cuda=true
build:using_cuda --action_env TF_NEED_CUDA=1
build:using_cuda --crosstool_top=@local_config_cuda//crosstool:toolchain

# This config refers to building CUDA op kernels with nvcc.
build:cuda --config=using_cuda
build:cuda --define=using_cuda_nvcc=true

마지막으로 --config=cuda--spawn_strategy=local 플래그를 두 개 더 사용하여 TensorFlow GPU가 포함된 MediaPipe를 빌드합니다. 예를 들면 다음과 같습니다.

$ bazel build -c opt --config=cuda --spawn_strategy=local \
    --define no_aws_support=true --copt -DMESA_EGL_NO_X11_HEADERS \
    mediapipe/examples/desktop/object_detection:object_detection_tensorflow

바이너리가 실행되는 동안 GPU 기기 정보를 출력합니다.

I external/org_tensorflow/tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcuda.so.1
I external/org_tensorflow/tensorflow/core/common_runtime/gpu/gpu_device.cc:1544] Found device 0 with properties: pciBusID: 0000:00:04.0 name: Tesla T4 computeCapability: 7.5 coreClock: 1.59GHz coreCount: 40 deviceMemorySize: 14.75GiB deviceMemoryBandwidth: 298.08GiB/s
I external/org_tensorflow/tensorflow/core/common_runtime/gpu/gpu_device.cc:1686] Adding visible gpu devices: 0

GPU 사용량을 모니터링하여 GPU가 모델 추론에 사용되는지 확인할 수 있습니다.

$ nvidia-smi --query-gpu=utilization.gpu --format=csv --loop=1

0 %
0 %
4 %
5 %
83 %
21 %
22 %
27 %
29 %
100 %
0 %
0%