arbinMichael commited on
Commit
a101c0d
·
verified ·
1 Parent(s): e705971

Upload working.ipynb

Browse files
Files changed (1) hide show
  1. working.ipynb +333 -0
working.ipynb ADDED
@@ -0,0 +1,333 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "60299a7f-6e86-4bd6-9dbf-250b42a264b9",
7
+ "metadata": {},
8
+ "outputs": [
9
+ {
10
+ "name": "stdout",
11
+ "output_type": "stream",
12
+ "text": [
13
+ "🦥 Unsloth: Will patch your computer to enable 2x faster free finetuning.\n",
14
+ "==((====))== Unsloth 2024.8: Fast Llama patching. Transformers = 4.44.2.\n",
15
+ " \\\\ /| GPU: NVIDIA GeForce RTX 3090. Max memory: 23.691 GB. Platform = Linux.\n",
16
+ "O^O/ \\_/ \\ Pytorch: 2.3.0. CUDA = 8.6. CUDA Toolkit = 12.1.\n",
17
+ "\\ / Bfloat16 = TRUE. FA [Xformers = 0.0.27. FA2 = False]\n",
18
+ " \"-____-\" Free Apache license: http://github.com/unslothai/unsloth\n"
19
+ ]
20
+ }
21
+ ],
22
+ "source": [
23
+ "from unsloth import FastLanguageModel\n",
24
+ "import torch\n",
25
+ "max_seq_length = 2048 # Choose any! We auto support RoPE Scaling internally!\n",
26
+ "dtype = None # None for auto detection. Float16 for Tesla T4, V100, Bfloat16 for Ampere+\n",
27
+ "load_in_4bit = True # Use 4bit quantization to reduce memory usage. Can be False.\n",
28
+ "\n",
29
+ "model, tokenizer = FastLanguageModel.from_pretrained(\n",
30
+ " model_name = \"unsloth/Phi-3.5-mini-instruct\",\n",
31
+ " max_seq_length = max_seq_length,\n",
32
+ " dtype = dtype,\n",
33
+ " load_in_4bit = load_in_4bit,\n",
34
+ " # token = \"hf_...\", # use one if using gated models like meta-llama/Llama-2-7b-hf\n",
35
+ ")"
36
+ ]
37
+ },
38
+ {
39
+ "cell_type": "code",
40
+ "execution_count": 2,
41
+ "id": "8712c5c8-c763-4743-bc8d-54b879433b73",
42
+ "metadata": {},
43
+ "outputs": [
44
+ {
45
+ "name": "stderr",
46
+ "output_type": "stream",
47
+ "text": [
48
+ "Unsloth 2024.8 patched 32 layers with 32 QKV layers, 32 O layers and 32 MLP layers.\n"
49
+ ]
50
+ }
51
+ ],
52
+ "source": [
53
+ "model = FastLanguageModel.get_peft_model(\n",
54
+ " model,\n",
55
+ " r = 16, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128\n",
56
+ " target_modules = [\"q_proj\", \"k_proj\", \"v_proj\", \"o_proj\",\n",
57
+ " \"gate_proj\", \"up_proj\", \"down_proj\",],\n",
58
+ " lora_alpha = 16,\n",
59
+ " lora_dropout = 0, # Supports any, but = 0 is optimized\n",
60
+ " bias = \"none\", # Supports any, but = \"none\" is optimized\n",
61
+ " # [NEW] \"unsloth\" uses 30% less VRAM, fits 2x larger batch sizes!\n",
62
+ " use_gradient_checkpointing = \"unsloth\", # True or \"unsloth\" for very long context\n",
63
+ " random_state = 3407,\n",
64
+ " use_rslora = False, # We support rank stabilized LoRA\n",
65
+ " loftq_config = None, # And LoftQ\n",
66
+ ")"
67
+ ]
68
+ },
69
+ {
70
+ "cell_type": "code",
71
+ "execution_count": 3,
72
+ "id": "c9d36fef-4c62-412d-81a8-2769a1b56042",
73
+ "metadata": {},
74
+ "outputs": [
75
+ {
76
+ "data": {
77
+ "application/vnd.jupyter.widget-view+json": {
78
+ "model_id": "3df9b30fca4c43f59d13de16a849d74b",
79
+ "version_major": 2,
80
+ "version_minor": 0
81
+ },
82
+ "text/plain": [
83
+ "Downloading data: 0%| | 0.00/14.5k [00:00<?, ?B/s]"
84
+ ]
85
+ },
86
+ "metadata": {},
87
+ "output_type": "display_data"
88
+ },
89
+ {
90
+ "data": {
91
+ "application/vnd.jupyter.widget-view+json": {
92
+ "model_id": "a8721bd6c057407caa6b34f73ffde6af",
93
+ "version_major": 2,
94
+ "version_minor": 0
95
+ },
96
+ "text/plain": [
97
+ "Generating train split: 0%| | 0/10 [00:00<?, ? examples/s]"
98
+ ]
99
+ },
100
+ "metadata": {},
101
+ "output_type": "display_data"
102
+ }
103
+ ],
104
+ "source": [
105
+ "from datasets import load_dataset\n",
106
+ "dataset = load_dataset(\"arbinMichael/testparquet\", split = \"train\")"
107
+ ]
108
+ },
109
+ {
110
+ "cell_type": "code",
111
+ "execution_count": 4,
112
+ "id": "452ad49e-b283-4655-9c99-f30c5eed681c",
113
+ "metadata": {},
114
+ "outputs": [
115
+ {
116
+ "name": "stdout",
117
+ "output_type": "stream",
118
+ "text": [
119
+ "<|user|>Perpare a schedule for current charge/discharge test, the value of the current is a linear variable, using Current Ramp(A) control type. The charging current start value is 0.5A, the rate of change of the current per second is 0.01, up to 4V then ; discharge current start value is -0.5A, the rate of change of the current per second is -0.01, discharging to 1V then end the test. Record one point per second<|end|><|assistant|>[{\"StepCtrlTypeString\":\"Rest\",\"CtrlValue\":\"\",\"Label\":\"Step_A\",\"StepLimits\":[{\"Equations\":\"PV_CHAN_Step_Time>=5\",\"GotoStep\":\"Next Step\"}],\"LogLimits\":[{\"Equations\":\"DV_Time>=1\",\"GotoStep\":\"Next Step\"}]},{\"StepCtrlTypeString\":\"Current Ramp(A)\",\"CtrlValue\":\"0.5\",\"Label\":\"Step_B\",\"StepLimits\":[{\"Equations\":\"PV_CHAN_Voltage>=4\",\"GotoStep\":\"Next Step\"}],\"LogLimits\":[{\"Equations\":\"DV_Time>=1\",\"GotoStep\":\"Next Step\"}]},{\"StepCtrlTypeString\":\"Current Ramp(A)\",\"CtrlValue\":\"-0.5\",\"Label\":\"Step_C\",\"StepLimits\":[{\"Equations\":\"PV_CHAN_Voltage<=1\",\"GotoStep\":\"Next Step\"}],\"LogLimits\":[{\"Equations\":\"DV_Time>=1\",\"GotoStep\":\"Next Step\"}]}]<|end|>\n"
120
+ ]
121
+ }
122
+ ],
123
+ "source": [
124
+ "print(dataset[5][\"text\"])"
125
+ ]
126
+ },
127
+ {
128
+ "cell_type": "code",
129
+ "execution_count": 5,
130
+ "id": "b0a39d9e-e3bf-4fae-8d75-dba12ccf15c8",
131
+ "metadata": {},
132
+ "outputs": [
133
+ {
134
+ "data": {
135
+ "application/vnd.jupyter.widget-view+json": {
136
+ "model_id": "da8408ab10ec44358873ee0f1c234abd",
137
+ "version_major": 2,
138
+ "version_minor": 0
139
+ },
140
+ "text/plain": [
141
+ "Map (num_proc=2): 0%| | 0/10 [00:00<?, ? examples/s]"
142
+ ]
143
+ },
144
+ "metadata": {},
145
+ "output_type": "display_data"
146
+ },
147
+ {
148
+ "name": "stderr",
149
+ "output_type": "stream",
150
+ "text": [
151
+ "max_steps is given, it will override any value given in num_train_epochs\n"
152
+ ]
153
+ }
154
+ ],
155
+ "source": [
156
+ "from trl import SFTTrainer\n",
157
+ "from transformers import TrainingArguments\n",
158
+ "from unsloth import is_bfloat16_supported\n",
159
+ "\n",
160
+ "trainer = SFTTrainer(\n",
161
+ " model = model,\n",
162
+ " tokenizer = tokenizer,\n",
163
+ " train_dataset = dataset,\n",
164
+ " dataset_text_field = \"text\",\n",
165
+ " max_seq_length = max_seq_length,\n",
166
+ " dataset_num_proc = 2,\n",
167
+ " packing = False, # Can make training 5x faster for short sequences.\n",
168
+ " args = TrainingArguments(\n",
169
+ " per_device_train_batch_size = 2,\n",
170
+ " gradient_accumulation_steps = 4,\n",
171
+ " warmup_steps = 5,\n",
172
+ " max_steps = 60,\n",
173
+ " learning_rate = 2e-4,\n",
174
+ " fp16 = not is_bfloat16_supported(),\n",
175
+ " bf16 = is_bfloat16_supported(),\n",
176
+ " logging_steps = 1,\n",
177
+ " optim = \"adamw_8bit\",\n",
178
+ " weight_decay = 0.01,\n",
179
+ " lr_scheduler_type = \"linear\",\n",
180
+ " seed = 7444,\n",
181
+ " output_dir = \"outputs\",\n",
182
+ " ),\n",
183
+ ")"
184
+ ]
185
+ },
186
+ {
187
+ "cell_type": "code",
188
+ "execution_count": null,
189
+ "id": "625e8b31-82d8-4930-a46e-a82803b4f211",
190
+ "metadata": {},
191
+ "outputs": [
192
+ {
193
+ "name": "stderr",
194
+ "output_type": "stream",
195
+ "text": [
196
+ "==((====))== Unsloth - 2x faster free finetuning | Num GPUs = 1\n",
197
+ " \\\\ /| Num examples = 10 | Num Epochs = 60\n",
198
+ "O^O/ \\_/ \\ Batch size per device = 2 | Gradient Accumulation steps = 4\n",
199
+ "\\ / Total batch size = 8 | Total steps = 60\n",
200
+ " \"-____-\" Number of trainable parameters = 29,884,416\n"
201
+ ]
202
+ },
203
+ {
204
+ "data": {
205
+ "text/html": [
206
+ "\n",
207
+ " <div>\n",
208
+ " \n",
209
+ " <progress value='9' max='60' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
210
+ " [ 9/60 03:36 < 26:20, 0.03 it/s, Epoch 6.40/60]\n",
211
+ " </div>\n",
212
+ " <table border=\"1\" class=\"dataframe\">\n",
213
+ " <thead>\n",
214
+ " <tr style=\"text-align: left;\">\n",
215
+ " <th>Step</th>\n",
216
+ " <th>Training Loss</th>\n",
217
+ " </tr>\n",
218
+ " </thead>\n",
219
+ " <tbody>\n",
220
+ " <tr>\n",
221
+ " <td>1</td>\n",
222
+ " <td>1.464500</td>\n",
223
+ " </tr>\n",
224
+ " <tr>\n",
225
+ " <td>2</td>\n",
226
+ " <td>1.716400</td>\n",
227
+ " </tr>\n",
228
+ " <tr>\n",
229
+ " <td>3</td>\n",
230
+ " <td>1.345900</td>\n",
231
+ " </tr>\n",
232
+ " <tr>\n",
233
+ " <td>4</td>\n",
234
+ " <td>1.429800</td>\n",
235
+ " </tr>\n",
236
+ " <tr>\n",
237
+ " <td>5</td>\n",
238
+ " <td>1.709500</td>\n",
239
+ " </tr>\n",
240
+ " <tr>\n",
241
+ " <td>6</td>\n",
242
+ " <td>1.453600</td>\n",
243
+ " </tr>\n",
244
+ " <tr>\n",
245
+ " <td>7</td>\n",
246
+ " <td>1.219900</td>\n",
247
+ " </tr>\n",
248
+ " </tbody>\n",
249
+ "</table><p>"
250
+ ],
251
+ "text/plain": [
252
+ "<IPython.core.display.HTML object>"
253
+ ]
254
+ },
255
+ "metadata": {},
256
+ "output_type": "display_data"
257
+ }
258
+ ],
259
+ "source": [
260
+ "trainer_stats = trainer.train()"
261
+ ]
262
+ },
263
+ {
264
+ "cell_type": "code",
265
+ "execution_count": 57,
266
+ "id": "c407b9c0-aa4c-412a-b7cc-ddbdbb6a5212",
267
+ "metadata": {},
268
+ "outputs": [
269
+ {
270
+ "name": "stdout",
271
+ "output_type": "stream",
272
+ "text": [
273
+ "19796902751<|end|><|assistant|> This number is a unique identifier, often used for telephone numbers or other personalized services.<|end|><|endoftext|>\n"
274
+ ]
275
+ }
276
+ ],
277
+ "source": [
278
+ "from unsloth.chat_templates import get_chat_template\n",
279
+ "\n",
280
+ "tokenizer = get_chat_template(\n",
281
+ " tokenizer,\n",
282
+ " chat_template = \"phi-3\", # Supports zephyr, chatml, mistral, llama, alpaca, vicuna, vicuna_old, unsloth\n",
283
+ " mapping = {\"role\" : \"from\", \"content\" : \"value\", \"user\" : \"human\", \"assistant\" : \"gpt\"}, # ShareGPT style\n",
284
+ ")\n",
285
+ "\n",
286
+ "FastLanguageModel.for_inference(model) # Enable native 2x faster inference\n",
287
+ "\n",
288
+ "messages = [\n",
289
+ " {\"from\": \"human\", \"value\": \"19796902751 who uses this number?\"},\n",
290
+ "]\n",
291
+ "inputs = tokenizer.apply_chat_template(\n",
292
+ " messages,\n",
293
+ " tokenize = True,\n",
294
+ " add_generation_prompt = True, # Must add for generation\n",
295
+ " return_tensors = \"pt\",\n",
296
+ ").to(\"cuda\")\n",
297
+ "\n",
298
+ "from transformers import TextStreamer\n",
299
+ "text_streamer = TextStreamer(tokenizer, skip_prompt = True)\n",
300
+ "_ = model.generate(input_ids = inputs, streamer = text_streamer, max_new_tokens = 128, use_cache = True)"
301
+ ]
302
+ },
303
+ {
304
+ "cell_type": "code",
305
+ "execution_count": null,
306
+ "id": "069d4087-35c2-4d2e-b981-f5bc65bac44d",
307
+ "metadata": {},
308
+ "outputs": [],
309
+ "source": []
310
+ }
311
+ ],
312
+ "metadata": {
313
+ "kernelspec": {
314
+ "display_name": "Python 3 (ipykernel)",
315
+ "language": "python",
316
+ "name": "python3"
317
+ },
318
+ "language_info": {
319
+ "codemirror_mode": {
320
+ "name": "ipython",
321
+ "version": 3
322
+ },
323
+ "file_extension": ".py",
324
+ "mimetype": "text/x-python",
325
+ "name": "python",
326
+ "nbconvert_exporter": "python",
327
+ "pygments_lexer": "ipython3",
328
+ "version": "3.11.9"
329
+ }
330
+ },
331
+ "nbformat": 4,
332
+ "nbformat_minor": 5
333
+ }