# RLにおけるFP16とBF16

### Float16 と Bfloat16 の比較

次のタイトルの論文がありました「**トレーニングと推論の不一致を FP16 で克服する**" <https://arxiv.org/pdf/2510.26788> 強化学習を行う際に、bfloat16 よりも float16 の精度を使用する方が劇的に優れていることを示しています。

<figure><img src="/files/f6c4b3fccbc0d37c201f74a9a65bca9f1140998d" alt=""><figcaption></figcaption></figure>

実際、生成が長くなるほど、bfloat16 を使用した場合の悪化が大きくなります：

<figure><img src="/files/6b7dc50bb9de5a2cd90b29f020a647ea83087c92" alt=""><figcaption></figcaption></figure>

我々は調査を行い、 **float16 の方がより安定していることを確認しました** bfloat16 よりも勾配ノルムがはるかに小さいことが見られます。詳細は <https://x.com/danielhanchen/status/1985557028295827482> および <https://x.com/danielhanchen/status/1985562902531850472>

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

<figure><img src="/files/a731a5d2d05a2b162e14a8b1991877457612e444" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/2a87245e4632c279bff83d99b9f1d8463d8ba658" alt=""><figcaption></figcaption></figure>
{% endcolumn %}

{% column width="50%" %}

<figure><img src="/files/145f4d9544522f5e17f2505bb0bbd24f116ad871" alt=""><figcaption></figcaption></figure>
{% endcolumn %}
{% endcolumns %}

### :exploding\_head:A100 のカスケードアテンションのバグ

以下によれば <https://x.com/RichardYRLi/status/1984858850143715759> および <https://yingru.notion.site/When-Speed-Kills-Stability-Demystifying-RL-Collapse-from-the-Training-Inference-Mismatch-271211a558b7808d8b12d403fd15edda>古い vLLM バージョン（0.11.0 より前）では A100 や類似の GPU に対してアテンション機構が壊れていました。vLLM をアップデートしてください！また、Unsloth の強化学習中に古い vLLM バージョンを検出した場合、vLLM ではデフォルトでカスケードアテンションを無効にしています。

<figure><img src="/files/527b80d04454e21428519f66975d252eb101993e" alt=""><figcaption></figcaption></figure>

ハードウェアが変わると結果も変わり、より新しく高価な GPU の方が推論側とトレーニング側の KL 差が小さくなります：

<figure><img src="/files/0ffa204278e57c8535ddd0ce90def8425431c0a7" alt=""><figcaption></figcaption></figure>

### :fire:Unsloth RL での float16 の使用

Unsloth の GRPO および RL で float16 精度を使用するには、次のように設定するだけです `dtype = torch.float16` あとはこちらで処理します！

{% code overflow="wrap" %}

```python
from unsloth import FastLanguageModel
import torch
max_seq_length = 2048 # より長い推論履歴には増やせます
lora_rank = 32 # ランクが大きいほど賢くなりますが遅くなります

model, tokenizer = FastLanguageModel.from_pretrained(
    model_name = "unsloth/Qwen3-4B-Base",
    max_seq_length = max_seq_length,
    load_in_4bit = False, # LoRA 16bit用はFalse
    fast_inference = True, # vLLM の高速推論を有効にする
    max_lora_rank = lora_rank,
    gpu_memory_utilization = 0.9, # メモリ不足の場合は値を下げてください
    
    dtype = torch.float16, # torch.float16、torch.bfloat16 を使用可能
)
```

{% 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/jp/meru/reinforcement-learning-rl-guide/advanced-rl-documentation/fp16-vs-bf16-for-rl.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.
