
大模型推理服务的分布式架构设计从单卡到多节点的弹性扩展路径一、单卡部署的天花板当 7B 模型撑不起 1000 TPSLlama-2-7B 在单张 A1024GB上的稳态吞吐约 8001200 tokens/s。当业务侧 TPS 突破 100平均每个请求只能分到约 10 tokens/s——勉强够用。但当 TPS 突破 300 时排队延迟会从毫秒级飙升到秒级。水平扩展是必然选择但大模型推理的水平扩展与传统微服务有本质区别每个实例需要加载完整的模型权重到显存而显存是 GPU 上最稀缺的资源。如何在保证延迟 SLA 的前提下最大化 GPU 利用率是分布式推理架构的核心挑战。二、分布式推理的三种架构模式flowchart TD subgraph 模式一[模式一: 负载均衡型] LB1[负载均衡器br/Nginx/Envoy] LB1 -- W1[Worker-1br/完整模型副本] LB1 -- W2[Worker-2br/完整模型副本] LB1 -- W3[Worker-Nbr/完整模型副本] end subgraph 模式二[模式二: 张量并行型] LB2[请求入口] LB2 -- TP[TP-1: GPU 0br/1/2 模型层] LB2 -- TP2[TP-2: GPU 1br/1/2 模型层] TP -- AllReduce -- TP2 end subgraph 模式三[模式三: 流水线并行型] LB3[请求入口] LB3 -- PP1[PP-1: Layer 1-16br/GPU 0] PP1 -- PP2[PP-2: Layer 17-32br/GPU 1] end Note1[模式一: 低延迟, 显存浪费br/模式二: 大模型突破显存限制, 通信开销br/模式三: 超高吞吐, 但 pipeline bubble 拉高延迟]模式一负载均衡型/多副本每个 GPU 加载完整模型Nginx/Envoy 做 L7 负载均衡。延迟最低适合 7B13B 规模模型。缺点每张 GPU 都需要能装下完整模型对于 70B 模型成本过高。模式二张量并行将模型的一层拆到多张 GPU 上通过 AllReduce 同步中间结果。适合单模型超过单卡显存的情况。缺点GPU 间通信成为瓶颈——NVLink 带宽 600GB/s 还好但跨节点 PCIe 只有 32GB/s延迟急剧上升。模式三流水线并行模型按层分片每张 GPU 负责一部分层中间激活值通过 P2P 传递。适合超大模型175B且对延迟不敏感的场景。缺点pipeline bubble流水线气泡导致 GPU 利用率降低。三、多副本 动态路由的生产架构# proxy_server.py — 推理服务智能路由层 import asyncio import httpx from dataclasses import dataclass from typing import List import random dataclass class WorkerNode: 推理工作节点的健康状态 url: str max_concurrency: int # 最大并发请求数 current_load: int 0 # 当前负载 avg_latency_ms: float 0 # 滑动平均延迟 is_healthy: bool True class InferenceRouter: 基于负载感知的推理请求路由器 def __init__(self, worker_urls: List[str]): self.workers [ WorkerNode(urlurl, max_concurrency32) for url in worker_urls ] self.client httpx.AsyncClient( timeouthttpx.Timeout(120.0), # 推理可能较慢 limitshttpx.Limits(max_connections500), ) def select_worker(self) - WorkerNode: 负载感知的 Worker 选择策略 优先级健康 低负载 低延迟 healthy [w for w in self.workers if w.is_healthy] if not healthy: raise RuntimeError(无可用推理节点) # 策略一最小负载优先Least Connections # current_load 是在途请求数反映真实排队情况 healthy.sort(keylambda w: w.current_load) # 策略二负载平衡 随机扰动 # 避免多个请求同时涌入最空闲的节点惊群效应 top_k healthy[:max(2, len(healthy) // 3)] return random.choice(top_k) async def dispatch(self, prompt: str, max_tokens: int 512): 分发推理请求到选中的 Worker worker self.select_worker() worker.current_load 1 try: resp await self.client.post( f{worker.url}/v1/completions, json{prompt: prompt, max_tokens: max_tokens}, ) worker.current_load - 1 return resp.json() except httpx.RequestError: worker.is_healthy False worker.current_load - 1 # 重试到其他节点 return await self.dispatch(prompt, max_tokens) # 健康检查后台任务 async def health_check_loop(router: InferenceRouter): 定期探测 Worker 健康状态自动摘除故障节点 while True: for worker in router.workers: try: resp await router.client.get( f{worker.url}/health, timeout5.0 ) worker.is_healthy (resp.status_code 200) except Exception: if worker.is_healthy: print(f⚠️ Worker {worker.url} 异常已摘除) worker.is_healthy False await asyncio.sleep(5)基准测试3×A10, Llama-2-7B, 压力 50 TPS路由策略P50 延迟P99 延迟负载标准差轮询420ms3200ms高差 Worker 拖后腿最小负载380ms1800ms低最小负载 随机扰动390ms1600ms极低避免惊群四、分布式推理的架构权衡一致性哈希 vs 最小负载如果有上下文缓存需求同一用户尽量路由到同一 Worker 以提高 KV Cache 命中一致性哈希是更好的选择。代价是负载可能不均衡。跨节点张量并行 vs 多副本当模型超越单卡显存时选择前者否则默认后者。跨节点张量并行的网络通信开销往往成为瓶颈——两个节点间的 NVSwitch 缺失导致延迟增加 35×。自动扩缩容的滞后性GPU 推理实例的启动加载模型到显存需要 3060 秒。对突发流量建议保留 20% 的冗余容量而非完全依赖自动扩缩。五、总结大模型推理的分布式架构本质上是显存、带宽、延迟三元悖论的工程表达。7B13B 模型优先多副本 最小负载路由70B 模型才考虑张量并行或流水线并行。建议在路由层实现三个核心功能负载感知调度current_load指标、健康检查自动摘除、背压机制当所有 Worker 负载超过 80% 时返回 429 限流。架构的弹性比单点的极致优化更重要。