> ## Documentation Index
> Fetch the complete documentation index at: https://docs.monostate.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# ORPO 训练

> 优势比偏好优化

# ORPO 训练

ORPO 在单个训练阶段中结合了 SFT 和偏好优化。

## 什么是 ORPO？

ORPO（Odds Ratio Preference Optimization）是 DPO 的更简单替代方案，不需要参考模型。它直接使用优势比优化偏好，减少内存使用和训练复杂度。

## 快速开始

```bash theme={null}
aitraining llm --train \
  --model google/gemma-2-2b \
  --data-path ./preferences.jsonl \
  --project-name gemma-orpo \
  --trainer orpo \
  --prompt-text-column prompt \
  --text-column chosen \
  --rejected-text-column rejected \
  --peft
```

<Warning>
  ORPO 需要 `--prompt-text-column` 和 `--rejected-text-column`。`--text-column` 默认为 `"text"`，因此只有当您的选择列名称不同时才需要指定。
</Warning>

## Python API

```python theme={null}
from autotrain.trainers.clm.params import LLMTrainingParams
from autotrain.project import AutoTrainProject

params = LLMTrainingParams(
    model="google/gemma-2-2b",
    data_path="./preferences.jsonl",
    project_name="gemma-orpo",

    trainer="orpo",
    prompt_text_column="prompt",
    text_column="chosen",
    rejected_text_column="rejected",
    dpo_beta=0.1,  # Default: 0.1
    max_completion_length=None,  # Default: None

    epochs=3,
    batch_size=2,
    lr=5e-5,

    peft=True,
    lora_r=16,
)

project = AutoTrainProject(params=params, backend="local", process=True)
project.create()
```

## 数据格式

与 DPO 相同 - 偏好对：

```json theme={null}
{
  "prompt": "What is AI?",
  "chosen": "AI is artificial intelligence, a field of computer science focused on creating systems that can perform tasks requiring human intelligence.",
  "rejected": "AI is just robots."
}
```

## ORPO vs DPO

| 方面     | ORPO | DPO                  |
| ------ | ---- | -------------------- |
| 参考模型   | 不需要  | 使用 PEFT 时不需要，完整微调时需要 |
| 内存使用   | 更低   | 更高（如果使用参考模型）         |
| 训练速度   | 更快   | 更慢                   |
| SFT 阶段 | 组合   | 分离                   |
| 复杂度    | 更简单  | 更多选项                 |

## 参数

| 参数                      | 描述           | 默认值    |
| ----------------------- | ------------ | ------ |
| `trainer`               | 设置为 `"orpo"` | 必需     |
| `dpo_beta`              | 优势比权重        | `0.1`  |
| `max_completion_length` | 响应最大 token 数 | `None` |
| `image_column`          | VLM 偏好训练的图像列 | `None` |

### VLM（视觉语言）ORPO

ORPO 支持视觉语言模型（如 Qwen 3.5-VL）进行图像+文本偏好对齐。设置 `image_column` 启用 VLM 模式：

```python theme={null}
params = LLMTrainingParams(
    model="Qwen/Qwen3.5-VL-9B",
    trainer="orpo",
    image_column="images",
    text_column="chosen",
    rejected_text_column="rejected",
    prompt_text_column="prompt",
)
```

数据集应包含带消息列表的 `chosen`/`rejected` 列，以及包含图像的图像列。图像列会自动重命名为 `images` 以兼容 TRL。

## 何时使用 ORPO

在以下情况选择 ORPO：

* 内存有限（不需要参考模型）
* 您想要组合 SFT + 对齐
* 偏好更简单的训练流程
* 从基础模型开始（未进行指令微调）

在以下情况选择 DPO：

* 您需要细粒度控制
* 使用已进行指令微调的模型
* 参考模型行为很重要

## 示例：客户支持

```python theme={null}
params = LLMTrainingParams(
    model="google/gemma-2-2b",
    data_path="./support_preferences.jsonl",
    project_name="support-bot",

    trainer="orpo",
    dpo_beta=0.15,

    epochs=3,
    batch_size=2,
    gradient_accumulation=4,
    lr=2e-5,

    peft=True,
    lora_r=32,
    lora_alpha=64,

    log="wandb",
)
```

## 下一步

<CardGroup cols={2}>
  <Card title="DPO Training" href="/advanced/dpo-training">
    替代对齐方法
  </Card>

  <Card title="Reward Modeling" href="/advanced/reward-modeling">
    训练奖励模型
  </Card>
</CardGroup>
