Upload sft_phi3_2.ipynb with huggingface_hub
Browse files- sft_phi3_2.ipynb +675 -0
sft_phi3_2.ipynb
ADDED
@@ -0,0 +1,675 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 32,
|
6 |
+
"id": "578786b8-092a-4de8-9955-4e87da557639",
|
7 |
+
"metadata": {
|
8 |
+
"scrolled": true
|
9 |
+
},
|
10 |
+
"outputs": [
|
11 |
+
{
|
12 |
+
"name": "stdout",
|
13 |
+
"output_type": "stream",
|
14 |
+
"text": [
|
15 |
+
"Requirement already satisfied: peft in /opt/conda/lib/python3.10/site-packages (0.11.1)\n",
|
16 |
+
"Requirement already satisfied: numpy>=1.17 in /opt/conda/lib/python3.10/site-packages (from peft) (1.26.3)\n",
|
17 |
+
"Requirement already satisfied: packaging>=20.0 in /opt/conda/lib/python3.10/site-packages (from peft) (23.1)\n",
|
18 |
+
"Requirement already satisfied: psutil in /opt/conda/lib/python3.10/site-packages (from peft) (5.9.0)\n",
|
19 |
+
"Requirement already satisfied: pyyaml in /opt/conda/lib/python3.10/site-packages (from peft) (6.0.1)\n",
|
20 |
+
"Requirement already satisfied: torch>=1.13.0 in /opt/conda/lib/python3.10/site-packages (from peft) (2.2.0)\n",
|
21 |
+
"Requirement already satisfied: transformers in /opt/conda/lib/python3.10/site-packages (from peft) (4.42.3)\n",
|
22 |
+
"Requirement already satisfied: tqdm in /opt/conda/lib/python3.10/site-packages (from peft) (4.66.4)\n",
|
23 |
+
"Requirement already satisfied: accelerate>=0.21.0 in /opt/conda/lib/python3.10/site-packages (from peft) (0.32.0)\n",
|
24 |
+
"Requirement already satisfied: safetensors in /opt/conda/lib/python3.10/site-packages (from peft) (0.4.3)\n",
|
25 |
+
"Requirement already satisfied: huggingface-hub>=0.17.0 in /opt/conda/lib/python3.10/site-packages (from peft) (0.23.4)\n",
|
26 |
+
"Requirement already satisfied: filelock in /opt/conda/lib/python3.10/site-packages (from huggingface-hub>=0.17.0->peft) (3.13.1)\n",
|
27 |
+
"Requirement already satisfied: fsspec>=2023.5.0 in /opt/conda/lib/python3.10/site-packages (from huggingface-hub>=0.17.0->peft) (2023.12.2)\n",
|
28 |
+
"Requirement already satisfied: requests in /opt/conda/lib/python3.10/site-packages (from huggingface-hub>=0.17.0->peft) (2.32.3)\n",
|
29 |
+
"Requirement already satisfied: typing-extensions>=3.7.4.3 in /opt/conda/lib/python3.10/site-packages (from huggingface-hub>=0.17.0->peft) (4.9.0)\n",
|
30 |
+
"Requirement already satisfied: sympy in /opt/conda/lib/python3.10/site-packages (from torch>=1.13.0->peft) (1.12)\n",
|
31 |
+
"Requirement already satisfied: networkx in /opt/conda/lib/python3.10/site-packages (from torch>=1.13.0->peft) (3.1)\n",
|
32 |
+
"Requirement already satisfied: jinja2 in /opt/conda/lib/python3.10/site-packages (from torch>=1.13.0->peft) (3.1.2)\n",
|
33 |
+
"Requirement already satisfied: regex!=2019.12.17 in /opt/conda/lib/python3.10/site-packages (from transformers->peft) (2024.5.15)\n",
|
34 |
+
"Requirement already satisfied: tokenizers<0.20,>=0.19 in /opt/conda/lib/python3.10/site-packages (from transformers->peft) (0.19.1)\n",
|
35 |
+
"Requirement already satisfied: MarkupSafe>=2.0 in /opt/conda/lib/python3.10/site-packages (from jinja2->torch>=1.13.0->peft) (2.1.3)\n",
|
36 |
+
"Requirement already satisfied: charset-normalizer<4,>=2 in /opt/conda/lib/python3.10/site-packages (from requests->huggingface-hub>=0.17.0->peft) (2.0.4)\n",
|
37 |
+
"Requirement already satisfied: idna<4,>=2.5 in /opt/conda/lib/python3.10/site-packages (from requests->huggingface-hub>=0.17.0->peft) (3.4)\n",
|
38 |
+
"Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/conda/lib/python3.10/site-packages (from requests->huggingface-hub>=0.17.0->peft) (1.26.18)\n",
|
39 |
+
"Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.10/site-packages (from requests->huggingface-hub>=0.17.0->peft) (2023.11.17)\n",
|
40 |
+
"Requirement already satisfied: mpmath>=0.19 in /opt/conda/lib/python3.10/site-packages (from sympy->torch>=1.13.0->peft) (1.3.0)\n",
|
41 |
+
"\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
|
42 |
+
"\u001b[0m"
|
43 |
+
]
|
44 |
+
}
|
45 |
+
],
|
46 |
+
"source": [
|
47 |
+
"#!pip install huggingface_hub torch transformers datasets trl \n",
|
48 |
+
"#!pip install flash-attn --no-build-isolation\n",
|
49 |
+
"!pip install --upgrade peft"
|
50 |
+
]
|
51 |
+
},
|
52 |
+
{
|
53 |
+
"cell_type": "code",
|
54 |
+
"execution_count": 35,
|
55 |
+
"id": "4a74bec4-4bf0-47be-802a-046073da573e",
|
56 |
+
"metadata": {},
|
57 |
+
"outputs": [],
|
58 |
+
"source": [
|
59 |
+
"import sys\n",
|
60 |
+
"import logging\n",
|
61 |
+
"\n",
|
62 |
+
"import datasets\n",
|
63 |
+
"from datasets import load_dataset\n",
|
64 |
+
"from peft import LoraConfig\n",
|
65 |
+
"import torch\n",
|
66 |
+
"import torch.nn as nn\n",
|
67 |
+
"import transformers\n",
|
68 |
+
"from trl import SFTTrainer, SFTConfig\n",
|
69 |
+
"from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments, BitsAndBytesConfig"
|
70 |
+
]
|
71 |
+
},
|
72 |
+
{
|
73 |
+
"cell_type": "code",
|
74 |
+
"execution_count": 4,
|
75 |
+
"id": "8a9bc6f8-4a1e-42d8-897d-5225e1b5011a",
|
76 |
+
"metadata": {},
|
77 |
+
"outputs": [],
|
78 |
+
"source": [
|
79 |
+
"dataset_id = (\"wikitext\", \"wikitext-103-raw-v1\")\n",
|
80 |
+
"dataset_id = \"HuggingFaceH4/ultrachat_200k\"\n",
|
81 |
+
"\n",
|
82 |
+
"dataset = load_dataset(dataset_id)"
|
83 |
+
]
|
84 |
+
},
|
85 |
+
{
|
86 |
+
"cell_type": "code",
|
87 |
+
"execution_count": 5,
|
88 |
+
"id": "f3b226eb-b159-4533-bd33-2746181a80b3",
|
89 |
+
"metadata": {},
|
90 |
+
"outputs": [],
|
91 |
+
"source": [
|
92 |
+
"training_config = {\n",
|
93 |
+
" \"bf16\": True,\n",
|
94 |
+
" \"do_eval\": False,\n",
|
95 |
+
" \"do_train\": True, # defualts to False, not sure where this fits\n",
|
96 |
+
" \"learning_rate\": 5.0e-06,\n",
|
97 |
+
" \"log_level\": \"info\",\n",
|
98 |
+
" \"logging_steps\": 20,\n",
|
99 |
+
" \"logging_strategy\": \"steps\",\n",
|
100 |
+
" \"lr_scheduler_type\": \"cosine\",\n",
|
101 |
+
" \"num_train_epochs\": 1,\n",
|
102 |
+
" \"max_steps\": -1,\n",
|
103 |
+
" \"output_dir\": \"./checkpoint_dir\", # model predictions and checkpoints\n",
|
104 |
+
" \"overwrite_output_dir\": True,\n",
|
105 |
+
" \"per_device_eval_batch_size\": 4,\n",
|
106 |
+
" \"per_device_train_batch_size\": 4,\n",
|
107 |
+
" \"remove_unused_columns\": True,\n",
|
108 |
+
" \"save_steps\": 100,\n",
|
109 |
+
" \"save_total_limit\": 1,\n",
|
110 |
+
" \"seed\": 0,\n",
|
111 |
+
" \"gradient_checkpointing\": True,\n",
|
112 |
+
" \"gradient_checkpointing_kwargs\":{\"use_reentrant\": False},\n",
|
113 |
+
" \"gradient_accumulation_steps\": 1, # number of steps to accumulate before beckprop\n",
|
114 |
+
" \"warmup_ratio\": 0.2,\n",
|
115 |
+
" \"packing\": False,\n",
|
116 |
+
" \"max_seq_length\": 2048,\n",
|
117 |
+
" \"dataset_text_field\": \"text\",\n",
|
118 |
+
" }\n",
|
119 |
+
"\n",
|
120 |
+
"peft_config = {\n",
|
121 |
+
" \"r\": 16, # default values VV\n",
|
122 |
+
" \"lora_alpha\": 32,\n",
|
123 |
+
" \"lora_dropout\": 0.05,\n",
|
124 |
+
" \"bias\": \"none\",\n",
|
125 |
+
" \"task_type\": \"CAUSAL_LM\",\n",
|
126 |
+
" \"target_modules\": \"all-linear\",\n",
|
127 |
+
" \"modules_to_save\": None,\n",
|
128 |
+
"}\n",
|
129 |
+
"\n",
|
130 |
+
"train_conf = SFTConfig(**training_config)\n",
|
131 |
+
"#train_conf = TrainingArguments(**training_config)\n",
|
132 |
+
"peft_conf = LoraConfig(**peft_config)"
|
133 |
+
]
|
134 |
+
},
|
135 |
+
{
|
136 |
+
"cell_type": "code",
|
137 |
+
"execution_count": 6,
|
138 |
+
"id": "20c9d834-50fe-4495-b003-7d80495c8439",
|
139 |
+
"metadata": {
|
140 |
+
"scrolled": true
|
141 |
+
},
|
142 |
+
"outputs": [
|
143 |
+
{
|
144 |
+
"data": {
|
145 |
+
"application/vnd.jupyter.widget-view+json": {
|
146 |
+
"model_id": "35f0c3abec7c4220b2cbd89ecc0671c0",
|
147 |
+
"version_major": 2,
|
148 |
+
"version_minor": 0
|
149 |
+
},
|
150 |
+
"text/plain": [
|
151 |
+
"Loading checkpoint shards: 0%| | 0/2 [00:00<?, ?it/s]"
|
152 |
+
]
|
153 |
+
},
|
154 |
+
"metadata": {},
|
155 |
+
"output_type": "display_data"
|
156 |
+
},
|
157 |
+
{
|
158 |
+
"name": "stderr",
|
159 |
+
"output_type": "stream",
|
160 |
+
"text": [
|
161 |
+
"Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.\n"
|
162 |
+
]
|
163 |
+
}
|
164 |
+
],
|
165 |
+
"source": [
|
166 |
+
"checkpoint_path = \"microsoft/Phi-3-mini-128k-instruct\"\n",
|
167 |
+
"model_kwargs = dict(\n",
|
168 |
+
" use_cache=False,\n",
|
169 |
+
" trust_remote_code=True,\n",
|
170 |
+
" attn_implementation=\"flash_attention_2\", # loading the model with flash-attenstion support\n",
|
171 |
+
" torch_dtype=torch.bfloat16,\n",
|
172 |
+
" device_map=\"auto\"\n",
|
173 |
+
")\n",
|
174 |
+
"\n",
|
175 |
+
"model = AutoModelForCausalLM.from_pretrained(checkpoint_path, **model_kwargs)\n",
|
176 |
+
"tokenizer = AutoTokenizer.from_pretrained(checkpoint_path, truncation=True)"
|
177 |
+
]
|
178 |
+
},
|
179 |
+
{
|
180 |
+
"cell_type": "code",
|
181 |
+
"execution_count": 6,
|
182 |
+
"id": "d684252c-2151-4601-8ebb-398bd3a63f00",
|
183 |
+
"metadata": {},
|
184 |
+
"outputs": [],
|
185 |
+
"source": [
|
186 |
+
"tokenizer.model_max_length = 2048\n",
|
187 |
+
"#tokenizer.pad_token = tokenizer.unk_token # use unk rather than eos token to prevent endless generation\n",
|
188 |
+
"#tokenizer.pad_token_id = tokenizer.convert_tokens_to_ids(tokenizer.pad_token)\n",
|
189 |
+
"tokenizer.padding_side = 'right'"
|
190 |
+
]
|
191 |
+
},
|
192 |
+
{
|
193 |
+
"cell_type": "code",
|
194 |
+
"execution_count": 7,
|
195 |
+
"id": "75869100-99f7-49c7-a9d3-7a3950dd7d72",
|
196 |
+
"metadata": {
|
197 |
+
"scrolled": true
|
198 |
+
},
|
199 |
+
"outputs": [],
|
200 |
+
"source": [
|
201 |
+
"def preproc(examples, tokenizer):\n",
|
202 |
+
" messages = examples['messages']\n",
|
203 |
+
" examples['text'] = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=False) #, return_dict=True)\n",
|
204 |
+
" return examples\n",
|
205 |
+
"\n",
|
206 |
+
"train_dataset = dataset['train_sft']\n",
|
207 |
+
"test_dataset = dataset['test_sft']\n",
|
208 |
+
"\n",
|
209 |
+
"train_dataset = train_dataset.map(preproc,\n",
|
210 |
+
" fn_kwargs={'tokenizer':tokenizer},\n",
|
211 |
+
" num_proc=24,\n",
|
212 |
+
" #batched=True,\n",
|
213 |
+
" remove_columns=list(train_dataset.features)).select(range(1000))\n",
|
214 |
+
"\n",
|
215 |
+
"test_dataset = test_dataset.map(preproc,\n",
|
216 |
+
" fn_kwargs={'tokenizer':tokenizer},\n",
|
217 |
+
" num_proc=24,\n",
|
218 |
+
" #batched=True,\n",
|
219 |
+
" remove_columns=list(test_dataset.features))#[10000:]"
|
220 |
+
]
|
221 |
+
},
|
222 |
+
{
|
223 |
+
"cell_type": "code",
|
224 |
+
"execution_count": 8,
|
225 |
+
"id": "56cd1b31-6f7e-4c7d-8524-b12cf94b9c9f",
|
226 |
+
"metadata": {},
|
227 |
+
"outputs": [
|
228 |
+
{
|
229 |
+
"data": {
|
230 |
+
"application/vnd.jupyter.widget-view+json": {
|
231 |
+
"model_id": "5d79f04152484f9494e389b264fc7176",
|
232 |
+
"version_major": 2,
|
233 |
+
"version_minor": 0
|
234 |
+
},
|
235 |
+
"text/plain": [
|
236 |
+
"Map: 0%| | 0/1000 [00:00<?, ? examples/s]"
|
237 |
+
]
|
238 |
+
},
|
239 |
+
"metadata": {},
|
240 |
+
"output_type": "display_data"
|
241 |
+
},
|
242 |
+
{
|
243 |
+
"name": "stderr",
|
244 |
+
"output_type": "stream",
|
245 |
+
"text": [
|
246 |
+
"Using auto half precision backend\n"
|
247 |
+
]
|
248 |
+
}
|
249 |
+
],
|
250 |
+
"source": [
|
251 |
+
"trainer = SFTTrainer(\n",
|
252 |
+
" model=model,\n",
|
253 |
+
" args=train_conf,\n",
|
254 |
+
" peft_config=peft_conf,\n",
|
255 |
+
" train_dataset=train_dataset,\n",
|
256 |
+
" #eval_dataset=test_dataset,\n",
|
257 |
+
" # max_seq_length=tokenizer.model_max_length,\n",
|
258 |
+
" # dataset_text_field=\"text\",\n",
|
259 |
+
" tokenizer=tokenizer,\n",
|
260 |
+
" # packing=True\n",
|
261 |
+
")"
|
262 |
+
]
|
263 |
+
},
|
264 |
+
{
|
265 |
+
"cell_type": "code",
|
266 |
+
"execution_count": null,
|
267 |
+
"id": "d8e6b669-1717-429a-9c43-3c02adb8a3d1",
|
268 |
+
"metadata": {},
|
269 |
+
"outputs": [],
|
270 |
+
"source": [
|
271 |
+
"train_result = trainer.train()\n",
|
272 |
+
"metrics = train_result.metrics\n",
|
273 |
+
"trainer.save_state()"
|
274 |
+
]
|
275 |
+
},
|
276 |
+
{
|
277 |
+
"cell_type": "code",
|
278 |
+
"execution_count": 12,
|
279 |
+
"id": "4d4207fc-1578-4591-a480-467fd2a5855b",
|
280 |
+
"metadata": {},
|
281 |
+
"outputs": [
|
282 |
+
{
|
283 |
+
"data": {
|
284 |
+
"text/plain": [
|
285 |
+
"{'train_runtime': 506.2204,\n",
|
286 |
+
" 'train_samples_per_second': 1.975,\n",
|
287 |
+
" 'train_steps_per_second': 0.494,\n",
|
288 |
+
" 'total_flos': 4.041582948790272e+16,\n",
|
289 |
+
" 'train_loss': 1.1037534561157227,\n",
|
290 |
+
" 'epoch': 1.0}"
|
291 |
+
]
|
292 |
+
},
|
293 |
+
"execution_count": 12,
|
294 |
+
"metadata": {},
|
295 |
+
"output_type": "execute_result"
|
296 |
+
}
|
297 |
+
],
|
298 |
+
"source": [
|
299 |
+
"metrics"
|
300 |
+
]
|
301 |
+
},
|
302 |
+
{
|
303 |
+
"cell_type": "code",
|
304 |
+
"execution_count": 7,
|
305 |
+
"id": "f92339ec-0448-40d2-9458-6242e35b9bdc",
|
306 |
+
"metadata": {},
|
307 |
+
"outputs": [
|
308 |
+
{
|
309 |
+
"data": {
|
310 |
+
"application/vnd.jupyter.widget-view+json": {
|
311 |
+
"model_id": "575ddc622079468cb4ca994ed76dc2f4",
|
312 |
+
"version_major": 2,
|
313 |
+
"version_minor": 0
|
314 |
+
},
|
315 |
+
"text/plain": [
|
316 |
+
"Loading checkpoint shards: 0%| | 0/2 [00:00<?, ?it/s]"
|
317 |
+
]
|
318 |
+
},
|
319 |
+
"metadata": {},
|
320 |
+
"output_type": "display_data"
|
321 |
+
}
|
322 |
+
],
|
323 |
+
"source": [
|
324 |
+
"from peft import PeftConfig, PeftModel \n",
|
325 |
+
"\n",
|
326 |
+
"checkpoint_path = \"microsoft/Phi-3-mini-128k-instruct\"\n",
|
327 |
+
"adapter_path = \"./checkpoint_dir/checkpoint-250\"\n",
|
328 |
+
"\n",
|
329 |
+
"model_kwargs = dict(\n",
|
330 |
+
" use_cache=False,\n",
|
331 |
+
" trust_remote_code=True,\n",
|
332 |
+
" attn_implementation=\"flash_attention_2\", # loading the model with flash-attenstion support\n",
|
333 |
+
" torch_dtype=torch.bfloat16,\n",
|
334 |
+
" device_map=\"auto\"\n",
|
335 |
+
")\n",
|
336 |
+
"\n",
|
337 |
+
"model = AutoModelForCausalLM.from_pretrained(checkpoint_path, **model_kwargs)"
|
338 |
+
]
|
339 |
+
},
|
340 |
+
{
|
341 |
+
"cell_type": "code",
|
342 |
+
"execution_count": 8,
|
343 |
+
"id": "f0cf458d-8b4f-4ff9-bd60-bbe510416cea",
|
344 |
+
"metadata": {},
|
345 |
+
"outputs": [
|
346 |
+
{
|
347 |
+
"name": "stderr",
|
348 |
+
"output_type": "stream",
|
349 |
+
"text": [
|
350 |
+
"Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.\n"
|
351 |
+
]
|
352 |
+
}
|
353 |
+
],
|
354 |
+
"source": [
|
355 |
+
"model = PeftModel.from_pretrained(model, adapter_path)\n",
|
356 |
+
"tokenizer = AutoTokenizer.from_pretrained(checkpoint_path)"
|
357 |
+
]
|
358 |
+
},
|
359 |
+
{
|
360 |
+
"cell_type": "code",
|
361 |
+
"execution_count": 83,
|
362 |
+
"id": "8e0b4443-cc10-47ca-a476-055fd915c1b9",
|
363 |
+
"metadata": {},
|
364 |
+
"outputs": [],
|
365 |
+
"source": [
|
366 |
+
"cats = None\n",
|
367 |
+
"kittens = None\n",
|
368 |
+
"paris = None"
|
369 |
+
]
|
370 |
+
},
|
371 |
+
{
|
372 |
+
"cell_type": "code",
|
373 |
+
"execution_count": 88,
|
374 |
+
"id": "b5ada882-b7d2-46c5-ba5b-54fab2556832",
|
375 |
+
"metadata": {},
|
376 |
+
"outputs": [],
|
377 |
+
"source": [
|
378 |
+
"input_text = [\n",
|
379 |
+
" {'role': 'user', 'content': \"Cats are\"},\n",
|
380 |
+
" #{'role': 'system', 'content': \"I'm going \"}\n",
|
381 |
+
"]\n",
|
382 |
+
"\n",
|
383 |
+
"input = \"Tell me about paris\"\n",
|
384 |
+
"\n",
|
385 |
+
"input = tokenizer(input, return_tensors='pt')\n",
|
386 |
+
"\n",
|
387 |
+
"def generate_out(input, len=100):\n",
|
388 |
+
" return model.generate(\n",
|
389 |
+
" input['input_ids'].to('cuda'),\n",
|
390 |
+
" max_length=len, # Maximum length of the generated text\n",
|
391 |
+
" num_return_sequences=1, # Number of sequences to generate\n",
|
392 |
+
" )\n",
|
393 |
+
"output = generate_out(input)[0].to(torch.float32)\n",
|
394 |
+
"\n",
|
395 |
+
"paris = output\n",
|
396 |
+
"\n",
|
397 |
+
"#loss(output, output)\n",
|
398 |
+
"#input = tokenizer.apply_chat_template(input_text, tokenize=False, add_generation_prompt=False)\n"
|
399 |
+
]
|
400 |
+
},
|
401 |
+
{
|
402 |
+
"cell_type": "code",
|
403 |
+
"execution_count": 92,
|
404 |
+
"id": "8f39d54c-7ec8-4e1a-9db3-83157eae8f7d",
|
405 |
+
"metadata": {},
|
406 |
+
"outputs": [
|
407 |
+
{
|
408 |
+
"ename": "TypeError",
|
409 |
+
"evalue": "loop of ufunc does not support argument 0 of type builtin_function_or_method which has no callable exp method",
|
410 |
+
"output_type": "error",
|
411 |
+
"traceback": [
|
412 |
+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
413 |
+
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
|
414 |
+
"\u001b[0;31mAttributeError\u001b[0m: 'builtin_function_or_method' object has no attribute 'exp'",
|
415 |
+
"\nThe above exception was the direct cause of the following exception:\n",
|
416 |
+
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
|
417 |
+
"Cell \u001b[0;32mIn[92], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnumpy\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mnp\u001b[39;00m\n\u001b[0;32m----> 3\u001b[0m \u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexp\u001b[49m\u001b[43m(\u001b[49m\u001b[43mloss\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcats\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mparis\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mitem\u001b[49m\u001b[43m)\u001b[49m\n",
|
418 |
+
"\u001b[0;31mTypeError\u001b[0m: loop of ufunc does not support argument 0 of type builtin_function_or_method which has no callable exp method"
|
419 |
+
]
|
420 |
+
}
|
421 |
+
],
|
422 |
+
"source": [
|
423 |
+
"import numpy as np\n",
|
424 |
+
"\n",
|
425 |
+
"np.exp(loss(cats, paris).item)"
|
426 |
+
]
|
427 |
+
},
|
428 |
+
{
|
429 |
+
"cell_type": "code",
|
430 |
+
"execution_count": 68,
|
431 |
+
"id": "d440d74d-2b5d-48e0-ad86-f355a25a6c16",
|
432 |
+
"metadata": {},
|
433 |
+
"outputs": [
|
434 |
+
{
|
435 |
+
"data": {
|
436 |
+
"text/plain": [
|
437 |
+
"tensor([ 274, 1446, 526, 451, 278, 871, 15006, 393, 508, 367,\n",
|
438 |
+
" 15201, 491, 445, 4195, 29889, 360, 12099, 29892, 27127, 1169,\n",
|
439 |
+
" 29892, 322, 1584, 17952, 508, 884, 8812, 515, 445, 17135,\n",
|
440 |
+
" 29889, 13, 13, 1576, 25828, 4835, 310, 285, 5570, 454,\n",
|
441 |
+
" 2679, 29747, 24424, 297, 20309, 508, 13100, 8679, 373, 278,\n",
|
442 |
+
" 7408, 310, 278, 17135, 29889, 512, 278, 4688, 22950, 29892,\n",
|
443 |
+
" 274, 1446, 1122, 1510, 694, 18906, 310, 4486, 2264, 29889,\n",
|
444 |
+
" 2398, 29892, 408, 278, 17135, 6728, 267, 29892, 896, 1122,\n",
|
445 |
+
" 4953, 454, 386, 1191, 293, 29892, 14074, 1009, 623, 300,\n",
|
446 |
+
" 568, 29892, 322, 2693, 2381, 324, 2435, 301, 962, 561],\n",
|
447 |
+
" device='cuda:0')"
|
448 |
+
]
|
449 |
+
},
|
450 |
+
"execution_count": 68,
|
451 |
+
"metadata": {},
|
452 |
+
"output_type": "execute_result"
|
453 |
+
}
|
454 |
+
],
|
455 |
+
"source": [
|
456 |
+
"output[0]"
|
457 |
+
]
|
458 |
+
},
|
459 |
+
{
|
460 |
+
"cell_type": "code",
|
461 |
+
"execution_count": 75,
|
462 |
+
"id": "8f557898-32bb-4cc9-b9de-cb80bb4ee21e",
|
463 |
+
"metadata": {},
|
464 |
+
"outputs": [
|
465 |
+
{
|
466 |
+
"data": {
|
467 |
+
"text/plain": [
|
468 |
+
"tensor([4, 3, 0])"
|
469 |
+
]
|
470 |
+
},
|
471 |
+
"execution_count": 75,
|
472 |
+
"metadata": {},
|
473 |
+
"output_type": "execute_result"
|
474 |
+
}
|
475 |
+
],
|
476 |
+
"source": [
|
477 |
+
"x = torch.tensor([123], dtype=torch.long)\n",
|
478 |
+
"\n",
|
479 |
+
"#loss(x,x)\n",
|
480 |
+
"\n",
|
481 |
+
"input = torch.randn(3, 5, requires_grad=True)\n",
|
482 |
+
"target = torch.empty(3, dtype=torch.long).random_(5)\n",
|
483 |
+
"\n",
|
484 |
+
"target"
|
485 |
+
]
|
486 |
+
},
|
487 |
+
{
|
488 |
+
"cell_type": "code",
|
489 |
+
"execution_count": 59,
|
490 |
+
"id": "139e9973-003a-484f-95f8-42428dd436f5",
|
491 |
+
"metadata": {},
|
492 |
+
"outputs": [
|
493 |
+
{
|
494 |
+
"name": "stdout",
|
495 |
+
"output_type": "stream",
|
496 |
+
"text": [
|
497 |
+
"cats are not the only animals that can be affected by this condition. Dogs, rabbits, and even birds can also suffer from this disease.\n",
|
498 |
+
"\n",
|
499 |
+
"The symptoms of feline leukemia virus infection can vary depending on the stage of the disease. In the early stages, cats may show no signs of illness. However, as the disease progresses, they may become lethargic, lose their appetite, and develop swollen lymph\n"
|
500 |
+
]
|
501 |
+
}
|
502 |
+
],
|
503 |
+
"source": [
|
504 |
+
"generated_text = tokenizer.decode(output[0], skip_special_tokens=True)\n",
|
505 |
+
"\n",
|
506 |
+
"print(generated_text)"
|
507 |
+
]
|
508 |
+
},
|
509 |
+
{
|
510 |
+
"cell_type": "code",
|
511 |
+
"execution_count": 54,
|
512 |
+
"id": "cda57a53-98f7-45eb-84c1-9ef8360926a4",
|
513 |
+
"metadata": {},
|
514 |
+
"outputs": [
|
515 |
+
{
|
516 |
+
"name": "stderr",
|
517 |
+
"output_type": "stream",
|
518 |
+
"text": [
|
519 |
+
"Attempting to cast a BatchEncoding to type torch.float16. This is not supported.\n",
|
520 |
+
"Attempting to cast a BatchEncoding to type torch.float16. This is not supported.\n"
|
521 |
+
]
|
522 |
+
},
|
523 |
+
{
|
524 |
+
"ename": "RuntimeError",
|
525 |
+
"evalue": "Expected floating point type for target with class probabilities, got Long",
|
526 |
+
"output_type": "error",
|
527 |
+
"traceback": [
|
528 |
+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
529 |
+
"\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
|
530 |
+
"Cell \u001b[0;32mIn[54], line 6\u001b[0m\n\u001b[1;32m 3\u001b[0m i \u001b[38;5;241m=\u001b[39m tokenizer(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mHello\u001b[39m\u001b[38;5;124m\"\u001b[39m, return_tensors\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mpt\u001b[39m\u001b[38;5;124m'\u001b[39m)\u001b[38;5;241m.\u001b[39mto(torch\u001b[38;5;241m.\u001b[39mfloat16)\n\u001b[1;32m 4\u001b[0m lab \u001b[38;5;241m=\u001b[39m tokenizer(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mHella\u001b[39m\u001b[38;5;124m\"\u001b[39m, return_tensors\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mpt\u001b[39m\u001b[38;5;124m'\u001b[39m)\u001b[38;5;241m.\u001b[39mto(torch\u001b[38;5;241m.\u001b[39mfloat16)\n\u001b[0;32m----> 6\u001b[0m \u001b[43mloss\u001b[49m\u001b[43m(\u001b[49m\u001b[43mgenerate_out\u001b[49m\u001b[43m(\u001b[49m\u001b[43mi\u001b[49m\u001b[43m,\u001b[49m\u001b[38;5;28;43mlen\u001b[39;49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mgenerate_out\u001b[49m\u001b[43m(\u001b[49m\u001b[43mlab\u001b[49m\u001b[43m,\u001b[49m\u001b[38;5;28;43mlen\u001b[39;49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n",
|
531 |
+
"File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py:1511\u001b[0m, in \u001b[0;36mModule._wrapped_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1509\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_compiled_call_impl(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs) \u001b[38;5;66;03m# type: ignore[misc]\u001b[39;00m\n\u001b[1;32m 1510\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m-> 1511\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_call_impl\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
|
532 |
+
"File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py:1520\u001b[0m, in \u001b[0;36mModule._call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1515\u001b[0m \u001b[38;5;66;03m# If we don't have any hooks, we want to skip the rest of the logic in\u001b[39;00m\n\u001b[1;32m 1516\u001b[0m \u001b[38;5;66;03m# this function, and just call forward.\u001b[39;00m\n\u001b[1;32m 1517\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m (\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_backward_hooks \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_backward_pre_hooks \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_forward_hooks \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_forward_pre_hooks\n\u001b[1;32m 1518\u001b[0m \u001b[38;5;129;01mor\u001b[39;00m _global_backward_pre_hooks \u001b[38;5;129;01mor\u001b[39;00m _global_backward_hooks\n\u001b[1;32m 1519\u001b[0m \u001b[38;5;129;01mor\u001b[39;00m _global_forward_hooks \u001b[38;5;129;01mor\u001b[39;00m _global_forward_pre_hooks):\n\u001b[0;32m-> 1520\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mforward_call\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1522\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 1523\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n",
|
533 |
+
"File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/torch/nn/modules/loss.py:1179\u001b[0m, in \u001b[0;36mCrossEntropyLoss.forward\u001b[0;34m(self, input, target)\u001b[0m\n\u001b[1;32m 1178\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mforward\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;28minput\u001b[39m: Tensor, target: Tensor) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Tensor:\n\u001b[0;32m-> 1179\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mF\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcross_entropy\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtarget\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mweight\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mweight\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1180\u001b[0m \u001b[43m \u001b[49m\u001b[43mignore_index\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mignore_index\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mreduction\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreduction\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1181\u001b[0m \u001b[43m \u001b[49m\u001b[43mlabel_smoothing\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mlabel_smoothing\u001b[49m\u001b[43m)\u001b[49m\n",
|
534 |
+
"File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/torch/nn/functional.py:3059\u001b[0m, in \u001b[0;36mcross_entropy\u001b[0;34m(input, target, weight, size_average, ignore_index, reduce, reduction, label_smoothing)\u001b[0m\n\u001b[1;32m 3057\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m size_average \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mor\u001b[39;00m reduce \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 3058\u001b[0m reduction \u001b[38;5;241m=\u001b[39m _Reduction\u001b[38;5;241m.\u001b[39mlegacy_get_string(size_average, reduce)\n\u001b[0;32m-> 3059\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_C\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_nn\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcross_entropy_loss\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtarget\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mweight\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m_Reduction\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_enum\u001b[49m\u001b[43m(\u001b[49m\u001b[43mreduction\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mignore_index\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mlabel_smoothing\u001b[49m\u001b[43m)\u001b[49m\n",
|
535 |
+
"\u001b[0;31mRuntimeError\u001b[0m: Expected floating point type for target with class probabilities, got Long"
|
536 |
+
]
|
537 |
+
}
|
538 |
+
],
|
539 |
+
"source": [
|
540 |
+
"loss = nn.CrossEntropyLoss()\n",
|
541 |
+
"\n",
|
542 |
+
"i = tokenizer(\"Hello\", return_tensors='pt').to(torch.float16)\n",
|
543 |
+
"lab = tokenizer(\"Hella\", return_tensors='pt').to(torch.float16\n",
|
544 |
+
"\n",
|
545 |
+
"loss(generate_out(i,len=10), generate_out(lab,len=10))\n",
|
546 |
+
"\n",
|
547 |
+
"#loss(i, lab)\n",
|
548 |
+
"\n",
|
549 |
+
" "
|
550 |
+
]
|
551 |
+
},
|
552 |
+
{
|
553 |
+
"cell_type": "markdown",
|
554 |
+
"id": "f99adf3c-aefb-4c22-a8b1-e8f79ef0b9d8",
|
555 |
+
"metadata": {},
|
556 |
+
"source": [
|
557 |
+
"### Test perplexity using MMLU"
|
558 |
+
]
|
559 |
+
},
|
560 |
+
{
|
561 |
+
"cell_type": "code",
|
562 |
+
"execution_count": null,
|
563 |
+
"id": "287155f7-6808-4419-9b12-8756e1746e28",
|
564 |
+
"metadata": {},
|
565 |
+
"outputs": [],
|
566 |
+
"source": [
|
567 |
+
"prompt = \"When a diver points a flashlight upward toward the surface of the water at an angle 20Β° from the normal, the beam of light\"\n",
|
568 |
+
"subject = \"conceptual_physics\"\n",
|
569 |
+
"answer = \"passes into the air above\"\n",
|
570 |
+
"\n",
|
571 |
+
"input_text = []\n",
|
572 |
+
"\n"
|
573 |
+
]
|
574 |
+
},
|
575 |
+
{
|
576 |
+
"cell_type": "code",
|
577 |
+
"execution_count": 93,
|
578 |
+
"id": "7bdb1138-7da5-44dc-9549-df7d40ec68e1",
|
579 |
+
"metadata": {},
|
580 |
+
"outputs": [
|
581 |
+
{
|
582 |
+
"name": "stderr",
|
583 |
+
"output_type": "stream",
|
584 |
+
"text": [
|
585 |
+
"huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
|
586 |
+
"To disable this warning, you can either:\n",
|
587 |
+
"\t- Avoid using `tokenizers` before the fork if possible\n",
|
588 |
+
"\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
|
589 |
+
]
|
590 |
+
},
|
591 |
+
{
|
592 |
+
"name": "stdout",
|
593 |
+
"output_type": "stream",
|
594 |
+
"text": [
|
595 |
+
"2024-07-04:16:44:18,903 INFO [__main__.py:272] Verbosity set to INFO\n",
|
596 |
+
"2024-07-04:16:44:24,067 INFO [__main__.py:369] Selected Tasks: ['hellaswag']\n",
|
597 |
+
"2024-07-04:16:44:24,115 INFO [evaluator.py:152] Setting random seed to 0 | Setting numpy seed to 1234 | Setting torch manual seed to 1234\n",
|
598 |
+
"2024-07-04:16:44:24,115 INFO [evaluator.py:189] Initializing hf model, with arguments: {'pretrained': 'EleutherAI/gpt-j-6B'}\n",
|
599 |
+
"2024-07-04:16:44:24,156 INFO [huggingface.py:170] Using device 'cuda:0'\n",
|
600 |
+
"config.json: 100%|βββββββββββββββββββββββββββββ| 930/930 [00:00<00:00, 7.62MB/s]\n",
|
601 |
+
"tokenizer_config.json: 100%|βββββββββββββββββββ| 619/619 [00:00<00:00, 5.78MB/s]\n",
|
602 |
+
"vocab.json: 100%|ββββββββββββββββββββββββββββ| 798k/798k [00:00<00:00, 20.4MB/s]\n",
|
603 |
+
"merges.txt: 100%|ββββββββββββββββββββββββββββ| 456k/456k [00:00<00:00, 6.58MB/s]\n",
|
604 |
+
"tokenizer.json: 100%|ββββββββββββββββββββββ| 1.37M/1.37M [00:00<00:00, 14.4MB/s]\n",
|
605 |
+
"added_tokens.json: 100%|βββββββββββββββββββ| 4.04k/4.04k [00:00<00:00, 44.5MB/s]\n",
|
606 |
+
"special_tokens_map.json: 100%|βββββββββββββββββ| 357/357 [00:00<00:00, 4.00MB/s]\n",
|
607 |
+
"pytorch_model.bin: 100%|ββββββββββββββββββββ| 24.2G/24.2G [02:04<00:00, 195MB/s]\n",
|
608 |
+
"Traceback (most recent call last):\n",
|
609 |
+
" File \"/opt/conda/bin/lm_eval\", line 8, in <module>\n",
|
610 |
+
" sys.exit(cli_evaluate())\n",
|
611 |
+
" File \"/lm-evaluation-harness/lm_eval/__main__.py\", line 375, in cli_evaluate\n",
|
612 |
+
" results = evaluator.simple_evaluate(\n",
|
613 |
+
" File \"/lm-evaluation-harness/lm_eval/utils.py\", line 395, in _wrapper\n",
|
614 |
+
" return fn(*args, **kwargs)\n",
|
615 |
+
" File \"/lm-evaluation-harness/lm_eval/evaluator.py\", line 192, in simple_evaluate\n",
|
616 |
+
" lm = lm_eval.api.registry.get_model(model).create_from_arg_string(\n",
|
617 |
+
" File \"/lm-evaluation-harness/lm_eval/api/model.py\", line 148, in create_from_arg_string\n",
|
618 |
+
" return cls(**args, **args2)\n",
|
619 |
+
" File \"/lm-evaluation-harness/lm_eval/models/huggingface.py\", line 218, in __init__\n",
|
620 |
+
" self._create_model(\n",
|
621 |
+
" File \"/lm-evaluation-harness/lm_eval/models/huggingface.py\", line 541, in _create_model\n",
|
622 |
+
" self._model = self.AUTO_MODEL_CLASS.from_pretrained(\n",
|
623 |
+
" File \"/opt/conda/lib/python3.10/site-packages/transformers/models/auto/auto_factory.py\", line 564, in from_pretrained\n",
|
624 |
+
" return model_class.from_pretrained(\n",
|
625 |
+
" File \"/opt/conda/lib/python3.10/site-packages/transformers/modeling_utils.py\", line 3838, in from_pretrained\n",
|
626 |
+
" ) = cls._load_pretrained_model(\n",
|
627 |
+
" File \"/opt/conda/lib/python3.10/site-packages/transformers/modeling_utils.py\", line 4298, in _load_pretrained_model\n",
|
628 |
+
" new_error_msgs, offload_index, state_dict_index = _load_state_dict_into_meta_model(\n",
|
629 |
+
" File \"/opt/conda/lib/python3.10/site-packages/transformers/modeling_utils.py\", line 895, in _load_state_dict_into_meta_model\n",
|
630 |
+
" set_module_tensor_to_device(model, param_name, param_device, **set_module_kwargs)\n",
|
631 |
+
" File \"/opt/conda/lib/python3.10/site-packages/accelerate/utils/modeling.py\", line 404, in set_module_tensor_to_device\n",
|
632 |
+
" new_value = value.to(device)\n",
|
633 |
+
"torch.cuda.OutOfMemoryError: CUDA out of memory. Tried to allocate 256.00 MiB. GPU 0 has a total capacity of 23.65 GiB of which 216.25 MiB is free. Process 1653457 has 14.78 GiB memory in use. Process 1705644 has 8.65 GiB memory in use. Of the allocated memory 8.27 GiB is allocated by PyTorch, and 667.00 KiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True to avoid fragmentation. See documentation for Memory Management (https://pytorch.org/docs/stable/notes/cuda.html#environment-variables)\n"
|
634 |
+
]
|
635 |
+
}
|
636 |
+
],
|
637 |
+
"source": [
|
638 |
+
"!lm_eval --model hf \\\n",
|
639 |
+
" --model_args pretrained=EleutherAI/gpt-j-6B \\\n",
|
640 |
+
" --tasks hellaswag \\\n",
|
641 |
+
" --device cuda:0 \\\n",
|
642 |
+
" --batch_size 8"
|
643 |
+
]
|
644 |
+
},
|
645 |
+
{
|
646 |
+
"cell_type": "code",
|
647 |
+
"execution_count": null,
|
648 |
+
"id": "a9e753f6-4272-4305-bbfc-2eceeb5c4532",
|
649 |
+
"metadata": {},
|
650 |
+
"outputs": [],
|
651 |
+
"source": []
|
652 |
+
}
|
653 |
+
],
|
654 |
+
"metadata": {
|
655 |
+
"kernelspec": {
|
656 |
+
"display_name": "Python 3 (ipykernel)",
|
657 |
+
"language": "python",
|
658 |
+
"name": "python3"
|
659 |
+
},
|
660 |
+
"language_info": {
|
661 |
+
"codemirror_mode": {
|
662 |
+
"name": "ipython",
|
663 |
+
"version": 3
|
664 |
+
},
|
665 |
+
"file_extension": ".py",
|
666 |
+
"mimetype": "text/x-python",
|
667 |
+
"name": "python",
|
668 |
+
"nbconvert_exporter": "python",
|
669 |
+
"pygments_lexer": "ipython3",
|
670 |
+
"version": "3.10.13"
|
671 |
+
}
|
672 |
+
},
|
673 |
+
"nbformat": 4,
|
674 |
+
"nbformat_minor": 5
|
675 |
+
}
|