最適化された大規模言語モデル (LLM) サービング
プレビュー
この機能は パブリック プレビュー段階です。
このガイドのコード例では、非推奨の APIs. Databricks では、LLM の推論を最適化するために 、プロビジョニングされたスループット エクスペリエンスを使用することをお勧めします。 「 最適化された LLM サービスエンドポイントをプロビジョニングされたスループットに移行する」を参照してください。
この記事では、Mosaic AI Model Serving で大規模言語モデル (LLM) の最適化を有効にする方法について説明します。
最適化されたLLMサービングは、従来のサービングアプローチと比較して、スループットとレイテンシを3〜5倍改善します。 次の表は、サポートされている LLM ファミリとそのバリアントをまとめたものです。
Databricks では、Databricks Marketplace を使用して基盤モデルをインストールすることをお勧めします。 モデル ファミリを検索し、モデル ページから [ アクセスを取得 ] を選択し、ログイン資格情報を入力して、モデルを Unity Catalog にインストールできます。
モデルファミリー | Marketplace からのインストール |
---|---|
Llama 2 | |
MPT | |
Mistral |
必要条件
-
最適化された LLM サービスは、 GPU デプロイのパブリック プレビューの一部としてサポートされています。
-
モデルは、MLflow 2.4 以降または Databricks Runtime 13.2 ML 以降を使用してログに記録する必要があります。
-
Databricks では、大規模なモデルのアップロードとダウンロードを高速化するために、Unity Catalog のモデルを使用することをお勧めします。
-
モデルをデプロイするときは、モデルのパラメーター サイズを適切なコンピュート サイズと一致させることが重要です。 推奨事項については、次の表を参照してください。 500 億以上のパラメーターを持つモデルの場合は、Databricks アカウント チームに連絡して、必要な GPU にアクセスしてください。
モデル パラメーターのサイズ
コンピュートの推奨サイズ
GPUタイプ
70億
1xA10
GPU_MEDIUM
130億
4xA10
MULTIGPU_MEDIUM
300億から340億
4xA10
MULTIGPU_MEDIUM
700億
8xA10 または 8xA100
GPU_MEDIUM_8
またはGPU_LARGE_8
大規模言語モデルをログに記録する
まず、MLflow transformers
フレーバーを使用してモデルをログに記録し、MLflow メタデータのタスク フィールドを metadata = {"task": "llm/v1/completions"}
で指定します。 これにより、モデルサービング エンドポイントに使用される API シグネチャが指定されます。
最適化された LLM サービスは、Databricks AI Gateway でサポートされているルートの種類と互換性があります。現在、 llm/v1/completions
. サポート対象外のモデルファミリーまたはタスクタイプを提供したい場合は、Databricks アカウントチームにお問い合わせください。
model = AutoModelForCausalLM.from_pretrained("mosaicml/mpt-7b-instruct",torch_dtype=torch.bfloat16, trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained("mosaicml/mpt-7b-instruct")
with mlflow.start_run():
components = {
"model": model,
"tokenizer": tokenizer,
}
mlflow.transformers.log_model(
artifact_path="model",
transformers_model=components,
input_example=["Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n### Instruction:\nWhat is Apache Spark?\n\n### Response:\n"],
metadata={"task": "llm/v1/completions"},
registered_model_name='mpt'
)
モデルがログに記録されたら、次のようにしてモデルを Unity Catalog に登録し、CATALOG.SCHEMA.MODEL_NAME
をモデルの 3 レベルの名前に置き換えることができます。
mlflow.set_registry_uri("databricks-uc")
registered_model_name=CATALOG.SCHEMA.MODEL_NAME
モデルサービングエンドポイントを作成する
次に、モデルサービングエンドポイントを作成します。 モデルが最適化された LLM サービングでサポートされている場合、 Databricks に配信しようとすると、最適化されたモデルサービングエンドポイントが自動的に作成されます。
import requests
import json
# Set the name of the MLflow endpoint
endpoint_name = "llama2-3b-chat"
# Name of the registered MLflow model
model_name = "ml.llm-catalog.llama-13b"
# Get the latest version of the MLflow model
model_version = 3
# Specify the type of compute (CPU, GPU_SMALL, GPU_MEDIUM, etc.)
workload_type = "GPU_MEDIUM"
# Specify the scale-out size of compute (Small, Medium, Large, etc.)
workload_size = "Small"
# Specify Scale to Zero (only supported for CPU endpoints)
scale_to_zero = False
# Get the API endpoint and token for the current notebook context
API_ROOT = dbutils.notebook.entry_point.getDbutils().notebook().getContext().apiUrl().get()
API_TOKEN = dbutils.notebook.entry_point.getDbutils().notebook().getContext().apiToken().get()
# send the POST request to create the serving endpoint
data = {
"name": endpoint_name,
"config": {
"served_models": [
{
"model_name": model_name,
"model_version": model_version,
"workload_size": workload_size,
"scale_to_zero_enabled": scale_to_zero,
"workload_type": workload_type,
}
]
},
}
headers = {"Context-Type": "text/json", "Authorization": f"Bearer {API_TOKEN}"}
response = requests.post(
url=f"{API_ROOT}/api/2.0/serving-endpoints", json=data, headers=headers
)
print(json.dumps(response.json(), indent=4))
入力および出力スキーマの形式
最適化された LLM サービス エンドポイントには、Databricks が制御する入力スキーマと出力スキーマがあります。 4つの異なる形式がサポートされています。
-
dataframe_split
は、split
方向の JSON シリアル化された Pandas データフレーム です。JSON{
"dataframe_split": {
"columns": ["prompt"],
"index": [0],
"data": [
[
"Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n### Instructions:\nWhat is Apache Spark?\n\n### Response:\n"
]
]
},
"params": {
"temperature": 0.5,
"max_tokens": 100,
"stop": ["word1", "word2"],
"candidate_count": 1
}
} -
dataframe_records
は、records
方向の JSON シリアル化された Pandas データフレーム です。JSON{
"dataframe_records": [
{
"prompt": "Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n### Instructions:\nWhat is Apache Spark?\n\n### Response:\n"
}
],
"params": {
"temperature": 0.5,
"max_tokens": 100,
"stop": ["word1", "word2"],
"candidate_count": 1
}
} -
実行
JSON{
"instances": [
{
"prompt": "Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n### Instructions:\nWhat is Apache Spark?\n\n### Response:\n"
}
],
"params": {
"temperature": 0.5,
"max_tokens": 100,
"stop": ["word1", "word2"],
"candidate_count": 1
}
} -
入力
JSON{
"inputs": {
"prompt": "Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n### Instructions:\nWhat is Apache Spark?\n\n### Response:\n"
},
"params": {
"temperature": 0.5,
"max_tokens": 100,
"stop": ["word1", "word2"],
"candidate_count": 1
}
}
エンドポイントのクエリ
エンドポイントの準備ができたら、API リクエストを行ってクエリを実行できます。 モデルのサイズと複雑さによっては、エンドポイントの準備に 30 分以上かかる場合があります。
data = {
"inputs": {
"prompt": [
"Hello, I'm a language model,"
]
},
"params": {
"max_tokens": 100,
"temperature": 0.0
}
}
headers = {"Context-Type": "text/json", "Authorization": f"Bearer {API_TOKEN}"}
response = requests.post(
url=f"{API_ROOT}/serving-endpoints/{endpoint_name}/invocations", json=data, headers=headers
)
print(json.dumps(response.json()))
制限
-
GPU で提供されるモデルのインストール要件が高まっているため、GPU サービス用のコンテナイメージの作成は、CPU サービス用のイメージ作成よりも時間がかかります。
- モデル サイズもイメージの作成に影響します。 たとえば、300 億個以上のパラメーターを持つモデルの構築には、少なくとも 1 時間かかる場合があります。
- Databricks は、次に同じバージョンのモデルをデプロイするときに同じコンテナーを再利用するため、後続のデプロイにかかる時間が短縮されます。
-
GPUコンピュートで提供されるモデルのセットアップ時間が長くなるため、GPUサービングのオートスケールはCPUサービングよりも時間がかかります。 Databricks では、要求のタイムアウトを回避するために、オーバープロビジョニングをお勧めします。