1. はじめに
概要
Cloud Run では、最近 GPU のサポートが追加されました。この機能は、ウェイティング リストに登録したユーザー向けの公開プレビュー版として提供されています。この機能を試してみたい場合は、こちらのフォームにご記入のうえ、順番待ちリストにご登録ください。Cloud Run は、Google Cloud 上のコンテナ プラットフォームです。クラスタを管理することなく、コンテナでコードを簡単に実行できます。
現在、利用可能な GPU は 24 GB の vRAM を搭載した Nvidia L4 GPU です。Cloud Run インスタンスごとに 1 つの GPU があり、Cloud Run の自動スケーリングは引き続き適用されます。これには、最大 5 つのインスタンスへのスケールアウト(割り当ての増加が可能)と、リクエストがない場合のゼロ インスタンスへのスケールダウンが含まれます。
Transformers.js は、Hugging Face の transformers 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 アプリを作成する
まず、ソースコードのディレクトリを作成し、そのディレクトリに移動します。
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 サービスを削除するには、https://console.cloud.google.com/run で Cloud Run Cloud Console に移動し、transformers-js-codelab サービスを削除します。
プロジェクト全体を削除する場合は、https://console.cloud.google.com/cloud-resource-manager に移動し、ステップ 2 で作成したプロジェクトを選択して、[削除] を選択します。プロジェクトを削除した場合は、Cloud SDK でプロジェクトを変更する必要があります。gcloud projects list を実行すると、使用可能なすべてのプロジェクトのリストを表示できます。