目录

首选项数据集

首选项数据集用于奖励建模,其中下游任务是微调基本模型 来捕捉一些潜在的人类偏好。目前,这些数据集在 torchtune 中使用,其中 直接首选项优化 (DPO) 配方

偏好数据集中的 ground-truth 通常是同一提示的两个完成之间的二进制比较的结果, 以及人类注释者根据某些预设标准表示一个完成比另一个更可取。 这些提示-完成对可以是 instruct 风格(单轮次,可选单个提示)、聊天风格(多轮次)或 用户和模型之间的其他一些交互集(例如自由格式文本完成)。

使用 DPO 配方在 torchtune 中使用首选项数据集进行微调的主要入口点是

本地首选项数据集示例

# my_preference_dataset.json
[
    {
        "chosen_conversations": [
            {
                "content": "What do I do when I have a hole in my trousers?",
                "role": "user"
            },
            { "content": "Fix the hole.", "role": "assistant" }
        ],
        "rejected_conversations": [
            {
                "content": "What do I do when I have a hole in my trousers?",
                "role": "user"
            },
            { "content": "Take them off.", "role": "assistant" }
        ]
    }
]
from torchtune.models.mistral import mistral_tokenizer
from torchtune.datasets import preference_dataset

 m_tokenizer = mistral_tokenizer(
     path="/tmp/Mistral-7B-v0.1/tokenizer.model",
     prompt_template="torchtune.models.mistral.MistralChatTemplate",
     max_seq_len=8192,
 )
column_map = {
    "chosen": "chosen_conversations",
    "rejected": "rejected_conversations"
}
ds = preference_dataset(
    tokenizer=tokenizer,
    source="json",
    column_map=column_map,
    data_files="my_preference_dataset.json",
    train_on_input=False,
    split="train",
)
tokenized_dict = ds[0]
print(m_tokenizer.decode(tokenized_dict["rejected_input_ids"]))
# user\n\nWhat do I do when I have a hole in my trousers?assistant\n\nTake them off.
print(tokenized_dict["rejected_labels"])
# [-100,-100,-100,-100,-100,-100,-100,-100,-100,-100,-100,-100, -100,-100,\
# -100,-100,-100,-100,-100,128006,78191,128007,271,18293,1124,1022,13,128009,-100]

这也可以通过 yaml 配置来完成:

# In config
tokenizer:
  _component_: torchtune.models.mistral.mistral_tokenizer
  path: /tmp/Mistral-7B-v0.1/tokenizer.model
  prompt_template: torchtune.models.mistral.MistralChatTemplate
  max_seq_len: 8192

dataset:
  _component_: torchtune.datasets.preference_dataset
  source: json
  data_files: my_preference_dataset.json
  column_map:
    chosen: chosen_conversations
    rejected: rejected_conversations
  train_on_input: False
  split: train

在此示例中,我们还展示了当 “chosen” 和/或 “rejected” 列名称与数据集中的相应列不同时,如何使用 column_map

首选项数据集格式

首选项数据集应有两列:“chosen”,表示人类注释者的首选响应,以及“rejected”,表示 人类注释者不喜欢的响应。这些列中的每一个都应该包含具有相同提示的消息列表。 消息列表可能包括系统提示符、说明、用户和助手之间的多次轮次或工具调用/返回。让我们来看看 Hugging Face 上的 Anthropic 的有用性/无害性数据集,作为多轮的示例 chat 样式格式:

| chosen                                | rejected                              |
|---------------------------------------|---------------------------------------|
|[{                                     |[{                                     |
| "role": "user",                       | "role": "user",                       |
| "content": "helping my granny with her| "content": "helping my granny with her|
| mobile phone issue"                   | mobile phone issue"                   |
| },                                    | },                                    |
| {                                     | {                                     |
| "role": "assistant",                  | "role": "assistant",                  |
| "content": "I see you are chatting    | "content": "Well, the best choice here|
| with your grandmother about an issue  | could be helping with so-called 'self-|
| with her mobile phone. How can I      | management behaviors'. These are      |
| help?"                                | things your grandma can do on her own |
| },                                    | to help her feel more in control."    |
| {                                     | }]                                    |
| "role": "user",                       |                                       |
| "content": "her phone is not turning  |                                       |
| on"                                   |                                       |
| },                                    |                                       |
| {...},                                |                                       |
|]                                      |                                       |

目前,仅支持 JSON 格式的对话,如上例所示。 您可以通过 torchtune 中开箱即用地使用此数据集

从 Hugging Face 加载首选项数据集

要从 Hugging Face 加载首选项数据集,您需要将数据集存储库名称传递给 。对于大多数 HF 数据集,您还需要指定 .sourcesplit

from torchtune.models.gemma import gemma_tokenizer
from torchtune.datasets import preference_dataset

g_tokenizer = gemma_tokenizer("/tmp/gemma-7b/tokenizer.model")
ds = chat_dataset(
    tokenizer=g_tokenizer,
    source="hendrydong/preference_700K",
    split="train",
)
# Tokenizer is passed into the dataset in the recipe so we don't need it here
dataset:
  _component_: torchtune.datasets.preference_dataset
  source: hendrydong/preference_700K
  split: train

内置首选项数据集

文档

访问 PyTorch 的全面开发人员文档

查看文档

教程

获取面向初学者和高级开发人员的深入教程

查看教程

资源

查找开发资源并解答您的问题

查看资源