使用 GPU

您可以將 GPU 加速器附加至 Managed Service for Apache Spark 批次工作負載,以達到下列效果:

  • 加快處理大規模資料分析工作負載。

  • 使用 GPU 機器學習程式庫,加速訓練大型資料集的模型。

  • 執行進階資料分析,例如影片或自然語言處理。

所有支援的 Managed Service for Apache Spark 執行階段,都會將 NVIDIA cuDF for Apache Spark 程式庫新增至每個工作負載節點。Managed Service for Apache Spark 執行階段1.1 版也會將 XGBoost 程式庫新增至工作負載節點。這些程式庫提供強大的資料轉換和機器學習工具,可用於 GPU 加速工作負載。

GPU 優點

以下是搭配使用 GPU 和 Managed Service for Apache Spark 工作負載的部分優點:

  • 提升效能:GPU 加速功能可大幅提升 Spark 工作負載效能,尤其是機器學習和深度學習、圖形處理和複雜分析等運算密集型工作。

  • 加快模型訓練速度:執行機器學習工作時,附加 GPU 可大幅縮短訓練模型所需的時間,讓資料科學家和工程師快速疊代及實驗。

  • 擴充性:客戶可以新增更多 GPU 節點,或為節點新增更強大的 GPU,以處理日益複雜的處理需求。

  • 成本效益:雖然 GPU 需要初期投資,但由於處理時間縮短,資源利用效率提高,長期下來可節省成本。

  • 強化資料分析:GPU 加速功能可讓您對大型資料集執行進階分析,例如圖片和影片分析,以及自然語言處理。

  • 改良產品:處理速度更快,可加快決策速度,並提升應用程式回應速度。

限制和注意事項

定價

GPU 加速器適用於進階定價級別。如要瞭解加速器定價資訊,請參閱「Managed Service for Apache Spark 定價」。

事前準備

建立附加 GPU 加速器的無伺服器批次工作負載前,請先完成下列步驟:

  1. 登入 Google Cloud 帳戶。如果您未曾使用過 Google Cloud,歡迎 建立帳戶,親自體驗實際使用我們產品的成效。新客戶還能獲得價值 $300 美元的免費抵免額,用於執行、測試及部署工作負載。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Dataproc, Compute Engine, and Cloud Storage APIs, if any are not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  5. Install the Google Cloud CLI.

  6. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  7. To initialize the gcloud CLI, run the following command:

    gcloud init
  8. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  9. Verify that billing is enabled for your Google Cloud project.

  10. Enable the Dataproc, Compute Engine, and Cloud Storage APIs, if any are not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  11. Install the Google Cloud CLI.

  12. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  13. To initialize the gcloud CLI, run the following command:

    gcloud init
  14. 前往 Google Cloud 控制台的「Cloud Storage bucket」頁面。

    前往「Buckets」(值區) 頁面

  15. 按一下「 Create」(建立)。
  16. 在「Create a bucket」(建立 bucket) 頁面中,輸入 bucket 資訊。如要前往下一個步驟,請按一下「Continue」(繼續)。
    1. 在「Get started」(開始使用) 部分,執行下列操作:
      • 輸入符合 bucket 命名規定的全域不重複名稱。
      • 如要新增值區標籤,請展開「Labels」(標籤) 專區 (),按一下「Add label」(新增標籤) ,然後為標籤指定key和value。
    2. 在「Choose where to store your data」(選擇資料的儲存位置) 專區中,執行下列操作:
      1. 選取「位置類型」。
      2. 從「Location type」(位置類型) 下拉式選單中,選擇 bucket 資料的永久儲存位置。
        • 如果選取「雙區域」位置類型,也可以勾選相關核取方塊,啟用強化型複製功能。
      3. 如要設定跨 bucket 複製,請選取「新增透過 Storage 移轉服務執行的跨 bucket 複製作業」,然後按照下列步驟操作:

        設定跨 bucket 複製作業

        1. 在「Bucket」選單中選取 bucket。
        2. 在「Replication settings」(複製設定) 專區中,按一下「Configure」(設定),即可調整複製工作的設定。

          「Configure cross-bucket replication」(設定跨 bucket 複製作業) 窗格隨即顯示。

          • 如要依物件名稱前置字串篩選要複製的物件,請輸入要納入或排除物件的前置字串,然後按一下「 新增前置字串」。
          • 如要為複製的物件設定儲存空間級別,請從「Storage class」(儲存空間級別) 選單中選取儲存空間級別。如果略過這個步驟,複製的物件預設會使用目的地 bucket 的儲存空間級別。
          • 按一下「完成」。
    3. 在「Choose how to store your data」(選擇資料儲存方式) 部分,執行下列操作:
      1. 選取 bucket 的預設儲存空間級別,或選取 Autoclass,讓系統自動管理 bucket 資料的儲存空間級別。
      2. 如要啟用階層命名空間,請在「Optimize storage for data-intensive workloads」(為資料密集型工作負載提供最理想的儲存空間) 區段中,選取「Enable hierarchical namespace on this bucket」(為這個 bucket 啟用階層命名空間)。
    4. 在「Choose how to control access to objects」(選取如何控制物件的存取權) 區段中,選取 bucket 是否強制執行公開存取權防護,並選取 bucket 物件的存取控管方法。
    5. 在「Choose how to protect object data」(選擇保護物件資料的方式) 部分,執行下列操作:
      • 在「資料保護」下方,選取要為 bucket 設定的選項。
        • 如要啟用虛刪除功能,請勾選「Soft delete policy (For data recovery)」(虛刪除政策 (用於資料復原)) 核取方塊,並指定物件刪除後要保留的天數。
        • 如要設定物件版本管理,請勾選「Object versioning (For version control)」(物件版本管理 (用於版本管控)) 核取方塊,並指定每個物件的版本數量上限,以及非現行版本的到期天數。
        • 如要啟用物件和 bucket 的資料保留政策,請勾選「資料保留 (法規遵循)」核取方塊,然後執行下列操作:
          • 如要啟用物件保留鎖定功能,請勾選「Enable object retention」(啟用物件保留功能) 核取方塊。
          • 如要啟用值區鎖定功能,請勾選「Set bucket retention policy」(設定值區資料保留政策) 核取方塊,然後選擇保留期限的時間單位和長度。
      • 如要選擇物件資料的加密方式,請展開「Data encryption」(資料加密) 專區 (),然後選取「Data encryption method」(資料加密方法)。
  17. 點選「建立」。

建立無伺服器批次工作負載,並使用 GPU 加速器

提交使用 NVIDIA L4 GPU 的 Managed Service for Apache Spark 批次工作負載,執行平行 PySpark 工作。請使用 gcloud CLI 按照下列步驟操作:

  1. 點選「展開」,然後使用文字或程式碼編輯器,建立列出的 PySpark 程式碼,並儲存至本機電腦上的 test-py-spark-gpu.py 檔案。

    #!/usr/bin/env python
    
    """S8s Accelerators Example."""
    
    import subprocess
    from typing import Any
    from pyspark.sql import SparkSession
    from pyspark.sql.functions import col
    from pyspark.sql.types import IntegerType
    from pyspark.sql.types import StructField
    from pyspark.sql.types import StructType
    
    spark = SparkSession.builder.appName("joindemo").getOrCreate()
    
    
    def get_num_gpus(_: Any) -> int:
      """Returns the number of GPUs."""
      p_nvidia_smi = subprocess.Popen(
          ["nvidia-smi", "-L"], stdin=None, stdout=subprocess.PIPE
      )
      p_wc = subprocess.Popen(
          ["wc", "-l"],
          stdin=p_nvidia_smi.stdout,
          stdout=subprocess.PIPE,
          stderr=subprocess.PIPE,
          universal_newlines=True,
      )
      [out, _] = p_wc.communicate()
      return int(out)
    
    
    num_workers = 5
    result = (
        spark.sparkContext.range(0, num_workers, 1, num_workers)
        .map(get_num_gpus)
        .collect()
    )
    num_gpus = sum(result)
    print(f"Total accelerators: {num_gpus}")
    
    # Run the join example
    schema = StructType([StructField("value", IntegerType(), True)])
    df = (
        spark.sparkContext.parallelize(range(1, 10000001), 6)
        .map(lambda x: (x,))
        .toDF(schema)
    )
    df2 = (
        spark.sparkContext.parallelize(range(1, 10000001), 6)
        .map(lambda x: (x,))
        .toDF(schema)
    )
    joined_df = (
        df.select(col("value").alias("a"))
        .join(df2.select(col("value").alias("b")), col("a") == col("b"))
        .explain()
    )
  2. 在本機電腦上使用 gcloud CLI,提交 Managed Service for Apache Spark 無伺服器批次工作,並指定五個 worker,每個 worker 都由 L4 GPU 加速:

    gcloud dataproc batches submit pyspark test-py-spark-gpu.py \
        --project=PROJECT_ID \
        --region=REGION \
        --deps-bucket=BUCKET_NAME \
        --version=1.1 \
        --properties=spark.dataproc.executor.compute.tier=premium,spark.dataproc.executor.disk.tier=premium,spark.dataproc.executor.resource.accelerator.type=l4,spark.executor.instances=5,spark.dataproc.driverEnv.LANG=C.UTF-8,spark.executorEnv.LANG=C.UTF-8,spark.shuffle.manager=com.nvidia.spark.rapids.RapidsShuffleManager
    

注意:

  • PROJECT_ID: Google Cloud 專案 ID。
  • REGION:可執行工作負載的 Compute Engine 區域。
  • BUCKET_NAME:Cloud Storage bucket 的名稱。Spark 會先將工作負載依附元件上傳至這個 bucket 中的 /dependencies 資料夾,再執行批次工作負載。
  • --version:所有支援的 Managed Service for Apache Spark 執行階段,都會將 NVIDIA cuDF for Apache Spark 程式庫新增至 GPU 加速工作負載的每個節點。只有執行階段 1.1 版會將 XGBoost 程式庫新增至 GPU 加速工作負載的每個節點。
  • --properties (請參閱「Spark 資源分配屬性」):

    • spark.dataproc.driverEnv.LANG=C.UTF-8 和 spark.executorEnv.LANG=C.UTF-8 (2.2 之前的執行階段版本必須使用):這些屬性會將預設字元集設為 C.UTF-8。
    • spark.dataproc.executor.compute.tier=premium (必要):GPU 加速工作負載會以進階資料運算單元 (DCU) 計費。請參閱 Managed Service for Apache Spark 加速器定價。

    • spark.dataproc.executor.disk.tier=premium (必要):具有 A100-40、A100-80 或 L4 加速器的節點必須使用進階磁碟級別。

    • spark.dataproc.executor.resource.accelerator.type=l4 (必要):只能指定一種 GPU 類型。範例工作會選取 L4 GPU。您可以使用下列引數名稱指定下列加速器類型:

      GPU 類型 引數名稱
      A100 40GB a100-40
      A100 80GB a100-80

    • spark.executor.instances=5 (必填):至少須有兩個。在本範例中,請設為 5。

    • spark.executor.cores (選用):您可以設定這項屬性,指定核心 vCPU 數量。L4 GPU 的有效值為 4 (預設)、8、12、16、24、48 或 96。A100 GPU 唯一有效且預設的值為 12。如果設定採用 L4 GPU 和 24、48 或 96 個核心,則每個執行器會附加 2、4 或 8 個 GPU。所有其他設定都附加 1 GPU。

    • spark.dataproc.executor.disk.size (必要):L4 GPU 的磁碟大小固定為 375 GB,但如果設定為 24、48 或 96 核心,則磁碟大小分別為 750、1,500 或 3,000 GB。提交 L4 加速工作負載時,如果將這項屬性設為其他值,就會發生錯誤。如果選取 A100 40 或 A100 80 GPU,有效大小為 375g、750g、1500g、3000g、6000g 和 9000g。

    • spark.executor.memory (選用) 和 spark.executor.memoryOverhead (受限):您可以設定記憶體,但無法設定 memoryOverhead。未由已設定屬性耗用的可用記憶體量,會套用至未設定屬性。spark.executor.memoryOverhead 會設為 PySpark 批次工作負載可用記憶體的 40%,其他工作負載則為 10% (請參閱「Spark 資源分配屬性」)。

      下表列出不同 A100 和 L4 GPU 設定可設定的記憶體容量上限。這兩項屬性的最小值為 1024 MB。

      A100 (40 GB) A100 (80 GB) L4 (4 核心) L4 (8 核心) L4 (12 核心) L4 (16 核心) L4 (24 核心) L4 (48 核心) L4 (96 核心)
      記憶體總量上限 (MB) 78040 165080 13384 26768 40152 53536 113072 160608 321216
    • NVIDIA cuDF for Apache Spark 程式庫屬性 (選用):根據預設,Managed Service for Apache Spark 會設定下列 NVIDIA cuDF for Apache Spark 程式庫屬性值:

      • spark.plugins=com.nvidia.spark.SQLPlugin
      • spark.executor.resource.gpu.amount=1
      • spark.task.resource.gpu.amount=1/$spark_executor_cores
      • spark.shuffle.manager=''。根據預設,這項屬性未設定。NVIDIA 建議您在使用 GPU 時開啟 CUDA-X 重組管理工具,以提升效能。方法是在提交工作負載時設定 spark.shuffle.manager=com.nvidia.spark.rapids.RapidsShuffleManager。
      • spark.rapids.sql.concurrentGpuTasks = (gpuMemoryinMB / 8, 4) 的最小值
      • spark.rapids.shuffle.multiThreaded.writer.threads = (VM 中的 CPU 核心數 / 每個 VM 的 GPU 數量) 和 32 兩者中的最小值
      • spark.rapids.shuffle.multiThreaded.reader.threads= VM 中的 CPU 核心數 / 每個 VM 的 GPU 數量,32

      如要設定 Apache Spark 程式庫的 NVIDIA cuDF 屬性,請參閱「NVIDIA cuDF plug-in for Apache Spark Configuration」;如要設定 Spark 進階屬性,請參閱「NVIDIA cuDF plug-in for Apache Spark Advanced Configuration」。