# Anleitung zum Hot-Swapping von LoRA

### :shaved\_ice: vLLM LoRA Hot Swapping / Dynamische LoRAs

Um LoRA-Serving für höchstens 4 LoRAs gleichzeitig zu ermöglichen (diese werden zur Laufzeit getauscht/geändert), setzen Sie zuerst die Umgebungsvariable, um Hot Swapping zu erlauben:

```bash
export VLLM_ALLOW_RUNTIME_LORA_UPDATING=True
```

Dann dienen Sie es mit LoRA-Unterstützung:

```bash
export VLLM_ALLOW_RUNTIME_LORA_UPDATING=True
vllm serve unsloth/Llama-3.1-8B-Instruct \
    --quantization fp8 \
    --kv-cache-dtype fp8
    --gpu-memory-utilization 0.8 \
    --max-model-len 65536 \
    --enable-lora \
    --max-loras 4 \
    --max-lora-rank 64
```

Um eine LoRA dynamisch zu laden (setzen Sie auch den LoRA-Namen), führen Sie aus:

```bash
curl -X POST http://localhost:8000/v1/load_lora_adapter \
    -H "Content-Type: application/json" \
    -d '{
        "lora_name": "LORA_NAME",
        "lora_path": "/path/to/LORA"
    }'
```

Um sie aus dem Pool zu entfernen:

```bash
curl -X POST http://localhost:8000/v1/unload_lora_adapter \
    -H "Content-Type: application/json" \
    -d '{
        "lora_name": "LORA_NAME"
    }'
```

Zum Beispiel beim Fine-Tuning mit Unsloth:

{% code overflow="wrap" %}

```python
from unsloth import FastLanguageModel
import torch
model, tokenizer = FastLanguageModel.from_pretrained(
    model_name = "unsloth/Llama-3.1-8B-Instruct",
    max_seq_length = 2048,
    load_in_4bit = True,
)
model = FastLanguageModel.get_peft_model(model)
```

{% endcode %}

Dann speichern wir nach dem Training die LoRAs:

```python
model.save_pretrained("finetuned_lora")
tokenizer.save_pretrained("finetuned_lora")
```

Wir können dann die LoRA laden:

{% code overflow="wrap" %}

```bash
curl -X POST http://localhost:8000/v1/load_lora_adapter \
    -H "Content-Type: application/json" \
    -d '{
        "lora_name": "LORA_NAME_finetuned_lora",
        "lora_path": "finetuned_lora"
    }'
```

{% endcode %}


---

# Agent Instructions: 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://unsloth.ai/docs/de/grundlagen/inference-and-deployment/vllm-guide/lora-hot-swapping-guide.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.
