unsloth/unsloth
andrewor14 89d8677c0c Enable FP8 + RL training for bf16 models (#3440)
* Enable FP8 + RL training for bf16 models

**Summary:** Enable FP8 + RL training using TorchAO for 1.33x faster training and 42% less model memory usage:
- We quantize the frozen LoRA weights into fp8 and keep the LoRA adapters in bf16
- We leverage TorchAO's `Float8Tensor`, which calls into fbgemm's fp8 x fp8 rowwise matmul kernel
- For now, we need to do an offline quantization first, because vllm doesn't support on-the-fly quantization for torchao yet  (this is in progress: https://github.com/vllm-project/vllm/pull/26327)

**Example usage:**
```
model, tokenizer = FastLanguageModel.from_pretrained(
    model_name = "unsloth/Qwen3-8B-Base",
    max_seq_length = 2048,
    load_in_4bit = False,
    fast_inference = True,
    max_lora_rank = 32,
    load_in_fp8 = True,  # set this to True
)

\# the rest is the same as before
model = FastLanguageModel.get_peft_model(...)
```

**Initial results:**
```
\# fp8
{'train_runtime': 1725.4337, 'train_samples_per_second': 0.232, 'train_steps_per_second': 0.058, 'train_loss': 0.00015715716748673002, 'epoch': 0.01}

\# bf16
{'train_runtime': 2297.8145, 'train_samples_per_second': 0.174, 'train_steps_per_second': 0.044, 'train_loss': 0.00016081033063528594, 'epoch': 0.01}
```

<img width="1199" height="448" alt="Screenshot 2025-11-11 at 4 10 50 PM" src="https://github.com/user-attachments/assets/b6304afd-89e9-42b1-8064-775807e17b23" />

Test script: https://gist.github.com/andrewor14/5b85119fae46845d07b608d420907423

**Requires:**
- https://github.com/pytorch/ao/pull/3158 (torchao nightly or 0.15.0+)
- https://github.com/unslothai/unsloth-zoo/pull/351

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update utils.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* _get_inference_mode_context_manager

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update utils.py

* Update utils.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Daniel Han <danielhanchen@gmail.com>
2025-11-19 23:51:43 -08:00
..
dataprep Formatting & bug fixes (#3563) 2025-11-07 06:00:22 -08:00
kernels Enable FP8 + RL training for bf16 models (#3440) 2025-11-19 23:51:43 -08:00
models Enable FP8 + RL training for bf16 models (#3440) 2025-11-19 23:51:43 -08:00
registry Formatting & bug fixes (#3563) 2025-11-07 06:00:22 -08:00
utils Formatting & bug fixes (#3563) 2025-11-07 06:00:22 -08:00
__init__.py Formatting & bug fixes (#3563) 2025-11-07 06:00:22 -08:00
_auto_install.py Add Torch 2.9 options 2025-10-26 21:49:30 -07:00
chat_templates.py Formatting & bug fixes (#3563) 2025-11-07 06:00:22 -08:00
device_type.py Formatting & bug fixes (#3563) 2025-11-07 06:00:22 -08:00
import_fixes.py Formatting & bug fixes (#3563) 2025-11-07 06:00:22 -08:00
ollama_template_mappers.py Formatting & bug fixes (#3563) 2025-11-07 06:00:22 -08:00
save.py Formatting & bug fixes (#3563) 2025-11-07 06:00:22 -08:00
tokenizer_utils.py Formatting & bug fixes (#3563) 2025-11-07 06:00:22 -08:00
trainer.py Formatting & bug fixes (#3563) 2025-11-07 06:00:22 -08:00