カスタム重みを使用したモデルのデプロイはプレビュー版です。事前定義された一連のベースモデルに基づいてモデルをファインチューニングし、カスタマイズしたモデルを Vertex AI Model Garden にデプロイできます。カスタムモデルをデプロイするには、カスタム ウェイトのインポートを使用して、モデル アーティファクトをプロジェクトの Cloud Storage バケットにアップロードします。これは、Vertex AI でワンクリックで実行できます。
サポートされているモデル
カスタムの重み付けでモデルをデプロイするの公開プレビュー版は、次のベースモデルでサポートされています。
モデル名 | バージョン |
---|---|
Llama |
|
Gemma |
|
Qwen |
|
Deepseek |
|
Mistral と Mixtral |
|
Phi-4 |
|
OpenAI OSS |
|
制限事項
カスタムの重み付けでは、量子化モデルのインポートはサポートされていません。
モデルファイル
モデルファイルは Hugging Face の重み形式で指定する必要があります。Hugging Face の重み形式の詳細については、Hugging Face モデルを使用するをご覧ください。
必要なファイルが提供されていない場合、モデルのデプロイが失敗する可能性があります。
次の表に、モデルのアーキテクチャに応じて異なるモデルファイルの種類を示します。
モデルファイルの内容 | ファイル形式 |
---|---|
モデル設定 |
|
モデルの重み |
|
重みインデックス |
|
トークナイザー ファイル |
|
ロケーション
Model Garden サービスからすべてのリージョンにカスタムモデルをデプロイできます。
前提条件
このセクションでは、カスタムモデルをデプロイする方法について説明します。
始める前に
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
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
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles. -
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
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles. -
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
- REGION: リージョン。例:
uscentral1
- MODEL_GCS: Google Cloud モデル。例:
gs://custom-weights-fishfooding/meta-llama/Llama-3.2-1B-Instruct
。 - PROJECT_ID: 実際のプロジェクト ID。
- MODEL_ID: モデル ID。
- MACHINE_TYPE: マシンタイプ。例:
g2-standard-12
- ACCELERATOR_TYPE: アクセラレータのタイプ。例:
NVIDIA_L4
- ACCELERATOR_COUNT: アクセラレータの数。
PROMPT: テキスト プロンプト。
Google Cloud コンソールで、[Model Garden] ページに移動します。
[カスタム重みを使用してモデルをデプロイ] をクリックします。[Vertex AI でカスタム重みを使用してモデルをデプロイする] ペインが表示されます。
[モデルソース] セクションで、次の操作を行います。
[参照] をクリックし、モデルが保存されているバケットを選択して、[選択] をクリックします。
省略可: [モデル名] フィールドにモデルの名前を入力します。
[デプロイの設定] セクションで、次の操作を行います。
[リージョン] フィールドでリージョンを選択し、[OK] をクリックします。
[マシン仕様] フィールドで、モデルのデプロイに使用されるマシン仕様を選択します。
省略可: [エンドポイント名] フィールドには、デフォルトでモデルのエンドポイントが表示されます。ただし、フィールドに別のエンドポイント名を入力することはできます。
[カスタム重みを使用してモデルをデプロイ] をクリックします。
Google Cloud コンソールで、[Model Garden] ページに移動します。
[エンドポイントとモデルを表示] をクリックします。
[リージョン] リストからリージョンを選択します。
エンドポイント ID とエンドポイント URL を取得するには、[マイ エンドポイント] セクションでエンドポイントをクリックします。
エンドポイント ID が [エンドポイント ID] フィールドに表示されます。
パブリック エンドポイント URL が [専用エンドポイント] フィールドに表示されます。
モデル ID を取得するには、[デプロイされたモデル] セクションにリストされているモデルを見つけて、次の操作を行います。
- [モデル] フィールドで、デプロイ済みモデルの名前をクリックします。
- [バージョンの詳細] をクリックします。[モデル ID] フィールドにモデル ID が表示されます。
- セルフデプロイ モデルの詳細については、セルフデプロイ モデルの概要をご覧ください。
- Model Garden の詳細については、Model Garden の概要をご覧ください。
- モデルのデプロイの詳細については、Model Garden でモデルを使用するをご覧ください。
- Gemma オープンモデルを使用する
- Llama のオープンモデルを使用する
- Hugging Face オープンモデルを使用する
このチュートリアルでは、Cloud Shell を使用して Google Cloudを操作していることを前提としています。Cloud Shell の代わりに別のシェルを使用する場合は、次の追加の構成を行います。
カスタムモデルをデプロイする
このセクションでは、カスタムモデルをデプロイする方法について説明します。
コマンドライン インターフェース(CLI)、Python、JavaScript を使用している場合は、次の変数をコードサンプルが機能する値に置き換えます。
コンソール
次の手順では、 Google Cloud コンソールを使用してカスタム重みでモデルをデプロイする方法について説明します。
gcloud CLI
このコマンドは、特定のリージョンにモデルをデプロイする方法を示しています。
gcloud ai model-garden models deploy --model=${MODEL_GCS} --region ${REGION}
このコマンドは、マシンタイプ、アクセラレータ タイプ、アクセラレータ数を使用して、モデルを特定のリージョンにデプロイする方法を示しています。特定のマシン構成を選択する場合は、3 つのフィールドすべてを設定する必要があります。
gcloud ai model-garden models deploy --model=${MODEL_GCS} --machine-type=${MACHINE_TYE} --accelerator-type=${ACCELERATOR_TYPE} --accelerator-count=${ACCELERATOR_COUNT} --region ${REGION}
Python
import vertexai
from google.cloud import aiplatform
from vertexai.preview import model_garden
vertexai.init(project=${PROJECT_ID}, location=${REGION})
custom_model = model_garden.CustomModel(
gcs_uri=GCS_URI,
)
endpoint = custom_model.deploy(
machine_type="${MACHINE_TYPE}",
accelerator_type="${ACCELERATOR_TYPE}",
accelerator_count="${ACCELERATOR_COUNT}",
model_display_name="custom-model",
endpoint_display_name="custom-model-endpoint")
endpoint.predict(instances=[{"prompt": "${PROMPT}"}], use_dedicated_endpoint=True)
また、custom_model.deploy()
メソッドにパラメータを渡す必要はありません。
import vertexai
from google.cloud import aiplatform
from vertexai.preview import model_garden
vertexai.init(project=${PROJECT_ID}, location=${REGION})
custom_model = model_garden.CustomModel(
gcs_uri=GCS_URI,
)
endpoint = custom_model.deploy()
endpoint.predict(instances=[{"prompt": "${PROMPT}"}], use_dedicated_endpoint=True)
curl
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
"https://${REGION}-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/${REGION}:deploy" \
-d '{
"custom_model": {
"gcs_uri": "'"${MODEL_GCS}"'"
},
"destination": "projects/'"${PROJECT_ID}"'/locations/'"${REGION}"'",
"model_config": {
"model_user_id": "'"${MODEL_ID}"'",
},
}'
または、API を使用してマシンタイプを明示的に設定することもできます。
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
"https://${REGION}-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/${REGION}:deploy" \
-d '{
"custom_model": {
"gcs_uri": "'"${MODEL_GCS}"'"
},
"destination": "projects/'"${PROJECT_ID}"'/locations/'"${REGION}"'",
"model_config": {
"model_user_id": "'"${MODEL_ID}"'",
},
"deploy_config": {
"dedicated_resources": {
"machine_spec": {
"machine_type": "'"${MACHINE_TYPE}"'",
"accelerator_type": "'"${ACCELERATOR_TYPE}"'",
"accelerator_count": '"${ACCELERATOR_COUNT}"'
},
"min_replica_count": 1
}
}
}'
クエリを作成する
モデルがデプロイされると、カスタムの重み付けはパブリック専用エンドポイントをサポートします。クエリは、API または SDK を使用して送信できます。
クエリを送信する前に、 Google Cloud コンソールでエンドポイント URL、エンドポイント ID、モデル ID を取得する必要があります。
情報を取得する手順は次のとおりです。
エンドポイントとデプロイされたモデルの情報を取得したら、次のコードサンプルで推論リクエストの送信方法を確認するか、専用パブリック エンドポイントにオンライン推論リクエストを送信するをご覧ください。
API
次のコードサンプルは、ユースケースに基づいて API を使用するさまざまな方法を示しています。
チャットの補完(単項)
このリクエストのサンプルでは、完全なチャット メッセージをモデルに送信し、レスポンス全体が生成された後に、レスポンスを 1 つのチャンクで取得します。これは、テキスト メッセージを送信して 1 つの完全な返信を受け取るのと同様です。
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
"https://${ENDPOINT_URL}/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION}/endpoints/${ENDPOINT_ID}/chat/completions" \
-d '{
"model": "'"${MODEL_ID}"'",
"temperature": 0,
"top_p": 1,
"max_tokens": 154,
"ignore_eos": true,
"messages": [
{
"role": "user",
"content": "How to tell the time by looking at the sky?"
}
]
}'
チャットの補完(ストリーミング)
このリクエストは、単項チャット完了リクエストのストリーミング バージョンです。リクエストに "stream": true
を追加すると、モデルはレスポンスを生成と同時に少しずつ送信します。これは、チャット アプリケーションでリアルタイムのタイプライターのような効果を作成するのに便利です。
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \ "https://${ENDPOINT_URL}/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION}/endpoints/${ENDPOINT_ID}/chat/completions" \
-d '{
"model": "'"${MODEL_ID}"'",
"stream": true,
"temperature": 0,
"top_p": 1,
"max_tokens": 154,
"ignore_eos": true,
"messages": [
{
"role": "user",
"content": "How to tell the time by looking at the sky?"
}
]
}'
予測
このリクエストは、モデルから推論を取得するための直接プロンプトを送信します。これは、テキストの要約や分類など、必ずしも会話型ではないタスクでよく使用されます。
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
"https://${ENDPOINT_URL}/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION}/endpoints/${ENDPOINT_ID}:predict" \
-d '{
"instances": [
{
"prompt": "How to tell the time by looking at the sky?",
"temperature": 0,
"top_p": 1,
"max_tokens": 154,
"ignore_eos": true
}
]
}'
未加工の予測
このリクエストは、Predict リクエストのストリーミング バージョンです。:streamRawPredict
エンドポイントを使用して "stream": true
を含めることで、このリクエストは直接プロンプトを送信し、生成されたモデルの出力を連続したデータ ストリームとして受け取ります。これは、ストリーミング チャット完了リクエストに似ています。
curl -X POST \
-N \
--output - \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
"https://${ENDPOINT_URL}/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION}/endpoints/${ENDPOINT_ID}:streamRawPredict" \
-d '{
"instances": [
{
"prompt": "How to tell the time by looking at the sky?",
"temperature": 0,
"top_p": 1,
"max_tokens": 154,
"ignore_eos": true,
"stream": true
}
]
}'
SDK
このコードサンプルでは、SDK を使用してモデルにクエリを送信し、そのモデルからレスポンスを取得します。
from google.cloud import aiplatform
project_id = ""
location = ""
endpoint_id = "" # Use the short ID here
aiplatform.init(project=project_id, location=location)
endpoint = aiplatform.Endpoint(endpoint_id)
prompt = "How to tell the time by looking at the sky?"
instances=[{"text": prompt}]
response = endpoint.predict(instances=instances, use_dedicated_endpoint=True)
print(response.predictions)
API の使用例については、カスタム重みをインポートするノートブックをご覧ください。