如何在 Cloud Run GPU 上运行 Transformers.js

1. 简介

概览

Cloud Run 最近添加了 GPU 支持。此功能目前属于公开预览版功能,需要加入等待名单才能使用。如果您有兴趣试用此功能,请填写此表单以加入等候名单。Cloud Run 是 Google Cloud 上的容器平台,可让您轻松在容器中运行代码,而无需管理集群。

目前,我们提供的 GPU 是 Nvidia L4 GPU,配备 24 GB vRAM。每个 Cloud Run 实例有一个 GPU,并且 Cloud Run 自动扩缩功能仍然适用。这包括横向扩容到最多 5 个实例(可增加配额),以及在没有请求时缩减到零个实例。

Transformers.js 的功能与 Hugging Face 的 transformers Python 库的功能相同,这意味着您可以使用非常相似的 API 运行相同的预训练模型。如需了解详情,请访问 Transformers.js 网站

在此 Codelab 中,您将创建应用并将其部署到 Cloud Run,该应用使用 Transformers.js 和 GPU。

学习内容

  • 如何在 Cloud Run 上使用 GPU 运行使用 Transformers.js 的应用

2. 启用 API 并设置环境变量

在开始使用此 Codelab 之前,您需要启用多个 API。此 Codelab 需要使用以下 API。您可以通过运行以下命令来启用这些 API:

gcloud services enable run.googleapis.com \
    storage.googleapis.com \
    cloudbuild.googleapis.com \

然后,您可以设置将在整个 Codelab 中使用的环境变量。

PROJECT_ID=<YOUR_PROJECT_ID>

AR_REPO_NAME=repo
REGION=us-central1

3. 创建 Transformers.js 应用

首先,为源代码创建一个目录,然后进入该目录。

mkdir transformers-js-codelab && cd $_

创建 package.json 文件。

{
    "name": "huggingface",
    "version": "1.0.0",
    "main": "index.js",
    "type": "module",
    "scripts": {
      "test": "echo \"Error: no test specified\" && exit 1"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "description": "",
    "dependencies": {
      "@huggingface/transformers": "^3.0.0-alpha.8",
      "@xenova/transformers": "^2.17.2",
      "express": "^4.17.1"
    }
  }

创建一个名为 index.js 的文件

import { pipeline } from "@xenova/transformers";

import express from 'express';

// make sure the text-generation pipeline is created first
// before anyone can access the routes
const generator = await pipeline('text-generation', 'Xenova/llama2.c-stories15M', {
    device: 'cuda',
    dtype: 'fp32',
});

// now create the app and routes
const app = express();

app.get('/', async (req, res) => {
  const text = 'A long time ago in a galaxy far far away,';
  const output = await generator(text, { max_new_tokens: 50 });
  res.send(output);
});

const port = parseInt(process.env.PORT) || 8080;
app.listen(port, () => {
  console.log(`transformers-js app: listening on port ${port}`);
});

创建 Dockerfile。dockerfile 将安装 Transformers.js 所需的其他 NVIDIA 驱动程序

FROM node:20
WORKDIR /usr/src/app

RUN apt-get update && \
 apt-get install software-properties-common -y && \
 wget https://developer.download.nvidia.com/compute/cuda/repos/debian12/x86_64/cuda-keyring_1.1-1_all.deb && \
 dpkg -i cuda-keyring_1.1-1_all.deb && \
 add-apt-repository contrib && \
 apt-get update && \
 apt-get -y install cuda-toolkit-12-6 && \
 apt-get -y install cudnn-cuda-12

EXPOSE 8080
COPY package.json .

RUN npm install

COPY index.js .
ENTRYPOINT ["node", "index.js"]

4. 构建并部署 Cloud Run 服务

在 Artifact Registry 中创建代码库。

gcloud artifacts repositories create $AR_REPO_NAME \
  --repository-format docker \
  --location us-central1

将代码提交到 Cloud Build。

IMAGE=us-central1-docker.pkg.dev/$PROJECT_ID/$AR_REPO_NAME/gpu-transformers-js
gcloud builds submit --tag $IMAGE

接下来,部署到 Cloud Run

gcloud beta run deploy transformers-js-codelab \
 --image=$IMAGE \
 --cpu 8 --memory 32Gi \
 --gpu=1 --no-cpu-throttling --gpu-type nvidia-l4 \
 --allow-unauthenticated \
 --region us-central1 \
 --project=$PROJECT_ID \
 --max-instances 1

5. 测试服务

您可以通过运行以下命令来测试服务:

SERVICE_URL=$(gcloud run services describe transformers-js-codelab --region $REGION --format 'value(status.url)')

curl $SERVICE_URL

您将看到类似以下内容:

[{"generated_text":"A long time ago in a galaxy far far away, there was a beautiful garden. Every day, the little girl would go to the garden and look at the flowers. She loved the garden so much that she would come back every day to visit it.\nOne day, the little girl was walking through"}]

6. 恭喜!

恭喜您完成此 Codelab!

我们建议您查看有关 Cloud Run GPU 的文档。

所学内容

  • 如何在 Cloud Run 上使用 GPU 运行使用 Transformers.js 的应用

7. 清理

为避免意外产生费用(例如,如果 Cloud Run 服务被意外调用次数超过免费层级中的每月 Cloud Run 调用分配),您可以删除 Cloud Run 或删除在第 2 步中创建的项目。

如需删除 Cloud Run 服务,请前往 Cloud Run Cloud 控制台 (https://console.cloud.google.com/run),然后删除 transformers-js-codelab 服务。

如果您选择删除整个项目,可以前往 https://console.cloud.google.com/cloud-resource-manager,选择在第 2 步中创建的项目,然后选择“删除”。如果您删除了项目,则需要在 Cloud SDK 中更改项目。您可以通过运行 gcloud projects list 查看所有可用项目的列表。