跨节点并行#
Ray Serve LLM 支持跨节点张量并行 (TP) 和流水线并行 (PP),允许您将模型推理分布到多个 GPU 和节点上。此功能使您能够
部署无法放入单个 GPU 或节点的模型。
在集群的可用资源上扩展模型服务。
利用 Ray 的放置组策略来控制 worker 放置,以获得性能或容错能力。
注意
默认情况下,Ray Serve LLM 使用 PACK 放置策略,该策略会尝试将 worker 放置在尽可能少的节点上。如果 worker 无法放入单个节点,它们会自动溢出到其他节点。这使得在单节点资源不足时可以进行跨节点部署。
张量并行#
张量并行将模型权重分布到多个 GPU 上,每个 GPU 在每次前向传播时处理模型张量的一部分。这种方法对于无法放入单个 GPU 的模型很有用。
以下示例展示了如何在 2 个 GPU 上配置张量并行
import vllm
from ray import serve
from ray.serve.llm import LLMConfig, build_openai_app
# Configure a model with tensor parallelism across 2 GPUs
# Tensor parallelism splits model weights across GPUs
llm_config = LLMConfig(
model_loading_config=dict(
model_id="llama-3.1-8b",
model_source="meta-llama/Llama-3.1-8B-Instruct",
),
deployment_config=dict(
autoscaling_config=dict(
min_replicas=1,
max_replicas=2,
)
),
accelerator_type="L4",
engine_kwargs=dict(
tensor_parallel_size=2,
max_model_len=8192,
),
)
# Deploy the application
app = build_openai_app({"llm_configs": [llm_config]})
serve.run(app, blocking=True)
流水线并行#
流水线并行将模型的层分布到多个 GPU 上,每个 GPU 处理模型层的一个子集。对于张量并行本身不足以处理的非常大的模型,这种方法很有用。
以下示例展示了如何在 2 个 GPU 上配置流水线并行
from ray import serve
from ray.serve.llm import LLMConfig, build_openai_app
# Configure a model with pipeline parallelism across 2 GPUs
# Pipeline parallelism splits model layers across GPUs
llm_config = LLMConfig(
model_loading_config=dict(
model_id="llama-3.1-8b",
model_source="meta-llama/Llama-3.1-8B-Instruct",
),
deployment_config=dict(
autoscaling_config=dict(
min_replicas=1,
max_replicas=1,
)
),
accelerator_type="L4",
engine_kwargs=dict(
pipeline_parallel_size=2,
max_model_len=8192,
),
)
# Deploy the application
app = build_openai_app({"llm_configs": [llm_config]})
serve.run(app, blocking=True)
组合张量并行和流水线并行#
对于极大的模型,您可以组合使用张量并行和流水线并行。GPU 的总数是 tensor_parallel_size 和 pipeline_parallel_size 的乘积。
以下示例展示了如何配置一个同时具有 TP 和 PP 的模型(共 4 个 GPU)
from ray import serve
from ray.serve.llm import LLMConfig, build_openai_app
# Configure a model with both tensor and pipeline parallelism
# This example uses 4 GPUs total (2 TP * 2 PP)
llm_config = LLMConfig(
model_loading_config=dict(
model_id="llama-3.1-8b",
model_source="meta-llama/Llama-3.1-8B-Instruct",
),
deployment_config=dict(
autoscaling_config=dict(
min_replicas=1,
max_replicas=1,
)
),
accelerator_type="L4",
engine_kwargs=dict(
tensor_parallel_size=2,
pipeline_parallel_size=2,
max_model_len=8192,
enable_chunked_prefill=True,
max_num_batched_tokens=4096,
),
)
# Deploy the application
app = build_openai_app({"llm_configs": [llm_config]})
serve.run(app, blocking=True)
自定义放置组#
您可以通过 placement_group_config 参数自定义 Ray 如何将 vLLM engine worker 放置到节点上。此参数接受一个包含 bundles(资源字典列表)和 strategy(放置策略)的字典。
Ray Serve LLM 默认使用 PACK 策略,该策略会尝试将 worker 放置在尽可能少的节点上。如果 worker 无法放入单个节点,它们会自动溢出到其他节点。有关所有可用放置策略的更多详细信息,请参阅 Ray Core 的放置策略文档。
注意
数据并行部署会自动将放置策略覆盖为 STRICT_PACK,因为每个副本必须共置才能实现正确的数据并行行为。
虽然您可以指定张量并行和流水线并行的程度,但模型 rank 到 GPU 的具体分配由 vLLM engine 管理,无法通过 Ray Serve LLM API 直接配置。Ray Serve 会自动将加速器类型标签注入到 bundles 中,并合并第一个 bundle 与副本 actor 资源(CPU、GPU、内存)。
以下示例展示了如何使用 SPREAD 策略将 worker 分布到多个节点以实现容错
from ray import serve
from ray.serve.llm import LLMConfig, build_openai_app
# Configure a model with custom placement group using SPREAD strategy
# SPREAD distributes workers across nodes for fault tolerance
llm_config = LLMConfig(
model_loading_config=dict(
model_id="llama-3.1-8b",
model_source="meta-llama/Llama-3.1-8B-Instruct",
),
deployment_config=dict(
autoscaling_config=dict(
min_replicas=1,
max_replicas=1,
)
),
accelerator_type="L4",
engine_kwargs=dict(
tensor_parallel_size=4,
max_model_len=8192,
),
placement_group_config=dict(
bundles=[{"GPU": 1}] * 4,
strategy="SPREAD",
),
)
# Deploy the application
app = build_openai_app({"llm_configs": [llm_config]})
serve.run(app, blocking=True)