使用 GPU 執行管道

本頁說明如何在 Dataflow 上執行 Apache Beam 管道,並使用 GPU。使用 GPU 的工作會產生費用,詳情請參閱 Dataflow 定價頁面

如要進一步瞭解如何在 Dataflow 中使用 GPU,請參閱「Dataflow 支援 GPU」。如要進一步瞭解使用 GPU 建構管道的開發人員工作流程,請參閱「關於搭配 Dataflow 使用 GPU」。

使用 Apache Beam 筆記本

如果您已有要在 Dataflow 上透過 GPU 執行的管道,可以略過本節。

您可以使用 Apache Beam 筆記本,透過 GPU 輕鬆製作管道原型並反覆開發,不必設定開發環境。如要開始使用,請參閱「使用 Apache Beam 筆記本進行開發」指南,啟動 Apache Beam 筆記本執行個體,然後按照「搭配 Apache Beam 使用 GPU」範例筆記本操作。

佈建 GPU 配額

GPU 裝置會受到專案配額限制。 Google Cloud 在所選區域申請 GPU 配額

安裝 GPU 驅動程式

如要在 Dataflow 工作人員上安裝 NVIDIA 驅動程式,請將 install-nvidia-driver 附加至 worker_accelerator 服務選項

指定 install-nvidia-driver 選項後,Dataflow 會使用 Container-Optimized OS 提供的 cos-extensions 公用程式,在 Dataflow 工作站上安裝 NVIDIA 驅動程式。指定 install-nvidia-driver 即表示您同意接受 NVIDIA 授權協議。

NVIDIA 驅動程式安裝程式提供的二進位檔和程式庫會掛接到 /usr/local/nvidia/,也就是執行管道使用者程式碼的容器。

GPU 驅動程式版本取決於 Dataflow 使用的 Container-Optimized OS 版本。如要找出特定 Dataflow 工作的 GPU 驅動程式版本,請在工作的「Dataflow Step Logs」(Dataflow 步驟記錄)中搜尋 GPU driver

建構自訂容器映像檔

如要與 GPU 互動,您可能需要額外的 NVIDIA 軟體,例如 GPU 加速程式庫CUDA Toolkit。在執行使用者程式碼的 Docker 容器中提供這些程式庫。

如要自訂容器映像檔,請提供符合 Apache Beam SDK 容器映像檔合約,且具備必要 GPU 程式庫的映像檔。

如要提供自訂容器映像檔,請使用 Dataflow Runner v2,並透過 sdk_container_image 管道選項提供容器映像檔。如果您使用的是 Apache Beam 2.29.0 以下版本,請使用 worker_harness_container_image 管道選項。詳情請參閱「使用自訂容器」。

如要建構自訂容器映像檔,請使用下列其中一種方法:

使用已設定 GPU 用途的現有映像檔

您可以從預先設定 GPU 使用的現有基礎映像檔,建構符合 Apache Beam SDK 容器合約的 Docker 映像檔。舉例來說,TensorFlow Docker 映像檔NVIDIA 容器映像檔已預先設定為使用 GPU。

以 Python 3.6 為基礎的 TensorFlow Docker 映像檔建構範例 Dockerfile 如下:

ARG BASE=tensorflow/tensorflow:2.5.0-gpu
FROM $BASE

# Check that the chosen base image provides the expected version of Python interpreter.
ARG PY_VERSION=3.6
RUN [[ $PY_VERSION == `python -c 'import sys; print("%s.%s" % sys.version_info[0:2])'` ]] \
   || { echo "Could not find Python interpreter or Python version is different from ${PY_VERSION}"; exit 1; }

RUN pip install --upgrade pip \
    && pip install --no-cache-dir apache-beam[gcp]==2.29.0 \
    # Verify that there are no conflicting dependencies.
    && pip check

# Copy the Apache Beam worker dependencies from the Beam Python 3.6 SDK image.
COPY --from=apache/beam_python3.6_sdk:2.29.0 /opt/apache/beam /opt/apache/beam

# Apache Beam worker expects pip at /usr/local/bin/pip by default.
# Some images have pip in a different location. If necessary, make a symlink.
# This line can be omitted in Beam 2.30.0 and later versions.
RUN [[ `which pip` == "/usr/local/bin/pip" ]] || ln -s `which pip` /usr/local/bin/pip

# Set the entrypoint to Apache Beam SDK worker launcher.
ENTRYPOINT [ "/opt/apache/beam/boot" ]

使用 TensorFlow Docker 映像檔時,請使用 TensorFlow 2.5.0 以上版本。舊版 TensorFlow Docker 映像檔會安裝 tensorflow-gpu 套件,而不是 tensorflow 套件。TensorFlow 2.1.0 版發布後,這項區別就不重要了,但 tfx 等幾個下游套件需要 tensorflow 套件。

容器越大,工作站啟動時間就越長。使用 Deep Learning Containers 等容器時,可能會發生這種效能變化。

安裝特定 Python 版本

如果對 Python 版本有嚴格要求,可以從含有必要 GPU 程式庫的 NVIDIA 基本映像檔建構映像檔。然後安裝 Python 解譯器。

以下範例說明如何從 CUDA 容器映像檔目錄中選取不含 Python 解譯器的 NVIDIA 映像檔。調整範例,安裝所需的 Python 3 和 pip 版本。本範例使用 TensorFlow。因此,選擇映像檔時,基礎映像檔中的 CUDA 和 cuDNN 版本會符合 TensorFlow 版本的需求

Dockerfile 範例如下:

# Select an NVIDIA base image with needed GPU stack from https://ngc.nvidia.com/catalog/containers/nvidia:cuda

FROM nvidia/cuda:11.0.3-cudnn8-runtime-ubuntu20.04

RUN \
    # Add Deadsnakes repository that has a variety of Python packages for Ubuntu.
    # See: https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa
    apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F23C5A6CF475977595C89F51BA6932366A755776 \
    && echo "deb http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal main" >> /etc/apt/sources.list.d/custom.list \
    && echo "deb-src http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal main" >> /etc/apt/sources.list.d/custom.list \
    && apt-get update \
    && apt-get install -y curl \
        python3.8 \
        # With python3.8 package, distutils need to be installed separately.
        python3-distutils \
    && rm -rf /var/lib/apt/lists/* \
    && update-alternatives --install /usr/bin/python python /usr/bin/python3.8 10 \
    && curl https://bootstrap.pypa.io/get-pip.py | python \
    && pip install --upgrade pip \
    # Install Apache Beam and Python packages that will interact with GPUs.
    && pip install --no-cache-dir apache-beam[gcp]==2.29.0 tensorflow==2.4.0 \
    # Verify that there are no conflicting dependencies.
    && pip check

# Copy the Apache Beam worker dependencies from the Beam Python 3.8 SDK image.
COPY --from=apache/beam_python3.8_sdk:2.29.0 /opt/apache/beam /opt/apache/beam

# Set the entrypoint to Apache Beam SDK worker launcher.
ENTRYPOINT [ "/opt/apache/beam/boot" ]

在某些 OS 發行版本中,使用 OS 套件管理員安裝特定 Python 版本可能會有困難。在此情況下,請使用 Miniconda 或 pyenv 等工具安裝 Python 解譯器。

Dockerfile 範例如下:

FROM nvidia/cuda:11.0.3-cudnn8-runtime-ubuntu20.04

# The Python version of the Dockerfile must match the Python version you use
# to launch the Dataflow job.

ARG PYTHON_VERSION=3.8

# Update PATH so we find our new Conda and Python installations.
ENV PATH=/opt/python/bin:/opt/conda/bin:$PATH

RUN apt-get update \
    && apt-get install -y wget \
    && rm -rf /var/lib/apt/lists/* \
    # The NVIDIA image doesn't come with Python pre-installed.
    # We use Miniconda to install the Python version of our choice.
    && wget -q https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh \
    && bash Miniconda3-latest-Linux-x86_64.sh -b -p /opt/conda \
    && rm Miniconda3-latest-Linux-x86_64.sh \
    # Create a new Python environment with needed version, and install pip.
    && conda create -y -p /opt/python python=$PYTHON_VERSION pip \
    # Remove unused Conda packages, install necessary Python packages via pip
    # to avoid mixing packages from pip and Conda.
    && conda clean -y --all --force-pkgs-dirs \
    && pip install --upgrade pip \
    # Install Apache Beam and Python packages that will interact with GPUs.
    && pip install --no-cache-dir apache-beam[gcp]==2.29.0 tensorflow==2.4.0 \
    # Verify that there are no conflicting dependencies.
    && pip check \
    # Apache Beam worker expects pip at /usr/local/bin/pip by default.
    # You can omit this line when using Beam 2.30.0 and later versions.
    && ln -s $(which pip) /usr/local/bin/pip

# Copy the Apache Beam worker dependencies from the Apache Beam SDK for Python 3.8 image.
COPY --from=apache/beam_python3.8_sdk:2.29.0 /opt/apache/beam /opt/apache/beam

# Set the entrypoint to Apache Beam SDK worker launcher.
ENTRYPOINT [ "/opt/apache/beam/boot" ]

使用 Apache Beam 容器映像檔

您可以設定容器映像檔,以使用 GPU,不必使用預先設定的映像檔。只有在預先設定的映像檔不適用時,才建議採用這種方法。如要設定自己的容器映像檔,請選取相容的程式庫,並設定執行環境。

Dockerfile 範例如下:

FROM apache/beam_python3.7_sdk:2.24.0
ENV INSTALLER_DIR="/tmp/installer_dir"

# The base image has TensorFlow 2.2.0, which requires CUDA 10.1 and cuDNN 7.6.
# You can download cuDNN from NVIDIA website
# https://developer.nvidia.com/cudnn
COPY cudnn-10.1-linux-x64-v7.6.0.64.tgz $INSTALLER_DIR/cudnn.tgz
RUN \
    # Download CUDA toolkit.
    wget -q -O $INSTALLER_DIR/cuda.run https://developer.download.nvidia.com/compute/cuda/10.1/Prod/local_installers/cuda_10.1.243_418.87.00_linux.run && \

    # Install CUDA toolkit. Print logs upon failure.
    sh $INSTALLER_DIR/cuda.run --toolkit --silent || (egrep '^\[ERROR\]' /var/log/cuda-installer.log && exit 1) && \
    # Install cuDNN.
    mkdir $INSTALLER_DIR/cudnn && \
    tar xvfz $INSTALLER_DIR/cudnn.tgz -C $INSTALLER_DIR/cudnn && \

    cp $INSTALLER_DIR/cudnn/cuda/include/cudnn*.h /usr/local/cuda/include && \
    cp $INSTALLER_DIR/cudnn/cuda/lib64/libcudnn* /usr/local/cuda/lib64 && \
    chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn* && \
    rm -rf $INSTALLER_DIR

# A volume with GPU drivers will be mounted at runtime at /usr/local/nvidia.
ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/nvidia/lib64:/usr/local/cuda/lib64

/usr/local/nvidia/lib64 中的驅動程式庫必須可在容器中探索為共用程式庫。如要讓驅動程式程式庫可供探索,請設定 LD_LIBRARY_PATH 環境變數。

如果您使用 TensorFlow,請務必選擇相容的 CUDA Toolkit 和 cuDNN 版本組合。詳情請參閱「軟體需求」和「測試的建構設定」。

選取 Dataflow 工作站的 GPU 類型和數量

如要設定要附加至 Dataflow 工作站的 GPU 類型和數量,請使用 worker_accelerator 服務選項。根據您的用途和在管道中使用 GPU 的方式,選取 GPU 類型和數量。

如需 Dataflow 支援的 GPU 類型清單,請參閱「Dataflow 支援的 GPU」。

使用 GPU 執行工作

使用 GPU 執行 Dataflow 工作時,請注意下列事項:

  • 由於 GPU 容器通常很大,為避免磁碟空間不足,請執行下列操作:

  • 請考量工作站 VM 上有多少程序會同時使用相同的 GPU。 然後,決定要將 GPU 限制為單一程序,還是允許多個程序使用 GPU。

    • 如果一個 Apache Beam SDK 程序可以使用大部分可用的 GPU 記憶體 (例如將大型模型載入 GPU),您可能需要設定管道選項 --experiments=no_use_multiple_sdk_containers,讓工作站使用單一程序。或者,您也可以使用自訂機器類型 (例如 n1-custom-1-NUMBER_OF_MBn1-custom-1-NUMBER_OF_MB-ext) 搭配一個 vCPU 的工作站,以擴充記憶體。詳情請參閱使用每個 vCPU 記憶體較多的機器類型
    • 如果 GPU 由多個程序共用,請使用 NVIDIA 多程序服務 (MPS),在共用 GPU 上啟用並行處理。

    如要瞭解背景資訊,請參閱「GPU 和工作人員平行處理」。

如要使用 GPU 執行 Dataflow 工作,請使用下列指令:如要使用「右側合適」,請使用accelerator 資源提示,而非worker_accelerator 服務選項

Python

python PIPELINE \
  --runner "DataflowRunner" \
  --project "PROJECT" \
  --temp_location "gs://BUCKET/tmp" \
  --region "REGION" \
  --worker_harness_container_image "IMAGE" \
  --disk_size_gb "DISK_SIZE_GB" \
  --dataflow_service_options "worker_accelerator=type:GPU_TYPE;count:GPU_COUNT;install-nvidia-driver" \
  --experiments "use_runner_v2"

更改下列內容:

  • PIPELINE:管道原始碼檔案
  • PROJECT:專案名稱 Google Cloud
  • BUCKET:Cloud Storage bucket
  • :Dataflow 區域,例如 us-central1REGION選取有支援 GPU_TYPE 的可用區的 `REGION` 。Dataflow 會自動將工作站指派到這個地區中含有 GPU 的區域。
  • IMAGE:Docker 映像檔的 Artifact Registry 路徑
  • DISK_SIZE_GB:每個工作站 VM 的開機磁碟大小,例如 50
  • GPU_TYPE:可用的 GPU 類型,例如 nvidia-tesla-t4
  • GPU_COUNT:要連結至每個工作站 VM 的 GPU 數量,例如 1

驗證 Dataflow 工作

如要確認工作是否使用搭載 GPU 的工作站 VM,請按照下列步驟操作:

  1. 確認工作適用的 Dataflow 工作站已啟動。
  2. 作業執行期間,找出與作業相關聯的工作人員 VM。
    1. 在「Search Products and Resources」(搜尋產品和資源) 提示中,貼上「Job ID」(工作 ID)
    2. 選取與工作相關聯的 Compute Engine VM 執行個體。

您也可以在 Compute Engine 控制台中查看所有執行中執行個體的清單。

  1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面

    前往 VM 執行個體

  2. 按一下「VM 執行個體詳細資料」

  3. 確認詳細資料頁面有「GPU」部分,且已附加 GPU。

如果工作未啟動 GPU,請檢查 worker_accelerator 服務選項是否已正確設定,且顯示在 dataflow_service_options 的 Dataflow 監控介面中。加速器中繼資料的權杖順序很重要。

舉例來說,Dataflow 監控介面中的 dataflow_service_options 管道選項可能如下所示:

['worker_accelerator=type:nvidia-tesla-t4;count:1;install-nvidia-driver', ...]

查看 GPU 使用率

如要查看工作站 VM 的 GPU 使用率,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Monitoring」頁面,或使用下列按鈕:

    前往「Monitoring」頁面

  2. 在 Monitoring 導覽窗格中,按一下「Metrics Explorer」

  3. 在「Resource Type」(資源類型) 部分指定 Dataflow Job。視要監控的指標而定,指定 GPU utilizationGPU memory utilization

詳情請參閱「Metrics Explorer」。

啟用 NVIDIA 多重處理服務

如果 Python 管道在具有多個 vCPU 的工作站上執行,您可以啟用 NVIDIA 多程序服務 (MPS),提升 GPU 作業的並行程度。如要進一步瞭解如何使用 MPS,請參閱「使用 NVIDIA MPS 提升共用 GPU 的效能」。

在 Dataflow Prime 中使用 GPU

Dataflow Prime 可讓您為管道的特定步驟要求加速器。如要在 Dataflow Prime 中使用 GPU,請勿使用 --dataflow-service_options=worker_accelerator 管道選項。請改用 accelerator 資源提示要求 GPU。 詳情請參閱「使用資源提示」。

排解 Dataflow 工作問題

如果使用 GPU 執行 Dataflow 工作時遇到問題,請參閱「排解 Dataflow GPU 工作問題」。

後續步驟