关于此 Codelab
1. 简介
概览
在此 Codelab 中,您将使用 Cloud Run 作业来使用 Meta 的 Llama 3.2-1b LLM 和 vLLM(由 Cloud Run 作业 GPU 提供支持)运行批量推理。您将使用 Cloud Run 卷装载将结果直接写入 Cloud Storage。
学习内容
- 如何使用 Cloud Run 作业 GPU 进行批量推理
- 如何使用 Cloud Run 卷装载写入 Cloud Storage
2. 准备工作
启用 API
在开始使用此 Codelab 之前,请通过运行以下命令启用以下 API:
gcloud services enable run.googleapis.com \
cloudbuild.googleapis.com \
secretmanager.googleapis.com \
artifactregistry.googleapis.com
GPU 配额
为受支持的区域申请增加配额。配额为 nvidia_l4_gpu_allocation_no_zonal_redundancy
,位于 Cloud Run Admin API 下。
注意:如果您使用的是新项目,则可能需要几分钟时间,API 配额才会显示在此页面中。
Hugging Face
此 Codelab 使用托管在 Hugging Face 上的模型。如需获取此模型,请请求具有“读取”权限的 Hugging Face 用户访问令牌。在下文中,我们会用 YOUR_HF_TOKEN
来引用它。
您还需要同意使用条款才能使用该模型:https://huggingface.co/meta-llama/Llama-3.2-1B
3. 设置和要求
本部分包含设置以下资源的说明:
- IAM 服务账号和关联的 IAM 权限。
- 用于存储 Hugging Face 令牌的 Secret Manager Secret。
- 用于存储推理结果的 Cloud Storage 存储分区。
如需设置所需资源,请按以下步骤操作:
- 为此 Codelab 设置环境变量:
export PROJECT_ID=<your_project_id>
export REGION=<your_region>
export HF_TOKEN=<YOUR_HF_TOKEN>
export SERVICE_ACCOUNT=inference-service-account
export SERVICE_ACCOUNT_EMAIL=${SERVICE_ACCOUNT}@${PROJECT_ID}.iam.gserviceaccount.com
export SECRET_ID=hugging-face-token
export BUCKET_NAME=inference-codelab-${PROJECT_ID} - 运行以下命令来创建服务账号:
gcloud iam service-accounts create ${SERVICE_ACCOUNT} \
--display-name="Service account for batch inference codelab" - 使用 Secret Manager 存储 Hugging Face 访问令牌:
gcloud secrets create $SECRET_ID \
--replication-policy="automatic"
printf $HF_TOKEN | gcloud secrets versions add $SECRET_ID --data-file=- - 向您的服务账号授予 Secret Manager Secret Accessor 角色:
gcloud secrets add-iam-policy-binding $SECRET_ID \
--member serviceAccount:$SERVICE_ACCOUNT_EMAIL \
--role='roles/secretmanager.secretAccessor' - 创建一个存储分区来托管经过微调的模型:
gcloud storage buckets create -l us-central1 gs://${BUCKET_NAME}
- 向您的服务账号授予对存储分区的访问权限:
gcloud storage buckets add-iam-policy-binding gs://$BUCKET_NAME \
--member=serviceAccount:$SERVICE_ACCOUNT_EMAIL \
--role=roles/storage.objectAdmin - 创建一个 Artifact Registry 仓库来存储容器映像。如果您之前在项目中使用过 Cloud Run 源代码部署,请跳过此步骤。
gcloud artifacts repositories create cloud-run-source-deploy \
--repository-format=docker \
--location=$REGION \
--project=$PROJECT_ID
4. 创建 Cloud Run 作业
在本部分中,您将创建以下代码:
- 从 Hugging Face 导入 Llama 模型
- 对模型执行批量推理。该作业在此过程中使用单个 L4 GPU。
- 将结果写入本地磁盘。系统会通过卷挂载将其写入 Cloud Storage。
如需创建 Cloud Run 作业和 Dockerfile,请按以下步骤操作:
- 创建一个目录来托管微调作业代码:
mkdir codelab-inference-job
cd codelab-inference-job - 创建一个名为
main.py
的文件# SPDX-License-Identifier: Apache-2.0
from vllm import LLM, SamplingParams
# Sample prompts.
prompts = [
"Cloud Run is",
"The future of AI is",
"The capital of Germany is",
"python as a programming language is",
]
# Create a sampling params object.
sampling_params = SamplingParams(temperature=0.8, top_p=0.95)
# Create an LLM.
llm = LLM(model="meta-llama/Llama-3.2-1B")
# Generate texts from the prompts. The output is a list of RequestOutput objects
# that contain the prompt, generated text, and other information.
outputs = llm.generate(prompts, sampling_params)
# Save the outputs to disk
with open("/results/output.txt", "w") as f:
for output in outputs:
prompt = output.prompt
generated_text = output.outputs[0].text
f.write(f"Prompt: {prompt!r}, Generated text: {generated_text!r}\n")
print(f"Wrote {len(outputs)} to disk.") - 创建
Dockerfile
:FROM python:3.12
ADD main.py .
RUN python -m pip install --upgrade pip setuptools
RUN pip install vllm
CMD ["python", "./main.py"]
5. 部署和执行作业
在此步骤中,您将使用 Cloud Run 源代码部署创建 Cloud Run 作业,然后执行该作业。此步骤还包含用于存储结果的 Cloud Run 卷挂载标志。
- 创建 Cloud Run 作业:
此命令会根据源代码构建映像并部署作业。此过程需要一些时间才能完成。gcloud beta run jobs deploy inference-job \
--region $REGION \
--source . \
--gpu=1 \
--set-secrets HF_TOKEN=${SECRET_ID}:latest \
--add-volume name=results,type=cloud-storage,bucket=${BUCKET_NAME} \
--add-volume-mount volume=results,mount-path=/results \
--service-account $SERVICE_ACCOUNT_EMAIL - 执行作业:
该作业需要几分钟才能完成。您可以使用上一个命令的输出中提供的链接来查看状态。gcloud run jobs execute inference-job --region $REGION --async
确认成功
如需确认作业是否已成功执行,请查看上一个命令的输出中显示的作业日志。
在 Cloud Storage 存储分区中查看结果:
在控制台中:
- 前往 Cloud Storage。
- 选择以
inference-codelab
开头的存储分区。 - 选择
output.txt
。 - 点击要求验证身份的网址,在浏览器中查看相应内容。
文件中应包含四个问题及其输出。