您可以将 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 Spark 工作负载结合使用时,可获得以下优势:
性能提升:GPU 加速可以显著提升 Spark 工作负载性能,尤其是对于计算密集型任务,例如机器学习和深度学习、图处理和复杂分析。
更快的模型训练:对于机器学习任务,挂接 GPU 可以显著缩短训练模型所需的时间,使数据科学家和工程师能够快速迭代和实验。
可伸缩性:客户可以向节点添加更多 GPU 节点或更强大的 GPU,以处理日益复杂的处理需求。
成本效益:虽然 GPU 需要初始投资,但由于处理时间缩短且资源利用率提高,您可以随着时间的推移节省成本。
增强的数据分析:借助 GPU 加速,您可以对大型数据集执行高级分析,例如图片和视频分析以及自然语言处理。
改进的产品:更快的处理速度可实现更快的决策和更快的应用响应。
限制和注意事项
GPU 加速器适用于高级价格层级。
您可以将 NVIDIA A100 或 NVIDIA L4 GPU 挂接到 Managed Service for Apache Spark 批处理工作负载。A100 和 L4 加速器受 Compute Engine GPU 区域可用性的约束。
仅当使用 Managed Service for Apache Spark Spark 运行时版本 1.x 时,XGBoost 库才会提供给 Managed Service for Apache Spark GPU 加速工作负载。
Managed Service for Apache Spark 采用 XGBoost 的 GPU 加速批处理作业会利用增加的 Compute Engine 配额。例如,如需运行使用 NVIDIA L4 GPU 的无服务器批处理工作负载,您必须分配 NVIDIA_L4_GPUS 配额。
启用了加速器的作业与
constraints/compute.requireShieldedVm组织政策不兼容。如果您的组织强制执行此政策,则其启用了加速器的作业将无法成功运行。将 CUDA-X GPU 加速与
2.2版本之前的受支持 Managed Service for Apache Spark 运行时搭配使用时,必须将默认字符集设置为 UTF-8。如需了解详情,请参阅创建具有 GPU 加速器的无服务器批处理工作负载。
价格
GPU 加速器适用于高级价格层级。如需了解加速器价格信息,请参阅 Managed Service for Apache Spark 价格。
准备工作
在创建挂接了 GPU 加速器的无服务器批量工作负载之前,请执行以下操作:
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
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.enablepermission. 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.-
Install the Google Cloud CLI.
-
Configure the gcloud CLI to use your federated identity.
For more information, see Sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init - 在 Google Cloud 控制台中,前往 Cloud Storage 存储桶页面。
- 点击 创建。
- 在创建存储桶页面上,输入您的存储桶信息。要转到下一步,请点击继续。
- 在开始使用部分中,执行以下操作:
- 在选择数据存储位置部分中,执行以下操作:
- 在选择数据存储方式部分中,执行以下操作:
- 在选择如何控制对对象的访问权限部分中,选择存储桶是否强制执行禁止公开访问,然后为存储桶对象选择访问权限控制方法。
- 在选择如何保护对象数据部分中,执行以下操作:
- 点击创建。
创建具有 GPU 加速器的无服务器批量工作负载
提交使用 NVIDIA L4 GPU 运行并行化 PySpark 任务的 Managed Service for Apache Spark 批量工作负载。使用 gcloud CLI 按照以下步骤操作:
点击展开,然后使用文本或代码编辑器创建列出的 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() )
在本地机器上使用 gcloud CLI 提交 Managed Service for Apache Spark 无服务器批量作业,该作业有 5 个工作器,每个工作器都使用 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 存储桶的名称。在运行批处理工作负载之前,Spark 会将工作负载依赖项上传到此存储桶中的
/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-40A100 80GB a100-80spark.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。所有其他配置都挂接了1GPU。spark.dataproc.executor.disk.size(必需):L4 GPU 的固定磁盘大小为 375 GB,但具有24、48或96核心的配置除外,这些配置分别具有750、1,500或3,000GB。如果您在提交 L4 加速工作负载时将此属性设置为其他值,则会发生错误。如果您选择 A100 40 或 A100 80 GPU,则有效大小为 375g、750g、1500g、3000g、6000g 和 9000g。spark.executor.memory(可选)和spark.executor.memoryOverhead(受限):您可以设置 memory,但不能设置 memoryOverhead。未设置的属性会应用未被已设置的属性占用的可用内存量。对于 PySpark 批处理工作负载,spark.executor.memoryOverhead设置为可用内存的 40%,对于其他工作负载,则设置为 10%(请参阅 Spark 资源分配属性)。下表显示了可为不同 A100 和 L4 GPU 配置设置的最大内存量。任一属性的最小值为
1024MB。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.SQLPluginspark.executor.resource.gpu.amount=1spark.task.resource.gpu.amount=1/$spark_executor_coresspark.shuffle.manager=''。默认情况下,此属性未设置。NVIDIA 建议在使用 GPU 时开启 CUDA-X shuffle manager,以提升性能。为此,请在提交工作负载时设置spark.shuffle.manager=com.nvidia.spark.rapids.RapidsShuffleManager。spark.rapids.sql.concurrentGpuTasks= (gpuMemoryinMB/ 8, 4) 的最小值spark.rapids.shuffle.multiThreaded.writer.threads=(虚拟机中的 CPU 核心数 / 每个虚拟机的 GPU 数量,32)中的最小值spark.rapids.shuffle.multiThreaded.reader.threads=(虚拟机中的 CPU 核心数 / 每个虚拟机的 GPU 数量,32)中的最小值
请参阅适用于 Apache Spark 的 NVIDIA cuDF 插件配置,以设置适用于 Apache Spark 的 NVIDIA cuDF 库属性;请参阅适用于 Apache Spark 的 NVIDIA cuDF 插件高级配置,以设置 Spark 高级属性。