# FP16 vs BF16 für RL

### Float16 vs Bfloat16

Es gab ein Paper mit dem Titel „**Überwindung der Trainings-Inferenz-Unstimmigkeit mittels FP16**" <https://arxiv.org/pdf/2510.26788> in dem gezeigt wird, dass die Verwendung von Float16-Präzision beim Reinforcement Learning deutlich besser sein kann als die Verwendung von Bfloat16.

<figure><img src="https://797013937-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Frec4qe1aQS0xyMzGvS9c%2Fimage.png?alt=media&#x26;token=2137e766-0f1f-48ec-b25f-2292d6f149f4" alt=""><figcaption></figcaption></figure>

Tatsächlich wird es bei längeren Generierungen immer schlimmer, wenn man Bfloat16 verwendet:

<figure><img src="https://797013937-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FWs7ioB2lraTbDbUCOAnn%2Fimage.png?alt=media&#x26;token=ac2b4f8e-210f-4bcc-bcbb-6e68f80781a6" alt=""><figcaption></figcaption></figure>

Wir haben eine Untersuchung durchgeführt, und **FESTGESTELLT, dass Float16 stabiler ist** als Bfloat16 mit deutlich kleineren Gradientennormen siehe <https://x.com/danielhanchen/status/1985557028295827482> und <https://x.com/danielhanchen/status/1985562902531850472>

{% columns %}
{% column width="50%" %}

<figure><img src="https://797013937-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FhvQ1W5wtV6TTfsetp7y2%2FG44d7ZFbIAANBBd.jpg?alt=media&#x26;token=35181a07-de3e-4321-b54e-4436b4a201ff" alt=""><figcaption></figcaption></figure>

<figure><img src="https://797013937-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2F62HkxnGcaKvxnSxbZMZu%2FG44c20SbwAAGo8j.jpg?alt=media&#x26;token=e0c7ecb8-6f0c-4ecf-b1a0-50f1b2a9a807" alt=""><figcaption></figcaption></figure>
{% endcolumn %}

{% column width="50%" %}

<figure><img src="https://797013937-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fsi18IkGqE4IuUvzroyHh%2FG44ix5FbQAM0L5l.jpg?alt=media&#x26;token=bc3b97ce-5df4-4b69-aa50-a8e339f21601" alt=""><figcaption></figcaption></figure>
{% endcolumn %}
{% endcolumns %}

### :exploding\_head:A100 Cascade-Attention-Fehler

Laut <https://x.com/RichardYRLi/status/1984858850143715759> und <https://yingru.notion.site/When-Speed-Kills-Stability-Demystifying-RL-Collapse-from-the-Training-Inference-Mismatch-271211a558b7808d8b12d403fd15edda>, hatten ältere vLLM-Versionen (vor 0.11.0) fehlerhafte Attention-Mechanismen für A100 und ähnliche GPUs. Bitte aktualisieren Sie vLLM! Wir deaktivieren außerdem standardmäßig Cascade Attention in vLLM während Unsloth-Reinforcement-Learning, wenn wir eine ältere vLLM-Version feststellen.

<figure><img src="https://797013937-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FnkCLRVIIGLADXBSCe58e%2Fimage.png?alt=media&#x26;token=6669642f-8690-44bf-b2de-6aa89acf2332" alt=""><figcaption></figcaption></figure>

Verschiedene Hardware verändert ebenfalls die Ergebnisse; neuere und teurere GPUs zeigen geringere KL-Differenzen zwischen Inferenz- und Trainingsseite:

<figure><img src="https://797013937-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FaroTTz68zzyofy6nagtH%2Fimage.webp?alt=media&#x26;token=3be09506-b8a0-42eb-8d17-af72496a9cd1" alt=""><figcaption></figcaption></figure>

### :fire:Verwendung von Float16 in Unsloth RL

Um Float16-Präzision in Unsloth GRPO und RL zu verwenden, müssen Sie lediglich `dtype = torch.float16` setzen, und wir kümmern uns um den Rest!

{% code overflow="wrap" %}

```python
from unsloth import FastLanguageModel
import torch
max_seq_length = 2048 # Kann für längere Reasoning-Traces erhöht werden
lora_rank = 32 # Größerer Rank = intelligenter, aber langsamer

model, tokenizer = FastLanguageModel.from_pretrained(
    model_name = "unsloth/Qwen3-4B-Base",
    max_seq_length = max_seq_length,
    load_in_4bit = False, # False für LoRA 16bit
    fast_inference = True, # vLLM Fast-Inferenz aktivieren
    max_lora_rank = lora_rank,
    gpu_memory_utilization = 0.9, # Bei Speichermangel reduzieren
    
    dtype = torch.float16, # Verwenden Sie torch.float16, torch.bfloat16
)
```

{% endcode %}
