forked from pytorch/torchtune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_instruct.py
155 lines (131 loc) · 5.94 KB
/
_instruct.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
from typing import Any, Callable, Dict, Optional, Union
from torchtune.data import InputOutputToMessages
from torchtune.datasets._packed import PackedDataset
from torchtune.datasets._sft import SFTDataset
from torchtune.modules.tokenizers import ModelTokenizer
def instruct_dataset(
tokenizer: ModelTokenizer,
*,
source: str,
column_map: Optional[Dict[str, str]] = None,
train_on_input: bool = False,
new_system_prompt: Optional[str] = None,
packed: bool = False,
filter_fn: Optional[Callable] = None,
split: str = "train",
**load_dataset_kwargs: Dict[str, Any],
) -> Union[SFTDataset, PackedDataset]:
"""
Configure a custom dataset with user instruction prompts and model responses.
This builder function can be used to configure a custom instruct dataset directly from the yaml config
as an alternative to :class:`~torchtune.datasets.SFTDataset`, as it is made to be config friendly.
The dataset should follow this format:
.. code-block:: text
| input | output |
|-----------------|------------------|
| "user prompt" | "model response" |
If your column names are different, you can use the ``column_map`` parameter to change
the expected column names. For example, if your dataset has columns ``"question"`` and
``"answer"`` you can use::
column_map = {"input": "question", "output": "answer"}
Masking of the prompt during training is controlled by the ``train_on_input`` flag, which is
set to ``False`` by default
- If ``train_on_input`` is True, the prompt is used during training and
contributes to the loss.
- If ``train_on_input`` is False, the prompt is masked out (tokens replaced with -100)
Args:
tokenizer (ModelTokenizer): Tokenizer used by the model that implements the ``tokenize_messages`` method.
source (str): path to dataset repository on Hugging Face. For local datasets,
define source as the data file type (e.g. "json", "csv", "text"), pass
in the filepath in ``data_files``, and set ``split="train"``. See `Hugging Face's
<https://huggingface.co/docs/datasets/en/package_reference/loading_methods#datasets.load_dataset.path>`_
``load_dataset`` for more details.
column_map (Optional[Dict[str, str]]): a mapping to change the expected "input"
and "output" column names to the actual column names in the dataset. Keys should be "input" and
"output" and values should be the actual column names. Default is None, keeping the default "input"
and "output" column names.
train_on_input (bool): Whether the model is trained on the user prompt or not.
Default is False.
new_system_prompt (Optional[str]): if specified, prepend a system message. This can
serve as instructions to guide the model response. Default is None.
packed (bool): Whether or not to pack the dataset to tokenizer's ``max_seq_len`` prior to training. Default is False.
filter_fn (Optional[Callable]): callable used to filter the dataset prior to any pre-processing. See
the Hugging Face `docs <https://huggingface.co/docs/datasets/v2.20.0/process#select-and-filter>`_ for more
details.
split (str): ``split`` argument for ``datasets.load_dataset``. You can use this argument to load a subset
of a given split, e.g. ``split="train[:10%]"``. Default is "train".
**load_dataset_kwargs (Dict[str, Any]): additional keyword arguments to pass to ``load_dataset``,
such as ``data_files`` or ``split``.
Examples:
::
my_dataset.json
[
{
"question": "What time is it in London?",
"answer": "It is 10:00 AM in London.",
},
{
...
},
...,
]
::
>>> from torchtune.datasets import instruct_dataset
>>> dataset = instruct_dataset(
... tokenizer=tokenizer,
... source="json",
... data_files="my_dataset.json",
... column_map={
... "input": "question",
... "output": "answer",
... },
... train_on_input=False,
... packed=False,
... split="train",
... )
>>> tokens = dataset[0]["tokens"]
>>> tokenizer.decode(tokens)
"What time is it in London?It is 10:00 AM in London."
This can also be accomplished via the yaml config:
.. code-block:: yaml
dataset:
_component_: torchtune.datasets.instruct_dataset
source: json
data_files: my_dataset.json
column_map:
input: question
output: answer
train_on_input: False
packed: False
split: train
Returns:
Union[SFTDataset, PackedDataset]: the configured :class:`~torchtune.datasets.SFTDataset`
or :class:`~torchtune.datasets.PackedDataset` if ``packed=True``
Raises:
ValueError: If ``packed=True`` and ``tokenizer.max_seq_len`` is not set.
"""
message_transform = InputOutputToMessages(
train_on_input=train_on_input,
column_map=column_map,
new_system_prompt=new_system_prompt,
)
ds = SFTDataset(
source=source,
message_transform=message_transform,
model_transform=tokenizer,
filter_fn=filter_fn,
split=split,
**load_dataset_kwargs,
)
if packed:
if tokenizer.max_seq_len is None:
raise ValueError(
"PackedDataset requires a max_seq_len to be set on the tokenizer."
)
return PackedDataset(ds, max_seq_len=tokenizer.max_seq_len)
return ds