samos123 commited on
Commit
9afafa3
1 Parent(s): 01626ba

Upload train.ipynb with huggingface_hub

Browse files
Files changed (1) hide show
  1. train.ipynb +1741 -0
train.ipynb ADDED
@@ -0,0 +1,1741 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "9c3e4532",
6
+ "metadata": {
7
+ "papermill": {
8
+ "duration": 0.941841,
9
+ "end_time": "2023-10-22T00:33:18.570079",
10
+ "exception": false,
11
+ "start_time": "2023-10-22T00:33:17.628238",
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-22T00:33:20.446183Z",
39
+ "iopub.status.busy": "2023-10-22T00:33:20.445407Z",
40
+ "iopub.status.idle": "2023-10-22T00:33:20.458365Z",
41
+ "shell.execute_reply": "2023-10-22T00:33:20.457645Z"
42
+ },
43
+ "papermill": {
44
+ "duration": 0.922166,
45
+ "end_time": "2023-10-22T00:33:20.459935",
46
+ "exception": false,
47
+ "start_time": "2023-10-22T00:33:19.537769",
48
+ "status": "completed"
49
+ },
50
+ "tags": []
51
+ },
52
+ "outputs": [
53
+ {
54
+ "data": {
55
+ "text/plain": [
56
+ "{'dataset_urls': 'https://huggingface.co/datasets/weaviate/WithRetrieval-Random-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-Random-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-22T00:33:22.506977Z",
96
+ "iopub.status.busy": "2023-10-22T00:33:22.506580Z",
97
+ "iopub.status.idle": "2023-10-22T00:33:25.872338Z",
98
+ "shell.execute_reply": "2023-10-22T00:33:25.871610Z"
99
+ },
100
+ "papermill": {
101
+ "duration": 4.499567,
102
+ "end_time": "2023-10-22T00:33:25.873924",
103
+ "exception": false,
104
+ "start_time": "2023-10-22T00:33:21.374357",
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-Random-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": "54ab7cdf53f047abbc1942959916933d",
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": "4968d1b11acf4422acba4770dba3de9e",
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": "7020c45b6867427cb4ed9890f1e043c0",
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": "75cbf6ed8b2844cc896d35ee9abd7a36",
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-22T00:33:27.705007Z",
210
+ "iopub.status.busy": "2023-10-22T00:33:27.704156Z",
211
+ "iopub.status.idle": "2023-10-22T00:37:47.218387Z",
212
+ "shell.execute_reply": "2023-10-22T00:37:47.217711Z"
213
+ },
214
+ "papermill": {
215
+ "duration": 261.366899,
216
+ "end_time": "2023-10-22T00:37:48.179776",
217
+ "exception": false,
218
+ "start_time": "2023-10-22T00:33:26.812877",
219
+ "status": "completed"
220
+ },
221
+ "tags": []
222
+ },
223
+ "outputs": [
224
+ {
225
+ "data": {
226
+ "application/vnd.jupyter.widget-view+json": {
227
+ "model_id": "f129585f22ba4d51a8f53545a1c8154b",
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-22T00:37:50.062611Z",
298
+ "iopub.status.busy": "2023-10-22T00:37:50.061711Z",
299
+ "iopub.status.idle": "2023-10-22T00:37:50.067405Z",
300
+ "shell.execute_reply": "2023-10-22T00:37:50.066788Z"
301
+ },
302
+ "papermill": {
303
+ "duration": 0.983273,
304
+ "end_time": "2023-10-22T00:37:50.068938",
305
+ "exception": false,
306
+ "start_time": "2023-10-22T00:37:49.085665",
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-22T00:37:51.917719Z",
360
+ "iopub.status.busy": "2023-10-22T00:37:51.916981Z",
361
+ "iopub.status.idle": "2023-10-22T00:37:51.922095Z",
362
+ "shell.execute_reply": "2023-10-22T00:37:51.921472Z"
363
+ },
364
+ "papermill": {
365
+ "duration": 0.926289,
366
+ "end_time": "2023-10-22T00:37:51.923526",
367
+ "exception": false,
368
+ "start_time": "2023-10-22T00:37:50.997237",
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-22T00:37:55.546481Z",
423
+ "iopub.status.busy": "2023-10-22T00:37:55.545754Z",
424
+ "iopub.status.idle": "2023-10-22T00:37:56.401487Z",
425
+ "shell.execute_reply": "2023-10-22T00:37:56.400701Z"
426
+ },
427
+ "papermill": {
428
+ "duration": 1.720149,
429
+ "end_time": "2023-10-22T00:37:56.403253",
430
+ "exception": false,
431
+ "start_time": "2023-10-22T00:37:54.683104",
432
+ "status": "completed"
433
+ },
434
+ "tags": []
435
+ },
436
+ "outputs": [
437
+ {
438
+ "name": "stdout",
439
+ "output_type": "stream",
440
+ "text": [
441
+ "Sun Oct 22 00:37:55 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 60C P0 31W / 72W | 3570MiB / 23034MiB | 0% Default |\r\n",
451
+ "| | | N/A |\r\n",
452
+ "+-------------------------------+----------------------+----------------------+\r\n",
453
+ "| 1 NVIDIA L4 Off | 00000000:00:05.0 Off | 0 |\r\n",
454
+ "| N/A 58C P0 30W / 72W | 4096MiB / 23034MiB | 0% Default |\r\n",
455
+ "| | | N/A |\r\n",
456
+ "+-------------------------------+----------------------+----------------------+\r\n"
457
+ ]
458
+ },
459
+ {
460
+ "name": "stdout",
461
+ "output_type": "stream",
462
+ "text": [
463
+ "| 2 NVIDIA L4 Off | 00000000:00:06.0 Off | 0 |\r\n",
464
+ "| N/A 56C 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 60C P0 32W / 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.050693,
492
+ "end_time": "2023-10-22T00:37:58.385886",
493
+ "exception": false,
494
+ "start_time": "2023-10-22T00:37:57.335193",
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-22T00:38:00.598168Z",
510
+ "iopub.status.busy": "2023-10-22T00:38:00.597836Z",
511
+ "iopub.status.idle": "2023-10-22T00:38:00.619438Z",
512
+ "shell.execute_reply": "2023-10-22T00:38:00.618759Z"
513
+ },
514
+ "papermill": {
515
+ "duration": 1.264362,
516
+ "end_time": "2023-10-22T00:38:00.620931",
517
+ "exception": false,
518
+ "start_time": "2023-10-22T00:37:59.356569",
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-22T00:38:04.767476Z",
597
+ "iopub.status.busy": "2023-10-22T00:38:04.766754Z",
598
+ "iopub.status.idle": "2023-10-22T00:38:11.742834Z",
599
+ "shell.execute_reply": "2023-10-22T00:38:11.742183Z"
600
+ },
601
+ "papermill": {
602
+ "duration": 7.967639,
603
+ "end_time": "2023-10-22T00:38:11.744550",
604
+ "exception": false,
605
+ "start_time": "2023-10-22T00:38:03.776911",
606
+ "status": "completed"
607
+ },
608
+ "tags": []
609
+ },
610
+ "outputs": [
611
+ {
612
+ "data": {
613
+ "application/vnd.jupyter.widget-view+json": {
614
+ "model_id": "360f10151af048819a1171718b9a9448",
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-22T00:38:13.734857Z",
653
+ "iopub.status.busy": "2023-10-22T00:38:13.734113Z",
654
+ "iopub.status.idle": "2023-10-22T00:39:46.961024Z",
655
+ "shell.execute_reply": "2023-10-22T00:39:46.960315Z"
656
+ },
657
+ "papermill": {
658
+ "duration": 95.416685,
659
+ "end_time": "2023-10-22T00:39:48.174721",
660
+ "exception": false,
661
+ "start_time": "2023-10-22T00:38:12.758036",
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-22T00:39:50.107840Z",
718
+ "iopub.status.busy": "2023-10-22T00:39:50.106880Z",
719
+ "iopub.status.idle": "2023-10-22T00:39:50.125663Z",
720
+ "shell.execute_reply": "2023-10-22T00:39:50.125045Z"
721
+ },
722
+ "papermill": {
723
+ "duration": 0.989767,
724
+ "end_time": "2023-10-22T00:39:50.127644",
725
+ "exception": false,
726
+ "start_time": "2023-10-22T00:39:49.137877",
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/Oct22_00-39-50_wgqlg-withretrieval-random-train-80-v2-modeller-gdth6,\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-22T00:39:52.733240Z",
869
+ "iopub.status.busy": "2023-10-22T00:39:52.732528Z",
870
+ "iopub.status.idle": "2023-10-22T00:39:52.735862Z",
871
+ "shell.execute_reply": "2023-10-22T00:39:52.735243Z"
872
+ },
873
+ "papermill": {
874
+ "duration": 1.548798,
875
+ "end_time": "2023-10-22T00:39:52.737292",
876
+ "exception": false,
877
+ "start_time": "2023-10-22T00:39:51.188494",
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-22T00:39:54.811779Z",
895
+ "iopub.status.busy": "2023-10-22T00:39:54.811180Z",
896
+ "iopub.status.idle": "2023-10-22T00:39:55.100635Z",
897
+ "shell.execute_reply": "2023-10-22T00:39:55.099818Z"
898
+ },
899
+ "papermill": {
900
+ "duration": 1.304129,
901
+ "end_time": "2023-10-22T00:39:55.102252",
902
+ "exception": false,
903
+ "start_time": "2023-10-22T00:39:53.798123",
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-22T00:39:57.094945Z",
931
+ "iopub.status.busy": "2023-10-22T00:39:57.094146Z",
932
+ "iopub.status.idle": "2023-10-22T04:52:40.595862Z",
933
+ "shell.execute_reply": "2023-10-22T04:52:40.595167Z"
934
+ },
935
+ "papermill": {
936
+ "duration": 15165.144124,
937
+ "end_time": "2023-10-22T04:52:41.273343",
938
+ "exception": false,
939
+ "start_time": "2023-10-22T00:39:56.129219",
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:12:34, 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.069300</td>\n",
972
+ " </tr>\n",
973
+ " <tr>\n",
974
+ " <td>100</td>\n",
975
+ " <td>0.515300</td>\n",
976
+ " </tr>\n",
977
+ " <tr>\n",
978
+ " <td>150</td>\n",
979
+ " <td>0.273700</td>\n",
980
+ " </tr>\n",
981
+ " <tr>\n",
982
+ " <td>200</td>\n",
983
+ " <td>0.173300</td>\n",
984
+ " </tr>\n",
985
+ " <tr>\n",
986
+ " <td>250</td>\n",
987
+ " <td>0.118800</td>\n",
988
+ " </tr>\n",
989
+ " <tr>\n",
990
+ " <td>300</td>\n",
991
+ " <td>0.084200</td>\n",
992
+ " </tr>\n",
993
+ " <tr>\n",
994
+ " <td>350</td>\n",
995
+ " <td>0.065800</td>\n",
996
+ " </tr>\n",
997
+ " <tr>\n",
998
+ " <td>400</td>\n",
999
+ " <td>0.054500</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.044200</td>\n",
1008
+ " </tr>\n",
1009
+ " <tr>\n",
1010
+ " <td>550</td>\n",
1011
+ " <td>0.040000</td>\n",
1012
+ " </tr>\n",
1013
+ " <tr>\n",
1014
+ " <td>600</td>\n",
1015
+ " <td>0.039400</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.034100</td>\n",
1024
+ " </tr>\n",
1025
+ " <tr>\n",
1026
+ " <td>750</td>\n",
1027
+ " <td>0.034400</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.026700</td>\n",
1040
+ " </tr>\n",
1041
+ " <tr>\n",
1042
+ " <td>950</td>\n",
1043
+ " <td>0.027900</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.026300</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.026100</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.025600</td>\n",
1080
+ " </tr>\n",
1081
+ " <tr>\n",
1082
+ " <td>1450</td>\n",
1083
+ " <td>0.024300</td>\n",
1084
+ " </tr>\n",
1085
+ " <tr>\n",
1086
+ " <td>1500</td>\n",
1087
+ " <td>0.023100</td>\n",
1088
+ " </tr>\n",
1089
+ " <tr>\n",
1090
+ " <td>1550</td>\n",
1091
+ " <td>0.024800</td>\n",
1092
+ " </tr>\n",
1093
+ " <tr>\n",
1094
+ " <td>1600</td>\n",
1095
+ " <td>0.023300</td>\n",
1096
+ " </tr>\n",
1097
+ " <tr>\n",
1098
+ " <td>1650</td>\n",
1099
+ " <td>0.019400</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.019300</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.018600</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.019300</td>\n",
1144
+ " </tr>\n",
1145
+ " <tr>\n",
1146
+ " <td>2250</td>\n",
1147
+ " <td>0.019800</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.07075354540412868, metrics={'train_runtime': 15162.9574, 'train_samples_per_second': 0.631, 'train_steps_per_second': 0.158, 'total_flos': 3.0420974601928704e+17, 'train_loss': 0.07075354540412868, '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-22T04:52:43.424539Z",
1204
+ "iopub.status.busy": "2023-10-22T04:52:43.423767Z",
1205
+ "iopub.status.idle": "2023-10-22T04:53:03.150108Z",
1206
+ "shell.execute_reply": "2023-10-22T04:53:03.149387Z"
1207
+ },
1208
+ "papermill": {
1209
+ "duration": 21.909882,
1210
+ "end_time": "2023-10-22T04:53:04.171757",
1211
+ "exception": false,
1212
+ "start_time": "2023-10-22T04:52:42.261875",
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-22T04:53:06.109528Z",
1371
+ "iopub.status.busy": "2023-10-22T04:53:06.108736Z",
1372
+ "iopub.status.idle": "2023-10-22T04:53:06.356699Z",
1373
+ "shell.execute_reply": "2023-10-22T04:53:06.355856Z"
1374
+ },
1375
+ "papermill": {
1376
+ "duration": 1.205767,
1377
+ "end_time": "2023-10-22T04:53:06.358311",
1378
+ "exception": false,
1379
+ "start_time": "2023-10-22T04:53:05.152544",
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 22 04:52 README.md\r\n",
1391
+ "1.0K -rw-r--r-- 1 root 3003 550 Oct 22 04:53 adapter_config.json\r\n",
1392
+ "1.2G -rw-r--r-- 1 root 3003 1.2G Oct 22 04:52 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-22T04:53:08.298375Z",
1417
+ "iopub.status.busy": "2023-10-22T04:53:08.297543Z",
1418
+ "iopub.status.idle": "2023-10-22T04:54:00.325080Z",
1419
+ "shell.execute_reply": "2023-10-22T04:54:00.324374Z"
1420
+ },
1421
+ "papermill": {
1422
+ "duration": 54.039738,
1423
+ "end_time": "2023-10-22T04:54:01.415212",
1424
+ "exception": false,
1425
+ "start_time": "2023-10-22T04:53:07.375474",
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-22T04:54:03.491633Z",
1479
+ "iopub.status.busy": "2023-10-22T04:54:03.490944Z",
1480
+ "iopub.status.idle": "2023-10-22T04:54:03.732210Z",
1481
+ "shell.execute_reply": "2023-10-22T04:54:03.731396Z"
1482
+ },
1483
+ "papermill": {
1484
+ "duration": 1.235829,
1485
+ "end_time": "2023-10-22T04:54:03.733715",
1486
+ "exception": false,
1487
+ "start_time": "2023-10-22T04:54:02.497886",
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 22 00:39 checkpoints\r\n",
1499
+ "drwxr-xr-x 1 root 3003 0 Oct 22 00:39 lora\r\n",
1500
+ "drwxr-xr-x 1 root 3003 0 Oct 22 00:33 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-22T04:54:05.862618Z",
1525
+ "iopub.status.busy": "2023-10-22T04:54:05.861702Z",
1526
+ "iopub.status.idle": "2023-10-22T04:56:43.569718Z",
1527
+ "shell.execute_reply": "2023-10-22T04:56:43.569054Z"
1528
+ },
1529
+ "papermill": {
1530
+ "duration": 159.765833,
1531
+ "end_time": "2023-10-22T04:56:44.594302",
1532
+ "exception": false,
1533
+ "start_time": "2023-10-22T04:54:04.828469",
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": "6d90a920-fb22-4291-8466-411ff41e31be",
1563
+ "metadata": {
1564
+ "execution": {
1565
+ "iopub.execute_input": "2023-10-22T04:56:46.484015Z",
1566
+ "iopub.status.busy": "2023-10-22T04:56:46.483256Z",
1567
+ "iopub.status.idle": "2023-10-22T04:56:46.762038Z",
1568
+ "shell.execute_reply": "2023-10-22T04:56:46.761255Z"
1569
+ },
1570
+ "papermill": {
1571
+ "duration": 1.264259,
1572
+ "end_time": "2023-10-22T04:56:46.763647",
1573
+ "exception": false,
1574
+ "start_time": "2023-10-22T04:56:45.499388",
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 22 04:56 added_tokens.json\r\n",
1586
+ " 0 drwxr-xr-x 1 root 3003 0 Oct 22 00:39 checkpoints\r\n",
1587
+ "1.0K -rw-r--r-- 1 root 3003 648 Oct 22 04:54 config.json\r\n",
1588
+ " 512 -rw-r--r-- 1 root 3003 183 Oct 22 04:54 generation_config.json\r\n",
1589
+ " 0 drwxr-xr-x 1 root 3003 0 Oct 22 00:39 lora\r\n",
1590
+ "9.3G -rw-r--r-- 1 root 3003 9.3G Oct 22 04:54 pytorch_model-00001-of-00002.bin\r\n",
1591
+ "3.3G -rw-r--r-- 1 root 3003 3.3G Oct 22 04:56 pytorch_model-00002-of-00002.bin\r\n",
1592
+ " 24K -rw-r--r-- 1 root 3003 24K Oct 22 04:56 pytorch_model.bin.index.json\r\n",
1593
+ "1.0K -rw-r--r-- 1 root 3003 552 Oct 22 04:56 special_tokens_map.json\r\n",
1594
+ " 0 drwxr-xr-x 1 root 3003 0 Oct 22 00:33 src\r\n",
1595
+ "1.8M -rw-r--r-- 1 root 3003 1.8M Oct 22 04:56 tokenizer.json\r\n",
1596
+ "489K -rw-r--r-- 1 root 3003 489K Oct 22 04:56 tokenizer.model\r\n",
1597
+ "1.5K -rw-r--r-- 1 root 3003 1.1K Oct 22 04:56 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
+ ]
1609
+ }
1610
+ ],
1611
+ "source": [
1612
+ "! ls -lash {trained_model_path}"
1613
+ ]
1614
+ },
1615
+ {
1616
+ "cell_type": "code",
1617
+ "execution_count": 20,
1618
+ "id": "202a694a",
1619
+ "metadata": {
1620
+ "execution": {
1621
+ "iopub.execute_input": "2023-10-22T04:56:48.598532Z",
1622
+ "iopub.status.busy": "2023-10-22T04:56:48.597715Z"
1623
+ },
1624
+ "papermill": {
1625
+ "duration": null,
1626
+ "end_time": null,
1627
+ "exception": false,
1628
+ "start_time": "2023-10-22T04:56:47.688302",
1629
+ "status": "running"
1630
+ },
1631
+ "tags": []
1632
+ },
1633
+ "outputs": [
1634
+ {
1635
+ "data": {
1636
+ "application/vnd.jupyter.widget-view+json": {
1637
+ "model_id": "fd6c61a8f74449ab8cd067d50d2265ad",
1638
+ "version_major": 2,
1639
+ "version_minor": 0
1640
+ },
1641
+ "text/plain": [
1642
+ "Upload 2 LFS files: 0%| | 0/2 [00:00<?, ?it/s]"
1643
+ ]
1644
+ },
1645
+ "metadata": {},
1646
+ "output_type": "display_data"
1647
+ },
1648
+ {
1649
+ "data": {
1650
+ "application/vnd.jupyter.widget-view+json": {
1651
+ "model_id": "7fdcfa71a5bf459f8a08db3d2d018d9f",
1652
+ "version_major": 2,
1653
+ "version_minor": 0
1654
+ },
1655
+ "text/plain": [
1656
+ "pytorch_model-00001-of-00002.bin: 0%| | 0.00/9.98G [00:00<?, ?B/s]"
1657
+ ]
1658
+ },
1659
+ "metadata": {},
1660
+ "output_type": "display_data"
1661
+ },
1662
+ {
1663
+ "data": {
1664
+ "application/vnd.jupyter.widget-view+json": {
1665
+ "model_id": "8d8a26f4fedf4be697918941db33850b",
1666
+ "version_major": 2,
1667
+ "version_minor": 0
1668
+ },
1669
+ "text/plain": [
1670
+ "pytorch_model-00002-of-00002.bin: 0%| | 0.00/3.50G [00:00<?, ?B/s]"
1671
+ ]
1672
+ },
1673
+ "metadata": {},
1674
+ "output_type": "display_data"
1675
+ }
1676
+ ],
1677
+ "source": [
1678
+ "from huggingface_hub import HfApi\n",
1679
+ "import shutil\n",
1680
+ "\n",
1681
+ "tokenizer_model_path_base = Path(model_path) / \"tokenizer.model\"\n",
1682
+ "tokenizer_model_path_trained = Path(trained_model_path) / \"tokenizer.model\"\n",
1683
+ "if tokenizer_model_path_base.exists() and not tokenizer_model_path_trained.exists():\n",
1684
+ " shutil.copy(tokenizer_model_path_base, tokenizer_model_path_trained)\n",
1685
+ "\n",
1686
+ "repo_id = params.get(\"push_to_hub\")\n",
1687
+ "if repo_id:\n",
1688
+ " model.push_to_hub(repo_id)\n",
1689
+ " tokenizer.push_to_hub(repo_id)\n",
1690
+ " hf_api = HfApi()\n",
1691
+ " # Upload tokenizer.model if it was in base model\n",
1692
+ " if tokenizer_model_path_base.exists():\n",
1693
+ " hf_api.upload_file(\n",
1694
+ " path_or_fileobj=tokenizer_model_path_base,\n",
1695
+ " path_in_repo=tokenizer_model_path_base.name,\n",
1696
+ " repo_id=repo_id,\n",
1697
+ " )\n",
1698
+ " logs_path = Path(\"/content/artifacts/src/train.ipynb\")\n",
1699
+ " if logs_path.exists():\n",
1700
+ " hf_api.upload_file(\n",
1701
+ " path_or_fileobj=logs_path,\n",
1702
+ " path_in_repo=logs_path.name,\n",
1703
+ " repo_id=repo_id,\n",
1704
+ " )\n"
1705
+ ]
1706
+ }
1707
+ ],
1708
+ "metadata": {
1709
+ "kernelspec": {
1710
+ "display_name": "Python 3 (ipykernel)",
1711
+ "language": "python",
1712
+ "name": "python3"
1713
+ },
1714
+ "language_info": {
1715
+ "codemirror_mode": {
1716
+ "name": "ipython",
1717
+ "version": 3
1718
+ },
1719
+ "file_extension": ".py",
1720
+ "mimetype": "text/x-python",
1721
+ "name": "python",
1722
+ "nbconvert_exporter": "python",
1723
+ "pygments_lexer": "ipython3",
1724
+ "version": "3.10.12"
1725
+ },
1726
+ "papermill": {
1727
+ "default_parameters": {},
1728
+ "duration": null,
1729
+ "end_time": null,
1730
+ "environment_variables": {},
1731
+ "exception": null,
1732
+ "input_path": "/content/src/train.ipynb",
1733
+ "output_path": "/content/artifacts/src/train.ipynb",
1734
+ "parameters": {},
1735
+ "start_time": "2023-10-22T00:33:15.528186",
1736
+ "version": "2.4.0"
1737
+ }
1738
+ },
1739
+ "nbformat": 4,
1740
+ "nbformat_minor": 5
1741
+ }