GPU 支援

OpenGL ES 支援

MediaPipe 支援 Android/Linux 至 3.2 以上版本的 OpenGL ES 和 ES 3.0 最高版本 。此外,MediaPipe 也支援 iOS 上的金屬。

需要 OpenGL ES 3.1 以上版本 (在 Android/Linux 系統上) 才能執行 機器學習推論計算機和圖形

停用 OpenGL ES 支援

根據預設,建構 MediaPipe (不含特殊 bazel 旗標) 會嘗試編譯 MediaPipe 然後連結至 OpenGL ES (適用於 iOS 和 Metal) 程式庫。

在不支援 OpenGL ES 的平台上 (請參閱 在 Linux 電腦上設定 OpenGL ES),也就是 應停用 OpenGL ES 支援:

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

OpenGL ES 在 Linux 電腦上的設定

在支援 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 或以上版本的列印,才能執行 TFLite 推論 提供的 CPU 作業時間使用此設定,建構具有:

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

如果只支援 ES 3.0 以下版本,您還是可以建構 MediaPipe 目標, 符合下列條件時,就不需要在 GPU 上進行 TFLite 推論:

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

Linux 電腦上的 TensorFlow CUDA 支援與設定

MediaPipe 架構不需要使用 CUDA 進行 GPU 運算和轉譯。不過 MediaPipe 可與 TensorFlow 搭配使用,在顯示輸出內容的影片資訊卡上,執行 GPU 推論作業 但都支援 CUDA。

如要使用 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 程式庫的位置。注意事項 下列程式碼片段也會加入 /usr/lib/x86_64-linux-gnu 和 將 /usr/include 傳入 $TF_CUDA_PATHS 代表 cudablas 和 libcudnn。

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