> 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-de/erweitert/multi-gpu-setup.md).

# Multi-GPU-Setup

Führen Sie große KI-Modelle über mehrere GPUs auf CLORE.AI aus.

{% hint style="success" %}
Finden Sie Multi-GPU-Server unter [CLORE.AI Marketplace](https://clore.ai/marketplace).
{% endhint %}

## Wann benötigen Sie Multi-GPU?

| Modellgröße | Option Einzel-GPU | Option Multi-GPU   |
| ----------- | ----------------- | ------------------ |
| ≤13B        | RTX 3090 (Q4)     | Nicht erforderlich |
| 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       |

***

## Multi-GPU-Konzepte

### Tensor-Parallellität (TP)

Teilen Sie Modellschichten über GPUs auf. Am besten für Inferenz.

```
GPU 0: Schichten 1-20
GPU 1: Schichten 21-40
```

**Vorteile:** Geringere Latenz, einfache Einrichtung **Nachteile:** Erfordert Hochgeschwindigkeits-Interconnect

### Pipeline-Parallellität (PP)

Verarbeiten Sie verschiedene Batches auf verschiedenen GPUs.

```
GPU 0: Batch 1 → GPU 1: Batch 1
GPU 0: Batch 2 → GPU 1: Batch 2
```

**Vorteile:** Höherer Durchsatz **Nachteile:** Höhere Latenz, komplexer

### Daten-Parallellität (DP)

Dasselbe Modell auf mehreren GPUs, unterschiedliche Daten.

```
GPU 0: Verarbeitet Batch A
GPU 1: Verarbeitet Batch B
```

**Vorteile:** Einfach, lineare Skalierung **Nachteile:** Jede GPU benötigt das vollständige Modell

***

## LLM Multi-GPU-Setup

### vLLM (Empfohlen)

**2 GPUs:**

```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 GPUs:**

```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 GPUs (für 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 Multi-GPU

Ollama verwendet automatisch mehrere GPUs, wenn verfügbar:

```bash
# Verfügbare GPUs prüfen
nvidia-smi

# Ollama erkennt automatisch alle GPUs und verwendet sie
ollama run llama3.1:70b
```

**Auf bestimmte GPUs beschränken:**

```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-Schichten pro Gerät angeben
./llama-server \
    -m llama-3.1-70b-q4.gguf \
    -ngl 999 \
    --split-mode layer \
    --tensor-split 0.5,0.5
```

***

## Bildgenerierung Multi-GPU

### ComfyUI

ComfyUI kann verschiedene Modelle auf verschiedene GPUs auslagern:

```python
# Im ComfyUI-Workflow
# Verwenden Sie "Load Checkpoint" mit device-Parameter
# device: "cuda:0" für die erste GPU
# device: "cuda:1" für die zweite GPU
```

**VAE auf separater GPU ausführen:**

```python
# Hauptmodell auf GPU 0
# VAE auf GPU 1
# Verringert VRAM-Belastung
```

### Stable Diffusion WebUI

**Multi-GPU in webui-user.sh aktivieren:**

```bash
export COMMANDLINE_ARGS="--device-id 0"
# Oder für bestimmte Modelle:
export COMMANDLINE_ARGS="--lowvram --device-id 0,1"
```

### FLUX Multi-GPU

```python
from diffusers import FluxPipeline
import torch

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

# Auf GPUs verteilen
pipe.enable_model_cpu_offload()  # oder
pipe.to("cuda:0")  # Explizite GPU-Auswahl
```

***

## Training Multi-GPU

### PyTorch Distributed

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

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

# Modell einkapseln
model = YourModel().to(local_rank)
model = DDP(model, device_ids=[local_rank])

# Trainingsschleife wie gewohnt
```

**Start:**

```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}
    }
)
```

**Start:**

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

### Accelerate (HuggingFace)

```python
from accelerate import Accelerator

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

**Konfigurieren:**

```bash
accelerate config  # Interaktive Einrichtung
accelerate launch train.py
```

### Kohya Training (LoRA)

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

***

## GPU-Auswahl

### Verfügbare GPUs prüfen

```bash
# Alle GPUs auflisten
nvidia-smi

# Detaillierte Infos
nvidia-smi -L

# Speichernutzung
nvidia-smi --query-gpu=index,memory.used,memory.total --format=csv
```

### Bestimmte GPUs auswählen

**Umgebungsvariable:**

```bash
# Nur GPU 0 und 1 verwenden
export CUDA_VISIBLE_DEVICES=0,1
python your_script.py

# Nur GPU 2 verwenden
export CUDA_VISIBLE_DEVICES=2
python your_script.py
```

**In Python:**

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

# Oder mit torch
import torch
device = torch.device("cuda:0")  # Erste sichtbare GPU
device = torch.device("cuda:1")  # Zweite sichtbare GPU
```

***

## Leistungsoptimierung

### NVLink vs PCIe

| Verbindung | Bandbreite | Am besten geeignet für        |
| ---------- | ---------- | ----------------------------- |
| NVLink     | 600 GB/s   | Tensor-Parallele Verarbeitung |
| PCIe 4.0   | 32 GB/s    | Datenparallellität            |
| PCIe 5.0   | 64 GB/s    | Gemischte Workloads           |

**NVLink-Status prüfen:**

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

### Optimale Konfiguration

| GPUs | TP-Größe | PP-Größe | Hinweise                  |
| ---- | -------- | -------- | ------------------------- |
| 2    | 2        | 1        | Einfache Tensor-Parallele |
| 4    | 4        | 1        | Erfordert NVLink          |
| 4    | 2        | 2        | PCIe-freundlich           |
| 8    | 8        | 1        | Volle Tensor-Parallele    |
| 8    | 4        | 2        | Gemischte Parallellität   |

### Speicherausgleich

**Gleichmäßige Aufteilung (Standard):**

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

**Benutzerdefinierte Aufteilung (ungleiche GPUs):**

```bash
# vLLM unterstützt keine Ungleichheit, verwenden Sie llama.cpp:
./llama-server --tensor-split 0.6,0.4
```

***

## Fehlerbehebung

### "NCCL-Fehler"

```bash
# NCCL-Debug setzen
export NCCL_DEBUG=INFO

# Versuchen Sie verschiedene NCCL-Algorithmen
export NCCL_ALGO=Ring
```

### "Nicht genügend Speicher auf GPU X"

```bash
# Speicher pro GPU prüfen
nvidia-smi

# Batch-Größe reduzieren
--max-batch-size 1

# Gradient Checkpointing aktivieren (Training)
--gradient-checkpointing
```

### "Langsame Multi-GPU-Leistung"

1. NVLink-Konnektivität prüfen
2. Tensor-Parallellgröße reduzieren
3. Stattdessen Pipeline-Parallellität verwenden
4. CPU-Engpass prüfen

### "GPUs nicht erkannt"

```bash
# CUDA überprüfen
nvidia-smi

# Prüfen, ob PyTorch GPUs sieht
python -c "import torch; print(torch.cuda.device_count())"

# Gegebenenfalls CUDA-Treiber neu installieren
```

***

## Kostenoptimierung

### Wann sich Multi-GPU lohnt

| Szenario                  | Einzel-GPU              | Multi-GPU                  | Gewinner              |
| ------------------------- | ----------------------- | -------------------------- | --------------------- |
| 70B gelegentliche Nutzung | A100 80GB (0,25 $/Std.) | 2x RTX 4090 (0,20 $/Std.)  | Multi                 |
| 70B Produktion            | A100 40GB (0,17 $/Std.) | 2x A100 40GB (0,34 $/Std.) | Einzeln (Q4)          |
| Training 7B               | RTX 4090 (0,10 $/Std.)  | 2x RTX 4090 (0,20 $/Std.)  | Hängt von der Zeit ab |

### Kosten-effektive Konfigurationen

| Einsatzgebiet         | Konfiguration | \~Kosten/Std. |
| --------------------- | ------------- | ------------- |
| 70B Inferenz          | 2x RTX 3090   | $0.12         |
| 70B schnelle Inferenz | 2x A100 40GB  | $0.34         |
| 70B FP16              | 2x A100 80GB  | $0.50         |
| Training 13B          | 2x RTX 4090   | $0.20         |

***

## Beispielkonfigurationen

### 70B Chat-Server

```bash
# 2x A100 40GB-Setup
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 erforderlich
python -m vllm.entrypoints.openai.api_server \
    --model deepseek-ai/DeepSeek-V3 \
    --tensor-parallel-size 8 \
    --trust-remote-code \
    --host 0.0.0.0
```

### Bild + LLM-Pipeline

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

# GPU 1: LLM für Prompts
CUDA_VISIBLE_DEVICES=1 python -m vllm.entrypoints.openai.api_server \
    --model meta-llama/Llama-3.1-8B-Instruct --port 8000
```

***

## Nächste Schritte

* [vLLM-Leitfaden](/guides/guides_v2-de/sprachmodelle/vllm.md) - LLM-Serving in Produktion
* [GPU-Vergleich](/guides/guides_v2-de/erste-schritte/gpu-comparison.md) - Wählen Sie Ihre GPUs
* [API-Integration](/guides/guides_v2-de/erweitert/api-integration.md) - Anwendungen erstellen
* [Kostenrechner](/guides/guides_v2-de/erste-schritte/cost-calculator.md) - Kosten schätzen


---

# 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:

```
GET https://docs.clore.ai/guides/guides_v2-de/erweitert/multi-gpu-setup.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
