samos123 commited on
Commit
37e0265
·
1 Parent(s): f1b1bef

Upload train.ipynb with huggingface_hub

Browse files
Files changed (1) hide show
  1. train.ipynb +1640 -0
train.ipynb ADDED
@@ -0,0 +1,1640 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "9c3e4532",
6
+ "metadata": {
7
+ "papermill": {
8
+ "duration": 1.084185,
9
+ "end_time": "2023-10-21T05:49:17.395684",
10
+ "exception": false,
11
+ "start_time": "2023-10-21T05:49:16.311499",
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-21T05:49:19.339339Z",
39
+ "iopub.status.busy": "2023-10-21T05:49:19.338625Z",
40
+ "iopub.status.idle": "2023-10-21T05:49:19.351013Z",
41
+ "shell.execute_reply": "2023-10-21T05:49:19.350424Z"
42
+ },
43
+ "papermill": {
44
+ "duration": 0.924056,
45
+ "end_time": "2023-10-21T05:49:19.352494",
46
+ "exception": false,
47
+ "start_time": "2023-10-21T05:49:18.428438",
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-40/resolve/main/WithRetrieval-SchemaSplit-Train-40.json',\n",
57
+ " 'logging_steps': 50,\n",
58
+ " 'modules_to_save': 'embed_tokens, lm_head',\n",
59
+ " 'num_train_epochs': 3,\n",
60
+ " 'per_device_eval_batch_size': 1,\n",
61
+ " 'per_device_train_batch_size': 1,\n",
62
+ " '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",
63
+ " 'push_to_hub': 'substratusai/wgql-WithRetrieval-SchemaSplit-Train-40',\n",
64
+ " 'save_steps': 50,\n",
65
+ " 'target_modules': 'q_proj, up_proj, o_proj, k_proj, down_proj, gate_proj, v_proj',\n",
66
+ " 'warmup_steps': 100}"
67
+ ]
68
+ },
69
+ "execution_count": 1,
70
+ "metadata": {},
71
+ "output_type": "execute_result"
72
+ }
73
+ ],
74
+ "source": [
75
+ "import json\n",
76
+ "from pathlib import Path\n",
77
+ "\n",
78
+ "params = {}\n",
79
+ "params_path = Path(\"/content/params.json\")\n",
80
+ "if params_path.is_file():\n",
81
+ " with params_path.open(\"r\", encoding=\"UTF-8\") as params_file:\n",
82
+ " params = json.load(params_file)\n",
83
+ "\n",
84
+ "\n",
85
+ "params"
86
+ ]
87
+ },
88
+ {
89
+ "cell_type": "code",
90
+ "execution_count": 2,
91
+ "id": "9fafd16b-d8c9-47bf-9116-c27b1d43a019",
92
+ "metadata": {
93
+ "execution": {
94
+ "iopub.execute_input": "2023-10-21T05:49:21.248001Z",
95
+ "iopub.status.busy": "2023-10-21T05:49:21.247682Z",
96
+ "iopub.status.idle": "2023-10-21T05:49:23.661856Z",
97
+ "shell.execute_reply": "2023-10-21T05:49:23.661065Z"
98
+ },
99
+ "papermill": {
100
+ "duration": 3.288968,
101
+ "end_time": "2023-10-21T05:49:23.663559",
102
+ "exception": false,
103
+ "start_time": "2023-10-21T05:49:20.374591",
104
+ "status": "completed"
105
+ },
106
+ "tags": []
107
+ },
108
+ "outputs": [
109
+ {
110
+ "name": "stdout",
111
+ "output_type": "stream",
112
+ "text": [
113
+ "Using the following URLs for the dataset: ['https://huggingface.co/datasets/weaviate/WithRetrieval-SchemaSplit-Train-40/resolve/main/WithRetrieval-SchemaSplit-Train-40.json']\n"
114
+ ]
115
+ },
116
+ {
117
+ "data": {
118
+ "application/vnd.jupyter.widget-view+json": {
119
+ "model_id": "b6969f92a1334ecd9b1f632e5868c724",
120
+ "version_major": 2,
121
+ "version_minor": 0
122
+ },
123
+ "text/plain": [
124
+ "Downloading data files: 0%| | 0/1 [00:00<?, ?it/s]"
125
+ ]
126
+ },
127
+ "metadata": {},
128
+ "output_type": "display_data"
129
+ },
130
+ {
131
+ "data": {
132
+ "application/vnd.jupyter.widget-view+json": {
133
+ "model_id": "4c727f7b7b804766a0184e8522bd1bf9",
134
+ "version_major": 2,
135
+ "version_minor": 0
136
+ },
137
+ "text/plain": [
138
+ "Downloading data: 0%| | 0.00/8.63M [00:00<?, ?B/s]"
139
+ ]
140
+ },
141
+ "metadata": {},
142
+ "output_type": "display_data"
143
+ },
144
+ {
145
+ "data": {
146
+ "application/vnd.jupyter.widget-view+json": {
147
+ "model_id": "6ecd7883b320473dae19fbc43242fc94",
148
+ "version_major": 2,
149
+ "version_minor": 0
150
+ },
151
+ "text/plain": [
152
+ "Extracting data files: 0%| | 0/1 [00:00<?, ?it/s]"
153
+ ]
154
+ },
155
+ "metadata": {},
156
+ "output_type": "display_data"
157
+ },
158
+ {
159
+ "data": {
160
+ "application/vnd.jupyter.widget-view+json": {
161
+ "model_id": "309f1fe79282496295045653532d7516",
162
+ "version_major": 2,
163
+ "version_minor": 0
164
+ },
165
+ "text/plain": [
166
+ "Generating train split: 0 examples [00:00, ? examples/s]"
167
+ ]
168
+ },
169
+ "metadata": {},
170
+ "output_type": "display_data"
171
+ },
172
+ {
173
+ "data": {
174
+ "text/plain": [
175
+ "DatasetDict({\n",
176
+ " train: Dataset({\n",
177
+ " features: ['input', 'output', 'nlcommand', 'apiRef', 'apiRefPath', 'schema', 'schemaPath'],\n",
178
+ " num_rows: 1493\n",
179
+ " })\n",
180
+ "})"
181
+ ]
182
+ },
183
+ "execution_count": 2,
184
+ "metadata": {},
185
+ "output_type": "execute_result"
186
+ }
187
+ ],
188
+ "source": [
189
+ "import os \n",
190
+ "from datasets import load_dataset\n",
191
+ "\n",
192
+ "dataset_urls = params.get(\"dataset_urls\")\n",
193
+ "if dataset_urls:\n",
194
+ " urls = [u.strip() for u in dataset_urls.split(\",\")]\n",
195
+ " print(f\"Using the following URLs for the dataset: {urls}\")\n",
196
+ " data = load_dataset(\"json\", data_files=urls)\n",
197
+ "else:\n",
198
+ " data = load_dataset(\"json\", data_files=\"/content/data/*.json*\")\n",
199
+ "data"
200
+ ]
201
+ },
202
+ {
203
+ "cell_type": "code",
204
+ "execution_count": 3,
205
+ "id": "08e478fa-d095-4145-9bd1-b4feec7bc4f0",
206
+ "metadata": {
207
+ "execution": {
208
+ "iopub.execute_input": "2023-10-21T05:49:26.648312Z",
209
+ "iopub.status.busy": "2023-10-21T05:49:26.647335Z",
210
+ "iopub.status.idle": "2023-10-21T05:53:45.197465Z",
211
+ "shell.execute_reply": "2023-10-21T05:53:45.196772Z"
212
+ },
213
+ "papermill": {
214
+ "duration": 260.502489,
215
+ "end_time": "2023-10-21T05:53:46.089986",
216
+ "exception": false,
217
+ "start_time": "2023-10-21T05:49:25.587497",
218
+ "status": "completed"
219
+ },
220
+ "tags": []
221
+ },
222
+ "outputs": [
223
+ {
224
+ "data": {
225
+ "application/vnd.jupyter.widget-view+json": {
226
+ "model_id": "d86522fa78fb4c7b8f05ac3424532dc5",
227
+ "version_major": 2,
228
+ "version_minor": 0
229
+ },
230
+ "text/plain": [
231
+ "Loading checkpoint shards: 0%| | 0/2 [00:00<?, ?it/s]"
232
+ ]
233
+ },
234
+ "metadata": {},
235
+ "output_type": "display_data"
236
+ },
237
+ {
238
+ "data": {
239
+ "text/plain": [
240
+ "LlamaForCausalLM(\n",
241
+ " (model): LlamaModel(\n",
242
+ " (embed_tokens): Embedding(32000, 4096)\n",
243
+ " (layers): ModuleList(\n",
244
+ " (0-31): 32 x LlamaDecoderLayer(\n",
245
+ " (self_attn): LlamaAttention(\n",
246
+ " (q_proj): Linear(in_features=4096, out_features=4096, bias=False)\n",
247
+ " (k_proj): Linear(in_features=4096, out_features=4096, bias=False)\n",
248
+ " (v_proj): Linear(in_features=4096, out_features=4096, bias=False)\n",
249
+ " (o_proj): Linear(in_features=4096, out_features=4096, bias=False)\n",
250
+ " (rotary_emb): LlamaRotaryEmbedding()\n",
251
+ " )\n",
252
+ " (mlp): LlamaMLP(\n",
253
+ " (gate_proj): Linear(in_features=4096, out_features=11008, bias=False)\n",
254
+ " (up_proj): Linear(in_features=4096, out_features=11008, bias=False)\n",
255
+ " (down_proj): Linear(in_features=11008, out_features=4096, bias=False)\n",
256
+ " (act_fn): SiLUActivation()\n",
257
+ " )\n",
258
+ " (input_layernorm): LlamaRMSNorm()\n",
259
+ " (post_attention_layernorm): LlamaRMSNorm()\n",
260
+ " )\n",
261
+ " )\n",
262
+ " (norm): LlamaRMSNorm()\n",
263
+ " )\n",
264
+ " (lm_head): Linear(in_features=4096, out_features=32000, bias=False)\n",
265
+ ")"
266
+ ]
267
+ },
268
+ "execution_count": 3,
269
+ "metadata": {},
270
+ "output_type": "execute_result"
271
+ }
272
+ ],
273
+ "source": [
274
+ "import transformers\n",
275
+ "import torch\n",
276
+ "import sys\n",
277
+ "from transformers import AutoTokenizer, AutoModelForCausalLM\n",
278
+ "\n",
279
+ "model_path = \"/content/model/\"\n",
280
+ "trained_model_path = \"/content/artifacts\"\n",
281
+ "trained_model_path_lora = \"/content/artifacts/lora\"\n",
282
+ "\n",
283
+ "tokenizer = AutoTokenizer.from_pretrained(model_path,\n",
284
+ " local_files_only=True)\n",
285
+ "model = AutoModelForCausalLM.from_pretrained(\n",
286
+ " model_path, torch_dtype=torch.float16, device_map=\"auto\", trust_remote_code=True)\n",
287
+ "model"
288
+ ]
289
+ },
290
+ {
291
+ "cell_type": "code",
292
+ "execution_count": 4,
293
+ "id": "88908150-1585-4781-9542-d68193d808bc",
294
+ "metadata": {
295
+ "execution": {
296
+ "iopub.execute_input": "2023-10-21T05:53:47.995897Z",
297
+ "iopub.status.busy": "2023-10-21T05:53:47.995017Z",
298
+ "iopub.status.idle": "2023-10-21T05:53:48.000852Z",
299
+ "shell.execute_reply": "2023-10-21T05:53:48.000183Z"
300
+ },
301
+ "papermill": {
302
+ "duration": 0.955502,
303
+ "end_time": "2023-10-21T05:53:48.002402",
304
+ "exception": false,
305
+ "start_time": "2023-10-21T05:53:47.046900",
306
+ "status": "completed"
307
+ },
308
+ "tags": []
309
+ },
310
+ "outputs": [
311
+ {
312
+ "data": {
313
+ "text/plain": [
314
+ "LlamaConfig {\n",
315
+ " \"_name_or_path\": \"/content/model/\",\n",
316
+ " \"architectures\": [\n",
317
+ " \"LlamaForCausalLM\"\n",
318
+ " ],\n",
319
+ " \"attention_bias\": false,\n",
320
+ " \"bos_token_id\": 1,\n",
321
+ " \"eos_token_id\": 2,\n",
322
+ " \"hidden_act\": \"silu\",\n",
323
+ " \"hidden_size\": 4096,\n",
324
+ " \"initializer_range\": 0.02,\n",
325
+ " \"intermediate_size\": 11008,\n",
326
+ " \"max_position_embeddings\": 4096,\n",
327
+ " \"model_type\": \"llama\",\n",
328
+ " \"num_attention_heads\": 32,\n",
329
+ " \"num_hidden_layers\": 32,\n",
330
+ " \"num_key_value_heads\": 32,\n",
331
+ " \"pretraining_tp\": 1,\n",
332
+ " \"rms_norm_eps\": 1e-05,\n",
333
+ " \"rope_scaling\": null,\n",
334
+ " \"rope_theta\": 10000.0,\n",
335
+ " \"tie_word_embeddings\": false,\n",
336
+ " \"torch_dtype\": \"float16\",\n",
337
+ " \"transformers_version\": \"4.34.1\",\n",
338
+ " \"use_cache\": true,\n",
339
+ " \"vocab_size\": 32000\n",
340
+ "}"
341
+ ]
342
+ },
343
+ "execution_count": 4,
344
+ "metadata": {},
345
+ "output_type": "execute_result"
346
+ }
347
+ ],
348
+ "source": [
349
+ "model.config"
350
+ ]
351
+ },
352
+ {
353
+ "cell_type": "code",
354
+ "execution_count": 5,
355
+ "id": "ec8a1a9f-fe60-49c7-ab20-04034323df8a",
356
+ "metadata": {
357
+ "execution": {
358
+ "iopub.execute_input": "2023-10-21T05:53:49.951921Z",
359
+ "iopub.status.busy": "2023-10-21T05:53:49.951284Z",
360
+ "iopub.status.idle": "2023-10-21T05:53:49.956287Z",
361
+ "shell.execute_reply": "2023-10-21T05:53:49.955612Z"
362
+ },
363
+ "papermill": {
364
+ "duration": 1.021616,
365
+ "end_time": "2023-10-21T05:53:49.957721",
366
+ "exception": false,
367
+ "start_time": "2023-10-21T05:53:48.936105",
368
+ "status": "completed"
369
+ },
370
+ "tags": []
371
+ },
372
+ "outputs": [
373
+ {
374
+ "name": "stdout",
375
+ "output_type": "stream",
376
+ "text": [
377
+ "## Instruction\n",
378
+ "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",
379
+ "\n",
380
+ "Only use the API reference to understand the syntax of the request.\n",
381
+ "\n",
382
+ "## Natural Language Query\n",
383
+ "{nlcommand}\n",
384
+ "\n",
385
+ "## Schema\n",
386
+ "{schema}\n",
387
+ "\n",
388
+ "## API reference\n",
389
+ "{apiRef}\n",
390
+ "\n",
391
+ "## Answer\n",
392
+ "{output}\n",
393
+ "</s>\n"
394
+ ]
395
+ }
396
+ ],
397
+ "source": [
398
+ "default_prompt = \"\"\"\n",
399
+ "Below is an instruction that describes a task. Write a response that appropriately completes the request.\n",
400
+ "### Instruction:\n",
401
+ "{prompt}\n",
402
+ "### Response:\n",
403
+ "{completion}\n",
404
+ "\"\"\"\n",
405
+ "\n",
406
+ "prompt = params.get(\"prompt_template\", default_prompt)\n",
407
+ "\n",
408
+ "eos_token = tokenizer.convert_ids_to_tokens(model.config.eos_token_id)\n",
409
+ "if prompt[-len(eos_token):] != eos_token:\n",
410
+ " prompt = prompt + eos_token\n",
411
+ "\n",
412
+ "print(prompt)\n"
413
+ ]
414
+ },
415
+ {
416
+ "cell_type": "code",
417
+ "execution_count": 6,
418
+ "id": "0abf96e1-3bc1-4ae7-80ac-c2e585e9c7c1",
419
+ "metadata": {
420
+ "execution": {
421
+ "iopub.execute_input": "2023-10-21T05:54:00.755035Z",
422
+ "iopub.status.busy": "2023-10-21T05:54:00.754343Z",
423
+ "iopub.status.idle": "2023-10-21T05:54:01.608931Z",
424
+ "shell.execute_reply": "2023-10-21T05:54:01.608154Z"
425
+ },
426
+ "papermill": {
427
+ "duration": 10.709526,
428
+ "end_time": "2023-10-21T05:54:01.610675",
429
+ "exception": false,
430
+ "start_time": "2023-10-21T05:53:50.901149",
431
+ "status": "completed"
432
+ },
433
+ "tags": []
434
+ },
435
+ "outputs": [
436
+ {
437
+ "name": "stdout",
438
+ "output_type": "stream",
439
+ "text": [
440
+ "Sat Oct 21 05:54:00 2023 \r\n",
441
+ "+-----------------------------------------------------------------------------+\r\n",
442
+ "| NVIDIA-SMI 525.105.17 Driver Version: 525.105.17 CUDA Version: 12.0 |\r\n",
443
+ "|-------------------------------+----------------------+----------------------+\r\n",
444
+ "| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |\r\n",
445
+ "| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |\r\n",
446
+ "| | | MIG M. |\r\n",
447
+ "|===============================+======================+======================|\r\n",
448
+ "| 0 NVIDIA L4 Off | 00000000:00:04.0 Off | 0 |\r\n",
449
+ "| N/A 76C P0 36W / 72W | 3570MiB / 23034MiB | 0% Default |\r\n",
450
+ "| | | N/A |\r\n",
451
+ "+-------------------------------+----------------------+----------------------+\r\n"
452
+ ]
453
+ },
454
+ {
455
+ "name": "stdout",
456
+ "output_type": "stream",
457
+ "text": [
458
+ "| 1 NVIDIA L4 Off | 00000000:00:05.0 Off | 0 |\r\n",
459
+ "| N/A 75C P0 35W / 72W | 4096MiB / 23034MiB | 0% Default |\r\n",
460
+ "| | | N/A |\r\n",
461
+ "+-------------------------------+----------------------+----------------------+\r\n",
462
+ "| 2 NVIDIA L4 Off | 00000000:00:06.0 Off | 0 |\r\n",
463
+ "| N/A 75C P0 35W / 72W | 4096MiB / 23034MiB | 0% Default |\r\n",
464
+ "| | | N/A |\r\n",
465
+ "+-------------------------------+----------------------+----------------------+\r\n",
466
+ "| 3 NVIDIA L4 Off | 00000000:00:07.0 Off | 0 |\r\n",
467
+ "| N/A 75C P0 33W / 72W | 3570MiB / 23034MiB | 0% Default |\r\n",
468
+ "| | | N/A |\r\n",
469
+ "+-------------------------------+----------------------+----------------------+\r\n",
470
+ " \r\n",
471
+ "+-----------------------------------------------------------------------------+\r\n",
472
+ "| Processes: |\r\n",
473
+ "| GPU GI CI PID Type Process name GPU Memory |\r\n",
474
+ "| ID ID Usage |\r\n",
475
+ "|=============================================================================|\r\n",
476
+ "+-----------------------------------------------------------------------------+\r\n"
477
+ ]
478
+ }
479
+ ],
480
+ "source": [
481
+ "! nvidia-smi"
482
+ ]
483
+ },
484
+ {
485
+ "attachments": {},
486
+ "cell_type": "markdown",
487
+ "id": "4d1e1795-c783-4ddf-999e-f1de19258928",
488
+ "metadata": {
489
+ "papermill": {
490
+ "duration": 1.044535,
491
+ "end_time": "2023-10-21T05:54:03.603440",
492
+ "exception": false,
493
+ "start_time": "2023-10-21T05:54:02.558905",
494
+ "status": "completed"
495
+ },
496
+ "tags": []
497
+ },
498
+ "source": [
499
+ "Prompt before fine tuning"
500
+ ]
501
+ },
502
+ {
503
+ "cell_type": "code",
504
+ "execution_count": 7,
505
+ "id": "f5dd944b-e2bd-4bfd-a5fa-55bc90239926",
506
+ "metadata": {
507
+ "execution": {
508
+ "iopub.execute_input": "2023-10-21T05:54:05.579378Z",
509
+ "iopub.status.busy": "2023-10-21T05:54:05.578687Z",
510
+ "iopub.status.idle": "2023-10-21T05:54:05.601523Z",
511
+ "shell.execute_reply": "2023-10-21T05:54:05.600770Z"
512
+ },
513
+ "papermill": {
514
+ "duration": 0.997872,
515
+ "end_time": "2023-10-21T05:54:05.603060",
516
+ "exception": false,
517
+ "start_time": "2023-10-21T05:54:04.605188",
518
+ "status": "completed"
519
+ },
520
+ "tags": []
521
+ },
522
+ "outputs": [
523
+ {
524
+ "data": {
525
+ "text/plain": [
526
+ "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",
527
+ "\t0: AddedToken(\"<unk>\", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),\n",
528
+ "\t1: AddedToken(\"<s>\", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),\n",
529
+ "\t2: AddedToken(\"</s>\", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),\n",
530
+ "\t32000: AddedToken(\"[PAD]\", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),\n",
531
+ "}"
532
+ ]
533
+ },
534
+ "execution_count": 7,
535
+ "metadata": {},
536
+ "output_type": "execute_result"
537
+ }
538
+ ],
539
+ "source": [
540
+ "from typing import Dict\n",
541
+ "# source: https://github.com/artidoro/qlora\n",
542
+ "DEFAULT_PAD_TOKEN = params.get(\"pad_token\", \"[PAD]\")\n",
543
+ "\n",
544
+ "def smart_tokenizer_and_embedding_resize(\n",
545
+ " special_tokens_dict: Dict,\n",
546
+ " tokenizer: transformers.PreTrainedTokenizer,\n",
547
+ " model: transformers.PreTrainedModel,\n",
548
+ "):\n",
549
+ " \"\"\"Resize tokenizer and embedding.\n",
550
+ "\n",
551
+ " Note: This is the unoptimized version that may make your embedding size not be divisible by 64.\n",
552
+ " \"\"\"\n",
553
+ " num_new_tokens = tokenizer.add_special_tokens(special_tokens_dict)\n",
554
+ " model.resize_token_embeddings(len(tokenizer))\n",
555
+ " if num_new_tokens > 0:\n",
556
+ " input_embeddings_data = model.get_input_embeddings().weight.data\n",
557
+ " output_embeddings_data = model.get_output_embeddings().weight.data\n",
558
+ "\n",
559
+ " input_embeddings_avg = input_embeddings_data[:-num_new_tokens].mean(dim=0, keepdim=True)\n",
560
+ " output_embeddings_avg = output_embeddings_data[:-num_new_tokens].mean(dim=0, keepdim=True)\n",
561
+ "\n",
562
+ " input_embeddings_data[-num_new_tokens:] = input_embeddings_avg\n",
563
+ " output_embeddings_data[-num_new_tokens:] = output_embeddings_avg\n",
564
+ "\n",
565
+ "if tokenizer._pad_token is None:\n",
566
+ " smart_tokenizer_and_embedding_resize(\n",
567
+ " special_tokens_dict=dict(pad_token=DEFAULT_PAD_TOKEN),\n",
568
+ " tokenizer=tokenizer,\n",
569
+ " model=model,\n",
570
+ " )\n",
571
+ "\n",
572
+ "if isinstance(tokenizer, transformers.LlamaTokenizer):\n",
573
+ " # LLaMA tokenizer may not have correct special tokens set.\n",
574
+ " # Check and add them if missing to prevent them from being parsed into different tokens.\n",
575
+ " # Note that these are present in the vocabulary.\n",
576
+ " # Note also that `model.config.pad_token_id` is 0 which corresponds to `<unk>` token.\n",
577
+ " print('Adding special tokens.')\n",
578
+ " tokenizer.add_special_tokens({\n",
579
+ " \"eos_token\": tokenizer.convert_ids_to_tokens(model.config.eos_token_id),\n",
580
+ " \"bos_token\": tokenizer.convert_ids_to_tokens(model.config.bos_token_id),\n",
581
+ " \"unk_token\": tokenizer.convert_ids_to_tokens(\n",
582
+ " model.config.pad_token_id if model.config.pad_token_id != -1 else tokenizer.pad_token_id\n",
583
+ " ),\n",
584
+ " })\n",
585
+ "\n",
586
+ "tokenizer"
587
+ ]
588
+ },
589
+ {
590
+ "cell_type": "code",
591
+ "execution_count": 8,
592
+ "id": "e78b510d",
593
+ "metadata": {
594
+ "execution": {
595
+ "iopub.execute_input": "2023-10-21T05:54:07.604577Z",
596
+ "iopub.status.busy": "2023-10-21T05:54:07.603812Z",
597
+ "iopub.status.idle": "2023-10-21T05:54:10.896691Z",
598
+ "shell.execute_reply": "2023-10-21T05:54:10.896027Z"
599
+ },
600
+ "papermill": {
601
+ "duration": 4.319511,
602
+ "end_time": "2023-10-21T05:54:10.898941",
603
+ "exception": false,
604
+ "start_time": "2023-10-21T05:54:06.579430",
605
+ "status": "completed"
606
+ },
607
+ "tags": []
608
+ },
609
+ "outputs": [
610
+ {
611
+ "data": {
612
+ "application/vnd.jupyter.widget-view+json": {
613
+ "model_id": "c8b882b3f67b457a921a73aa350a8aee",
614
+ "version_major": 2,
615
+ "version_minor": 0
616
+ },
617
+ "text/plain": [
618
+ "Map: 0%| | 0/1493 [00:00<?, ? examples/s]"
619
+ ]
620
+ },
621
+ "metadata": {},
622
+ "output_type": "display_data"
623
+ },
624
+ {
625
+ "name": "stdout",
626
+ "output_type": "stream",
627
+ "text": [
628
+ "After tokenizing: DatasetDict({\n",
629
+ " train: Dataset({\n",
630
+ " features: ['input', 'output', 'nlcommand', 'apiRef', 'apiRefPath', 'schema', 'schemaPath', 'input_ids', 'attention_mask'],\n",
631
+ " num_rows: 1493\n",
632
+ " })\n",
633
+ "})\n"
634
+ ]
635
+ }
636
+ ],
637
+ "source": [
638
+ "from typing import Dict\n",
639
+ "\n",
640
+ "data = data.map(lambda x: tokenizer(prompt.format_map(x)))\n",
641
+ "\n",
642
+ "print(\"After tokenizing:\", data)"
643
+ ]
644
+ },
645
+ {
646
+ "cell_type": "code",
647
+ "execution_count": 9,
648
+ "id": "5dae6c6f-3ae1-4697-852e-fce24a82b9e8",
649
+ "metadata": {
650
+ "execution": {
651
+ "iopub.execute_input": "2023-10-21T05:54:12.827852Z",
652
+ "iopub.status.busy": "2023-10-21T05:54:12.827108Z",
653
+ "iopub.status.idle": "2023-10-21T05:55:48.548492Z",
654
+ "shell.execute_reply": "2023-10-21T05:55:48.547729Z"
655
+ },
656
+ "papermill": {
657
+ "duration": 97.837612,
658
+ "end_time": "2023-10-21T05:55:49.692538",
659
+ "exception": false,
660
+ "start_time": "2023-10-21T05:54:11.854926",
661
+ "status": "completed"
662
+ },
663
+ "tags": []
664
+ },
665
+ "outputs": [
666
+ {
667
+ "name": "stdout",
668
+ "output_type": "stream",
669
+ "text": [
670
+ "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"
671
+ ]
672
+ },
673
+ {
674
+ "name": "stdout",
675
+ "output_type": "stream",
676
+ "text": [
677
+ "trainable params: 564,281,344 || all params: 7,040,552,960 || trainable%: 8.01473047935144\n"
678
+ ]
679
+ }
680
+ ],
681
+ "source": [
682
+ "from peft import get_peft_model, LoraConfig, prepare_model_for_kbit_training\n",
683
+ "\n",
684
+ "target_modules = params.get(\"target_modules\")\n",
685
+ "if target_modules:\n",
686
+ " target_modules = [mod.strip() for mod in target_modules.split(\",\")]\n",
687
+ "\n",
688
+ "modules_to_save = params.get(\"modules_to_save\")\n",
689
+ "if modules_to_save:\n",
690
+ " modules_to_save = [mod.strip() for mod in modules_to_save.split(\",\")]\n",
691
+ "\n",
692
+ "lora_config2 = LoraConfig(\n",
693
+ " r=16,\n",
694
+ " lora_alpha=16,\n",
695
+ " lora_dropout=0.05,\n",
696
+ " bias=\"none\",\n",
697
+ " task_type=\"CAUSAL_LM\",\n",
698
+ " target_modules=target_modules,\n",
699
+ " modules_to_save = modules_to_save\n",
700
+ ")\n",
701
+ "print(lora_config2)\n",
702
+ "\n",
703
+ "model = prepare_model_for_kbit_training(model)\n",
704
+ "\n",
705
+ "# add LoRA adaptor\n",
706
+ "model = get_peft_model(model, lora_config2)\n",
707
+ "model.print_trainable_parameters()"
708
+ ]
709
+ },
710
+ {
711
+ "cell_type": "code",
712
+ "execution_count": 10,
713
+ "id": "70a3e36c-62cf-45aa-8f37-0db0e40857dc",
714
+ "metadata": {
715
+ "execution": {
716
+ "iopub.execute_input": "2023-10-21T05:55:51.796898Z",
717
+ "iopub.status.busy": "2023-10-21T05:55:51.795890Z",
718
+ "iopub.status.idle": "2023-10-21T05:55:51.815502Z",
719
+ "shell.execute_reply": "2023-10-21T05:55:51.814850Z"
720
+ },
721
+ "papermill": {
722
+ "duration": 1.051739,
723
+ "end_time": "2023-10-21T05:55:51.817016",
724
+ "exception": false,
725
+ "start_time": "2023-10-21T05:55:50.765277",
726
+ "status": "completed"
727
+ },
728
+ "tags": []
729
+ },
730
+ "outputs": [
731
+ {
732
+ "data": {
733
+ "text/plain": [
734
+ "TrainingArguments(\n",
735
+ "_n_gpu=4,\n",
736
+ "adafactor=False,\n",
737
+ "adam_beta1=0.9,\n",
738
+ "adam_beta2=0.999,\n",
739
+ "adam_epsilon=1e-08,\n",
740
+ "auto_find_batch_size=False,\n",
741
+ "bf16=False,\n",
742
+ "bf16_full_eval=False,\n",
743
+ "data_seed=None,\n",
744
+ "dataloader_drop_last=False,\n",
745
+ "dataloader_num_workers=0,\n",
746
+ "dataloader_pin_memory=True,\n",
747
+ "ddp_backend=None,\n",
748
+ "ddp_broadcast_buffers=None,\n",
749
+ "ddp_bucket_cap_mb=None,\n",
750
+ "ddp_find_unused_parameters=None,\n",
751
+ "ddp_timeout=1800,\n",
752
+ "debug=[],\n",
753
+ "deepspeed=None,\n",
754
+ "disable_tqdm=False,\n",
755
+ "dispatch_batches=None,\n",
756
+ "do_eval=False,\n",
757
+ "do_predict=False,\n",
758
+ "do_train=False,\n",
759
+ "eval_accumulation_steps=None,\n",
760
+ "eval_delay=0,\n",
761
+ "eval_steps=None,\n",
762
+ "evaluation_strategy=no,\n",
763
+ "fp16=True,\n",
764
+ "fp16_backend=auto,\n",
765
+ "fp16_full_eval=False,\n",
766
+ "fp16_opt_level=O1,\n",
767
+ "fsdp=[],\n",
768
+ "fsdp_config={'min_num_params': 0, 'xla': False, 'xla_fsdp_grad_ckpt': False},\n",
769
+ "fsdp_min_num_params=0,\n",
770
+ "fsdp_transformer_layer_cls_to_wrap=None,\n",
771
+ "full_determinism=False,\n",
772
+ "gradient_accumulation_steps=4,\n",
773
+ "gradient_checkpointing=False,\n",
774
+ "greater_is_better=None,\n",
775
+ "group_by_length=False,\n",
776
+ "half_precision_backend=auto,\n",
777
+ "hub_always_push=False,\n",
778
+ "hub_model_id=None,\n",
779
+ "hub_private_repo=False,\n",
780
+ "hub_strategy=every_save,\n",
781
+ "hub_token=<HUB_TOKEN>,\n",
782
+ "ignore_data_skip=False,\n",
783
+ "include_inputs_for_metrics=False,\n",
784
+ "include_tokens_per_second=False,\n",
785
+ "jit_mode_eval=False,\n",
786
+ "label_names=None,\n",
787
+ "label_smoothing_factor=0.0,\n",
788
+ "learning_rate=3e-05,\n",
789
+ "length_column_name=length,\n",
790
+ "load_best_model_at_end=False,\n",
791
+ "local_rank=0,\n",
792
+ "log_level=passive,\n",
793
+ "log_level_replica=warning,\n",
794
+ "log_on_each_node=True,\n",
795
+ "logging_dir=/content/artifacts/checkpoints/runs/Oct21_05-55-51_wgqlg-withretrieval-schemasplit-train-40-modeller-zmvfq,\n",
796
+ "logging_first_step=False,\n",
797
+ "logging_nan_inf_filter=True,\n",
798
+ "logging_steps=50,\n",
799
+ "logging_strategy=steps,\n",
800
+ "lr_scheduler_type=cosine,\n",
801
+ "max_grad_norm=1.0,\n",
802
+ "max_steps=-1,\n",
803
+ "metric_for_best_model=None,\n",
804
+ "mp_parameters=,\n",
805
+ "no_cuda=False,\n",
806
+ "num_train_epochs=3.0,\n",
807
+ "optim=paged_adamw_32bit,\n",
808
+ "optim_args=None,\n",
809
+ "output_dir=/content/artifacts/checkpoints,\n",
810
+ "overwrite_output_dir=False,\n",
811
+ "past_index=-1,\n",
812
+ "per_device_eval_batch_size=1,\n",
813
+ "per_device_train_batch_size=1,\n",
814
+ "prediction_loss_only=False,\n",
815
+ "push_to_hub=False,\n",
816
+ "push_to_hub_model_id=None,\n",
817
+ "push_to_hub_organization=None,\n",
818
+ "push_to_hub_token=<PUSH_TO_HUB_TOKEN>,\n",
819
+ "ray_scope=last,\n",
820
+ "remove_unused_columns=True,\n",
821
+ "report_to=[],\n",
822
+ "resume_from_checkpoint=None,\n",
823
+ "run_name=/content/artifacts/checkpoints,\n",
824
+ "save_on_each_node=False,\n",
825
+ "save_safetensors=False,\n",
826
+ "save_steps=50,\n",
827
+ "save_strategy=steps,\n",
828
+ "save_total_limit=None,\n",
829
+ "seed=42,\n",
830
+ "sharded_ddp=[],\n",
831
+ "skip_memory_metrics=True,\n",
832
+ "tf32=None,\n",
833
+ "torch_compile=False,\n",
834
+ "torch_compile_backend=None,\n",
835
+ "torch_compile_mode=None,\n",
836
+ "torchdynamo=None,\n",
837
+ "tpu_metrics_debug=False,\n",
838
+ "tpu_num_cores=None,\n",
839
+ "use_cpu=False,\n",
840
+ "use_ipex=False,\n",
841
+ "use_legacy_prediction_loop=False,\n",
842
+ "use_mps_device=False,\n",
843
+ "warmup_ratio=0.02,\n",
844
+ "warmup_steps=100,\n",
845
+ "weight_decay=0.0,\n",
846
+ ")"
847
+ ]
848
+ },
849
+ "execution_count": 10,
850
+ "metadata": {},
851
+ "output_type": "execute_result"
852
+ }
853
+ ],
854
+ "source": [
855
+ "from utils import parse_training_args\n",
856
+ "\n",
857
+ "training_args = parse_training_args(params)\n",
858
+ "training_args"
859
+ ]
860
+ },
861
+ {
862
+ "cell_type": "code",
863
+ "execution_count": 11,
864
+ "id": "2ae3e5f9-e28e-457b-b6bf-a62a472241bf",
865
+ "metadata": {
866
+ "execution": {
867
+ "iopub.execute_input": "2023-10-21T05:55:53.899792Z",
868
+ "iopub.status.busy": "2023-10-21T05:55:53.899027Z",
869
+ "iopub.status.idle": "2023-10-21T05:55:53.902455Z",
870
+ "shell.execute_reply": "2023-10-21T05:55:53.901834Z"
871
+ },
872
+ "papermill": {
873
+ "duration": 1.100827,
874
+ "end_time": "2023-10-21T05:55:53.903903",
875
+ "exception": false,
876
+ "start_time": "2023-10-21T05:55:52.803076",
877
+ "status": "completed"
878
+ },
879
+ "tags": []
880
+ },
881
+ "outputs": [],
882
+ "source": [
883
+ "# data = data[\"train\"].train_test_split(test_size=0.1)\n",
884
+ "# data\n"
885
+ ]
886
+ },
887
+ {
888
+ "cell_type": "code",
889
+ "execution_count": 12,
890
+ "id": "5bc91439-6108-445c-8f85-e6558c9f0677",
891
+ "metadata": {
892
+ "execution": {
893
+ "iopub.execute_input": "2023-10-21T05:55:56.641848Z",
894
+ "iopub.status.busy": "2023-10-21T05:55:56.641104Z",
895
+ "iopub.status.idle": "2023-10-21T05:55:56.906471Z",
896
+ "shell.execute_reply": "2023-10-21T05:55:56.905658Z"
897
+ },
898
+ "papermill": {
899
+ "duration": 1.310772,
900
+ "end_time": "2023-10-21T05:55:56.908127",
901
+ "exception": false,
902
+ "start_time": "2023-10-21T05:55:55.597355",
903
+ "status": "completed"
904
+ },
905
+ "tags": []
906
+ },
907
+ "outputs": [
908
+ {
909
+ "name": "stderr",
910
+ "output_type": "stream",
911
+ "text": [
912
+ "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
913
+ "To disable this warning, you can either:\n",
914
+ "\t- Avoid using `tokenizers` before the fork if possible\n",
915
+ "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
916
+ ]
917
+ }
918
+ ],
919
+ "source": [
920
+ "! mkdir -p {trained_model_path_lora}"
921
+ ]
922
+ },
923
+ {
924
+ "cell_type": "code",
925
+ "execution_count": 13,
926
+ "id": "b33e407a-9d4f-49f6-a74b-b80db8cc3a8a",
927
+ "metadata": {
928
+ "execution": {
929
+ "iopub.execute_input": "2023-10-21T05:55:58.965151Z",
930
+ "iopub.status.busy": "2023-10-21T05:55:58.964343Z",
931
+ "iopub.status.idle": "2023-10-21T07:47:28.532976Z",
932
+ "shell.execute_reply": "2023-10-21T07:47:28.532251Z"
933
+ },
934
+ "papermill": {
935
+ "duration": 6690.621815,
936
+ "end_time": "2023-10-21T07:47:28.534533",
937
+ "exception": false,
938
+ "start_time": "2023-10-21T05:55:57.912718",
939
+ "status": "completed"
940
+ },
941
+ "tags": []
942
+ },
943
+ "outputs": [
944
+ {
945
+ "name": "stderr",
946
+ "output_type": "stream",
947
+ "text": [
948
+ "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"
949
+ ]
950
+ },
951
+ {
952
+ "data": {
953
+ "text/html": [
954
+ "\n",
955
+ " <div>\n",
956
+ " \n",
957
+ " <progress value='1119' max='1119' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
958
+ " [1119/1119 1:51:20, Epoch 2/3]\n",
959
+ " </div>\n",
960
+ " <table border=\"1\" class=\"dataframe\">\n",
961
+ " <thead>\n",
962
+ " <tr style=\"text-align: left;\">\n",
963
+ " <th>Step</th>\n",
964
+ " <th>Training Loss</th>\n",
965
+ " </tr>\n",
966
+ " </thead>\n",
967
+ " <tbody>\n",
968
+ " <tr>\n",
969
+ " <td>50</td>\n",
970
+ " <td>1.057700</td>\n",
971
+ " </tr>\n",
972
+ " <tr>\n",
973
+ " <td>100</td>\n",
974
+ " <td>0.489600</td>\n",
975
+ " </tr>\n",
976
+ " <tr>\n",
977
+ " <td>150</td>\n",
978
+ " <td>0.260400</td>\n",
979
+ " </tr>\n",
980
+ " <tr>\n",
981
+ " <td>200</td>\n",
982
+ " <td>0.147400</td>\n",
983
+ " </tr>\n",
984
+ " <tr>\n",
985
+ " <td>250</td>\n",
986
+ " <td>0.081700</td>\n",
987
+ " </tr>\n",
988
+ " <tr>\n",
989
+ " <td>300</td>\n",
990
+ " <td>0.057000</td>\n",
991
+ " </tr>\n",
992
+ " <tr>\n",
993
+ " <td>350</td>\n",
994
+ " <td>0.041700</td>\n",
995
+ " </tr>\n",
996
+ " <tr>\n",
997
+ " <td>400</td>\n",
998
+ " <td>0.037400</td>\n",
999
+ " </tr>\n",
1000
+ " <tr>\n",
1001
+ " <td>450</td>\n",
1002
+ " <td>0.033400</td>\n",
1003
+ " </tr>\n",
1004
+ " <tr>\n",
1005
+ " <td>500</td>\n",
1006
+ " <td>0.029200</td>\n",
1007
+ " </tr>\n",
1008
+ " <tr>\n",
1009
+ " <td>550</td>\n",
1010
+ " <td>0.027600</td>\n",
1011
+ " </tr>\n",
1012
+ " <tr>\n",
1013
+ " <td>600</td>\n",
1014
+ " <td>0.027100</td>\n",
1015
+ " </tr>\n",
1016
+ " <tr>\n",
1017
+ " <td>650</td>\n",
1018
+ " <td>0.025000</td>\n",
1019
+ " </tr>\n",
1020
+ " <tr>\n",
1021
+ " <td>700</td>\n",
1022
+ " <td>0.024600</td>\n",
1023
+ " </tr>\n",
1024
+ " <tr>\n",
1025
+ " <td>750</td>\n",
1026
+ " <td>0.024500</td>\n",
1027
+ " </tr>\n",
1028
+ " <tr>\n",
1029
+ " <td>800</td>\n",
1030
+ " <td>0.020400</td>\n",
1031
+ " </tr>\n",
1032
+ " <tr>\n",
1033
+ " <td>850</td>\n",
1034
+ " <td>0.020500</td>\n",
1035
+ " </tr>\n",
1036
+ " <tr>\n",
1037
+ " <td>900</td>\n",
1038
+ " <td>0.020800</td>\n",
1039
+ " </tr>\n",
1040
+ " <tr>\n",
1041
+ " <td>950</td>\n",
1042
+ " <td>0.020800</td>\n",
1043
+ " </tr>\n",
1044
+ " <tr>\n",
1045
+ " <td>1000</td>\n",
1046
+ " <td>0.020900</td>\n",
1047
+ " </tr>\n",
1048
+ " <tr>\n",
1049
+ " <td>1050</td>\n",
1050
+ " <td>0.021100</td>\n",
1051
+ " </tr>\n",
1052
+ " <tr>\n",
1053
+ " <td>1100</td>\n",
1054
+ " <td>0.020300</td>\n",
1055
+ " </tr>\n",
1056
+ " </tbody>\n",
1057
+ "</table><p>"
1058
+ ],
1059
+ "text/plain": [
1060
+ "<IPython.core.display.HTML object>"
1061
+ ]
1062
+ },
1063
+ "metadata": {},
1064
+ "output_type": "display_data"
1065
+ },
1066
+ {
1067
+ "data": {
1068
+ "text/plain": [
1069
+ "TrainOutput(global_step=1119, training_loss=0.11247422747893245, metrics={'train_runtime': 6689.036, 'train_samples_per_second': 0.67, 'train_steps_per_second': 0.167, 'total_flos': 1.4445806420565197e+17, 'train_loss': 0.11247422747893245, 'epoch': 3.0})"
1070
+ ]
1071
+ },
1072
+ "execution_count": 13,
1073
+ "metadata": {},
1074
+ "output_type": "execute_result"
1075
+ }
1076
+ ],
1077
+ "source": [
1078
+ "trainer = transformers.Trainer(\n",
1079
+ " model=model,\n",
1080
+ " train_dataset=data[\"train\"],\n",
1081
+ "# eval_dataset=data[\"test\"],\n",
1082
+ " args=training_args,\n",
1083
+ " data_collator=transformers.DataCollatorForLanguageModeling(tokenizer, mlm=False),\n",
1084
+ ")\n",
1085
+ "model.config.use_cache = False # silence the warnings. Please re-enable for inference!\n",
1086
+ "\n",
1087
+ "checkpoint_path = Path(\"/content/artifacts/checkpoints\")\n",
1088
+ "\n",
1089
+ "# Only set resume_from_checkpoint True when directory exists and contains files\n",
1090
+ "resume_from_checkpoint = checkpoint_path.is_dir() and any(checkpoint_path.iterdir())\n",
1091
+ "if resume_from_checkpoint:\n",
1092
+ " print(\"Resuming from checkpoint:\", list(checkpoint_path.rglob(\"\")))\n",
1093
+ "trainer.train(resume_from_checkpoint=resume_from_checkpoint)"
1094
+ ]
1095
+ },
1096
+ {
1097
+ "cell_type": "code",
1098
+ "execution_count": 14,
1099
+ "id": "172e47a7-400e-4f82-a5e3-38135ecf532f",
1100
+ "metadata": {
1101
+ "execution": {
1102
+ "iopub.execute_input": "2023-10-21T07:47:30.428814Z",
1103
+ "iopub.status.busy": "2023-10-21T07:47:30.428055Z",
1104
+ "iopub.status.idle": "2023-10-21T07:47:46.873882Z",
1105
+ "shell.execute_reply": "2023-10-21T07:47:46.873193Z"
1106
+ },
1107
+ "papermill": {
1108
+ "duration": 17.445662,
1109
+ "end_time": "2023-10-21T07:47:46.875405",
1110
+ "exception": false,
1111
+ "start_time": "2023-10-21T07:47:29.429743",
1112
+ "status": "completed"
1113
+ },
1114
+ "tags": []
1115
+ },
1116
+ "outputs": [
1117
+ {
1118
+ "data": {
1119
+ "text/plain": [
1120
+ "PeftModelForCausalLM(\n",
1121
+ " (base_model): LoraModel(\n",
1122
+ " (model): LlamaForCausalLM(\n",
1123
+ " (model): LlamaModel(\n",
1124
+ " (embed_tokens): ModulesToSaveWrapper(\n",
1125
+ " (original_module): Embedding(32001, 4096)\n",
1126
+ " (modules_to_save): ModuleDict(\n",
1127
+ " (default): Embedding(32001, 4096)\n",
1128
+ " )\n",
1129
+ " )\n",
1130
+ " (layers): ModuleList(\n",
1131
+ " (0-31): 32 x LlamaDecoderLayer(\n",
1132
+ " (self_attn): LlamaAttention(\n",
1133
+ " (q_proj): Linear(\n",
1134
+ " in_features=4096, out_features=4096, bias=False\n",
1135
+ " (lora_dropout): ModuleDict(\n",
1136
+ " (default): Dropout(p=0.05, inplace=False)\n",
1137
+ " )\n",
1138
+ " (lora_A): ModuleDict(\n",
1139
+ " (default): Linear(in_features=4096, out_features=16, bias=False)\n",
1140
+ " )\n",
1141
+ " (lora_B): ModuleDict(\n",
1142
+ " (default): Linear(in_features=16, out_features=4096, bias=False)\n",
1143
+ " )\n",
1144
+ " (lora_embedding_A): ParameterDict()\n",
1145
+ " (lora_embedding_B): ParameterDict()\n",
1146
+ " )\n",
1147
+ " (k_proj): Linear(\n",
1148
+ " in_features=4096, out_features=4096, bias=False\n",
1149
+ " (lora_dropout): ModuleDict(\n",
1150
+ " (default): Dropout(p=0.05, inplace=False)\n",
1151
+ " )\n",
1152
+ " (lora_A): ModuleDict(\n",
1153
+ " (default): Linear(in_features=4096, out_features=16, bias=False)\n",
1154
+ " )\n",
1155
+ " (lora_B): ModuleDict(\n",
1156
+ " (default): Linear(in_features=16, out_features=4096, bias=False)\n",
1157
+ " )\n",
1158
+ " (lora_embedding_A): ParameterDict()\n",
1159
+ " (lora_embedding_B): ParameterDict()\n",
1160
+ " )\n",
1161
+ " (v_proj): Linear(\n",
1162
+ " in_features=4096, out_features=4096, bias=False\n",
1163
+ " (lora_dropout): ModuleDict(\n",
1164
+ " (default): Dropout(p=0.05, inplace=False)\n",
1165
+ " )\n",
1166
+ " (lora_A): ModuleDict(\n",
1167
+ " (default): Linear(in_features=4096, out_features=16, bias=False)\n",
1168
+ " )\n",
1169
+ " (lora_B): ModuleDict(\n",
1170
+ " (default): Linear(in_features=16, out_features=4096, bias=False)\n",
1171
+ " )\n",
1172
+ " (lora_embedding_A): ParameterDict()\n",
1173
+ " (lora_embedding_B): ParameterDict()\n",
1174
+ " )\n",
1175
+ " (o_proj): Linear(\n",
1176
+ " in_features=4096, out_features=4096, bias=False\n",
1177
+ " (lora_dropout): ModuleDict(\n",
1178
+ " (default): Dropout(p=0.05, inplace=False)\n",
1179
+ " )\n",
1180
+ " (lora_A): ModuleDict(\n",
1181
+ " (default): Linear(in_features=4096, out_features=16, bias=False)\n",
1182
+ " )\n",
1183
+ " (lora_B): ModuleDict(\n",
1184
+ " (default): Linear(in_features=16, out_features=4096, bias=False)\n",
1185
+ " )\n",
1186
+ " (lora_embedding_A): ParameterDict()\n",
1187
+ " (lora_embedding_B): ParameterDict()\n",
1188
+ " )\n",
1189
+ " (rotary_emb): LlamaRotaryEmbedding()\n",
1190
+ " )\n",
1191
+ " (mlp): LlamaMLP(\n",
1192
+ " (gate_proj): Linear(\n",
1193
+ " in_features=4096, out_features=11008, bias=False\n",
1194
+ " (lora_dropout): ModuleDict(\n",
1195
+ " (default): Dropout(p=0.05, inplace=False)\n",
1196
+ " )\n",
1197
+ " (lora_A): ModuleDict(\n",
1198
+ " (default): Linear(in_features=4096, out_features=16, bias=False)\n",
1199
+ " )\n",
1200
+ " (lora_B): ModuleDict(\n",
1201
+ " (default): Linear(in_features=16, out_features=11008, bias=False)\n",
1202
+ " )\n",
1203
+ " (lora_embedding_A): ParameterDict()\n",
1204
+ " (lora_embedding_B): ParameterDict()\n",
1205
+ " )\n",
1206
+ " (up_proj): Linear(\n",
1207
+ " in_features=4096, out_features=11008, bias=False\n",
1208
+ " (lora_dropout): ModuleDict(\n",
1209
+ " (default): Dropout(p=0.05, inplace=False)\n",
1210
+ " )\n",
1211
+ " (lora_A): ModuleDict(\n",
1212
+ " (default): Linear(in_features=4096, out_features=16, bias=False)\n",
1213
+ " )\n",
1214
+ " (lora_B): ModuleDict(\n",
1215
+ " (default): Linear(in_features=16, out_features=11008, bias=False)\n",
1216
+ " )\n",
1217
+ " (lora_embedding_A): ParameterDict()\n",
1218
+ " (lora_embedding_B): ParameterDict()\n",
1219
+ " )\n",
1220
+ " (down_proj): Linear(\n",
1221
+ " in_features=11008, out_features=4096, bias=False\n",
1222
+ " (lora_dropout): ModuleDict(\n",
1223
+ " (default): Dropout(p=0.05, inplace=False)\n",
1224
+ " )\n",
1225
+ " (lora_A): ModuleDict(\n",
1226
+ " (default): Linear(in_features=11008, out_features=16, bias=False)\n",
1227
+ " )\n",
1228
+ " (lora_B): ModuleDict(\n",
1229
+ " (default): Linear(in_features=16, out_features=4096, bias=False)\n",
1230
+ " )\n",
1231
+ " (lora_embedding_A): ParameterDict()\n",
1232
+ " (lora_embedding_B): ParameterDict()\n",
1233
+ " )\n",
1234
+ " (act_fn): SiLUActivation()\n",
1235
+ " )\n",
1236
+ " (input_layernorm): LlamaRMSNorm()\n",
1237
+ " (post_attention_layernorm): LlamaRMSNorm()\n",
1238
+ " )\n",
1239
+ " )\n",
1240
+ " (norm): LlamaRMSNorm()\n",
1241
+ " )\n",
1242
+ " (lm_head): ModulesToSaveWrapper(\n",
1243
+ " (original_module): Linear(in_features=4096, out_features=32001, bias=False)\n",
1244
+ " (modules_to_save): ModuleDict(\n",
1245
+ " (default): Linear(in_features=4096, out_features=32001, bias=False)\n",
1246
+ " )\n",
1247
+ " )\n",
1248
+ " )\n",
1249
+ " )\n",
1250
+ ")"
1251
+ ]
1252
+ },
1253
+ "execution_count": 14,
1254
+ "metadata": {},
1255
+ "output_type": "execute_result"
1256
+ }
1257
+ ],
1258
+ "source": [
1259
+ "model.save_pretrained(trained_model_path_lora)\n",
1260
+ "model"
1261
+ ]
1262
+ },
1263
+ {
1264
+ "cell_type": "code",
1265
+ "execution_count": 15,
1266
+ "id": "dea4e68e-57a7-48bd-bad9-f03dfe3f8a06",
1267
+ "metadata": {
1268
+ "execution": {
1269
+ "iopub.execute_input": "2023-10-21T07:47:48.699946Z",
1270
+ "iopub.status.busy": "2023-10-21T07:47:48.699212Z",
1271
+ "iopub.status.idle": "2023-10-21T07:47:48.949489Z",
1272
+ "shell.execute_reply": "2023-10-21T07:47:48.948666Z"
1273
+ },
1274
+ "papermill": {
1275
+ "duration": 1.175557,
1276
+ "end_time": "2023-10-21T07:47:48.950963",
1277
+ "exception": false,
1278
+ "start_time": "2023-10-21T07:47:47.775406",
1279
+ "status": "completed"
1280
+ },
1281
+ "tags": []
1282
+ },
1283
+ "outputs": [
1284
+ {
1285
+ "name": "stdout",
1286
+ "output_type": "stream",
1287
+ "text": [
1288
+ "total 1.2G\r\n",
1289
+ " 512 -rw-r--r-- 1 root 3003 88 Oct 21 07:47 README.md\r\n",
1290
+ "1.0K -rw-r--r-- 1 root 3003 550 Oct 21 07:47 adapter_config.json\r\n",
1291
+ "1.2G -rw-r--r-- 1 root 3003 1.2G Oct 21 07:47 adapter_model.bin\r\n"
1292
+ ]
1293
+ },
1294
+ {
1295
+ "name": "stderr",
1296
+ "output_type": "stream",
1297
+ "text": [
1298
+ "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
1299
+ "To disable this warning, you can either:\n",
1300
+ "\t- Avoid using `tokenizers` before the fork if possible\n",
1301
+ "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
1302
+ ]
1303
+ }
1304
+ ],
1305
+ "source": [
1306
+ "! ls -lash {trained_model_path_lora}"
1307
+ ]
1308
+ },
1309
+ {
1310
+ "cell_type": "code",
1311
+ "execution_count": 16,
1312
+ "id": "09db36b7-ead6-4368-9bfb-13ba1ba800a5",
1313
+ "metadata": {
1314
+ "execution": {
1315
+ "iopub.execute_input": "2023-10-21T07:47:50.799568Z",
1316
+ "iopub.status.busy": "2023-10-21T07:47:50.798899Z",
1317
+ "iopub.status.idle": "2023-10-21T07:48:42.484011Z",
1318
+ "shell.execute_reply": "2023-10-21T07:48:42.483286Z"
1319
+ },
1320
+ "papermill": {
1321
+ "duration": 53.672087,
1322
+ "end_time": "2023-10-21T07:48:43.522023",
1323
+ "exception": false,
1324
+ "start_time": "2023-10-21T07:47:49.849936",
1325
+ "status": "completed"
1326
+ },
1327
+ "tags": []
1328
+ },
1329
+ "outputs": [
1330
+ {
1331
+ "data": {
1332
+ "text/plain": [
1333
+ "LlamaForCausalLM(\n",
1334
+ " (model): LlamaModel(\n",
1335
+ " (embed_tokens): Embedding(32001, 4096)\n",
1336
+ " (layers): ModuleList(\n",
1337
+ " (0-31): 32 x LlamaDecoderLayer(\n",
1338
+ " (self_attn): LlamaAttention(\n",
1339
+ " (q_proj): Linear(in_features=4096, out_features=4096, bias=False)\n",
1340
+ " (k_proj): Linear(in_features=4096, out_features=4096, bias=False)\n",
1341
+ " (v_proj): Linear(in_features=4096, out_features=4096, bias=False)\n",
1342
+ " (o_proj): Linear(in_features=4096, out_features=4096, bias=False)\n",
1343
+ " (rotary_emb): LlamaRotaryEmbedding()\n",
1344
+ " )\n",
1345
+ " (mlp): LlamaMLP(\n",
1346
+ " (gate_proj): Linear(in_features=4096, out_features=11008, bias=False)\n",
1347
+ " (up_proj): Linear(in_features=4096, out_features=11008, bias=False)\n",
1348
+ " (down_proj): Linear(in_features=11008, out_features=4096, bias=False)\n",
1349
+ " (act_fn): SiLUActivation()\n",
1350
+ " )\n",
1351
+ " (input_layernorm): LlamaRMSNorm()\n",
1352
+ " (post_attention_layernorm): LlamaRMSNorm()\n",
1353
+ " )\n",
1354
+ " )\n",
1355
+ " (norm): LlamaRMSNorm()\n",
1356
+ " )\n",
1357
+ " (lm_head): Linear(in_features=4096, out_features=32001, bias=False)\n",
1358
+ ")"
1359
+ ]
1360
+ },
1361
+ "execution_count": 16,
1362
+ "metadata": {},
1363
+ "output_type": "execute_result"
1364
+ }
1365
+ ],
1366
+ "source": [
1367
+ "model = model.merge_and_unload().half()\n",
1368
+ "model"
1369
+ ]
1370
+ },
1371
+ {
1372
+ "cell_type": "code",
1373
+ "execution_count": 17,
1374
+ "id": "270a9a72-3a12-4d83-aa7d-2d167cb28cb4",
1375
+ "metadata": {
1376
+ "execution": {
1377
+ "iopub.execute_input": "2023-10-21T07:48:45.317087Z",
1378
+ "iopub.status.busy": "2023-10-21T07:48:45.316765Z",
1379
+ "iopub.status.idle": "2023-10-21T07:48:45.559747Z",
1380
+ "shell.execute_reply": "2023-10-21T07:48:45.558874Z"
1381
+ },
1382
+ "papermill": {
1383
+ "duration": 1.11534,
1384
+ "end_time": "2023-10-21T07:48:45.561396",
1385
+ "exception": false,
1386
+ "start_time": "2023-10-21T07:48:44.446056",
1387
+ "status": "completed"
1388
+ },
1389
+ "tags": []
1390
+ },
1391
+ "outputs": [
1392
+ {
1393
+ "name": "stdout",
1394
+ "output_type": "stream",
1395
+ "text": [
1396
+ "total 0\r\n",
1397
+ "drwxr-xr-x 1 root 3003 0 Oct 21 05:55 checkpoints\r\n",
1398
+ "drwxr-xr-x 1 root 3003 0 Oct 21 05:55 lora\r\n",
1399
+ "drwxr-xr-x 1 root 3003 0 Oct 21 05:49 src\r\n"
1400
+ ]
1401
+ },
1402
+ {
1403
+ "name": "stderr",
1404
+ "output_type": "stream",
1405
+ "text": [
1406
+ "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
1407
+ "To disable this warning, you can either:\n",
1408
+ "\t- Avoid using `tokenizers` before the fork if possible\n",
1409
+ "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
1410
+ ]
1411
+ }
1412
+ ],
1413
+ "source": [
1414
+ "! ls -l {trained_model_path}"
1415
+ ]
1416
+ },
1417
+ {
1418
+ "cell_type": "code",
1419
+ "execution_count": 18,
1420
+ "id": "260e9d79-6eb8-4516-bf8f-825a25606391",
1421
+ "metadata": {
1422
+ "execution": {
1423
+ "iopub.execute_input": "2023-10-21T07:48:47.429854Z",
1424
+ "iopub.status.busy": "2023-10-21T07:48:47.429062Z",
1425
+ "iopub.status.idle": "2023-10-21T07:51:23.634942Z",
1426
+ "shell.execute_reply": "2023-10-21T07:51:23.634264Z"
1427
+ },
1428
+ "papermill": {
1429
+ "duration": 158.141645,
1430
+ "end_time": "2023-10-21T07:51:24.665966",
1431
+ "exception": false,
1432
+ "start_time": "2023-10-21T07:48:46.524321",
1433
+ "status": "completed"
1434
+ },
1435
+ "tags": []
1436
+ },
1437
+ "outputs": [
1438
+ {
1439
+ "data": {
1440
+ "text/plain": [
1441
+ "('/content/artifacts/tokenizer_config.json',\n",
1442
+ " '/content/artifacts/special_tokens_map.json',\n",
1443
+ " '/content/artifacts/tokenizer.model',\n",
1444
+ " '/content/artifacts/added_tokens.json',\n",
1445
+ " '/content/artifacts/tokenizer.json')"
1446
+ ]
1447
+ },
1448
+ "execution_count": 18,
1449
+ "metadata": {},
1450
+ "output_type": "execute_result"
1451
+ }
1452
+ ],
1453
+ "source": [
1454
+ "model.save_pretrained(trained_model_path)\n",
1455
+ "tokenizer.save_pretrained(trained_model_path)"
1456
+ ]
1457
+ },
1458
+ {
1459
+ "cell_type": "code",
1460
+ "execution_count": 19,
1461
+ "id": "6d90a920-fb22-4291-8466-411ff41e31be",
1462
+ "metadata": {
1463
+ "execution": {
1464
+ "iopub.execute_input": "2023-10-21T07:51:26.557278Z",
1465
+ "iopub.status.busy": "2023-10-21T07:51:26.556503Z",
1466
+ "iopub.status.idle": "2023-10-21T07:51:26.796901Z",
1467
+ "shell.execute_reply": "2023-10-21T07:51:26.796120Z"
1468
+ },
1469
+ "papermill": {
1470
+ "duration": 1.217017,
1471
+ "end_time": "2023-10-21T07:51:26.798456",
1472
+ "exception": false,
1473
+ "start_time": "2023-10-21T07:51:25.581439",
1474
+ "status": "completed"
1475
+ },
1476
+ "tags": []
1477
+ },
1478
+ "outputs": [
1479
+ {
1480
+ "name": "stdout",
1481
+ "output_type": "stream",
1482
+ "text": [
1483
+ "total 13G\r\n",
1484
+ " 512 -rw-r--r-- 1 root 3003 21 Oct 21 07:51 added_tokens.json\r\n",
1485
+ " 0 drwxr-xr-x 1 root 3003 0 Oct 21 05:55 checkpoints\r\n",
1486
+ "1.0K -rw-r--r-- 1 root 3003 648 Oct 21 07:48 config.json\r\n",
1487
+ " 512 -rw-r--r-- 1 root 3003 183 Oct 21 07:48 generation_config.json\r\n",
1488
+ " 0 drwxr-xr-x 1 root 3003 0 Oct 21 05:55 lora\r\n",
1489
+ "9.3G -rw-r--r-- 1 root 3003 9.3G Oct 21 07:49 pytorch_model-00001-of-00002.bin\r\n",
1490
+ "3.3G -rw-r--r-- 1 root 3003 3.3G Oct 21 07:50 pytorch_model-00002-of-00002.bin\r\n",
1491
+ " 24K -rw-r--r-- 1 root 3003 24K Oct 21 07:51 pytorch_model.bin.index.json\r\n",
1492
+ "1.0K -rw-r--r-- 1 root 3003 552 Oct 21 07:51 special_tokens_map.json\r\n",
1493
+ " 0 drwxr-xr-x 1 root 3003 0 Oct 21 05:49 src\r\n",
1494
+ "1.8M -rw-r--r-- 1 root 3003 1.8M Oct 21 07:51 tokenizer.json\r\n",
1495
+ "489K -rw-r--r-- 1 root 3003 489K Oct 21 07:51 tokenizer.model\r\n",
1496
+ "1.5K -rw-r--r-- 1 root 3003 1.1K Oct 21 07:51 tokenizer_config.json\r\n"
1497
+ ]
1498
+ },
1499
+ {
1500
+ "name": "stderr",
1501
+ "output_type": "stream",
1502
+ "text": [
1503
+ "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
1504
+ "To disable this warning, you can either:\n",
1505
+ "\t- Avoid using `tokenizers` before the fork if possible\n",
1506
+ "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
1507
+ ]
1508
+ }
1509
+ ],
1510
+ "source": [
1511
+ "! ls -lash {trained_model_path}"
1512
+ ]
1513
+ },
1514
+ {
1515
+ "cell_type": "code",
1516
+ "execution_count": 20,
1517
+ "id": "202a694a",
1518
+ "metadata": {
1519
+ "execution": {
1520
+ "iopub.execute_input": "2023-10-21T07:51:28.659968Z",
1521
+ "iopub.status.busy": "2023-10-21T07:51:28.659180Z"
1522
+ },
1523
+ "papermill": {
1524
+ "duration": null,
1525
+ "end_time": null,
1526
+ "exception": false,
1527
+ "start_time": "2023-10-21T07:51:27.744359",
1528
+ "status": "running"
1529
+ },
1530
+ "tags": []
1531
+ },
1532
+ "outputs": [
1533
+ {
1534
+ "data": {
1535
+ "application/vnd.jupyter.widget-view+json": {
1536
+ "model_id": "671a3174ba724efd8ea3d2b141b75b98",
1537
+ "version_major": 2,
1538
+ "version_minor": 0
1539
+ },
1540
+ "text/plain": [
1541
+ "pytorch_model-00002-of-00002.bin: 0%| | 0.00/3.50G [00:00<?, ?B/s]"
1542
+ ]
1543
+ },
1544
+ "metadata": {},
1545
+ "output_type": "display_data"
1546
+ },
1547
+ {
1548
+ "data": {
1549
+ "application/vnd.jupyter.widget-view+json": {
1550
+ "model_id": "f1a97636b33c4485bb557cf32d97950f",
1551
+ "version_major": 2,
1552
+ "version_minor": 0
1553
+ },
1554
+ "text/plain": [
1555
+ "Upload 2 LFS files: 0%| | 0/2 [00:00<?, ?it/s]"
1556
+ ]
1557
+ },
1558
+ "metadata": {},
1559
+ "output_type": "display_data"
1560
+ },
1561
+ {
1562
+ "data": {
1563
+ "application/vnd.jupyter.widget-view+json": {
1564
+ "model_id": "268a578f3d774938990b28374f4d3957",
1565
+ "version_major": 2,
1566
+ "version_minor": 0
1567
+ },
1568
+ "text/plain": [
1569
+ "pytorch_model-00001-of-00002.bin: 0%| | 0.00/9.98G [00:00<?, ?B/s]"
1570
+ ]
1571
+ },
1572
+ "metadata": {},
1573
+ "output_type": "display_data"
1574
+ }
1575
+ ],
1576
+ "source": [
1577
+ "from huggingface_hub import HfApi\n",
1578
+ "import shutil\n",
1579
+ "\n",
1580
+ "tokenizer_model_path_base = Path(model_path) / \"tokenizer.model\"\n",
1581
+ "tokenizer_model_path_trained = Path(trained_model_path) / \"tokenizer.model\"\n",
1582
+ "if tokenizer_model_path_base.exists() and not tokenizer_model_path_trained.exists():\n",
1583
+ " shutil.copy(tokenizer_model_path_base, tokenizer_model_path_trained)\n",
1584
+ "\n",
1585
+ "repo_id = params.get(\"push_to_hub\")\n",
1586
+ "if repo_id:\n",
1587
+ " model.push_to_hub(repo_id)\n",
1588
+ " tokenizer.push_to_hub(repo_id)\n",
1589
+ " hf_api = HfApi()\n",
1590
+ " # Upload tokenizer.model if it was in base model\n",
1591
+ " if tokenizer_model_path_base.exists():\n",
1592
+ " hf_api.upload_file(\n",
1593
+ " path_or_fileobj=tokenizer_model_path_base,\n",
1594
+ " path_in_repo=tokenizer_model_path_base.name,\n",
1595
+ " repo_id=repo_id,\n",
1596
+ " )\n",
1597
+ " logs_path = Path(\"/content/artifacts/src/train.ipynb\")\n",
1598
+ " if logs_path.exists():\n",
1599
+ " hf_api.upload_file(\n",
1600
+ " path_or_fileobj=logs_path,\n",
1601
+ " path_in_repo=logs_path.name,\n",
1602
+ " repo_id=repo_id,\n",
1603
+ " )\n"
1604
+ ]
1605
+ }
1606
+ ],
1607
+ "metadata": {
1608
+ "kernelspec": {
1609
+ "display_name": "Python 3 (ipykernel)",
1610
+ "language": "python",
1611
+ "name": "python3"
1612
+ },
1613
+ "language_info": {
1614
+ "codemirror_mode": {
1615
+ "name": "ipython",
1616
+ "version": 3
1617
+ },
1618
+ "file_extension": ".py",
1619
+ "mimetype": "text/x-python",
1620
+ "name": "python",
1621
+ "nbconvert_exporter": "python",
1622
+ "pygments_lexer": "ipython3",
1623
+ "version": "3.10.12"
1624
+ },
1625
+ "papermill": {
1626
+ "default_parameters": {},
1627
+ "duration": null,
1628
+ "end_time": null,
1629
+ "environment_variables": {},
1630
+ "exception": null,
1631
+ "input_path": "/content/src/train.ipynb",
1632
+ "output_path": "/content/artifacts/src/train.ipynb",
1633
+ "parameters": {},
1634
+ "start_time": "2023-10-21T05:49:13.952191",
1635
+ "version": "2.4.0"
1636
+ }
1637
+ },
1638
+ "nbformat": 4,
1639
+ "nbformat_minor": 5
1640
+ }