您可以將 GPU 加速器附加至 Google Cloud Serverless for Apache Spark 批次工作負載,達成下列結果:
加快處理大規模資料分析工作負載的速度。
使用 GPU 機器學習程式庫,加速訓練大型資料集的模型。
執行進階資料分析,例如影片或自然語言處理。
所有支援的 Serverless for Apache Spark Spark 執行階段,都會在每個工作負載節點中新增 Spark RAPIDS 程式庫。無伺服器 Apache Spark Spark 執行階段 1.1 版 也將 XGBoost 程式庫 新增至工作負載節點。這些程式庫提供強大的資料轉換和機器學習工具,可用於 GPU 加速工作負載。
GPU 的優點
搭配使用 GPU 和 Serverless for Apache Spark Spark 工作負載的優點如下:
提升效能:GPU 加速功能可大幅提升 Spark 工作負載的效能,特別是機器學習和深度學習、圖形處理及複雜分析等需要大量運算資源的工作。
加快模型訓練速度:對於機器學習工作,附加 GPU 可大幅縮短模型訓練時間,讓資料科學家和工程師快速疊代及實驗。
擴充性:客戶可以為節點新增更多 GPU 節點或更強大的 GPU,以處理日益複雜的處理需求。
成本效益:雖然 GPU 需要初期投資,但由於處理時間縮短,資源使用效率也更高,因此長期下來可節省成本。
強化資料分析:GPU 加速功能可讓您對大型資料集執行進階分析,例如圖片和影片分析,以及自然語言處理。
改良產品:處理速度更快,決策速度也更快,應用程式的回應速度也更快。
限制和注意事項
您可以將 NVIDIA A100 或 NVIDIA L4 GPU 連接至 Google Cloud Serverless for Apache Spark 批次工作負載。A100 和 L4 加速器須遵守 Compute Engine GPU 區域可用性規定。
只有在使用 Serverless for Apache Spark Spark 執行階段 1.x 版時,Serverless for Apache Spark GPU 加速工作負載才會提供 XGBoost 程式庫。
使用 XGBoost 的 Serverless for Apache Spark GPU 加速批次作業會使用增加的 Compute Engine 配額。舉例來說,如要執行使用 NVIDIA L4 GPU 的無伺服器批次工作負載,您必須分配 NVIDIA_L4_GPUS 配額。
啟用加速器的工作與
constraints/compute.requireShieldedVm
機構政策不相容。如果貴機構強制執行這項政策,啟用加速器的作業就無法順利執行。使用 RAPIDS GPU 加速功能時,如果支援的 Serverless for Apache Spark 執行階段版本早於
2.2
,則必須將預設字元集設為 UTF-8。詳情請參閱「使用 GPU 加速器建立無伺服器批次工作負載」一文。
定價
如要瞭解加速器價格資訊,請參閱「Serverless for Apache Spark 定價」一文。
事前準備
建立附加 GPU 加速器的無伺服器批次工作負載前,請先完成下列步驟:
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Dataproc, Compute Engine, and Cloud Storage APIs.
-
Install the Google Cloud CLI.
-
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
-
如要初始化 gcloud CLI,請執行下列指令:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Dataproc, Compute Engine, and Cloud Storage APIs.
-
Install the Google Cloud CLI.
-
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
-
如要初始化 gcloud CLI,請執行下列指令:
gcloud init
- In the Google Cloud console, go to the Cloud Storage Buckets page.
- Click Create.
- On the Create a bucket page, enter your bucket information. To go to the next
step, click Continue.
-
In the Get started section, do the following:
- Enter a globally unique name that meets the bucket naming requirements.
- To add a
bucket label,
expand the Labels section ( ),
click add_box
Add label, and specify a
key
and avalue
for your label.
-
In the Choose where to store your data section, do the following:
- Select a Location type.
- Choose a location where your bucket's data is permanently stored from the Location type drop-down menu.
- If you select the dual-region location type, you can also choose to enable turbo replication by using the relevant checkbox.
- To set up cross-bucket replication, select
Add cross-bucket replication via Storage Transfer Service and
follow these steps:
Set up cross-bucket replication
- In the Bucket menu, select a bucket.
In the Replication settings section, click Configure to configure settings for the replication job.
The Configure cross-bucket replication pane appears.
- To filter objects to replicate by object name prefix, enter a prefix that you want to include or exclude objects from, then click Add a prefix.
- To set a storage class for the replicated objects, select a storage class from the Storage class menu. If you skip this step, the replicated objects will use the destination bucket's storage class by default.
- Click Done.
-
In the Choose how to store your data section, do the following:
- Select a default storage class for the bucket or Autoclass for automatic storage class management of your bucket's data.
- To enable hierarchical namespace, in the Optimize storage for data-intensive workloads section, select Enable hierarchical namespace on this bucket.
- In the Choose how to control access to objects section, select whether or not your bucket enforces public access prevention, and select an access control method for your bucket's objects.
-
In the Choose how to protect object data section, do the
following:
- Select any of the options under Data protection that you
want to set for your bucket.
- To enable soft delete, click the Soft delete policy (For data recovery) checkbox, and specify the number of days you want to retain objects after deletion.
- To set Object Versioning, click the Object versioning (For version control) checkbox, and specify the maximum number of versions per object and the number of days after which the noncurrent versions expire.
- To enable the retention policy on objects and buckets, click the Retention (For compliance) checkbox, and then do the following:
- To enable Object Retention Lock, click the Enable object retention checkbox.
- To enable Bucket Lock, click the Set bucket retention policy checkbox, and choose a unit of time and a length of time for your retention period.
- To choose how your object data will be encrypted, expand the Data encryption section (Data encryption method. ), and select a
- Select any of the options under Data protection that you
want to set for your bucket.
-
In the Get started section, do the following:
- Click Create.
按一下「展開我」,然後使用文字或程式碼編輯器,在您本機的
test-py-spark-gpu.py
檔案中建立並儲存列出的 PySpark 程式碼。#!/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() )
在本機上使用 gcloud CLI 提交 Serverless for Apache Spark 無伺服器批次工作,其中有五個工作站,每個工作站都以 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 會先將工作負載依附元件上傳至這個值區的
/dependencies
資料夾,再執行批次工作負載。 - --version:所有支援的 Serverless for Apache Spark 執行階段 Google Cloud 都會將 RAPIDS 程式庫新增至 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
(必要): 系統會使用進階資料運算單元 (DCU),計算 GPU 加速工作負載的費用。請參閱「Serverless 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
(必填):至少要有兩個。在這個範例中,請設為五。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
(選用):您可以設定其中一個屬性,但不能同時設定兩者。系統會將未由已設定屬性耗用的可用記憶體量,套用至未設定的屬性。根據預設,PySpark 批次工作負載的spark.executor.memoryOverhead
會設為可用記憶體的 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 Spark RAPIDS 屬性 (選用):根據預設,Serverless for Apache Spark 會設定下列 Spark RAPIDS 屬性值:
spark.plugins
=com.nvidia.spark.SQLPluginspark.executor.resource.gpu.amount
=1spark.task.resource.gpu.amount
=1/$spark_executor_coresspark.shuffle.manager
=''。根據預設,此屬性為未設定。使用 GPU 時,NVIDIA 建議開啟 RAPIDS 隨機管理工具,以提升效能。如要這麼做,請在提交工作負載時設定spark.shuffle.manager=com.nvidia.spark.rapids.RapidsShuffleManager
。spark.rapids.sql.concurrentGpuTasks
= minimum of (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)
如要設定 Spark RAPIDS 屬性,請參閱「RAPIDS Accelerator for Apache Spark Configuration」;如要設定 Spark 進階屬性,請參閱「RAPIDS Accelerator for Apache Spark Advanced Configuration」。
使用 GPU 加速器建立無伺服器批次工作負載
提交使用 NVIDIA L4 GPU 的 Serverless for Apache Spark 批次工作負載,執行平行處理的 PySpark 工作。使用 gcloud CLI 執行下列步驟:
注意: