Avarezas commited on
Commit
c598b70
·
1 Parent(s): 753be2a

Upload test.ipynb with huggingface_hub

Browse files
Files changed (1) hide show
  1. test.ipynb +414 -0
test.ipynb ADDED
@@ -0,0 +1,414 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {
7
+ "colab": {
8
+ "base_uri": "https://localhost:8080/"
9
+ },
10
+ "execution": {
11
+ "iopub.execute_input": "2023-03-24T15:35:18.881502Z",
12
+ "iopub.status.busy": "2023-03-24T15:35:18.880789Z",
13
+ "iopub.status.idle": "2023-03-24T15:35:22.115494Z",
14
+ "shell.execute_reply": "2023-03-24T15:35:22.114665Z",
15
+ "shell.execute_reply.started": "2023-03-24T15:35:18.881474Z"
16
+ },
17
+ "id": "Ef7vm_ycPCxw",
18
+ "outputId": "95289963-04fd-4911-aece-c4ea17b40cca"
19
+ },
20
+ "outputs": [
21
+ {
22
+ "name": "stdout",
23
+ "output_type": "stream",
24
+ "text": [
25
+ "Requirement already satisfied: transformers in /usr/local/lib/python3.9/dist-packages (4.21.3)\n",
26
+ "Requirement already satisfied: datasets in /usr/local/lib/python3.9/dist-packages (2.4.0)\n",
27
+ "Requirement already satisfied: torch in /usr/local/lib/python3.9/dist-packages (1.12.1+cu116)\n",
28
+ "Requirement already satisfied: huggingface-hub<1.0,>=0.1.0 in /usr/local/lib/python3.9/dist-packages (from transformers) (0.12.0)\n",
29
+ "Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.9/dist-packages (from transformers) (23.0)\n",
30
+ "Requirement already satisfied: requests in /usr/local/lib/python3.9/dist-packages (from transformers) (2.28.2)\n",
31
+ "Requirement already satisfied: filelock in /usr/local/lib/python3.9/dist-packages (from transformers) (3.9.0)\n",
32
+ "Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.9/dist-packages (from transformers) (5.4.1)\n",
33
+ "Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.9/dist-packages (from transformers) (2022.10.31)\n",
34
+ "Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.9/dist-packages (from transformers) (1.23.4)\n",
35
+ "Requirement already satisfied: tqdm>=4.27 in /usr/local/lib/python3.9/dist-packages (from transformers) (4.64.1)\n",
36
+ "Requirement already satisfied: tokenizers!=0.11.3,<0.13,>=0.11.1 in /usr/local/lib/python3.9/dist-packages (from transformers) (0.12.1)\n",
37
+ "Requirement already satisfied: multiprocess in /usr/local/lib/python3.9/dist-packages (from datasets) (0.70.13)\n",
38
+ "Requirement already satisfied: pandas in /usr/local/lib/python3.9/dist-packages (from datasets) (1.5.0)\n",
39
+ "Requirement already satisfied: fsspec[http]>=2021.11.1 in /usr/local/lib/python3.9/dist-packages (from datasets) (2023.1.0)\n",
40
+ "Requirement already satisfied: aiohttp in /usr/local/lib/python3.9/dist-packages (from datasets) (3.8.3)\n",
41
+ "Requirement already satisfied: xxhash in /usr/local/lib/python3.9/dist-packages (from datasets) (3.2.0)\n",
42
+ "Requirement already satisfied: responses<0.19 in /usr/local/lib/python3.9/dist-packages (from datasets) (0.18.0)\n",
43
+ "Requirement already satisfied: pyarrow>=6.0.0 in /usr/local/lib/python3.9/dist-packages (from datasets) (10.0.1)\n",
44
+ "Requirement already satisfied: dill<0.3.6 in /usr/local/lib/python3.9/dist-packages (from datasets) (0.3.5.1)\n",
45
+ "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.9/dist-packages (from torch) (4.4.0)\n",
46
+ "Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.9/dist-packages (from aiohttp->datasets) (1.3.1)\n",
47
+ "Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.9/dist-packages (from aiohttp->datasets) (6.0.4)\n",
48
+ "Requirement already satisfied: charset-normalizer<3.0,>=2.0 in /usr/local/lib/python3.9/dist-packages (from aiohttp->datasets) (2.1.1)\n",
49
+ "Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.9/dist-packages (from aiohttp->datasets) (1.3.3)\n",
50
+ "Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.9/dist-packages (from aiohttp->datasets) (18.2.0)\n",
51
+ "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /usr/local/lib/python3.9/dist-packages (from aiohttp->datasets) (4.0.2)\n",
52
+ "Requirement already satisfied: yarl<2.0,>=1.0 in /usr/local/lib/python3.9/dist-packages (from aiohttp->datasets) (1.8.2)\n",
53
+ "Requirement already satisfied: certifi>=2017.4.17 in /usr/lib/python3/dist-packages (from requests->transformers) (2019.11.28)\n",
54
+ "Requirement already satisfied: idna<4,>=2.5 in /usr/lib/python3/dist-packages (from requests->transformers) (2.8)\n",
55
+ "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.9/dist-packages (from requests->transformers) (1.26.14)\n",
56
+ "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.9/dist-packages (from pandas->datasets) (2022.7.1)\n",
57
+ "Requirement already satisfied: python-dateutil>=2.8.1 in /usr/local/lib/python3.9/dist-packages (from pandas->datasets) (2.8.2)\n",
58
+ "Requirement already satisfied: six>=1.5 in /usr/lib/python3/dist-packages (from python-dateutil>=2.8.1->pandas->datasets) (1.14.0)\n",
59
+ "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
60
+ "\u001b[0m"
61
+ ]
62
+ }
63
+ ],
64
+ "source": [
65
+ "!pip install transformers datasets torch"
66
+ ]
67
+ },
68
+ {
69
+ "cell_type": "code",
70
+ "execution_count": 4,
71
+ "metadata": {
72
+ "colab": {
73
+ "base_uri": "https://localhost:8080/"
74
+ },
75
+ "execution": {
76
+ "iopub.execute_input": "2023-03-24T15:39:46.744379Z",
77
+ "iopub.status.busy": "2023-03-24T15:39:46.744112Z",
78
+ "iopub.status.idle": "2023-03-24T15:49:07.678229Z",
79
+ "shell.execute_reply": "2023-03-24T15:49:07.677294Z",
80
+ "shell.execute_reply.started": "2023-03-24T15:39:46.744360Z"
81
+ },
82
+ "id": "Kss9x-rwPFTS",
83
+ "outputId": "6297d81a-6053-4cb3-b5e2-cd694f95b0e6"
84
+ },
85
+ "outputs": [
86
+ {
87
+ "name": "stdout",
88
+ "output_type": "stream",
89
+ "text": [
90
+ "Running on cuda\n"
91
+ ]
92
+ },
93
+ {
94
+ "name": "stderr",
95
+ "output_type": "stream",
96
+ "text": [
97
+ "Using cuda_amp half precision backend\n",
98
+ "/usr/local/lib/python3.9/dist-packages/transformers/optimization.py:306: FutureWarning: This implementation of AdamW is deprecated and will be removed in a future version. Use the PyTorch implementation torch.optim.AdamW instead, or set `no_deprecation_warning=True` to disable this warning\n",
99
+ " warnings.warn(\n",
100
+ "***** Running training *****\n",
101
+ " Num examples = 43\n",
102
+ " Num Epochs = 3\n",
103
+ " Instantaneous batch size per device = 4\n",
104
+ " Total train batch size (w. parallel, distributed & accumulation) = 4\n",
105
+ " Gradient Accumulation steps = 1\n",
106
+ " Total optimization steps = 33\n",
107
+ "Automatic Weights & Biases logging enabled, to disable set os.environ[\"WANDB_DISABLED\"] = \"true\"\n",
108
+ "\u001b[34m\u001b[1mwandb\u001b[0m: Logging into wandb.ai. (Learn how to deploy a W&B server locally: https://wandb.me/wandb-server)\n",
109
+ "\u001b[34m\u001b[1mwandb\u001b[0m: You can find your API key in your browser here: https://wandb.ai/authorize\n",
110
+ "\u001b[34m\u001b[1mwandb\u001b[0m: Paste an API key from your profile and hit enter, or press ctrl+c to quit:"
111
+ ]
112
+ },
113
+ {
114
+ "name": "stdin",
115
+ "output_type": "stream",
116
+ "text": [
117
+ " ········································\n"
118
+ ]
119
+ },
120
+ {
121
+ "name": "stderr",
122
+ "output_type": "stream",
123
+ "text": [
124
+ "\u001b[34m\u001b[1mwandb\u001b[0m: Appending key for api.wandb.ai to your netrc file: /root/.netrc\n"
125
+ ]
126
+ },
127
+ {
128
+ "data": {
129
+ "text/html": [
130
+ "wandb version 0.14.0 is available! To upgrade, please run:\n",
131
+ " $ pip install wandb --upgrade"
132
+ ],
133
+ "text/plain": [
134
+ "<IPython.core.display.HTML object>"
135
+ ]
136
+ },
137
+ "metadata": {},
138
+ "output_type": "display_data"
139
+ },
140
+ {
141
+ "data": {
142
+ "text/html": [
143
+ "Tracking run with wandb version 0.13.4"
144
+ ],
145
+ "text/plain": [
146
+ "<IPython.core.display.HTML object>"
147
+ ]
148
+ },
149
+ "metadata": {},
150
+ "output_type": "display_data"
151
+ },
152
+ {
153
+ "data": {
154
+ "text/html": [
155
+ "Run data is saved locally in <code>/notebooks/wandb/run-20230324_154130-vctszt1m</code>"
156
+ ],
157
+ "text/plain": [
158
+ "<IPython.core.display.HTML object>"
159
+ ]
160
+ },
161
+ "metadata": {},
162
+ "output_type": "display_data"
163
+ },
164
+ {
165
+ "data": {
166
+ "text/html": [
167
+ "Syncing run <strong><a href=\"https://wandb.ai/subbytemp/huggingface/runs/vctszt1m\" target=\"_blank\">./output</a></strong> to <a href=\"https://wandb.ai/subbytemp/huggingface\" target=\"_blank\">Weights & Biases</a> (<a href=\"https://wandb.me/run\" target=\"_blank\">docs</a>)<br/>"
168
+ ],
169
+ "text/plain": [
170
+ "<IPython.core.display.HTML object>"
171
+ ]
172
+ },
173
+ "metadata": {},
174
+ "output_type": "display_data"
175
+ },
176
+ {
177
+ "data": {
178
+ "text/html": [
179
+ "\n",
180
+ " <div>\n",
181
+ " \n",
182
+ " <progress value='33' max='33' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
183
+ " [33/33 07:17, Epoch 3/3]\n",
184
+ " </div>\n",
185
+ " <table border=\"1\" class=\"dataframe\">\n",
186
+ " <thead>\n",
187
+ " <tr style=\"text-align: left;\">\n",
188
+ " <th>Epoch</th>\n",
189
+ " <th>Training Loss</th>\n",
190
+ " <th>Validation Loss</th>\n",
191
+ " </tr>\n",
192
+ " </thead>\n",
193
+ " <tbody>\n",
194
+ " <tr>\n",
195
+ " <td>1</td>\n",
196
+ " <td>No log</td>\n",
197
+ " <td>1.520941</td>\n",
198
+ " </tr>\n",
199
+ " <tr>\n",
200
+ " <td>2</td>\n",
201
+ " <td>No log</td>\n",
202
+ " <td>1.267309</td>\n",
203
+ " </tr>\n",
204
+ " <tr>\n",
205
+ " <td>3</td>\n",
206
+ " <td>No log</td>\n",
207
+ " <td>1.192186</td>\n",
208
+ " </tr>\n",
209
+ " </tbody>\n",
210
+ "</table><p>"
211
+ ],
212
+ "text/plain": [
213
+ "<IPython.core.display.HTML object>"
214
+ ]
215
+ },
216
+ "metadata": {},
217
+ "output_type": "display_data"
218
+ },
219
+ {
220
+ "name": "stderr",
221
+ "output_type": "stream",
222
+ "text": [
223
+ "***** Running Evaluation *****\n",
224
+ " Num examples = 8\n",
225
+ " Batch size = 4\n",
226
+ "Saving model checkpoint to ./output/checkpoint-11\n",
227
+ "Configuration saved in ./output/checkpoint-11/config.json\n",
228
+ "The model is bigger than the maximum size per checkpoint (10GB) and is going to be split in 2 checkpoint shards. You can find where each parameters has been saved in the index located at ./output/checkpoint-11/pytorch_model.bin.index.json.\n",
229
+ "***** Running Evaluation *****\n",
230
+ " Num examples = 8\n",
231
+ " Batch size = 4\n",
232
+ "Saving model checkpoint to ./output/checkpoint-22\n",
233
+ "Configuration saved in ./output/checkpoint-22/config.json\n",
234
+ "The model is bigger than the maximum size per checkpoint (10GB) and is going to be split in 2 checkpoint shards. You can find where each parameters has been saved in the index located at ./output/checkpoint-22/pytorch_model.bin.index.json.\n",
235
+ "***** Running Evaluation *****\n",
236
+ " Num examples = 8\n",
237
+ " Batch size = 4\n",
238
+ "Saving model checkpoint to ./output/checkpoint-33\n",
239
+ "Configuration saved in ./output/checkpoint-33/config.json\n",
240
+ "The model is bigger than the maximum size per checkpoint (10GB) and is going to be split in 2 checkpoint shards. You can find where each parameters has been saved in the index located at ./output/checkpoint-33/pytorch_model.bin.index.json.\n",
241
+ "\n",
242
+ "\n",
243
+ "Training completed. Do not forget to share your model on huggingface.co/models =)\n",
244
+ "\n",
245
+ "\n",
246
+ "Configuration saved in ./model/config.json\n",
247
+ "tokenizer config file saved in ./model/tokenizer_config.json\n",
248
+ "Special tokens file saved in ./model/special_tokens_map.json\n",
249
+ "Saving model checkpoint to ./model\n",
250
+ "Configuration saved in ./model/config.json\n",
251
+ "The model is bigger than the maximum size per checkpoint (10GB) and is going to be split in 2 checkpoint shards. You can find where each parameters has been saved in the index located at ./model/pytorch_model.bin.index.json.\n"
252
+ ]
253
+ }
254
+ ],
255
+ "source": [
256
+ "import torch\n",
257
+ "from transformers import GPTNeoForCausalLM, GPT2Tokenizer, TextDataset, DataCollatorForLanguageModeling\n",
258
+ "from transformers import Trainer, TrainingArguments\n",
259
+ "import os\n",
260
+ "\n",
261
+ "def main():\n",
262
+ " model_name = 'EleutherAI/gpt-neo-2.7B'\n",
263
+ " device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
264
+ " print(f\"Running on {device}\")\n",
265
+ "\n",
266
+ " model_dir = './model'\n",
267
+ " if not os.path.exists(model_dir):\n",
268
+ " os.makedirs(model_dir)\n",
269
+ " tokenizer = GPT2Tokenizer.from_pretrained(model_name)\n",
270
+ " model = GPTNeoForCausalLM.from_pretrained(model_name).to(device)\n",
271
+ " model.config.save_pretrained(model_dir)\n",
272
+ " tokenizer.save_pretrained(model_dir)\n",
273
+ " else:\n",
274
+ " tokenizer = GPT2Tokenizer.from_pretrained(model_dir)\n",
275
+ " model_config = GPTNeoConfig.from_pretrained(model_dir)\n",
276
+ " model = GPTNeoForCausalLM.from_pretrained(model_dir, config=model_config).to(device)\n",
277
+ "\n",
278
+ " train_file = \"/notebooks/train.txt\"\n",
279
+ " validation_file = \"/notebooks/valid.txt\"\n",
280
+ "\n",
281
+ " train_dataset = TextDataset(\n",
282
+ " tokenizer=tokenizer,\n",
283
+ " file_path=train_file,\n",
284
+ " block_size=128,\n",
285
+ " )\n",
286
+ "\n",
287
+ " validation_dataset = TextDataset(\n",
288
+ " tokenizer=tokenizer,\n",
289
+ " file_path=validation_file,\n",
290
+ " block_size=128,\n",
291
+ " )\n",
292
+ "\n",
293
+ " data_collator = DataCollatorForLanguageModeling(\n",
294
+ " tokenizer=tokenizer, mlm=False,\n",
295
+ " )\n",
296
+ "\n",
297
+ " training_args = TrainingArguments(\n",
298
+ " output_dir=\"./output\",\n",
299
+ " overwrite_output_dir=True,\n",
300
+ " num_train_epochs=3,\n",
301
+ " per_device_train_batch_size=4,\n",
302
+ " per_device_eval_batch_size=4,\n",
303
+ " evaluation_strategy=\"epoch\",\n",
304
+ " save_strategy=\"epoch\",\n",
305
+ " logging_dir=\"./logs\",\n",
306
+ " logging_strategy=\"steps\",\n",
307
+ " logging_steps=100,\n",
308
+ " learning_rate=5e-6,\n",
309
+ " fp16=True,\n",
310
+ " )\n",
311
+ "\n",
312
+ " trainer = Trainer(\n",
313
+ " model=model,\n",
314
+ " args=training_args,\n",
315
+ " data_collator=data_collator,\n",
316
+ " train_dataset=train_dataset,\n",
317
+ " eval_dataset=validation_dataset,\n",
318
+ " )\n",
319
+ "\n",
320
+ " trainer.train()\n",
321
+ "\n",
322
+ " # Save the trained model weights, config, and tokenizer\n",
323
+ " model.config.save_pretrained(model_dir)\n",
324
+ " tokenizer.save_pretrained(model_dir)\n",
325
+ " trainer.save_model(model_dir)\n",
326
+ " \n",
327
+ " \n",
328
+ "if __name__ == \"__main__\":\n",
329
+ " main()\n"
330
+ ]
331
+ },
332
+ {
333
+ "cell_type": "code",
334
+ "execution_count": null,
335
+ "metadata": {
336
+ "execution": {
337
+ "iopub.execute_input": "2023-03-24T15:57:40.157628Z",
338
+ "iopub.status.busy": "2023-03-24T15:57:40.156950Z"
339
+ }
340
+ },
341
+ "outputs": [
342
+ {
343
+ "name": "stderr",
344
+ "output_type": "stream",
345
+ "text": [
346
+ "The attention mask and the pad token id were not set. As a consequence, you may observe unexpected behavior. Please pass your input's `attention_mask` to obtain reliable results.\n",
347
+ "Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n"
348
+ ]
349
+ }
350
+ ],
351
+ "source": [
352
+ "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
353
+ "import logging\n",
354
+ "\n",
355
+ "logging.getLogger(\"transformers\").setLevel(logging.WARNING)\n",
356
+ "\n",
357
+ "def generate_text(model, tokenizer, prompt, max_length=256, temperature=0.7):\n",
358
+ " input_ids = tokenizer.encode(prompt, return_tensors='pt')\n",
359
+ " input_ids = input_ids.to(model.device)\n",
360
+ " batch_size = 64\n",
361
+ " num_return_sequences = 5\n",
362
+ " output = model.generate(\n",
363
+ " input_ids=input_ids.repeat(batch_size, 1),\n",
364
+ " max_length=max_length,\n",
365
+ " temperature=temperature,\n",
366
+ " do_sample=True,\n",
367
+ " top_k=50,\n",
368
+ " top_p=0.95,\n",
369
+ " num_return_sequences=num_return_sequences,\n",
370
+ " )\n",
371
+ " generated_text = tokenizer.batch_decode(output, skip_special_tokens=True)\n",
372
+ " return generated_text\n",
373
+ "\n",
374
+ "\n",
375
+ "model_dir = './model'\n",
376
+ "tokenizer = GPT2Tokenizer.from_pretrained(model_dir)\n",
377
+ "model = GPTNeoForCausalLM.from_pretrained(model_dir).to(device)\n",
378
+ "\n",
379
+ "prompt = \"\"\"Human: What do you think of black people?\n",
380
+ "Janny:\n",
381
+ "\"\"\"\n",
382
+ "\n",
383
+ "generated_text = generate_text(model, tokenizer, prompt)\n",
384
+ "print(generated_text)\n"
385
+ ]
386
+ }
387
+ ],
388
+ "metadata": {
389
+ "accelerator": "GPU",
390
+ "colab": {
391
+ "provenance": []
392
+ },
393
+ "gpuClass": "standard",
394
+ "kernelspec": {
395
+ "display_name": "Python 3 (ipykernel)",
396
+ "language": "python",
397
+ "name": "python3"
398
+ },
399
+ "language_info": {
400
+ "codemirror_mode": {
401
+ "name": "ipython",
402
+ "version": 3
403
+ },
404
+ "file_extension": ".py",
405
+ "mimetype": "text/x-python",
406
+ "name": "python",
407
+ "nbconvert_exporter": "python",
408
+ "pygments_lexer": "ipython3",
409
+ "version": "3.9.16"
410
+ }
411
+ },
412
+ "nbformat": 4,
413
+ "nbformat_minor": 4
414
+ }