longhoang06 commited on
Commit
53476bd
·
1 Parent(s): 2dca23a

Upload folder using huggingface_hub

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. OVM/.ipynb_checkpoints/create_mc_dataset-checkpoint.ipynb +620 -0
  2. OVM/.ipynb_checkpoints/predict_notebook-checkpoint.ipynb +0 -0
  3. OVM/Dockerfile.ClickAI +51 -0
  4. OVM/OVM/.ipynb_checkpoints/Untitled-checkpoint.ipynb +1309 -0
  5. OVM/OVM/.ipynb_checkpoints/__init__-checkpoint.py +0 -0
  6. OVM/OVM/.ipynb_checkpoints/build_verifier-checkpoint.py +100 -0
  7. OVM/OVM/.ipynb_checkpoints/gen_solutions-1-checkpoint.py +90 -0
  8. OVM/OVM/.ipynb_checkpoints/gen_solutions-2-checkpoint.py +83 -0
  9. OVM/OVM/.ipynb_checkpoints/prompt_template-checkpoint.py +29 -0
  10. OVM/OVM/.ipynb_checkpoints/train_generator-checkpoint.py +163 -0
  11. OVM/OVM/.ipynb_checkpoints/train_generator-checkpoint.sh +2 -0
  12. OVM/OVM/.ipynb_checkpoints/v_dataset-checkpoint.py +81 -0
  13. OVM/OVM/Untitled.ipynb +0 -0
  14. OVM/OVM/__init__.py +0 -0
  15. OVM/OVM/__pycache__/build_verifier.cpython-310.pyc +0 -0
  16. OVM/OVM/__pycache__/prompt_template.cpython-310.pyc +0 -0
  17. OVM/OVM/__pycache__/v_dataset.cpython-310.pyc +0 -0
  18. OVM/OVM/build_verifier.py +100 -0
  19. OVM/OVM/checkpoint/generator/README.md +220 -0
  20. OVM/OVM/checkpoint/generator/adapter_config.json +29 -0
  21. OVM/OVM/checkpoint/generator/adapter_model.bin +3 -0
  22. OVM/OVM/data/.ipynb_checkpoints/merge-checkpoint.ipynb +192 -0
  23. OVM/OVM/data/gen1/data-00000-of-00001.arrow +3 -0
  24. OVM/OVM/data/gen1/dataset_info.json +24 -0
  25. OVM/OVM/data/gen1/state.json +18 -0
  26. OVM/OVM/data/gen2/data-00000-of-00001.arrow +3 -0
  27. OVM/OVM/data/gen2/dataset_info.json +24 -0
  28. OVM/OVM/data/gen2/state.json +13 -0
  29. OVM/OVM/data/gen_all_375000/data-00000-of-00001.arrow +3 -0
  30. OVM/OVM/data/gen_all_375000/dataset_info.json +24 -0
  31. OVM/OVM/data/gen_all_375000/state.json +18 -0
  32. OVM/OVM/data/merge.ipynb +257 -0
  33. OVM/OVM/gen_solutions-1.py +90 -0
  34. OVM/OVM/gen_solutions-2.py +83 -0
  35. OVM/OVM/generatation/.ipynb_checkpoints/main-checkpoint.py +117 -0
  36. OVM/OVM/generatation/.ipynb_checkpoints/main_108-checkpoint.py +108 -0
  37. OVM/OVM/generatation/.ipynb_checkpoints/main_121-checkpoint.py +116 -0
  38. OVM/OVM/generatation/__pycache__/prompt_template.cpython-310.pyc +0 -0
  39. OVM/OVM/generatation/__pycache__/prompt_template.cpython-311.pyc +0 -0
  40. OVM/OVM/generatation/__pycache__/prompt_template.cpython-38.pyc +0 -0
  41. OVM/OVM/generatation/checkpoint/generator/README.md +220 -0
  42. OVM/OVM/generatation/checkpoint/generator/adapter_config.json +29 -0
  43. OVM/OVM/generatation/checkpoint/generator/adapter_model.bin +3 -0
  44. OVM/OVM/generatation/main.py +117 -0
  45. OVM/OVM/generatation/main_108.py +108 -0
  46. OVM/OVM/generatation/main_121.py +116 -0
  47. OVM/OVM/generatation/output_dir/.ipynb_checkpoints/merge-checkpoint.ipynb +179 -0
  48. OVM/OVM/generatation/output_dir/1000_1100/data-00000-of-00001.arrow +3 -0
  49. OVM/OVM/generatation/output_dir/1000_1100/dataset_info.json +24 -0
  50. OVM/OVM/generatation/output_dir/1000_1100/state.json +13 -0
OVM/.ipynb_checkpoints/create_mc_dataset-checkpoint.ipynb ADDED
@@ -0,0 +1,620 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 3,
6
+ "id": "477e1474-49ab-4c29-8c32-eac32c231679",
7
+ "metadata": {
8
+ "tags": []
9
+ },
10
+ "outputs": [
11
+ {
12
+ "name": "stdout",
13
+ "output_type": "stream",
14
+ "text": [
15
+ "/home/jovyan/nas_comm/1_user/[email protected]/research/math/math-mistral/docker/extractive-qa-mrc\n"
16
+ ]
17
+ },
18
+ {
19
+ "name": "stderr",
20
+ "output_type": "stream",
21
+ "text": [
22
+ "/opt/conda/envs/mistral/lib/python3.10/site-packages/IPython/core/magics/osm.py:417: UserWarning: using dhist requires you to install the `pickleshare` library.\n",
23
+ " self.shell.db['dhist'] = compress_dhist(dhist)[-100:]\n"
24
+ ]
25
+ }
26
+ ],
27
+ "source": [
28
+ "%cd extractive-qa-mrc"
29
+ ]
30
+ },
31
+ {
32
+ "cell_type": "code",
33
+ "execution_count": 5,
34
+ "id": "8282b3fd-e955-4021-857e-1cb721f6a04e",
35
+ "metadata": {
36
+ "tags": []
37
+ },
38
+ "outputs": [
39
+ {
40
+ "name": "stdout",
41
+ "output_type": "stream",
42
+ "text": [
43
+ "Token will not been saved to git credential helper. Pass `add_to_git_credential=True` if you want to set the git credential as well.\n",
44
+ "Token is valid (permission: write).\n",
45
+ "Your token has been saved to /home/jovyan/.cache/huggingface/token\n",
46
+ "Login successful\n"
47
+ ]
48
+ }
49
+ ],
50
+ "source": [
51
+ "from huggingface_hub import login\n",
52
+ "from transformers import AutoTokenizer\n",
53
+ "from datasets import load_dataset\n",
54
+ "import re\n",
55
+ "import torch\n",
56
+ "from tqdm.auto import tqdm\n",
57
+ "import random\n",
58
+ "random.seed(42)\n",
59
+ "login(token = \"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\")"
60
+ ]
61
+ },
62
+ {
63
+ "cell_type": "code",
64
+ "execution_count": 6,
65
+ "id": "b3230e48-d2f8-4be6-8fa5-b5018f493685",
66
+ "metadata": {
67
+ "tags": []
68
+ },
69
+ "outputs": [
70
+ {
71
+ "name": "stderr",
72
+ "output_type": "stream",
73
+ "text": [
74
+ "Downloading readme: 100%|██████████| 475/475 [00:00<00:00, 1.26MB/s]\n",
75
+ "Downloading data files: 0%| | 0/1 [00:00<?, ?it/s]\n",
76
+ "Downloading data: 0%| | 0.00/2.75M [00:00<?, ?B/s]\u001b[A\n",
77
+ "Downloading data: 100%|██████████| 2.75M/2.75M [00:01<00:00, 1.52MB/s]\u001b[A\n",
78
+ "Downloading data files: 100%|██████████| 1/1 [00:01<00:00, 1.82s/it]\n",
79
+ "Extracting data files: 100%|██████████| 1/1 [00:00<00:00, 988.52it/s]\n",
80
+ "Generating train split: 100%|██████████| 8792/8792 [00:00<00:00, 47323.69 examples/s]\n"
81
+ ]
82
+ }
83
+ ],
84
+ "source": [
85
+ "gsm8k = load_dataset(\"longhoang06/Vi-GSM8K\")[\"train\"]"
86
+ ]
87
+ },
88
+ {
89
+ "cell_type": "code",
90
+ "execution_count": 4,
91
+ "id": "a973c559-1d0f-4978-8648-4049f82c49a7",
92
+ "metadata": {
93
+ "tags": []
94
+ },
95
+ "outputs": [],
96
+ "source": [
97
+ "def add_labels(dataset):\n",
98
+ " answer = dataset[\"answer\"]\n",
99
+ " label = answer.split(\"Đáp án:\")[-1].strip()\n",
100
+ " dataset[\"label\"] = label\n",
101
+ " return dataset\n",
102
+ "\n",
103
+ "gsm8k = gsm8k.map(add_labels)"
104
+ ]
105
+ },
106
+ {
107
+ "cell_type": "code",
108
+ "execution_count": 5,
109
+ "id": "a1f08265-6a52-44b3-a7f0-0900e6f0ccce",
110
+ "metadata": {
111
+ "tags": []
112
+ },
113
+ "outputs": [
114
+ {
115
+ "name": "stdout",
116
+ "output_type": "stream",
117
+ "text": [
118
+ "[2023-11-25 16:32:31,497] [INFO] [real_accelerator.py:158:get_accelerator] Setting ds_accelerator to cuda (auto detect)\n"
119
+ ]
120
+ }
121
+ ],
122
+ "source": [
123
+ "from infer import tokenize_function, data_collator, extract_answer\n",
124
+ "from model.mrc_model import MRCQuestionAnswering\n",
125
+ "from transformers import AutoTokenizer"
126
+ ]
127
+ },
128
+ {
129
+ "cell_type": "code",
130
+ "execution_count": 6,
131
+ "id": "2942f780-4621-4c80-a63d-5ab8b4e886c3",
132
+ "metadata": {
133
+ "tags": []
134
+ },
135
+ "outputs": [],
136
+ "source": [
137
+ "model_checkpoint = \"nguyenvulebinh/vi-mrc-large\"\n",
138
+ "tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)\n",
139
+ "model = MRCQuestionAnswering.from_pretrained(model_checkpoint)"
140
+ ]
141
+ },
142
+ {
143
+ "cell_type": "code",
144
+ "execution_count": 7,
145
+ "id": "22820d8e-33cd-4a8a-9f28-730b1f49ea82",
146
+ "metadata": {
147
+ "scrolled": true,
148
+ "tags": []
149
+ },
150
+ "outputs": [
151
+ {
152
+ "data": {
153
+ "text/plain": [
154
+ "MRCQuestionAnswering(\n",
155
+ " (roberta): RobertaModel(\n",
156
+ " (embeddings): RobertaEmbeddings(\n",
157
+ " (word_embeddings): Embedding(250002, 1024, padding_idx=1)\n",
158
+ " (position_embeddings): Embedding(514, 1024, padding_idx=1)\n",
159
+ " (token_type_embeddings): Embedding(1, 1024)\n",
160
+ " (LayerNorm): LayerNorm((1024,), eps=1e-05, elementwise_affine=True)\n",
161
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
162
+ " )\n",
163
+ " (encoder): RobertaEncoder(\n",
164
+ " (layer): ModuleList(\n",
165
+ " (0-23): 24 x RobertaLayer(\n",
166
+ " (attention): RobertaAttention(\n",
167
+ " (self): RobertaSelfAttention(\n",
168
+ " (query): Linear(in_features=1024, out_features=1024, bias=True)\n",
169
+ " (key): Linear(in_features=1024, out_features=1024, bias=True)\n",
170
+ " (value): Linear(in_features=1024, out_features=1024, bias=True)\n",
171
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
172
+ " )\n",
173
+ " (output): RobertaSelfOutput(\n",
174
+ " (dense): Linear(in_features=1024, out_features=1024, bias=True)\n",
175
+ " (LayerNorm): LayerNorm((1024,), eps=1e-05, elementwise_affine=True)\n",
176
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
177
+ " )\n",
178
+ " )\n",
179
+ " (intermediate): RobertaIntermediate(\n",
180
+ " (dense): Linear(in_features=1024, out_features=4096, bias=True)\n",
181
+ " (intermediate_act_fn): GELUActivation()\n",
182
+ " )\n",
183
+ " (output): RobertaOutput(\n",
184
+ " (dense): Linear(in_features=4096, out_features=1024, bias=True)\n",
185
+ " (LayerNorm): LayerNorm((1024,), eps=1e-05, elementwise_affine=True)\n",
186
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
187
+ " )\n",
188
+ " )\n",
189
+ " )\n",
190
+ " )\n",
191
+ " )\n",
192
+ " (qa_outputs): Linear(in_features=1024, out_features=2, bias=True)\n",
193
+ ")"
194
+ ]
195
+ },
196
+ "execution_count": 7,
197
+ "metadata": {},
198
+ "output_type": "execute_result"
199
+ }
200
+ ],
201
+ "source": [
202
+ "model.to(\"cuda:1\")"
203
+ ]
204
+ },
205
+ {
206
+ "cell_type": "code",
207
+ "execution_count": 8,
208
+ "id": "ea6ffa65-e01f-44f7-840f-702e70110095",
209
+ "metadata": {
210
+ "tags": []
211
+ },
212
+ "outputs": [
213
+ {
214
+ "data": {
215
+ "application/vnd.jupyter.widget-view+json": {
216
+ "model_id": "04a45697b6f348b7a72f54b8b6c2302e",
217
+ "version_major": 2,
218
+ "version_minor": 0
219
+ },
220
+ "text/plain": [
221
+ " 0%| | 0/8792 [00:00<?, ?it/s]"
222
+ ]
223
+ },
224
+ "metadata": {},
225
+ "output_type": "display_data"
226
+ }
227
+ ],
228
+ "source": [
229
+ "ground_truths = []\n",
230
+ "questions = gsm8k[\"question\"]\n",
231
+ "answers = contexts = gsm8k[\"answer\"]\n",
232
+ "labels = gsm8k[\"label\"]\n",
233
+ "\n",
234
+ "progress_bar = tqdm(range(len(questions)))\n",
235
+ "for ques, cont, lab in zip(questions, contexts, labels):\n",
236
+ " cont = cont.split(\"Đáp án:\")[0].strip()\n",
237
+ " QA_input = {\n",
238
+ " \"question\": ques,\n",
239
+ " \"context\": cont,\n",
240
+ " }\n",
241
+ " inputs = [tokenize_function(QA_input, tokenizer)]\n",
242
+ " inputs_ids = data_collator(inputs, tokenizer)\n",
243
+ " inputs_ids = {k:v.to(\"cuda:1\") for k, v in inputs_ids.items()}\n",
244
+ " with torch.no_grad():\n",
245
+ " outputs = model(**inputs_ids)\n",
246
+ " text = extract_answer(inputs, outputs, tokenizer)[0][\"answer\"]\n",
247
+ " normalize_text = re.findall(r'-?\\d+[., ]\\d+|-?\\d+', text)\n",
248
+ " if 0 < len(normalize_text) <=1 and normalize_text[0] == lab:\n",
249
+ " ground_truths.append(text)\n",
250
+ " else:\n",
251
+ " ground_truths.append(lab)\n",
252
+ " \n",
253
+ " progress_bar.update(1)"
254
+ ]
255
+ },
256
+ {
257
+ "cell_type": "code",
258
+ "execution_count": 9,
259
+ "id": "2f2c7ae7-7faf-4f6e-8b87-16b1bb316fc9",
260
+ "metadata": {
261
+ "tags": []
262
+ },
263
+ "outputs": [],
264
+ "source": [
265
+ "# 7 + x = Y năm\n",
266
+ "# m=80\n",
267
+ "# 85 dặm xa hơn\n",
268
+ "# 20 vàng\n",
269
+ "# Tổng cộng là 7 đô la\n",
270
+ "# 28 rùa .\n",
271
+ "check_ground_truths = [\"7 + x = Y năm\", \"m=80\", \"85 dặm xa hơn\", \"20 vàng\", \"Tổng cộng là 7 đô la\", \"28 rùa .\"]\n",
272
+ "for check_gr in check_ground_truths:\n",
273
+ " check_id = ground_truths.index(check_gr)\n",
274
+ " if check_gr == check_ground_truths[0]:\n",
275
+ " ground_truths[check_id] = \"7 tuổi\"\n",
276
+ " elif check_gr == check_ground_truths[1]:\n",
277
+ " ground_truths[check_id] = \"80 viên bi\"\n",
278
+ " elif check_gr == check_ground_truths[2]:\n",
279
+ " ground_truths[check_id] = \"85 dặm\"\n",
280
+ " elif check_gr == check_ground_truths[3]:\n",
281
+ " ground_truths[check_id] = \"20\"\n",
282
+ " elif check_gr == check_ground_truths[4]:\n",
283
+ " ground_truths[check_id] = \"7 đô la\"\n",
284
+ " elif check_gr == check_ground_truths[5]:\n",
285
+ " ground_truths[check_id] = \"28 con rùa\""
286
+ ]
287
+ },
288
+ {
289
+ "cell_type": "code",
290
+ "execution_count": 10,
291
+ "id": "a823330a-4164-4f33-a4af-233288f07463",
292
+ "metadata": {
293
+ "tags": []
294
+ },
295
+ "outputs": [
296
+ {
297
+ "name": "stdout",
298
+ "output_type": "stream",
299
+ "text": [
300
+ "Đầu ngày, Hiệu trưởng Kumar hướng dẫn Harold treo cờ lên cột cờ. Cột cờ dài 60 feet và khi được kéo lên hoàn toàn, lá cờ nằm trên đỉnh cột cờ. Cuối buổi sáng hôm đó, Hiệu phó Zizi đã chỉ thị cho Harold hạ cờ xuống nửa cột. Vì vậy, Harold đã hạ lá cờ xuống giữa cột. Sau đó, Hiệu trưởng Kumar bảo Harold kéo lá cờ lên đỉnh cột một lần nữa và Harold đã làm đúng như vậy. Vào cuối ngày, Hiệu phó Zizi hướng dẫn Harold hạ cờ xuống hoàn toàn, tháo nó ra khỏi cột và cất vào buổi tối. Trong suốt ngày, lá cờ đã di chuyển lên xuống cột bao xa, tính bằng feet?\n",
301
+ " Một nửa quãng đường lên cột cờ là 60/2 = 30 feet. Do đó, Harold đã di chuyển lá cờ 60 lên + 30 xuống + 30 lên + 60 xuống = 180 feet. Đáp án: 180\n",
302
+ "180\n",
303
+ "180\n"
304
+ ]
305
+ }
306
+ ],
307
+ "source": [
308
+ "i = 4670\n",
309
+ "print(questions[i])\n",
310
+ "print(answers[i])\n",
311
+ "print(labels[i])\n",
312
+ "print(ground_truths[i])"
313
+ ]
314
+ },
315
+ {
316
+ "cell_type": "code",
317
+ "execution_count": 11,
318
+ "id": "61e31821-d1cd-4829-8278-ce7a9369055a",
319
+ "metadata": {
320
+ "tags": []
321
+ },
322
+ "outputs": [],
323
+ "source": [
324
+ "def create_multi_choices(ground_truths: list, labels: list) -> list:\n",
325
+ " special_numbers = [\"3,888\", \"9,492\", \"3,472\", \"4,582\", \"1,600\"]\n",
326
+ " choices = []\n",
327
+ " progress_bar = tqdm(range(len(ground_truths)))\n",
328
+ " for i in range(len(ground_truths)):\n",
329
+ " label = re.findall(r'-?\\d+[., ]\\d+|-?\\d+', labels[i])[0]\n",
330
+ " if label not in special_numbers:\n",
331
+ " number = int(label)\n",
332
+ " epsilon = int((number - 1) / 2) + 1\n",
333
+ " if number > 2:\n",
334
+ " lower_bound = max(0, number - epsilon)\n",
335
+ " upper_bound = number + epsilon\n",
336
+ " elif 0 < number <= 2:\n",
337
+ " lower_bound = 1\n",
338
+ " upper_bound = 6\n",
339
+ " else:\n",
340
+ " if epsilon > 0:\n",
341
+ " lower_bound = number - epsilon\n",
342
+ " upper_bound = number + epsilon\n",
343
+ " else:\n",
344
+ " lower_bound = number + epsilon\n",
345
+ " upper_bound = number - epsilon\n",
346
+ "\n",
347
+ " if upper_bound - lower_bound < 3:\n",
348
+ " upper_bound += 3\n",
349
+ "\n",
350
+ " random_numbers = []\n",
351
+ " while 0 <= len(random_numbers) <= 3:\n",
352
+ " random_number = random.randint(lower_bound, upper_bound)\n",
353
+ " if random_number not in random_numbers and random_number != number:\n",
354
+ " random_numbers.append(random_number)\n",
355
+ "\n",
356
+ " ground_truth = ground_truths[i]\n",
357
+ " option_0 = ground_truth.replace(label, str(random_numbers[0]))\n",
358
+ " option_1 = ground_truth.replace(label, str(random_numbers[1]))\n",
359
+ " option_2 = ground_truth.replace(label, str(random_numbers[2]))\n",
360
+ " else:\n",
361
+ " number = float(label.replace(\",\", \".\"))\n",
362
+ " epsilon = round(float((number - 1) / 2), 3)\n",
363
+ " lower_bound = max(0, number - epsilon)\n",
364
+ " upper_bound = number + epsilon\n",
365
+ "\n",
366
+ " random_numbers = []\n",
367
+ " while 0 <= len(random_numbers) <= 3:\n",
368
+ " random_number = round(random.uniform(lower_bound, upper_bound), 3)\n",
369
+ " if random_number not in random_numbers and random_number != number:\n",
370
+ " random_numbers.append(random_number)\n",
371
+ "\n",
372
+ " ground_truth = ground_truths[i]\n",
373
+ " option_0 = ground_truth.replace(label, str(random_numbers[0]).replace(\".\", \",\"))\n",
374
+ " option_1 = ground_truth.replace(label, str(random_numbers[1]).replace(\".\", \",\"))\n",
375
+ " option_2 = ground_truth.replace(label, str(random_numbers[2]).replace(\".\", \",\"))\n",
376
+ "\n",
377
+ " option_list = [ground_truth, option_0, option_1, option_2]\n",
378
+ " random.shuffle(option_list)\n",
379
+ "\n",
380
+ " option_list[0] = \"A. \" + option_list[0]\n",
381
+ " option_list[1] = \"B. \" + option_list[1]\n",
382
+ " option_list[2] = \"C. \" + option_list[2]\n",
383
+ " option_list[3] = \"D. \" + option_list[3]\n",
384
+ " gold_choice = [option for option in option_list if option.split(\". \")[-1] == ground_truth]\n",
385
+ " choices.append((option_list, gold_choice[0]))\n",
386
+ " progress_bar.update(1)\n",
387
+ " \n",
388
+ " return choices"
389
+ ]
390
+ },
391
+ {
392
+ "cell_type": "code",
393
+ "execution_count": 12,
394
+ "id": "08520704-ce76-45d2-bddc-b3dcb0ff6b16",
395
+ "metadata": {
396
+ "tags": []
397
+ },
398
+ "outputs": [
399
+ {
400
+ "data": {
401
+ "application/vnd.jupyter.widget-view+json": {
402
+ "model_id": "01b888c5bca64fcaa9b1ddbd6da88155",
403
+ "version_major": 2,
404
+ "version_minor": 0
405
+ },
406
+ "text/plain": [
407
+ " 0%| | 0/8792 [00:00<?, ?it/s]"
408
+ ]
409
+ },
410
+ "metadata": {},
411
+ "output_type": "display_data"
412
+ }
413
+ ],
414
+ "source": [
415
+ "multi_choices = create_multi_choices(ground_truths, labels)"
416
+ ]
417
+ },
418
+ {
419
+ "cell_type": "code",
420
+ "execution_count": 13,
421
+ "id": "56a00daa-7cf2-43e1-b3e9-96fa5a7b77c8",
422
+ "metadata": {
423
+ "tags": []
424
+ },
425
+ "outputs": [
426
+ {
427
+ "data": {
428
+ "text/plain": [
429
+ "(['A. 71', 'B. 39', 'C. 72', 'D. 50'], 'C. 72')"
430
+ ]
431
+ },
432
+ "execution_count": 13,
433
+ "metadata": {},
434
+ "output_type": "execute_result"
435
+ }
436
+ ],
437
+ "source": [
438
+ "multi_choices[0]"
439
+ ]
440
+ },
441
+ {
442
+ "cell_type": "code",
443
+ "execution_count": 14,
444
+ "id": "0eee0ae7-ecb7-4905-876f-09728c1bdd34",
445
+ "metadata": {
446
+ "tags": []
447
+ },
448
+ "outputs": [],
449
+ "source": [
450
+ "gsm8k_multi_choices = []\n",
451
+ "for i in range(len(ground_truths)):\n",
452
+ " gsm8k_multi_choices.append(\n",
453
+ " {\"question\": questions[i],\n",
454
+ " \"choices\": multi_choices[i][0],\n",
455
+ " \"explanation\": answers[i],\n",
456
+ " \"answer\": multi_choices[i][1],\n",
457
+ " }\n",
458
+ " )"
459
+ ]
460
+ },
461
+ {
462
+ "cell_type": "code",
463
+ "execution_count": 15,
464
+ "id": "eb3bbc6d-e4a6-40a1-96cf-76d41d031eb7",
465
+ "metadata": {
466
+ "tags": []
467
+ },
468
+ "outputs": [
469
+ {
470
+ "name": "stdout",
471
+ "output_type": "stream",
472
+ "text": [
473
+ "/home/jovyan/nas_comm/1_user/[email protected]/research/math/math-mistral\n"
474
+ ]
475
+ },
476
+ {
477
+ "name": "stderr",
478
+ "output_type": "stream",
479
+ "text": [
480
+ "/opt/conda/envs/main/lib/python3.10/site-packages/IPython/core/magics/osm.py:417: UserWarning: using dhist requires you to install the `pickleshare` library.\n",
481
+ " self.shell.db['dhist'] = compress_dhist(dhist)[-100:]\n"
482
+ ]
483
+ }
484
+ ],
485
+ "source": [
486
+ "%cd ..\n",
487
+ "with open(\"math_train.json\", \"r\") as f:\n",
488
+ " zac_dataset = json.load(f)\n",
489
+ "zac_dataset = zac_dataset[\"data\"]"
490
+ ]
491
+ },
492
+ {
493
+ "cell_type": "code",
494
+ "execution_count": 16,
495
+ "id": "a780cd12-6a12-44f0-b795-d69232e93f65",
496
+ "metadata": {
497
+ "tags": []
498
+ },
499
+ "outputs": [],
500
+ "source": [
501
+ "zac_dataset = [d for d in zac_dataset if \"explanation\" in d.keys()]\n",
502
+ "for d in zac_dataset:\n",
503
+ " del d[\"id\"]"
504
+ ]
505
+ },
506
+ {
507
+ "cell_type": "code",
508
+ "execution_count": 18,
509
+ "id": "f44ba9cc-7af8-48e3-bf98-c17b18e2902e",
510
+ "metadata": {
511
+ "tags": []
512
+ },
513
+ "outputs": [],
514
+ "source": [
515
+ "full_dataset = gsm8k_multi_choices + zac_dataset"
516
+ ]
517
+ },
518
+ {
519
+ "cell_type": "code",
520
+ "execution_count": 19,
521
+ "id": "8e0704b5-7379-4c91-91ec-6948bfe39f99",
522
+ "metadata": {
523
+ "tags": []
524
+ },
525
+ "outputs": [
526
+ {
527
+ "data": {
528
+ "text/plain": [
529
+ "9328"
530
+ ]
531
+ },
532
+ "execution_count": 19,
533
+ "metadata": {},
534
+ "output_type": "execute_result"
535
+ }
536
+ ],
537
+ "source": [
538
+ "len(full_dataset)"
539
+ ]
540
+ },
541
+ {
542
+ "cell_type": "code",
543
+ "execution_count": 20,
544
+ "id": "1ecb76e1-65dd-41f4-b70b-bddc938e2e3d",
545
+ "metadata": {
546
+ "tags": []
547
+ },
548
+ "outputs": [],
549
+ "source": [
550
+ "import pandas as pd\n",
551
+ "from datasets import Dataset\n",
552
+ "\n",
553
+ "df = pd.DataFrame(full_dataset)\n",
554
+ "dataset = Dataset.from_pandas(df)"
555
+ ]
556
+ },
557
+ {
558
+ "cell_type": "code",
559
+ "execution_count": 23,
560
+ "id": "ba20bd40-473c-457e-b460-10d1c64cac36",
561
+ "metadata": {
562
+ "tags": []
563
+ },
564
+ "outputs": [
565
+ {
566
+ "data": {
567
+ "application/vnd.jupyter.widget-view+json": {
568
+ "model_id": "2ebec07ba4aa4a2f8fccb10804345982",
569
+ "version_major": 2,
570
+ "version_minor": 0
571
+ },
572
+ "text/plain": [
573
+ "Pushing dataset shards to the dataset hub: 0%| | 0/1 [00:00<?, ?it/s]"
574
+ ]
575
+ },
576
+ "metadata": {},
577
+ "output_type": "display_data"
578
+ },
579
+ {
580
+ "data": {
581
+ "application/vnd.jupyter.widget-view+json": {
582
+ "model_id": "6b9acd7e26564d58b5d38498e30efb29",
583
+ "version_major": 2,
584
+ "version_minor": 0
585
+ },
586
+ "text/plain": [
587
+ "Creating parquet from Arrow format: 0%| | 0/10 [00:00<?, ?ba/s]"
588
+ ]
589
+ },
590
+ "metadata": {},
591
+ "output_type": "display_data"
592
+ }
593
+ ],
594
+ "source": [
595
+ "dataset.push_to_hub(\"MC-ViMath\", private = True)"
596
+ ]
597
+ }
598
+ ],
599
+ "metadata": {
600
+ "kernelspec": {
601
+ "display_name": "Python (mistral)",
602
+ "language": "python",
603
+ "name": "mistral"
604
+ },
605
+ "language_info": {
606
+ "codemirror_mode": {
607
+ "name": "ipython",
608
+ "version": 3
609
+ },
610
+ "file_extension": ".py",
611
+ "mimetype": "text/x-python",
612
+ "name": "python",
613
+ "nbconvert_exporter": "python",
614
+ "pygments_lexer": "ipython3",
615
+ "version": "3.10.13"
616
+ }
617
+ },
618
+ "nbformat": 4,
619
+ "nbformat_minor": 5
620
+ }
OVM/.ipynb_checkpoints/predict_notebook-checkpoint.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
OVM/Dockerfile.ClickAI ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM nvcr.io/nvidia/pytorch:22.12-py3
2
+
3
+ LABEL maintainer="ClickAI"
4
+
5
+ ARG DEBIAN_FRONTEND=noninteractive
6
+
7
+ RUN apt update
8
+ RUN apt install -y git libsndfile1-dev tesseract-ocr espeak-ng python3 python3-pip ffmpeg
9
+ RUN python3 -m pip install --no-cache-dir --upgrade pip
10
+
11
+ ARG REF=main
12
+ RUN git clone https://github.com/huggingface/transformers && cd transformers && git checkout $REF
13
+
14
+ ARG PYTORCH='2.1.0'
15
+ ARG TORCH_VISION=''
16
+ ARG TORCH_AUDIO=''
17
+ ARG CUDA='cu118'
18
+
19
+ RUN [ ${#PYTORCH} -gt 0 ] && VERSION='torch=='$PYTORCH'.*' || VERSION='torch'; python3 -m pip install --no-cache-dir -U $VERSION --extra-index-url https://download.pytorch.org/whl/$CUDA
20
+ RUN [ ${#TORCH_VISION} -gt 0 ] && VERSION='torchvision=='TORCH_VISION'.*' || VERSION='torchvision'; python3 -m pip install --no-cache-dir -U $VERSION --extra-index-url https://download.pytorch.org/whl/$CUDA
21
+ RUN [ ${#TORCH_AUDIO} -gt 0 ] && VERSION='torchaudio=='TORCH_AUDIO'.*' || VERSION='torchaudio'; python3 -m pip install --no-cache-dir -U $VERSION --extra-index-url https://download.pytorch.org/whl/$CUDA
22
+
23
+ RUN python3 -m pip install --no-cache-dir -e ./transformers[dev-torch,testing,video]
24
+ RUN python3 -m pip uninstall -y tensorflow flax
25
+ RUN python3 -m pip install --no-cache-dir git+https://github.com/facebookresearch/detectron2.git pytesseract
26
+ RUN python3 -m pip install -U "itsdangerous<2.1.0"
27
+ RUN cd transformers && python3 setup.py develop
28
+
29
+ ADD requirements.txt /requirements.txt
30
+ RUN cd / && pip install -r requirements.txt
31
+
32
+ COPY extractive-qa-mrc /extractive-qa-mrc
33
+ COPY checkpoint /checkpoint
34
+ COPY data /data
35
+
36
+
37
+ COPY create_mc_dataset.ipynb /create_mc_dataset.ipynb
38
+ COPY math-mistral.py /math-mistral.py
39
+ COPY math-mistral.sh /math-mistral.sh
40
+ COPY mc-mistral.py /mc-mistral.py
41
+ COPY mc-mistral.sh /mc-mistral.sh
42
+ COPY predict_notebook.ipynb /predict_notebook.ipynb
43
+ COPY predict.py /predict.py
44
+ COPY predict.sh /predict.sh
45
+
46
+
47
+ RUN python3 -m pip uninstall transformer-engine -y
48
+
49
+
50
+ WORKDIR /
51
+ ENTRYPOINT ["/bin/bash", "/predict.sh"]
OVM/OVM/.ipynb_checkpoints/Untitled-checkpoint.ipynb ADDED
@@ -0,0 +1,1309 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "a207d6b3-e0ac-4a02-8125-087244598d72",
7
+ "metadata": {
8
+ "tags": []
9
+ },
10
+ "outputs": [],
11
+ "source": [
12
+ "import os\n",
13
+ "os.environ[\"CUDA_DEVICE_ORDER\"]=\"PCI_BUS_ID\" \n",
14
+ "os.environ[\"CUDA_VISIBLE_DEVICES\"]=\"4,5\""
15
+ ]
16
+ },
17
+ {
18
+ "cell_type": "code",
19
+ "execution_count": 2,
20
+ "id": "4ab7f1af-89e5-4d04-ab32-761292e0fc40",
21
+ "metadata": {
22
+ "tags": []
23
+ },
24
+ "outputs": [
25
+ {
26
+ "name": "stderr",
27
+ "output_type": "stream",
28
+ "text": [
29
+ "/opt/conda/envs/mistral/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
30
+ " from .autonotebook import tqdm as notebook_tqdm\n"
31
+ ]
32
+ }
33
+ ],
34
+ "source": [
35
+ "from transformers import AutoTokenizer, AutoModelForCausalLM\n",
36
+ "import torch\n",
37
+ "import torch.nn as nn \n",
38
+ "from build_verifier import VerifierModel\n",
39
+ "from v_dataset import VerifierDataset\n",
40
+ "from torch.utils.data import DataLoader"
41
+ ]
42
+ },
43
+ {
44
+ "cell_type": "code",
45
+ "execution_count": 3,
46
+ "id": "4adc4982-6ab2-414f-a19e-b796f306ceb1",
47
+ "metadata": {
48
+ "tags": []
49
+ },
50
+ "outputs": [
51
+ {
52
+ "name": "stderr",
53
+ "output_type": "stream",
54
+ "text": [
55
+ "Loading checkpoint shards: 100%|██████████| 2/2 [00:24<00:00, 12.19s/it]\n"
56
+ ]
57
+ }
58
+ ],
59
+ "source": [
60
+ "model = AutoModelForCausalLM.from_pretrained(\"mistralai/Mistral-7B-v0.1\", device_map = \"auto\", torch_dtype = torch.bfloat16)"
61
+ ]
62
+ },
63
+ {
64
+ "cell_type": "code",
65
+ "execution_count": 4,
66
+ "id": "e0f599b5-d3ca-4ebb-b6ba-a5a77936e1a1",
67
+ "metadata": {
68
+ "tags": []
69
+ },
70
+ "outputs": [],
71
+ "source": [
72
+ "tokenizer = AutoTokenizer.from_pretrained(\"mistralai/Mistral-7B-v0.1\")\n",
73
+ "tokenizer.pad_token = tokenizer.eos_token"
74
+ ]
75
+ },
76
+ {
77
+ "cell_type": "code",
78
+ "execution_count": 6,
79
+ "id": "d498bc4d-366f-4a7c-a189-9afe6a2d4889",
80
+ "metadata": {
81
+ "tags": []
82
+ },
83
+ "outputs": [],
84
+ "source": [
85
+ "VERIFIER = VerifierModel(backbone = model, checkpoint_dir = \"/home/jovyan/workspace/1_user/[email protected]/temp/check/model.pt\")"
86
+ ]
87
+ },
88
+ {
89
+ "cell_type": "code",
90
+ "execution_count": 7,
91
+ "id": "978eebc9-2cbc-42cf-8ca7-d582e0a83866",
92
+ "metadata": {
93
+ "tags": []
94
+ },
95
+ "outputs": [],
96
+ "source": [
97
+ "torch.cuda.empty_cache()"
98
+ ]
99
+ },
100
+ {
101
+ "cell_type": "code",
102
+ "execution_count": 8,
103
+ "id": "c5bf1882-8e07-4fb3-a687-7726374bb0d7",
104
+ "metadata": {
105
+ "tags": []
106
+ },
107
+ "outputs": [],
108
+ "source": [
109
+ "torch.save(VERIFIER.state_dict(), \"/home/jovyan/workspace/1_user/[email protected]/temp/check/model.pt\")"
110
+ ]
111
+ },
112
+ {
113
+ "cell_type": "code",
114
+ "execution_count": 6,
115
+ "id": "5f8e83bf-f0f2-4fb1-adec-7935e7ab0987",
116
+ "metadata": {
117
+ "tags": []
118
+ },
119
+ "outputs": [],
120
+ "source": [
121
+ "device = model.device"
122
+ ]
123
+ },
124
+ {
125
+ "cell_type": "code",
126
+ "execution_count": 7,
127
+ "id": "c1a605f8-30d7-4bde-aa48-463754b4f309",
128
+ "metadata": {
129
+ "tags": []
130
+ },
131
+ "outputs": [],
132
+ "source": [
133
+ "verifier_cls = VerifierDataset(\n",
134
+ " tokenizer = tokenizer,\n",
135
+ " data_path = \"longhoang06/vi-ovm-dataset\",\n",
136
+ " max_length = 512,\n",
137
+ " load_data_method = \"hf_hub\",\n",
138
+ " mapping = True,\n",
139
+ ")"
140
+ ]
141
+ },
142
+ {
143
+ "cell_type": "code",
144
+ "execution_count": 8,
145
+ "id": "3ab84d61-9e9a-42f5-9183-7e9267ccb6a6",
146
+ "metadata": {
147
+ "tags": []
148
+ },
149
+ "outputs": [],
150
+ "source": [
151
+ "dataset = verifier_cls.dataset[\"train\"]"
152
+ ]
153
+ },
154
+ {
155
+ "cell_type": "code",
156
+ "execution_count": 9,
157
+ "id": "57b216f4-47b1-42ab-b451-ff01add4fc7c",
158
+ "metadata": {
159
+ "tags": []
160
+ },
161
+ "outputs": [],
162
+ "source": [
163
+ "dataset.set_format(\"torch\")"
164
+ ]
165
+ },
166
+ {
167
+ "cell_type": "code",
168
+ "execution_count": 10,
169
+ "id": "f8c90769-96a6-4770-901a-70ca6de1edb1",
170
+ "metadata": {
171
+ "tags": []
172
+ },
173
+ "outputs": [],
174
+ "source": [
175
+ "train_dataloader = DataLoader(dataset, batch_size = 2, pin_memory = True)"
176
+ ]
177
+ },
178
+ {
179
+ "cell_type": "code",
180
+ "execution_count": 11,
181
+ "id": "07185511-7530-4786-826d-95358d5800e5",
182
+ "metadata": {
183
+ "tags": []
184
+ },
185
+ "outputs": [
186
+ {
187
+ "name": "stdout",
188
+ "output_type": "stream",
189
+ "text": [
190
+ "VerifierModelOutput(loss=tensor(1.3851e+11, device='cuda:0', grad_fn=<AddBackward0>), v_scores=tensor([[[ 0.0583],\n",
191
+ " [-1.9375],\n",
192
+ " [-1.9609],\n",
193
+ " ...,\n",
194
+ " [-2.1094],\n",
195
+ " [-0.7891],\n",
196
+ " [-0.9062]],\n",
197
+ "\n",
198
+ " [[ 0.1157],\n",
199
+ " [-0.5586],\n",
200
+ " [-0.6953],\n",
201
+ " ...,\n",
202
+ " [-2.4688],\n",
203
+ " [-2.3594],\n",
204
+ " [-0.6641]]], device='cuda:0', dtype=torch.bfloat16,\n",
205
+ " grad_fn=<UnsafeViewBackward0>), all_losses={'llm_loss': tensor(1.0603, device='cuda:0', grad_fn=<NllLossBackward0>), 'v_loss': tensor(1.3851e+11, device='cuda:0', dtype=torch.bfloat16,\n",
206
+ " grad_fn=<DivBackward0>)})\n"
207
+ ]
208
+ }
209
+ ],
210
+ "source": [
211
+ "for batch in train_dataloader:\n",
212
+ " batch = {k:v.to(\"cuda\") for k, v in batch.items()}\n",
213
+ " outputs = VERIFIER(\n",
214
+ " input_ids = batch[\"input_ids\"],\n",
215
+ " attention_mask = batch[\"attention_mask\"],\n",
216
+ " labels = batch[\"labels\"],\n",
217
+ " v_labels = batch[\"v_labels\"],\n",
218
+ " output_all_losses = True,\n",
219
+ " )\n",
220
+ " print(outputs)\n",
221
+ " break"
222
+ ]
223
+ },
224
+ {
225
+ "cell_type": "code",
226
+ "execution_count": 16,
227
+ "id": "28052242-89c8-44dc-867f-8d7c6bc4b080",
228
+ "metadata": {
229
+ "tags": []
230
+ },
231
+ "outputs": [
232
+ {
233
+ "data": {
234
+ "text/plain": [
235
+ "torch.bfloat16"
236
+ ]
237
+ },
238
+ "execution_count": 16,
239
+ "metadata": {},
240
+ "output_type": "execute_result"
241
+ }
242
+ ],
243
+ "source": [
244
+ "model.dtype"
245
+ ]
246
+ },
247
+ {
248
+ "cell_type": "code",
249
+ "execution_count": 19,
250
+ "id": "6380f930-f00e-41d8-926c-57c76c9af69d",
251
+ "metadata": {
252
+ "tags": []
253
+ },
254
+ "outputs": [
255
+ {
256
+ "name": "stdout",
257
+ "output_type": "stream",
258
+ "text": [
259
+ "Help on class Linear in module torch.nn.modules.linear:\n",
260
+ "\n",
261
+ "class Linear(torch.nn.modules.module.Module)\n",
262
+ " | Linear(in_features: int, out_features: int, bias: bool = True, device=None, dtype=None) -> None\n",
263
+ " | \n",
264
+ " | Applies a linear transformation to the incoming data: :math:`y = xA^T + b`\n",
265
+ " | \n",
266
+ " | This module supports :ref:`TensorFloat32<tf32_on_ampere>`.\n",
267
+ " | \n",
268
+ " | On certain ROCm devices, when using float16 inputs this module will use :ref:`different precision<fp16_on_mi200>` for backward.\n",
269
+ " | \n",
270
+ " | Args:\n",
271
+ " | in_features: size of each input sample\n",
272
+ " | out_features: size of each output sample\n",
273
+ " | bias: If set to ``False``, the layer will not learn an additive bias.\n",
274
+ " | Default: ``True``\n",
275
+ " | \n",
276
+ " | Shape:\n",
277
+ " | - Input: :math:`(*, H_{in})` where :math:`*` means any number of\n",
278
+ " | dimensions including none and :math:`H_{in} = \\text{in\\_features}`.\n",
279
+ " | - Output: :math:`(*, H_{out})` where all but the last dimension\n",
280
+ " | are the same shape as the input and :math:`H_{out} = \\text{out\\_features}`.\n",
281
+ " | \n",
282
+ " | Attributes:\n",
283
+ " | weight: the learnable weights of the module of shape\n",
284
+ " | :math:`(\\text{out\\_features}, \\text{in\\_features})`. The values are\n",
285
+ " | initialized from :math:`\\mathcal{U}(-\\sqrt{k}, \\sqrt{k})`, where\n",
286
+ " | :math:`k = \\frac{1}{\\text{in\\_features}}`\n",
287
+ " | bias: the learnable bias of the module of shape :math:`(\\text{out\\_features})`.\n",
288
+ " | If :attr:`bias` is ``True``, the values are initialized from\n",
289
+ " | :math:`\\mathcal{U}(-\\sqrt{k}, \\sqrt{k})` where\n",
290
+ " | :math:`k = \\frac{1}{\\text{in\\_features}}`\n",
291
+ " | \n",
292
+ " | Examples::\n",
293
+ " | \n",
294
+ " | >>> m = nn.Linear(20, 30)\n",
295
+ " | >>> input = torch.randn(128, 20)\n",
296
+ " | >>> output = m(input)\n",
297
+ " | >>> print(output.size())\n",
298
+ " | torch.Size([128, 30])\n",
299
+ " | \n",
300
+ " | Method resolution order:\n",
301
+ " | Linear\n",
302
+ " | torch.nn.modules.module.Module\n",
303
+ " | builtins.object\n",
304
+ " | \n",
305
+ " | Methods defined here:\n",
306
+ " | \n",
307
+ " | __init__(self, in_features: int, out_features: int, bias: bool = True, device=None, dtype=None) -> None\n",
308
+ " | Initializes internal Module state, shared by both nn.Module and ScriptModule.\n",
309
+ " | \n",
310
+ " | extra_repr(self) -> str\n",
311
+ " | Set the extra representation of the module\n",
312
+ " | \n",
313
+ " | To print customized extra information, you should re-implement\n",
314
+ " | this method in your own modules. Both single-line and multi-line\n",
315
+ " | strings are acceptable.\n",
316
+ " | \n",
317
+ " | forward(self, input: torch.Tensor) -> torch.Tensor\n",
318
+ " | Defines the computation performed at every call.\n",
319
+ " | \n",
320
+ " | Should be overridden by all subclasses.\n",
321
+ " | \n",
322
+ " | .. note::\n",
323
+ " | Although the recipe for forward pass needs to be defined within\n",
324
+ " | this function, one should call the :class:`Module` instance afterwards\n",
325
+ " | instead of this since the former takes care of running the\n",
326
+ " | registered hooks while the latter silently ignores them.\n",
327
+ " | \n",
328
+ " | reset_parameters(self) -> None\n",
329
+ " | \n",
330
+ " | ----------------------------------------------------------------------\n",
331
+ " | Data and other attributes defined here:\n",
332
+ " | \n",
333
+ " | __annotations__ = {'in_features': <class 'int'>, 'out_features': <clas...\n",
334
+ " | \n",
335
+ " | __constants__ = ['in_features', 'out_features']\n",
336
+ " | \n",
337
+ " | __slotnames__ = []\n",
338
+ " | \n",
339
+ " | ----------------------------------------------------------------------\n",
340
+ " | Methods inherited from torch.nn.modules.module.Module:\n",
341
+ " | \n",
342
+ " | __call__ = _call_impl(self, *args, **kwargs)\n",
343
+ " | \n",
344
+ " | __delattr__(self, name)\n",
345
+ " | Implement delattr(self, name).\n",
346
+ " | \n",
347
+ " | __dir__(self)\n",
348
+ " | Default dir() implementation.\n",
349
+ " | \n",
350
+ " | __getattr__(self, name: str) -> Union[torch.Tensor, ForwardRef('Module')]\n",
351
+ " | \n",
352
+ " | __repr__(self)\n",
353
+ " | Return repr(self).\n",
354
+ " | \n",
355
+ " | __setattr__(self, name: str, value: Union[torch.Tensor, ForwardRef('Module')]) -> None\n",
356
+ " | Implement setattr(self, name, value).\n",
357
+ " | \n",
358
+ " | __setstate__(self, state)\n",
359
+ " | \n",
360
+ " | add_module(self, name: str, module: Optional[ForwardRef('Module')]) -> None\n",
361
+ " | Adds a child module to the current module.\n",
362
+ " | \n",
363
+ " | The module can be accessed as an attribute using the given name.\n",
364
+ " | \n",
365
+ " | Args:\n",
366
+ " | name (str): name of the child module. The child module can be\n",
367
+ " | accessed from this module using the given name\n",
368
+ " | module (Module): child module to be added to the module.\n",
369
+ " | \n",
370
+ " | apply(self: ~T, fn: Callable[[ForwardRef('Module')], NoneType]) -> ~T\n",
371
+ " | Applies ``fn`` recursively to every submodule (as returned by ``.children()``)\n",
372
+ " | as well as self. Typical use includes initializing the parameters of a model\n",
373
+ " | (see also :ref:`nn-init-doc`).\n",
374
+ " | \n",
375
+ " | Args:\n",
376
+ " | fn (:class:`Module` -> None): function to be applied to each submodule\n",
377
+ " | \n",
378
+ " | Returns:\n",
379
+ " | Module: self\n",
380
+ " | \n",
381
+ " | Example::\n",
382
+ " | \n",
383
+ " | >>> @torch.no_grad()\n",
384
+ " | >>> def init_weights(m):\n",
385
+ " | >>> print(m)\n",
386
+ " | >>> if type(m) == nn.Linear:\n",
387
+ " | >>> m.weight.fill_(1.0)\n",
388
+ " | >>> print(m.weight)\n",
389
+ " | >>> net = nn.Sequential(nn.Linear(2, 2), nn.Linear(2, 2))\n",
390
+ " | >>> net.apply(init_weights)\n",
391
+ " | Linear(in_features=2, out_features=2, bias=True)\n",
392
+ " | Parameter containing:\n",
393
+ " | tensor([[1., 1.],\n",
394
+ " | [1., 1.]], requires_grad=True)\n",
395
+ " | Linear(in_features=2, out_features=2, bias=True)\n",
396
+ " | Parameter containing:\n",
397
+ " | tensor([[1., 1.],\n",
398
+ " | [1., 1.]], requires_grad=True)\n",
399
+ " | Sequential(\n",
400
+ " | (0): Linear(in_features=2, out_features=2, bias=True)\n",
401
+ " | (1): Linear(in_features=2, out_features=2, bias=True)\n",
402
+ " | )\n",
403
+ " | \n",
404
+ " | bfloat16(self: ~T) -> ~T\n",
405
+ " | Casts all floating point parameters and buffers to ``bfloat16`` datatype.\n",
406
+ " | \n",
407
+ " | .. note::\n",
408
+ " | This method modifies the module in-place.\n",
409
+ " | \n",
410
+ " | Returns:\n",
411
+ " | Module: self\n",
412
+ " | \n",
413
+ " | buffers(self, recurse: bool = True) -> Iterator[torch.Tensor]\n",
414
+ " | Returns an iterator over module buffers.\n",
415
+ " | \n",
416
+ " | Args:\n",
417
+ " | recurse (bool): if True, then yields buffers of this module\n",
418
+ " | and all submodules. Otherwise, yields only buffers that\n",
419
+ " | are direct members of this module.\n",
420
+ " | \n",
421
+ " | Yields:\n",
422
+ " | torch.Tensor: module buffer\n",
423
+ " | \n",
424
+ " | Example::\n",
425
+ " | \n",
426
+ " | >>> # xdoctest: +SKIP(\"undefined vars\")\n",
427
+ " | >>> for buf in model.buffers():\n",
428
+ " | >>> print(type(buf), buf.size())\n",
429
+ " | <class 'torch.Tensor'> (20L,)\n",
430
+ " | <class 'torch.Tensor'> (20L, 1L, 5L, 5L)\n",
431
+ " | \n",
432
+ " | children(self) -> Iterator[ForwardRef('Module')]\n",
433
+ " | Returns an iterator over immediate children modules.\n",
434
+ " | \n",
435
+ " | Yields:\n",
436
+ " | Module: a child module\n",
437
+ " | \n",
438
+ " | cpu(self: ~T) -> ~T\n",
439
+ " | Moves all model parameters and buffers to the CPU.\n",
440
+ " | \n",
441
+ " | .. note::\n",
442
+ " | This method modifies the module in-place.\n",
443
+ " | \n",
444
+ " | Returns:\n",
445
+ " | Module: self\n",
446
+ " | \n",
447
+ " | cuda(self: ~T, device: Union[int, torch.device, NoneType] = None) -> ~T\n",
448
+ " | Moves all model parameters and buffers to the GPU.\n",
449
+ " | \n",
450
+ " | This also makes associated parameters and buffers different objects. So\n",
451
+ " | it should be called before constructing optimizer if the module will\n",
452
+ " | live on GPU while being optimized.\n",
453
+ " | \n",
454
+ " | .. note::\n",
455
+ " | This method modifies the module in-place.\n",
456
+ " | \n",
457
+ " | Args:\n",
458
+ " | device (int, optional): if specified, all parameters will be\n",
459
+ " | copied to that device\n",
460
+ " | \n",
461
+ " | Returns:\n",
462
+ " | Module: self\n",
463
+ " | \n",
464
+ " | double(self: ~T) -> ~T\n",
465
+ " | Casts all floating point parameters and buffers to ``double`` datatype.\n",
466
+ " | \n",
467
+ " | .. note::\n",
468
+ " | This method modifies the module in-place.\n",
469
+ " | \n",
470
+ " | Returns:\n",
471
+ " | Module: self\n",
472
+ " | \n",
473
+ " | eval(self: ~T) -> ~T\n",
474
+ " | Sets the module in evaluation mode.\n",
475
+ " | \n",
476
+ " | This has any effect only on certain modules. See documentations of\n",
477
+ " | particular modules for details of their behaviors in training/evaluation\n",
478
+ " | mode, if they are affected, e.g. :class:`Dropout`, :class:`BatchNorm`,\n",
479
+ " | etc.\n",
480
+ " | \n",
481
+ " | This is equivalent with :meth:`self.train(False) <torch.nn.Module.train>`.\n",
482
+ " | \n",
483
+ " | See :ref:`locally-disable-grad-doc` for a comparison between\n",
484
+ " | `.eval()` and several similar mechanisms that may be confused with it.\n",
485
+ " | \n",
486
+ " | Returns:\n",
487
+ " | Module: self\n",
488
+ " | \n",
489
+ " | float(self: ~T) -> ~T\n",
490
+ " | Casts all floating point parameters and buffers to ``float`` datatype.\n",
491
+ " | \n",
492
+ " | .. note::\n",
493
+ " | This method modifies the module in-place.\n",
494
+ " | \n",
495
+ " | Returns:\n",
496
+ " | Module: self\n",
497
+ " | \n",
498
+ " | get_buffer(self, target: str) -> 'Tensor'\n",
499
+ " | Returns the buffer given by ``target`` if it exists,\n",
500
+ " | otherwise throws an error.\n",
501
+ " | \n",
502
+ " | See the docstring for ``get_submodule`` for a more detailed\n",
503
+ " | explanation of this method's functionality as well as how to\n",
504
+ " | correctly specify ``target``.\n",
505
+ " | \n",
506
+ " | Args:\n",
507
+ " | target: The fully-qualified string name of the buffer\n",
508
+ " | to look for. (See ``get_submodule`` for how to specify a\n",
509
+ " | fully-qualified string.)\n",
510
+ " | \n",
511
+ " | Returns:\n",
512
+ " | torch.Tensor: The buffer referenced by ``target``\n",
513
+ " | \n",
514
+ " | Raises:\n",
515
+ " | AttributeError: If the target string references an invalid\n",
516
+ " | path or resolves to something that is not a\n",
517
+ " | buffer\n",
518
+ " | \n",
519
+ " | get_extra_state(self) -> Any\n",
520
+ " | Returns any extra state to include in the module's state_dict.\n",
521
+ " | Implement this and a corresponding :func:`set_extra_state` for your module\n",
522
+ " | if you need to store extra state. This function is called when building the\n",
523
+ " | module's `state_dict()`.\n",
524
+ " | \n",
525
+ " | Note that extra state should be picklable to ensure working serialization\n",
526
+ " | of the state_dict. We only provide provide backwards compatibility guarantees\n",
527
+ " | for serializing Tensors; other objects may break backwards compatibility if\n",
528
+ " | their serialized pickled form changes.\n",
529
+ " | \n",
530
+ " | Returns:\n",
531
+ " | object: Any extra state to store in the module's state_dict\n",
532
+ " | \n",
533
+ " | get_parameter(self, target: str) -> 'Parameter'\n",
534
+ " | Returns the parameter given by ``target`` if it exists,\n",
535
+ " | otherwise throws an error.\n",
536
+ " | \n",
537
+ " | See the docstring for ``get_submodule`` for a more detailed\n",
538
+ " | explanation of this method's functionality as well as how to\n",
539
+ " | correctly specify ``target``.\n",
540
+ " | \n",
541
+ " | Args:\n",
542
+ " | target: The fully-qualified string name of the Parameter\n",
543
+ " | to look for. (See ``get_submodule`` for how to specify a\n",
544
+ " | fully-qualified string.)\n",
545
+ " | \n",
546
+ " | Returns:\n",
547
+ " | torch.nn.Parameter: The Parameter referenced by ``target``\n",
548
+ " | \n",
549
+ " | Raises:\n",
550
+ " | AttributeError: If the target string references an invalid\n",
551
+ " | path or resolves to something that is not an\n",
552
+ " | ``nn.Parameter``\n",
553
+ " | \n",
554
+ " | get_submodule(self, target: str) -> 'Module'\n",
555
+ " | Returns the submodule given by ``target`` if it exists,\n",
556
+ " | otherwise throws an error.\n",
557
+ " | \n",
558
+ " | For example, let's say you have an ``nn.Module`` ``A`` that\n",
559
+ " | looks like this:\n",
560
+ " | \n",
561
+ " | .. code-block:: text\n",
562
+ " | \n",
563
+ " | A(\n",
564
+ " | (net_b): Module(\n",
565
+ " | (net_c): Module(\n",
566
+ " | (conv): Conv2d(16, 33, kernel_size=(3, 3), stride=(2, 2))\n",
567
+ " | )\n",
568
+ " | (linear): Linear(in_features=100, out_features=200, bias=True)\n",
569
+ " | )\n",
570
+ " | )\n",
571
+ " | \n",
572
+ " | (The diagram shows an ``nn.Module`` ``A``. ``A`` has a nested\n",
573
+ " | submodule ``net_b``, which itself has two submodules ``net_c``\n",
574
+ " | and ``linear``. ``net_c`` then has a submodule ``conv``.)\n",
575
+ " | \n",
576
+ " | To check whether or not we have the ``linear`` submodule, we\n",
577
+ " | would call ``get_submodule(\"net_b.linear\")``. To check whether\n",
578
+ " | we have the ``conv`` submodule, we would call\n",
579
+ " | ``get_submodule(\"net_b.net_c.conv\")``.\n",
580
+ " | \n",
581
+ " | The runtime of ``get_submodule`` is bounded by the degree\n",
582
+ " | of module nesting in ``target``. A query against\n",
583
+ " | ``named_modules`` achieves the same result, but it is O(N) in\n",
584
+ " | the number of transitive modules. So, for a simple check to see\n",
585
+ " | if some submodule exists, ``get_submodule`` should always be\n",
586
+ " | used.\n",
587
+ " | \n",
588
+ " | Args:\n",
589
+ " | target: The fully-qualified string name of the submodule\n",
590
+ " | to look for. (See above example for how to specify a\n",
591
+ " | fully-qualified string.)\n",
592
+ " | \n",
593
+ " | Returns:\n",
594
+ " | torch.nn.Module: The submodule referenced by ``target``\n",
595
+ " | \n",
596
+ " | Raises:\n",
597
+ " | AttributeError: If the target string references an invalid\n",
598
+ " | path or resolves to something that is not an\n",
599
+ " | ``nn.Module``\n",
600
+ " | \n",
601
+ " | half(self: ~T) -> ~T\n",
602
+ " | Casts all floating point parameters and buffers to ``half`` datatype.\n",
603
+ " | \n",
604
+ " | .. note::\n",
605
+ " | This method modifies the module in-place.\n",
606
+ " | \n",
607
+ " | Returns:\n",
608
+ " | Module: self\n",
609
+ " | \n",
610
+ " | ipu(self: ~T, device: Union[int, torch.device, NoneType] = None) -> ~T\n",
611
+ " | Moves all model parameters and buffers to the IPU.\n",
612
+ " | \n",
613
+ " | This also makes associated parameters and buffers different objects. So\n",
614
+ " | it should be called before constructing optimizer if the module will\n",
615
+ " | live on IPU while being optimized.\n",
616
+ " | \n",
617
+ " | .. note::\n",
618
+ " | This method modifies the module in-place.\n",
619
+ " | \n",
620
+ " | Arguments:\n",
621
+ " | device (int, optional): if specified, all parameters will be\n",
622
+ " | copied to that device\n",
623
+ " | \n",
624
+ " | Returns:\n",
625
+ " | Module: self\n",
626
+ " | \n",
627
+ " | load_state_dict(self, state_dict: Mapping[str, Any], strict: bool = True)\n",
628
+ " | Copies parameters and buffers from :attr:`state_dict` into\n",
629
+ " | this module and its descendants. If :attr:`strict` is ``True``, then\n",
630
+ " | the keys of :attr:`state_dict` must exactly match the keys returned\n",
631
+ " | by this module's :meth:`~torch.nn.Module.state_dict` function.\n",
632
+ " | \n",
633
+ " | Args:\n",
634
+ " | state_dict (dict): a dict containing parameters and\n",
635
+ " | persistent buffers.\n",
636
+ " | strict (bool, optional): whether to strictly enforce that the keys\n",
637
+ " | in :attr:`state_dict` match the keys returned by this module's\n",
638
+ " | :meth:`~torch.nn.Module.state_dict` function. Default: ``True``\n",
639
+ " | \n",
640
+ " | Returns:\n",
641
+ " | ``NamedTuple`` with ``missing_keys`` and ``unexpected_keys`` fields:\n",
642
+ " | * **missing_keys** is a list of str containing the missing keys\n",
643
+ " | * **unexpected_keys** is a list of str containing the unexpected keys\n",
644
+ " | \n",
645
+ " | Note:\n",
646
+ " | If a parameter or buffer is registered as ``None`` and its corresponding key\n",
647
+ " | exists in :attr:`state_dict`, :meth:`load_state_dict` will raise a\n",
648
+ " | ``RuntimeError``.\n",
649
+ " | \n",
650
+ " | modules(self) -> Iterator[ForwardRef('Module')]\n",
651
+ " | Returns an iterator over all modules in the network.\n",
652
+ " | \n",
653
+ " | Yields:\n",
654
+ " | Module: a module in the network\n",
655
+ " | \n",
656
+ " | Note:\n",
657
+ " | Duplicate modules are returned only once. In the following\n",
658
+ " | example, ``l`` will be returned only once.\n",
659
+ " | \n",
660
+ " | Example::\n",
661
+ " | \n",
662
+ " | >>> l = nn.Linear(2, 2)\n",
663
+ " | >>> net = nn.Sequential(l, l)\n",
664
+ " | >>> for idx, m in enumerate(net.modules()):\n",
665
+ " | ... print(idx, '->', m)\n",
666
+ " | \n",
667
+ " | 0 -> Sequential(\n",
668
+ " | (0): Linear(in_features=2, out_features=2, bias=True)\n",
669
+ " | (1): Linear(in_features=2, out_features=2, bias=True)\n",
670
+ " | )\n",
671
+ " | 1 -> Linear(in_features=2, out_features=2, bias=True)\n",
672
+ " | \n",
673
+ " | named_buffers(self, prefix: str = '', recurse: bool = True, remove_duplicate: bool = True) -> Iterator[Tuple[str, torch.Tensor]]\n",
674
+ " | Returns an iterator over module buffers, yielding both the\n",
675
+ " | name of the buffer as well as the buffer itself.\n",
676
+ " | \n",
677
+ " | Args:\n",
678
+ " | prefix (str): prefix to prepend to all buffer names.\n",
679
+ " | recurse (bool, optional): if True, then yields buffers of this module\n",
680
+ " | and all submodules. Otherwise, yields only buffers that\n",
681
+ " | are direct members of this module. Defaults to True.\n",
682
+ " | remove_duplicate (bool, optional): whether to remove the duplicated buffers in the result. Defaults to True.\n",
683
+ " | \n",
684
+ " | Yields:\n",
685
+ " | (str, torch.Tensor): Tuple containing the name and buffer\n",
686
+ " | \n",
687
+ " | Example::\n",
688
+ " | \n",
689
+ " | >>> # xdoctest: +SKIP(\"undefined vars\")\n",
690
+ " | >>> for name, buf in self.named_buffers():\n",
691
+ " | >>> if name in ['running_var']:\n",
692
+ " | >>> print(buf.size())\n",
693
+ " | \n",
694
+ " | named_children(self) -> Iterator[Tuple[str, ForwardRef('Module')]]\n",
695
+ " | Returns an iterator over immediate children modules, yielding both\n",
696
+ " | the name of the module as well as the module itself.\n",
697
+ " | \n",
698
+ " | Yields:\n",
699
+ " | (str, Module): Tuple containing a name and child module\n",
700
+ " | \n",
701
+ " | Example::\n",
702
+ " | \n",
703
+ " | >>> # xdoctest: +SKIP(\"undefined vars\")\n",
704
+ " | >>> for name, module in model.named_children():\n",
705
+ " | >>> if name in ['conv4', 'conv5']:\n",
706
+ " | >>> print(module)\n",
707
+ " | \n",
708
+ " | named_modules(self, memo: Optional[Set[ForwardRef('Module')]] = None, prefix: str = '', remove_duplicate: bool = True)\n",
709
+ " | Returns an iterator over all modules in the network, yielding\n",
710
+ " | both the name of the module as well as the module itself.\n",
711
+ " | \n",
712
+ " | Args:\n",
713
+ " | memo: a memo to store the set of modules already added to the result\n",
714
+ " | prefix: a prefix that will be added to the name of the module\n",
715
+ " | remove_duplicate: whether to remove the duplicated module instances in the result\n",
716
+ " | or not\n",
717
+ " | \n",
718
+ " | Yields:\n",
719
+ " | (str, Module): Tuple of name and module\n",
720
+ " | \n",
721
+ " | Note:\n",
722
+ " | Duplicate modules are returned only once. In the following\n",
723
+ " | example, ``l`` will be returned only once.\n",
724
+ " | \n",
725
+ " | Example::\n",
726
+ " | \n",
727
+ " | >>> l = nn.Linear(2, 2)\n",
728
+ " | >>> net = nn.Sequential(l, l)\n",
729
+ " | >>> for idx, m in enumerate(net.named_modules()):\n",
730
+ " | ... print(idx, '->', m)\n",
731
+ " | \n",
732
+ " | 0 -> ('', Sequential(\n",
733
+ " | (0): Linear(in_features=2, out_features=2, bias=True)\n",
734
+ " | (1): Linear(in_features=2, out_features=2, bias=True)\n",
735
+ " | ))\n",
736
+ " | 1 -> ('0', Linear(in_features=2, out_features=2, bias=True))\n",
737
+ " | \n",
738
+ " | named_parameters(self, prefix: str = '', recurse: bool = True, remove_duplicate: bool = True) -> Iterator[Tuple[str, torch.nn.parameter.Parameter]]\n",
739
+ " | Returns an iterator over module parameters, yielding both the\n",
740
+ " | name of the parameter as well as the parameter itself.\n",
741
+ " | \n",
742
+ " | Args:\n",
743
+ " | prefix (str): prefix to prepend to all parameter names.\n",
744
+ " | recurse (bool): if True, then yields parameters of this module\n",
745
+ " | and all submodules. Otherwise, yields only parameters that\n",
746
+ " | are direct members of this module.\n",
747
+ " | remove_duplicate (bool, optional): whether to remove the duplicated\n",
748
+ " | parameters in the result. Defaults to True.\n",
749
+ " | \n",
750
+ " | Yields:\n",
751
+ " | (str, Parameter): Tuple containing the name and parameter\n",
752
+ " | \n",
753
+ " | Example::\n",
754
+ " | \n",
755
+ " | >>> # xdoctest: +SKIP(\"undefined vars\")\n",
756
+ " | >>> for name, param in self.named_parameters():\n",
757
+ " | >>> if name in ['bias']:\n",
758
+ " | >>> print(param.size())\n",
759
+ " | \n",
760
+ " | parameters(self, recurse: bool = True) -> Iterator[torch.nn.parameter.Parameter]\n",
761
+ " | Returns an iterator over module parameters.\n",
762
+ " | \n",
763
+ " | This is typically passed to an optimizer.\n",
764
+ " | \n",
765
+ " | Args:\n",
766
+ " | recurse (bool): if True, then yields parameters of this module\n",
767
+ " | and all submodules. Otherwise, yields only parameters that\n",
768
+ " | are direct members of this module.\n",
769
+ " | \n",
770
+ " | Yields:\n",
771
+ " | Parameter: module parameter\n",
772
+ " | \n",
773
+ " | Example::\n",
774
+ " | \n",
775
+ " | >>> # xdoctest: +SKIP(\"undefined vars\")\n",
776
+ " | >>> for param in model.parameters():\n",
777
+ " | >>> print(type(param), param.size())\n",
778
+ " | <class 'torch.Tensor'> (20L,)\n",
779
+ " | <class 'torch.Tensor'> (20L, 1L, 5L, 5L)\n",
780
+ " | \n",
781
+ " | register_backward_hook(self, hook: Callable[[ForwardRef('Module'), Union[Tuple[torch.Tensor, ...], torch.Tensor], Union[Tuple[torch.Tensor, ...], torch.Tensor]], Union[NoneType, Tuple[torch.Tensor, ...], torch.Tensor]]) -> torch.utils.hooks.RemovableHandle\n",
782
+ " | Registers a backward hook on the module.\n",
783
+ " | \n",
784
+ " | This function is deprecated in favor of :meth:`~torch.nn.Module.register_full_backward_hook` and\n",
785
+ " | the behavior of this function will change in future versions.\n",
786
+ " | \n",
787
+ " | Returns:\n",
788
+ " | :class:`torch.utils.hooks.RemovableHandle`:\n",
789
+ " | a handle that can be used to remove the added hook by calling\n",
790
+ " | ``handle.remove()``\n",
791
+ " | \n",
792
+ " | register_buffer(self, name: str, tensor: Optional[torch.Tensor], persistent: bool = True) -> None\n",
793
+ " | Adds a buffer to the module.\n",
794
+ " | \n",
795
+ " | This is typically used to register a buffer that should not to be\n",
796
+ " | considered a model parameter. For example, BatchNorm's ``running_mean``\n",
797
+ " | is not a parameter, but is part of the module's state. Buffers, by\n",
798
+ " | default, are persistent and will be saved alongside parameters. This\n",
799
+ " | behavior can be changed by setting :attr:`persistent` to ``False``. The\n",
800
+ " | only difference between a persistent buffer and a non-persistent buffer\n",
801
+ " | is that the latter will not be a part of this module's\n",
802
+ " | :attr:`state_dict`.\n",
803
+ " | \n",
804
+ " | Buffers can be accessed as attributes using given names.\n",
805
+ " | \n",
806
+ " | Args:\n",
807
+ " | name (str): name of the buffer. The buffer can be accessed\n",
808
+ " | from this module using the given name\n",
809
+ " | tensor (Tensor or None): buffer to be registered. If ``None``, then operations\n",
810
+ " | that run on buffers, such as :attr:`cuda`, are ignored. If ``None``,\n",
811
+ " | the buffer is **not** included in the module's :attr:`state_dict`.\n",
812
+ " | persistent (bool): whether the buffer is part of this module's\n",
813
+ " | :attr:`state_dict`.\n",
814
+ " | \n",
815
+ " | Example::\n",
816
+ " | \n",
817
+ " | >>> # xdoctest: +SKIP(\"undefined vars\")\n",
818
+ " | >>> self.register_buffer('running_mean', torch.zeros(num_features))\n",
819
+ " | \n",
820
+ " | register_forward_hook(self, hook: Union[Callable[[~T, Tuple[Any, ...], Any], Optional[Any]], Callable[[~T, Tuple[Any, ...], Dict[str, Any], Any], Optional[Any]]], *, prepend: bool = False, with_kwargs: bool = False) -> torch.utils.hooks.RemovableHandle\n",
821
+ " | Registers a forward hook on the module.\n",
822
+ " | \n",
823
+ " | The hook will be called every time after :func:`forward` has computed an output.\n",
824
+ " | \n",
825
+ " | If ``with_kwargs`` is ``False`` or not specified, the input contains only\n",
826
+ " | the positional arguments given to the module. Keyword arguments won't be\n",
827
+ " | passed to the hooks and only to the ``forward``. The hook can modify the\n",
828
+ " | output. It can modify the input inplace but it will not have effect on\n",
829
+ " | forward since this is called after :func:`forward` is called. The hook\n",
830
+ " | should have the following signature::\n",
831
+ " | \n",
832
+ " | hook(module, args, output) -> None or modified output\n",
833
+ " | \n",
834
+ " | If ``with_kwargs`` is ``True``, the forward hook will be passed the\n",
835
+ " | ``kwargs`` given to the forward function and be expected to return the\n",
836
+ " | output possibly modified. The hook should have the following signature::\n",
837
+ " | \n",
838
+ " | hook(module, args, kwargs, output) -> None or modified output\n",
839
+ " | \n",
840
+ " | Args:\n",
841
+ " | hook (Callable): The user defined hook to be registered.\n",
842
+ " | prepend (bool): If ``True``, the provided ``hook`` will be fired\n",
843
+ " | before all existing ``forward`` hooks on this\n",
844
+ " | :class:`torch.nn.modules.Module`. Otherwise, the provided\n",
845
+ " | ``hook`` will be fired after all existing ``forward`` hooks on\n",
846
+ " | this :class:`torch.nn.modules.Module`. Note that global\n",
847
+ " | ``forward`` hooks registered with\n",
848
+ " | :func:`register_module_forward_hook` will fire before all hooks\n",
849
+ " | registered by this method.\n",
850
+ " | Default: ``False``\n",
851
+ " | with_kwargs (bool): If ``True``, the ``hook`` will be passed the\n",
852
+ " | kwargs given to the forward function.\n",
853
+ " | Default: ``False``\n",
854
+ " | \n",
855
+ " | Returns:\n",
856
+ " | :class:`torch.utils.hooks.RemovableHandle`:\n",
857
+ " | a handle that can be used to remove the added hook by calling\n",
858
+ " | ``handle.remove()``\n",
859
+ " | \n",
860
+ " | register_forward_pre_hook(self, hook: Union[Callable[[~T, Tuple[Any, ...]], Optional[Any]], Callable[[~T, Tuple[Any, ...], Dict[str, Any]], Optional[Tuple[Any, Dict[str, Any]]]]], *, prepend: bool = False, with_kwargs: bool = False) -> torch.utils.hooks.RemovableHandle\n",
861
+ " | Registers a forward pre-hook on the module.\n",
862
+ " | \n",
863
+ " | The hook will be called every time before :func:`forward` is invoked.\n",
864
+ " | \n",
865
+ " | \n",
866
+ " | If ``with_kwargs`` is false or not specified, the input contains only\n",
867
+ " | the positional arguments given to the module. Keyword arguments won't be\n",
868
+ " | passed to the hooks and only to the ``forward``. The hook can modify the\n",
869
+ " | input. User can either return a tuple or a single modified value in the\n",
870
+ " | hook. We will wrap the value into a tuple if a single value is returned\n",
871
+ " | (unless that value is already a tuple). The hook should have the\n",
872
+ " | following signature::\n",
873
+ " | \n",
874
+ " | hook(module, args) -> None or modified input\n",
875
+ " | \n",
876
+ " | If ``with_kwargs`` is true, the forward pre-hook will be passed the\n",
877
+ " | kwargs given to the forward function. And if the hook modifies the\n",
878
+ " | input, both the args and kwargs should be returned. The hook should have\n",
879
+ " | the following signature::\n",
880
+ " | \n",
881
+ " | hook(module, args, kwargs) -> None or a tuple of modified input and kwargs\n",
882
+ " | \n",
883
+ " | Args:\n",
884
+ " | hook (Callable): The user defined hook to be registered.\n",
885
+ " | prepend (bool): If true, the provided ``hook`` will be fired before\n",
886
+ " | all existing ``forward_pre`` hooks on this\n",
887
+ " | :class:`torch.nn.modules.Module`. Otherwise, the provided\n",
888
+ " | ``hook`` will be fired after all existing ``forward_pre`` hooks\n",
889
+ " | on this :class:`torch.nn.modules.Module`. Note that global\n",
890
+ " | ``forward_pre`` hooks registered with\n",
891
+ " | :func:`register_module_forward_pre_hook` will fire before all\n",
892
+ " | hooks registered by this method.\n",
893
+ " | Default: ``False``\n",
894
+ " | with_kwargs (bool): If true, the ``hook`` will be passed the kwargs\n",
895
+ " | given to the forward function.\n",
896
+ " | Default: ``False``\n",
897
+ " | \n",
898
+ " | Returns:\n",
899
+ " | :class:`torch.utils.hooks.RemovableHandle`:\n",
900
+ " | a handle that can be used to remove the added hook by calling\n",
901
+ " | ``handle.remove()``\n",
902
+ " | \n",
903
+ " | register_full_backward_hook(self, hook: Callable[[ForwardRef('Module'), Union[Tuple[torch.Tensor, ...], torch.Tensor], Union[Tuple[torch.Tensor, ...], torch.Tensor]], Union[NoneType, Tuple[torch.Tensor, ...], torch.Tensor]], prepend: bool = False) -> torch.utils.hooks.RemovableHandle\n",
904
+ " | Registers a backward hook on the module.\n",
905
+ " | \n",
906
+ " | The hook will be called every time the gradients with respect to a module\n",
907
+ " | are computed, i.e. the hook will execute if and only if the gradients with\n",
908
+ " | respect to module outputs are computed. The hook should have the following\n",
909
+ " | signature::\n",
910
+ " | \n",
911
+ " | hook(module, grad_input, grad_output) -> tuple(Tensor) or None\n",
912
+ " | \n",
913
+ " | The :attr:`grad_input` and :attr:`grad_output` are tuples that contain the gradients\n",
914
+ " | with respect to the inputs and outputs respectively. The hook should\n",
915
+ " | not modify its arguments, but it can optionally return a new gradient with\n",
916
+ " | respect to the input that will be used in place of :attr:`grad_input` in\n",
917
+ " | subsequent computations. :attr:`grad_input` will only correspond to the inputs given\n",
918
+ " | as positional arguments and all kwarg arguments are ignored. Entries\n",
919
+ " | in :attr:`grad_input` and :attr:`grad_output` will be ``None`` for all non-Tensor\n",
920
+ " | arguments.\n",
921
+ " | \n",
922
+ " | For technical reasons, when this hook is applied to a Module, its forward function will\n",
923
+ " | receive a view of each Tensor passed to the Module. Similarly the caller will receive a view\n",
924
+ " | of each Tensor returned by the Module's forward function.\n",
925
+ " | \n",
926
+ " | .. warning ::\n",
927
+ " | Modifying inputs or outputs inplace is not allowed when using backward hooks and\n",
928
+ " | will raise an error.\n",
929
+ " | \n",
930
+ " | Args:\n",
931
+ " | hook (Callable): The user-defined hook to be registered.\n",
932
+ " | prepend (bool): If true, the provided ``hook`` will be fired before\n",
933
+ " | all existing ``backward`` hooks on this\n",
934
+ " | :class:`torch.nn.modules.Module`. Otherwise, the provided\n",
935
+ " | ``hook`` will be fired after all existing ``backward`` hooks on\n",
936
+ " | this :class:`torch.nn.modules.Module`. Note that global\n",
937
+ " | ``backward`` hooks registered with\n",
938
+ " | :func:`register_module_full_backward_hook` will fire before\n",
939
+ " | all hooks registered by this method.\n",
940
+ " | \n",
941
+ " | Returns:\n",
942
+ " | :class:`torch.utils.hooks.RemovableHandle`:\n",
943
+ " | a handle that can be used to remove the added hook by calling\n",
944
+ " | ``handle.remove()``\n",
945
+ " | \n",
946
+ " | register_full_backward_pre_hook(self, hook: Callable[[ForwardRef('Module'), Union[Tuple[torch.Tensor, ...], torch.Tensor]], Union[NoneType, Tuple[torch.Tensor, ...], torch.Tensor]], prepend: bool = False) -> torch.utils.hooks.RemovableHandle\n",
947
+ " | Registers a backward pre-hook on the module.\n",
948
+ " | \n",
949
+ " | The hook will be called every time the gradients for the module are computed.\n",
950
+ " | The hook should have the following signature::\n",
951
+ " | \n",
952
+ " | hook(module, grad_output) -> Tensor or None\n",
953
+ " | \n",
954
+ " | The :attr:`grad_output` is a tuple. The hook should\n",
955
+ " | not modify its arguments, but it can optionally return a new gradient with\n",
956
+ " | respect to the output that will be used in place of :attr:`grad_output` in\n",
957
+ " | subsequent computations. Entries in :attr:`grad_output` will be ``None`` for\n",
958
+ " | all non-Tensor arguments.\n",
959
+ " | \n",
960
+ " | For technical reasons, when this hook is applied to a Module, its forward function will\n",
961
+ " | receive a view of each Tensor passed to the Module. Similarly the caller will receive a view\n",
962
+ " | of each Tensor returned by the Module's forward function.\n",
963
+ " | \n",
964
+ " | .. warning ::\n",
965
+ " | Modifying inputs inplace is not allowed when using backward hooks and\n",
966
+ " | will raise an error.\n",
967
+ " | \n",
968
+ " | Args:\n",
969
+ " | hook (Callable): The user-defined hook to be registered.\n",
970
+ " | prepend (bool): If true, the provided ``hook`` will be fired before\n",
971
+ " | all existing ``backward_pre`` hooks on this\n",
972
+ " | :class:`torch.nn.modules.Module`. Otherwise, the provided\n",
973
+ " | ``hook`` will be fired after all existing ``backward_pre`` hooks\n",
974
+ " | on this :class:`torch.nn.modules.Module`. Note that global\n",
975
+ " | ``backward_pre`` hooks registered with\n",
976
+ " | :func:`register_module_full_backward_pre_hook` will fire before\n",
977
+ " | all hooks registered by this method.\n",
978
+ " | \n",
979
+ " | Returns:\n",
980
+ " | :class:`torch.utils.hooks.RemovableHandle`:\n",
981
+ " | a handle that can be used to remove the added hook by calling\n",
982
+ " | ``handle.remove()``\n",
983
+ " | \n",
984
+ " | register_load_state_dict_post_hook(self, hook)\n",
985
+ " | Registers a post hook to be run after module's ``load_state_dict``\n",
986
+ " | is called.\n",
987
+ " | \n",
988
+ " | It should have the following signature::\n",
989
+ " | hook(module, incompatible_keys) -> None\n",
990
+ " | \n",
991
+ " | The ``module`` argument is the current module that this hook is registered\n",
992
+ " | on, and the ``incompatible_keys`` argument is a ``NamedTuple`` consisting\n",
993
+ " | of attributes ``missing_keys`` and ``unexpected_keys``. ``missing_keys``\n",
994
+ " | is a ``list`` of ``str`` containing the missing keys and\n",
995
+ " | ``unexpected_keys`` is a ``list`` of ``str`` containing the unexpected keys.\n",
996
+ " | \n",
997
+ " | The given incompatible_keys can be modified inplace if needed.\n",
998
+ " | \n",
999
+ " | Note that the checks performed when calling :func:`load_state_dict` with\n",
1000
+ " | ``strict=True`` are affected by modifications the hook makes to\n",
1001
+ " | ``missing_keys`` or ``unexpected_keys``, as expected. Additions to either\n",
1002
+ " | set of keys will result in an error being thrown when ``strict=True``, and\n",
1003
+ " | clearing out both missing and unexpected keys will avoid an error.\n",
1004
+ " | \n",
1005
+ " | Returns:\n",
1006
+ " | :class:`torch.utils.hooks.RemovableHandle`:\n",
1007
+ " | a handle that can be used to remove the added hook by calling\n",
1008
+ " | ``handle.remove()``\n",
1009
+ " | \n",
1010
+ " | register_module(self, name: str, module: Optional[ForwardRef('Module')]) -> None\n",
1011
+ " | Alias for :func:`add_module`.\n",
1012
+ " | \n",
1013
+ " | register_parameter(self, name: str, param: Optional[torch.nn.parameter.Parameter]) -> None\n",
1014
+ " | Adds a parameter to the module.\n",
1015
+ " | \n",
1016
+ " | The parameter can be accessed as an attribute using given name.\n",
1017
+ " | \n",
1018
+ " | Args:\n",
1019
+ " | name (str): name of the parameter. The parameter can be accessed\n",
1020
+ " | from this module using the given name\n",
1021
+ " | param (Parameter or None): parameter to be added to the module. If\n",
1022
+ " | ``None``, then operations that run on parameters, such as :attr:`cuda`,\n",
1023
+ " | are ignored. If ``None``, the parameter is **not** included in the\n",
1024
+ " | module's :attr:`state_dict`.\n",
1025
+ " | \n",
1026
+ " | register_state_dict_pre_hook(self, hook)\n",
1027
+ " | These hooks will be called with arguments: ``self``, ``prefix``,\n",
1028
+ " | and ``keep_vars`` before calling ``state_dict`` on ``self``. The registered\n",
1029
+ " | hooks can be used to perform pre-processing before the ``state_dict``\n",
1030
+ " | call is made.\n",
1031
+ " | \n",
1032
+ " | requires_grad_(self: ~T, requires_grad: bool = True) -> ~T\n",
1033
+ " | Change if autograd should record operations on parameters in this\n",
1034
+ " | module.\n",
1035
+ " | \n",
1036
+ " | This method sets the parameters' :attr:`requires_grad` attributes\n",
1037
+ " | in-place.\n",
1038
+ " | \n",
1039
+ " | This method is helpful for freezing part of the module for finetuning\n",
1040
+ " | or training parts of a model individually (e.g., GAN training).\n",
1041
+ " | \n",
1042
+ " | See :ref:`locally-disable-grad-doc` for a comparison between\n",
1043
+ " | `.requires_grad_()` and several similar mechanisms that may be confused with it.\n",
1044
+ " | \n",
1045
+ " | Args:\n",
1046
+ " | requires_grad (bool): whether autograd should record operations on\n",
1047
+ " | parameters in this module. Default: ``True``.\n",
1048
+ " | \n",
1049
+ " | Returns:\n",
1050
+ " | Module: self\n",
1051
+ " | \n",
1052
+ " | set_extra_state(self, state: Any)\n",
1053
+ " | This function is called from :func:`load_state_dict` to handle any extra state\n",
1054
+ " | found within the `state_dict`. Implement this function and a corresponding\n",
1055
+ " | :func:`get_extra_state` for your module if you need to store extra state within its\n",
1056
+ " | `state_dict`.\n",
1057
+ " | \n",
1058
+ " | Args:\n",
1059
+ " | state (dict): Extra state from the `state_dict`\n",
1060
+ " | \n",
1061
+ " | share_memory(self: ~T) -> ~T\n",
1062
+ " | See :meth:`torch.Tensor.share_memory_`\n",
1063
+ " | \n",
1064
+ " | state_dict(self, *args, destination=None, prefix='', keep_vars=False)\n",
1065
+ " | Returns a dictionary containing references to the whole state of the module.\n",
1066
+ " | \n",
1067
+ " | Both parameters and persistent buffers (e.g. running averages) are\n",
1068
+ " | included. Keys are corresponding parameter and buffer names.\n",
1069
+ " | Parameters and buffers set to ``None`` are not included.\n",
1070
+ " | \n",
1071
+ " | .. note::\n",
1072
+ " | The returned object is a shallow copy. It contains references\n",
1073
+ " | to the module's parameters and buffers.\n",
1074
+ " | \n",
1075
+ " | .. warning::\n",
1076
+ " | Currently ``state_dict()`` also accepts positional arguments for\n",
1077
+ " | ``destination``, ``prefix`` and ``keep_vars`` in order. However,\n",
1078
+ " | this is being deprecated and keyword arguments will be enforced in\n",
1079
+ " | future releases.\n",
1080
+ " | \n",
1081
+ " | .. warning::\n",
1082
+ " | Please avoid the use of argument ``destination`` as it is not\n",
1083
+ " | designed for end-users.\n",
1084
+ " | \n",
1085
+ " | Args:\n",
1086
+ " | destination (dict, optional): If provided, the state of module will\n",
1087
+ " | be updated into the dict and the same object is returned.\n",
1088
+ " | Otherwise, an ``OrderedDict`` will be created and returned.\n",
1089
+ " | Default: ``None``.\n",
1090
+ " | prefix (str, optional): a prefix added to parameter and buffer\n",
1091
+ " | names to compose the keys in state_dict. Default: ``''``.\n",
1092
+ " | keep_vars (bool, optional): by default the :class:`~torch.Tensor` s\n",
1093
+ " | returned in the state dict are detached from autograd. If it's\n",
1094
+ " | set to ``True``, detaching will not be performed.\n",
1095
+ " | Default: ``False``.\n",
1096
+ " | \n",
1097
+ " | Returns:\n",
1098
+ " | dict:\n",
1099
+ " | a dictionary containing a whole state of the module\n",
1100
+ " | \n",
1101
+ " | Example::\n",
1102
+ " | \n",
1103
+ " | >>> # xdoctest: +SKIP(\"undefined vars\")\n",
1104
+ " | >>> module.state_dict().keys()\n",
1105
+ " | ['bias', 'weight']\n",
1106
+ " | \n",
1107
+ " | to(self, *args, **kwargs)\n",
1108
+ " | Moves and/or casts the parameters and buffers.\n",
1109
+ " | \n",
1110
+ " | This can be called as\n",
1111
+ " | \n",
1112
+ " | .. function:: to(device=None, dtype=None, non_blocking=False)\n",
1113
+ " | :noindex:\n",
1114
+ " | \n",
1115
+ " | .. function:: to(dtype, non_blocking=False)\n",
1116
+ " | :noindex:\n",
1117
+ " | \n",
1118
+ " | .. function:: to(tensor, non_blocking=False)\n",
1119
+ " | :noindex:\n",
1120
+ " | \n",
1121
+ " | .. function:: to(memory_format=torch.channels_last)\n",
1122
+ " | :noindex:\n",
1123
+ " | \n",
1124
+ " | Its signature is similar to :meth:`torch.Tensor.to`, but only accepts\n",
1125
+ " | floating point or complex :attr:`dtype`\\ s. In addition, this method will\n",
1126
+ " | only cast the floating point or complex parameters and buffers to :attr:`dtype`\n",
1127
+ " | (if given). The integral parameters and buffers will be moved\n",
1128
+ " | :attr:`device`, if that is given, but with dtypes unchanged. When\n",
1129
+ " | :attr:`non_blocking` is set, it tries to convert/move asynchronously\n",
1130
+ " | with respect to the host if possible, e.g., moving CPU Tensors with\n",
1131
+ " | pinned memory to CUDA devices.\n",
1132
+ " | \n",
1133
+ " | See below for examples.\n",
1134
+ " | \n",
1135
+ " | .. note::\n",
1136
+ " | This method modifies the module in-place.\n",
1137
+ " | \n",
1138
+ " | Args:\n",
1139
+ " | device (:class:`torch.device`): the desired device of the parameters\n",
1140
+ " | and buffers in this module\n",
1141
+ " | dtype (:class:`torch.dtype`): the desired floating point or complex dtype of\n",
1142
+ " | the parameters and buffers in this module\n",
1143
+ " | tensor (torch.Tensor): Tensor whose dtype and device are the desired\n",
1144
+ " | dtype and device for all parameters and buffers in this module\n",
1145
+ " | memory_format (:class:`torch.memory_format`): the desired memory\n",
1146
+ " | format for 4D parameters and buffers in this module (keyword\n",
1147
+ " | only argument)\n",
1148
+ " | \n",
1149
+ " | Returns:\n",
1150
+ " | Module: self\n",
1151
+ " | \n",
1152
+ " | Examples::\n",
1153
+ " | \n",
1154
+ " | >>> # xdoctest: +IGNORE_WANT(\"non-deterministic\")\n",
1155
+ " | >>> linear = nn.Linear(2, 2)\n",
1156
+ " | >>> linear.weight\n",
1157
+ " | Parameter containing:\n",
1158
+ " | tensor([[ 0.1913, -0.3420],\n",
1159
+ " | [-0.5113, -0.2325]])\n",
1160
+ " | >>> linear.to(torch.double)\n",
1161
+ " | Linear(in_features=2, out_features=2, bias=True)\n",
1162
+ " | >>> linear.weight\n",
1163
+ " | Parameter containing:\n",
1164
+ " | tensor([[ 0.1913, -0.3420],\n",
1165
+ " | [-0.5113, -0.2325]], dtype=torch.float64)\n",
1166
+ " | >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA1)\n",
1167
+ " | >>> gpu1 = torch.device(\"cuda:1\")\n",
1168
+ " | >>> linear.to(gpu1, dtype=torch.half, non_blocking=True)\n",
1169
+ " | Linear(in_features=2, out_features=2, bias=True)\n",
1170
+ " | >>> linear.weight\n",
1171
+ " | Parameter containing:\n",
1172
+ " | tensor([[ 0.1914, -0.3420],\n",
1173
+ " | [-0.5112, -0.2324]], dtype=torch.float16, device='cuda:1')\n",
1174
+ " | >>> cpu = torch.device(\"cpu\")\n",
1175
+ " | >>> linear.to(cpu)\n",
1176
+ " | Linear(in_features=2, out_features=2, bias=True)\n",
1177
+ " | >>> linear.weight\n",
1178
+ " | Parameter containing:\n",
1179
+ " | tensor([[ 0.1914, -0.3420],\n",
1180
+ " | [-0.5112, -0.2324]], dtype=torch.float16)\n",
1181
+ " | \n",
1182
+ " | >>> linear = nn.Linear(2, 2, bias=None).to(torch.cdouble)\n",
1183
+ " | >>> linear.weight\n",
1184
+ " | Parameter containing:\n",
1185
+ " | tensor([[ 0.3741+0.j, 0.2382+0.j],\n",
1186
+ " | [ 0.5593+0.j, -0.4443+0.j]], dtype=torch.complex128)\n",
1187
+ " | >>> linear(torch.ones(3, 2, dtype=torch.cdouble))\n",
1188
+ " | tensor([[0.6122+0.j, 0.1150+0.j],\n",
1189
+ " | [0.6122+0.j, 0.1150+0.j],\n",
1190
+ " | [0.6122+0.j, 0.1150+0.j]], dtype=torch.complex128)\n",
1191
+ " | \n",
1192
+ " | to_empty(self: ~T, *, device: Union[str, torch.device]) -> ~T\n",
1193
+ " | Moves the parameters and buffers to the specified device without copying storage.\n",
1194
+ " | \n",
1195
+ " | Args:\n",
1196
+ " | device (:class:`torch.device`): The desired device of the parameters\n",
1197
+ " | and buffers in this module.\n",
1198
+ " | \n",
1199
+ " | Returns:\n",
1200
+ " | Module: self\n",
1201
+ " | \n",
1202
+ " | train(self: ~T, mode: bool = True) -> ~T\n",
1203
+ " | Sets the module in training mode.\n",
1204
+ " | \n",
1205
+ " | This has any effect only on certain modules. See documentations of\n",
1206
+ " | particular modules for details of their behaviors in training/evaluation\n",
1207
+ " | mode, if they are affected, e.g. :class:`Dropout`, :class:`BatchNorm`,\n",
1208
+ " | etc.\n",
1209
+ " | \n",
1210
+ " | Args:\n",
1211
+ " | mode (bool): whether to set training mode (``True``) or evaluation\n",
1212
+ " | mode (``False``). Default: ``True``.\n",
1213
+ " | \n",
1214
+ " | Returns:\n",
1215
+ " | Module: self\n",
1216
+ " | \n",
1217
+ " | type(self: ~T, dst_type: Union[torch.dtype, str]) -> ~T\n",
1218
+ " | Casts all parameters and buffers to :attr:`dst_type`.\n",
1219
+ " | \n",
1220
+ " | .. note::\n",
1221
+ " | This method modifies the module in-place.\n",
1222
+ " | \n",
1223
+ " | Args:\n",
1224
+ " | dst_type (type or string): the desired type\n",
1225
+ " | \n",
1226
+ " | Returns:\n",
1227
+ " | Module: self\n",
1228
+ " | \n",
1229
+ " | xpu(self: ~T, device: Union[int, torch.device, NoneType] = None) -> ~T\n",
1230
+ " | Moves all model parameters and buffers to the XPU.\n",
1231
+ " | \n",
1232
+ " | This also makes associated parameters and buffers different objects. So\n",
1233
+ " | it should be called before constructing optimizer if the module will\n",
1234
+ " | live on XPU while being optimized.\n",
1235
+ " | \n",
1236
+ " | .. note::\n",
1237
+ " | This method modifies the module in-place.\n",
1238
+ " | \n",
1239
+ " | Arguments:\n",
1240
+ " | device (int, optional): if specified, all parameters will be\n",
1241
+ " | copied to that device\n",
1242
+ " | \n",
1243
+ " | Returns:\n",
1244
+ " | Module: self\n",
1245
+ " | \n",
1246
+ " | zero_grad(self, set_to_none: bool = True) -> None\n",
1247
+ " | Sets gradients of all model parameters to zero. See similar function\n",
1248
+ " | under :class:`torch.optim.Optimizer` for more context.\n",
1249
+ " | \n",
1250
+ " | Args:\n",
1251
+ " | set_to_none (bool): instead of setting to zero, set the grads to None.\n",
1252
+ " | See :meth:`torch.optim.Optimizer.zero_grad` for details.\n",
1253
+ " | \n",
1254
+ " | ----------------------------------------------------------------------\n",
1255
+ " | Data descriptors inherited from torch.nn.modules.module.Module:\n",
1256
+ " | \n",
1257
+ " | __dict__\n",
1258
+ " | dictionary for instance variables (if defined)\n",
1259
+ " | \n",
1260
+ " | __weakref__\n",
1261
+ " | list of weak references to the object (if defined)\n",
1262
+ " | \n",
1263
+ " | ----------------------------------------------------------------------\n",
1264
+ " | Data and other attributes inherited from torch.nn.modules.module.Module:\n",
1265
+ " | \n",
1266
+ " | T_destination = ~T_destination\n",
1267
+ " | \n",
1268
+ " | call_super_init = False\n",
1269
+ " | \n",
1270
+ " | dump_patches = False\n",
1271
+ "\n"
1272
+ ]
1273
+ }
1274
+ ],
1275
+ "source": [
1276
+ "help(nn.Linear)"
1277
+ ]
1278
+ },
1279
+ {
1280
+ "cell_type": "code",
1281
+ "execution_count": null,
1282
+ "id": "71647469-0803-425c-836d-7623abc7bdd6",
1283
+ "metadata": {},
1284
+ "outputs": [],
1285
+ "source": []
1286
+ }
1287
+ ],
1288
+ "metadata": {
1289
+ "kernelspec": {
1290
+ "display_name": "Python (mistral)",
1291
+ "language": "python",
1292
+ "name": "mistral"
1293
+ },
1294
+ "language_info": {
1295
+ "codemirror_mode": {
1296
+ "name": "ipython",
1297
+ "version": 3
1298
+ },
1299
+ "file_extension": ".py",
1300
+ "mimetype": "text/x-python",
1301
+ "name": "python",
1302
+ "nbconvert_exporter": "python",
1303
+ "pygments_lexer": "ipython3",
1304
+ "version": "3.10.13"
1305
+ }
1306
+ },
1307
+ "nbformat": 4,
1308
+ "nbformat_minor": 5
1309
+ }
OVM/OVM/.ipynb_checkpoints/__init__-checkpoint.py ADDED
File without changes
OVM/OVM/.ipynb_checkpoints/build_verifier-checkpoint.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers.generation.utils import ModelOutput
2
+ from transformers import PreTrainedModel
3
+ from typing import Optional, List, Dict
4
+ import torch
5
+ import torch.nn as nn
6
+ import torch.nn.functional as F
7
+ from dataclasses import dataclass
8
+
9
+ @dataclass
10
+ class VerifierModelOutput(ModelOutput):
11
+ loss: Optional[torch.FloatTensor] = None
12
+ v_scores: torch.FloatTensor = None
13
+ all_losses: Optional[Dict[str, torch.FloatTensor]] = None
14
+
15
+ class VerifierModel(nn.Module):
16
+ def __init__(self, backbone, checkpoint_dir = None):
17
+ super(VerifierModel, self).__init__()
18
+ self.backbone = backbone
19
+
20
+ device = self.backbone.device
21
+ dtype = self.backbone.dtype
22
+
23
+ self.gain = nn.Parameter(
24
+ torch.randn((1,), device = device, dtype = dtype)
25
+ )
26
+ self.bias = nn.Parameter(
27
+ torch.randn((1,), device = device, dtype = dtype)
28
+ )
29
+ self.dropout = nn.Dropout(p = 0.2)
30
+ self.vscore_head = nn.Linear(self.backbone.get_input_embeddings().embedding_dim, 1, bias = False, device = device, dtype = dtype)
31
+
32
+ if checkpoint_dir:
33
+ verifier_params = torch.load(checkpoint_dir)
34
+ self.load_state_dict(verifier_params, strict = False)
35
+ torch.cuda.empty_cache()
36
+
37
+ else:
38
+ self.init_head_params()
39
+
40
+ self.pad_token_id = backbone.config.pad_token_id
41
+
42
+ def init_head_params(self):
43
+ output_embeddings = self.backbone.get_output_embeddings().weight.data
44
+ output_embeddings_avg = output_embeddings.mean(dim = 0, keepdim = True)
45
+
46
+ self.vscore_head.weight = nn.Parameter(output_embeddings_avg)
47
+
48
+ def loss_fct(self, v_scores: torch.FloatTensor, v_labels: torch.LongTensor):
49
+ return self.mse_loss_with_mask(v_scores.squeeze(), v_labels.type_as(v_scores))
50
+
51
+ def transform(self, last_hidden_states):
52
+ return self.gain * last_hidden_states + self.bias
53
+
54
+ def forward(self,
55
+ input_ids: torch.LongTensor,
56
+ attention_mask: Optional[torch.Tensor] = None,
57
+ position_ids: Optional[torch.LongTensor] = None,
58
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
59
+ labels: Optional[torch.LongTensor] = None,
60
+ v_labels: Optional[torch.LongTensor] = None,
61
+ output_all_losses: Optional[bool] = None,
62
+ ):
63
+ outputs = self.backbone(
64
+ input_ids = input_ids,
65
+ attention_mask = attention_mask,
66
+ position_ids = position_ids,
67
+ past_key_values = past_key_values,
68
+ labels = labels,
69
+ use_cache = False,
70
+ output_hidden_states = True,
71
+ return_dict = True,
72
+ )
73
+ llm_logits = outputs.logits
74
+ llm_loss = outputs.loss
75
+ llm_hidden_states = outputs.hidden_states
76
+
77
+ v_hidden_states = self.transform(llm_hidden_states[-1])
78
+ v_scores = self.vscore_head(self.dropout(v_hidden_states))
79
+
80
+ v_loss, loss = None, None
81
+ if v_labels:
82
+ v_loss = self.loss_fct(v_scores, v_labels)
83
+ loss = v_loss + (llm_loss if labels is not None else 0)
84
+
85
+ all_losses = None
86
+ if output_all_losses:
87
+ all_losses = {'llm_loss': llm_loss, 'v_loss': v_loss}
88
+
89
+ return VerifierModelOutput(
90
+ loss = loss,
91
+ v_scores = v_scores,
92
+ all_losses = all_losses,
93
+ )
94
+
95
+ def mse_loss_with_mask(self, scores: torch.FloatTensor, labels: torch.FloatTensor, IGNORE_INDEX: int = -100):
96
+ scores = torch.where(labels.ne(IGNORE_INDEX), scores, 0)
97
+ labels = torch.where(labels.ne(IGNORE_INDEX), labels, 0)
98
+ return F.mse_loss(scores, labels, reduction = 'sum') / scores.shape[0]
99
+
100
+
OVM/OVM/.ipynb_checkpoints/gen_solutions-1-checkpoint.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
3
+ os.environ["CUDA_VISIBLE_DEVICES"]="4,5,6,7"
4
+
5
+ from datasets import load_dataset, Dataset
6
+ import transformers
7
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
8
+ from transformers import DataCollatorForSeq2Seq, get_scheduler
9
+ import torch
10
+ from peft import LoraConfig, PeftConfig, get_peft_model, PeftModel
11
+ from torch.optim import AdamW
12
+ from tqdm.auto import tqdm
13
+ import os
14
+ from prompt_template import Prompter
15
+
16
+ dataset = load_dataset("longhoang06/Vi-GSM8K", split = "train")
17
+ gen_size = 3750
18
+ # gen_dataset = dataset.select(range(gen_size))
19
+ # gen_dataset = dataset.select(range(1000)) # part 1
20
+ gen_dataset = dataset.select(range(1000, 1500)) # part 2
21
+ # gen_dataset = dataset.select(range(1500, 2500)) # part 3
22
+ # gen_dataset = dataset.select(range(2500, 3750)) # part 4
23
+
24
+
25
+ model_path = "mistralai/Mistral-7B-v0.1"
26
+ peft_path = "checkpoint/generator"
27
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
28
+ tokenizer.pad_token = tokenizer.eos_token
29
+
30
+ model = AutoModelForCausalLM.from_pretrained(model_path, device_map = "auto", torch_dtype = torch.bfloat16)
31
+ model = PeftModel.from_pretrained(model, peft_path)
32
+ model = model.merge_and_unload()
33
+
34
+ def generator_and_save(dataset):
35
+ def get_answer(text):
36
+ return text.split("Đáp án:")[-1].strip()
37
+
38
+ prompter = Prompter()
39
+ progress_bar = tqdm(range(len(dataset)))
40
+ results = []
41
+
42
+ for d in dataset:
43
+ question = d["question"]
44
+ answer = d["answer"]
45
+ prompt = prompter.generate_prompt(instruction = question)
46
+
47
+ input_ids = tokenizer(prompt, return_tensors = "pt").input_ids
48
+ with torch.no_grad():
49
+ outputs = model.generate(
50
+ input_ids = input_ids.to("cuda"),
51
+ max_new_tokens = 512,
52
+ temperature = 0.7,
53
+ top_k = 50,
54
+ top_p = 1,
55
+ num_return_sequences = 50,
56
+ bos_token_id = tokenizer.bos_token_id,
57
+ eos_token_id = tokenizer.eos_token_id,
58
+ do_sample = True,
59
+ )
60
+ outputs.to("cpu")
61
+ texts = tokenizer.batch_decode(outputs, skip_special_tokens = True)
62
+ texts = [prompter.get_response(text) for text in texts]
63
+ batch = [
64
+ {"question": question,
65
+ "answer": answer,
66
+ "candidate": candidate,
67
+ "label": True if get_answer(candidate) == get_answer(answer) else False
68
+ } for candidate in texts
69
+ ]
70
+ results.extend(batch)
71
+ del batch
72
+ del input_ids
73
+ progress_bar.update(1)
74
+
75
+ new_dataset = Dataset.from_dict(
76
+ {"question": [d["question"] for d in results],
77
+ "answer": [d["answer"] for d in results],
78
+ "candidate": [d["candidate"] for d in results],
79
+ "label": [d["label"] for d in results],
80
+ }
81
+ )
82
+
83
+ # new_dataset.save_to_disk("data/gen1")
84
+ new_dataset.save_to_disk("data/gen1-1000-1500")
85
+
86
+ if __name__ == "__main__":
87
+ generator_and_save(gen_dataset)
88
+
89
+
90
+
OVM/OVM/.ipynb_checkpoints/gen_solutions-2-checkpoint.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
3
+ os.environ["CUDA_VISIBLE_DEVICES"]="2,3"
4
+
5
+ from datasets import load_dataset, Dataset
6
+ import transformers
7
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
8
+ from transformers import DataCollatorForSeq2Seq, get_scheduler
9
+ import torch
10
+ from peft import LoraConfig, PeftConfig, get_peft_model, PeftModel
11
+ from torch.optim import AdamW
12
+ from tqdm.auto import tqdm
13
+ import os
14
+ from prompt_template import Prompter
15
+
16
+ dataset = load_dataset("longhoang06/Vi-GSM8K", split = "train")
17
+ gen_size = 3750
18
+ gen_dataset = dataset.select(range(gen_size, 2*gen_size))
19
+
20
+ model_path = "mistralai/Mistral-7B-v0.1"
21
+ peft_path = "checkpoint/generator"
22
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
23
+ tokenizer.pad_token = tokenizer.eos_token
24
+
25
+ model = AutoModelForCausalLM.from_pretrained(model_path, device_map = "auto", torch_dtype = torch.bfloat16)
26
+ model = PeftModel.from_pretrained(model, peft_path)
27
+ model = model.merge_and_unload()
28
+
29
+ def generator_and_save(dataset):
30
+ def get_answer(text):
31
+ return text.split("Đáp án:")[-1].strip()
32
+
33
+ prompter = Prompter()
34
+ progress_bar = tqdm(range(len(dataset)))
35
+ results = []
36
+
37
+ for d in dataset:
38
+ question = d["question"]
39
+ answer = d["answer"]
40
+ prompt = prompter.generate_prompt(instruction = question)
41
+
42
+ input_ids = tokenizer(prompt, return_tensors = "pt").input_ids
43
+ with torch.no_grad():
44
+ outputs = model.generate(
45
+ input_ids = input_ids.to("cuda"),
46
+ max_new_tokens = 512,
47
+ temperature = 0.7,
48
+ top_k = 50,
49
+ top_p = 1,
50
+ num_return_sequences = 50,
51
+ bos_token_id = tokenizer.bos_token_id,
52
+ eos_token_id = tokenizer.eos_token_id,
53
+ do_sample = True,
54
+ )
55
+ texts = tokenizer.batch_decode(outputs, skip_special_tokens = True)
56
+ texts = [prompter.get_response(text) for text in texts]
57
+ batch = [
58
+ {"question": question,
59
+ "answer": answer,
60
+ "candidate": candidate,
61
+ "label": True if get_answer(candidate) == get_answer(answer) else False
62
+ } for candidate in texts
63
+ ]
64
+ results.extend(batch)
65
+ del batch
66
+ del input_ids
67
+ progress_bar.update(1)
68
+
69
+ new_dataset = Dataset.from_dict(
70
+ {"question": [d["question"] for d in results],
71
+ "answer": [d["answer"] for d in results],
72
+ "candidate": [d["candidate"] for d in results],
73
+ "label": [d["label"] for d in results],
74
+ }
75
+ )
76
+
77
+ new_dataset.save_to_disk("data/gen2")
78
+
79
+ if __name__ == "__main__":
80
+ generator_and_save(gen_dataset)
81
+
82
+
83
+
OVM/OVM/.ipynb_checkpoints/prompt_template-checkpoint.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class Prompter:
2
+ def __init__(self):
3
+ self.generator_template = "Dưới đây là một Instruction mô tả nhiệm vụ. Viết một Response hoàn thành yêu cầu một cách thích hợp.\n\n ### Instruction:\n{instruction}\n\n### Response: Hãy suy nghĩ từng bước.\n"
4
+ self.verifier_template = "### Câu hỏi: {question}\n### Trả lời: {answer}"
5
+
6
+ def generator_prompt(
7
+ self,
8
+ instruction: str,
9
+ response: str = None,
10
+ ) -> str:
11
+
12
+ prompt = self.generator_template.format(instruction = instruction)
13
+ if response:
14
+ prompt = f"{prompt}{response}"
15
+ return prompt
16
+
17
+ def verifier_prompt(
18
+ self,
19
+ question: str,
20
+ answer: str,
21
+ ) -> str:
22
+ return self.verifier_template.format(question = question, answer = answer)
23
+
24
+ def get_response(self, output: str) -> str:
25
+ parts = output.split("### Response: Hãy suy nghĩ từng bước.")
26
+ if len(parts) > 1:
27
+ return parts[1].strip()
28
+ else:
29
+ return ""
OVM/OVM/.ipynb_checkpoints/train_generator-checkpoint.py ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datasets import load_dataset
2
+ import transformers
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
4
+ from transformers import DataCollatorForSeq2Seq, get_scheduler
5
+ import torch
6
+ from torch.utils.data import DataLoader
7
+ from peft import LoraConfig, PeftConfig, get_peft_model
8
+ from torch.optim import AdamW
9
+ from tqdm import tqdm
10
+ from torch.distributed import destroy_process_group, init_process_group
11
+ from torch.utils.data.distributed import DistributedSampler
12
+ from torch.nn.utils import clip_grad_norm_
13
+ from torch.nn.parallel import DistributedDataParallel as DDP
14
+ from torch.utils.data import SequentialSampler
15
+ import os
16
+ from prompt_template import Prompter
17
+
18
+ backend = "nccl"
19
+ init_process_group(backend = backend)
20
+ local_rank = int(os.environ["LOCAL_RANK"])
21
+
22
+ dataset = load_dataset("longhoang06/Vi-GSM8K", split = "train")
23
+ train_size = 7500
24
+ train_dataset = dataset.select(range(train_size))
25
+
26
+ model_path = "mistralai/Mistral-7B-v0.1"
27
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
28
+ tokenizer.pad_token = tokenizer.eos_token
29
+
30
+ prompter = Prompter()
31
+ max_length = 640
32
+ train_dataset = train_dataset.filter(lambda x: len(tokenizer(prompter.generator_prompt(instruction = x["question"], response = x["answer"])).input_ids) + 1 <= max_length)
33
+
34
+ def tokenize_fn(prompt: str):
35
+ result = tokenizer(
36
+ prompt,
37
+ truncation = True,
38
+ max_length = max_length,
39
+ padding = False,
40
+ return_tensors = None)
41
+
42
+ if (
43
+ result["input_ids"][-1] != tokenizer.eos_token_id
44
+ and len(result["input_ids"]) < max_length
45
+ ):
46
+ result["input_ids"].append(tokenizer.eos_token_id)
47
+ result["attention_mask"].append(1)
48
+
49
+ result["labels"] = result["input_ids"].copy()
50
+ return result
51
+
52
+ def get_items(dataset):
53
+ full_prompt = prompter.generator_prompt(
54
+ dataset["question"],
55
+ dataset["answer"],
56
+ )
57
+
58
+ tokenized_full_prompt = tokenize_fn(full_prompt)
59
+ return tokenized_full_prompt
60
+
61
+ train_dataset = train_dataset.map(get_items, num_proc = 16)
62
+ train_dataset = train_dataset.remove_columns(column_names = ["question", "answer"])
63
+
64
+ train_dataloader = DataLoader(
65
+ train_dataset,
66
+ batch_size = 1,
67
+ sampler = DistributedSampler(train_dataset),
68
+ collate_fn = DataCollatorForSeq2Seq(
69
+ tokenizer = tokenizer,
70
+ padding = True,
71
+ return_tensors = "pt",
72
+ ),
73
+ pin_memory = True,
74
+ )
75
+
76
+ bnb_config = BitsAndBytesConfig(
77
+ load_in_4bit = True,
78
+ bnb_4bit_use_double_quant = False,
79
+ bnb_4bit_quant_type = "nf4",
80
+ bnb_4bit_compute_dtype = torch.bfloat16,
81
+ )
82
+
83
+ model = AutoModelForCausalLM.from_pretrained(
84
+ model_path,
85
+ quantization_config = bnb_config,
86
+ device_map = {"": torch.device(f"cuda:{local_rank}")},
87
+ torch_dtype = torch.bfloat16,
88
+ )
89
+
90
+ r = 64
91
+ lora_alpha = 16
92
+ lora_dropout = 0.1
93
+
94
+ lora_config = LoraConfig(
95
+ r = r,
96
+ lora_alpha = lora_alpha,
97
+ lora_dropout = lora_dropout,
98
+ bias = "none",
99
+ task_type = "CAUSAL_LM",
100
+ target_modules = [
101
+ "q_proj" , "k_proj" , "v_proj", "o_proj", "gate_proj" , "up_proj" ,"down_proj", "lm_head",
102
+ ]
103
+ )
104
+
105
+ model = get_peft_model(model, lora_config)
106
+
107
+ model = model.to(f"cuda:{local_rank}")
108
+ model = DDP(model, device_ids = [local_rank])
109
+
110
+ def train():
111
+
112
+ epochs = 3
113
+ lr = 2e-4
114
+ max_norm_value = 0.3
115
+ num_update_steps_per_epoch = len(train_dataloader)
116
+ num_steps = num_update_steps_per_epoch * epochs
117
+ warmup_ratio = 0.03
118
+ num_warmup_steps = int(warmup_ratio * num_steps)
119
+ optimizer = AdamW(model.parameters(), lr = lr, weight_decay = 0.001)
120
+ lr_scheduler = get_scheduler(
121
+ "cosine",
122
+ optimizer = optimizer,
123
+ num_warmup_steps = num_warmup_steps,
124
+ num_training_steps = num_steps,
125
+ )
126
+
127
+ def is_master_process():
128
+ ddp_rank = int(os.environ['RANK'])
129
+ return ddp_rank == 0
130
+
131
+ logging_steps = 100
132
+ for epoch in range(epochs):
133
+ train_dataloader.sampler.set_epoch(epoch)
134
+ total_loss = 0
135
+ cur_steps = 0
136
+ model.train()
137
+ for batch in tqdm(train_dataloader):
138
+ batch = {k: v.to(local_rank) for k, v in batch.items()}
139
+ outputs = model(**batch)
140
+
141
+ loss = outputs.loss
142
+ total_loss += loss.item()
143
+ loss.backward()
144
+
145
+ clip_grad_norm_(model.parameters(), max_norm_value)
146
+
147
+ optimizer.step()
148
+ lr_scheduler.step()
149
+ optimizer.zero_grad()
150
+
151
+ cur_steps += 1
152
+
153
+ if cur_steps % logging_steps == 0 and is_master_process():
154
+ print(f"Epoch: {epoch + 1} -- cur_steps: {cur_steps} -- train_loss: {total_loss/cur_steps} -- lr: {optimizer.param_groups[0]['lr']}")
155
+
156
+ if is_master_process():
157
+ print("SAVING......................................................................")
158
+ model.module.save_pretrained(f"checkpoint/generator")
159
+ print(f"----------------------------- END OF TRAINING -----------------------------")
160
+
161
+ if __name__ == "__main__":
162
+ train()
163
+ destroy_process_group()
OVM/OVM/.ipynb_checkpoints/train_generator-checkpoint.sh ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ export CUDA_VISIBLE_DEVICES="0,1,2,3"
2
+ torchrun --nproc_per_node=4 train_generator.py
OVM/OVM/.ipynb_checkpoints/v_dataset-checkpoint.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import transformers
3
+ from datasets import load_dataset, load_from_disk
4
+ from prompt_template import Prompter
5
+
6
+ class VerifierDataset:
7
+ def __init__(
8
+ self,
9
+ tokenizer: transformers.PreTrainedTokenizer = None,
10
+ data_path: str = None,
11
+ max_length: int = None,
12
+ load_data_method: str = None,
13
+ mapping: bool = False,
14
+ ):
15
+ self.tokenizer = tokenizer
16
+ if load_data_method == "hf_hub":
17
+ self.dataset = load_dataset(data_path)
18
+ elif load_data_method == "local":
19
+ self.dataset = load_from_disk(data_path)
20
+ else:
21
+ raise NotImplementedError
22
+
23
+ self.max_length = max_length
24
+ self.prompter = Prompter()
25
+
26
+ if mapping:
27
+ self.dataset = self.dataset.map(self.get_items, num_proc = 16)
28
+ self.dataset = self.dataset.remove_columns(["question", "answer", "candidate", "label"])
29
+
30
+ def left_padding(
31
+ self,
32
+ input_ids: list,
33
+ attention_mask: list,
34
+ labels: list,
35
+ v_labels: list,
36
+ padding_value: int = -100,
37
+ ):
38
+ pad_length = self.max_length - len(input_ids)
39
+ input_ids = [self.tokenizer.pad_token_id]*pad_length + input_ids
40
+ attention_mask = [0]*pad_length + attention_mask
41
+ labels = [padding_value]*pad_length + labels
42
+ v_labels = [padding_value]*pad_length + v_labels
43
+
44
+ return input_ids, attention_mask, labels, v_labels
45
+
46
+ def get_items(self, dataset, IGNORE_INDEX : int = -100):
47
+ prompt = self.prompter.verifier_prompt(question = dataset["question"], answer = dataset["candidate"])
48
+ question = prompt.split("### Trả lời:")[0]
49
+ len_question = len(self.tokenizer(question).input_ids)
50
+ label = dataset["label"]
51
+
52
+ result = self.tokenizer(
53
+ prompt,
54
+ truncation = True,
55
+ max_length = self.max_length,
56
+ padding = False,
57
+ return_tensors = None,
58
+ )
59
+
60
+ if (
61
+ result["input_ids"][-1] != self.tokenizer.eos_token_id
62
+ and len(result["input_ids"]) < self.max_length
63
+ ):
64
+ result["input_ids"].append(self.tokenizer.eos_token_id)
65
+ result["attention_mask"].append(1)
66
+
67
+ result["attention_mask"][:len_question] = [0]*len_question
68
+
69
+ result["labels"] = result["input_ids"].copy()
70
+ result["labels"] = [token if mask != 0 else IGNORE_INDEX for token, mask in zip(result["labels"], result["attention_mask"])]
71
+
72
+ v_labels = [int(label)] * len(result["input_ids"])
73
+ result["v_labels"] = [token if mask != 0 else IGNORE_INDEX for token, mask in zip(result["labels"], result["attention_mask"])]
74
+
75
+ result["input_ids"], result["attention_mask"], result["labels"], result["v_labels"] = self.left_padding(
76
+ result["input_ids"], result["attention_mask"], result["labels"], result["v_labels"], padding_value = IGNORE_INDEX)
77
+
78
+ return result
79
+
80
+
81
+
OVM/OVM/Untitled.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
OVM/OVM/__init__.py ADDED
File without changes
OVM/OVM/__pycache__/build_verifier.cpython-310.pyc ADDED
Binary file (3.63 kB). View file
 
OVM/OVM/__pycache__/prompt_template.cpython-310.pyc ADDED
Binary file (1.55 kB). View file
 
OVM/OVM/__pycache__/v_dataset.cpython-310.pyc ADDED
Binary file (2.63 kB). View file
 
OVM/OVM/build_verifier.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers.generation.utils import ModelOutput
2
+ from transformers import PreTrainedModel
3
+ from typing import Optional, List, Dict
4
+ import torch
5
+ import torch.nn as nn
6
+ import torch.nn.functional as F
7
+ from dataclasses import dataclass
8
+
9
+ @dataclass
10
+ class VerifierModelOutput(ModelOutput):
11
+ loss: Optional[torch.FloatTensor] = None
12
+ v_scores: torch.FloatTensor = None
13
+ all_losses: Optional[Dict[str, torch.FloatTensor]] = None
14
+
15
+ class VerifierModel(nn.Module):
16
+ def __init__(self, backbone, checkpoint_dir = None):
17
+ super(VerifierModel, self).__init__()
18
+ self.backbone = backbone
19
+
20
+ device = self.backbone.device
21
+ dtype = self.backbone.dtype
22
+
23
+ self.gain = nn.Parameter(
24
+ torch.randn((1,), device = device, dtype = dtype)
25
+ )
26
+ self.bias = nn.Parameter(
27
+ torch.randn((1,), device = device, dtype = dtype)
28
+ )
29
+ self.dropout = nn.Dropout(p = 0.2)
30
+ self.vscore_head = nn.Linear(self.backbone.get_input_embeddings().embedding_dim, 1, bias = False, device = device, dtype = dtype)
31
+
32
+ if checkpoint_dir:
33
+ verifier_params = torch.load(checkpoint_dir)
34
+ self.load_state_dict(verifier_params, strict = False)
35
+ torch.cuda.empty_cache()
36
+
37
+ else:
38
+ self.init_head_params()
39
+
40
+ self.pad_token_id = backbone.config.pad_token_id
41
+
42
+ def init_head_params(self):
43
+ output_embeddings = self.backbone.get_output_embeddings().weight.data
44
+ output_embeddings_avg = output_embeddings.mean(dim = 0, keepdim = True)
45
+
46
+ self.vscore_head.weight = nn.Parameter(output_embeddings_avg)
47
+
48
+ def loss_fct(self, v_scores: torch.FloatTensor, v_labels: torch.LongTensor):
49
+ return self.mse_loss_with_mask(v_scores.squeeze(), v_labels.type_as(v_scores))
50
+
51
+ def transform(self, last_hidden_states):
52
+ return self.gain * last_hidden_states + self.bias
53
+
54
+ def forward(self,
55
+ input_ids: torch.LongTensor,
56
+ attention_mask: Optional[torch.Tensor] = None,
57
+ position_ids: Optional[torch.LongTensor] = None,
58
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
59
+ labels: Optional[torch.LongTensor] = None,
60
+ v_labels: Optional[torch.LongTensor] = None,
61
+ output_all_losses: Optional[bool] = None,
62
+ ):
63
+ outputs = self.backbone(
64
+ input_ids = input_ids,
65
+ attention_mask = attention_mask,
66
+ position_ids = position_ids,
67
+ past_key_values = past_key_values,
68
+ labels = labels,
69
+ use_cache = False,
70
+ output_hidden_states = True,
71
+ return_dict = True,
72
+ )
73
+ llm_logits = outputs.logits
74
+ llm_loss = outputs.loss
75
+ llm_hidden_states = outputs.hidden_states
76
+
77
+ v_hidden_states = self.transform(llm_hidden_states[-1])
78
+ v_scores = self.vscore_head(self.dropout(v_hidden_states))
79
+
80
+ v_loss, loss = None, None
81
+ if v_labels:
82
+ v_loss = self.loss_fct(v_scores, v_labels)
83
+ loss = v_loss + (llm_loss if labels is not None else 0)
84
+
85
+ all_losses = None
86
+ if output_all_losses:
87
+ all_losses = {'llm_loss': llm_loss, 'v_loss': v_loss}
88
+
89
+ return VerifierModelOutput(
90
+ loss = loss,
91
+ v_scores = v_scores,
92
+ all_losses = all_losses,
93
+ )
94
+
95
+ def mse_loss_with_mask(self, scores: torch.FloatTensor, labels: torch.FloatTensor, IGNORE_INDEX: int = -100):
96
+ scores = torch.where(labels.ne(IGNORE_INDEX), scores, 0)
97
+ labels = torch.where(labels.ne(IGNORE_INDEX), labels, 0)
98
+ return F.mse_loss(scores, labels, reduction = 'sum') / scores.shape[0]
99
+
100
+
OVM/OVM/checkpoint/generator/README.md ADDED
@@ -0,0 +1,220 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: peft
3
+ base_model: mistralai/Mistral-7B-v0.1
4
+ ---
5
+
6
+ # Model Card for Model ID
7
+
8
+ <!-- Provide a quick summary of what the model is/does. -->
9
+
10
+
11
+
12
+ ## Model Details
13
+
14
+ ### Model Description
15
+
16
+ <!-- Provide a longer summary of what this model is. -->
17
+
18
+
19
+
20
+ - **Developed by:** [More Information Needed]
21
+ - **Funded by [optional]:** [More Information Needed]
22
+ - **Shared by [optional]:** [More Information Needed]
23
+ - **Model type:** [More Information Needed]
24
+ - **Language(s) (NLP):** [More Information Needed]
25
+ - **License:** [More Information Needed]
26
+ - **Finetuned from model [optional]:** [More Information Needed]
27
+
28
+ ### Model Sources [optional]
29
+
30
+ <!-- Provide the basic links for the model. -->
31
+
32
+ - **Repository:** [More Information Needed]
33
+ - **Paper [optional]:** [More Information Needed]
34
+ - **Demo [optional]:** [More Information Needed]
35
+
36
+ ## Uses
37
+
38
+ <!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
39
+
40
+ ### Direct Use
41
+
42
+ <!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
43
+
44
+ [More Information Needed]
45
+
46
+ ### Downstream Use [optional]
47
+
48
+ <!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
49
+
50
+ [More Information Needed]
51
+
52
+ ### Out-of-Scope Use
53
+
54
+ <!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
55
+
56
+ [More Information Needed]
57
+
58
+ ## Bias, Risks, and Limitations
59
+
60
+ <!-- This section is meant to convey both technical and sociotechnical limitations. -->
61
+
62
+ [More Information Needed]
63
+
64
+ ### Recommendations
65
+
66
+ <!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
67
+
68
+ Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
69
+
70
+ ## How to Get Started with the Model
71
+
72
+ Use the code below to get started with the model.
73
+
74
+ [More Information Needed]
75
+
76
+ ## Training Details
77
+
78
+ ### Training Data
79
+
80
+ <!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
81
+
82
+ [More Information Needed]
83
+
84
+ ### Training Procedure
85
+
86
+ <!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
87
+
88
+ #### Preprocessing [optional]
89
+
90
+ [More Information Needed]
91
+
92
+
93
+ #### Training Hyperparameters
94
+
95
+ - **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
96
+
97
+ #### Speeds, Sizes, Times [optional]
98
+
99
+ <!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
100
+
101
+ [More Information Needed]
102
+
103
+ ## Evaluation
104
+
105
+ <!-- This section describes the evaluation protocols and provides the results. -->
106
+
107
+ ### Testing Data, Factors & Metrics
108
+
109
+ #### Testing Data
110
+
111
+ <!-- This should link to a Dataset Card if possible. -->
112
+
113
+ [More Information Needed]
114
+
115
+ #### Factors
116
+
117
+ <!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
118
+
119
+ [More Information Needed]
120
+
121
+ #### Metrics
122
+
123
+ <!-- These are the evaluation metrics being used, ideally with a description of why. -->
124
+
125
+ [More Information Needed]
126
+
127
+ ### Results
128
+
129
+ [More Information Needed]
130
+
131
+ #### Summary
132
+
133
+
134
+
135
+ ## Model Examination [optional]
136
+
137
+ <!-- Relevant interpretability work for the model goes here -->
138
+
139
+ [More Information Needed]
140
+
141
+ ## Environmental Impact
142
+
143
+ <!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
144
+
145
+ Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
146
+
147
+ - **Hardware Type:** [More Information Needed]
148
+ - **Hours used:** [More Information Needed]
149
+ - **Cloud Provider:** [More Information Needed]
150
+ - **Compute Region:** [More Information Needed]
151
+ - **Carbon Emitted:** [More Information Needed]
152
+
153
+ ## Technical Specifications [optional]
154
+
155
+ ### Model Architecture and Objective
156
+
157
+ [More Information Needed]
158
+
159
+ ### Compute Infrastructure
160
+
161
+ [More Information Needed]
162
+
163
+ #### Hardware
164
+
165
+ [More Information Needed]
166
+
167
+ #### Software
168
+
169
+ [More Information Needed]
170
+
171
+ ## Citation [optional]
172
+
173
+ <!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
174
+
175
+ **BibTeX:**
176
+
177
+ [More Information Needed]
178
+
179
+ **APA:**
180
+
181
+ [More Information Needed]
182
+
183
+ ## Glossary [optional]
184
+
185
+ <!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
186
+
187
+ [More Information Needed]
188
+
189
+ ## More Information [optional]
190
+
191
+ [More Information Needed]
192
+
193
+ ## Model Card Authors [optional]
194
+
195
+ [More Information Needed]
196
+
197
+ ## Model Card Contact
198
+
199
+ [More Information Needed]
200
+
201
+
202
+ ## Training procedure
203
+
204
+
205
+ The following `bitsandbytes` quantization config was used during training:
206
+ - quant_method: bitsandbytes
207
+ - load_in_8bit: False
208
+ - load_in_4bit: True
209
+ - llm_int8_threshold: 6.0
210
+ - llm_int8_skip_modules: None
211
+ - llm_int8_enable_fp32_cpu_offload: False
212
+ - llm_int8_has_fp16_weight: False
213
+ - bnb_4bit_quant_type: nf4
214
+ - bnb_4bit_use_double_quant: False
215
+ - bnb_4bit_compute_dtype: bfloat16
216
+
217
+ ### Framework versions
218
+
219
+
220
+ - PEFT 0.6.2
OVM/OVM/checkpoint/generator/adapter_config.json ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "alpha_pattern": {},
3
+ "auto_mapping": null,
4
+ "base_model_name_or_path": "mistralai/Mistral-7B-v0.1",
5
+ "bias": "none",
6
+ "fan_in_fan_out": false,
7
+ "inference_mode": true,
8
+ "init_lora_weights": true,
9
+ "layers_pattern": null,
10
+ "layers_to_transform": null,
11
+ "lora_alpha": 16,
12
+ "lora_dropout": 0.1,
13
+ "modules_to_save": null,
14
+ "peft_type": "LORA",
15
+ "r": 64,
16
+ "rank_pattern": {},
17
+ "revision": null,
18
+ "target_modules": [
19
+ "gate_proj",
20
+ "up_proj",
21
+ "down_proj",
22
+ "v_proj",
23
+ "q_proj",
24
+ "k_proj",
25
+ "o_proj",
26
+ "lm_head"
27
+ ],
28
+ "task_type": "CAUSAL_LM"
29
+ }
OVM/OVM/checkpoint/generator/adapter_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:53cf21d240f5ff6dbce9f68fdf039ccf390f6021457cb2a2ec257833b19a95a6
3
+ size 680491413
OVM/OVM/data/.ipynb_checkpoints/merge-checkpoint.ipynb ADDED
@@ -0,0 +1,192 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "c633fee7-6770-4959-8248-cf7213606f36",
7
+ "metadata": {
8
+ "tags": []
9
+ },
10
+ "outputs": [],
11
+ "source": [
12
+ "from glob import glob\n",
13
+ "from datasets import load_from_disk\n",
14
+ "from datasets import concatenate_datasets"
15
+ ]
16
+ },
17
+ {
18
+ "cell_type": "code",
19
+ "execution_count": 2,
20
+ "id": "c8fca22d-51de-4641-87a0-6492beb88988",
21
+ "metadata": {
22
+ "tags": []
23
+ },
24
+ "outputs": [],
25
+ "source": [
26
+ "dataset_names = glob(\"./*/\")"
27
+ ]
28
+ },
29
+ {
30
+ "cell_type": "code",
31
+ "execution_count": 3,
32
+ "id": "c31ce749-c27e-4398-bc66-7788428cd3de",
33
+ "metadata": {
34
+ "tags": []
35
+ },
36
+ "outputs": [],
37
+ "source": [
38
+ "dataset_list = [load_from_disk(dataset_name) for dataset_name in dataset_names]"
39
+ ]
40
+ },
41
+ {
42
+ "cell_type": "code",
43
+ "execution_count": 4,
44
+ "id": "b3784b1a-aff8-4bdd-8b5a-13216d70f56d",
45
+ "metadata": {
46
+ "tags": []
47
+ },
48
+ "outputs": [
49
+ {
50
+ "data": {
51
+ "text/plain": [
52
+ "2"
53
+ ]
54
+ },
55
+ "execution_count": 4,
56
+ "metadata": {},
57
+ "output_type": "execute_result"
58
+ }
59
+ ],
60
+ "source": [
61
+ "len(dataset_names)"
62
+ ]
63
+ },
64
+ {
65
+ "cell_type": "code",
66
+ "execution_count": 5,
67
+ "id": "bcf3f458-71be-4a07-a1c6-6d07fd683e41",
68
+ "metadata": {
69
+ "tags": []
70
+ },
71
+ "outputs": [],
72
+ "source": [
73
+ "dataset = concatenate_datasets(dataset_list)"
74
+ ]
75
+ },
76
+ {
77
+ "cell_type": "code",
78
+ "execution_count": 7,
79
+ "id": "9ccd8ca0-0f2d-4a80-8a8c-0bf0df42ffad",
80
+ "metadata": {
81
+ "tags": []
82
+ },
83
+ "outputs": [
84
+ {
85
+ "data": {
86
+ "text/plain": [
87
+ "Dataset({\n",
88
+ " features: ['question', 'answer', 'candidate', 'label'],\n",
89
+ " num_rows: 375000\n",
90
+ "})"
91
+ ]
92
+ },
93
+ "execution_count": 7,
94
+ "metadata": {},
95
+ "output_type": "execute_result"
96
+ }
97
+ ],
98
+ "source": [
99
+ "dataset"
100
+ ]
101
+ },
102
+ {
103
+ "cell_type": "code",
104
+ "execution_count": 6,
105
+ "id": "86c7b496-c5c3-49d2-a9fd-d5d2236424ef",
106
+ "metadata": {
107
+ "tags": []
108
+ },
109
+ "outputs": [
110
+ {
111
+ "data": {
112
+ "text/plain": [
113
+ "7500.0"
114
+ ]
115
+ },
116
+ "execution_count": 6,
117
+ "metadata": {},
118
+ "output_type": "execute_result"
119
+ }
120
+ ],
121
+ "source": [
122
+ "len(dataset)/50"
123
+ ]
124
+ },
125
+ {
126
+ "cell_type": "code",
127
+ "execution_count": 8,
128
+ "id": "b693ac3b-c626-4a9a-b006-17a470dc0ca3",
129
+ "metadata": {
130
+ "tags": []
131
+ },
132
+ "outputs": [
133
+ {
134
+ "data": {
135
+ "application/vnd.jupyter.widget-view+json": {
136
+ "model_id": "453243bb11274124b011818dcbc8f61e",
137
+ "version_major": 2,
138
+ "version_minor": 0
139
+ },
140
+ "text/plain": [
141
+ "Saving the dataset (0/1 shards): 0%| | 0/375000 [00:00<?, ? examples/s]"
142
+ ]
143
+ },
144
+ "metadata": {},
145
+ "output_type": "display_data"
146
+ }
147
+ ],
148
+ "source": [
149
+ "dataset.save_to_disk(\"gen_all_375000\")"
150
+ ]
151
+ },
152
+ {
153
+ "cell_type": "code",
154
+ "execution_count": 10,
155
+ "id": "8955eedc-86cb-42cb-a776-63823634cea1",
156
+ "metadata": {},
157
+ "outputs": [],
158
+ "source": [
159
+ "# dataset.push_to_hub(\"anhdungitvn/math_gen_ovm_7500_375000\", token=\"\")"
160
+ ]
161
+ },
162
+ {
163
+ "cell_type": "code",
164
+ "execution_count": null,
165
+ "id": "afc86804-3a31-4772-89bf-1d10d5f8b9e8",
166
+ "metadata": {},
167
+ "outputs": [],
168
+ "source": []
169
+ }
170
+ ],
171
+ "metadata": {
172
+ "kernelspec": {
173
+ "display_name": "lora_llama2",
174
+ "language": "python",
175
+ "name": "lora_llama2"
176
+ },
177
+ "language_info": {
178
+ "codemirror_mode": {
179
+ "name": "ipython",
180
+ "version": 3
181
+ },
182
+ "file_extension": ".py",
183
+ "mimetype": "text/x-python",
184
+ "name": "python",
185
+ "nbconvert_exporter": "python",
186
+ "pygments_lexer": "ipython3",
187
+ "version": "3.10.12"
188
+ }
189
+ },
190
+ "nbformat": 4,
191
+ "nbformat_minor": 5
192
+ }
OVM/OVM/data/gen1/data-00000-of-00001.arrow ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:512080e6220a5f95a33f41a32c18a19fd17db731cbc351c7e393faca25a60562
3
+ size 169615856
OVM/OVM/data/gen1/dataset_info.json ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "citation": "",
3
+ "description": "",
4
+ "features": {
5
+ "question": {
6
+ "dtype": "string",
7
+ "_type": "Value"
8
+ },
9
+ "answer": {
10
+ "dtype": "string",
11
+ "_type": "Value"
12
+ },
13
+ "candidate": {
14
+ "dtype": "string",
15
+ "_type": "Value"
16
+ },
17
+ "label": {
18
+ "dtype": "bool",
19
+ "_type": "Value"
20
+ }
21
+ },
22
+ "homepage": "",
23
+ "license": ""
24
+ }
OVM/OVM/data/gen1/state.json ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_data_files": [
3
+ {
4
+ "filename": "data-00000-of-00001.arrow"
5
+ }
6
+ ],
7
+ "_fingerprint": "c5cb50243754f927",
8
+ "_format_columns": [
9
+ "question",
10
+ "answer",
11
+ "candidate",
12
+ "label"
13
+ ],
14
+ "_format_kwargs": {},
15
+ "_format_type": null,
16
+ "_output_all_columns": false,
17
+ "_split": null
18
+ }
OVM/OVM/data/gen2/data-00000-of-00001.arrow ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a581ca9cc18b2202b85baedebf93a7a9c3358931f584abb4be6e6c116abf6017
3
+ size 171862696
OVM/OVM/data/gen2/dataset_info.json ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "citation": "",
3
+ "description": "",
4
+ "features": {
5
+ "question": {
6
+ "dtype": "string",
7
+ "_type": "Value"
8
+ },
9
+ "answer": {
10
+ "dtype": "string",
11
+ "_type": "Value"
12
+ },
13
+ "candidate": {
14
+ "dtype": "string",
15
+ "_type": "Value"
16
+ },
17
+ "label": {
18
+ "dtype": "bool",
19
+ "_type": "Value"
20
+ }
21
+ },
22
+ "homepage": "",
23
+ "license": ""
24
+ }
OVM/OVM/data/gen2/state.json ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_data_files": [
3
+ {
4
+ "filename": "data-00000-of-00001.arrow"
5
+ }
6
+ ],
7
+ "_fingerprint": "307618c3fdb99a47",
8
+ "_format_columns": null,
9
+ "_format_kwargs": {},
10
+ "_format_type": null,
11
+ "_output_all_columns": false,
12
+ "_split": null
13
+ }
OVM/OVM/data/gen_all_375000/data-00000-of-00001.arrow ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:96729c9f55738cad9ee92f1c26968759121fb749c0f93db468330c53c178a5a4
3
+ size 341467688
OVM/OVM/data/gen_all_375000/dataset_info.json ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "citation": "",
3
+ "description": "",
4
+ "features": {
5
+ "question": {
6
+ "dtype": "string",
7
+ "_type": "Value"
8
+ },
9
+ "answer": {
10
+ "dtype": "string",
11
+ "_type": "Value"
12
+ },
13
+ "candidate": {
14
+ "dtype": "string",
15
+ "_type": "Value"
16
+ },
17
+ "label": {
18
+ "dtype": "bool",
19
+ "_type": "Value"
20
+ }
21
+ },
22
+ "homepage": "",
23
+ "license": ""
24
+ }
OVM/OVM/data/gen_all_375000/state.json ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_data_files": [
3
+ {
4
+ "filename": "data-00000-of-00001.arrow"
5
+ }
6
+ ],
7
+ "_fingerprint": "393f29e5745cb559",
8
+ "_format_columns": [
9
+ "question",
10
+ "answer",
11
+ "candidate",
12
+ "label"
13
+ ],
14
+ "_format_kwargs": {},
15
+ "_format_type": null,
16
+ "_output_all_columns": false,
17
+ "_split": null
18
+ }
OVM/OVM/data/merge.ipynb ADDED
@@ -0,0 +1,257 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "c633fee7-6770-4959-8248-cf7213606f36",
7
+ "metadata": {
8
+ "tags": []
9
+ },
10
+ "outputs": [],
11
+ "source": [
12
+ "from glob import glob\n",
13
+ "from datasets import load_from_disk\n",
14
+ "from datasets import concatenate_datasets"
15
+ ]
16
+ },
17
+ {
18
+ "cell_type": "code",
19
+ "execution_count": 2,
20
+ "id": "c8fca22d-51de-4641-87a0-6492beb88988",
21
+ "metadata": {
22
+ "tags": []
23
+ },
24
+ "outputs": [],
25
+ "source": [
26
+ "dataset_names = glob(\"./*/\")"
27
+ ]
28
+ },
29
+ {
30
+ "cell_type": "code",
31
+ "execution_count": 3,
32
+ "id": "c31ce749-c27e-4398-bc66-7788428cd3de",
33
+ "metadata": {
34
+ "tags": []
35
+ },
36
+ "outputs": [],
37
+ "source": [
38
+ "dataset_list = [load_from_disk(dataset_name) for dataset_name in dataset_names]"
39
+ ]
40
+ },
41
+ {
42
+ "cell_type": "code",
43
+ "execution_count": 4,
44
+ "id": "b3784b1a-aff8-4bdd-8b5a-13216d70f56d",
45
+ "metadata": {
46
+ "tags": []
47
+ },
48
+ "outputs": [
49
+ {
50
+ "data": {
51
+ "text/plain": [
52
+ "2"
53
+ ]
54
+ },
55
+ "execution_count": 4,
56
+ "metadata": {},
57
+ "output_type": "execute_result"
58
+ }
59
+ ],
60
+ "source": [
61
+ "len(dataset_names)"
62
+ ]
63
+ },
64
+ {
65
+ "cell_type": "code",
66
+ "execution_count": 5,
67
+ "id": "bcf3f458-71be-4a07-a1c6-6d07fd683e41",
68
+ "metadata": {
69
+ "tags": []
70
+ },
71
+ "outputs": [],
72
+ "source": [
73
+ "dataset = concatenate_datasets(dataset_list)"
74
+ ]
75
+ },
76
+ {
77
+ "cell_type": "code",
78
+ "execution_count": 7,
79
+ "id": "9ccd8ca0-0f2d-4a80-8a8c-0bf0df42ffad",
80
+ "metadata": {
81
+ "tags": []
82
+ },
83
+ "outputs": [
84
+ {
85
+ "data": {
86
+ "text/plain": [
87
+ "Dataset({\n",
88
+ " features: ['question', 'answer', 'candidate', 'label'],\n",
89
+ " num_rows: 375000\n",
90
+ "})"
91
+ ]
92
+ },
93
+ "execution_count": 7,
94
+ "metadata": {},
95
+ "output_type": "execute_result"
96
+ }
97
+ ],
98
+ "source": [
99
+ "dataset"
100
+ ]
101
+ },
102
+ {
103
+ "cell_type": "code",
104
+ "execution_count": 6,
105
+ "id": "86c7b496-c5c3-49d2-a9fd-d5d2236424ef",
106
+ "metadata": {
107
+ "tags": []
108
+ },
109
+ "outputs": [
110
+ {
111
+ "data": {
112
+ "text/plain": [
113
+ "7500.0"
114
+ ]
115
+ },
116
+ "execution_count": 6,
117
+ "metadata": {},
118
+ "output_type": "execute_result"
119
+ }
120
+ ],
121
+ "source": [
122
+ "len(dataset)/50"
123
+ ]
124
+ },
125
+ {
126
+ "cell_type": "code",
127
+ "execution_count": 8,
128
+ "id": "b693ac3b-c626-4a9a-b006-17a470dc0ca3",
129
+ "metadata": {
130
+ "tags": []
131
+ },
132
+ "outputs": [
133
+ {
134
+ "data": {
135
+ "application/vnd.jupyter.widget-view+json": {
136
+ "model_id": "453243bb11274124b011818dcbc8f61e",
137
+ "version_major": 2,
138
+ "version_minor": 0
139
+ },
140
+ "text/plain": [
141
+ "Saving the dataset (0/1 shards): 0%| | 0/375000 [00:00<?, ? examples/s]"
142
+ ]
143
+ },
144
+ "metadata": {},
145
+ "output_type": "display_data"
146
+ }
147
+ ],
148
+ "source": [
149
+ "dataset.save_to_disk(\"gen_all_375000\")"
150
+ ]
151
+ },
152
+ {
153
+ "cell_type": "code",
154
+ "execution_count": 2,
155
+ "id": "8955eedc-86cb-42cb-a776-63823634cea1",
156
+ "metadata": {},
157
+ "outputs": [],
158
+ "source": [
159
+ "dataset = load_from_disk(\"gen_all_375000\")"
160
+ ]
161
+ },
162
+ {
163
+ "cell_type": "code",
164
+ "execution_count": 3,
165
+ "id": "9e7d3a69-7a9c-4206-b5e3-e437e4a8bd82",
166
+ "metadata": {
167
+ "tags": []
168
+ },
169
+ "outputs": [
170
+ {
171
+ "data": {
172
+ "text/plain": [
173
+ "Dataset({\n",
174
+ " features: ['question', 'answer', 'candidate', 'label'],\n",
175
+ " num_rows: 375000\n",
176
+ "})"
177
+ ]
178
+ },
179
+ "execution_count": 3,
180
+ "metadata": {},
181
+ "output_type": "execute_result"
182
+ }
183
+ ],
184
+ "source": [
185
+ "dataset"
186
+ ]
187
+ },
188
+ {
189
+ "cell_type": "code",
190
+ "execution_count": 4,
191
+ "id": "afc86804-3a31-4772-89bf-1d10d5f8b9e8",
192
+ "metadata": {},
193
+ "outputs": [
194
+ {
195
+ "data": {
196
+ "application/vnd.jupyter.widget-view+json": {
197
+ "model_id": "b951444583784176b097381066f45df0",
198
+ "version_major": 2,
199
+ "version_minor": 0
200
+ },
201
+ "text/plain": [
202
+ "Pushing dataset shards to the dataset hub: 0%| | 0/1 [00:00<?, ?it/s]"
203
+ ]
204
+ },
205
+ "metadata": {},
206
+ "output_type": "display_data"
207
+ },
208
+ {
209
+ "data": {
210
+ "application/vnd.jupyter.widget-view+json": {
211
+ "model_id": "1cbaa93e51af4999bb27261a6aebbb85",
212
+ "version_major": 2,
213
+ "version_minor": 0
214
+ },
215
+ "text/plain": [
216
+ "Creating parquet from Arrow format: 0%| | 0/375 [00:00<?, ?ba/s]"
217
+ ]
218
+ },
219
+ "metadata": {},
220
+ "output_type": "display_data"
221
+ }
222
+ ],
223
+ "source": [
224
+ "dataset.push_to_hub(\"vi-ovm-dataset\", token=\"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\")"
225
+ ]
226
+ },
227
+ {
228
+ "cell_type": "code",
229
+ "execution_count": null,
230
+ "id": "84dca87a-1303-4928-88e7-10d3005b367f",
231
+ "metadata": {},
232
+ "outputs": [],
233
+ "source": []
234
+ }
235
+ ],
236
+ "metadata": {
237
+ "kernelspec": {
238
+ "display_name": "lora_llama2",
239
+ "language": "python",
240
+ "name": "lora_llama2"
241
+ },
242
+ "language_info": {
243
+ "codemirror_mode": {
244
+ "name": "ipython",
245
+ "version": 3
246
+ },
247
+ "file_extension": ".py",
248
+ "mimetype": "text/x-python",
249
+ "name": "python",
250
+ "nbconvert_exporter": "python",
251
+ "pygments_lexer": "ipython3",
252
+ "version": "3.10.12"
253
+ }
254
+ },
255
+ "nbformat": 4,
256
+ "nbformat_minor": 5
257
+ }
OVM/OVM/gen_solutions-1.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
3
+ os.environ["CUDA_VISIBLE_DEVICES"]="4,5,6,7"
4
+
5
+ from datasets import load_dataset, Dataset
6
+ import transformers
7
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
8
+ from transformers import DataCollatorForSeq2Seq, get_scheduler
9
+ import torch
10
+ from peft import LoraConfig, PeftConfig, get_peft_model, PeftModel
11
+ from torch.optim import AdamW
12
+ from tqdm.auto import tqdm
13
+ import os
14
+ from prompt_template import Prompter
15
+
16
+ dataset = load_dataset("longhoang06/Vi-GSM8K", split = "train")
17
+ gen_size = 3750
18
+ # gen_dataset = dataset.select(range(gen_size))
19
+ # gen_dataset = dataset.select(range(1000)) # part 1
20
+ gen_dataset = dataset.select(range(1000, 1500)) # part 2
21
+ # gen_dataset = dataset.select(range(1500, 2500)) # part 3
22
+ # gen_dataset = dataset.select(range(2500, 3750)) # part 4
23
+
24
+
25
+ model_path = "mistralai/Mistral-7B-v0.1"
26
+ peft_path = "checkpoint/generator"
27
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
28
+ tokenizer.pad_token = tokenizer.eos_token
29
+
30
+ model = AutoModelForCausalLM.from_pretrained(model_path, device_map = "auto", torch_dtype = torch.bfloat16)
31
+ model = PeftModel.from_pretrained(model, peft_path)
32
+ model = model.merge_and_unload()
33
+
34
+ def generator_and_save(dataset):
35
+ def get_answer(text):
36
+ return text.split("Đáp án:")[-1].strip()
37
+
38
+ prompter = Prompter()
39
+ progress_bar = tqdm(range(len(dataset)))
40
+ results = []
41
+
42
+ for d in dataset:
43
+ question = d["question"]
44
+ answer = d["answer"]
45
+ prompt = prompter.generate_prompt(instruction = question)
46
+
47
+ input_ids = tokenizer(prompt, return_tensors = "pt").input_ids
48
+ with torch.no_grad():
49
+ outputs = model.generate(
50
+ input_ids = input_ids.to("cuda"),
51
+ max_new_tokens = 512,
52
+ temperature = 0.7,
53
+ top_k = 50,
54
+ top_p = 1,
55
+ num_return_sequences = 50,
56
+ bos_token_id = tokenizer.bos_token_id,
57
+ eos_token_id = tokenizer.eos_token_id,
58
+ do_sample = True,
59
+ )
60
+ outputs.to("cpu")
61
+ texts = tokenizer.batch_decode(outputs, skip_special_tokens = True)
62
+ texts = [prompter.get_response(text) for text in texts]
63
+ batch = [
64
+ {"question": question,
65
+ "answer": answer,
66
+ "candidate": candidate,
67
+ "label": True if get_answer(candidate) == get_answer(answer) else False
68
+ } for candidate in texts
69
+ ]
70
+ results.extend(batch)
71
+ del batch
72
+ del input_ids
73
+ progress_bar.update(1)
74
+
75
+ new_dataset = Dataset.from_dict(
76
+ {"question": [d["question"] for d in results],
77
+ "answer": [d["answer"] for d in results],
78
+ "candidate": [d["candidate"] for d in results],
79
+ "label": [d["label"] for d in results],
80
+ }
81
+ )
82
+
83
+ # new_dataset.save_to_disk("data/gen1")
84
+ new_dataset.save_to_disk("data/gen1-1000-1500")
85
+
86
+ if __name__ == "__main__":
87
+ generator_and_save(gen_dataset)
88
+
89
+
90
+
OVM/OVM/gen_solutions-2.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
3
+ os.environ["CUDA_VISIBLE_DEVICES"]="2,3"
4
+
5
+ from datasets import load_dataset, Dataset
6
+ import transformers
7
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
8
+ from transformers import DataCollatorForSeq2Seq, get_scheduler
9
+ import torch
10
+ from peft import LoraConfig, PeftConfig, get_peft_model, PeftModel
11
+ from torch.optim import AdamW
12
+ from tqdm.auto import tqdm
13
+ import os
14
+ from prompt_template import Prompter
15
+
16
+ dataset = load_dataset("longhoang06/Vi-GSM8K", split = "train")
17
+ gen_size = 3750
18
+ gen_dataset = dataset.select(range(gen_size, 2*gen_size))
19
+
20
+ model_path = "mistralai/Mistral-7B-v0.1"
21
+ peft_path = "checkpoint/generator"
22
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
23
+ tokenizer.pad_token = tokenizer.eos_token
24
+
25
+ model = AutoModelForCausalLM.from_pretrained(model_path, device_map = "auto", torch_dtype = torch.bfloat16)
26
+ model = PeftModel.from_pretrained(model, peft_path)
27
+ model = model.merge_and_unload()
28
+
29
+ def generator_and_save(dataset):
30
+ def get_answer(text):
31
+ return text.split("Đáp án:")[-1].strip()
32
+
33
+ prompter = Prompter()
34
+ progress_bar = tqdm(range(len(dataset)))
35
+ results = []
36
+
37
+ for d in dataset:
38
+ question = d["question"]
39
+ answer = d["answer"]
40
+ prompt = prompter.generate_prompt(instruction = question)
41
+
42
+ input_ids = tokenizer(prompt, return_tensors = "pt").input_ids
43
+ with torch.no_grad():
44
+ outputs = model.generate(
45
+ input_ids = input_ids.to("cuda"),
46
+ max_new_tokens = 512,
47
+ temperature = 0.7,
48
+ top_k = 50,
49
+ top_p = 1,
50
+ num_return_sequences = 50,
51
+ bos_token_id = tokenizer.bos_token_id,
52
+ eos_token_id = tokenizer.eos_token_id,
53
+ do_sample = True,
54
+ )
55
+ texts = tokenizer.batch_decode(outputs, skip_special_tokens = True)
56
+ texts = [prompter.get_response(text) for text in texts]
57
+ batch = [
58
+ {"question": question,
59
+ "answer": answer,
60
+ "candidate": candidate,
61
+ "label": True if get_answer(candidate) == get_answer(answer) else False
62
+ } for candidate in texts
63
+ ]
64
+ results.extend(batch)
65
+ del batch
66
+ del input_ids
67
+ progress_bar.update(1)
68
+
69
+ new_dataset = Dataset.from_dict(
70
+ {"question": [d["question"] for d in results],
71
+ "answer": [d["answer"] for d in results],
72
+ "candidate": [d["candidate"] for d in results],
73
+ "label": [d["label"] for d in results],
74
+ }
75
+ )
76
+
77
+ new_dataset.save_to_disk("data/gen2")
78
+
79
+ if __name__ == "__main__":
80
+ generator_and_save(gen_dataset)
81
+
82
+
83
+
OVM/OVM/generatation/.ipynb_checkpoints/main-checkpoint.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
3
+ # os.environ["CUDA_VISIBLE_DEVICES"]="0,1,2,3"
4
+ os.environ["CUDA_VISIBLE_DEVICES"]="4,5,6,7"
5
+
6
+
7
+ # START = 3300 # runing
8
+ # START = 3310 # runing
9
+ START = 3320 # runing
10
+ # START = 3330 # runing
11
+ # START = 3340 # runing
12
+
13
+
14
+
15
+ WINDOW = 10
16
+
17
+
18
+ from datasets import load_dataset, Dataset
19
+ import transformers
20
+ transformers.logging.set_verbosity_error()
21
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
22
+ from transformers import DataCollatorForSeq2Seq, get_scheduler
23
+ import torch
24
+ from peft import LoraConfig, PeftConfig, get_peft_model, PeftModel
25
+ from torch.optim import AdamW
26
+ from tqdm.auto import tqdm
27
+ import os
28
+ from prompt_template import Prompter
29
+
30
+ dataset = load_dataset("longhoang06/Vi-GSM8K", split = "train")
31
+ # gen_size = 3750
32
+ # gen_dataset = dataset.select(range(gen_size))
33
+ # gen_dataset = dataset.select(range(1000)) # part 1
34
+ # gen_dataset = dataset.select(range(1000, 1500)) # part 2
35
+ # gen_dataset = dataset.select(range(1500, 2500)) # part 3
36
+ # gen_dataset = dataset.select(range(2500, 3750)) # part 4
37
+
38
+ # REMAIN: (1000, 1500)
39
+ # REMAIN: (2500, 3750)
40
+
41
+
42
+ END = START+WINDOW
43
+ OUTPUT_DIR = f"output_dir/{START}_{END}"
44
+ if os.path.isdir(OUTPUT_DIR):
45
+ raise Exception("OUTPUT_DIR EXISTED")
46
+
47
+ gen_dataset = dataset.select(range(START, END))
48
+
49
+
50
+
51
+ model_path = "mistralai/Mistral-7B-v0.1"
52
+ peft_path = "./checkpoint/generator"
53
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
54
+ tokenizer.pad_token = tokenizer.eos_token
55
+
56
+ model = AutoModelForCausalLM.from_pretrained(model_path, device_map = "auto", torch_dtype = torch.bfloat16)
57
+ model = PeftModel.from_pretrained(model, peft_path)
58
+ model = model.merge_and_unload()
59
+
60
+ def generator_and_save(dataset):
61
+ def get_answer(text):
62
+ return text.split("Đáp án:")[-1].strip()
63
+
64
+ prompter = Prompter()
65
+ progress_bar = tqdm(range(len(dataset)))
66
+ progress_bar.set_description(f"{START}_{END}")
67
+ results = []
68
+
69
+ for d in dataset:
70
+ question = d["question"]
71
+ answer = d["answer"]
72
+ prompt = prompter.generate_prompt(instruction = question)
73
+
74
+ input_ids = tokenizer(prompt, return_tensors = "pt").input_ids
75
+ with torch.no_grad():
76
+ outputs = model.generate(
77
+ input_ids = input_ids.to("cuda"),
78
+ max_new_tokens = 512,
79
+ temperature = 0.7,
80
+ top_k = 50,
81
+ top_p = 1,
82
+ num_return_sequences = 50,
83
+ bos_token_id = tokenizer.bos_token_id,
84
+ eos_token_id = tokenizer.eos_token_id,
85
+ do_sample = True,
86
+ )
87
+ outputs.to("cpu")
88
+ texts = tokenizer.batch_decode(outputs, skip_special_tokens = True)
89
+ texts = [prompter.get_response(text) for text in texts]
90
+ batch = [
91
+ {"question": question,
92
+ "answer": answer,
93
+ "candidate": candidate,
94
+ "label": True if get_answer(candidate) == get_answer(answer) else False
95
+ } for candidate in texts
96
+ ]
97
+ results.extend(batch)
98
+ del batch
99
+ del input_ids
100
+ progress_bar.update(1)
101
+
102
+ new_dataset = Dataset.from_dict(
103
+ {"question": [d["question"] for d in results],
104
+ "answer": [d["answer"] for d in results],
105
+ "candidate": [d["candidate"] for d in results],
106
+ "label": [d["label"] for d in results],
107
+ }
108
+ )
109
+
110
+ # new_dataset.save_to_disk("data/gen1")
111
+ new_dataset.save_to_disk(OUTPUT_DIR)
112
+
113
+ if __name__ == "__main__":
114
+ generator_and_save(gen_dataset)
115
+
116
+
117
+
OVM/OVM/generatation/.ipynb_checkpoints/main_108-checkpoint.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
3
+ os.environ["CUDA_VISIBLE_DEVICES"]="2,3,4,5,6"
4
+
5
+ START = 3500 # runing
6
+ WINDOW = 100
7
+
8
+
9
+ from datasets import load_dataset, Dataset
10
+ import transformers
11
+ transformers.logging.set_verbosity_error()
12
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
13
+ from transformers import DataCollatorForSeq2Seq, get_scheduler
14
+ import torch
15
+ from peft import LoraConfig, PeftConfig, get_peft_model, PeftModel
16
+ from torch.optim import AdamW
17
+ from tqdm.auto import tqdm
18
+ import os
19
+ from prompt_template import Prompter
20
+
21
+ dataset = load_dataset("longhoang06/Vi-GSM8K", split = "train")
22
+ # gen_size = 3750
23
+ # gen_dataset = dataset.select(range(gen_size))
24
+ # gen_dataset = dataset.select(range(1000)) # part 1
25
+ # gen_dataset = dataset.select(range(1000, 1500)) # part 2
26
+ # gen_dataset = dataset.select(range(1500, 2500)) # part 3
27
+ # gen_dataset = dataset.select(range(2500, 3750)) # part 4
28
+
29
+ # REMAIN: (1000, 1500)
30
+ # REMAIN: (2500, 3750)
31
+
32
+
33
+ END = START+WINDOW
34
+ OUTPUT_DIR = f"output_dir/{START}_{END}"
35
+ if os.path.isdir(OUTPUT_DIR):
36
+ raise Exception("OUTPUT_DIR EXISTED")
37
+
38
+ gen_dataset = dataset.select(range(START, END))
39
+
40
+
41
+
42
+ model_path = "mistralai/Mistral-7B-v0.1"
43
+ peft_path = "./checkpoint/generator"
44
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
45
+ tokenizer.pad_token = tokenizer.eos_token
46
+
47
+ model = AutoModelForCausalLM.from_pretrained(model_path, device_map = "auto", torch_dtype = torch.bfloat16)
48
+ model = PeftModel.from_pretrained(model, peft_path)
49
+ model = model.merge_and_unload()
50
+
51
+ def generator_and_save(dataset):
52
+ def get_answer(text):
53
+ return text.split("Đáp án:")[-1].strip()
54
+
55
+ prompter = Prompter()
56
+ progress_bar = tqdm(range(len(dataset)))
57
+ progress_bar.set_description(f"{START}_{END}")
58
+ results = []
59
+
60
+ for d in dataset:
61
+ question = d["question"]
62
+ answer = d["answer"]
63
+ prompt = prompter.generate_prompt(instruction = question)
64
+
65
+ input_ids = tokenizer(prompt, return_tensors = "pt").input_ids
66
+ with torch.no_grad():
67
+ outputs = model.generate(
68
+ input_ids = input_ids.to("cuda"),
69
+ max_new_tokens = 512,
70
+ temperature = 0.7,
71
+ top_k = 50,
72
+ top_p = 1,
73
+ num_return_sequences = 50,
74
+ bos_token_id = tokenizer.bos_token_id,
75
+ eos_token_id = tokenizer.eos_token_id,
76
+ do_sample = True,
77
+ )
78
+ outputs.to("cpu")
79
+ texts = tokenizer.batch_decode(outputs, skip_special_tokens = True)
80
+ texts = [prompter.get_response(text) for text in texts]
81
+ batch = [
82
+ {"question": question,
83
+ "answer": answer,
84
+ "candidate": candidate,
85
+ "label": True if get_answer(candidate) == get_answer(answer) else False
86
+ } for candidate in texts
87
+ ]
88
+ results.extend(batch)
89
+ del batch
90
+ del input_ids
91
+ progress_bar.update(1)
92
+
93
+ new_dataset = Dataset.from_dict(
94
+ {"question": [d["question"] for d in results],
95
+ "answer": [d["answer"] for d in results],
96
+ "candidate": [d["candidate"] for d in results],
97
+ "label": [d["label"] for d in results],
98
+ }
99
+ )
100
+
101
+ # new_dataset.save_to_disk("data/gen1")
102
+ new_dataset.save_to_disk(OUTPUT_DIR)
103
+
104
+ if __name__ == "__main__":
105
+ generator_and_save(gen_dataset)
106
+
107
+
108
+
OVM/OVM/generatation/.ipynb_checkpoints/main_121-checkpoint.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
3
+ # os.environ["CUDA_VISIBLE_DEVICES"]="0,1,2,3"
4
+ os.environ["CUDA_VISIBLE_DEVICES"]="4,5,6,7"
5
+
6
+ # START = 3300 # runing
7
+ # START = 3310 # runing
8
+ # START = 3320 # runing
9
+ START = 3330 # runing
10
+ # START = 3340 # runing
11
+
12
+
13
+
14
+ WINDOW = 10
15
+
16
+
17
+ from datasets import load_dataset, Dataset
18
+ import transformers
19
+ transformers.logging.set_verbosity_error()
20
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
21
+ from transformers import DataCollatorForSeq2Seq, get_scheduler
22
+ import torch
23
+ from peft import LoraConfig, PeftConfig, get_peft_model, PeftModel
24
+ from torch.optim import AdamW
25
+ from tqdm.auto import tqdm
26
+ import os
27
+ from prompt_template import Prompter
28
+
29
+ dataset = load_dataset("longhoang06/Vi-GSM8K", split = "train")
30
+ # gen_size = 3750
31
+ # gen_dataset = dataset.select(range(gen_size))
32
+ # gen_dataset = dataset.select(range(1000)) # part 1
33
+ # gen_dataset = dataset.select(range(1000, 1500)) # part 2
34
+ # gen_dataset = dataset.select(range(1500, 2500)) # part 3
35
+ # gen_dataset = dataset.select(range(2500, 3750)) # part 4
36
+
37
+ # REMAIN: (1000, 1500)
38
+ # REMAIN: (2500, 3750)
39
+
40
+
41
+ END = START+WINDOW
42
+ OUTPUT_DIR = f"output_dir/{START}_{END}"
43
+ if os.path.isdir(OUTPUT_DIR):
44
+ raise Exception("OUTPUT_DIR EXISTED")
45
+
46
+ gen_dataset = dataset.select(range(START, END))
47
+
48
+
49
+
50
+ model_path = "mistralai/Mistral-7B-v0.1"
51
+ peft_path = "./checkpoint/generator"
52
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
53
+ tokenizer.pad_token = tokenizer.eos_token
54
+
55
+ model = AutoModelForCausalLM.from_pretrained(model_path, device_map = "auto", torch_dtype = torch.bfloat16)
56
+ model = PeftModel.from_pretrained(model, peft_path)
57
+ model = model.merge_and_unload()
58
+
59
+ def generator_and_save(dataset):
60
+ def get_answer(text):
61
+ return text.split("Đáp án:")[-1].strip()
62
+
63
+ prompter = Prompter()
64
+ progress_bar = tqdm(range(len(dataset)))
65
+ progress_bar.set_description(f"{START}_{END}")
66
+ results = []
67
+
68
+ for d in dataset:
69
+ question = d["question"]
70
+ answer = d["answer"]
71
+ prompt = prompter.generate_prompt(instruction = question)
72
+
73
+ input_ids = tokenizer(prompt, return_tensors = "pt").input_ids
74
+ with torch.no_grad():
75
+ outputs = model.generate(
76
+ input_ids = input_ids.to("cuda"),
77
+ max_new_tokens = 512,
78
+ temperature = 0.7,
79
+ top_k = 50,
80
+ top_p = 1,
81
+ num_return_sequences = 50,
82
+ bos_token_id = tokenizer.bos_token_id,
83
+ eos_token_id = tokenizer.eos_token_id,
84
+ do_sample = True,
85
+ )
86
+ outputs.to("cpu")
87
+ texts = tokenizer.batch_decode(outputs, skip_special_tokens = True)
88
+ texts = [prompter.get_response(text) for text in texts]
89
+ batch = [
90
+ {"question": question,
91
+ "answer": answer,
92
+ "candidate": candidate,
93
+ "label": True if get_answer(candidate) == get_answer(answer) else False
94
+ } for candidate in texts
95
+ ]
96
+ results.extend(batch)
97
+ del batch
98
+ del input_ids
99
+ progress_bar.update(1)
100
+
101
+ new_dataset = Dataset.from_dict(
102
+ {"question": [d["question"] for d in results],
103
+ "answer": [d["answer"] for d in results],
104
+ "candidate": [d["candidate"] for d in results],
105
+ "label": [d["label"] for d in results],
106
+ }
107
+ )
108
+
109
+ # new_dataset.save_to_disk("data/gen1")
110
+ new_dataset.save_to_disk(OUTPUT_DIR)
111
+
112
+ if __name__ == "__main__":
113
+ generator_and_save(gen_dataset)
114
+
115
+
116
+
OVM/OVM/generatation/__pycache__/prompt_template.cpython-310.pyc ADDED
Binary file (1.25 kB). View file
 
OVM/OVM/generatation/__pycache__/prompt_template.cpython-311.pyc ADDED
Binary file (1.69 kB). View file
 
OVM/OVM/generatation/__pycache__/prompt_template.cpython-38.pyc ADDED
Binary file (1.25 kB). View file
 
OVM/OVM/generatation/checkpoint/generator/README.md ADDED
@@ -0,0 +1,220 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: peft
3
+ base_model: mistralai/Mistral-7B-v0.1
4
+ ---
5
+
6
+ # Model Card for Model ID
7
+
8
+ <!-- Provide a quick summary of what the model is/does. -->
9
+
10
+
11
+
12
+ ## Model Details
13
+
14
+ ### Model Description
15
+
16
+ <!-- Provide a longer summary of what this model is. -->
17
+
18
+
19
+
20
+ - **Developed by:** [More Information Needed]
21
+ - **Funded by [optional]:** [More Information Needed]
22
+ - **Shared by [optional]:** [More Information Needed]
23
+ - **Model type:** [More Information Needed]
24
+ - **Language(s) (NLP):** [More Information Needed]
25
+ - **License:** [More Information Needed]
26
+ - **Finetuned from model [optional]:** [More Information Needed]
27
+
28
+ ### Model Sources [optional]
29
+
30
+ <!-- Provide the basic links for the model. -->
31
+
32
+ - **Repository:** [More Information Needed]
33
+ - **Paper [optional]:** [More Information Needed]
34
+ - **Demo [optional]:** [More Information Needed]
35
+
36
+ ## Uses
37
+
38
+ <!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
39
+
40
+ ### Direct Use
41
+
42
+ <!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
43
+
44
+ [More Information Needed]
45
+
46
+ ### Downstream Use [optional]
47
+
48
+ <!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
49
+
50
+ [More Information Needed]
51
+
52
+ ### Out-of-Scope Use
53
+
54
+ <!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
55
+
56
+ [More Information Needed]
57
+
58
+ ## Bias, Risks, and Limitations
59
+
60
+ <!-- This section is meant to convey both technical and sociotechnical limitations. -->
61
+
62
+ [More Information Needed]
63
+
64
+ ### Recommendations
65
+
66
+ <!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
67
+
68
+ Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
69
+
70
+ ## How to Get Started with the Model
71
+
72
+ Use the code below to get started with the model.
73
+
74
+ [More Information Needed]
75
+
76
+ ## Training Details
77
+
78
+ ### Training Data
79
+
80
+ <!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
81
+
82
+ [More Information Needed]
83
+
84
+ ### Training Procedure
85
+
86
+ <!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
87
+
88
+ #### Preprocessing [optional]
89
+
90
+ [More Information Needed]
91
+
92
+
93
+ #### Training Hyperparameters
94
+
95
+ - **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
96
+
97
+ #### Speeds, Sizes, Times [optional]
98
+
99
+ <!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
100
+
101
+ [More Information Needed]
102
+
103
+ ## Evaluation
104
+
105
+ <!-- This section describes the evaluation protocols and provides the results. -->
106
+
107
+ ### Testing Data, Factors & Metrics
108
+
109
+ #### Testing Data
110
+
111
+ <!-- This should link to a Dataset Card if possible. -->
112
+
113
+ [More Information Needed]
114
+
115
+ #### Factors
116
+
117
+ <!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
118
+
119
+ [More Information Needed]
120
+
121
+ #### Metrics
122
+
123
+ <!-- These are the evaluation metrics being used, ideally with a description of why. -->
124
+
125
+ [More Information Needed]
126
+
127
+ ### Results
128
+
129
+ [More Information Needed]
130
+
131
+ #### Summary
132
+
133
+
134
+
135
+ ## Model Examination [optional]
136
+
137
+ <!-- Relevant interpretability work for the model goes here -->
138
+
139
+ [More Information Needed]
140
+
141
+ ## Environmental Impact
142
+
143
+ <!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
144
+
145
+ Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
146
+
147
+ - **Hardware Type:** [More Information Needed]
148
+ - **Hours used:** [More Information Needed]
149
+ - **Cloud Provider:** [More Information Needed]
150
+ - **Compute Region:** [More Information Needed]
151
+ - **Carbon Emitted:** [More Information Needed]
152
+
153
+ ## Technical Specifications [optional]
154
+
155
+ ### Model Architecture and Objective
156
+
157
+ [More Information Needed]
158
+
159
+ ### Compute Infrastructure
160
+
161
+ [More Information Needed]
162
+
163
+ #### Hardware
164
+
165
+ [More Information Needed]
166
+
167
+ #### Software
168
+
169
+ [More Information Needed]
170
+
171
+ ## Citation [optional]
172
+
173
+ <!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
174
+
175
+ **BibTeX:**
176
+
177
+ [More Information Needed]
178
+
179
+ **APA:**
180
+
181
+ [More Information Needed]
182
+
183
+ ## Glossary [optional]
184
+
185
+ <!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
186
+
187
+ [More Information Needed]
188
+
189
+ ## More Information [optional]
190
+
191
+ [More Information Needed]
192
+
193
+ ## Model Card Authors [optional]
194
+
195
+ [More Information Needed]
196
+
197
+ ## Model Card Contact
198
+
199
+ [More Information Needed]
200
+
201
+
202
+ ## Training procedure
203
+
204
+
205
+ The following `bitsandbytes` quantization config was used during training:
206
+ - quant_method: bitsandbytes
207
+ - load_in_8bit: False
208
+ - load_in_4bit: True
209
+ - llm_int8_threshold: 6.0
210
+ - llm_int8_skip_modules: None
211
+ - llm_int8_enable_fp32_cpu_offload: False
212
+ - llm_int8_has_fp16_weight: False
213
+ - bnb_4bit_quant_type: nf4
214
+ - bnb_4bit_use_double_quant: False
215
+ - bnb_4bit_compute_dtype: bfloat16
216
+
217
+ ### Framework versions
218
+
219
+
220
+ - PEFT 0.6.2
OVM/OVM/generatation/checkpoint/generator/adapter_config.json ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "alpha_pattern": {},
3
+ "auto_mapping": null,
4
+ "base_model_name_or_path": "mistralai/Mistral-7B-v0.1",
5
+ "bias": "none",
6
+ "fan_in_fan_out": false,
7
+ "inference_mode": true,
8
+ "init_lora_weights": true,
9
+ "layers_pattern": null,
10
+ "layers_to_transform": null,
11
+ "lora_alpha": 16,
12
+ "lora_dropout": 0.1,
13
+ "modules_to_save": null,
14
+ "peft_type": "LORA",
15
+ "r": 64,
16
+ "rank_pattern": {},
17
+ "revision": null,
18
+ "target_modules": [
19
+ "gate_proj",
20
+ "up_proj",
21
+ "down_proj",
22
+ "v_proj",
23
+ "q_proj",
24
+ "k_proj",
25
+ "o_proj",
26
+ "lm_head"
27
+ ],
28
+ "task_type": "CAUSAL_LM"
29
+ }
OVM/OVM/generatation/checkpoint/generator/adapter_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:53cf21d240f5ff6dbce9f68fdf039ccf390f6021457cb2a2ec257833b19a95a6
3
+ size 680491413
OVM/OVM/generatation/main.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
3
+ # os.environ["CUDA_VISIBLE_DEVICES"]="0,1,2,3"
4
+ os.environ["CUDA_VISIBLE_DEVICES"]="4,5,6,7"
5
+
6
+
7
+ # START = 3300 # runing
8
+ # START = 3310 # runing
9
+ START = 3320 # runing
10
+ # START = 3330 # runing
11
+ # START = 3340 # runing
12
+
13
+
14
+
15
+ WINDOW = 10
16
+
17
+
18
+ from datasets import load_dataset, Dataset
19
+ import transformers
20
+ transformers.logging.set_verbosity_error()
21
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
22
+ from transformers import DataCollatorForSeq2Seq, get_scheduler
23
+ import torch
24
+ from peft import LoraConfig, PeftConfig, get_peft_model, PeftModel
25
+ from torch.optim import AdamW
26
+ from tqdm.auto import tqdm
27
+ import os
28
+ from prompt_template import Prompter
29
+
30
+ dataset = load_dataset("longhoang06/Vi-GSM8K", split = "train")
31
+ # gen_size = 3750
32
+ # gen_dataset = dataset.select(range(gen_size))
33
+ # gen_dataset = dataset.select(range(1000)) # part 1
34
+ # gen_dataset = dataset.select(range(1000, 1500)) # part 2
35
+ # gen_dataset = dataset.select(range(1500, 2500)) # part 3
36
+ # gen_dataset = dataset.select(range(2500, 3750)) # part 4
37
+
38
+ # REMAIN: (1000, 1500)
39
+ # REMAIN: (2500, 3750)
40
+
41
+
42
+ END = START+WINDOW
43
+ OUTPUT_DIR = f"output_dir/{START}_{END}"
44
+ if os.path.isdir(OUTPUT_DIR):
45
+ raise Exception("OUTPUT_DIR EXISTED")
46
+
47
+ gen_dataset = dataset.select(range(START, END))
48
+
49
+
50
+
51
+ model_path = "mistralai/Mistral-7B-v0.1"
52
+ peft_path = "./checkpoint/generator"
53
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
54
+ tokenizer.pad_token = tokenizer.eos_token
55
+
56
+ model = AutoModelForCausalLM.from_pretrained(model_path, device_map = "auto", torch_dtype = torch.bfloat16)
57
+ model = PeftModel.from_pretrained(model, peft_path)
58
+ model = model.merge_and_unload()
59
+
60
+ def generator_and_save(dataset):
61
+ def get_answer(text):
62
+ return text.split("Đáp án:")[-1].strip()
63
+
64
+ prompter = Prompter()
65
+ progress_bar = tqdm(range(len(dataset)))
66
+ progress_bar.set_description(f"{START}_{END}")
67
+ results = []
68
+
69
+ for d in dataset:
70
+ question = d["question"]
71
+ answer = d["answer"]
72
+ prompt = prompter.generate_prompt(instruction = question)
73
+
74
+ input_ids = tokenizer(prompt, return_tensors = "pt").input_ids
75
+ with torch.no_grad():
76
+ outputs = model.generate(
77
+ input_ids = input_ids.to("cuda"),
78
+ max_new_tokens = 512,
79
+ temperature = 0.7,
80
+ top_k = 50,
81
+ top_p = 1,
82
+ num_return_sequences = 50,
83
+ bos_token_id = tokenizer.bos_token_id,
84
+ eos_token_id = tokenizer.eos_token_id,
85
+ do_sample = True,
86
+ )
87
+ outputs.to("cpu")
88
+ texts = tokenizer.batch_decode(outputs, skip_special_tokens = True)
89
+ texts = [prompter.get_response(text) for text in texts]
90
+ batch = [
91
+ {"question": question,
92
+ "answer": answer,
93
+ "candidate": candidate,
94
+ "label": True if get_answer(candidate) == get_answer(answer) else False
95
+ } for candidate in texts
96
+ ]
97
+ results.extend(batch)
98
+ del batch
99
+ del input_ids
100
+ progress_bar.update(1)
101
+
102
+ new_dataset = Dataset.from_dict(
103
+ {"question": [d["question"] for d in results],
104
+ "answer": [d["answer"] for d in results],
105
+ "candidate": [d["candidate"] for d in results],
106
+ "label": [d["label"] for d in results],
107
+ }
108
+ )
109
+
110
+ # new_dataset.save_to_disk("data/gen1")
111
+ new_dataset.save_to_disk(OUTPUT_DIR)
112
+
113
+ if __name__ == "__main__":
114
+ generator_and_save(gen_dataset)
115
+
116
+
117
+
OVM/OVM/generatation/main_108.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
3
+ os.environ["CUDA_VISIBLE_DEVICES"]="2,3,4,5,6"
4
+
5
+ START = 3500 # runing
6
+ WINDOW = 100
7
+
8
+
9
+ from datasets import load_dataset, Dataset
10
+ import transformers
11
+ transformers.logging.set_verbosity_error()
12
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
13
+ from transformers import DataCollatorForSeq2Seq, get_scheduler
14
+ import torch
15
+ from peft import LoraConfig, PeftConfig, get_peft_model, PeftModel
16
+ from torch.optim import AdamW
17
+ from tqdm.auto import tqdm
18
+ import os
19
+ from prompt_template import Prompter
20
+
21
+ dataset = load_dataset("longhoang06/Vi-GSM8K", split = "train")
22
+ # gen_size = 3750
23
+ # gen_dataset = dataset.select(range(gen_size))
24
+ # gen_dataset = dataset.select(range(1000)) # part 1
25
+ # gen_dataset = dataset.select(range(1000, 1500)) # part 2
26
+ # gen_dataset = dataset.select(range(1500, 2500)) # part 3
27
+ # gen_dataset = dataset.select(range(2500, 3750)) # part 4
28
+
29
+ # REMAIN: (1000, 1500)
30
+ # REMAIN: (2500, 3750)
31
+
32
+
33
+ END = START+WINDOW
34
+ OUTPUT_DIR = f"output_dir/{START}_{END}"
35
+ if os.path.isdir(OUTPUT_DIR):
36
+ raise Exception("OUTPUT_DIR EXISTED")
37
+
38
+ gen_dataset = dataset.select(range(START, END))
39
+
40
+
41
+
42
+ model_path = "mistralai/Mistral-7B-v0.1"
43
+ peft_path = "./checkpoint/generator"
44
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
45
+ tokenizer.pad_token = tokenizer.eos_token
46
+
47
+ model = AutoModelForCausalLM.from_pretrained(model_path, device_map = "auto", torch_dtype = torch.bfloat16)
48
+ model = PeftModel.from_pretrained(model, peft_path)
49
+ model = model.merge_and_unload()
50
+
51
+ def generator_and_save(dataset):
52
+ def get_answer(text):
53
+ return text.split("Đáp án:")[-1].strip()
54
+
55
+ prompter = Prompter()
56
+ progress_bar = tqdm(range(len(dataset)))
57
+ progress_bar.set_description(f"{START}_{END}")
58
+ results = []
59
+
60
+ for d in dataset:
61
+ question = d["question"]
62
+ answer = d["answer"]
63
+ prompt = prompter.generate_prompt(instruction = question)
64
+
65
+ input_ids = tokenizer(prompt, return_tensors = "pt").input_ids
66
+ with torch.no_grad():
67
+ outputs = model.generate(
68
+ input_ids = input_ids.to("cuda"),
69
+ max_new_tokens = 512,
70
+ temperature = 0.7,
71
+ top_k = 50,
72
+ top_p = 1,
73
+ num_return_sequences = 50,
74
+ bos_token_id = tokenizer.bos_token_id,
75
+ eos_token_id = tokenizer.eos_token_id,
76
+ do_sample = True,
77
+ )
78
+ outputs.to("cpu")
79
+ texts = tokenizer.batch_decode(outputs, skip_special_tokens = True)
80
+ texts = [prompter.get_response(text) for text in texts]
81
+ batch = [
82
+ {"question": question,
83
+ "answer": answer,
84
+ "candidate": candidate,
85
+ "label": True if get_answer(candidate) == get_answer(answer) else False
86
+ } for candidate in texts
87
+ ]
88
+ results.extend(batch)
89
+ del batch
90
+ del input_ids
91
+ progress_bar.update(1)
92
+
93
+ new_dataset = Dataset.from_dict(
94
+ {"question": [d["question"] for d in results],
95
+ "answer": [d["answer"] for d in results],
96
+ "candidate": [d["candidate"] for d in results],
97
+ "label": [d["label"] for d in results],
98
+ }
99
+ )
100
+
101
+ # new_dataset.save_to_disk("data/gen1")
102
+ new_dataset.save_to_disk(OUTPUT_DIR)
103
+
104
+ if __name__ == "__main__":
105
+ generator_and_save(gen_dataset)
106
+
107
+
108
+
OVM/OVM/generatation/main_121.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
3
+ # os.environ["CUDA_VISIBLE_DEVICES"]="0,1,2,3"
4
+ os.environ["CUDA_VISIBLE_DEVICES"]="4,5,6,7"
5
+
6
+ # START = 3300 # runing
7
+ # START = 3310 # runing
8
+ # START = 3320 # runing
9
+ START = 3330 # runing
10
+ # START = 3340 # runing
11
+
12
+
13
+
14
+ WINDOW = 10
15
+
16
+
17
+ from datasets import load_dataset, Dataset
18
+ import transformers
19
+ transformers.logging.set_verbosity_error()
20
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
21
+ from transformers import DataCollatorForSeq2Seq, get_scheduler
22
+ import torch
23
+ from peft import LoraConfig, PeftConfig, get_peft_model, PeftModel
24
+ from torch.optim import AdamW
25
+ from tqdm.auto import tqdm
26
+ import os
27
+ from prompt_template import Prompter
28
+
29
+ dataset = load_dataset("longhoang06/Vi-GSM8K", split = "train")
30
+ # gen_size = 3750
31
+ # gen_dataset = dataset.select(range(gen_size))
32
+ # gen_dataset = dataset.select(range(1000)) # part 1
33
+ # gen_dataset = dataset.select(range(1000, 1500)) # part 2
34
+ # gen_dataset = dataset.select(range(1500, 2500)) # part 3
35
+ # gen_dataset = dataset.select(range(2500, 3750)) # part 4
36
+
37
+ # REMAIN: (1000, 1500)
38
+ # REMAIN: (2500, 3750)
39
+
40
+
41
+ END = START+WINDOW
42
+ OUTPUT_DIR = f"output_dir/{START}_{END}"
43
+ if os.path.isdir(OUTPUT_DIR):
44
+ raise Exception("OUTPUT_DIR EXISTED")
45
+
46
+ gen_dataset = dataset.select(range(START, END))
47
+
48
+
49
+
50
+ model_path = "mistralai/Mistral-7B-v0.1"
51
+ peft_path = "./checkpoint/generator"
52
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
53
+ tokenizer.pad_token = tokenizer.eos_token
54
+
55
+ model = AutoModelForCausalLM.from_pretrained(model_path, device_map = "auto", torch_dtype = torch.bfloat16)
56
+ model = PeftModel.from_pretrained(model, peft_path)
57
+ model = model.merge_and_unload()
58
+
59
+ def generator_and_save(dataset):
60
+ def get_answer(text):
61
+ return text.split("Đáp án:")[-1].strip()
62
+
63
+ prompter = Prompter()
64
+ progress_bar = tqdm(range(len(dataset)))
65
+ progress_bar.set_description(f"{START}_{END}")
66
+ results = []
67
+
68
+ for d in dataset:
69
+ question = d["question"]
70
+ answer = d["answer"]
71
+ prompt = prompter.generate_prompt(instruction = question)
72
+
73
+ input_ids = tokenizer(prompt, return_tensors = "pt").input_ids
74
+ with torch.no_grad():
75
+ outputs = model.generate(
76
+ input_ids = input_ids.to("cuda"),
77
+ max_new_tokens = 512,
78
+ temperature = 0.7,
79
+ top_k = 50,
80
+ top_p = 1,
81
+ num_return_sequences = 50,
82
+ bos_token_id = tokenizer.bos_token_id,
83
+ eos_token_id = tokenizer.eos_token_id,
84
+ do_sample = True,
85
+ )
86
+ outputs.to("cpu")
87
+ texts = tokenizer.batch_decode(outputs, skip_special_tokens = True)
88
+ texts = [prompter.get_response(text) for text in texts]
89
+ batch = [
90
+ {"question": question,
91
+ "answer": answer,
92
+ "candidate": candidate,
93
+ "label": True if get_answer(candidate) == get_answer(answer) else False
94
+ } for candidate in texts
95
+ ]
96
+ results.extend(batch)
97
+ del batch
98
+ del input_ids
99
+ progress_bar.update(1)
100
+
101
+ new_dataset = Dataset.from_dict(
102
+ {"question": [d["question"] for d in results],
103
+ "answer": [d["answer"] for d in results],
104
+ "candidate": [d["candidate"] for d in results],
105
+ "label": [d["label"] for d in results],
106
+ }
107
+ )
108
+
109
+ # new_dataset.save_to_disk("data/gen1")
110
+ new_dataset.save_to_disk(OUTPUT_DIR)
111
+
112
+ if __name__ == "__main__":
113
+ generator_and_save(gen_dataset)
114
+
115
+
116
+
OVM/OVM/generatation/output_dir/.ipynb_checkpoints/merge-checkpoint.ipynb ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "c633fee7-6770-4959-8248-cf7213606f36",
7
+ "metadata": {
8
+ "tags": []
9
+ },
10
+ "outputs": [],
11
+ "source": [
12
+ "from glob import glob\n",
13
+ "from datasets import load_from_disk\n",
14
+ "from datasets import concatenate_datasets"
15
+ ]
16
+ },
17
+ {
18
+ "cell_type": "code",
19
+ "execution_count": 2,
20
+ "id": "c8fca22d-51de-4641-87a0-6492beb88988",
21
+ "metadata": {
22
+ "tags": []
23
+ },
24
+ "outputs": [],
25
+ "source": [
26
+ "dataset_names = glob(\"./*/\")"
27
+ ]
28
+ },
29
+ {
30
+ "cell_type": "code",
31
+ "execution_count": 3,
32
+ "id": "c31ce749-c27e-4398-bc66-7788428cd3de",
33
+ "metadata": {
34
+ "tags": []
35
+ },
36
+ "outputs": [],
37
+ "source": [
38
+ "dataset_list = [load_from_disk(dataset_name) for dataset_name in dataset_names]"
39
+ ]
40
+ },
41
+ {
42
+ "cell_type": "code",
43
+ "execution_count": 4,
44
+ "id": "b3784b1a-aff8-4bdd-8b5a-13216d70f56d",
45
+ "metadata": {
46
+ "tags": []
47
+ },
48
+ "outputs": [
49
+ {
50
+ "data": {
51
+ "text/plain": [
52
+ "26"
53
+ ]
54
+ },
55
+ "execution_count": 4,
56
+ "metadata": {},
57
+ "output_type": "execute_result"
58
+ }
59
+ ],
60
+ "source": [
61
+ "len(dataset_names)"
62
+ ]
63
+ },
64
+ {
65
+ "cell_type": "code",
66
+ "execution_count": 5,
67
+ "id": "bcf3f458-71be-4a07-a1c6-6d07fd683e41",
68
+ "metadata": {
69
+ "tags": []
70
+ },
71
+ "outputs": [],
72
+ "source": [
73
+ "dataset = concatenate_datasets(dataset_list)"
74
+ ]
75
+ },
76
+ {
77
+ "cell_type": "code",
78
+ "execution_count": 6,
79
+ "id": "86c7b496-c5c3-49d2-a9fd-d5d2236424ef",
80
+ "metadata": {
81
+ "tags": []
82
+ },
83
+ "outputs": [
84
+ {
85
+ "data": {
86
+ "text/plain": [
87
+ "3750.0"
88
+ ]
89
+ },
90
+ "execution_count": 6,
91
+ "metadata": {},
92
+ "output_type": "execute_result"
93
+ }
94
+ ],
95
+ "source": [
96
+ "len(dataset)/50"
97
+ ]
98
+ },
99
+ {
100
+ "cell_type": "code",
101
+ "execution_count": 7,
102
+ "id": "b62b27bc-b7c1-4f66-9deb-24860ead220b",
103
+ "metadata": {
104
+ "tags": []
105
+ },
106
+ "outputs": [
107
+ {
108
+ "data": {
109
+ "text/plain": [
110
+ "1750"
111
+ ]
112
+ },
113
+ "execution_count": 7,
114
+ "metadata": {},
115
+ "output_type": "execute_result"
116
+ }
117
+ ],
118
+ "source": [
119
+ "3750-2000"
120
+ ]
121
+ },
122
+ {
123
+ "cell_type": "code",
124
+ "execution_count": 8,
125
+ "id": "b693ac3b-c626-4a9a-b006-17a470dc0ca3",
126
+ "metadata": {
127
+ "tags": []
128
+ },
129
+ "outputs": [
130
+ {
131
+ "data": {
132
+ "application/vnd.jupyter.widget-view+json": {
133
+ "model_id": "a07ee12da7574b9680dda2c7e2a27e5e",
134
+ "version_major": 2,
135
+ "version_minor": 0
136
+ },
137
+ "text/plain": [
138
+ "Saving the dataset (0/1 shards): 0%| | 0/187500 [00:00<?, ? examples/s]"
139
+ ]
140
+ },
141
+ "metadata": {},
142
+ "output_type": "display_data"
143
+ }
144
+ ],
145
+ "source": [
146
+ "dataset.save_to_disk(\"/home/jovyan/nas_comm/1_user/[email protected]/research/math/math-mistral/OVM/data/gen1\")"
147
+ ]
148
+ },
149
+ {
150
+ "cell_type": "code",
151
+ "execution_count": null,
152
+ "id": "8955eedc-86cb-42cb-a776-63823634cea1",
153
+ "metadata": {},
154
+ "outputs": [],
155
+ "source": []
156
+ }
157
+ ],
158
+ "metadata": {
159
+ "kernelspec": {
160
+ "display_name": "lora_llama2",
161
+ "language": "python",
162
+ "name": "lora_llama2"
163
+ },
164
+ "language_info": {
165
+ "codemirror_mode": {
166
+ "name": "ipython",
167
+ "version": 3
168
+ },
169
+ "file_extension": ".py",
170
+ "mimetype": "text/x-python",
171
+ "name": "python",
172
+ "nbconvert_exporter": "python",
173
+ "pygments_lexer": "ipython3",
174
+ "version": "3.10.12"
175
+ }
176
+ },
177
+ "nbformat": 4,
178
+ "nbformat_minor": 5
179
+ }
OVM/OVM/generatation/output_dir/1000_1100/data-00000-of-00001.arrow ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:421e72d9f485410815e24d051a9030d65993668104eb62bbe02a6711eb27222d
3
+ size 4156512
OVM/OVM/generatation/output_dir/1000_1100/dataset_info.json ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "citation": "",
3
+ "description": "",
4
+ "features": {
5
+ "question": {
6
+ "dtype": "string",
7
+ "_type": "Value"
8
+ },
9
+ "answer": {
10
+ "dtype": "string",
11
+ "_type": "Value"
12
+ },
13
+ "candidate": {
14
+ "dtype": "string",
15
+ "_type": "Value"
16
+ },
17
+ "label": {
18
+ "dtype": "bool",
19
+ "_type": "Value"
20
+ }
21
+ },
22
+ "homepage": "",
23
+ "license": ""
24
+ }
OVM/OVM/generatation/output_dir/1000_1100/state.json ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_data_files": [
3
+ {
4
+ "filename": "data-00000-of-00001.arrow"
5
+ }
6
+ ],
7
+ "_fingerprint": "51a575e0fbde2b43",
8
+ "_format_columns": null,
9
+ "_format_kwargs": {},
10
+ "_format_type": null,
11
+ "_output_all_columns": false,
12
+ "_split": null
13
+ }