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

1. 简介

概览

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

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

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

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

学习内容

  • 如何使用 GPU 在 Cloud Run 上使用 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 应用

首先,为源代码创建一个目录,然后通过 cd 命令进入该目录。

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 文档。

所学内容

  • 如何使用 GPU 在 Cloud Run 上使用 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 来查看所有可用项目的列表。