> For the complete documentation index, see [llms.txt](https://docs.clore.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.clore.ai/guides/guides_v2-zh/gao-ji/multi-gpu-setup.md).

# 多 GPU 设置

在 Clore.ai 上跨多块 GPU 运行大型 AI 模型

在 CLORE.AI 上跨多块 GPU 运行大型 AI 模型。

{% hint style="success" %}
在以下位置查找多 GPU 服务器： [CLORE.AI 市场](https://clore.ai/marketplace).
{% endhint %}

## 何时需要多 GPU？

{% hint style="warning" %}
**Clore.ai 市场上未列出多 GPU 的 80GB 级机型。** 目前列出的最大配置是 4× RTX PRO 6000 Blackwell（每张 96GB，共 380GB）以及 8–11× RTX 5090（每张 32GB）。A100 / H200 / B200 容量可按 [裸机](https://clore.ai/bare-metal) 需求提供。部署前请查看 [GPU 价格与可用性](/guides/guides_v2-zh/ru-men-zhi-nan/pricing.md) 。
{% endhint %}

| 模型大小     | 单 GPU 选项      | 多 GPU 选项     |
| -------- | ------------- | ------------ |
| ≤13B     | RTX 3090（Q4）  | 不需要          |
| 30B      | RTX 4090（Q4）  | 2x RTX 3090  |
| 70B      | A100 40GB（Q4） | 2x RTX 4090  |
| 70B FP16 | -             | 2x A100 80GB |
| 100B+    | -             | 4x A100 80GB |
| 405B     | -             | 8x A100 80GB |

***

## 多 GPU 概念

### 张量并行（TP）

将模型层分割到不同 GPU 上。最适合推理。

```
GPU 0：第 1-20 层
GPU 1：第 21-40 层
```

**优点：** 更低延迟，设置简单 **缺点：** 需要高速互连

### 流水线并行（PP）

在不同 GPU 上处理不同批次。

```
GPU 0：批次 1 → GPU 1：批次 1
GPU 0：批次 2 → GPU 1：批次 2
```

**优点：** 更高吞吐量 **缺点：** 更高延迟，更复杂

### 数据并行（DP）

相同模型运行在多块 GPU 上，处理不同数据。

```
GPU 0：处理批次 A
GPU 1：处理批次 B
```

**优点：** 简单，线性扩展 **缺点：** 每块 GPU 都需要完整模型

***

## LLM 多 GPU 设置

### vLLM（推荐）

**2 块 GPU：**

```bash
python -m vllm.entrypoints.openai.api_server \
    --model meta-llama/Llama-3.1-70B-Instruct \
    --tensor-parallel-size 2 \\
    --host 0.0.0.0
```

**4 块 GPU：**

```bash
python -m vllm.entrypoints.openai.api_server \
    --model meta-llama/Llama-3.1-70B-Instruct \
    --tensor-parallel-size 4 \\
    --host 0.0.0.0
```

**8 块 GPU（用于 405B）：**

```bash
python -m vllm.entrypoints.openai.api_server \
    --model meta-llama/Llama-3.1-405B-Instruct \\
    --tensor-parallel-size 8 \\
    --host 0.0.0.0
```

### Ollama 多 GPU

Ollama 在可用时会自动使用多块 GPU：

```bash
# 检查可用 GPU
nvidia-smi

# Ollama 会自动检测并使用所有 GPU
ollama run llama3.1:70b
```

**限制使用指定 GPU：**

```bash
CUDA_VISIBLE_DEVICES=0,1 ollama run llama3.1:70b
```

### Text Generation Inference（TGI）

```bash
docker run --gpus all -p 8080:80 \\
    ghcr.io/huggingface/text-generation-inference:latest \\
    --model-id meta-llama/Llama-3.1-70B-Instruct \\
    --num-shard 2
```

### llama.cpp

```bash
# 指定每个设备的 GPU 层数
./llama-server \\
    -m llama-3.1-70b-q4.gguf \\
    -ngl 999 \\
    --split-mode layer \\
    --tensor-split 0.5,0.5
```

***

## 图像生成多 GPU

### ComfyUI

ComfyUI 可以将不同模型卸载到不同 GPU 上：

```python
# 在 ComfyUI 工作流中
# 使用带 device 参数的“加载检查点”
# device: "cuda:0" 表示第一块 GPU
# device: "cuda:1" 表示第二块 GPU
```

**在单独的 GPU 上运行 VAE：**

```python
# 主模型在 GPU 0 上
# VAE 在 GPU 1 上
# 减少 VRAM 压力
```

### Stable Diffusion WebUI

**在 webui-user.sh 中启用多 GPU：**

```bash
export COMMANDLINE_ARGS="--device-id 0"
# 或用于指定模型：
export COMMANDLINE_ARGS="--lowvram --device-id 0,1"
```

### FLUX 多 GPU

```python
from diffusers import FluxPipeline
import torch

pipe = FluxPipeline.from_pretrained(
    "black-forest-labs/FLUX.1-dev",
    torch_dtype=torch.bfloat16
)

# 跨 GPU 分配
pipe.enable_model_cpu_offload()  # 或
pipe.to("cuda:0")  # 显式选择 GPU
```

***

## 训练多 GPU

### PyTorch 分布式

```python
import torch
import torch.distributed as dist
from torch.nn.parallel import DistributedDataParallel as DDP

# 初始化
dist.init_process_group("nccl")
local_rank = int(os.environ["LOCAL_RANK"] )
torch.cuda.set_device(local_rank)

# 包装模型
model = YourModel().to(local_rank)
model = DDP(model, device_ids=[local_rank])

# 正常进行训练循环
```

**启动：**

```bash
torchrun --nproc_per_node=2 train.py
```

### DeepSpeed

```python
import deepspeed

model, optimizer, _, _ = deepspeed.initialize(
    model=model,
    config={
        "train_batch_size": 32,
        "fp16": {"enabled": True},
        "zero_optimization": {"stage": 2}
    }
)
```

**启动：**

```bash
deepspeed --num_gpus=2 train.py
```

### Accelerate（HuggingFace）

```python
from accelerate import Accelerator

accelerator = Accelerator()
model, optimizer, dataloader = accelerator.prepare(
    model, optimizer, dataloader
)
```

**配置：**

```bash
accelerate config  # 交互式设置
accelerate launch train.py
```

### Kohya 训练（LoRA）

```bash
# 多 GPU LoRA 训练
accelerate launch --num_processes=2 train_network.py \\
    --pretrained_model_name_or_path="model.safetensors" \\
    --train_data_dir="./images" \\
    --output_dir="./output"
```

***

## GPU 选择

### 检查可用 GPU

```bash
# 列出所有 GPU
nvidia-smi

# 详细信息
nvidia-smi -L

# 内存使用情况
nvidia-smi --query-gpu=index,memory.used,memory.total --format=csv
```

### 选择指定 GPU

**环境变量：**

```bash
# 仅使用 GPU 0 和 1
export CUDA_VISIBLE_DEVICES=0,1
python your_script.py

# 仅使用 GPU 2
export CUDA_VISIBLE_DEVICES=2
python your_script.py
```

**在 Python 中：**

```python
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0,1"

# 或使用 torch
import torch
device = torch.device("cuda:0")  # 第一块可见 GPU
device = torch.device("cuda:1")  # 第二块可见 GPU
```

***

## 性能优化

### NVLink 与 PCIe

| 连接       | 带宽       | 最适合    |
| -------- | -------- | ------ |
| NVLink   | 600 GB/s | 张量并行   |
| PCIe 4.0 | 32 GB/s  | 数据并行   |
| PCIe 5.0 | 64 GB/s  | 混合工作负载 |

**检查 NVLink 状态：**

```bash
nvidia-smi nvlink --status
```

### 最佳配置

| GPU | TP 大小 | PP 大小 | 备注        |
| --- | ----- | ----- | --------- |
| 2   | 2     | 1     | 简单张量并行    |
| 4   | 4     | 1     | 需要 NVLink |
| 4   | 2     | 2     | 适合 PCIe   |
| 8   | 8     | 1     | 完整张量并行    |
| 8   | 4     | 2     | 混合并行      |

### 内存平衡

**均分（默认）：**

```bash
--tensor-parallel-size 2
```

**自定义分配（不均衡 GPU）：**

```bash
# vLLM 不支持不均衡分配，改用 llama.cpp：
./llama-server --tensor-split 0.6,0.4
```

***

## 故障排查

### "NCCL 错误"

```bash
# 设置 NCCL 调试
export NCCL_DEBUG=INFO

# 尝试不同的 NCCL 算法
export NCCL_ALGO=Ring
```

### "GPU X 显存不足"

```bash
# 检查每块 GPU 的内存
nvidia-smi

# 减小批次大小
--max-batch-size 1

# 启用梯度检查点（训练）
--gradient-checkpointing
```

### "多 GPU 性能缓慢"

1. 检查 NVLink 连通性
2. 减小张量并行大小
3. 改用流水线并行
4. 检查 CPU 瓶颈

### "未检测到 GPU"

```bash
# 验证 CUDA
nvidia-smi

# 检查 PyTorch 是否能看到 GPU
python -c "import torch; print(torch.cuda.device_count())"

# 如有需要，重新安装 CUDA 驱动
```

***

## 成本优化

### 何时值得使用多 GPU

| 场景       | 单 GPU                                        | 多 GPU                                           | 胜出者       |
| -------- | -------------------------------------------- | ----------------------------------------------- | --------- |
| 70B 偶尔使用 | A100 80GB（[裸机](https://clore.ai/bare-metal)) | 2x RTX 4090（$0.28–0.84/小时）                      | 多 GPU     |
| 70B 生产环境 | A100 40GB（[裸机](https://clore.ai/bare-metal)) | 2x A100 40GB（[裸机](https://clore.ai/bare-metal)) | 单 GPU（Q4） |
| 训练 7B    | RTX 4090（$0.14–0.42/小时）                      | 2x RTX 4090（$0.28–0.84/小时）                      | 取决于时间     |

### 高性价比配置

| 使用场景     | 配置           | 约每小时成本 |
| -------- | ------------ | ------ |
| 70B 推理   | 2x RTX 3090  | $0.12  |
| 70B 快速推理 | 2x A100 40GB | $0.34  |
| 70B FP16 | 2x A100 80GB | $0.50  |
| 训练 13B   | 2x RTX 4090  | $0.20  |

***

## 示例配置

### 70B 聊天服务器

```bash
# 2x A100 40GB 配置
python -m vllm.entrypoints.openai.api_server \
    --model meta-llama/Llama-3.1-70B-Instruct \
    --tensor-parallel-size 2 \\
    --max-model-len 8192 \
    --host 0.0.0.0 \\
    --port 8000
```

### DeepSeek-V3（671B）

```bash
# 需要 8x A100 80GB
python -m vllm.entrypoints.openai.api_server \
    --model deepseek-ai/DeepSeek-V3 \\
    --tensor-parallel-size 8 \\
    --trust-remote-code \\
    --host 0.0.0.0
```

### 图像 + LLM 流水线

```bash
# GPU 0：Stable Diffusion
CUDA_VISIBLE_DEVICES=0 python comfyui/main.py --port 8188 &

# GPU 1：用于提示词的 LLM
CUDA_VISIBLE_DEVICES=1 python -m vllm.entrypoints.openai.api_server \\
    --model meta-llama/Llama-3.1-8B-Instruct --port 8000
```

***

## 下一步

* [vLLM 指南](/guides/guides_v2-zh/yu-yan-mo-xing/vllm.md) - 生产级 LLM 服务
* [GPU 对比](/guides/guides_v2-zh/ru-men-zhi-nan/gpu-comparison.md) - 选择你的 GPU
* [API 集成](/guides/guides_v2-zh/gao-ji/api-integration.md) - 构建应用程序
* [成本计算器](/guides/guides_v2-zh/ru-men-zhi-nan/cost-calculator.md) - 估算成本


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.clore.ai/guides/guides_v2-zh/gao-ji/multi-gpu-setup.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
