samos123 commited on
Commit
c9f3f3f
·
1 Parent(s): 78a7d83

Upload train.ipynb with huggingface_hub

Browse files
Files changed (1) hide show
  1. train.ipynb +1902 -0
train.ipynb ADDED
@@ -0,0 +1,1902 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "9c3e4532",
6
+ "metadata": {
7
+ "papermill": {
8
+ "duration": 1.349527,
9
+ "end_time": "2023-10-20T04:29:10.661274",
10
+ "exception": false,
11
+ "start_time": "2023-10-20T04:29:09.311747",
12
+ "status": "completed"
13
+ },
14
+ "tags": []
15
+ },
16
+ "source": [
17
+ "# Train models using HuggingFace libraries\n",
18
+ "\n",
19
+ "This notebook takes parameters from a params.json file which is automatically\n",
20
+ "created by Substratus K8s operator.\n",
21
+ "\n",
22
+ "The following parameters influence what happens in this notebook:\n",
23
+ "- `dataset_urls`: A comma separated list of URLs. The URLs should point to\n",
24
+ " json files that contain your training dataset. If unset a json or jsonl\n",
25
+ " file should be present under the `/content/data/` directory.\n",
26
+ "- `prompt_template`: The prompt template to use for training\n",
27
+ "- `push_to_hub`: if this variable is set a repo id, then the trained\n",
28
+ " model will get pushed to HuggingFace hub. For example,\n",
29
+ " set it to \"substratusai/my-model\" to publish to substratusai HF org."
30
+ ]
31
+ },
32
+ {
33
+ "cell_type": "code",
34
+ "execution_count": 1,
35
+ "id": "86ccd646",
36
+ "metadata": {
37
+ "execution": {
38
+ "iopub.execute_input": "2023-10-20T04:29:12.619462Z",
39
+ "iopub.status.busy": "2023-10-20T04:29:12.619161Z",
40
+ "iopub.status.idle": "2023-10-20T04:29:12.631792Z",
41
+ "shell.execute_reply": "2023-10-20T04:29:12.631220Z"
42
+ },
43
+ "papermill": {
44
+ "duration": 0.998973,
45
+ "end_time": "2023-10-20T04:29:12.633545",
46
+ "exception": false,
47
+ "start_time": "2023-10-20T04:29:11.634572",
48
+ "status": "completed"
49
+ },
50
+ "tags": []
51
+ },
52
+ "outputs": [
53
+ {
54
+ "data": {
55
+ "text/plain": [
56
+ "{'dataset_urls': 'https://huggingface.co/datasets/weaviate/WithRetrieval-SchemaSplit-Train-80/resolve/main/WithRetrieval-Random-Train-80.json',\n",
57
+ " 'inference_prompt_template': '## Instruction\\nYour task is to write GraphQL for the Natural Language Query provided. Use the provided API reference and Schema to generate the GraphQL. The GraphQL should be valid for Weaviate.\\n\\nOnly use the API reference to understand the syntax of the request.\\n\\n## Natural Language Query\\n{nlcommand}\\n\\n## Schema\\n{schema}\\n\\n## API reference\\n{apiRef}\\n\\n## Answer\\n```graphql\\n',\n",
58
+ " 'logging_steps': 50,\n",
59
+ " 'modules_to_save': 'embed_tokens, lm_head',\n",
60
+ " 'num_train_epochs': 3,\n",
61
+ " 'per_device_eval_batch_size': 1,\n",
62
+ " 'per_device_train_batch_size': 1,\n",
63
+ " 'prompt_template': '## Instruction\\nYour task is to write GraphQL for the Natural Language Query provided. Use the provided API reference and Schema to generate the GraphQL. The GraphQL should be valid for Weaviate.\\n\\nOnly use the API reference to understand the syntax of the request.\\n\\n## Natural Language Query\\n{nlcommand}\\n\\n## Schema\\n{schema}\\n\\n## API reference\\n{apiRef}\\n\\n## Answer\\n{output}\\n',\n",
64
+ " 'push_to_hub': 'substratusai/wgql-WithRetrieval-SchemaSplit-Train-80',\n",
65
+ " 'save_steps': 50,\n",
66
+ " 'target_modules': 'q_proj, up_proj, o_proj, k_proj, down_proj, gate_proj, v_proj',\n",
67
+ " 'warmup_steps': 100}"
68
+ ]
69
+ },
70
+ "execution_count": 1,
71
+ "metadata": {},
72
+ "output_type": "execute_result"
73
+ }
74
+ ],
75
+ "source": [
76
+ "import json\n",
77
+ "from pathlib import Path\n",
78
+ "\n",
79
+ "params = {}\n",
80
+ "params_path = Path(\"/content/params.json\")\n",
81
+ "if params_path.is_file():\n",
82
+ " with params_path.open(\"r\", encoding=\"UTF-8\") as params_file:\n",
83
+ " params = json.load(params_file)\n",
84
+ "\n",
85
+ "\n",
86
+ "params"
87
+ ]
88
+ },
89
+ {
90
+ "cell_type": "code",
91
+ "execution_count": 2,
92
+ "id": "9fafd16b-d8c9-47bf-9116-c27b1d43a019",
93
+ "metadata": {
94
+ "execution": {
95
+ "iopub.execute_input": "2023-10-20T04:29:15.467402Z",
96
+ "iopub.status.busy": "2023-10-20T04:29:15.466447Z",
97
+ "iopub.status.idle": "2023-10-20T04:29:18.661586Z",
98
+ "shell.execute_reply": "2023-10-20T04:29:18.660751Z"
99
+ },
100
+ "papermill": {
101
+ "duration": 5.013972,
102
+ "end_time": "2023-10-20T04:29:18.663116",
103
+ "exception": false,
104
+ "start_time": "2023-10-20T04:29:13.649144",
105
+ "status": "completed"
106
+ },
107
+ "tags": []
108
+ },
109
+ "outputs": [
110
+ {
111
+ "name": "stdout",
112
+ "output_type": "stream",
113
+ "text": [
114
+ "Using the following URLs for the dataset: ['https://huggingface.co/datasets/weaviate/WithRetrieval-SchemaSplit-Train-80/resolve/main/WithRetrieval-Random-Train-80.json']\n"
115
+ ]
116
+ },
117
+ {
118
+ "data": {
119
+ "application/vnd.jupyter.widget-view+json": {
120
+ "model_id": "975d63e8725b4e61b10cc1257c68b465",
121
+ "version_major": 2,
122
+ "version_minor": 0
123
+ },
124
+ "text/plain": [
125
+ "Downloading data files: 0%| | 0/1 [00:00<?, ?it/s]"
126
+ ]
127
+ },
128
+ "metadata": {},
129
+ "output_type": "display_data"
130
+ },
131
+ {
132
+ "data": {
133
+ "application/vnd.jupyter.widget-view+json": {
134
+ "model_id": "aca925947a6c4ca8b8136e98f870fd44",
135
+ "version_major": 2,
136
+ "version_minor": 0
137
+ },
138
+ "text/plain": [
139
+ "Downloading data: 0%| | 0.00/18.2M [00:00<?, ?B/s]"
140
+ ]
141
+ },
142
+ "metadata": {},
143
+ "output_type": "display_data"
144
+ },
145
+ {
146
+ "data": {
147
+ "application/vnd.jupyter.widget-view+json": {
148
+ "model_id": "93dc58aca1c84d35a61a46aba1fcd6f7",
149
+ "version_major": 2,
150
+ "version_minor": 0
151
+ },
152
+ "text/plain": [
153
+ "Extracting data files: 0%| | 0/1 [00:00<?, ?it/s]"
154
+ ]
155
+ },
156
+ "metadata": {},
157
+ "output_type": "display_data"
158
+ },
159
+ {
160
+ "data": {
161
+ "application/vnd.jupyter.widget-view+json": {
162
+ "model_id": "39cd86b5a5364a29854b3847cdf1dc3b",
163
+ "version_major": 2,
164
+ "version_minor": 0
165
+ },
166
+ "text/plain": [
167
+ "Generating train split: 0 examples [00:00, ? examples/s]"
168
+ ]
169
+ },
170
+ "metadata": {},
171
+ "output_type": "display_data"
172
+ },
173
+ {
174
+ "data": {
175
+ "text/plain": [
176
+ "DatasetDict({\n",
177
+ " train: Dataset({\n",
178
+ " features: ['input', 'output', 'nlcommand', 'apiRef', 'apiRefPath', 'schema', 'schemaPath'],\n",
179
+ " num_rows: 3190\n",
180
+ " })\n",
181
+ "})"
182
+ ]
183
+ },
184
+ "execution_count": 2,
185
+ "metadata": {},
186
+ "output_type": "execute_result"
187
+ }
188
+ ],
189
+ "source": [
190
+ "import os \n",
191
+ "from datasets import load_dataset\n",
192
+ "\n",
193
+ "dataset_urls = params.get(\"dataset_urls\")\n",
194
+ "if dataset_urls:\n",
195
+ " urls = [u.strip() for u in dataset_urls.split(\",\")]\n",
196
+ " print(f\"Using the following URLs for the dataset: {urls}\")\n",
197
+ " data = load_dataset(\"json\", data_files=urls)\n",
198
+ "else:\n",
199
+ " data = load_dataset(\"json\", data_files=\"/content/data/*.json*\")\n",
200
+ "data"
201
+ ]
202
+ },
203
+ {
204
+ "cell_type": "code",
205
+ "execution_count": 3,
206
+ "id": "08e478fa-d095-4145-9bd1-b4feec7bc4f0",
207
+ "metadata": {
208
+ "execution": {
209
+ "iopub.execute_input": "2023-10-20T04:29:20.676775Z",
210
+ "iopub.status.busy": "2023-10-20T04:29:20.675674Z",
211
+ "iopub.status.idle": "2023-10-20T04:32:04.018319Z",
212
+ "shell.execute_reply": "2023-10-20T04:32:04.017641Z"
213
+ },
214
+ "papermill": {
215
+ "duration": 165.158475,
216
+ "end_time": "2023-10-20T04:32:04.854327",
217
+ "exception": false,
218
+ "start_time": "2023-10-20T04:29:19.695852",
219
+ "status": "completed"
220
+ },
221
+ "tags": []
222
+ },
223
+ "outputs": [
224
+ {
225
+ "data": {
226
+ "application/vnd.jupyter.widget-view+json": {
227
+ "model_id": "64a84b0e830b4b7084aa55b1f5caf243",
228
+ "version_major": 2,
229
+ "version_minor": 0
230
+ },
231
+ "text/plain": [
232
+ "Loading checkpoint shards: 0%| | 0/2 [00:00<?, ?it/s]"
233
+ ]
234
+ },
235
+ "metadata": {},
236
+ "output_type": "display_data"
237
+ },
238
+ {
239
+ "data": {
240
+ "text/plain": [
241
+ "LlamaForCausalLM(\n",
242
+ " (model): LlamaModel(\n",
243
+ " (embed_tokens): Embedding(32000, 4096)\n",
244
+ " (layers): ModuleList(\n",
245
+ " (0-31): 32 x LlamaDecoderLayer(\n",
246
+ " (self_attn): LlamaAttention(\n",
247
+ " (q_proj): Linear(in_features=4096, out_features=4096, bias=False)\n",
248
+ " (k_proj): Linear(in_features=4096, out_features=4096, bias=False)\n",
249
+ " (v_proj): Linear(in_features=4096, out_features=4096, bias=False)\n",
250
+ " (o_proj): Linear(in_features=4096, out_features=4096, bias=False)\n",
251
+ " (rotary_emb): LlamaRotaryEmbedding()\n",
252
+ " )\n",
253
+ " (mlp): LlamaMLP(\n",
254
+ " (gate_proj): Linear(in_features=4096, out_features=11008, bias=False)\n",
255
+ " (up_proj): Linear(in_features=4096, out_features=11008, bias=False)\n",
256
+ " (down_proj): Linear(in_features=11008, out_features=4096, bias=False)\n",
257
+ " (act_fn): SiLUActivation()\n",
258
+ " )\n",
259
+ " (input_layernorm): LlamaRMSNorm()\n",
260
+ " (post_attention_layernorm): LlamaRMSNorm()\n",
261
+ " )\n",
262
+ " )\n",
263
+ " (norm): LlamaRMSNorm()\n",
264
+ " )\n",
265
+ " (lm_head): Linear(in_features=4096, out_features=32000, bias=False)\n",
266
+ ")"
267
+ ]
268
+ },
269
+ "execution_count": 3,
270
+ "metadata": {},
271
+ "output_type": "execute_result"
272
+ }
273
+ ],
274
+ "source": [
275
+ "import transformers\n",
276
+ "import torch\n",
277
+ "import sys\n",
278
+ "from transformers import AutoTokenizer, AutoModelForCausalLM\n",
279
+ "\n",
280
+ "model_path = \"/content/model/\"\n",
281
+ "trained_model_path = \"/content/artifacts\"\n",
282
+ "trained_model_path_lora = \"/content/artifacts/lora\"\n",
283
+ "\n",
284
+ "tokenizer = AutoTokenizer.from_pretrained(model_path,\n",
285
+ " local_files_only=True)\n",
286
+ "model = AutoModelForCausalLM.from_pretrained(\n",
287
+ " model_path, torch_dtype=torch.float16, device_map=\"auto\", trust_remote_code=True)\n",
288
+ "model"
289
+ ]
290
+ },
291
+ {
292
+ "cell_type": "code",
293
+ "execution_count": 4,
294
+ "id": "88908150-1585-4781-9542-d68193d808bc",
295
+ "metadata": {
296
+ "execution": {
297
+ "iopub.execute_input": "2023-10-20T04:32:06.864041Z",
298
+ "iopub.status.busy": "2023-10-20T04:32:06.863105Z",
299
+ "iopub.status.idle": "2023-10-20T04:32:06.869087Z",
300
+ "shell.execute_reply": "2023-10-20T04:32:06.868397Z"
301
+ },
302
+ "papermill": {
303
+ "duration": 1.050541,
304
+ "end_time": "2023-10-20T04:32:06.870604",
305
+ "exception": false,
306
+ "start_time": "2023-10-20T04:32:05.820063",
307
+ "status": "completed"
308
+ },
309
+ "tags": []
310
+ },
311
+ "outputs": [
312
+ {
313
+ "data": {
314
+ "text/plain": [
315
+ "LlamaConfig {\n",
316
+ " \"_name_or_path\": \"/content/model/\",\n",
317
+ " \"architectures\": [\n",
318
+ " \"LlamaForCausalLM\"\n",
319
+ " ],\n",
320
+ " \"attention_bias\": false,\n",
321
+ " \"bos_token_id\": 1,\n",
322
+ " \"eos_token_id\": 2,\n",
323
+ " \"hidden_act\": \"silu\",\n",
324
+ " \"hidden_size\": 4096,\n",
325
+ " \"initializer_range\": 0.02,\n",
326
+ " \"intermediate_size\": 11008,\n",
327
+ " \"max_position_embeddings\": 4096,\n",
328
+ " \"model_type\": \"llama\",\n",
329
+ " \"num_attention_heads\": 32,\n",
330
+ " \"num_hidden_layers\": 32,\n",
331
+ " \"num_key_value_heads\": 32,\n",
332
+ " \"pretraining_tp\": 1,\n",
333
+ " \"rms_norm_eps\": 1e-05,\n",
334
+ " \"rope_scaling\": null,\n",
335
+ " \"rope_theta\": 10000.0,\n",
336
+ " \"tie_word_embeddings\": false,\n",
337
+ " \"torch_dtype\": \"float16\",\n",
338
+ " \"transformers_version\": \"4.34.1\",\n",
339
+ " \"use_cache\": true,\n",
340
+ " \"vocab_size\": 32000\n",
341
+ "}"
342
+ ]
343
+ },
344
+ "execution_count": 4,
345
+ "metadata": {},
346
+ "output_type": "execute_result"
347
+ }
348
+ ],
349
+ "source": [
350
+ "model.config"
351
+ ]
352
+ },
353
+ {
354
+ "cell_type": "code",
355
+ "execution_count": 5,
356
+ "id": "ec8a1a9f-fe60-49c7-ab20-04034323df8a",
357
+ "metadata": {
358
+ "execution": {
359
+ "iopub.execute_input": "2023-10-20T04:32:08.917095Z",
360
+ "iopub.status.busy": "2023-10-20T04:32:08.916433Z",
361
+ "iopub.status.idle": "2023-10-20T04:32:08.921823Z",
362
+ "shell.execute_reply": "2023-10-20T04:32:08.921173Z"
363
+ },
364
+ "papermill": {
365
+ "duration": 0.987524,
366
+ "end_time": "2023-10-20T04:32:08.923396",
367
+ "exception": false,
368
+ "start_time": "2023-10-20T04:32:07.935872",
369
+ "status": "completed"
370
+ },
371
+ "tags": []
372
+ },
373
+ "outputs": [
374
+ {
375
+ "name": "stdout",
376
+ "output_type": "stream",
377
+ "text": [
378
+ "## Instruction\n",
379
+ "Your task is to write GraphQL for the Natural Language Query provided. Use the provided API reference and Schema to generate the GraphQL. The GraphQL should be valid for Weaviate.\n",
380
+ "\n",
381
+ "Only use the API reference to understand the syntax of the request.\n",
382
+ "\n",
383
+ "## Natural Language Query\n",
384
+ "{nlcommand}\n",
385
+ "\n",
386
+ "## Schema\n",
387
+ "{schema}\n",
388
+ "\n",
389
+ "## API reference\n",
390
+ "{apiRef}\n",
391
+ "\n",
392
+ "## Answer\n",
393
+ "{output}\n",
394
+ "</s>\n"
395
+ ]
396
+ }
397
+ ],
398
+ "source": [
399
+ "default_prompt = \"\"\"\n",
400
+ "Below is an instruction that describes a task. Write a response that appropriately completes the request.\n",
401
+ "### Instruction:\n",
402
+ "{prompt}\n",
403
+ "### Response:\n",
404
+ "{completion}\n",
405
+ "\"\"\"\n",
406
+ "\n",
407
+ "prompt = params.get(\"prompt_template\", default_prompt)\n",
408
+ "\n",
409
+ "eos_token = tokenizer.convert_ids_to_tokens(model.config.eos_token_id)\n",
410
+ "if prompt[-len(eos_token):] != eos_token:\n",
411
+ " prompt = prompt + eos_token\n",
412
+ "\n",
413
+ "print(prompt)\n"
414
+ ]
415
+ },
416
+ {
417
+ "cell_type": "code",
418
+ "execution_count": 6,
419
+ "id": "0abf96e1-3bc1-4ae7-80ac-c2e585e9c7c1",
420
+ "metadata": {
421
+ "execution": {
422
+ "iopub.execute_input": "2023-10-20T04:32:11.202559Z",
423
+ "iopub.status.busy": "2023-10-20T04:32:11.201752Z",
424
+ "iopub.status.idle": "2023-10-20T04:32:12.068010Z",
425
+ "shell.execute_reply": "2023-10-20T04:32:12.067140Z"
426
+ },
427
+ "papermill": {
428
+ "duration": 2.153714,
429
+ "end_time": "2023-10-20T04:32:12.069912",
430
+ "exception": false,
431
+ "start_time": "2023-10-20T04:32:09.916198",
432
+ "status": "completed"
433
+ },
434
+ "tags": []
435
+ },
436
+ "outputs": [
437
+ {
438
+ "name": "stdout",
439
+ "output_type": "stream",
440
+ "text": [
441
+ "Fri Oct 20 04:32:11 2023 \r\n",
442
+ "+-----------------------------------------------------------------------------+\r\n",
443
+ "| NVIDIA-SMI 525.105.17 Driver Version: 525.105.17 CUDA Version: 12.0 |\r\n",
444
+ "|-------------------------------+----------------------+----------------------+\r\n",
445
+ "| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |\r\n",
446
+ "| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |\r\n",
447
+ "| | | MIG M. |\r\n",
448
+ "|===============================+======================+======================|\r\n",
449
+ "| 0 NVIDIA L4 Off | 00000000:00:04.0 Off | 0 |\r\n",
450
+ "| N/A 56C P0 30W / 72W | 3570MiB / 23034MiB | 0% Default |\r\n",
451
+ "| | | N/A |\r\n",
452
+ "+-------------------------------+----------------------+----------------------+\r\n"
453
+ ]
454
+ },
455
+ {
456
+ "name": "stdout",
457
+ "output_type": "stream",
458
+ "text": [
459
+ "| 1 NVIDIA L4 Off | 00000000:00:05.0 Off | 0 |\r\n",
460
+ "| N/A 56C P0 29W / 72W | 4096MiB / 23034MiB | 0% Default |\r\n",
461
+ "| | | N/A |\r\n",
462
+ "+-------------------------------+----------------------+----------------------+\r\n",
463
+ "| 2 NVIDIA L4 Off | 00000000:00:06.0 Off | 0 |\r\n",
464
+ "| N/A 58C P0 30W / 72W | 4096MiB / 23034MiB | 0% Default |\r\n",
465
+ "| | | N/A |\r\n",
466
+ "+-------------------------------+----------------------+----------------------+\r\n",
467
+ "| 3 NVIDIA L4 Off | 00000000:00:07.0 Off | 0 |\r\n",
468
+ "| N/A 58C P0 31W / 72W | 3570MiB / 23034MiB | 0% Default |\r\n",
469
+ "| | | N/A |\r\n",
470
+ "+-------------------------------+----------------------+----------------------+\r\n",
471
+ " \r\n",
472
+ "+-----------------------------------------------------------------------------+\r\n",
473
+ "| Processes: |\r\n",
474
+ "| GPU GI CI PID Type Process name GPU Memory |\r\n",
475
+ "| ID ID Usage |\r\n",
476
+ "|=============================================================================|\r\n",
477
+ "+-----------------------------------------------------------------------------+\r\n"
478
+ ]
479
+ }
480
+ ],
481
+ "source": [
482
+ "! nvidia-smi"
483
+ ]
484
+ },
485
+ {
486
+ "attachments": {},
487
+ "cell_type": "markdown",
488
+ "id": "4d1e1795-c783-4ddf-999e-f1de19258928",
489
+ "metadata": {
490
+ "papermill": {
491
+ "duration": 1.669218,
492
+ "end_time": "2023-10-20T04:32:14.740838",
493
+ "exception": false,
494
+ "start_time": "2023-10-20T04:32:13.071620",
495
+ "status": "completed"
496
+ },
497
+ "tags": []
498
+ },
499
+ "source": [
500
+ "Prompt before fine tuning"
501
+ ]
502
+ },
503
+ {
504
+ "cell_type": "code",
505
+ "execution_count": 7,
506
+ "id": "f5dd944b-e2bd-4bfd-a5fa-55bc90239926",
507
+ "metadata": {
508
+ "execution": {
509
+ "iopub.execute_input": "2023-10-20T04:32:19.219082Z",
510
+ "iopub.status.busy": "2023-10-20T04:32:19.218724Z",
511
+ "iopub.status.idle": "2023-10-20T04:32:19.240542Z",
512
+ "shell.execute_reply": "2023-10-20T04:32:19.239838Z"
513
+ },
514
+ "papermill": {
515
+ "duration": 3.091602,
516
+ "end_time": "2023-10-20T04:32:19.242046",
517
+ "exception": false,
518
+ "start_time": "2023-10-20T04:32:16.150444",
519
+ "status": "completed"
520
+ },
521
+ "tags": []
522
+ },
523
+ "outputs": [
524
+ {
525
+ "data": {
526
+ "text/plain": [
527
+ "LlamaTokenizerFast(name_or_path='/content/model/', vocab_size=32000, model_max_length=1000000000000000019884624838656, is_fast=True, padding_side='right', truncation_side='right', special_tokens={'bos_token': '<s>', 'eos_token': '</s>', 'unk_token': '<unk>', 'pad_token': '[PAD]'}, clean_up_tokenization_spaces=False), added_tokens_decoder={\n",
528
+ "\t0: AddedToken(\"<unk>\", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),\n",
529
+ "\t1: AddedToken(\"<s>\", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),\n",
530
+ "\t2: AddedToken(\"</s>\", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),\n",
531
+ "\t32000: AddedToken(\"[PAD]\", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),\n",
532
+ "}"
533
+ ]
534
+ },
535
+ "execution_count": 7,
536
+ "metadata": {},
537
+ "output_type": "execute_result"
538
+ }
539
+ ],
540
+ "source": [
541
+ "from typing import Dict\n",
542
+ "# source: https://github.com/artidoro/qlora\n",
543
+ "DEFAULT_PAD_TOKEN = params.get(\"pad_token\", \"[PAD]\")\n",
544
+ "\n",
545
+ "def smart_tokenizer_and_embedding_resize(\n",
546
+ " special_tokens_dict: Dict,\n",
547
+ " tokenizer: transformers.PreTrainedTokenizer,\n",
548
+ " model: transformers.PreTrainedModel,\n",
549
+ "):\n",
550
+ " \"\"\"Resize tokenizer and embedding.\n",
551
+ "\n",
552
+ " Note: This is the unoptimized version that may make your embedding size not be divisible by 64.\n",
553
+ " \"\"\"\n",
554
+ " num_new_tokens = tokenizer.add_special_tokens(special_tokens_dict)\n",
555
+ " model.resize_token_embeddings(len(tokenizer))\n",
556
+ " if num_new_tokens > 0:\n",
557
+ " input_embeddings_data = model.get_input_embeddings().weight.data\n",
558
+ " output_embeddings_data = model.get_output_embeddings().weight.data\n",
559
+ "\n",
560
+ " input_embeddings_avg = input_embeddings_data[:-num_new_tokens].mean(dim=0, keepdim=True)\n",
561
+ " output_embeddings_avg = output_embeddings_data[:-num_new_tokens].mean(dim=0, keepdim=True)\n",
562
+ "\n",
563
+ " input_embeddings_data[-num_new_tokens:] = input_embeddings_avg\n",
564
+ " output_embeddings_data[-num_new_tokens:] = output_embeddings_avg\n",
565
+ "\n",
566
+ "if tokenizer._pad_token is None:\n",
567
+ " smart_tokenizer_and_embedding_resize(\n",
568
+ " special_tokens_dict=dict(pad_token=DEFAULT_PAD_TOKEN),\n",
569
+ " tokenizer=tokenizer,\n",
570
+ " model=model,\n",
571
+ " )\n",
572
+ "\n",
573
+ "if isinstance(tokenizer, transformers.LlamaTokenizer):\n",
574
+ " # LLaMA tokenizer may not have correct special tokens set.\n",
575
+ " # Check and add them if missing to prevent them from being parsed into different tokens.\n",
576
+ " # Note that these are present in the vocabulary.\n",
577
+ " # Note also that `model.config.pad_token_id` is 0 which corresponds to `<unk>` token.\n",
578
+ " print('Adding special tokens.')\n",
579
+ " tokenizer.add_special_tokens({\n",
580
+ " \"eos_token\": tokenizer.convert_ids_to_tokens(model.config.eos_token_id),\n",
581
+ " \"bos_token\": tokenizer.convert_ids_to_tokens(model.config.bos_token_id),\n",
582
+ " \"unk_token\": tokenizer.convert_ids_to_tokens(\n",
583
+ " model.config.pad_token_id if model.config.pad_token_id != -1 else tokenizer.pad_token_id\n",
584
+ " ),\n",
585
+ " })\n",
586
+ "\n",
587
+ "tokenizer"
588
+ ]
589
+ },
590
+ {
591
+ "cell_type": "code",
592
+ "execution_count": 8,
593
+ "id": "e78b510d",
594
+ "metadata": {
595
+ "execution": {
596
+ "iopub.execute_input": "2023-10-20T04:32:21.353541Z",
597
+ "iopub.status.busy": "2023-10-20T04:32:21.352756Z",
598
+ "iopub.status.idle": "2023-10-20T04:32:28.425095Z",
599
+ "shell.execute_reply": "2023-10-20T04:32:28.424393Z"
600
+ },
601
+ "papermill": {
602
+ "duration": 8.18185,
603
+ "end_time": "2023-10-20T04:32:28.426669",
604
+ "exception": false,
605
+ "start_time": "2023-10-20T04:32:20.244819",
606
+ "status": "completed"
607
+ },
608
+ "tags": []
609
+ },
610
+ "outputs": [
611
+ {
612
+ "data": {
613
+ "application/vnd.jupyter.widget-view+json": {
614
+ "model_id": "fe3e712d3b954f6d9a6c5c0bac764ca5",
615
+ "version_major": 2,
616
+ "version_minor": 0
617
+ },
618
+ "text/plain": [
619
+ "Map: 0%| | 0/3190 [00:00<?, ? examples/s]"
620
+ ]
621
+ },
622
+ "metadata": {},
623
+ "output_type": "display_data"
624
+ },
625
+ {
626
+ "name": "stdout",
627
+ "output_type": "stream",
628
+ "text": [
629
+ "After tokenizing: DatasetDict({\n",
630
+ " train: Dataset({\n",
631
+ " features: ['input', 'output', 'nlcommand', 'apiRef', 'apiRefPath', 'schema', 'schemaPath', 'input_ids', 'attention_mask'],\n",
632
+ " num_rows: 3190\n",
633
+ " })\n",
634
+ "})\n"
635
+ ]
636
+ }
637
+ ],
638
+ "source": [
639
+ "from typing import Dict\n",
640
+ "\n",
641
+ "data = data.map(lambda x: tokenizer(prompt.format_map(x)))\n",
642
+ "\n",
643
+ "print(\"After tokenizing:\", data)"
644
+ ]
645
+ },
646
+ {
647
+ "cell_type": "code",
648
+ "execution_count": 9,
649
+ "id": "5dae6c6f-3ae1-4697-852e-fce24a82b9e8",
650
+ "metadata": {
651
+ "execution": {
652
+ "iopub.execute_input": "2023-10-20T04:32:30.430657Z",
653
+ "iopub.status.busy": "2023-10-20T04:32:30.429866Z",
654
+ "iopub.status.idle": "2023-10-20T04:34:04.076111Z",
655
+ "shell.execute_reply": "2023-10-20T04:34:04.075349Z"
656
+ },
657
+ "papermill": {
658
+ "duration": 95.945885,
659
+ "end_time": "2023-10-20T04:34:05.372590",
660
+ "exception": false,
661
+ "start_time": "2023-10-20T04:32:29.426705",
662
+ "status": "completed"
663
+ },
664
+ "tags": []
665
+ },
666
+ "outputs": [
667
+ {
668
+ "name": "stdout",
669
+ "output_type": "stream",
670
+ "text": [
671
+ "LoraConfig(peft_type=<PeftType.LORA: 'LORA'>, auto_mapping=None, base_model_name_or_path=None, revision=None, task_type='CAUSAL_LM', inference_mode=False, r=16, target_modules=['q_proj', 'up_proj', 'o_proj', 'k_proj', 'down_proj', 'gate_proj', 'v_proj'], lora_alpha=16, lora_dropout=0.05, fan_in_fan_out=False, bias='none', modules_to_save=['embed_tokens', 'lm_head'], init_lora_weights=True, layers_to_transform=None, layers_pattern=None)\n"
672
+ ]
673
+ },
674
+ {
675
+ "name": "stdout",
676
+ "output_type": "stream",
677
+ "text": [
678
+ "trainable params: 564,281,344 || all params: 7,040,552,960 || trainable%: 8.01473047935144\n"
679
+ ]
680
+ }
681
+ ],
682
+ "source": [
683
+ "from peft import get_peft_model, LoraConfig, prepare_model_for_kbit_training\n",
684
+ "\n",
685
+ "target_modules = params.get(\"target_modules\")\n",
686
+ "if target_modules:\n",
687
+ " target_modules = [mod.strip() for mod in target_modules.split(\",\")]\n",
688
+ "\n",
689
+ "modules_to_save = params.get(\"modules_to_save\")\n",
690
+ "if modules_to_save:\n",
691
+ " modules_to_save = [mod.strip() for mod in modules_to_save.split(\",\")]\n",
692
+ "\n",
693
+ "lora_config2 = LoraConfig(\n",
694
+ " r=16,\n",
695
+ " lora_alpha=16,\n",
696
+ " lora_dropout=0.05,\n",
697
+ " bias=\"none\",\n",
698
+ " task_type=\"CAUSAL_LM\",\n",
699
+ " target_modules=target_modules,\n",
700
+ " modules_to_save = modules_to_save\n",
701
+ ")\n",
702
+ "print(lora_config2)\n",
703
+ "\n",
704
+ "model = prepare_model_for_kbit_training(model)\n",
705
+ "\n",
706
+ "# add LoRA adaptor\n",
707
+ "model = get_peft_model(model, lora_config2)\n",
708
+ "model.print_trainable_parameters()"
709
+ ]
710
+ },
711
+ {
712
+ "cell_type": "code",
713
+ "execution_count": 10,
714
+ "id": "70a3e36c-62cf-45aa-8f37-0db0e40857dc",
715
+ "metadata": {
716
+ "execution": {
717
+ "iopub.execute_input": "2023-10-20T04:34:07.472890Z",
718
+ "iopub.status.busy": "2023-10-20T04:34:07.472055Z",
719
+ "iopub.status.idle": "2023-10-20T04:34:07.493277Z",
720
+ "shell.execute_reply": "2023-10-20T04:34:07.492455Z"
721
+ },
722
+ "papermill": {
723
+ "duration": 1.07078,
724
+ "end_time": "2023-10-20T04:34:07.494832",
725
+ "exception": false,
726
+ "start_time": "2023-10-20T04:34:06.424052",
727
+ "status": "completed"
728
+ },
729
+ "tags": []
730
+ },
731
+ "outputs": [
732
+ {
733
+ "data": {
734
+ "text/plain": [
735
+ "TrainingArguments(\n",
736
+ "_n_gpu=4,\n",
737
+ "adafactor=False,\n",
738
+ "adam_beta1=0.9,\n",
739
+ "adam_beta2=0.999,\n",
740
+ "adam_epsilon=1e-08,\n",
741
+ "auto_find_batch_size=False,\n",
742
+ "bf16=False,\n",
743
+ "bf16_full_eval=False,\n",
744
+ "data_seed=None,\n",
745
+ "dataloader_drop_last=False,\n",
746
+ "dataloader_num_workers=0,\n",
747
+ "dataloader_pin_memory=True,\n",
748
+ "ddp_backend=None,\n",
749
+ "ddp_broadcast_buffers=None,\n",
750
+ "ddp_bucket_cap_mb=None,\n",
751
+ "ddp_find_unused_parameters=None,\n",
752
+ "ddp_timeout=1800,\n",
753
+ "debug=[],\n",
754
+ "deepspeed=None,\n",
755
+ "disable_tqdm=False,\n",
756
+ "dispatch_batches=None,\n",
757
+ "do_eval=False,\n",
758
+ "do_predict=False,\n",
759
+ "do_train=False,\n",
760
+ "eval_accumulation_steps=None,\n",
761
+ "eval_delay=0,\n",
762
+ "eval_steps=None,\n",
763
+ "evaluation_strategy=no,\n",
764
+ "fp16=True,\n",
765
+ "fp16_backend=auto,\n",
766
+ "fp16_full_eval=False,\n",
767
+ "fp16_opt_level=O1,\n",
768
+ "fsdp=[],\n",
769
+ "fsdp_config={'min_num_params': 0, 'xla': False, 'xla_fsdp_grad_ckpt': False},\n",
770
+ "fsdp_min_num_params=0,\n",
771
+ "fsdp_transformer_layer_cls_to_wrap=None,\n",
772
+ "full_determinism=False,\n",
773
+ "gradient_accumulation_steps=4,\n",
774
+ "gradient_checkpointing=False,\n",
775
+ "greater_is_better=None,\n",
776
+ "group_by_length=False,\n",
777
+ "half_precision_backend=auto,\n",
778
+ "hub_always_push=False,\n",
779
+ "hub_model_id=None,\n",
780
+ "hub_private_repo=False,\n",
781
+ "hub_strategy=every_save,\n",
782
+ "hub_token=<HUB_TOKEN>,\n",
783
+ "ignore_data_skip=False,\n",
784
+ "include_inputs_for_metrics=False,\n",
785
+ "include_tokens_per_second=False,\n",
786
+ "jit_mode_eval=False,\n",
787
+ "label_names=None,\n",
788
+ "label_smoothing_factor=0.0,\n",
789
+ "learning_rate=3e-05,\n",
790
+ "length_column_name=length,\n",
791
+ "load_best_model_at_end=False,\n",
792
+ "local_rank=0,\n",
793
+ "log_level=passive,\n",
794
+ "log_level_replica=warning,\n",
795
+ "log_on_each_node=True,\n",
796
+ "logging_dir=/content/artifacts/checkpoints/runs/Oct20_04-34-07_wgqlg-withretrieval-schemasplit-train-80-v4-modeller-hhc7z,\n",
797
+ "logging_first_step=False,\n",
798
+ "logging_nan_inf_filter=True,\n",
799
+ "logging_steps=50,\n",
800
+ "logging_strategy=steps,\n",
801
+ "lr_scheduler_type=cosine,\n",
802
+ "max_grad_norm=1.0,\n",
803
+ "max_steps=-1,\n",
804
+ "metric_for_best_model=None,\n",
805
+ "mp_parameters=,\n",
806
+ "no_cuda=False,\n",
807
+ "num_train_epochs=3.0,\n",
808
+ "optim=paged_adamw_32bit,\n",
809
+ "optim_args=None,\n",
810
+ "output_dir=/content/artifacts/checkpoints,\n",
811
+ "overwrite_output_dir=False,\n",
812
+ "past_index=-1,\n",
813
+ "per_device_eval_batch_size=1,\n",
814
+ "per_device_train_batch_size=1,\n",
815
+ "prediction_loss_only=False,\n",
816
+ "push_to_hub=False,\n",
817
+ "push_to_hub_model_id=None,\n",
818
+ "push_to_hub_organization=None,\n",
819
+ "push_to_hub_token=<PUSH_TO_HUB_TOKEN>,\n",
820
+ "ray_scope=last,\n",
821
+ "remove_unused_columns=True,\n",
822
+ "report_to=[],\n",
823
+ "resume_from_checkpoint=None,\n",
824
+ "run_name=/content/artifacts/checkpoints,\n",
825
+ "save_on_each_node=False,\n",
826
+ "save_safetensors=False,\n",
827
+ "save_steps=50,\n",
828
+ "save_strategy=steps,\n",
829
+ "save_total_limit=None,\n",
830
+ "seed=42,\n",
831
+ "sharded_ddp=[],\n",
832
+ "skip_memory_metrics=True,\n",
833
+ "tf32=None,\n",
834
+ "torch_compile=False,\n",
835
+ "torch_compile_backend=None,\n",
836
+ "torch_compile_mode=None,\n",
837
+ "torchdynamo=None,\n",
838
+ "tpu_metrics_debug=False,\n",
839
+ "tpu_num_cores=None,\n",
840
+ "use_cpu=False,\n",
841
+ "use_ipex=False,\n",
842
+ "use_legacy_prediction_loop=False,\n",
843
+ "use_mps_device=False,\n",
844
+ "warmup_ratio=0.02,\n",
845
+ "warmup_steps=100,\n",
846
+ "weight_decay=0.0,\n",
847
+ ")"
848
+ ]
849
+ },
850
+ "execution_count": 10,
851
+ "metadata": {},
852
+ "output_type": "execute_result"
853
+ }
854
+ ],
855
+ "source": [
856
+ "from utils import parse_training_args\n",
857
+ "\n",
858
+ "training_args = parse_training_args(params)\n",
859
+ "training_args"
860
+ ]
861
+ },
862
+ {
863
+ "cell_type": "code",
864
+ "execution_count": 11,
865
+ "id": "2ae3e5f9-e28e-457b-b6bf-a62a472241bf",
866
+ "metadata": {
867
+ "execution": {
868
+ "iopub.execute_input": "2023-10-20T04:34:09.671203Z",
869
+ "iopub.status.busy": "2023-10-20T04:34:09.670480Z",
870
+ "iopub.status.idle": "2023-10-20T04:34:09.673797Z",
871
+ "shell.execute_reply": "2023-10-20T04:34:09.673184Z"
872
+ },
873
+ "papermill": {
874
+ "duration": 1.114255,
875
+ "end_time": "2023-10-20T04:34:09.675322",
876
+ "exception": false,
877
+ "start_time": "2023-10-20T04:34:08.561067",
878
+ "status": "completed"
879
+ },
880
+ "tags": []
881
+ },
882
+ "outputs": [],
883
+ "source": [
884
+ "# data = data[\"train\"].train_test_split(test_size=0.1)\n",
885
+ "# data\n"
886
+ ]
887
+ },
888
+ {
889
+ "cell_type": "code",
890
+ "execution_count": 12,
891
+ "id": "5bc91439-6108-445c-8f85-e6558c9f0677",
892
+ "metadata": {
893
+ "execution": {
894
+ "iopub.execute_input": "2023-10-20T04:34:11.808129Z",
895
+ "iopub.status.busy": "2023-10-20T04:34:11.807696Z",
896
+ "iopub.status.idle": "2023-10-20T04:34:12.065605Z",
897
+ "shell.execute_reply": "2023-10-20T04:34:12.064728Z"
898
+ },
899
+ "papermill": {
900
+ "duration": 1.353165,
901
+ "end_time": "2023-10-20T04:34:12.067207",
902
+ "exception": false,
903
+ "start_time": "2023-10-20T04:34:10.714042",
904
+ "status": "completed"
905
+ },
906
+ "tags": []
907
+ },
908
+ "outputs": [
909
+ {
910
+ "name": "stderr",
911
+ "output_type": "stream",
912
+ "text": [
913
+ "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
914
+ "To disable this warning, you can either:\n",
915
+ "\t- Avoid using `tokenizers` before the fork if possible\n",
916
+ "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
917
+ ]
918
+ }
919
+ ],
920
+ "source": [
921
+ "! mkdir -p {trained_model_path_lora}"
922
+ ]
923
+ },
924
+ {
925
+ "cell_type": "code",
926
+ "execution_count": 13,
927
+ "id": "b33e407a-9d4f-49f6-a74b-b80db8cc3a8a",
928
+ "metadata": {
929
+ "execution": {
930
+ "iopub.execute_input": "2023-10-20T04:34:14.147929Z",
931
+ "iopub.status.busy": "2023-10-20T04:34:14.147060Z",
932
+ "iopub.status.idle": "2023-10-20T08:45:44.939895Z",
933
+ "shell.execute_reply": "2023-10-20T08:45:44.939204Z"
934
+ },
935
+ "papermill": {
936
+ "duration": 15092.697329,
937
+ "end_time": "2023-10-20T08:45:45.801991",
938
+ "exception": false,
939
+ "start_time": "2023-10-20T04:34:13.104662",
940
+ "status": "completed"
941
+ },
942
+ "tags": []
943
+ },
944
+ "outputs": [
945
+ {
946
+ "name": "stderr",
947
+ "output_type": "stream",
948
+ "text": [
949
+ "You're using a LlamaTokenizerFast tokenizer. Please note that with a fast tokenizer, using the `__call__` method is faster than using a method to encode the text followed by a call to the `pad` method to get a padded encoding.\n"
950
+ ]
951
+ },
952
+ {
953
+ "data": {
954
+ "text/html": [
955
+ "\n",
956
+ " <div>\n",
957
+ " \n",
958
+ " <progress value='2391' max='2391' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
959
+ " [2391/2391 4:11:21, Epoch 2/3]\n",
960
+ " </div>\n",
961
+ " <table border=\"1\" class=\"dataframe\">\n",
962
+ " <thead>\n",
963
+ " <tr style=\"text-align: left;\">\n",
964
+ " <th>Step</th>\n",
965
+ " <th>Training Loss</th>\n",
966
+ " </tr>\n",
967
+ " </thead>\n",
968
+ " <tbody>\n",
969
+ " <tr>\n",
970
+ " <td>50</td>\n",
971
+ " <td>1.069500</td>\n",
972
+ " </tr>\n",
973
+ " <tr>\n",
974
+ " <td>100</td>\n",
975
+ " <td>0.515400</td>\n",
976
+ " </tr>\n",
977
+ " <tr>\n",
978
+ " <td>150</td>\n",
979
+ " <td>0.274500</td>\n",
980
+ " </tr>\n",
981
+ " <tr>\n",
982
+ " <td>200</td>\n",
983
+ " <td>0.173600</td>\n",
984
+ " </tr>\n",
985
+ " <tr>\n",
986
+ " <td>250</td>\n",
987
+ " <td>0.118500</td>\n",
988
+ " </tr>\n",
989
+ " <tr>\n",
990
+ " <td>300</td>\n",
991
+ " <td>0.084000</td>\n",
992
+ " </tr>\n",
993
+ " <tr>\n",
994
+ " <td>350</td>\n",
995
+ " <td>0.065700</td>\n",
996
+ " </tr>\n",
997
+ " <tr>\n",
998
+ " <td>400</td>\n",
999
+ " <td>0.054700</td>\n",
1000
+ " </tr>\n",
1001
+ " <tr>\n",
1002
+ " <td>450</td>\n",
1003
+ " <td>0.048400</td>\n",
1004
+ " </tr>\n",
1005
+ " <tr>\n",
1006
+ " <td>500</td>\n",
1007
+ " <td>0.044000</td>\n",
1008
+ " </tr>\n",
1009
+ " <tr>\n",
1010
+ " <td>550</td>\n",
1011
+ " <td>0.039800</td>\n",
1012
+ " </tr>\n",
1013
+ " <tr>\n",
1014
+ " <td>600</td>\n",
1015
+ " <td>0.039200</td>\n",
1016
+ " </tr>\n",
1017
+ " <tr>\n",
1018
+ " <td>650</td>\n",
1019
+ " <td>0.038100</td>\n",
1020
+ " </tr>\n",
1021
+ " <tr>\n",
1022
+ " <td>700</td>\n",
1023
+ " <td>0.034400</td>\n",
1024
+ " </tr>\n",
1025
+ " <tr>\n",
1026
+ " <td>750</td>\n",
1027
+ " <td>0.034300</td>\n",
1028
+ " </tr>\n",
1029
+ " <tr>\n",
1030
+ " <td>800</td>\n",
1031
+ " <td>0.032600</td>\n",
1032
+ " </tr>\n",
1033
+ " <tr>\n",
1034
+ " <td>850</td>\n",
1035
+ " <td>0.027300</td>\n",
1036
+ " </tr>\n",
1037
+ " <tr>\n",
1038
+ " <td>900</td>\n",
1039
+ " <td>0.026600</td>\n",
1040
+ " </tr>\n",
1041
+ " <tr>\n",
1042
+ " <td>950</td>\n",
1043
+ " <td>0.027700</td>\n",
1044
+ " </tr>\n",
1045
+ " <tr>\n",
1046
+ " <td>1000</td>\n",
1047
+ " <td>0.026800</td>\n",
1048
+ " </tr>\n",
1049
+ " <tr>\n",
1050
+ " <td>1050</td>\n",
1051
+ " <td>0.026100</td>\n",
1052
+ " </tr>\n",
1053
+ " <tr>\n",
1054
+ " <td>1100</td>\n",
1055
+ " <td>0.026900</td>\n",
1056
+ " </tr>\n",
1057
+ " <tr>\n",
1058
+ " <td>1150</td>\n",
1059
+ " <td>0.026200</td>\n",
1060
+ " </tr>\n",
1061
+ " <tr>\n",
1062
+ " <td>1200</td>\n",
1063
+ " <td>0.025400</td>\n",
1064
+ " </tr>\n",
1065
+ " <tr>\n",
1066
+ " <td>1250</td>\n",
1067
+ " <td>0.023900</td>\n",
1068
+ " </tr>\n",
1069
+ " <tr>\n",
1070
+ " <td>1300</td>\n",
1071
+ " <td>0.025000</td>\n",
1072
+ " </tr>\n",
1073
+ " <tr>\n",
1074
+ " <td>1350</td>\n",
1075
+ " <td>0.024000</td>\n",
1076
+ " </tr>\n",
1077
+ " <tr>\n",
1078
+ " <td>1400</td>\n",
1079
+ " <td>0.025500</td>\n",
1080
+ " </tr>\n",
1081
+ " <tr>\n",
1082
+ " <td>1450</td>\n",
1083
+ " <td>0.024200</td>\n",
1084
+ " </tr>\n",
1085
+ " <tr>\n",
1086
+ " <td>1500</td>\n",
1087
+ " <td>0.023000</td>\n",
1088
+ " </tr>\n",
1089
+ " <tr>\n",
1090
+ " <td>1550</td>\n",
1091
+ " <td>0.024700</td>\n",
1092
+ " </tr>\n",
1093
+ " <tr>\n",
1094
+ " <td>1600</td>\n",
1095
+ " <td>0.023400</td>\n",
1096
+ " </tr>\n",
1097
+ " <tr>\n",
1098
+ " <td>1650</td>\n",
1099
+ " <td>0.019500</td>\n",
1100
+ " </tr>\n",
1101
+ " <tr>\n",
1102
+ " <td>1700</td>\n",
1103
+ " <td>0.019600</td>\n",
1104
+ " </tr>\n",
1105
+ " <tr>\n",
1106
+ " <td>1750</td>\n",
1107
+ " <td>0.020400</td>\n",
1108
+ " </tr>\n",
1109
+ " <tr>\n",
1110
+ " <td>1800</td>\n",
1111
+ " <td>0.019600</td>\n",
1112
+ " </tr>\n",
1113
+ " <tr>\n",
1114
+ " <td>1850</td>\n",
1115
+ " <td>0.019200</td>\n",
1116
+ " </tr>\n",
1117
+ " <tr>\n",
1118
+ " <td>1900</td>\n",
1119
+ " <td>0.019600</td>\n",
1120
+ " </tr>\n",
1121
+ " <tr>\n",
1122
+ " <td>1950</td>\n",
1123
+ " <td>0.018700</td>\n",
1124
+ " </tr>\n",
1125
+ " <tr>\n",
1126
+ " <td>2000</td>\n",
1127
+ " <td>0.019400</td>\n",
1128
+ " </tr>\n",
1129
+ " <tr>\n",
1130
+ " <td>2050</td>\n",
1131
+ " <td>0.020000</td>\n",
1132
+ " </tr>\n",
1133
+ " <tr>\n",
1134
+ " <td>2100</td>\n",
1135
+ " <td>0.020300</td>\n",
1136
+ " </tr>\n",
1137
+ " <tr>\n",
1138
+ " <td>2150</td>\n",
1139
+ " <td>0.019400</td>\n",
1140
+ " </tr>\n",
1141
+ " <tr>\n",
1142
+ " <td>2200</td>\n",
1143
+ " <td>0.019400</td>\n",
1144
+ " </tr>\n",
1145
+ " <tr>\n",
1146
+ " <td>2250</td>\n",
1147
+ " <td>0.019700</td>\n",
1148
+ " </tr>\n",
1149
+ " <tr>\n",
1150
+ " <td>2300</td>\n",
1151
+ " <td>0.019300</td>\n",
1152
+ " </tr>\n",
1153
+ " <tr>\n",
1154
+ " <td>2350</td>\n",
1155
+ " <td>0.019500</td>\n",
1156
+ " </tr>\n",
1157
+ " </tbody>\n",
1158
+ "</table><p>"
1159
+ ],
1160
+ "text/plain": [
1161
+ "<IPython.core.display.HTML object>"
1162
+ ]
1163
+ },
1164
+ "metadata": {},
1165
+ "output_type": "display_data"
1166
+ },
1167
+ {
1168
+ "data": {
1169
+ "text/plain": [
1170
+ "TrainOutput(global_step=2391, training_loss=0.07075796158230988, metrics={'train_runtime': 15090.3717, 'train_samples_per_second': 0.634, 'train_steps_per_second': 0.158, 'total_flos': 3.0420974601928704e+17, 'train_loss': 0.07075796158230988, 'epoch': 3.0})"
1171
+ ]
1172
+ },
1173
+ "execution_count": 13,
1174
+ "metadata": {},
1175
+ "output_type": "execute_result"
1176
+ }
1177
+ ],
1178
+ "source": [
1179
+ "trainer = transformers.Trainer(\n",
1180
+ " model=model,\n",
1181
+ " train_dataset=data[\"train\"],\n",
1182
+ "# eval_dataset=data[\"test\"],\n",
1183
+ " args=training_args,\n",
1184
+ " data_collator=transformers.DataCollatorForLanguageModeling(tokenizer, mlm=False),\n",
1185
+ ")\n",
1186
+ "model.config.use_cache = False # silence the warnings. Please re-enable for inference!\n",
1187
+ "\n",
1188
+ "checkpoint_path = Path(\"/content/artifacts/checkpoints\")\n",
1189
+ "\n",
1190
+ "# Only set resume_from_checkpoint True when directory exists and contains files\n",
1191
+ "resume_from_checkpoint = checkpoint_path.is_dir() and any(checkpoint_path.iterdir())\n",
1192
+ "if resume_from_checkpoint:\n",
1193
+ " print(\"Resuming from checkpoint:\", list(checkpoint_path.rglob(\"\")))\n",
1194
+ "trainer.train(resume_from_checkpoint=resume_from_checkpoint)"
1195
+ ]
1196
+ },
1197
+ {
1198
+ "cell_type": "code",
1199
+ "execution_count": 14,
1200
+ "id": "172e47a7-400e-4f82-a5e3-38135ecf532f",
1201
+ "metadata": {
1202
+ "execution": {
1203
+ "iopub.execute_input": "2023-10-20T08:45:47.918275Z",
1204
+ "iopub.status.busy": "2023-10-20T08:45:47.917652Z",
1205
+ "iopub.status.idle": "2023-10-20T08:46:06.598970Z",
1206
+ "shell.execute_reply": "2023-10-20T08:46:06.598308Z"
1207
+ },
1208
+ "papermill": {
1209
+ "duration": 19.754456,
1210
+ "end_time": "2023-10-20T08:46:06.600631",
1211
+ "exception": false,
1212
+ "start_time": "2023-10-20T08:45:46.846175",
1213
+ "status": "completed"
1214
+ },
1215
+ "tags": []
1216
+ },
1217
+ "outputs": [
1218
+ {
1219
+ "data": {
1220
+ "text/plain": [
1221
+ "PeftModelForCausalLM(\n",
1222
+ " (base_model): LoraModel(\n",
1223
+ " (model): LlamaForCausalLM(\n",
1224
+ " (model): LlamaModel(\n",
1225
+ " (embed_tokens): ModulesToSaveWrapper(\n",
1226
+ " (original_module): Embedding(32001, 4096)\n",
1227
+ " (modules_to_save): ModuleDict(\n",
1228
+ " (default): Embedding(32001, 4096)\n",
1229
+ " )\n",
1230
+ " )\n",
1231
+ " (layers): ModuleList(\n",
1232
+ " (0-31): 32 x LlamaDecoderLayer(\n",
1233
+ " (self_attn): LlamaAttention(\n",
1234
+ " (q_proj): Linear(\n",
1235
+ " in_features=4096, out_features=4096, bias=False\n",
1236
+ " (lora_dropout): ModuleDict(\n",
1237
+ " (default): Dropout(p=0.05, inplace=False)\n",
1238
+ " )\n",
1239
+ " (lora_A): ModuleDict(\n",
1240
+ " (default): Linear(in_features=4096, out_features=16, bias=False)\n",
1241
+ " )\n",
1242
+ " (lora_B): ModuleDict(\n",
1243
+ " (default): Linear(in_features=16, out_features=4096, bias=False)\n",
1244
+ " )\n",
1245
+ " (lora_embedding_A): ParameterDict()\n",
1246
+ " (lora_embedding_B): ParameterDict()\n",
1247
+ " )\n",
1248
+ " (k_proj): Linear(\n",
1249
+ " in_features=4096, out_features=4096, bias=False\n",
1250
+ " (lora_dropout): ModuleDict(\n",
1251
+ " (default): Dropout(p=0.05, inplace=False)\n",
1252
+ " )\n",
1253
+ " (lora_A): ModuleDict(\n",
1254
+ " (default): Linear(in_features=4096, out_features=16, bias=False)\n",
1255
+ " )\n",
1256
+ " (lora_B): ModuleDict(\n",
1257
+ " (default): Linear(in_features=16, out_features=4096, bias=False)\n",
1258
+ " )\n",
1259
+ " (lora_embedding_A): ParameterDict()\n",
1260
+ " (lora_embedding_B): ParameterDict()\n",
1261
+ " )\n",
1262
+ " (v_proj): Linear(\n",
1263
+ " in_features=4096, out_features=4096, bias=False\n",
1264
+ " (lora_dropout): ModuleDict(\n",
1265
+ " (default): Dropout(p=0.05, inplace=False)\n",
1266
+ " )\n",
1267
+ " (lora_A): ModuleDict(\n",
1268
+ " (default): Linear(in_features=4096, out_features=16, bias=False)\n",
1269
+ " )\n",
1270
+ " (lora_B): ModuleDict(\n",
1271
+ " (default): Linear(in_features=16, out_features=4096, bias=False)\n",
1272
+ " )\n",
1273
+ " (lora_embedding_A): ParameterDict()\n",
1274
+ " (lora_embedding_B): ParameterDict()\n",
1275
+ " )\n",
1276
+ " (o_proj): Linear(\n",
1277
+ " in_features=4096, out_features=4096, bias=False\n",
1278
+ " (lora_dropout): ModuleDict(\n",
1279
+ " (default): Dropout(p=0.05, inplace=False)\n",
1280
+ " )\n",
1281
+ " (lora_A): ModuleDict(\n",
1282
+ " (default): Linear(in_features=4096, out_features=16, bias=False)\n",
1283
+ " )\n",
1284
+ " (lora_B): ModuleDict(\n",
1285
+ " (default): Linear(in_features=16, out_features=4096, bias=False)\n",
1286
+ " )\n",
1287
+ " (lora_embedding_A): ParameterDict()\n",
1288
+ " (lora_embedding_B): ParameterDict()\n",
1289
+ " )\n",
1290
+ " (rotary_emb): LlamaRotaryEmbedding()\n",
1291
+ " )\n",
1292
+ " (mlp): LlamaMLP(\n",
1293
+ " (gate_proj): Linear(\n",
1294
+ " in_features=4096, out_features=11008, bias=False\n",
1295
+ " (lora_dropout): ModuleDict(\n",
1296
+ " (default): Dropout(p=0.05, inplace=False)\n",
1297
+ " )\n",
1298
+ " (lora_A): ModuleDict(\n",
1299
+ " (default): Linear(in_features=4096, out_features=16, bias=False)\n",
1300
+ " )\n",
1301
+ " (lora_B): ModuleDict(\n",
1302
+ " (default): Linear(in_features=16, out_features=11008, bias=False)\n",
1303
+ " )\n",
1304
+ " (lora_embedding_A): ParameterDict()\n",
1305
+ " (lora_embedding_B): ParameterDict()\n",
1306
+ " )\n",
1307
+ " (up_proj): Linear(\n",
1308
+ " in_features=4096, out_features=11008, bias=False\n",
1309
+ " (lora_dropout): ModuleDict(\n",
1310
+ " (default): Dropout(p=0.05, inplace=False)\n",
1311
+ " )\n",
1312
+ " (lora_A): ModuleDict(\n",
1313
+ " (default): Linear(in_features=4096, out_features=16, bias=False)\n",
1314
+ " )\n",
1315
+ " (lora_B): ModuleDict(\n",
1316
+ " (default): Linear(in_features=16, out_features=11008, bias=False)\n",
1317
+ " )\n",
1318
+ " (lora_embedding_A): ParameterDict()\n",
1319
+ " (lora_embedding_B): ParameterDict()\n",
1320
+ " )\n",
1321
+ " (down_proj): Linear(\n",
1322
+ " in_features=11008, out_features=4096, bias=False\n",
1323
+ " (lora_dropout): ModuleDict(\n",
1324
+ " (default): Dropout(p=0.05, inplace=False)\n",
1325
+ " )\n",
1326
+ " (lora_A): ModuleDict(\n",
1327
+ " (default): Linear(in_features=11008, out_features=16, bias=False)\n",
1328
+ " )\n",
1329
+ " (lora_B): ModuleDict(\n",
1330
+ " (default): Linear(in_features=16, out_features=4096, bias=False)\n",
1331
+ " )\n",
1332
+ " (lora_embedding_A): ParameterDict()\n",
1333
+ " (lora_embedding_B): ParameterDict()\n",
1334
+ " )\n",
1335
+ " (act_fn): SiLUActivation()\n",
1336
+ " )\n",
1337
+ " (input_layernorm): LlamaRMSNorm()\n",
1338
+ " (post_attention_layernorm): LlamaRMSNorm()\n",
1339
+ " )\n",
1340
+ " )\n",
1341
+ " (norm): LlamaRMSNorm()\n",
1342
+ " )\n",
1343
+ " (lm_head): ModulesToSaveWrapper(\n",
1344
+ " (original_module): Linear(in_features=4096, out_features=32001, bias=False)\n",
1345
+ " (modules_to_save): ModuleDict(\n",
1346
+ " (default): Linear(in_features=4096, out_features=32001, bias=False)\n",
1347
+ " )\n",
1348
+ " )\n",
1349
+ " )\n",
1350
+ " )\n",
1351
+ ")"
1352
+ ]
1353
+ },
1354
+ "execution_count": 14,
1355
+ "metadata": {},
1356
+ "output_type": "execute_result"
1357
+ }
1358
+ ],
1359
+ "source": [
1360
+ "model.save_pretrained(trained_model_path_lora)\n",
1361
+ "model"
1362
+ ]
1363
+ },
1364
+ {
1365
+ "cell_type": "code",
1366
+ "execution_count": 15,
1367
+ "id": "dea4e68e-57a7-48bd-bad9-f03dfe3f8a06",
1368
+ "metadata": {
1369
+ "execution": {
1370
+ "iopub.execute_input": "2023-10-20T08:46:08.783717Z",
1371
+ "iopub.status.busy": "2023-10-20T08:46:08.782966Z",
1372
+ "iopub.status.idle": "2023-10-20T08:46:09.033621Z",
1373
+ "shell.execute_reply": "2023-10-20T08:46:09.032713Z"
1374
+ },
1375
+ "papermill": {
1376
+ "duration": 1.261451,
1377
+ "end_time": "2023-10-20T08:46:09.035288",
1378
+ "exception": false,
1379
+ "start_time": "2023-10-20T08:46:07.773837",
1380
+ "status": "completed"
1381
+ },
1382
+ "tags": []
1383
+ },
1384
+ "outputs": [
1385
+ {
1386
+ "name": "stdout",
1387
+ "output_type": "stream",
1388
+ "text": [
1389
+ "total 1.2G\r\n",
1390
+ " 512 -rw-r--r-- 1 root 3003 88 Oct 20 08:45 README.md\r\n",
1391
+ "1.0K -rw-r--r-- 1 root 3003 550 Oct 20 08:46 adapter_config.json\r\n",
1392
+ "1.2G -rw-r--r-- 1 root 3003 1.2G Oct 20 08:45 adapter_model.bin\r\n"
1393
+ ]
1394
+ },
1395
+ {
1396
+ "name": "stderr",
1397
+ "output_type": "stream",
1398
+ "text": [
1399
+ "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
1400
+ "To disable this warning, you can either:\n",
1401
+ "\t- Avoid using `tokenizers` before the fork if possible\n",
1402
+ "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
1403
+ ]
1404
+ }
1405
+ ],
1406
+ "source": [
1407
+ "! ls -lash {trained_model_path_lora}"
1408
+ ]
1409
+ },
1410
+ {
1411
+ "cell_type": "code",
1412
+ "execution_count": 16,
1413
+ "id": "09db36b7-ead6-4368-9bfb-13ba1ba800a5",
1414
+ "metadata": {
1415
+ "execution": {
1416
+ "iopub.execute_input": "2023-10-20T08:46:11.071508Z",
1417
+ "iopub.status.busy": "2023-10-20T08:46:11.070823Z",
1418
+ "iopub.status.idle": "2023-10-20T08:47:03.388936Z",
1419
+ "shell.execute_reply": "2023-10-20T08:47:03.388264Z"
1420
+ },
1421
+ "papermill": {
1422
+ "duration": 54.544709,
1423
+ "end_time": "2023-10-20T08:47:04.578229",
1424
+ "exception": false,
1425
+ "start_time": "2023-10-20T08:46:10.033520",
1426
+ "status": "completed"
1427
+ },
1428
+ "tags": []
1429
+ },
1430
+ "outputs": [
1431
+ {
1432
+ "data": {
1433
+ "text/plain": [
1434
+ "LlamaForCausalLM(\n",
1435
+ " (model): LlamaModel(\n",
1436
+ " (embed_tokens): Embedding(32001, 4096)\n",
1437
+ " (layers): ModuleList(\n",
1438
+ " (0-31): 32 x LlamaDecoderLayer(\n",
1439
+ " (self_attn): LlamaAttention(\n",
1440
+ " (q_proj): Linear(in_features=4096, out_features=4096, bias=False)\n",
1441
+ " (k_proj): Linear(in_features=4096, out_features=4096, bias=False)\n",
1442
+ " (v_proj): Linear(in_features=4096, out_features=4096, bias=False)\n",
1443
+ " (o_proj): Linear(in_features=4096, out_features=4096, bias=False)\n",
1444
+ " (rotary_emb): LlamaRotaryEmbedding()\n",
1445
+ " )\n",
1446
+ " (mlp): LlamaMLP(\n",
1447
+ " (gate_proj): Linear(in_features=4096, out_features=11008, bias=False)\n",
1448
+ " (up_proj): Linear(in_features=4096, out_features=11008, bias=False)\n",
1449
+ " (down_proj): Linear(in_features=11008, out_features=4096, bias=False)\n",
1450
+ " (act_fn): SiLUActivation()\n",
1451
+ " )\n",
1452
+ " (input_layernorm): LlamaRMSNorm()\n",
1453
+ " (post_attention_layernorm): LlamaRMSNorm()\n",
1454
+ " )\n",
1455
+ " )\n",
1456
+ " (norm): LlamaRMSNorm()\n",
1457
+ " )\n",
1458
+ " (lm_head): Linear(in_features=4096, out_features=32001, bias=False)\n",
1459
+ ")"
1460
+ ]
1461
+ },
1462
+ "execution_count": 16,
1463
+ "metadata": {},
1464
+ "output_type": "execute_result"
1465
+ }
1466
+ ],
1467
+ "source": [
1468
+ "model = model.merge_and_unload().half()\n",
1469
+ "model"
1470
+ ]
1471
+ },
1472
+ {
1473
+ "cell_type": "code",
1474
+ "execution_count": 17,
1475
+ "id": "270a9a72-3a12-4d83-aa7d-2d167cb28cb4",
1476
+ "metadata": {
1477
+ "execution": {
1478
+ "iopub.execute_input": "2023-10-20T08:47:06.891191Z",
1479
+ "iopub.status.busy": "2023-10-20T08:47:06.890837Z",
1480
+ "iopub.status.idle": "2023-10-20T08:47:07.137287Z",
1481
+ "shell.execute_reply": "2023-10-20T08:47:07.136391Z"
1482
+ },
1483
+ "papermill": {
1484
+ "duration": 1.395578,
1485
+ "end_time": "2023-10-20T08:47:07.138853",
1486
+ "exception": false,
1487
+ "start_time": "2023-10-20T08:47:05.743275",
1488
+ "status": "completed"
1489
+ },
1490
+ "tags": []
1491
+ },
1492
+ "outputs": [
1493
+ {
1494
+ "name": "stdout",
1495
+ "output_type": "stream",
1496
+ "text": [
1497
+ "total 0\r\n",
1498
+ "drwxr-xr-x 1 root 3003 0 Oct 20 04:34 checkpoints\r\n",
1499
+ "drwxr-xr-x 1 root 3003 0 Oct 20 04:34 lora\r\n",
1500
+ "drwxr-xr-x 1 root 3003 0 Oct 20 04:29 src\r\n"
1501
+ ]
1502
+ },
1503
+ {
1504
+ "name": "stderr",
1505
+ "output_type": "stream",
1506
+ "text": [
1507
+ "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
1508
+ "To disable this warning, you can either:\n",
1509
+ "\t- Avoid using `tokenizers` before the fork if possible\n",
1510
+ "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
1511
+ ]
1512
+ }
1513
+ ],
1514
+ "source": [
1515
+ "! ls -l {trained_model_path}"
1516
+ ]
1517
+ },
1518
+ {
1519
+ "cell_type": "code",
1520
+ "execution_count": 18,
1521
+ "id": "260e9d79-6eb8-4516-bf8f-825a25606391",
1522
+ "metadata": {
1523
+ "execution": {
1524
+ "iopub.execute_input": "2023-10-20T08:47:09.318793Z",
1525
+ "iopub.status.busy": "2023-10-20T08:47:09.317959Z",
1526
+ "iopub.status.idle": "2023-10-20T08:50:32.295853Z",
1527
+ "shell.execute_reply": "2023-10-20T08:50:32.295187Z"
1528
+ },
1529
+ "papermill": {
1530
+ "duration": 205.2148,
1531
+ "end_time": "2023-10-20T08:50:33.467533",
1532
+ "exception": false,
1533
+ "start_time": "2023-10-20T08:47:08.252733",
1534
+ "status": "completed"
1535
+ },
1536
+ "tags": []
1537
+ },
1538
+ "outputs": [
1539
+ {
1540
+ "data": {
1541
+ "text/plain": [
1542
+ "('/content/artifacts/tokenizer_config.json',\n",
1543
+ " '/content/artifacts/special_tokens_map.json',\n",
1544
+ " '/content/artifacts/tokenizer.model',\n",
1545
+ " '/content/artifacts/added_tokens.json',\n",
1546
+ " '/content/artifacts/tokenizer.json')"
1547
+ ]
1548
+ },
1549
+ "execution_count": 18,
1550
+ "metadata": {},
1551
+ "output_type": "execute_result"
1552
+ }
1553
+ ],
1554
+ "source": [
1555
+ "model.save_pretrained(trained_model_path)\n",
1556
+ "tokenizer.save_pretrained(trained_model_path)"
1557
+ ]
1558
+ },
1559
+ {
1560
+ "cell_type": "code",
1561
+ "execution_count": 19,
1562
+ "id": "a575ff52-a6ff-4322-af8e-2629c0e110a0",
1563
+ "metadata": {
1564
+ "execution": {
1565
+ "iopub.execute_input": "2023-10-20T08:50:35.318692Z",
1566
+ "iopub.status.busy": "2023-10-20T08:50:35.317875Z",
1567
+ "iopub.status.idle": "2023-10-20T08:50:35.798065Z",
1568
+ "shell.execute_reply": "2023-10-20T08:50:35.797213Z"
1569
+ },
1570
+ "papermill": {
1571
+ "duration": 1.406588,
1572
+ "end_time": "2023-10-20T08:50:35.799961",
1573
+ "exception": false,
1574
+ "start_time": "2023-10-20T08:50:34.393373",
1575
+ "status": "completed"
1576
+ },
1577
+ "tags": []
1578
+ },
1579
+ "outputs": [
1580
+ {
1581
+ "name": "stdout",
1582
+ "output_type": "stream",
1583
+ "text": [
1584
+ "total 13G\r\n",
1585
+ " 512 -rw-r--r-- 1 root 3003 21 Oct 20 08:50 added_tokens.json\r\n",
1586
+ " 0 drwxr-xr-x 1 root 3003 0 Oct 20 04:34 checkpoints\r\n",
1587
+ "1.0K -rw-r--r-- 1 root 3003 648 Oct 20 08:47 config.json\r\n",
1588
+ " 512 -rw-r--r-- 1 root 3003 183 Oct 20 08:47 generation_config.json\r\n",
1589
+ " 0 drwxr-xr-x 1 root 3003 0 Oct 20 04:34 lora\r\n",
1590
+ "9.3G -rw-r--r-- 1 root 3003 9.3G Oct 20 08:47 pytorch_model-00001-of-00002.bin\r\n",
1591
+ "3.3G -rw-r--r-- 1 root 3003 3.3G Oct 20 08:49 pytorch_model-00002-of-00002.bin\r\n",
1592
+ " 24K -rw-r--r-- 1 root 3003 24K Oct 20 08:50 pytorch_model.bin.index.json\r\n",
1593
+ "1.0K -rw-r--r-- 1 root 3003 552 Oct 20 08:50 special_tokens_map.json\r\n",
1594
+ " 0 drwxr-xr-x 1 root 3003 0 Oct 20 04:29 src\r\n",
1595
+ "1.8M -rw-r--r-- 1 root 3003 1.8M Oct 20 08:50 tokenizer.json\r\n",
1596
+ "489K -rw-r--r-- 1 root 3003 489K Oct 20 08:50 tokenizer.model\r\n",
1597
+ "1.5K -rw-r--r-- 1 root 3003 1.1K Oct 20 08:50 tokenizer_config.json\r\n"
1598
+ ]
1599
+ },
1600
+ {
1601
+ "name": "stderr",
1602
+ "output_type": "stream",
1603
+ "text": [
1604
+ "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
1605
+ "To disable this warning, you can either:\n",
1606
+ "\t- Avoid using `tokenizers` before the fork if possible\n",
1607
+ "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n",
1608
+ "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
1609
+ "To disable this warning, you can either:\n",
1610
+ "\t- Avoid using `tokenizers` before the fork if possible\n",
1611
+ "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
1612
+ ]
1613
+ },
1614
+ {
1615
+ "name": "stdout",
1616
+ "output_type": "stream",
1617
+ "text": [
1618
+ "total 13G\r\n",
1619
+ "2.0K -rw-r--r-- 1 root 3003 1.6K Oct 5 05:21 .gitattributes\r\n",
1620
+ "7.0K -rw-r--r-- 1 root 3003 6.9K Oct 5 05:21 LICENSE.txt\r\n",
1621
+ " 11K -rw-r--r-- 1 root 3003 11K Oct 5 05:21 README.md\r\n",
1622
+ "1.2M -rw-r--r-- 1 root 3003 1.2M Oct 5 05:21 Responsible-Use-Guide.pdf\r\n",
1623
+ "5.0K -rw-r--r-- 1 root 3003 4.7K Oct 5 05:21 USE_POLICY.md\r\n",
1624
+ "1.0K -rw-r--r-- 1 root 3003 609 Oct 5 05:21 config.json\r\n",
1625
+ " 512 -rw-r--r-- 1 root 3003 188 Oct 5 05:21 generation_config.json\r\n",
1626
+ "9.3G -rw-r--r-- 1 root 3003 9.3G Oct 5 05:22 model-00001-of-00002.safetensors\r\n",
1627
+ "3.3G -rw-r--r-- 1 root 3003 3.3G Oct 5 05:22 model-00002-of-00002.safetensors\r\n",
1628
+ " 27K -rw-r--r-- 1 root 3003 27K Oct 5 05:21 model.safetensors.index.json\r\n",
1629
+ " 512 -rw-r--r-- 1 root 3003 414 Oct 5 05:21 special_tokens_map.json\r\n",
1630
+ " 0 drwxr-xr-x 1 root 3003 0 Oct 20 08:50 src\r\n",
1631
+ "1.8M -rw-r--r-- 1 root 3003 1.8M Oct 5 05:21 tokenizer.json\r\n",
1632
+ "489K -rw-r--r-- 1 root 3003 489K Oct 5 05:21 tokenizer.model\r\n",
1633
+ "1.0K -rw-r--r-- 1 root 3003 776 Oct 5 05:21 tokenizer_config.json\r\n"
1634
+ ]
1635
+ }
1636
+ ],
1637
+ "source": [
1638
+ "! ls -lash {trained_model_path}\n",
1639
+ "! ls -lash {model_path}"
1640
+ ]
1641
+ },
1642
+ {
1643
+ "cell_type": "code",
1644
+ "execution_count": 20,
1645
+ "id": "6d90a920-fb22-4291-8466-411ff41e31be",
1646
+ "metadata": {
1647
+ "execution": {
1648
+ "iopub.execute_input": "2023-10-20T08:50:37.854282Z",
1649
+ "iopub.status.busy": "2023-10-20T08:50:37.853439Z",
1650
+ "iopub.status.idle": "2023-10-20T08:50:38.103769Z",
1651
+ "shell.execute_reply": "2023-10-20T08:50:38.102831Z"
1652
+ },
1653
+ "papermill": {
1654
+ "duration": 1.245211,
1655
+ "end_time": "2023-10-20T08:50:38.105374",
1656
+ "exception": false,
1657
+ "start_time": "2023-10-20T08:50:36.860163",
1658
+ "status": "completed"
1659
+ },
1660
+ "tags": []
1661
+ },
1662
+ "outputs": [
1663
+ {
1664
+ "name": "stdout",
1665
+ "output_type": "stream",
1666
+ "text": [
1667
+ "total 13G\r\n",
1668
+ " 512 -rw-r--r-- 1 root 3003 21 Oct 20 08:50 added_tokens.json\r\n",
1669
+ " 0 drwxr-xr-x 1 root 3003 0 Oct 20 04:34 checkpoints\r\n",
1670
+ "1.0K -rw-r--r-- 1 root 3003 648 Oct 20 08:47 config.json\r\n",
1671
+ " 512 -rw-r--r-- 1 root 3003 183 Oct 20 08:47 generation_config.json\r\n",
1672
+ " 0 drwxr-xr-x 1 root 3003 0 Oct 20 04:34 lora\r\n",
1673
+ "9.3G -rw-r--r-- 1 root 3003 9.3G Oct 20 08:47 pytorch_model-00001-of-00002.bin\r\n",
1674
+ "3.3G -rw-r--r-- 1 root 3003 3.3G Oct 20 08:49 pytorch_model-00002-of-00002.bin\r\n",
1675
+ " 24K -rw-r--r-- 1 root 3003 24K Oct 20 08:50 pytorch_model.bin.index.json\r\n",
1676
+ "1.0K -rw-r--r-- 1 root 3003 552 Oct 20 08:50 special_tokens_map.json\r\n",
1677
+ " 0 drwxr-xr-x 1 root 3003 0 Oct 20 04:29 src\r\n",
1678
+ "1.8M -rw-r--r-- 1 root 3003 1.8M Oct 20 08:50 tokenizer.json\r\n",
1679
+ "489K -rw-r--r-- 1 root 3003 489K Oct 20 08:50 tokenizer.model\r\n",
1680
+ "1.5K -rw-r--r-- 1 root 3003 1.1K Oct 20 08:50 tokenizer_config.json\r\n"
1681
+ ]
1682
+ },
1683
+ {
1684
+ "name": "stderr",
1685
+ "output_type": "stream",
1686
+ "text": [
1687
+ "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
1688
+ "To disable this warning, you can either:\n",
1689
+ "\t- Avoid using `tokenizers` before the fork if possible\n",
1690
+ "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
1691
+ ]
1692
+ }
1693
+ ],
1694
+ "source": [
1695
+ "! ls -lash {trained_model_path}"
1696
+ ]
1697
+ },
1698
+ {
1699
+ "cell_type": "code",
1700
+ "execution_count": 21,
1701
+ "id": "202a694a",
1702
+ "metadata": {
1703
+ "execution": {
1704
+ "iopub.execute_input": "2023-10-20T08:50:39.994565Z",
1705
+ "iopub.status.busy": "2023-10-20T08:50:39.993719Z"
1706
+ },
1707
+ "papermill": {
1708
+ "duration": null,
1709
+ "end_time": null,
1710
+ "exception": false,
1711
+ "start_time": "2023-10-20T08:50:39.040596",
1712
+ "status": "running"
1713
+ },
1714
+ "tags": []
1715
+ },
1716
+ "outputs": [
1717
+ {
1718
+ "data": {
1719
+ "application/vnd.jupyter.widget-view+json": {
1720
+ "model_id": "79038243991f481395b9433f4dcf59b5",
1721
+ "version_major": 2,
1722
+ "version_minor": 0
1723
+ },
1724
+ "text/plain": [
1725
+ "pytorch_model-00001-of-00002.bin: 0%| | 0.00/9.98G [00:00<?, ?B/s]"
1726
+ ]
1727
+ },
1728
+ "metadata": {},
1729
+ "output_type": "display_data"
1730
+ },
1731
+ {
1732
+ "data": {
1733
+ "application/vnd.jupyter.widget-view+json": {
1734
+ "model_id": "f9a6e0650c66418d8b8a8370e9a956e0",
1735
+ "version_major": 2,
1736
+ "version_minor": 0
1737
+ },
1738
+ "text/plain": [
1739
+ "pytorch_model-00002-of-00002.bin: 0%| | 0.00/3.50G [00:00<?, ?B/s]"
1740
+ ]
1741
+ },
1742
+ "metadata": {},
1743
+ "output_type": "display_data"
1744
+ },
1745
+ {
1746
+ "data": {
1747
+ "application/vnd.jupyter.widget-view+json": {
1748
+ "model_id": "c4b11f630b62451e9f885874a6247781",
1749
+ "version_major": 2,
1750
+ "version_minor": 0
1751
+ },
1752
+ "text/plain": [
1753
+ "Upload 2 LFS files: 0%| | 0/2 [00:00<?, ?it/s]"
1754
+ ]
1755
+ },
1756
+ "metadata": {},
1757
+ "output_type": "display_data"
1758
+ }
1759
+ ],
1760
+ "source": [
1761
+ "from huggingface_hub import HfApi\n",
1762
+ "import shutil\n",
1763
+ "\n",
1764
+ "tokenizer_model_path_base = Path(model_path) / \"tokenizer.model\"\n",
1765
+ "tokenizer_model_path_trained = Path(trained_model_path) / \"tokenizer.model\"\n",
1766
+ "if tokenizer_model_path_base.exists() and not tokenizer_model_path_trained.exists():\n",
1767
+ " shutil.copy(tokenizer_model_path_base, tokenizer_model_path_trained)\n",
1768
+ "\n",
1769
+ "repo_id = params.get(\"push_to_hub\")\n",
1770
+ "if repo_id:\n",
1771
+ " model.push_to_hub(repo_id)\n",
1772
+ " tokenizer.push_to_hub(repo_id)\n",
1773
+ " hf_api = HfApi()\n",
1774
+ " # Upload tokenizer.model if it was in base model\n",
1775
+ " if tokenizer_model_path_base.exists():\n",
1776
+ " hf_api.upload_file(\n",
1777
+ " path_or_fileobj=tokenizer_model_path_base,\n",
1778
+ " path_in_repo=tokenizer_model_path_base.name,\n",
1779
+ " repo_id=repo_id,\n",
1780
+ " )\n",
1781
+ " logs_path = Path(\"/content/artifacts/src/train.ipynb\")\n",
1782
+ " if logs_path.exists():\n",
1783
+ " hf_api.upload_file(\n",
1784
+ " path_or_fileobj=logs_path,\n",
1785
+ " path_in_repo=logs_path.name,\n",
1786
+ " repo_id=repo_id,\n",
1787
+ " )\n"
1788
+ ]
1789
+ },
1790
+ {
1791
+ "cell_type": "code",
1792
+ "execution_count": null,
1793
+ "id": "d1528b32-3e4d-463c-9473-97908d7dfcf5",
1794
+ "metadata": {
1795
+ "papermill": {
1796
+ "duration": null,
1797
+ "end_time": null,
1798
+ "exception": null,
1799
+ "start_time": null,
1800
+ "status": "pending"
1801
+ },
1802
+ "tags": []
1803
+ },
1804
+ "outputs": [],
1805
+ "source": [
1806
+ "! nvidia-smi"
1807
+ ]
1808
+ },
1809
+ {
1810
+ "cell_type": "code",
1811
+ "execution_count": null,
1812
+ "id": "04651d47",
1813
+ "metadata": {
1814
+ "papermill": {
1815
+ "duration": null,
1816
+ "end_time": null,
1817
+ "exception": null,
1818
+ "start_time": null,
1819
+ "status": "pending"
1820
+ },
1821
+ "tags": []
1822
+ },
1823
+ "outputs": [],
1824
+ "source": [
1825
+ "inference_prompt = params.get(\"inference_prompt_template\")\n",
1826
+ "if inference_prompt:\n",
1827
+ " model = AutoModelForCausalLM.from_pretrained(\n",
1828
+ " trained_model_path, torch_dtype=torch.float16, device_map=\"auto\", trust_remote_code=True,\n",
1829
+ " load_in_8bit=True)\n",
1830
+ " device = \"cuda\"\n",
1831
+ " model_inputs = tokenizer([inference_prompt.format_map(data[\"train\"][0])],\n",
1832
+ " return_tensors=\"pt\").to(device)\n",
1833
+ "\n",
1834
+ " generated_ids = model.generate(**model_inputs,\n",
1835
+ " max_new_tokens=300, temperature=0.6)\n",
1836
+ "\n",
1837
+ " print(tokenizer.decode(generated_ids[0], skip_special_tokens=True))"
1838
+ ]
1839
+ },
1840
+ {
1841
+ "cell_type": "code",
1842
+ "execution_count": null,
1843
+ "id": "f5cc20fc-5aaf-4e4c-92f8-cce0ed0fe1ee",
1844
+ "metadata": {
1845
+ "papermill": {
1846
+ "duration": null,
1847
+ "end_time": null,
1848
+ "exception": null,
1849
+ "start_time": null,
1850
+ "status": "pending"
1851
+ },
1852
+ "tags": []
1853
+ },
1854
+ "outputs": [],
1855
+ "source": [
1856
+ "repo_id = params.get(\"push_to_hub\")\n",
1857
+ "if repo_id:\n",
1858
+ " hf_api = HfApi()\n",
1859
+ " logs_path = Path(\"/content/artifacts/src/train.ipynb\")\n",
1860
+ " if logs_path.exists():\n",
1861
+ " hf_api.upload_file(\n",
1862
+ " path_or_fileobj=logs_path,\n",
1863
+ " path_in_repo=logs_path.name,\n",
1864
+ " repo_id=repo_id,\n",
1865
+ " )"
1866
+ ]
1867
+ }
1868
+ ],
1869
+ "metadata": {
1870
+ "kernelspec": {
1871
+ "display_name": "Python 3 (ipykernel)",
1872
+ "language": "python",
1873
+ "name": "python3"
1874
+ },
1875
+ "language_info": {
1876
+ "codemirror_mode": {
1877
+ "name": "ipython",
1878
+ "version": 3
1879
+ },
1880
+ "file_extension": ".py",
1881
+ "mimetype": "text/x-python",
1882
+ "name": "python",
1883
+ "nbconvert_exporter": "python",
1884
+ "pygments_lexer": "ipython3",
1885
+ "version": "3.10.12"
1886
+ },
1887
+ "papermill": {
1888
+ "default_parameters": {},
1889
+ "duration": null,
1890
+ "end_time": null,
1891
+ "environment_variables": {},
1892
+ "exception": null,
1893
+ "input_path": "/content/src/train.ipynb",
1894
+ "output_path": "/content/artifacts/src/train.ipynb",
1895
+ "parameters": {},
1896
+ "start_time": "2023-10-20T04:29:06.977195",
1897
+ "version": "2.4.0"
1898
+ }
1899
+ },
1900
+ "nbformat": 4,
1901
+ "nbformat_minor": 5
1902
+ }