> 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/training/litgpt.md).

# LitGPT

**LitGPT** ist eine leistungsstarke Bibliothek für das Vortrainieren, Feinabstimmen und Bereitstellen von 20+ großen Sprachmodellen, die auf PyTorch Lightning aufbauen. Mit über 12.000 GitHub-Stars ist sie ein Standard-Toolkit für Ingenieure, die sauberen, leicht anpassbaren LLM-Trainingscode ohne den Abstraktions-Overhead von HuggingFace Transformers benötigen.

Jedes Modell in LitGPT umfasst etwa 1.000 Zeilen sauberes PyTorch — keine Vererbungsketten mit 10 Ebenen, keine Magie. Du kannst dir die Llama-3-Implementierung an einem Nachmittag von Anfang bis Ende ansehen und sie sicher anpassen.

{% hint style="success" %}
Alle Beispiele können auf GPU-Servern ausgeführt werden, die gemietet wurden über [CLORE.AI-Marktplatz](https://clore.ai/marketplace).
{% endhint %}

***

## Was ist LitGPT?

LitGPT bietet produktionsreife Implementierungen modernster LLMs mit einer einheitlichen Trainingsschnittstelle:

* **20+ unterstützte Modelle** — Llama 3, Gemma 2, Mistral, Phi-3, Falcon, StableLM und mehr
* **Von Grund auf vortrainieren** — vollständiges Vortraining mit Flash Attention, FSDP und Gradient Checkpointing
* **Effizient feinabstimmen** — vollständige Feinabstimmung, LoRA, QLoRA und Adapter-Methoden
* **Mit Zuversicht bereitstellen** — integrierter Inferenzserver mit Quantisierung
* **Multi-GPU-Unterstützung** — DDP, FSDP und Tensorparallelismus direkt ab Werk
* **Speichereffizient** — 4-Bit-Quantisierung, Gradient Checkpointing, Activation Checkpointing

***

## Serveranforderungen

| Komponente     | Minimum          | Empfohlen                |
| -------------- | ---------------- | ------------------------ |
| GPU            | RTX 3090 (24 GB) | A100 80 GB / H100        |
| VRAM           | 16 GB (7B LoRA)  | 80 GB+ (70B vollständig) |
| RAM            | 32 GB            | 64 GB+                   |
| CPU            | 8 Kerne          | 16+ Kerne                |
| Speicher       | 100 GB           | 500 GB+                  |
| Betriebssystem | Ubuntu 20.04+    | Ubuntu 22.04             |
| Python         | 3.10+            | 3.11                     |
| CUDA           | 12.8+            | 12.8+                    |

### VRAM-Anforderungen nach Aufgabe

| Aufgabe                     | Modell      | VRAM              |
| --------------------------- | ----------- | ----------------- |
| Inferenz (4-Bit)            | Llama-3 8B  | \~6 GB            |
| LoRA-Feinabstimmung         | Llama-3 8B  | \~16 GB           |
| Vollständige Feinabstimmung | Llama-3 8B  | \~80 GB           |
| LoRA-Feinabstimmung         | Llama-3 70B | \~48 GB (2×A100)  |
| Vollständige Feinabstimmung | Llama-3 70B | \~640 GB (8×A100) |
| QLoRA-Feinabstimmung        | Llama-3 8B  | \~8 GB            |

***

## Ports

| Port | Dienst                | Hinweise                                    |
| ---- | --------------------- | ------------------------------------------- |
| 22   | SSH                   | Terminalzugriff & Dateitransfer             |
| 8000 | LitGPT-Inferenzserver | REST-API für das Bereitstellen von Modellen |

***

## Schnellstart mit Docker

```bash
# Offizielles LitGPT-Image herunterladen
docker pull pytorchlightning/litgpt:latest

# Interaktiven Container mit GPU ausführen
docker run -it --gpus all \\
  -p 8000:8000 \
  -v $(pwd)/checkpoints:/checkpoints \\
  -v $(pwd)/data:/data \\
  pytorchlightning/litgpt:latest \\
  bash

# Oder direkt einen bestimmten Befehl ausführen
docker run --gpus all \\
  -v $(pwd)/checkpoints:/checkpoints \\
  pytorchlightning/litgpt:latest \\
  litgpt download --repo_id meta-llama/Llama-3.2-3B-Instruct
```

***

## Installation auf Clore.ai

### Schritt 1 — Einen Server mieten

1. Gehe zu [Clore.ai-Marktplatz](https://clore.ai/marketplace)
2. Filtern nach **VRAM ≥ 24 GB** (RTX 3090 oder besser)
3. Wähle ein **PyTorch** oder **CUDA 12.8** Basis-Image
4. Ports öffnen **22** und **8000** in deinen Bestelleinstellungen
5. Wähle **Speicher ≥ 200 GB** für Modellgewichte

### Schritt 2 — Per SSH verbinden

```bash
ssh root@<server-ip> -p <ssh-port>
```

### Schritt 3 — LitGPT installieren

```bash
# Installation per pip (empfohlen)
pip install litgpt

# Mit allen Extras (Quantisierung, Server usw.)
pip install 'litgpt[all]'

# Oder aus den Quellen installieren, um die neuesten Funktionen zu erhalten
git clone https://github.com/Lightning-AI/litgpt.git
cd litgpt
pip install -e '.[all]'
```

### Schritt 4 — Installation überprüfen

```bash
litgpt --help
```

Erwartete Ausgabe:

```
Verwendung: litgpt [OPTIONEN] BEFEHL [ARGUMENTE]...
  
Befehle:
  chat       Mit einem Modell chatten
  convert    Modellgewichte konvertieren
  download   Modellgewichte herunterladen
  evaluate   Ein Modell evaluieren
  finetune   Ein Modell feinabstimmen
  generate   Text generieren
  pretrain   Ein Modell vortrainieren
  serve      Ein Modell für Inferenz bereitstellen
```

***

## Modelle herunterladen

LitGPT lädt Modelle von Hugging Face herunter:

```bash
# Verfügbare Modelle auflisten
litgpt download --list

# Llama 3.2 3B herunterladen (für geschützte Modelle ist ein HF-Token erforderlich)
litgpt download \\
  --repo_id meta-llama/Llama-3.2-3B-Instruct \\
  --checkpoint_dir checkpoints/

# Mistral 7B herunterladen (offener Zugriff)
litgpt download \\
  --repo_id mistralai/Mistral-7B-Instruct-v0.3

# Gemma 2 2B herunterladen
litgpt download \\
  --repo_id google/gemma-2-2b-it \\
  --access_token dein-hf-token

# Phi-3 herunterladen (klein, aber leistungsstark)
litgpt download \\
  --repo_id microsoft/Phi-3-mini-4k-instruct
```

### HuggingFace-Token festlegen

```bash
# Für geschützte Modelle (Llama, Gemma)
export HF_TOKEN=hf_your-token-here

# Oder per CLI authentifizieren
pip install huggingface_hub
huggingface-cli login
```

***

## Inferenz (Chat & Generieren)

```bash
# Interaktiver Chat
litgpt chat \\
  --checkpoint_dir checkpoints/meta-llama/Llama-3.2-3B-Instruct

# Einzelgenerierung
litgpt generate \\
  --prompt "Erkläre GPU-Computing in einfachen Worten" \\
  --checkpoint_dir checkpoints/meta-llama/Llama-3.2-3B-Instruct \\
  --max_new_tokens 200

# Mit Temperatur und Sampling
litgpt generate \\
  --prompt "Schreibe eine Python-Funktion, um eine Liste zu sortieren" \\
  --checkpoint_dir checkpoints/mistralai/Mistral-7B-Instruct-v0.3 \\
  --temperature 0.7 \\
  --top_p 0.9 \\
  --max_new_tokens 500
```

***

## Finetuning

### LoRA-Feinabstimmung (empfohlen)

LoRA trainiert nur eine kleine Menge an Adapter-Parametern (typischerweise 0,1–1 % der Gesamtgewichte), während das Basismodell eingefroren bleibt. Llama 3 8B LoRA auf 10K Beispielen dauert etwa 2 Stunden auf einer RTX 3090 mit `r=16`.

```bash
# Bereite deinen Datensatz vor
# Format: JSON Lines mit {"instruction": "...", "input": "...", "output": "..."}
cat > data/train.json << 'EOF'
{"instruction": "Was ist GPU-Cloud-Computing?", "input": "", "output": "GPU-Cloud-Computing bietet über das Internet on-demand Zugriff auf GPU-Hardware und ermöglicht so KI-Training und Inferenz ohne den Besitz physischer Hardware."}
{"instruction": "Wie miete ich eine GPU auf Clore.ai?", "input": "", "output": "Besuche clore.ai/marketplace, filtere nach GPU-Spezifikationen, wähle einen Server, konfiguriere die Ports und klicke auf Mieten. Der SSH-Zugriff wird sofort bereitgestellt."}
EOF

# Feinabstimmung mit LoRA
litgpt finetune lora \\
  --checkpoint_dir checkpoints/meta-llama/Llama-3.2-3B-Instruct \\
  --data JSON \\
  --data.json_path data/train.json \\
  --train.epochs 3 \\
  --train.micro_batch_size 4 \\
  --lora_r 8 \\
  --lora_alpha 16 \\
  --out_dir out/llama-lora-finetuned

# Training überwachen
# LitGPT gibt Logs mit Verlust, Lernrate und ETA aus
```

### QLoRA (4-Bit + LoRA)

Verwende QLoRA, um große Modelle mit begrenztem VRAM feinabzustimmen. Llama 3 8B passt auf eine einzelne RTX 3090 mit 24 GB:

```bash
litgpt finetune lora \\
  --checkpoint_dir checkpoints/meta-llama/Llama-3.2-8B-Instruct \\
  --quantize bnb.nf4 \\
  --train.epochs 3 \\
  --train.micro_batch_size 2 \\
  --lora_r 16 \\
  --lora_alpha 32 \\
  --out_dir out/llama-qlora
```

### Vollständige Feinabstimmung

```bash
litgpt finetune full \\
  --checkpoint_dir checkpoints/meta-llama/Llama-3.2-3B-Instruct \\
  --data JSON \\
  --data.json_path data/train.json \\
  --train.epochs 2 \\
  --train.micro_batch_size 2 \\
  --train.accumulate_gradients 8 \\
  --out_dir out/llama-full-finetuned
```

### Multi-GPU-Training

```bash
# Verwende FSDP über mehrere GPUs hinweg
litgpt finetune full \\
  --checkpoint_dir checkpoints/meta-llama/Llama-3.2-8B-Instruct \\
  --devices 4 \\
  --strategy fsdp \\
  --train.epochs 3 \\
  --out_dir out/llama-multigpu
```

***

## Modelle bereitstellen (REST-API)

```bash
# Inferenzserver starten
litgpt serve \\
  --checkpoint_dir checkpoints/meta-llama/Llama-3.2-3B-Instruct \\
  --host 0.0.0.0 \
  --port 8000

# API testen
curl -X POST http://localhost:8000/predict \\
  -H "Content-Type: application/json" \\
  -d '{
    "prompt": "Was ist die Hauptstadt von Frankreich?",
    "max_new_tokens": 100,
    "temperature": 0.7
  }'
```

### Python-Client

```python
import requests

response = requests.post(
    "http://<server-ip>:8000/predict",
    json={
        "prompt": "Erkläre Reinforcement Learning",
        "max_new_tokens": 500,
        "temperature": 0.8,
        "top_p": 0.9,
    }
)
print(response.json()["output"])

```

***

## Vortraining von Grund auf

Für das Training eines benutzerdefinierten LLMs von Grund auf mit deinen eigenen Daten:

```bash
# Vortrainingsdaten vorbereiten (tokenisiert und in Chunks aufgeteilt)
python scripts/prepare_redpajama.py \\
  --source_path /data/raw_text \\
  --checkpoint_dir checkpoints/meta-llama/Llama-3.2-3B-Instruct \\
  --destination_path /data/tokenized

# Vortraining starten
litgpt pretrain \\
  --model_name Llama-3.2 \\
  --data /data/tokenized \\
  --train.micro_batch_size 4 \\
  --train.max_tokens 10_000_000_000 \\
  --devices 8 \\
  --strategy fsdp \\
  --out_dir out/my-pretrained-llm
```

***

## Modelle konvertieren und exportieren

```bash
# LoRA-Gewichte in das Basismodell zusammenführen
litgpt merge_lora \\
  --checkpoint_dir out/llama-lora-finetuned

# Für die Verteilung in HuggingFace-Format konvertieren
litgpt convert to_hf \\
  --checkpoint_dir out/llama-lora-finetuned/final \\
  --output_dir hf_model/

# In GGUF-Format exportieren (für Ollama/LlamaCpp)
# Verwende das llama.cpp-Konvertierungsskript nach dem HF-Export
python llama.cpp/convert.py hf_model/ --outfile model.gguf
```

***

## Modelle evaluieren

```bash
# MMLU-Benchmark ausführen
litgpt evaluate \\
  --checkpoint_dir checkpoints/meta-llama/Llama-3.2-3B-Instruct \\
  --tasks mmlu \\
  --num_fewshot 5

# Mehrere Benchmarks ausführen
litgpt evaluate \\
  --checkpoint_dir out/llama-lora-finetuned/final \\
  --tasks "mmlu,hellaswag,truthfulqa_mc"
```

***

## GPU-Empfehlungen für Clore.ai

{% hint style="warning" %}
**Multi-GPU-Rigs der 80GB-Klasse sind auf dem Clore.ai-Marktplatz nicht gelistet.** Die größten heute gelisteten Systeme sind 4× RTX PRO 6000 Blackwell (je 96 GB, 380 GB gesamt) und 8–11× RTX 5090 (je 32 GB). Kapazitäten für A100 / H200 / B200 werden als [Bare Metal](https://clore.ai/bare-metal) auf Anfrage verkauft. Prüfe [GPU-Preise & Verfügbarkeit](/guides/guides_v2-de/erste-schritte/pricing.md) bevor du eine Bereitstellung dimensionierst.
{% endhint %}

LitGPT deckt drei unterschiedliche Workloads ab — Inferenz, LoRA-Feinabstimmung und vollständiges Vortraining — jeweils mit unterschiedlichen GPU-Anforderungen.

| Arbeitslast                                               | GPU            | VRAM  | Hinweise                                                                            |
| --------------------------------------------------------- | -------------- | ----- | ----------------------------------------------------------------------------------- |
| Inferenz / Chat (7–8B-Modelle)                            | **RTX 3090**   | 24 GB | Passt Llama 3 8B in bf16; \~95 Tok/s Generierung                                    |
| LoRA-Feinabstimmung (7–8B-Modelle)                        | **RTX 3090**   | 24 GB | Preisgünstige Wahl; QLoRA hält den VRAM unter 10 GB                                 |
| LoRA-Feinabstimmung (7–8B), schnelle Iteration            | **RTX 4090**   | 24 GB | Etwa 35 % schneller als die 3090; verkürzt einen 2-Stunden-Job auf etwa 1,4 Stunden |
| Vollständige Feinabstimmung (7B) oder QLoRA (70B)         | **A100 40 GB** | 40 GB | 40 GB reichen für 7B in voller Präzision oder 70B in 4 Bit                          |
| Vollständige Feinabstimmung (13B+) oder Vortraining-Läufe | **A100 80 GB** | 80 GB | Höchster Durchsatz; \~2.800 Tok/s Training auf 8B                                   |

**Für die meisten Nutzer empfohlen:** RTX-3090-Paar (2×24 GB = 48 GB effektiv mit FSDP). Bewältigt QLoRA auf 70B-Modellen oder vollständige Feinabstimmung auf 7B-Modellen mit Tensorparallelismus. Kosten auf Clore.ai: $0.07–0.21/Stunde für zwei 3090s.

**Für Vortraining oder Feinabstimmung von >70B:** Verwende 4×A100 80GB mit FSDP. Die FSDP-Integration von LitGPT übernimmt das Sharding transparent — einfach übergeben `--devices 4 --strategy fsdp`.

***

## Fehlerbehebung

### CUDA Out of Memory

```bash
# Batch-Größe reduzieren
--train.micro_batch_size 1

# Gradient Checkpointing aktivieren
--train.gradient_checkpointing true

# Verwende QLoRA statt LoRA
--quantize bnb.nf4

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

### Download schlägt fehl / HuggingFace 401

```bash
# HF-Token festlegen
export HF_TOKEN=hf_your-token-here
huggingface-cli login

# Oder direkt übergeben
litgpt download \\
  --repo_id meta-llama/Llama-3.2-3B-Instruct \\
  --access_token hf_your-token
```

### Trainingsverlust sinkt nicht

```bash
# Prüfe dein Datenformat — muss gültige JSON Lines sein
python -c "
import json
with open('data/train.json') as f:
    for i, line in enumerate(f):
        json.loads(line)
        if i < 3: print(f'Line {i}: OK')
print('All lines valid')
"

# Lernrate senken
--train.lr 1e-5  # Der Standardwert ist für kleine Datensätze oft zu hoch

# Datensatzgröße prüfen — LoRA benötigt mindestens 100–1000 Beispiele
wc -l data/train.json
```

### Server-Port 8000 nicht erreichbar

```bash
# Prüfe, ob der Server lauscht
ss -tlnp | grep 8000

# Firewall öffnen
ufw allow 8000/tcp

# Server mit explizitem Host neu starten
litgpt serve \\
  --checkpoint_dir checkpoints/... \\
  --host 0.0.0.0 \
  --port 8000
```

### Multi-GPU-Training hängt

```bash
# NCCL-Konnektivität prüfen
python -c "import torch; print(torch.cuda.device_count())"

# Versuche bei kleineren Modellen DDP statt FSDP
--strategy ddp

# NCCL-Umgebungsvariablen setzen
export NCCL_DEBUG=INFO
export NCCL_IB_DISABLE=1  # Falls InfiniBand nicht verfügbar ist
```

***

## Nützliche Links

* **GitHub**: <https://github.com/Lightning-AI/litgpt> ⭐ 12K+
* **Dokumentation**: <https://lightning.ai/docs/litgpt>
* **PyTorch Lightning**: <https://lightning.ai>
* **HuggingFace-Modelle**: <https://huggingface.co/models>
* **Discord**: <https://discord.gg/lightning-ai>
* **Clore.ai-Marktplatz**: <https://clore.ai/marketplace>


---

# 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-de/training/litgpt.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.
