yuccaaa commited on
Commit
ad45b24
·
verified ·
1 Parent(s): 66c747f

Upload ms-swift/examples/notebook/qwen2_5-self-cognition/self-cognition-sft.ipynb with huggingface_hub

Browse files
ms-swift/examples/notebook/qwen2_5-self-cognition/self-cognition-sft.ipynb ADDED
@@ -0,0 +1,219 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "## 10-minute self-cognition SFT\n",
8
+ "\n",
9
+ "Here is a demonstration of using python to perform self-cognition SFT of Qwen2.5-3B-Instruct. Through this tutorial, you can quickly understand some details of swift sft, which will be of great help in customizing ms-swift for you~\n",
10
+ "\n",
11
+ "Are you ready? Let's begin the journey...\n",
12
+ "\n",
13
+ "中文版:[魔搭教程](https://github.com/modelscope/modelscope-classroom/blob/main/LLM-tutorial/R.10%E5%88%86%E9%92%9F%E6%94%B9%E5%8F%98%E5%A4%A7%E6%A8%A1%E5%9E%8B%E8%87%AA%E6%88%91%E8%AE%A4%E7%9F%A5.ipynb)"
14
+ ]
15
+ },
16
+ {
17
+ "cell_type": "code",
18
+ "execution_count": 1,
19
+ "metadata": {
20
+ "vscode": {
21
+ "languageId": "shellscript"
22
+ }
23
+ },
24
+ "outputs": [],
25
+ "source": [
26
+ "# # install ms-swift\n",
27
+ "# pip install ms-swift -U"
28
+ ]
29
+ },
30
+ {
31
+ "cell_type": "code",
32
+ "execution_count": 2,
33
+ "metadata": {},
34
+ "outputs": [],
35
+ "source": [
36
+ "# import some libraries\n",
37
+ "import os\n",
38
+ "os.environ['CUDA_VISIBLE_DEVICES'] = '0'\n",
39
+ "\n",
40
+ "from swift.llm import get_model_tokenizer, load_dataset, get_template, EncodePreprocessor\n",
41
+ "from swift.utils import get_logger, find_all_linears, get_model_parameter_info, plot_images, seed_everything\n",
42
+ "from swift.tuners import Swift, LoraConfig\n",
43
+ "from swift.trainers import Seq2SeqTrainer, Seq2SeqTrainingArguments\n",
44
+ "from functools import partial\n",
45
+ "\n",
46
+ "logger = get_logger()\n",
47
+ "seed_everything(42)"
48
+ ]
49
+ },
50
+ {
51
+ "cell_type": "code",
52
+ "execution_count": 3,
53
+ "metadata": {},
54
+ "outputs": [],
55
+ "source": [
56
+ "# Hyperparameters for training\n",
57
+ "# model\n",
58
+ "model_id_or_path = 'Qwen/Qwen2.5-3B-Instruct' # model_id or model_path\n",
59
+ "system = 'You are a helpful assistant.'\n",
60
+ "output_dir = 'output'\n",
61
+ "\n",
62
+ "# dataset\n",
63
+ "dataset = ['AI-ModelScope/alpaca-gpt4-data-zh#500', 'AI-ModelScope/alpaca-gpt4-data-en#500',\n",
64
+ " 'swift/self-cognition#500'] # dataset_id or dataset_path\n",
65
+ "data_seed = 42\n",
66
+ "max_length = 2048\n",
67
+ "split_dataset_ratio = 0.01 # Split validation set\n",
68
+ "num_proc = 4 # The number of processes for data loading.\n",
69
+ "# The following two parameters are used to override the placeholders in the self-cognition dataset.\n",
70
+ "model_name = ['小黄', 'Xiao Huang'] # The Chinese name and English name of the model\n",
71
+ "model_author = ['魔搭', 'ModelScope'] # The Chinese name and English name of the model author\n",
72
+ "\n",
73
+ "# lora\n",
74
+ "lora_rank = 8\n",
75
+ "lora_alpha = 32\n",
76
+ "\n",
77
+ "# training_args\n",
78
+ "training_args = Seq2SeqTrainingArguments(\n",
79
+ " output_dir=output_dir,\n",
80
+ " learning_rate=1e-4,\n",
81
+ " per_device_train_batch_size=1,\n",
82
+ " per_device_eval_batch_size=1,\n",
83
+ " gradient_checkpointing=True,\n",
84
+ " weight_decay=0.1,\n",
85
+ " lr_scheduler_type='cosine',\n",
86
+ " warmup_ratio=0.05,\n",
87
+ " report_to=['tensorboard'],\n",
88
+ " logging_first_step=True,\n",
89
+ " save_strategy='steps',\n",
90
+ " save_steps=50,\n",
91
+ " eval_strategy='steps',\n",
92
+ " eval_steps=50,\n",
93
+ " gradient_accumulation_steps=16,\n",
94
+ " num_train_epochs=1,\n",
95
+ " metric_for_best_model='loss',\n",
96
+ " save_total_limit=2,\n",
97
+ " logging_steps=5,\n",
98
+ " dataloader_num_workers=1,\n",
99
+ " data_seed=data_seed,\n",
100
+ ")\n",
101
+ "\n",
102
+ "output_dir = os.path.abspath(os.path.expanduser(output_dir))\n",
103
+ "logger.info(f'output_dir: {output_dir}')"
104
+ ]
105
+ },
106
+ {
107
+ "cell_type": "code",
108
+ "execution_count": 4,
109
+ "metadata": {},
110
+ "outputs": [],
111
+ "source": [
112
+ "# Obtain the model and template, and add a trainable Lora layer on the model.\n",
113
+ "model, tokenizer = get_model_tokenizer(model_id_or_path)\n",
114
+ "logger.info(f'model_info: {model.model_info}')\n",
115
+ "template = get_template(model.model_meta.template, tokenizer, default_system=system, max_length=max_length)\n",
116
+ "template.set_mode('train')\n",
117
+ "\n",
118
+ "target_modules = find_all_linears(model)\n",
119
+ "lora_config = LoraConfig(task_type='CAUSAL_LM', r=lora_rank, lora_alpha=lora_alpha,\n",
120
+ " target_modules=target_modules)\n",
121
+ "model = Swift.prepare_model(model, lora_config)\n",
122
+ "logger.info(f'lora_config: {lora_config}')\n",
123
+ "\n",
124
+ "# Print model structure and trainable parameters.\n",
125
+ "logger.info(f'model: {model}')\n",
126
+ "model_parameter_info = get_model_parameter_info(model)\n",
127
+ "logger.info(f'model_parameter_info: {model_parameter_info}')"
128
+ ]
129
+ },
130
+ {
131
+ "cell_type": "code",
132
+ "execution_count": 5,
133
+ "metadata": {},
134
+ "outputs": [],
135
+ "source": [
136
+ "# Download and load the dataset, split it into a training set and a validation set,\n",
137
+ "# and encode the text data into tokens.\n",
138
+ "train_dataset, val_dataset = load_dataset(dataset, split_dataset_ratio=split_dataset_ratio, num_proc=num_proc,\n",
139
+ " model_name=model_name, model_author=model_author, seed=data_seed)\n",
140
+ "\n",
141
+ "logger.info(f'train_dataset: {train_dataset}')\n",
142
+ "logger.info(f'val_dataset: {val_dataset}')\n",
143
+ "logger.info(f'train_dataset[0]: {train_dataset[0]}')\n",
144
+ "\n",
145
+ "train_dataset = EncodePreprocessor(template=template)(train_dataset, num_proc=num_proc)\n",
146
+ "val_dataset = EncodePreprocessor(template=template)(val_dataset, num_proc=num_proc)\n",
147
+ "logger.info(f'encoded_train_dataset[0]: {train_dataset[0]}')\n",
148
+ "\n",
149
+ "# Print a sample\n",
150
+ "template.print_inputs(train_dataset[0])"
151
+ ]
152
+ },
153
+ {
154
+ "cell_type": "code",
155
+ "execution_count": 6,
156
+ "metadata": {},
157
+ "outputs": [],
158
+ "source": [
159
+ "# Get the trainer and start the training.\n",
160
+ "model.enable_input_require_grads() # Compatible with gradient checkpointing\n",
161
+ "trainer = Seq2SeqTrainer(\n",
162
+ " model=model,\n",
163
+ " args=training_args,\n",
164
+ " data_collator=template.data_collator,\n",
165
+ " train_dataset=train_dataset,\n",
166
+ " eval_dataset=val_dataset,\n",
167
+ " template=template,\n",
168
+ ")\n",
169
+ "trainer.train()\n",
170
+ "\n",
171
+ "last_model_checkpoint = trainer.state.last_model_checkpoint\n",
172
+ "logger.info(f'last_model_checkpoint: {last_model_checkpoint}')"
173
+ ]
174
+ },
175
+ {
176
+ "cell_type": "code",
177
+ "execution_count": null,
178
+ "metadata": {},
179
+ "outputs": [],
180
+ "source": [
181
+ "# Visualize the training loss.\n",
182
+ "# You can also use the TensorBoard visualization interface during training by entering\n",
183
+ "# `tensorboard --logdir '{output_dir}/runs'` at the command line.\n",
184
+ "images_dir = os.path.join(output_dir, 'images')\n",
185
+ "logger.info(f'images_dir: {images_dir}')\n",
186
+ "plot_images(images_dir, training_args.logging_dir, ['train/loss'], 0.9) # save images\n",
187
+ "\n",
188
+ "# Read and display the image.\n",
189
+ "# The light yellow line represents the actual loss value,\n",
190
+ "# while the yellow line represents the loss value smoothed with a smoothing factor of 0.9.\n",
191
+ "from IPython.display import display\n",
192
+ "from PIL import Image\n",
193
+ "image = Image.open(os.path.join(images_dir, 'train_loss.png'))\n",
194
+ "display(image)"
195
+ ]
196
+ }
197
+ ],
198
+ "metadata": {
199
+ "kernelspec": {
200
+ "display_name": "py310",
201
+ "language": "python",
202
+ "name": "python3"
203
+ },
204
+ "language_info": {
205
+ "codemirror_mode": {
206
+ "name": "ipython",
207
+ "version": 3
208
+ },
209
+ "file_extension": ".py",
210
+ "mimetype": "text/x-python",
211
+ "name": "python",
212
+ "nbconvert_exporter": "python",
213
+ "pygments_lexer": "ipython3",
214
+ "version": "3.10.16"
215
+ }
216
+ },
217
+ "nbformat": 4,
218
+ "nbformat_minor": 2
219
+ }