yuccaaa commited on
Commit
b31a2dc
·
verified ·
1 Parent(s): 6804d1c

Upload ms-swift/docs/source_en/Customization/Custom-dataset.md with huggingface_hub

Browse files
ms-swift/docs/source_en/Customization/Custom-dataset.md ADDED
@@ -0,0 +1,245 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Custom Dataset
2
+
3
+ There are three methods for accessing custom datasets, each offering progressively greater control over preprocessing functions but also increasing in complexity. For example, Solution 1 is the most convenient but offers the least control over preprocessing functions, requiring prior conversion of the dataset into a specific format:
4
+
5
+ 1. **Recommended**: Directly use the command line parameter to access the dataset with `--dataset <dataset_path1> <dataset_path2>`. This will use `AutoPreprocessor` to convert your dataset into a standard format (supporting four dataset formats; see the introduction to AutoPreprocessor below). You can use `--columns` to transform column names. The supported input formats include csv, json, jsonl, txt, and folders (e.g. git clone open-source datasets). This solution does not require modifying `dataset_info.json` and is suitable for users new to ms-swift. The following two solutions are suitable for developers looking to extend ms-swift.
6
+ 2. Add the dataset to `dataset_info.json`, which you can refer to in the built-in [dataset_info.json](https://github.com/modelscope/ms-swift/blob/main/swift/llm/dataset/data/dataset_info.json) of ms-swift. This solution also uses AutoPreprocessor to convert the dataset to a standard format. `dataset_info.json` is a list of metadata for datasets, and one of the fields ms_dataset_id/hf_dataset_id/dataset_path must be filled. Column name transformation can be done through the `columns` field. Datasets added to `dataset_info.json` or registered ones will automatically generate [supported dataset documentation](https://swift.readthedocs.io/en/latest/Instruction/Supported-models-and-datasets.html) when running [run_dataset_info.py](https://github.com/modelscope/ms-swift/blob/main/scripts/utils/run_dataset_info.py). In addition, you can use the external `dataset_info.json` approach by parsing the JSON file with `--custom_dataset_info xxx.json` (to facilitate users who prefer `pip install` over `git clone`), and then specify `--dataset <dataset_id/dataset_dir/dataset_path>`.
7
+ 3. Manually register the dataset to have the most flexible customization capability for preprocessing functions, allowing the use of functions to preprocess datasets, but it is more difficult. You can refer to the [built-in datasets](https://github.com/modelscope/ms-swift/blob/main/swift/llm/dataset/dataset/llm.py) or [examples](https://github.com/modelscope/swift/blob/main/examples/custom). You can specify `--custom_register_path xxx.py` to parse external registration content (convenient for users who use pip install instead of git clone).
8
+ - Solutions one and two leverage solution three under the hood, where the registration process occurs automatically.
9
+
10
+ The following is an introduction to the dataset formats that `AutoPreprocessor` can handle:
11
+
12
+ The standard dataset format for ms-swift accepts keys such as: 'messages', 'rejected_response', 'label', 'images', 'videos', 'audios', 'tools', and 'objects'. Among these, 'messages' is a required key. 'rejected_response' is used for DPO and other RLHF training, 'label' is used for KTO training and classification model training. The keys 'images', 'videos', and 'audios' are used to store paths or URLs for multimodal data, 'tools' is used for Agent tasks, and 'objects' is used for grounding tasks.
13
+
14
+ There are three core preprocessors in ms-swift: `MessagesPreprocessor`, `AlpacaPreprocessor`, and `ResponsePreprocessor`. `MessagesPreprocessor` is used to convert datasets in the messages and sharegpt format into the standard format. `AlpacaPreprocessor` converts datasets in the alpaca format, while `ResponsePreprocessor` converts datasets in the query/response format. `AutoPreprocessor` automatically selects the appropriate preprocessor for the task.
15
+
16
+ The following four formats will all be converted into the `messages` field of the ms-swift standard format under the processing of `AutoPreprocessor`, meaning they can all be directly used with `--dataset <dataset-path>`:
17
+
18
+ Messages format (standard format):
19
+ ```jsonl
20
+ {"messages": [{"role": "system", "content": "<system>"}, {"role": "user", "content": "<query1>"}, {"role": "assistant", "content": "<response1>"}, {"role": "user", "content": "<query2>"}, {"role": "assistant", "content": "<response2>"}]}
21
+ ```
22
+ - Note: The system part is optional. The system in the dataset has a higher priority than the `--system` passed through the command line, followed by the `default_system` defined in the template.
23
+
24
+ ShareGPT format:
25
+ ```jsonl
26
+ {"system": "<system>", "conversation": [{"human": "<query1>", "assistant": "<response1>"}, {"human": "<query2>", "assistant": "<response2>"}]}
27
+ ```
28
+
29
+ Alpaca format:
30
+ ```jsonl
31
+ {"system": "<system>", "instruction": "<query-inst>", "input": "<query-input>", "output": "<response>"}
32
+ ```
33
+
34
+ Query-Response format:
35
+ ```jsonl
36
+ {"system": "<system>", "query": "<query2>", "response": "<response2>", "history": [["<query1>", "<response1>"]]}
37
+ ```
38
+
39
+ ## Standard Dataset Format
40
+
41
+ The following outlines the standard dataset format for ms-swift, where the "system" field is optional and uses the "default_system" defined in the template by default. The four dataset formats introduced earlier can also be processed by AutoPreprocessor into the standard dataset format.
42
+
43
+ ### Pre-training
44
+
45
+ ```jsonl
46
+ {"messages": [{"role": "assistant", "content": "I love music"}]}
47
+ {"messages": [{"role": "assistant", "content": "Coach, I want to play basketball"}]}
48
+ {"messages": [{"role": "assistant", "content": "Which is more authoritative, tomato and egg rice or the third fresh stir-fry?"}]}
49
+ ```
50
+
51
+ ### Supervised Fine-tuning
52
+
53
+ ```jsonl
54
+ {"messages": [{"role": "system", "content": "You are a useful and harmless assistant"}, {"role": "user", "content": "Tell me tomorrow's weather"}, {"role": "assistant", "content": "Tomorrow's weather will be sunny"}]}
55
+ {"messages": [{"role": "system", "content": "You are a useful and harmless math calculator"}, {"role": "user", "content": "What is 1 + 1?"}, {"role": "assistant", "content": "It equals 2"}, {"role": "user", "content": "What about adding 1?"}, {"role": "assistant", "content": "It equals 3"}]}
56
+ ```
57
+
58
+ ### RLHF
59
+
60
+ #### DPO/ORPO/CPO/SimPO/RM
61
+
62
+ ```jsonl
63
+ {"messages": [{"role": "system", "content": "You are a useful and harmless assistant"}, {"role": "user", "content": "Tell me tomorrow's weather"}, {"role": "assistant", "content": "Tomorrow's weather will be sunny"}], "rejected_response": "I don't know"}
64
+ {"messages": [{"role": "system", "content": "You are a useful and harmless math calculator"}, {"role": "user", "content": "What is 1 + 1?"}, {"role": "assistant", "content": "It equals 2"}, {"role": "user", "content": "What about adding 1?"}, {"role": "assistant", "content": "It equals 3"}], "rejected_response": "I don't know"}
65
+ ```
66
+
67
+ #### KTO
68
+
69
+ ```jsonl
70
+ {"messages": [{"role": "system", "content": "You are a useful and harmless assistant"}, {"role": "user", "content": "Tell me tomorrow's weather"}, {"role": "assistant", "content": "I don't know"}], "label": false}
71
+ {"messages": [{"role": "system", "content": "You are a useful and harmless math calculator"}, {"role": "user", "content": "What is 1 + 1?"}, {"role": "assistant", "content": "It equals 2"}, {"role": "user", "content": "What about adding 1?"}, {"role": "assistant", "content": "It equals 3"}], "label": true}
72
+ ```
73
+
74
+ #### PPO/GRPO
75
+
76
+ ```jsonl
77
+ {"messages": [{"role": "system", "content": "You are a useful and harmless assistant"}, {"role": "user", "content": "Tell me tomorrow's weather"}]}
78
+ {"messages": [{"role": "system", "content": "You are a useful and harmless math calculator"}, {"role": "user", "content": "What is 1 + 1?"}, {"role": "assistant", "content": "It equals 2"}, {"role": "user", "content": "What about adding 1?"}]}
79
+ {"messages": [{"role": "user", "content": "What is your name?"}]}
80
+ ```
81
+ - Note: GRPO will pass through all additional field content to the ORM, unlike other training methods that, by default, delete extra fields. For example, you can additionally pass in 'solution'. The custom ORM needs to include a positional argument called `completions`, with other arguments as keyword arguments passed through from the additional dataset fields.
82
+
83
+ ### Sequence Classification
84
+
85
+ **Single-label Task**:
86
+ ```jsonl
87
+ {"messages": [{"role": "user", "content": "The weather is really nice today"}], "label": 1}
88
+ {"messages": [{"role": "user", "content": "Today is really unlucky"}], "label": 0}
89
+ {"messages": [{"role": "user", "content": "So happy"}], "label": 1}
90
+ ```
91
+
92
+ **Multi-label Task**:
93
+
94
+ ```jsonl
95
+ {"messages": [{"role": "user", "content": "<sentence>"}], "label": [1, 3, 5]}
96
+ ```
97
+
98
+ **Single Regression Task**:
99
+
100
+ ```jsonl
101
+ {"messages": [{"role": "user", "content": "Calculate the similarity between two sentences, with a range of 0-1.\nsentence1: <sentence1>\nsentence2: <sentence2>"}], "label": 0.8}
102
+ ```
103
+
104
+ **Multi Regression Task**:
105
+
106
+ ```jsonl
107
+ {"messages": [{"role": "user", "content": "<sentence>"}], "label": [1.2, -0.6, 0.8]}
108
+ ```
109
+
110
+ ### Embedding
111
+
112
+ Please refer to [embedding训练文档](../BestPractices/Embedding.md#dataset-format).
113
+
114
+ ### Multimodal
115
+
116
+ For multimodal datasets, the format is the same as the aforementioned tasks. The difference lies in the addition of several keys: `images`, `videos`, and `audios`, which represent the URLs or paths (preferably absolute paths) of multimodal resources. The tags `<image>`, `<video>`, and `<audio>` indicate where to insert images, videos, or audio. MS-Swift supports multiple images, videos, and audio files. These special tokens will be replaced during preprocessing, as referenced [here](https://github.com/modelscope/ms-swift/blob/main/swift/llm/template/template/qwen.py#L198). The four examples below respectively demonstrate the data format for plain text, as well as formats containing image, video, and audio data.
117
+
118
+
119
+ Pre-training:
120
+ ```jsonl
121
+ {"messages": [{"role": "assistant", "content": "Pre-trained text goes here"}]}
122
+ {"messages": [{"role": "assistant", "content": "<image>is a puppy, <image>is a kitten"}], "images": ["/xxx/x.jpg", "/xxx/x.png"]}
123
+ {"messages": [{"role": "assistant", "content": "<audio>describes how nice the weather is today"}], "audios": ["/xxx/x.wav"]}
124
+ {"messages": [{"role": "assistant", "content": "<image>is an elephant, <video>is a lion running"}], "images": ["/xxx/x.jpg"], "videos": ["/xxx/x.mp4"]}
125
+ ```
126
+
127
+ Supervised Fine-tuning:
128
+
129
+ ```jsonl
130
+ {"messages": [{"role": "user", "content": "Where is the capital of Zhejiang?"}, {"role": "assistant", "content": "The capital of Zhejiang is Hangzhou."}]}
131
+ {"messages": [{"role": "user", "content": "<image><image>What is the difference between the two images?"}, {"role": "assistant", "content": "The first one is a kitten, and the second one is a puppy."}], "images": ["/xxx/x.jpg", "/xxx/x.png"]}
132
+ {"messages": [{"role": "user", "content": "<audio>What did the audio say?"}, {"role": "assistant", "content": "The weather is really nice today."}], "audios": ["/xxx/x.mp3"]}
133
+ {"messages": [{"role": "system", "content": "You are a helpful and harmless assistant."}, {"role": "user", "content": "<image>What is in the image, <video>What is in the video?"}, {"role": "assistant", "content": "The image shows an elephant, and the video shows a puppy running on the grass."}], "images": ["/xxx/x.jpg"], "videos": ["/xxx/x.mp4"]}
134
+ ```
135
+
136
+ The data format for RLHF and sequence classification of multimodal models can reference the format of pure text large models, with additional fields such as `images` added on top of that.
137
+
138
+ #### Grounding
139
+
140
+ For grounding (object detection) tasks, SWIFT supports two methods:
141
+
142
+ 1. Directly use the data format of the grounding task corresponding to the model. For example, the format for qwen2-vl is as follows:
143
+
144
+ ```
145
+ {"messages": [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "<image>Describe the image."}, {"role": "assistant", "content": "<|object_ref_start|>a dog<|object_ref_end|><|box_start|>(221,423),(569,886)<|box_end|> and <|object_ref_start|>a woman<|object_ref_end|><|box_start|>(451,381),(733,793)<|box_end|> are playing on the beach"}], "images": ["/xxx/x.jpg"]}
146
+ {"messages": [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "<image>Find the <|object_ref_start|>sheep<|object_ref_end|> in the image"}, {"role": "assistant", "content": "<|box_start|>(101,201),(150,266)<|box_end|><|box_start|>(401,601),(550,666)<|box_end|>"}], "images": ["/xxx/x.jpg"]}
147
+ {"messages": [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "<image>Help me open Google Chrome"}, {"role": "assistant", "content": "Action: click(start_box='<|box_start|>(246,113)<|box_end|>')"}], "images": ["/xxx/x.jpg"]}
148
+ ```
149
+
150
+ When using this type of data, please note:
151
+
152
+ - Different models have different special characters and data format for the grounding task.
153
+ - The handling of bounding box normalization varies across different models: for example, qwen2.5-vl uses absolute coordinates, while qwen2-vl and internvl2.5 require bounding box coordinates to be normalized to the thousandth scale.
154
+
155
+ 1. Use SWIFT's grounding data format:
156
+
157
+ ```
158
+ {"messages": [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "<image>Describe the image."}, {"role": "assistant", "content": "<ref-object><bbox> and <ref-object><bbox> are playing on the beach"}], "images": ["/xxx/x.jpg"], "objects": {"ref": ["a dog", "a woman"], "bbox": [[331.5, 761.4, 853.5, 1594.8], [676.5, 685.8, 1099.5, 1427.4]]}}
159
+ {"messages": [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "<image>Find the <ref-object> in the image"}, {"role": "assistant", "content": "<bbox><bbox>"}], "images": ["/xxx/x.jpg"], "objects": {"ref": ["sheep"], "bbox": [[90.9, 160.8, 135, 212.8], [360.9, 480.8, 495, 532.8]]}}
160
+ {"messages": [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "<image>Help me open Google Chrome"}, {"role": "assistant", "content": "Action: click(start_box='<bbox>')"}], "images": ["/xxx/x.jpg"], "objects": {"ref": [], "bbox": [[615, 226]]}}
161
+ ```
162
+
163
+ The format will automatically convert the dataset format to the corresponding model's grounding task format and select the appropriate model's bbox normalization method. Compared to the general format, this format includes an additional "objects" field, which contains the following subfields:
164
+
165
+ - ref: Used to replace `<ref-object>`.
166
+ - bbox: Used to replace `<bbox>`. If the length of each box in the bbox is 2, it represents the x and y coordinates. If the box length is 4, it represents the x and y coordinates of two points.
167
+ - bbox_type: Optional values are 'real' and 'norm1'. The default is 'real', meaning the bbox represents the actual bounding box value. If set to 'norm1', the bbox is normalized to the range 0~1.
168
+ - image_id: This parameter is only effective when bbox_type is 'real'. It indicates the index of the image corresponding to the bbox, used for scaling the bbox. The index starts from 0, and the default is 0 for all.
169
+
170
+ ### Text-to-Image Format
171
+
172
+ ```jsonl
173
+ {"messages": [{"role": "system", "content": "You are a useful and harmless assistant"}, {"role": "user", "content": "Draw me an apple"}, {"role": "assistant", "content": "<image>"}], "images": ["/xxx/x.jpg"]}
174
+ ```
175
+
176
+ ### Agent Format
177
+
178
+ Refer to the [Agent documentation](../Instruction/Agent-support.md) for the Agent format.
179
+
180
+
181
+ ## dataset_info.json
182
+
183
+ You can refer to the ms-swift built-in [dataset_info.json](https://github.com/modelscope/ms-swift/blob/main/swift/llm/dataset/data/dataset_info.json). This approach uses the AutoPreprocessor function to convert the dataset into a standard format. The dataset_info.json file contains a list of metadata about the dataset. Here are some examples:
184
+
185
+
186
+ ```json
187
+ [
188
+ {
189
+ "ms_dataset_id": "xxx/xxx"
190
+ },
191
+ {
192
+ "dataset_path": "<dataset_dir/dataset_path>"
193
+ },
194
+ {
195
+ "ms_dataset_id": "<dataset_id>",
196
+ "subsets": ["v1"],
197
+ "split": ["train", "validation"],
198
+ "columns": {
199
+ "input": "query",
200
+ "output": "response"
201
+ }
202
+ },
203
+ {
204
+ "ms_dataset_id": "<dataset_id>",
205
+ "hf_dataset_id": "<hf_dataset_id>",
206
+ "subsets": [{
207
+ "subset": "subset1",
208
+ "columns": {
209
+ "problem": "query",
210
+ "content": "response"
211
+ }
212
+ },
213
+ {
214
+ "subset": "subset2",
215
+ "columns": {
216
+ "messages": "_",
217
+ "new_messages": "messages"
218
+ }
219
+ }]
220
+ }
221
+ ]
222
+ ```
223
+
224
+ The following parameters are supported:
225
+
226
+ - ms_dataset_id: Refers to the DatasetMeta parameter.
227
+ - hf_dataset_id: Refers to the DatasetMeta parameter.
228
+ - dataset_path: Refers to the DatasetMeta parameter.
229
+ - dataset_name: Refers to the DatasetMeta parameter.
230
+ - subsets: Refers to the DatasetMeta parameter.
231
+ - split: Refers to the DatasetMeta parameter.
232
+ - columns: Transforms column names before preprocessing the dataset.
233
+
234
+ ## Dataset Registration
235
+
236
+ `register_dataset` will register the dataset in `DATASET_MAPPING`. You can call the function `register_dataset(dataset_meta)` to complete the dataset registration, where `dataset_meta` will store the metadata of the model. The parameter list for DatasetMeta is as follows:
237
+
238
+ - ms_dataset_id: The dataset_id for ModelScope, default is None.
239
+ - hf_dataset_id: The dataset_id for HuggingFace, default is None.
240
+ - dataset_path: The local path to the dataset (an absolute path is recommended), default is None.
241
+ - dataset_name: The alias of the dataset, which can be specified via `--dataset <dataset_name>`. This is very convenient when the dataset_path is long. The default value is None.
242
+ - subsets: A list of subdataset names or a list of `SubsetDataset` objects, default is `['default']`. (The concepts of subdatasets and splits only exist for dataset_id or dataset_dir (open source datasets cloned via git)).
243
+ - split: Defaults to `['train']`.
244
+ - preprocess_func: A preprocessing function or callable object, default is `AutoPreprocessor()`. This preprocessing function takes an `HfDataset` as input and returns an `HfDataset` in the standard format.
245
+ - load_function: Defaults to `DatasetLoader.load`. If a custom loading function is needed, it should return an `HfDataset` in the standard format, allowing users maximum flexibility while bypassing the ms-swift dataset loading mechanism. This parameter usually does not need to be modified.