GPU 支援

支援 OpenGL ES

MediaPipe 針對 Android/Linux 支援 OpenGL ES 3.2 以上版本;在 iOS 上則支援 ES 3.0 以上的版本。此外,MediaPipe 也支援 iOS 的金屬。

必須使用 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 文字。

您需要使用 ES 3.1 以上的列印版本,才能在 MediaPipe 中對 GPU 執行 TFLite 推論。完成這項設定後,請使用以下程式碼建構應用程式:

$ 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 將 /usr/lib/x86_64-linux-gnu/usr/include 加到 $TF_CUDA_PATHS 中。

$ 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

最後,使用 TensorFlow GPU 建構 MediaPipe,並同時使用兩個標記 --config=cuda--spawn_strategy=local。例如:

$ 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%