如何在 AWS 上部署和微调 DeepSeek 模型
一份运行中的文档,展示如何在 AWS 上使用 Hugging Face 部署和微调 DeepSeek R1 模型。
什么是 DeepSeek-R1?
如果你曾被一个棘手的数学问题困扰,你就会知道多花点时间仔细思考和解决问题是多么有用。 OpenAI 的 o1 模型 表明,当 LLM 被训练以在推理时使用更多计算来做到这一点时,它们在解决数学、编码和逻辑等推理任务方面的表现会显著提升。
然而,OpenAI 推理模型背后的秘诀一直被严密保守。直到上周,DeepSeek 发布了他们的 DeepSeek-R1 模型,并迅速轰动了互联网(甚至股市!)。
DeepSeek AI 开源了 DeepSeek-R1-Zero、DeepSeek-R1,以及从 DeepSeek-R1 蒸馏而来的六个基于 Llama 和 Qwen 架构的密集模型。您可以在 DeepSeek R1 集合中找到它们。
我们与亚马逊网络服务合作,使开发人员更容易在 AWS 服务上部署最新的 Hugging Face 模型,以构建更好的生成式 AI 应用程序。
让我们回顾一下如何在 AWS 上使用 Hugging Face 部署和微调 DeepSeek R1 模型。
部署 DeepSeek R1 模型
使用 Hugging Face 推理端点在 AWS 上部署
Hugging Face 推理端点 提供了一种简单且安全的方式,用于在 AWS 上的专用计算资源上部署机器学习模型以供生产使用。推理端点使开发人员和数据科学家都能创建 AI 应用程序而无需管理基础设施:简化部署过程到只需点击几下,包括处理大量请求的自动扩缩、通过按需扩展到零来降低基础设施成本以及提供高级安全性。
借助推理端点,您可以部署 DeepSeek-R1 的 6 个蒸馏模型中的任何一个,以及由 Unsloth 创建的 DeepSeek R1 量化版本:https://huggingface.co/unsloth/DeepSeek-R1-GGUF。在模型页面上,点击“部署”,然后点击“HF 推理端点”。您将被重定向到推理端点页面,我们已经为您选择了一个优化的推理容器和运行模型推荐的硬件。创建端点后,您可以向 DeepSeek R1 发送查询,每小时只需 8.3 美元,费用惊人!🤯
您可以在推理端点模型目录中找到 DeepSeek R1 和蒸馏模型,以及其他流行的开放 LLM,它们都已准备好在优化配置上部署。
| 注意: 团队正在努力支持在 Inferentia 实例上部署 DeepSeek 模型。敬请期待!
在 Amazon Bedrock Marketplace 上部署
您可以通过 Bedrock 市场在 Amazon Bedrock 上部署 Deepseek 蒸馏模型,这将在 Amazon SageMaker AI 后台部署一个端点。以下是您如何通过 AWS 控制台进行操作的视频:
使用 Hugging Face LLM DLC 在 Amazon Sagemaker AI 上部署
DeepSeek R1 在 GPU 上
| 注意: 团队正在努力支持使用 Hugging Face LLM DLC 在 Amazon Sagemaker AI 的 GPU 上部署 DeepSeek-R1。敬请期待!
蒸馏模型在 GPU 上
您可以使用 Jumpstart 或 Python Sagemaker SDK,在 Amazon Sagemaker AI 上使用 Hugging Face LLM DLC 部署 Deepseek 蒸馏模型。以下是您如何通过 AWS 控制台进行操作的视频:
现在我们已经了解了如何使用 Jumpstart 部署,接下来我们来详细介绍使用 Python Sagemaker SDK 部署 DeepSeek-R1-Distill-Llama-70B 的过程。
代码片段可在模型页面的“部署”按钮下找到!
在此之前,我们先了解一些先决条件。请确保您已配置 SageMaker Domain,在 SageMaker 中拥有足够的配额,并拥有一个 JupyterLab 空间。对于 DeepSeek-R1-Distill-Llama-70B,您应该将 ml.g6.48xlarge 用于端点使用的默认配额提高到 1。
供参考,以下是我们为您推荐的每种蒸馏变体的硬件配置
模型 | 实例类型 | 每个副本的 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 SDK。
!pip install sagemaker --upgrade
然后,实例化一个 `sagemaker_session`,用于确定当前区域和执行角色。
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"]
使用 Python SDK 创建 SageMaker 模型对象
model_id = "deepseek-ai/DeepSeek-R1-Distill-Llama-70B"
model_name = hf_model_id.split("/")[-1].lower()
# Hub Model configuration. https://huggingface.co/models
hub = {
"HF_MODEL_ID": model_id,
"SM_NUM_GPUS": json.dumps(8)
}
# create Hugging Face Model Class
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"
# deploy model to SageMaker Inference
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?"})
就是这样,您部署了一个 Llama 70B 推理模型!
因为您正在使用 TGI v3 容器,所以会自动选择给定硬件的最佳性能参数。
测试完端点后,请务必将其删除。
predictor.delete_model()
predictor.delete_endpoint()
蒸馏模型在 Neuron 上
让我们详细介绍如何在 Neuron 实例(如 AWS Trainium 2 和 AWS Inferentia 2)上部署 DeepSeek-R1-Distill-Llama-70B。
代码片段可在模型页面的“部署”按钮下找到!
部署到 Neuron 实例的先决条件是相同的。请确保您已配置 SageMaker Domain,在 SageMaker 中拥有足够的配额,并拥有一个 JupyterLab 空间。对于 DeepSeek-R1-Distill-Llama-70B,您应该将 ml.inf2.48xlarge
用于端点使用的默认配额提高到 1。
然后,实例化一个 `sagemaker_session`,用于确定当前区域和执行角色。
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"]
使用 Python SDK 创建 SageMaker 模型对象
image_uri = get_huggingface_llm_image_uri("huggingface-neuronx", version="0.0.25")
model_id = "deepseek-ai/DeepSeek-R1-Distill-Llama-70B"
model_name = hf_model_id.split("/")[-1].lower()
# Hub Model configuration
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",
}
# create Hugging Face Model Class
huggingface_model = HuggingFaceModel(
image_uri=image_uri,
env=hub,
role=role,
)
将模型部署到 SageMaker 端点并测试端点
endpoint_name = f"{model_name}-ep"
# deploy model to SageMaker Inference
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,
}
}
)
就是这样,您已在 Neuron 实例上部署了 Llama 70B 推理模型!它在后台从 Hugging Face 下载了一个预编译模型,以加快端点启动时间。
测试完端点后,请务必将其删除。
predictor.delete_model()
predictor.delete_endpoint()
使用 Hugging Face Neuron 深度学习 AMI 在 EC2 Neuron 上部署
本指南将详细介绍如何在 inf2.48xlarge
AWS EC2 实例上导出、部署和运行 DeepSeek-R1-Distill-Llama-70B。
在此之前,我们先了解一些先决条件。请确保您已在 Marketplace 上订阅了 Hugging Face Neuron 深度学习 AMI。它为您提供了在 Trainium 和 Inferentia 上训练和部署 Hugging Face 模型所需的所有依赖项。然后,使用该 AMI 在 EC2 中启动一个 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
下载 Hugging Face 缓存中的编译模型并启动 TGI 端点需要几分钟。
然后,您可以测试端点
curl localhost:8080/generate \
-X POST \
-d '{"inputs":"Why is the sky dark at night?"}' \
-H 'Content-Type: application/json'
测试完成后,请务必暂停 EC2 实例。
| 注意: 团队正在努力支持使用 Hugging Face Neuron 深度学习 AMI 在 Trainium 和 Inferentia 上部署 DeepSeek R1。敬请期待!
微调 DeepSeek R1 模型
使用 Hugging Face 训练 DLC 在 Amazon SageMaker AI 上微调
| 注意: 团队正在努力支持使用 Hugging Face 训练 DLC 对所有 DeepSeek 模型进行微调。敬请期待!
使用 Hugging Face Neuron 深度学习 AMI 在 EC2 Neuron 上微调
| 注意: 团队正在努力支持使用 Hugging Face Neuron 深度学习 AMI 对所有 DeepSeek 模型进行微调。敬请期待!