AWSでのDeepSeek R1モデルのデプロイとファインチューニング方法
Hugging Faceは、AWSサービス上でDeepSeek R1モデルをデプロイおよびファインチューニングするためのステップバイステップガイドを提供します。
DeepSeek‑R1とは?
DeepSeek‑R1は、DeepSeek AIによってリリースされたオープンソースの推論モデルで、LlamaおよびQwenアーキテクチャに基づく6つのディスティルドバージョンが付属しています。このリリースは、推論時にさらに多くのコンピューティングリソースを使用することで、数学、コーディング、論理などのタスクにおける推論性能が向上することを示したOpenAIのo1モデルに続いています。
AWSでのDeepSeek R1モデルのデプロイ
Hugging Face Inference Endpoints、Amazon Bedrock Marketplace、Amazon SageMaker AI(GPUまたはNeuron)、またはHugging Face Neuron Deep Learning AMIを使用したEC2 Neuronを介して、AWS上でDeepSeek R1およびそのディスティルドモデルをデプロイできます。
Hugging Face Inference Endpointsを使用したデプロイ
Hugging Face Inference Endpointsでは、オートスケーリングとスケール・トゥ・ゼロ機能を備えた、任意のDeepSeek R1ディスティルドモデルまたはUnsloth GGUF量子化バージョン用のマネージドエンドポイントを起動できます。ベースモデルの場合、時間あたり約8.30ドルです。デプロイするには、Hugging Faceのモデルページを開き、[Deploy]をクリックし、その後[HF Inference Endpoints]を選択します。ページには最適化されたコンテナと推奨ハードウェアが事前に入力されます。作成後、エンドポイントにクエリを送信できます。チームは、InferentiaインスタンスでのDeepSeekモデルのデプロイを可能にする作業を行っています。
Amazon Bedrock Marketplaceを使用したデプロイ
Bedrock Marketplaceでは、ワンクリックでDeepSeekディスティルドモデル用のSageMakerホストエンドポイントを起動できます。バックグラウンドでAmazon SageMaker AIのエンドポイントがデプロイされます。短いビデオでは、AWSコンソールをナビゲートしてデプロイを完了する方法を示しています。
Hugging Face LLM DLCs(GPU)を使用したAmazon SageMaker AIでのデプロイ
GPUベースの推論では、SageMaker JumpstartまたはPython SDKでHugging Face LLM DLCsを使用できます。各ディスティルドバリアントに対して推奨される特定のインスタンスタイプがあります。
| モデル | インスタンスタイプ | レプリカあたりのGPU数 |
|---|---|---|
| deepseek-ai/DeepSeek-R1-Distill-Llama-70B | ml.g6.48xlarge | 8 |
| deepseek-ai/DeepSeek-R1-Distill-Qwen-32B | ml.g6.12xlarge | 4 |
| deepseek-ai/DeepSeek-R1-Distill-Qwen-14B | ml.g6.12xlarge | 4 |
| deepseek-ai/DeepSeek-R1-Distill-Llama-8B | ml.g6.2xlarge | 1 |
| deepseek-ai/DeepSeek-R1-Distill-Qwen-7B | ml.g6.2xlarge | 1 |
| deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B | ml.g6.2xlarge | 1 |
デプロイする前に、SageMakerドメインが設定されていること、選択したインスタンスタイプに十分なクォータがあること、およびJupyterLabスペースが利用可能であることを確認してください。70B Llamaディスティルドモデルについては、ml.g6.48xlargeのデフォルトクォータを1に引き上げてください。
!pip install sagemaker --upgrade
セッションをインスタンス化し、実行ロールを取得します:
import json
import sagemaker
import boto3
from sagemaker.huggingface import HuggingFaceModel, get_huggingface_llm_image_uri
try:
role = sagemaker.get_execution_role()
except ValueError:
iam = boto3.client("iam
role = iam.get_role(RoleName="sagemaker_execution_role\["Role\]["Arn
Hugging Face Modelオブジェクトを作成します:
model_id = "deepseek-ai/DeepSeek-R1-Distill-Llama-70B"
model_name = model_id.split("\/\["[-1].lower()
hub = {
"HF_MODEL_ID": model_id,
"SM_NUM_GPUS": json.dumps(8)
}
huggingface_model = HuggingFaceModel(
image_uri=get_huggingface_llm_image_uri("huggingface", version="3.0.1\[
env=hub,
role=role,
)
SageMakerエンドポイントにデプロイし、テストします:
endpoint_name = f"{model_name}-ep"
predictor = huggingface_model.deploy(
endpoint_name=endpoint_name,
initial_instance_count=1,
instance_type="ml.g6.48xlarge",
container_startup_health_check_timeout=2400,
)
# send request
predictor.predict({"inputs": "What is the meaning of life?"})
テスト後にエンドポイントを削除します:
predictor.delete_model()
predictor.delete_endpoint()
TGI v3コンテナは、ハードウェアに最適なパラメータを自動的に選択します。
Hugging Face LLM DLCs(Neuron)を使用したAmazon SageMaker AIでのデプロイ
Neuronベースの推論(Trainium/Inferentia)では、HF_NUM_CORESやMAX_BATCH_SIZEなどの環境変数を設定したHugging Face Neuron DLCを使用し、ml.inf2.48xlargeにデプロイします。
GPUデプロイの前提条件と同様です:設定されたSageMakerドメイン、ml.inf2.48xlargeに十分なクォータ、およびJupyterLabスペース。
セッションをインスタンス化し、実行ロールを取得します(上記と同じコード)。
Neuron固有の設定でHugging Face Modelオブジェクトを作成します:
image_uri = get_huggingface_llm_image_uri("huggingface-neuronx", version="0.0.25\[
model_id = "deepseek-ai/DeepSeek-R1-Distill-Llama-70B"
model_name = model_id.split("\/\["[-1].lower()
hub = {
"HF_MODEL_ID": model_id,
"HF_NUM_CORES": "24",
"HF_AUTO_CAST_TYPE": "bf16",
"MAX_BATCH_SIZE": "4",
"MAX_INPUT_TOKENS": "3686",
"MAX_TOTAL_TOKENS": "4096",
}
huggingface_model = HuggingFaceModel(
image_uri=image_uri,
env=hub,
role=role,
)
SageMakerエンドポイントにデプロイし、テストします:
endpoint_name = f"{model_name}-ep"
predictor = huggingface_model.deploy(
endpoint_name=endpoint_name,
initial_instance_count=1,
instance_type="ml.inf2.48xlarge",
container_startup_health_check_timeout=3600,
volume_size=512,
)
# send request
predictor.predict(
{
"inputs": "What is is the capital of France?",
"parameters": {
"do_sample": True,
"max_new_tokens": 128,
"temperature": 0.7,
"top_k": 50,
"top_p": 0.95,
}
}
)
以前に示したとおり、テスト後にエンドポイントを削除します。
Hugging Face Neuron Deep Learning AMIを使用したEC2 Neuronでのデプロイ
Hugging Face Neuron Deep Learning AMIとTGIエンドポイントを起動するDockerコマンドを使用して、EC2 inf2.48xlargeインスタンスでDeepSeek R1ディスティルドモデルを実行できます。
まず、AWS MarketplaceでHugging Face Neuron Deep Learning AMIをサブスクライブし、そのAMIを使用してinf2.48xlargeインスタンスを起動し、SSHで接続します。
その後、エンドポイントを起動します:
docker run -p 8080:80 \
-v $(pwd)/data:/data \
--device=/dev/neuron0 \
--device=/dev/neuron1 \
--device=/dev/neuron2 \
--device=/dev/neuron3 \
--device=/dev/neuron4 \
--device=/dev/neuron5 \
--device=/dev/neuron6 \
--device=/dev/neuron7 \
--device=/dev/neuron8 \
--device=/dev/neuron9 \
--device=/dev/neuron10 \
--device=/dev/neuron11 \
-e HF_BATCH_SIZE=4 \
-e HF_SEQUENCE_LENGTH=4096 \
-e HF_AUTO_CAST_TYPE="bf16" \
-e HF_NUM_CORES=24 \
ghcr.io/huggingface/neuronx-tgi:latest \
--model-id deepseek-ai/DeepSeek-R1-Distill-Llama-70B \
--max-batch-size 4 \
--max-total-tokens 4096
コンパイル済みモデルのダウンロードとTGIエンドポイントの起動に数分待ちます。
エンドポイントをテストします:
curl localhost:8080/generate \
-X POST \
-d '{"inputs":"Why is the sky dark at night?"}' \
-H 'Content-Type: application/json'
テストが終了したらEC2インスタンスを一時停止します。チームは、このAMIを使用してTrainiumおよびInferentiaでのDeepSeek R1デプロイを可能にする作業を行っています。
AWSでのDeepSeek R1モデルのファインチューニング
AWSでのDeepSeek R1モデルのファインチューニングは現在有効化されつつあります。このガイドでは、SageMakerでのHugging Face Training DLCおよびNeuron Deep Learning AMIの今後のサポートについて言及しています。
Hugging Face Training DLCsを使用したAmazon SageMaker AIでのファインチューニング
チームは、SageMakerでのHugging Face Training DLCを使用したDeepSeek R1モデルの完全なファインチューニングを有効にする作業を行っています。詳細については後日公開されます。
Hugging Face Neuron Deep Learning AMIを使用したEC2 Neuronでのファインチューニング
同様に、Hugging Face Neuron Deep Learning AMIを使用したNeuronインスタンスでのファインチューニングは開発中です。