> 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/sprachmodelle/mlc-llm.md).

# MLC-LLM

**Universelle LLM-Bereitstellung durch ML-Kompilierung** — führe jedes große Sprachmodell auf jeder Hardware mit maximaler Leistung mithilfe von Machine-Learning-Kompilierung aus.

> 🌟 **20.000+ GitHub-Stars** | Betreut vom MLC-AI-Team | Apache-2.0-Lizenz

***

## Was ist MLC-LLM?

MLC-LLM (Machine Learning Compilation for Large Language Models) ist ein universelles Framework, das eine effiziente Bereitstellung großer Sprachmodelle über verschiedene Hardware-Backends hinweg ermöglicht. Durch die Nutzung von **TVM (Tensor Virtual Machine)** als Kompilierungs-Backend kompiliert MLC-LLM LLM-Modelle direkt zu nativem Hardware-Code — und erreicht nahezu optimale Leistung ohne hardware-spezifische Entwicklung.

### Wichtige Funktionen

* **Universelle Hardware-Unterstützung** — NVIDIA CUDA, AMD ROCm, Apple Metal, Vulkan, WebGPU
* **OpenAI-kompatible REST-API** — direkter Ersatz für bestehende Workflows
* **Mehrere Modellformate** — Llama, Mistral, Gemma, Phi, Qwen, Falcon und mehr
* **4-Bit-/8-Bit-Quantisierung** — große Modelle auf Consumer-GPUs ausführen
* **Chat-Oberfläche** — integrierte Web-UI für sofortiges Testen
* **Python- und CLI-Tools** — flexible Integrationsmöglichkeiten

### Warum MLC-LLM auf Clore.ai nutzen?

Der GPU-Marktplatz von Clore.ai verschafft dir Zugang zu leistungsstarken NVIDIA-GPUs zu wettbewerbsfähigen Mietpreisen. Der Kompilierungsansatz von MLC-LLM holt den maximalen Durchsatz aus jeder GPU heraus — ideal für:

* Produktions-API-Inferenz in großem Maßstab
* Forschung und Benchmarking über verschiedene Modellgrößen hinweg
* Kosteneffizientes Serving mit quantisierten Modellen
* Bereitstellung mehrerer Modelle auf einer einzelnen GPU-Instanz

***

## Schnellstart auf Clore.ai

### Schritt 1: Finde einen GPU-Server

1. Gehe zu [clore.ai](https://clore.ai) Marktplatz
2. Server filtern: **NVIDIA-GPU**, mindestens **8 GB VRAM** (16 GB+ empfohlen für 7B+-Modelle)
3. Für optimale Leistung: RTX 3090, RTX 4090, A100 oder H100

### Schritt 2: MLC-LLM bereitstellen

{% hint style="info" %}
**Hinweis:** MLC-LLM veröffentlicht kein offizielles vorgefertigtes Docker-Image auf Docker Hub. Der empfohlene Bereitstellungsansatz ist, ein NVIDIA-CUDA-Basisimage zu verwenden und MLC-LLM per pip zu installieren. Verwende `nvidia/cuda:12.8.1-devel-ubuntu22.04` als dein Basisimage auf Clore.ai.
{% endhint %}

Verwende in deiner Clore.ai-Bestellkonfiguration ein NVIDIA-CUDA-Basisimage:

```
Docker-Image: nvidia/cuda:12.8.1-devel-ubuntu22.04
```

**Portzuordnungen:**

| Container-Port | Zweck           |
| -------------- | --------------- |
| `22`           | SSH-Zugriff     |
| `8000`         | REST-API-Server |

**Empfohlene Umgebungsvariablen:**

```
MLC_MODEL=HF://mlc-ai/Llama-3-8B-Instruct-q4f16_1-MLC
MLC_HOST=0.0.0.0
MLC_PORT=8000
```

**Startskript** (nach SSH ausführen):

```bash
pip install --pre -U -f https://mlc.ai/wheels mlc-llm-nightly-cu121 mlc-ai-nightly-cu121
```

### Schritt 3: Per SSH verbinden

```bash
ssh root@<clore-node-ip> -p <assigned-ssh-port>
```

***

## Installation & Einrichtung

### Option A: Vorgekompilierte Modelle verwenden (am schnellsten)

MLC-AI verwaltet eine Bibliothek vorgekompilierter Modelle auf Hugging Face. Keine Kompilierung erforderlich:

```bash
# Vorgekompilierte Llama 3 8B abrufen und ausführen (4-Bit-quantisiert)
python -m mlc_llm serve HF://mlc-ai/Llama-3-8B-Instruct-q4f16_1-MLC \
  --host 0.0.0.0 \
  --port 8000
```

### Option B: Eigenes Modell kompilieren

Für benutzerdefinierte Modelle oder spezifische Quantisierungsanforderungen:

```bash
# Schritt 1: Modellgewichte konvertieren
python -m mlc_llm convert_weight \
  ./path/to/model \
  --quantization q4f16_1 \
  --output ./compiled/model-q4f16_1

# Schritt 2: Modellkonfiguration erzeugen
python -m mlc_llm gen_config \
  ./path/to/model \
  --quantization q4f16_1 \
  --conv-template llama-3 \
  --output ./compiled/model-q4f16_1

# Schritt 3: Das Modell kompilieren
python -m mlc_llm compile \
  ./compiled/model-q4f16_1/mlc-chat-config.json \
  --device cuda \
  --output ./compiled/model-q4f16_1/lib.so
```

{% hint style="info" %}
**Kompilierungszeit:** Das Kompilieren eines 7B-Modells dauert beim ersten Lauf typischerweise 10–30 Minuten. Kompilierte Artefakte werden zwischengespeichert und bei späteren Starts wiederverwendet.
{% endhint %}

***

## Ausführen des API-Servers

### Starte den OpenAI-kompatiblen Server

```bash
python -m mlc_llm serve \
  HF://mlc-ai/Llama-3-8B-Instruct-q4f16_1-MLC \
  --host 0.0.0.0 \
  --port 8000 \\
  --max-batch-size 4 \
  --max-total-sequence-length 8192
```

### Ausgabe beim Serverstart

```
[2024-01-01 12:00:00] INFO: Lade Modell von HF://mlc-ai/Llama-3-8B-Instruct-q4f16_1-MLC
[2024-01-01 12:00:15] INFO: Modell erfolgreich geladen
[2024-01-01 12:00:15] INFO: Server wird auf 0.0.0.0:8000 gestartet
[2024-01-01 12:00:15] INFO: OpenAI-kompatible API verfügbar unter http://0.0.0.0:8000/v1
```

### Verfügbare API-Endpunkte

| Endpunkt                     | Methode | Beschreibung                             |
| ---------------------------- | ------- | ---------------------------------------- |
| `/v1/chat/completions`       | POST    | Chat-Vervollständigungen (OpenAI-Format) |
| `/v1/completions`            | POST    | Text-Vervollständigungen                 |
| `/v1/models`                 | GET     | Verfügbare Modelle auflisten             |
| `/v1/debug/dump_event_trace` | GET     | Leistungsdebugging                       |

***

## API-Verwendungsbeispiele

### Chat-Vervollständigungen (Python)

```python
from openai import OpenAI

# Verweise auf deinen Clore.ai-Server
client = OpenAI(
    base_url="http://<clore-node-ip>:<api-port>/v1",
    api_key="none"  # MLC-LLM erfordert standardmäßig keine Authentifizierung
)

response = client.chat.completions.create(
    model="Llama-3-8B-Instruct-q4f16_1-MLC",
    messages=[
        {"role": "system", "content": "Du bist ein hilfreicher Assistent."},
        {"role": "user", "content": "Erkläre Quantencomputing in einfachen Worten."}
    ],
    temperature=0.7,
    max_tokens=512
)

print(response.choices[0].message.content)
```

### Streaming-Antwort

```python
stream = client.chat.completions.create(
    model="Llama-3-8B-Instruct-q4f16_1-MLC",
    messages=[{"role": "user", "content": "Schreibe eine Kurzgeschichte über KI."}],
    stream=True,
    max_tokens=1024
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)
```

### cURL-Beispiel

```bash
curl http://<clore-node-ip>:<api-port>/v1/chat/completions \
  -H "Content-Type: application/json" \\
  -d '{
    "model": "Llama-3-8B-Instruct-q4f16_1-MLC",
    "messages": [
      {"role": "user", "content": "Was ist 2+2?"}
    ],
    "temperature": 0.7,
    "max_tokens": 100
  }'
```

***

## Verfügbare vorgekompilierte Modelle

MLC-AI stellt sofort nutzbare kompilierte Modelle auf Hugging Face bereit:

### Llama-3-Serie

```bash
# 8B Instruct (für die meisten Anwendungsfälle empfohlen)
HF://mlc-ai/Llama-3-8B-Instruct-q4f16_1-MLC

# 70B Instruct (erfordert 40 GB+ VRAM oder Multi-GPU)
HF://mlc-ai/Llama-3-70B-Instruct-q4f16_1-MLC
```

### Mistral / Mixtral

```bash
HF://mlc-ai/Mistral-7B-Instruct-v0.3-q4f16_1-MLC
HF://mlc-ai/Mixtral-8x7B-Instruct-v0.1-q4f16_1-MLC
```

### Gemma

```bash
HF://mlc-ai/gemma-2b-it-q4f16_1-MLC
HF://mlc-ai/gemma-7b-it-q4f16_1-MLC
```

### Phi

```bash
HF://mlc-ai/phi-2-q4f16_1-MLC
HF://mlc-ai/Phi-3-mini-4k-instruct-q4f16_1-MLC
```

{% hint style="success" %}
**Vollständige Modellliste:** Alle vorgekompilierten Modelle ansehen unter [huggingface.co/mlc-ai](https://huggingface.co/mlc-ai)
{% endhint %}

***

## Quantisierungsoptionen

MLC-LLM unterstützt mehrere Quantisierungsschemata. Wähle je nach deinem VRAM-Budget:

| Quantisierung | Bits                         | Qualität | VRAM (7B) | VRAM (13B) |
| ------------- | ---------------------------- | -------- | --------- | ---------- |
| `q4f16_1`     | 4-Bit                        | ★★★★☆    | \~4 GB    | \~7 GB     |
| `q4f32_1`     | 4-Bit (f32-Akk.)             | ★★★★☆    | \~4 GB    | \~7 GB     |
| `q8f16_1`     | 8-Bit                        | ★★★★★    | \~8 GB    | \~14 GB    |
| `q0f16`       | 16-Bit (keine Quantisierung) | ★★★★★    | \~14 GB   | \~26 GB    |
| `q0f32`       | 32-Bit (keine Quantisierung) | ★★★★★    | \~28 GB   | \~52 GB    |

{% hint style="warning" %}
**VRAM-Empfehlung:** Lass immer 2–3 GB Reserve für CUDA-Overhead und KV-Cache. Ein 7B-Modell mit `q4f16_1` benötigt bei einer typischen Arbeitslast insgesamt etwa 6–7 GB.
{% endhint %}

***

## Multi-GPU-Bereitstellung

Für große Modelle (70B+), die mehrere GPUs erfordern:

```bash
# Tensor-Parallelismus über 2 GPUs aktivieren
python -m mlc_llm serve \
  HF://mlc-ai/Llama-3-70B-Instruct-q4f16_1-MLC \
  --host 0.0.0.0 \
  --port 8000 \\
  --tensor-parallel-shards 2
```

Prüfe vor der Bereitstellung die GPU-Topologie:

```bash
nvidia-smi topo -m  # NVLink-/PCIe-Konnektivität prüfen
```

{% hint style="info" %}
**Beste Leistung:** Multi-GPU funktioniert am besten mit über NVLink verbundenen Karten (z. B. A100 80GB SXM-Paare). Über PCIe verbundene GPUs zeigen bei großen Modellen Engpässe.
{% endhint %}

***

## Web-Chat-Oberfläche

MLC-LLM enthält eine integrierte Web-UI, die verfügbar ist, sobald der Server läuft:

```bash
# Server mit aktivierter Web-UI starten
python -m mlc_llm serve \
  HF://mlc-ai/Llama-3-8B-Instruct-q4f16_1-MLC \
  --host 0.0.0.0 \
  --port 8000 \\
  --enable-debug  # Optional: aktiviert den Debug-Endpunkt
```

Rufe die UI unter folgender Adresse auf: `http://<clore-node-ip>:<api-port>`

***

## Leistungsoptimierung

### Batch-Größe optimieren

```bash
# Batch-Größe erhöhen für höheren Durchsatz (erfordert mehr VRAM)
python -m mlc_llm serve \
  HF://mlc-ai/Llama-3-8B-Instruct-q4f16_1-MLC \
  --host 0.0.0.0 \
  --port 8000 \\
  --max-batch-size 8 \
  --max-total-sequence-length 16384 \
  --prefill-chunk-size 2048
```

### GPU-Auslastung überwachen

```bash
# In einem separaten Terminal
watch -n 1 nvidia-smi

# Detailliertere Überwachung
nvidia-smi dmon -s u  # Streaming-Auslastungsmetriken
```

### Durchsatz benchmarken

```python
import time
from openai import OpenAI

client = OpenAI(base_url="http://localhost:8000/v1", api_key="none")

start = time.time()
response = client.chat.completions.create(
    model="Llama-3-8B-Instruct-q4f16_1-MLC",
    messages=[{"role": "user", "content": "Zähle von 1 bis 100"}],
    max_tokens=512
)
elapsed = time.time() - start

tokens = response.usage.completion_tokens
print(f"Durchsatz: {tokens/elapsed:.1f} Token/s")
```

***

## Docker-Compose-Setup

Für eine produktionsreife Bereitstellung auf Clore.ai mit einem NVIDIA-CUDA-Basisimage und per pip installiertem MLC-LLM:

```yaml
version: '3.8'
services:
  mlc-llm:
    image: nvidia/cuda:12.8.1-devel-ubuntu22.04
    runtime: nvidia
    environment:
      - NVIDIA_VISIBLE_DEVICES=all
    ports:
      - "8000:8000"
    volumes:
      - ./models:/root/models
      - mlc-cache:/root/.cache/mlc_llm
    command: >
      bash -c "pip install --pre -U -f https://mlc.ai/wheels mlc-llm-nightly-cu121 mlc-ai-nightly-cu121 &&
      python -m mlc_llm serve
      HF://mlc-ai/Llama-3-8B-Instruct-q4f16_1-MLC
      --host 0.0.0.0
      --port 8000
      --max-batch-size 4"
    restart: unless-stopped

volumes:
  mlc-cache:
```

***

## Fehlerbehebung

### Modell-Download schlägt fehl

```bash
# Internetverbindung prüfen
curl -I https://huggingface.co

# Mit huggingface-cli manuell herunterladen
pip install huggingface_hub
huggingface-cli download mlc-ai/Llama-3-8B-Instruct-q4f16_1-MLC
```

### Speicher erschöpft (OOM)

```bash
# Kontextlänge reduzieren
python -m mlc_llm serve MODEL \
  --max-total-sequence-length 4096  # Gegenüber dem Standard reduzieren

# Aggressivere Quantisierung verwenden
# Von q8f16_1 zu q4f16_1 wechseln
```

### CUDA-Versionsinkompatibilität

```bash
# CUDA-Version prüfen
nvcc --version
nvidia-smi | grep CUDA

# Für CUDA-12.8-Server installieren:
pip install --pre -U -f https://mlc.ai/wheels mlc-llm-nightly-cu121 mlc-ai-nightly-cu121

# Für CUDA-13.x-Server installieren:
pip install --pre -U -f https://mlc.ai/wheels mlc-llm-nightly-cu122 mlc-ai-nightly-cu122
```

{% hint style="danger" %}
**Häufige Falle:** MLC-LLM-Pip-Wheels sind CUDA-version-spezifisch. Stelle sicher, dass du die richtige Variante installierst, die zu der CUDA-Version deines Servers passt. Verfügbare Wheels findest du unter [mlc.ai/wheels](https://mlc.ai/wheels).
{% endhint %}

### Server nicht erreichbar

```bash
# Prüfen, ob der Port lauscht
ss -tlnp | grep 8000

# Firewall prüfen
iptables -L -n | grep 8000

# Zuerst lokal testen
curl http://localhost:8000/v1/models
```

***

## GPU-Empfehlungen für Clore.ai

Der Kompilierungsansatz von MLC-LLM liefert auf jeder GPU-Klasse nahezu optimalen Durchsatz. Wähle je nach Modellgröße und Budget:

| GPU       | VRAM  | Clore.ai-Preis                            | Am besten geeignet für                  | Durchsatz (Llama 3 8B Q4) |
| --------- | ----- | ----------------------------------------- | --------------------------------------- | ------------------------- |
| RTX 3090  | 24 GB | ca. 0,07–0,21 $/h                         | 7B–13B-Modelle, kostengünstiges Serving | \~85 tok/s                |
| RTX 4090  | 24 GB | ca. 0,14–0,42 $/h                         | 7B–34B-Modelle, schnelles Serving       | \~140 tok/s               |
| A100 40GB | 40 GB | [Bare Metal](https://clore.ai/bare-metal) | 34B–70B, Produktions-API                | \~110 tok/s               |
| A100 80GB | 80 GB | [Bare Metal](https://clore.ai/bare-metal) | 70B+, Multi-Modell-Serving              | \~130 tok/s               |
| H100 SXM  | 80 GB | \~$1.04/hr                                | Maximaler Durchsatz, FP8                | \~280 tok/s               |

**Empfohlener Startpunkt:** Die RTX 3090 für $0.07–0.21/Std. bietet das beste Preis-Leistungs-Verhältnis für das Serving von Llama 3 8B und Mistral 7B über MLC-LLM. Die kompilierten Kernel holen eine nahezu maximale Auslastung aus Consumer-GPUs heraus.

Für 70B-Modelle (z. B. Llama 3 70B Q4): A100 40GB ([Bare Metal](https://clore.ai/bare-metal)) oder zwei RTX 3090s per Tensor-Parallelismus verwenden.

***

## Ressourcen

* 📦 **Pip-Wheels:** [mlc.ai/wheels](https://mlc.ai/wheels) (Installation per pip, kein Docker-Hub-Image verfügbar)
* 🐙 **GitHub:** [github.com/mlc-ai/mlc-llm](https://github.com/mlc-ai/mlc-llm)
* 📚 **Dokumentation:** [llm.mlc.ai/docs](https://llm.mlc.ai/docs)
* 🤗 **Vorgekompilierte Modelle:** [huggingface.co/mlc-ai](https://huggingface.co/mlc-ai)
* 💬 **Discord:** [discord.gg/9Xpy2HGBuD](https://discord.gg/9Xpy2HGBuD)


---

# 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/sprachmodelle/mlc-llm.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.
