Model save
Browse files- .ipynb_checkpoints/aqlm_2bit_training-checkpoint.ipynb +622 -0
- .ipynb_checkpoints/test_ft-checkpoint.py +165 -0
- =0.27.0 +35 -0
- =1.1.0 +0 -0
- README.md +52 -0
- adapter_config.json +34 -0
- adapter_model.safetensors +3 -0
- aqlm_2bit_training.ipynb +622 -0
- test_ft.py +165 -0
- training_args.bin +3 -0
.ipynb_checkpoints/aqlm_2bit_training-checkpoint.ipynb
ADDED
@@ -0,0 +1,622 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"metadata": {
|
6 |
+
"id": "XIyP_0r6zuVc"
|
7 |
+
},
|
8 |
+
"source": [
|
9 |
+
"# Training Large Language Models in 2bit with `aqlm`, `transformers` and `PEFT`\n",
|
10 |
+
"\n",
|
11 |
+
"<a target=\"_blank\" href=\"https://colab.research.google.com/github/Vahe1994/AQLM/blob/main/notebooks/aqlm_2bit_training.ipynb\">\n",
|
12 |
+
" <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/>\n",
|
13 |
+
"</a>\n",
|
14 |
+
"\n",
|
15 |
+
"Welcome to this notebook that goes through the recent `aqlm` integration that introduces minimal performance degradation 2bit quantization techniques.\n",
|
16 |
+
"\n",
|
17 |
+
"In this notebook, we will learn how to load a large model in 2bit (`Mixtral-8x7b`) and train it using Google Colab and PEFT library from Hugging Face 🤗.\n"
|
18 |
+
]
|
19 |
+
},
|
20 |
+
{
|
21 |
+
"cell_type": "markdown",
|
22 |
+
"metadata": {
|
23 |
+
"id": "A_VgSpl4Dsr3"
|
24 |
+
},
|
25 |
+
"source": [
|
26 |
+
"**Install the `aqlm` library**\n",
|
27 |
+
"- It's the only extra dependency to run AQLM models.\n",
|
28 |
+
"- Add `[gpu]` to install the required CUDA specific dependencies.\n",
|
29 |
+
"- Install the latest `accelerate` and `transformers` releases to properly support it."
|
30 |
+
]
|
31 |
+
},
|
32 |
+
{
|
33 |
+
"cell_type": "code",
|
34 |
+
"execution_count": null,
|
35 |
+
"metadata": {
|
36 |
+
"id": "FuXIFTFapAMI"
|
37 |
+
},
|
38 |
+
"outputs": [],
|
39 |
+
"source": [
|
40 |
+
"%%capture\n",
|
41 |
+
"!pip install aqlm[gpu]>=1.1.0\n",
|
42 |
+
"!pip install git+https://github.com/huggingface/peft.git@main\n",
|
43 |
+
"!pip install accelerate>=0.27.0\n",
|
44 |
+
"!pip install git+https://github.com/huggingface/transformers.git@main\n",
|
45 |
+
"!pip install datasets\n",
|
46 |
+
"!pip install bitsandbytes\n",
|
47 |
+
"# for 8-bit optimizer only"
|
48 |
+
]
|
49 |
+
},
|
50 |
+
{
|
51 |
+
"cell_type": "markdown",
|
52 |
+
"metadata": {
|
53 |
+
"id": "MJ-5idQwzvg-"
|
54 |
+
},
|
55 |
+
"source": [
|
56 |
+
"First let's load the model we are going to use - `Mixtral-8x7b`! Note that the model itself is around 50GB in half precision"
|
57 |
+
]
|
58 |
+
},
|
59 |
+
{
|
60 |
+
"cell_type": "code",
|
61 |
+
"execution_count": 23,
|
62 |
+
"metadata": {
|
63 |
+
"id": "E0Nl5mWL0k2T"
|
64 |
+
},
|
65 |
+
"outputs": [],
|
66 |
+
"source": [
|
67 |
+
"import torch\n",
|
68 |
+
"from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig\n",
|
69 |
+
"\n",
|
70 |
+
"model_id = \"ISTA-DASLab/Meta-Llama-3-8B-Instruct-AQLM-2Bit-1x16\"\n",
|
71 |
+
"\n",
|
72 |
+
"tokenizer = AutoTokenizer.from_pretrained(model_id)\n",
|
73 |
+
"model = AutoModelForCausalLM.from_pretrained(model_id, device_map=\"auto\", torch_dtype=\"bfloat16\", low_cpu_mem_usage=True)"
|
74 |
+
]
|
75 |
+
},
|
76 |
+
{
|
77 |
+
"cell_type": "markdown",
|
78 |
+
"metadata": {
|
79 |
+
"id": "Mp2gMi1ZzGET"
|
80 |
+
},
|
81 |
+
"source": [
|
82 |
+
"**Add LoRA**\n",
|
83 |
+
"\n",
|
84 |
+
"To alter model's behavior, we have to make it trainable. We can do that by addind a small set of trainable parameters on top of the untrainable quantized ones."
|
85 |
+
]
|
86 |
+
},
|
87 |
+
{
|
88 |
+
"cell_type": "code",
|
89 |
+
"execution_count": 24,
|
90 |
+
"metadata": {
|
91 |
+
"colab": {
|
92 |
+
"base_uri": "https://localhost:8080/"
|
93 |
+
},
|
94 |
+
"id": "Ybeyl20n3dYH",
|
95 |
+
"outputId": "0efda156-4886-4718-9877-e93a17dc02d2"
|
96 |
+
},
|
97 |
+
"outputs": [
|
98 |
+
{
|
99 |
+
"name": "stdout",
|
100 |
+
"output_type": "stream",
|
101 |
+
"text": [
|
102 |
+
"trainable params: 41,943,040 || all params: 2,084,114,432 || trainable%: 2.0125\n"
|
103 |
+
]
|
104 |
+
}
|
105 |
+
],
|
106 |
+
"source": [
|
107 |
+
"from peft import LoraConfig, get_peft_model\n",
|
108 |
+
"\n",
|
109 |
+
"config = LoraConfig(\n",
|
110 |
+
" r=16,\n",
|
111 |
+
" lora_alpha=32,\n",
|
112 |
+
" target_modules=['q_proj','k_proj','v_proj','o_proj','gate_proj','down_proj','up_proj', ],\n",
|
113 |
+
" lora_dropout=0.05,\n",
|
114 |
+
" bias=\"none\",\n",
|
115 |
+
" task_type=\"CAUSAL_LM\"\n",
|
116 |
+
")\n",
|
117 |
+
"\n",
|
118 |
+
"model = get_peft_model(model, config)\n",
|
119 |
+
"model.print_trainable_parameters()\n",
|
120 |
+
"model.enable_input_require_grads() # it's needed for gradient checkpointing"
|
121 |
+
]
|
122 |
+
},
|
123 |
+
{
|
124 |
+
"cell_type": "markdown",
|
125 |
+
"metadata": {
|
126 |
+
"id": "4xSPH1D_Wv9x"
|
127 |
+
},
|
128 |
+
"source": [
|
129 |
+
"Here we add a trainable adapter ontop of every `q_prok`, `k_proj` and `o_proj` linear layer."
|
130 |
+
]
|
131 |
+
},
|
132 |
+
{
|
133 |
+
"cell_type": "markdown",
|
134 |
+
"metadata": {
|
135 |
+
"id": "FCc64bfnmd3j"
|
136 |
+
},
|
137 |
+
"source": [
|
138 |
+
"**Loading a dataset**\n",
|
139 |
+
"\n",
|
140 |
+
"Let's load a common dataset, english quotes, to fine tune our model on famous quotes."
|
141 |
+
]
|
142 |
+
},
|
143 |
+
{
|
144 |
+
"cell_type": "code",
|
145 |
+
"execution_count": 13,
|
146 |
+
"metadata": {
|
147 |
+
"id": "s6f4z8EYmcJ6"
|
148 |
+
},
|
149 |
+
"outputs": [
|
150 |
+
{
|
151 |
+
"data": {
|
152 |
+
"application/vnd.jupyter.widget-view+json": {
|
153 |
+
"model_id": "9ef07f1bc62e4887817a81d4a3e15da1",
|
154 |
+
"version_major": 2,
|
155 |
+
"version_minor": 0
|
156 |
+
},
|
157 |
+
"text/plain": [
|
158 |
+
"Resolving data files: 0%| | 0/114 [00:00<?, ?it/s]"
|
159 |
+
]
|
160 |
+
},
|
161 |
+
"metadata": {},
|
162 |
+
"output_type": "display_data"
|
163 |
+
},
|
164 |
+
{
|
165 |
+
"name": "stdout",
|
166 |
+
"output_type": "stream",
|
167 |
+
"text": [
|
168 |
+
"Loaded dataset with 100000 examples\n"
|
169 |
+
]
|
170 |
+
},
|
171 |
+
{
|
172 |
+
"data": {
|
173 |
+
"application/vnd.jupyter.widget-view+json": {
|
174 |
+
"model_id": "560c7be6397c4e3aac2318d97f1f8f86",
|
175 |
+
"version_major": 2,
|
176 |
+
"version_minor": 0
|
177 |
+
},
|
178 |
+
"text/plain": [
|
179 |
+
"tokenizer_config.json: 0%| | 0.00/26.0 [00:00<?, ?B/s]"
|
180 |
+
]
|
181 |
+
},
|
182 |
+
"metadata": {},
|
183 |
+
"output_type": "display_data"
|
184 |
+
},
|
185 |
+
{
|
186 |
+
"data": {
|
187 |
+
"application/vnd.jupyter.widget-view+json": {
|
188 |
+
"model_id": "b667958a3b3d4529b77baf5e5bc9c259",
|
189 |
+
"version_major": 2,
|
190 |
+
"version_minor": 0
|
191 |
+
},
|
192 |
+
"text/plain": [
|
193 |
+
"config.json: 0%| | 0.00/665 [00:00<?, ?B/s]"
|
194 |
+
]
|
195 |
+
},
|
196 |
+
"metadata": {},
|
197 |
+
"output_type": "display_data"
|
198 |
+
},
|
199 |
+
{
|
200 |
+
"data": {
|
201 |
+
"application/vnd.jupyter.widget-view+json": {
|
202 |
+
"model_id": "10359f3b8d974be49da2d3fd87f89576",
|
203 |
+
"version_major": 2,
|
204 |
+
"version_minor": 0
|
205 |
+
},
|
206 |
+
"text/plain": [
|
207 |
+
"vocab.json: 0%| | 0.00/1.04M [00:00<?, ?B/s]"
|
208 |
+
]
|
209 |
+
},
|
210 |
+
"metadata": {},
|
211 |
+
"output_type": "display_data"
|
212 |
+
},
|
213 |
+
{
|
214 |
+
"data": {
|
215 |
+
"application/vnd.jupyter.widget-view+json": {
|
216 |
+
"model_id": "97835946d4a44460bc1bd48276b8d3d0",
|
217 |
+
"version_major": 2,
|
218 |
+
"version_minor": 0
|
219 |
+
},
|
220 |
+
"text/plain": [
|
221 |
+
"merges.txt: 0%| | 0.00/456k [00:00<?, ?B/s]"
|
222 |
+
]
|
223 |
+
},
|
224 |
+
"metadata": {},
|
225 |
+
"output_type": "display_data"
|
226 |
+
},
|
227 |
+
{
|
228 |
+
"data": {
|
229 |
+
"application/vnd.jupyter.widget-view+json": {
|
230 |
+
"model_id": "ed37faeff8914b369649cb514981991d",
|
231 |
+
"version_major": 2,
|
232 |
+
"version_minor": 0
|
233 |
+
},
|
234 |
+
"text/plain": [
|
235 |
+
"tokenizer.json: 0%| | 0.00/1.36M [00:00<?, ?B/s]"
|
236 |
+
]
|
237 |
+
},
|
238 |
+
"metadata": {},
|
239 |
+
"output_type": "display_data"
|
240 |
+
},
|
241 |
+
{
|
242 |
+
"name": "stderr",
|
243 |
+
"output_type": "stream",
|
244 |
+
"text": [
|
245 |
+
"/usr/local/lib/python3.10/dist-packages/transformers/tokenization_utils_base.py:1601: FutureWarning: `clean_up_tokenization_spaces` was not set. It will be set to `True` by default. This behavior will be deprecated in transformers v4.45, and will be then set to `False` by default. For more details check this issue: https://github.com/huggingface/transformers/issues/31884\n",
|
246 |
+
" warnings.warn(\n"
|
247 |
+
]
|
248 |
+
},
|
249 |
+
{
|
250 |
+
"data": {
|
251 |
+
"application/vnd.jupyter.widget-view+json": {
|
252 |
+
"model_id": "7a56a1781f3347f8a056a18dc24ea7a9",
|
253 |
+
"version_major": 2,
|
254 |
+
"version_minor": 0
|
255 |
+
},
|
256 |
+
"text/plain": [
|
257 |
+
"Map: 0%| | 0/100000 [00:00<?, ? examples/s]"
|
258 |
+
]
|
259 |
+
},
|
260 |
+
"metadata": {},
|
261 |
+
"output_type": "display_data"
|
262 |
+
},
|
263 |
+
{
|
264 |
+
"name": "stdout",
|
265 |
+
"output_type": "stream",
|
266 |
+
"text": [
|
267 |
+
"Processed dataset has 100000 examples\n",
|
268 |
+
"Features: {'input_ids': Sequence(feature=Value(dtype='int32', id=None), length=-1, id=None), 'attention_mask': Sequence(feature=Value(dtype='int8', id=None), length=-1, id=None)}\n"
|
269 |
+
]
|
270 |
+
}
|
271 |
+
],
|
272 |
+
"source": [
|
273 |
+
"from datasets import load_dataset, Dataset\n",
|
274 |
+
"import itertools\n",
|
275 |
+
"from transformers import AutoTokenizer\n",
|
276 |
+
"\n",
|
277 |
+
"# Load the dataset in streaming mode\n",
|
278 |
+
"ds = load_dataset(\"open-web-math/open-web-math\", split=\"train\", streaming=True)\n",
|
279 |
+
"\n",
|
280 |
+
"# Define the number of examples you want to load\n",
|
281 |
+
"num_examples = 100000 # Adjust this number as needed\n",
|
282 |
+
"\n",
|
283 |
+
"# Create a subset by taking the first num_examples\n",
|
284 |
+
"subset = list(itertools.islice(ds, num_examples))\n",
|
285 |
+
"\n",
|
286 |
+
"# Convert the subset to a Dataset object\n",
|
287 |
+
"data = Dataset.from_list(subset)\n",
|
288 |
+
"print(f\"Loaded dataset with {len(data)} examples\")\n",
|
289 |
+
"\n",
|
290 |
+
"# Initialize tokenizer (replace 'gpt2' with your specific model if different)\n",
|
291 |
+
"tokenizer = AutoTokenizer.from_pretrained('gpt2')\n",
|
292 |
+
"\n",
|
293 |
+
"max_seq_length = 2048\n",
|
294 |
+
"tokenizer.pad_token = tokenizer.eos_token\n",
|
295 |
+
"tokenizer.model_max_length = max_seq_length\n",
|
296 |
+
"\n",
|
297 |
+
"def preprocess_function(examples):\n",
|
298 |
+
" # Join the list of strings into a single string\n",
|
299 |
+
" texts = [\" \".join(text) for text in examples[\"text\"]]\n",
|
300 |
+
" return tokenizer(texts, truncation=True, max_length=max_seq_length, padding=\"max_length\")\n",
|
301 |
+
"\n",
|
302 |
+
"# Process the dataset\n",
|
303 |
+
"processed_dataset = data.map(preprocess_function, batched=True, remove_columns=data.column_names)\n",
|
304 |
+
"\n",
|
305 |
+
"print(f\"Processed dataset has {len(processed_dataset)} examples\")\n",
|
306 |
+
"print(f\"Features: {processed_dataset.features}\")\n"
|
307 |
+
]
|
308 |
+
},
|
309 |
+
{
|
310 |
+
"cell_type": "code",
|
311 |
+
"execution_count": 15,
|
312 |
+
"metadata": {},
|
313 |
+
"outputs": [],
|
314 |
+
"source": [
|
315 |
+
"import argparse\n",
|
316 |
+
"import torch\n",
|
317 |
+
"from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments\n",
|
318 |
+
"import transformers\n",
|
319 |
+
"from peft import LoraConfig, get_peft_model\n",
|
320 |
+
"from datasets import load_dataset\n",
|
321 |
+
"from transformers.trainer_callback import TrainerCallback\n",
|
322 |
+
"import os\n",
|
323 |
+
"import random\n",
|
324 |
+
"import subprocess\n",
|
325 |
+
"from huggingface_hub import HfApi, hf_hub_download\n",
|
326 |
+
"\n",
|
327 |
+
"\n",
|
328 |
+
"# Custom callback to push to Hub\n",
|
329 |
+
"class PushToHubCallback(TrainerCallback):\n",
|
330 |
+
" def __init__(self, trainer, push_frequency):\n",
|
331 |
+
" self.trainer = trainer\n",
|
332 |
+
" self.push_frequency = push_frequency\n",
|
333 |
+
"\n",
|
334 |
+
" def on_step_end(self, args, state, control, **kwargs):\n",
|
335 |
+
" if state.global_step % self.push_frequency == 0:\n",
|
336 |
+
" self.trainer.save_model()\n",
|
337 |
+
" self.trainer.push_to_hub(\n",
|
338 |
+
" commit_message=f\"Training in progress - Step {state.global_step}\"\n",
|
339 |
+
" )\n"
|
340 |
+
]
|
341 |
+
},
|
342 |
+
{
|
343 |
+
"cell_type": "code",
|
344 |
+
"execution_count": 21,
|
345 |
+
"metadata": {},
|
346 |
+
"outputs": [
|
347 |
+
{
|
348 |
+
"data": {
|
349 |
+
"application/vnd.jupyter.widget-view+json": {
|
350 |
+
"model_id": "74a0f8d448004c048d8b0608fa3a61fd",
|
351 |
+
"version_major": 2,
|
352 |
+
"version_minor": 0
|
353 |
+
},
|
354 |
+
"text/plain": [
|
355 |
+
"VBox(children=(HTML(value='<center> <img\\nsrc=https://huggingface.co/front/assets/huggingface_logo-noborder.sv…"
|
356 |
+
]
|
357 |
+
},
|
358 |
+
"metadata": {},
|
359 |
+
"output_type": "display_data"
|
360 |
+
}
|
361 |
+
],
|
362 |
+
"source": [
|
363 |
+
"from huggingface_hub import notebook_login\n",
|
364 |
+
"\n",
|
365 |
+
"notebook_login()"
|
366 |
+
]
|
367 |
+
},
|
368 |
+
{
|
369 |
+
"cell_type": "code",
|
370 |
+
"execution_count": null,
|
371 |
+
"metadata": {},
|
372 |
+
"outputs": [
|
373 |
+
{
|
374 |
+
"name": "stderr",
|
375 |
+
"output_type": "stream",
|
376 |
+
"text": [
|
377 |
+
"max_steps is given, it will override any value given in num_train_epochs\n",
|
378 |
+
"/usr/local/lib/python3.10/dist-packages/torch/_dynamo/eval_frame.py:600: UserWarning: torch.utils.checkpoint: the use_reentrant parameter should be passed explicitly. In version 2.4 we will raise an exception if use_reentrant is not passed. use_reentrant=False is recommended, but if you need to preserve the current default behavior, you can pass use_reentrant=True. Refer to docs for more details on the differences between the two variants.\n",
|
379 |
+
" return fn(*args, **kwargs)\n",
|
380 |
+
"/usr/local/lib/python3.10/dist-packages/torch/utils/checkpoint.py:295: FutureWarning: `torch.cpu.amp.autocast(args...)` is deprecated. Please use `torch.amp.autocast('cpu', args...)` instead.\n",
|
381 |
+
" with torch.enable_grad(), device_autocast_ctx, torch.cpu.amp.autocast(**ctx.cpu_autocast_kwargs): # type: ignore[attr-defined]\n"
|
382 |
+
]
|
383 |
+
},
|
384 |
+
{
|
385 |
+
"data": {
|
386 |
+
"text/html": [
|
387 |
+
"\n",
|
388 |
+
" <div>\n",
|
389 |
+
" \n",
|
390 |
+
" <progress value='22' max='10000' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
|
391 |
+
" [ 22/10000 09:16 < 77:05:02, 0.04 it/s, Epoch 0.01/4]\n",
|
392 |
+
" </div>\n",
|
393 |
+
" <table border=\"1\" class=\"dataframe\">\n",
|
394 |
+
" <thead>\n",
|
395 |
+
" <tr style=\"text-align: left;\">\n",
|
396 |
+
" <th>Step</th>\n",
|
397 |
+
" <th>Training Loss</th>\n",
|
398 |
+
" </tr>\n",
|
399 |
+
" </thead>\n",
|
400 |
+
" <tbody>\n",
|
401 |
+
" <tr>\n",
|
402 |
+
" <td>1</td>\n",
|
403 |
+
" <td>5.558500</td>\n",
|
404 |
+
" </tr>\n",
|
405 |
+
" </tbody>\n",
|
406 |
+
"</table><p>"
|
407 |
+
],
|
408 |
+
"text/plain": [
|
409 |
+
"<IPython.core.display.HTML object>"
|
410 |
+
]
|
411 |
+
},
|
412 |
+
"metadata": {},
|
413 |
+
"output_type": "display_data"
|
414 |
+
}
|
415 |
+
],
|
416 |
+
"source": [
|
417 |
+
"hub_model_id = \"davisrbr/math-lora\"\n",
|
418 |
+
"tokenizer.pad_token = tokenizer.eos_token\n",
|
419 |
+
"torch.cuda.empty_cache()\n",
|
420 |
+
"trainer = transformers.Trainer(\n",
|
421 |
+
" model=model,\n",
|
422 |
+
" train_dataset=processed_dataset,\n",
|
423 |
+
" args=TrainingArguments(\n",
|
424 |
+
" per_device_train_batch_size=4,\n",
|
425 |
+
" gradient_accumulation_steps=8,\n",
|
426 |
+
" gradient_checkpointing=True,\n",
|
427 |
+
" warmup_steps=200,\n",
|
428 |
+
" max_steps=10000,\n",
|
429 |
+
" learning_rate=2e-4,\n",
|
430 |
+
" bf16=True,\n",
|
431 |
+
" logging_steps=25,\n",
|
432 |
+
" output_dir=\".\",\n",
|
433 |
+
" optim=\"adamw_bnb_8bit\",\n",
|
434 |
+
" logging_first_step=True,\n",
|
435 |
+
" push_to_hub=True,\n",
|
436 |
+
" hub_model_id=hub_model_id,\n",
|
437 |
+
" ),\n",
|
438 |
+
" data_collator=transformers.DataCollatorForLanguageModeling(tokenizer, mlm=False),\n",
|
439 |
+
")\n",
|
440 |
+
"model.config.use_cache = False\n",
|
441 |
+
"\n",
|
442 |
+
"push_frequency = 100\n",
|
443 |
+
"trainer.add_callback(PushToHubCallback(trainer, push_frequency,))\n",
|
444 |
+
"\n",
|
445 |
+
"trainer.train()\n",
|
446 |
+
"\n",
|
447 |
+
"final_commit_hash = trainer.push_to_hub(\"Training complete\")\n",
|
448 |
+
"print(f\"Training complete. Final commit hash: {final_commit_hash}\")"
|
449 |
+
]
|
450 |
+
},
|
451 |
+
{
|
452 |
+
"cell_type": "markdown",
|
453 |
+
"metadata": {
|
454 |
+
"id": "_0MOtwf3zdZp"
|
455 |
+
},
|
456 |
+
"source": [
|
457 |
+
"Run the cell below to run the training! For the sake of the demo, we just ran it for few steps just to showcase how to use this integration with existing tools on the HF ecosystem."
|
458 |
+
]
|
459 |
+
},
|
460 |
+
{
|
461 |
+
"cell_type": "code",
|
462 |
+
"execution_count": null,
|
463 |
+
"metadata": {
|
464 |
+
"colab": {
|
465 |
+
"base_uri": "https://localhost:8080/",
|
466 |
+
"height": 481
|
467 |
+
},
|
468 |
+
"id": "jq0nX33BmfaC",
|
469 |
+
"outputId": "7f470980-c49e-4230-b947-ad43510f1bee"
|
470 |
+
},
|
471 |
+
"outputs": [
|
472 |
+
{
|
473 |
+
"name": "stderr",
|
474 |
+
"output_type": "stream",
|
475 |
+
"text": [
|
476 |
+
"/usr/local/lib/python3.10/dist-packages/torch/utils/checkpoint.py:460: UserWarning: torch.utils.checkpoint: please pass in use_reentrant=True or use_reentrant=False explicitly. The default value of use_reentrant will be updated to be False in the future. To maintain current behavior, pass use_reentrant=True. It is recommended that you use use_reentrant=False. Refer to docs for more details on the differences between the two variants.\n",
|
477 |
+
" warnings.warn(\n"
|
478 |
+
]
|
479 |
+
},
|
480 |
+
{
|
481 |
+
"data": {
|
482 |
+
"text/html": [
|
483 |
+
"\n",
|
484 |
+
" <div>\n",
|
485 |
+
" \n",
|
486 |
+
" <progress value='10' max='10' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
|
487 |
+
" [10/10 13:02, Epoch 0/1]\n",
|
488 |
+
" </div>\n",
|
489 |
+
" <table border=\"1\" class=\"dataframe\">\n",
|
490 |
+
" <thead>\n",
|
491 |
+
" <tr style=\"text-align: left;\">\n",
|
492 |
+
" <th>Step</th>\n",
|
493 |
+
" <th>Training Loss</th>\n",
|
494 |
+
" </tr>\n",
|
495 |
+
" </thead>\n",
|
496 |
+
" <tbody>\n",
|
497 |
+
" <tr>\n",
|
498 |
+
" <td>1</td>\n",
|
499 |
+
" <td>2.042200</td>\n",
|
500 |
+
" </tr>\n",
|
501 |
+
" <tr>\n",
|
502 |
+
" <td>2</td>\n",
|
503 |
+
" <td>1.293400</td>\n",
|
504 |
+
" </tr>\n",
|
505 |
+
" <tr>\n",
|
506 |
+
" <td>3</td>\n",
|
507 |
+
" <td>1.447500</td>\n",
|
508 |
+
" </tr>\n",
|
509 |
+
" <tr>\n",
|
510 |
+
" <td>4</td>\n",
|
511 |
+
" <td>1.433600</td>\n",
|
512 |
+
" </tr>\n",
|
513 |
+
" <tr>\n",
|
514 |
+
" <td>5</td>\n",
|
515 |
+
" <td>1.725900</td>\n",
|
516 |
+
" </tr>\n",
|
517 |
+
" <tr>\n",
|
518 |
+
" <td>6</td>\n",
|
519 |
+
" <td>1.506400</td>\n",
|
520 |
+
" </tr>\n",
|
521 |
+
" <tr>\n",
|
522 |
+
" <td>7</td>\n",
|
523 |
+
" <td>1.549600</td>\n",
|
524 |
+
" </tr>\n",
|
525 |
+
" <tr>\n",
|
526 |
+
" <td>8</td>\n",
|
527 |
+
" <td>1.038300</td>\n",
|
528 |
+
" </tr>\n",
|
529 |
+
" <tr>\n",
|
530 |
+
" <td>9</td>\n",
|
531 |
+
" <td>1.603300</td>\n",
|
532 |
+
" </tr>\n",
|
533 |
+
" <tr>\n",
|
534 |
+
" <td>10</td>\n",
|
535 |
+
" <td>1.676400</td>\n",
|
536 |
+
" </tr>\n",
|
537 |
+
" </tbody>\n",
|
538 |
+
"</table><p>"
|
539 |
+
],
|
540 |
+
"text/plain": [
|
541 |
+
"<IPython.core.display.HTML object>"
|
542 |
+
]
|
543 |
+
},
|
544 |
+
"metadata": {},
|
545 |
+
"output_type": "display_data"
|
546 |
+
},
|
547 |
+
{
|
548 |
+
"data": {
|
549 |
+
"text/plain": [
|
550 |
+
"TrainOutput(global_step=10, training_loss=1.531658697128296, metrics={'train_runtime': 861.2678, 'train_samples_per_second': 0.046, 'train_steps_per_second': 0.012, 'total_flos': 56809829376000.0, 'train_loss': 1.531658697128296, 'epoch': 0.02})"
|
551 |
+
]
|
552 |
+
},
|
553 |
+
"execution_count": 6,
|
554 |
+
"metadata": {},
|
555 |
+
"output_type": "execute_result"
|
556 |
+
}
|
557 |
+
],
|
558 |
+
"source": [
|
559 |
+
"import transformers\n",
|
560 |
+
"\n",
|
561 |
+
"tokenizer.pad_token = tokenizer.eos_token\n",
|
562 |
+
"\n",
|
563 |
+
"trainer = transformers.Trainer(\n",
|
564 |
+
" model=model,\n",
|
565 |
+
" train_dataset=data[\"train\"],\n",
|
566 |
+
" args=transformers.TrainingArguments(\n",
|
567 |
+
" per_device_train_batch_size=1,\n",
|
568 |
+
" gradient_accumulation_steps=8,\n",
|
569 |
+
" gradient_checkpointing=True,\n",
|
570 |
+
" warmup_steps=2,\n",
|
571 |
+
" max_steps=10,\n",
|
572 |
+
" learning_rate=2e-4,\n",
|
573 |
+
" fp16=True,\n",
|
574 |
+
" logging_steps=1,\n",
|
575 |
+
" output_dir=\"outputs\",\n",
|
576 |
+
" optim=\"adamw_bnb_8bit\",\n",
|
577 |
+
" logging_first_step=True,\n",
|
578 |
+
" ),\n",
|
579 |
+
" data_collator=transformers.DataCollatorForLanguageModeling(tokenizer, mlm=False),\n",
|
580 |
+
")\n",
|
581 |
+
"model.config.use_cache = False # silence the warnings. Please re-enable for inference!\n",
|
582 |
+
"trainer.train()"
|
583 |
+
]
|
584 |
+
},
|
585 |
+
{
|
586 |
+
"cell_type": "code",
|
587 |
+
"execution_count": null,
|
588 |
+
"metadata": {
|
589 |
+
"id": "05iBmtP6X3Mq"
|
590 |
+
},
|
591 |
+
"outputs": [],
|
592 |
+
"source": []
|
593 |
+
}
|
594 |
+
],
|
595 |
+
"metadata": {
|
596 |
+
"accelerator": "GPU",
|
597 |
+
"colab": {
|
598 |
+
"gpuType": "T4",
|
599 |
+
"provenance": []
|
600 |
+
},
|
601 |
+
"gpuClass": "standard",
|
602 |
+
"kernelspec": {
|
603 |
+
"display_name": "Python 3 (ipykernel)",
|
604 |
+
"language": "python",
|
605 |
+
"name": "python3"
|
606 |
+
},
|
607 |
+
"language_info": {
|
608 |
+
"codemirror_mode": {
|
609 |
+
"name": "ipython",
|
610 |
+
"version": 3
|
611 |
+
},
|
612 |
+
"file_extension": ".py",
|
613 |
+
"mimetype": "text/x-python",
|
614 |
+
"name": "python",
|
615 |
+
"nbconvert_exporter": "python",
|
616 |
+
"pygments_lexer": "ipython3",
|
617 |
+
"version": "3.10.12"
|
618 |
+
}
|
619 |
+
},
|
620 |
+
"nbformat": 4,
|
621 |
+
"nbformat_minor": 4
|
622 |
+
}
|
.ipynb_checkpoints/test_ft-checkpoint.py
ADDED
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments
|
4 |
+
import transformers
|
5 |
+
from peft import LoraConfig, get_peft_model
|
6 |
+
from datasets import load_dataset
|
7 |
+
from transformers.trainer_callback import TrainerCallback
|
8 |
+
import os
|
9 |
+
import random
|
10 |
+
import subprocess
|
11 |
+
from huggingface_hub import HfApi, hf_hub_download
|
12 |
+
|
13 |
+
def generate_mmlu_slurm(model_path, hub_model_id, output_dir, num_gpus=1):
|
14 |
+
model_short_name = model_path.split('/')[-1]
|
15 |
+
filename = f"run_mmlu_{model_short_name}.sbatch"
|
16 |
+
|
17 |
+
port = random.randint(10000, 65535)
|
18 |
+
|
19 |
+
content = f"""#!/bin/bash
|
20 |
+
#SBATCH --nodes=1
|
21 |
+
#SBATCH --gpus-per-node={num_gpus}
|
22 |
+
#SBATCH --time=24:00:00
|
23 |
+
#SBATCH --job-name={port}_mmlu_{model_short_name}
|
24 |
+
#SBATCH --mail-user=mailto:[email protected]
|
25 |
+
#SBATCH --mail-type=ALL
|
26 |
+
|
27 |
+
source /opt/rh/devtoolset-10/enable
|
28 |
+
source /data/davis_brown/miniconda3/bin/activate
|
29 |
+
conda init
|
30 |
+
conda activate quip
|
31 |
+
|
32 |
+
CUDA_VISIBLE_DEVICES=0 lm_eval \\
|
33 |
+
--model hf \\
|
34 |
+
--model_args pretrained={model_path},parallelize=True,peft={hub_model_id} \\
|
35 |
+
--tasks mmlu \\
|
36 |
+
--device cuda:0 \\
|
37 |
+
--batch_size 8 \\
|
38 |
+
--output_path={output_dir}/{hub_model_id} \\
|
39 |
+
--num_fewshot 5
|
40 |
+
"""
|
41 |
+
|
42 |
+
with open(filename, 'w') as f:
|
43 |
+
f.write(content)
|
44 |
+
|
45 |
+
print(f"Generated MMLU evaluation SLURM script: {filename}")
|
46 |
+
return filename
|
47 |
+
|
48 |
+
def launch_mmlu_evaluation(model_path, hub_model_id, output_dir):
|
49 |
+
slurm_script = generate_mmlu_slurm(model_path, hub_model_id, output_dir)
|
50 |
+
try:
|
51 |
+
subprocess.run(["sbatch", slurm_script], check=True)
|
52 |
+
print(f"Submitted MMLU evaluation job: {slurm_script}")
|
53 |
+
except subprocess.CalledProcessError as e:
|
54 |
+
print(f"Failed to submit MMLU evaluation job: {e}")
|
55 |
+
|
56 |
+
# Custom callback to push to Hub
|
57 |
+
class PushToHubCallback(TrainerCallback):
|
58 |
+
def __init__(self, trainer, push_frequency):
|
59 |
+
self.trainer = trainer
|
60 |
+
self.push_frequency = push_frequency
|
61 |
+
|
62 |
+
def on_step_end(self, args, state, control, **kwargs):
|
63 |
+
if state.global_step % self.push_frequency == 0:
|
64 |
+
self.trainer.save_model()
|
65 |
+
self.trainer.push_to_hub(
|
66 |
+
commit_message=f"Training in progress - Step {state.global_step}"
|
67 |
+
)
|
68 |
+
def main(args):
|
69 |
+
if args.only_mmlu:
|
70 |
+
launch_mmlu_evaluation(args.model_id, args.hub_model_id, args.output_dir)
|
71 |
+
return
|
72 |
+
|
73 |
+
model_id = args.model_id
|
74 |
+
output_dir = args.output_dir
|
75 |
+
hub_model_id = args.hub_model_id
|
76 |
+
|
77 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
78 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", torch_dtype="auto", low_cpu_mem_usage=True)
|
79 |
+
|
80 |
+
target_modules = ['q_proj','k_proj','v_proj','o_proj','gate_proj','down_proj','up_proj', ]# 'lm_head']
|
81 |
+
|
82 |
+
config = LoraConfig(
|
83 |
+
r=args.lora_rank,
|
84 |
+
lora_alpha=args.lora_rank,
|
85 |
+
target_modules=target_modules,
|
86 |
+
lora_dropout=0.05,
|
87 |
+
bias="none",
|
88 |
+
task_type="CAUSAL_LM",
|
89 |
+
use_rslora=True
|
90 |
+
)
|
91 |
+
|
92 |
+
model = get_peft_model(model, config)
|
93 |
+
model.print_trainable_parameters()
|
94 |
+
model.enable_input_require_grads()
|
95 |
+
|
96 |
+
# data = load_dataset("togethercomputer/RedPajama-Data-1T-Sample")
|
97 |
+
data = load_dataset("open-web-math/open-web-math")
|
98 |
+
|
99 |
+
|
100 |
+
max_seq_length = args.max_seq_length
|
101 |
+
tokenizer.pad_token = tokenizer.eos_token
|
102 |
+
tokenizer.model_max_length = max_seq_length
|
103 |
+
|
104 |
+
def preprocess_function(examples):
|
105 |
+
return tokenizer(examples["text"], truncation=True, max_length=max_seq_length, padding="max_length")
|
106 |
+
|
107 |
+
processed_dataset = data["train"].map(preprocess_function, batched=True)
|
108 |
+
|
109 |
+
tokenizer.pad_token = tokenizer.eos_token
|
110 |
+
torch.cuda.empty_cache()
|
111 |
+
trainer = transformers.Trainer(
|
112 |
+
model=model,
|
113 |
+
train_dataset=processed_dataset,
|
114 |
+
args=TrainingArguments(
|
115 |
+
per_device_train_batch_size=args.batch_size,
|
116 |
+
gradient_accumulation_steps=args.gradient_accumulation_steps,
|
117 |
+
gradient_checkpointing=True,
|
118 |
+
warmup_steps=200,
|
119 |
+
max_steps=args.max_steps,
|
120 |
+
learning_rate=2e-4,
|
121 |
+
bf16=True,
|
122 |
+
logging_steps=25,
|
123 |
+
output_dir=output_dir,
|
124 |
+
optim="adamw_bnb_8bit",
|
125 |
+
logging_first_step=True,
|
126 |
+
push_to_hub=True,
|
127 |
+
hub_model_id=hub_model_id,
|
128 |
+
),
|
129 |
+
data_collator=transformers.DataCollatorForLanguageModeling(tokenizer, mlm=False),
|
130 |
+
)
|
131 |
+
model.config.use_cache = False
|
132 |
+
|
133 |
+
push_frequency = 100
|
134 |
+
trainer.add_callback(PushToHubCallback(trainer, push_frequency, hub_model_id))
|
135 |
+
|
136 |
+
trainer.train()
|
137 |
+
|
138 |
+
final_commit_hash = trainer.push_to_hub("Training complete")
|
139 |
+
print(f"Training complete. Final commit hash: {final_commit_hash}")
|
140 |
+
|
141 |
+
# MMLU Evaluation
|
142 |
+
if args.run_mmlu:
|
143 |
+
launch_mmlu_evaluation(model_id, hub_model_id, output_dir)
|
144 |
+
|
145 |
+
if __name__ == "__main__":
|
146 |
+
parser = argparse.ArgumentParser(description="Fine-tune a language model and/or run MMLU evaluation")
|
147 |
+
parser.add_argument("--model_id", type=str, default="ISTA-DASLab/Meta-Llama-3-8B-Instruct-AQLM-2Bit-1x16",
|
148 |
+
help="Model ID to fine-tune or evaluate")
|
149 |
+
parser.add_argument("--max_seq_length", type=int, default=2048, help="Maximum sequence length")
|
150 |
+
parser.add_argument("--output_dir", type=str, required=True, help="Output directory for checkpoints and results")
|
151 |
+
parser.add_argument("--hub_model_id", type=str,
|
152 |
+
default="davisrbr/ISTA-DASLab-Meta-Llama-3-8B-Instruct-AQLM-2Bit-1x16-hf-100000_r8_cont",
|
153 |
+
help="Hub model ID for pushing or LoRA weights")
|
154 |
+
parser.add_argument("--batch_size", type=int, default=1, help="Per-device batch size")
|
155 |
+
parser.add_argument("--gradient_accumulation_steps", type=int, default=8, help="Gradient accumulation steps")
|
156 |
+
parser.add_argument("--max_steps", type=int, default=50000, help="Maximum number of training steps")
|
157 |
+
parser.add_argument("--run_mmlu", action="store_true", help="Run MMLU evaluation after training")
|
158 |
+
parser.add_argument("--lora_rank", type=int, default=8, help="Rank of LoRA adaptation")
|
159 |
+
parser.add_argument("--only_mmlu", action="store_true", help="Only run MMLU evaluation without training")
|
160 |
+
parser.add_argument("--launch_slurm", action="store_true", help="Launch the entire script as a SLURM job")
|
161 |
+
parser.add_argument("--num_gpus", type=int, default=4, help="Number of GPUs to use for training")
|
162 |
+
parser.add_argument("--commit_hash", type=str, help="Specific commit hash to evaluate (for MMLU only)")
|
163 |
+
|
164 |
+
args = parser.parse_args()
|
165 |
+
main(args)
|
=0.27.0
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Requirement already satisfied: accelerate in /usr/local/lib/python3.10/dist-packages (0.33.0)
|
2 |
+
Requirement already satisfied: numpy<2.0.0,>=1.17 in /usr/local/lib/python3.10/dist-packages (from accelerate) (1.24.1)
|
3 |
+
Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from accelerate) (23.2)
|
4 |
+
Requirement already satisfied: psutil in /usr/local/lib/python3.10/dist-packages (from accelerate) (5.9.6)
|
5 |
+
Requirement already satisfied: pyyaml in /usr/local/lib/python3.10/dist-packages (from accelerate) (6.0.1)
|
6 |
+
Requirement already satisfied: torch>=1.10.0 in /usr/local/lib/python3.10/dist-packages (from accelerate) (2.4.0)
|
7 |
+
Requirement already satisfied: huggingface-hub>=0.21.0 in /usr/local/lib/python3.10/dist-packages (from accelerate) (0.24.5)
|
8 |
+
Requirement already satisfied: safetensors>=0.3.1 in /usr/local/lib/python3.10/dist-packages (from accelerate) (0.4.4)
|
9 |
+
Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.21.0->accelerate) (3.9.0)
|
10 |
+
Requirement already satisfied: fsspec>=2023.5.0 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.21.0->accelerate) (2024.6.1)
|
11 |
+
Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.21.0->accelerate) (2.31.0)
|
12 |
+
Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.21.0->accelerate) (4.66.5)
|
13 |
+
Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.21.0->accelerate) (4.12.2)
|
14 |
+
Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (1.12)
|
15 |
+
Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (3.0)
|
16 |
+
Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (3.1.2)
|
17 |
+
Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (12.1.105)
|
18 |
+
Requirement already satisfied: nvidia-cuda-runtime-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (12.1.105)
|
19 |
+
Requirement already satisfied: nvidia-cuda-cupti-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (12.1.105)
|
20 |
+
Requirement already satisfied: nvidia-cudnn-cu12==9.1.0.70 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (9.1.0.70)
|
21 |
+
Requirement already satisfied: nvidia-cublas-cu12==12.1.3.1 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (12.1.3.1)
|
22 |
+
Requirement already satisfied: nvidia-cufft-cu12==11.0.2.54 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (11.0.2.54)
|
23 |
+
Requirement already satisfied: nvidia-curand-cu12==10.3.2.106 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (10.3.2.106)
|
24 |
+
Requirement already satisfied: nvidia-cusolver-cu12==11.4.5.107 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (11.4.5.107)
|
25 |
+
Requirement already satisfied: nvidia-cusparse-cu12==12.1.0.106 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (12.1.0.106)
|
26 |
+
Requirement already satisfied: nvidia-nccl-cu12==2.20.5 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (2.20.5)
|
27 |
+
Requirement already satisfied: nvidia-nvtx-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (12.1.105)
|
28 |
+
Requirement already satisfied: triton==3.0.0 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (3.0.0)
|
29 |
+
Requirement already satisfied: nvidia-nvjitlink-cu12 in /usr/local/lib/python3.10/dist-packages (from nvidia-cusolver-cu12==11.4.5.107->torch>=1.10.0->accelerate) (12.6.20)
|
30 |
+
Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch>=1.10.0->accelerate) (2.1.2)
|
31 |
+
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.21.0->accelerate) (2.1.1)
|
32 |
+
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.21.0->accelerate) (3.4)
|
33 |
+
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.21.0->accelerate) (1.26.13)
|
34 |
+
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.21.0->accelerate) (2022.12.7)
|
35 |
+
Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.10/dist-packages (from sympy->torch>=1.10.0->accelerate) (1.3.0)
|
=1.1.0
ADDED
The diff for this file is too large to render.
See raw diff
|
|
README.md
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
base_model: ISTA-DASLab/Meta-Llama-3-8B-Instruct-AQLM-2Bit-1x16
|
3 |
+
library_name: peft
|
4 |
+
tags:
|
5 |
+
- generated_from_trainer
|
6 |
+
model-index:
|
7 |
+
- name: math-lora
|
8 |
+
results: []
|
9 |
+
---
|
10 |
+
|
11 |
+
<!-- This model card has been generated automatically according to the information the Trainer had access to. You
|
12 |
+
should probably proofread and complete it, then remove this comment. -->
|
13 |
+
|
14 |
+
# math-lora
|
15 |
+
|
16 |
+
This model is a fine-tuned version of [ISTA-DASLab/Meta-Llama-3-8B-Instruct-AQLM-2Bit-1x16](https://huggingface.co/ISTA-DASLab/Meta-Llama-3-8B-Instruct-AQLM-2Bit-1x16) on the None dataset.
|
17 |
+
|
18 |
+
## Model description
|
19 |
+
|
20 |
+
More information needed
|
21 |
+
|
22 |
+
## Intended uses & limitations
|
23 |
+
|
24 |
+
More information needed
|
25 |
+
|
26 |
+
## Training and evaluation data
|
27 |
+
|
28 |
+
More information needed
|
29 |
+
|
30 |
+
## Training procedure
|
31 |
+
|
32 |
+
### Training hyperparameters
|
33 |
+
|
34 |
+
The following hyperparameters were used during training:
|
35 |
+
- learning_rate: 0.0002
|
36 |
+
- train_batch_size: 4
|
37 |
+
- eval_batch_size: 8
|
38 |
+
- seed: 42
|
39 |
+
- gradient_accumulation_steps: 8
|
40 |
+
- total_train_batch_size: 32
|
41 |
+
- optimizer: Adam with betas=(0.9,0.999) and epsilon=1e-08
|
42 |
+
- lr_scheduler_type: linear
|
43 |
+
- lr_scheduler_warmup_steps: 200
|
44 |
+
- training_steps: 10000
|
45 |
+
|
46 |
+
### Framework versions
|
47 |
+
|
48 |
+
- PEFT 0.12.1.dev0
|
49 |
+
- Transformers 4.45.0.dev0
|
50 |
+
- Pytorch 2.4.0+cu121
|
51 |
+
- Datasets 2.21.0
|
52 |
+
- Tokenizers 0.19.1
|
adapter_config.json
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"alpha_pattern": {},
|
3 |
+
"auto_mapping": null,
|
4 |
+
"base_model_name_or_path": "ISTA-DASLab/Meta-Llama-3-8B-Instruct-AQLM-2Bit-1x16",
|
5 |
+
"bias": "none",
|
6 |
+
"fan_in_fan_out": false,
|
7 |
+
"inference_mode": true,
|
8 |
+
"init_lora_weights": true,
|
9 |
+
"layer_replication": null,
|
10 |
+
"layers_pattern": null,
|
11 |
+
"layers_to_transform": null,
|
12 |
+
"loftq_config": {},
|
13 |
+
"lora_alpha": 32,
|
14 |
+
"lora_dropout": 0.05,
|
15 |
+
"megatron_config": null,
|
16 |
+
"megatron_core": "megatron.core",
|
17 |
+
"modules_to_save": null,
|
18 |
+
"peft_type": "LORA",
|
19 |
+
"r": 16,
|
20 |
+
"rank_pattern": {},
|
21 |
+
"revision": null,
|
22 |
+
"target_modules": [
|
23 |
+
"gate_proj",
|
24 |
+
"down_proj",
|
25 |
+
"k_proj",
|
26 |
+
"o_proj",
|
27 |
+
"up_proj",
|
28 |
+
"v_proj",
|
29 |
+
"q_proj"
|
30 |
+
],
|
31 |
+
"task_type": "CAUSAL_LM",
|
32 |
+
"use_dora": false,
|
33 |
+
"use_rslora": false
|
34 |
+
}
|
adapter_model.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:82ba54afcc89ac3e9bbbae5baec7816b8511b7f56bc84988f1bdf274f598e5bd
|
3 |
+
size 167832240
|
aqlm_2bit_training.ipynb
ADDED
@@ -0,0 +1,622 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"metadata": {
|
6 |
+
"id": "XIyP_0r6zuVc"
|
7 |
+
},
|
8 |
+
"source": [
|
9 |
+
"# Training Large Language Models in 2bit with `aqlm`, `transformers` and `PEFT`\n",
|
10 |
+
"\n",
|
11 |
+
"<a target=\"_blank\" href=\"https://colab.research.google.com/github/Vahe1994/AQLM/blob/main/notebooks/aqlm_2bit_training.ipynb\">\n",
|
12 |
+
" <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/>\n",
|
13 |
+
"</a>\n",
|
14 |
+
"\n",
|
15 |
+
"Welcome to this notebook that goes through the recent `aqlm` integration that introduces minimal performance degradation 2bit quantization techniques.\n",
|
16 |
+
"\n",
|
17 |
+
"In this notebook, we will learn how to load a large model in 2bit (`Mixtral-8x7b`) and train it using Google Colab and PEFT library from Hugging Face 🤗.\n"
|
18 |
+
]
|
19 |
+
},
|
20 |
+
{
|
21 |
+
"cell_type": "markdown",
|
22 |
+
"metadata": {
|
23 |
+
"id": "A_VgSpl4Dsr3"
|
24 |
+
},
|
25 |
+
"source": [
|
26 |
+
"**Install the `aqlm` library**\n",
|
27 |
+
"- It's the only extra dependency to run AQLM models.\n",
|
28 |
+
"- Add `[gpu]` to install the required CUDA specific dependencies.\n",
|
29 |
+
"- Install the latest `accelerate` and `transformers` releases to properly support it."
|
30 |
+
]
|
31 |
+
},
|
32 |
+
{
|
33 |
+
"cell_type": "code",
|
34 |
+
"execution_count": null,
|
35 |
+
"metadata": {
|
36 |
+
"id": "FuXIFTFapAMI"
|
37 |
+
},
|
38 |
+
"outputs": [],
|
39 |
+
"source": [
|
40 |
+
"%%capture\n",
|
41 |
+
"!pip install aqlm[gpu]>=1.1.0\n",
|
42 |
+
"!pip install git+https://github.com/huggingface/peft.git@main\n",
|
43 |
+
"!pip install accelerate>=0.27.0\n",
|
44 |
+
"!pip install git+https://github.com/huggingface/transformers.git@main\n",
|
45 |
+
"!pip install datasets\n",
|
46 |
+
"!pip install bitsandbytes\n",
|
47 |
+
"# for 8-bit optimizer only"
|
48 |
+
]
|
49 |
+
},
|
50 |
+
{
|
51 |
+
"cell_type": "markdown",
|
52 |
+
"metadata": {
|
53 |
+
"id": "MJ-5idQwzvg-"
|
54 |
+
},
|
55 |
+
"source": [
|
56 |
+
"First let's load the model we are going to use - `Mixtral-8x7b`! Note that the model itself is around 50GB in half precision"
|
57 |
+
]
|
58 |
+
},
|
59 |
+
{
|
60 |
+
"cell_type": "code",
|
61 |
+
"execution_count": 23,
|
62 |
+
"metadata": {
|
63 |
+
"id": "E0Nl5mWL0k2T"
|
64 |
+
},
|
65 |
+
"outputs": [],
|
66 |
+
"source": [
|
67 |
+
"import torch\n",
|
68 |
+
"from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig\n",
|
69 |
+
"\n",
|
70 |
+
"model_id = \"ISTA-DASLab/Meta-Llama-3-8B-Instruct-AQLM-2Bit-1x16\"\n",
|
71 |
+
"\n",
|
72 |
+
"tokenizer = AutoTokenizer.from_pretrained(model_id)\n",
|
73 |
+
"model = AutoModelForCausalLM.from_pretrained(model_id, device_map=\"auto\", torch_dtype=\"bfloat16\", low_cpu_mem_usage=True)"
|
74 |
+
]
|
75 |
+
},
|
76 |
+
{
|
77 |
+
"cell_type": "markdown",
|
78 |
+
"metadata": {
|
79 |
+
"id": "Mp2gMi1ZzGET"
|
80 |
+
},
|
81 |
+
"source": [
|
82 |
+
"**Add LoRA**\n",
|
83 |
+
"\n",
|
84 |
+
"To alter model's behavior, we have to make it trainable. We can do that by addind a small set of trainable parameters on top of the untrainable quantized ones."
|
85 |
+
]
|
86 |
+
},
|
87 |
+
{
|
88 |
+
"cell_type": "code",
|
89 |
+
"execution_count": 24,
|
90 |
+
"metadata": {
|
91 |
+
"colab": {
|
92 |
+
"base_uri": "https://localhost:8080/"
|
93 |
+
},
|
94 |
+
"id": "Ybeyl20n3dYH",
|
95 |
+
"outputId": "0efda156-4886-4718-9877-e93a17dc02d2"
|
96 |
+
},
|
97 |
+
"outputs": [
|
98 |
+
{
|
99 |
+
"name": "stdout",
|
100 |
+
"output_type": "stream",
|
101 |
+
"text": [
|
102 |
+
"trainable params: 41,943,040 || all params: 2,084,114,432 || trainable%: 2.0125\n"
|
103 |
+
]
|
104 |
+
}
|
105 |
+
],
|
106 |
+
"source": [
|
107 |
+
"from peft import LoraConfig, get_peft_model\n",
|
108 |
+
"\n",
|
109 |
+
"config = LoraConfig(\n",
|
110 |
+
" r=16,\n",
|
111 |
+
" lora_alpha=32,\n",
|
112 |
+
" target_modules=['q_proj','k_proj','v_proj','o_proj','gate_proj','down_proj','up_proj', ],\n",
|
113 |
+
" lora_dropout=0.05,\n",
|
114 |
+
" bias=\"none\",\n",
|
115 |
+
" task_type=\"CAUSAL_LM\"\n",
|
116 |
+
")\n",
|
117 |
+
"\n",
|
118 |
+
"model = get_peft_model(model, config)\n",
|
119 |
+
"model.print_trainable_parameters()\n",
|
120 |
+
"model.enable_input_require_grads() # it's needed for gradient checkpointing"
|
121 |
+
]
|
122 |
+
},
|
123 |
+
{
|
124 |
+
"cell_type": "markdown",
|
125 |
+
"metadata": {
|
126 |
+
"id": "4xSPH1D_Wv9x"
|
127 |
+
},
|
128 |
+
"source": [
|
129 |
+
"Here we add a trainable adapter ontop of every `q_prok`, `k_proj` and `o_proj` linear layer."
|
130 |
+
]
|
131 |
+
},
|
132 |
+
{
|
133 |
+
"cell_type": "markdown",
|
134 |
+
"metadata": {
|
135 |
+
"id": "FCc64bfnmd3j"
|
136 |
+
},
|
137 |
+
"source": [
|
138 |
+
"**Loading a dataset**\n",
|
139 |
+
"\n",
|
140 |
+
"Let's load a common dataset, english quotes, to fine tune our model on famous quotes."
|
141 |
+
]
|
142 |
+
},
|
143 |
+
{
|
144 |
+
"cell_type": "code",
|
145 |
+
"execution_count": 13,
|
146 |
+
"metadata": {
|
147 |
+
"id": "s6f4z8EYmcJ6"
|
148 |
+
},
|
149 |
+
"outputs": [
|
150 |
+
{
|
151 |
+
"data": {
|
152 |
+
"application/vnd.jupyter.widget-view+json": {
|
153 |
+
"model_id": "9ef07f1bc62e4887817a81d4a3e15da1",
|
154 |
+
"version_major": 2,
|
155 |
+
"version_minor": 0
|
156 |
+
},
|
157 |
+
"text/plain": [
|
158 |
+
"Resolving data files: 0%| | 0/114 [00:00<?, ?it/s]"
|
159 |
+
]
|
160 |
+
},
|
161 |
+
"metadata": {},
|
162 |
+
"output_type": "display_data"
|
163 |
+
},
|
164 |
+
{
|
165 |
+
"name": "stdout",
|
166 |
+
"output_type": "stream",
|
167 |
+
"text": [
|
168 |
+
"Loaded dataset with 100000 examples\n"
|
169 |
+
]
|
170 |
+
},
|
171 |
+
{
|
172 |
+
"data": {
|
173 |
+
"application/vnd.jupyter.widget-view+json": {
|
174 |
+
"model_id": "560c7be6397c4e3aac2318d97f1f8f86",
|
175 |
+
"version_major": 2,
|
176 |
+
"version_minor": 0
|
177 |
+
},
|
178 |
+
"text/plain": [
|
179 |
+
"tokenizer_config.json: 0%| | 0.00/26.0 [00:00<?, ?B/s]"
|
180 |
+
]
|
181 |
+
},
|
182 |
+
"metadata": {},
|
183 |
+
"output_type": "display_data"
|
184 |
+
},
|
185 |
+
{
|
186 |
+
"data": {
|
187 |
+
"application/vnd.jupyter.widget-view+json": {
|
188 |
+
"model_id": "b667958a3b3d4529b77baf5e5bc9c259",
|
189 |
+
"version_major": 2,
|
190 |
+
"version_minor": 0
|
191 |
+
},
|
192 |
+
"text/plain": [
|
193 |
+
"config.json: 0%| | 0.00/665 [00:00<?, ?B/s]"
|
194 |
+
]
|
195 |
+
},
|
196 |
+
"metadata": {},
|
197 |
+
"output_type": "display_data"
|
198 |
+
},
|
199 |
+
{
|
200 |
+
"data": {
|
201 |
+
"application/vnd.jupyter.widget-view+json": {
|
202 |
+
"model_id": "10359f3b8d974be49da2d3fd87f89576",
|
203 |
+
"version_major": 2,
|
204 |
+
"version_minor": 0
|
205 |
+
},
|
206 |
+
"text/plain": [
|
207 |
+
"vocab.json: 0%| | 0.00/1.04M [00:00<?, ?B/s]"
|
208 |
+
]
|
209 |
+
},
|
210 |
+
"metadata": {},
|
211 |
+
"output_type": "display_data"
|
212 |
+
},
|
213 |
+
{
|
214 |
+
"data": {
|
215 |
+
"application/vnd.jupyter.widget-view+json": {
|
216 |
+
"model_id": "97835946d4a44460bc1bd48276b8d3d0",
|
217 |
+
"version_major": 2,
|
218 |
+
"version_minor": 0
|
219 |
+
},
|
220 |
+
"text/plain": [
|
221 |
+
"merges.txt: 0%| | 0.00/456k [00:00<?, ?B/s]"
|
222 |
+
]
|
223 |
+
},
|
224 |
+
"metadata": {},
|
225 |
+
"output_type": "display_data"
|
226 |
+
},
|
227 |
+
{
|
228 |
+
"data": {
|
229 |
+
"application/vnd.jupyter.widget-view+json": {
|
230 |
+
"model_id": "ed37faeff8914b369649cb514981991d",
|
231 |
+
"version_major": 2,
|
232 |
+
"version_minor": 0
|
233 |
+
},
|
234 |
+
"text/plain": [
|
235 |
+
"tokenizer.json: 0%| | 0.00/1.36M [00:00<?, ?B/s]"
|
236 |
+
]
|
237 |
+
},
|
238 |
+
"metadata": {},
|
239 |
+
"output_type": "display_data"
|
240 |
+
},
|
241 |
+
{
|
242 |
+
"name": "stderr",
|
243 |
+
"output_type": "stream",
|
244 |
+
"text": [
|
245 |
+
"/usr/local/lib/python3.10/dist-packages/transformers/tokenization_utils_base.py:1601: FutureWarning: `clean_up_tokenization_spaces` was not set. It will be set to `True` by default. This behavior will be deprecated in transformers v4.45, and will be then set to `False` by default. For more details check this issue: https://github.com/huggingface/transformers/issues/31884\n",
|
246 |
+
" warnings.warn(\n"
|
247 |
+
]
|
248 |
+
},
|
249 |
+
{
|
250 |
+
"data": {
|
251 |
+
"application/vnd.jupyter.widget-view+json": {
|
252 |
+
"model_id": "7a56a1781f3347f8a056a18dc24ea7a9",
|
253 |
+
"version_major": 2,
|
254 |
+
"version_minor": 0
|
255 |
+
},
|
256 |
+
"text/plain": [
|
257 |
+
"Map: 0%| | 0/100000 [00:00<?, ? examples/s]"
|
258 |
+
]
|
259 |
+
},
|
260 |
+
"metadata": {},
|
261 |
+
"output_type": "display_data"
|
262 |
+
},
|
263 |
+
{
|
264 |
+
"name": "stdout",
|
265 |
+
"output_type": "stream",
|
266 |
+
"text": [
|
267 |
+
"Processed dataset has 100000 examples\n",
|
268 |
+
"Features: {'input_ids': Sequence(feature=Value(dtype='int32', id=None), length=-1, id=None), 'attention_mask': Sequence(feature=Value(dtype='int8', id=None), length=-1, id=None)}\n"
|
269 |
+
]
|
270 |
+
}
|
271 |
+
],
|
272 |
+
"source": [
|
273 |
+
"from datasets import load_dataset, Dataset\n",
|
274 |
+
"import itertools\n",
|
275 |
+
"from transformers import AutoTokenizer\n",
|
276 |
+
"\n",
|
277 |
+
"# Load the dataset in streaming mode\n",
|
278 |
+
"ds = load_dataset(\"open-web-math/open-web-math\", split=\"train\", streaming=True)\n",
|
279 |
+
"\n",
|
280 |
+
"# Define the number of examples you want to load\n",
|
281 |
+
"num_examples = 100000 # Adjust this number as needed\n",
|
282 |
+
"\n",
|
283 |
+
"# Create a subset by taking the first num_examples\n",
|
284 |
+
"subset = list(itertools.islice(ds, num_examples))\n",
|
285 |
+
"\n",
|
286 |
+
"# Convert the subset to a Dataset object\n",
|
287 |
+
"data = Dataset.from_list(subset)\n",
|
288 |
+
"print(f\"Loaded dataset with {len(data)} examples\")\n",
|
289 |
+
"\n",
|
290 |
+
"# Initialize tokenizer (replace 'gpt2' with your specific model if different)\n",
|
291 |
+
"tokenizer = AutoTokenizer.from_pretrained('gpt2')\n",
|
292 |
+
"\n",
|
293 |
+
"max_seq_length = 2048\n",
|
294 |
+
"tokenizer.pad_token = tokenizer.eos_token\n",
|
295 |
+
"tokenizer.model_max_length = max_seq_length\n",
|
296 |
+
"\n",
|
297 |
+
"def preprocess_function(examples):\n",
|
298 |
+
" # Join the list of strings into a single string\n",
|
299 |
+
" texts = [\" \".join(text) for text in examples[\"text\"]]\n",
|
300 |
+
" return tokenizer(texts, truncation=True, max_length=max_seq_length, padding=\"max_length\")\n",
|
301 |
+
"\n",
|
302 |
+
"# Process the dataset\n",
|
303 |
+
"processed_dataset = data.map(preprocess_function, batched=True, remove_columns=data.column_names)\n",
|
304 |
+
"\n",
|
305 |
+
"print(f\"Processed dataset has {len(processed_dataset)} examples\")\n",
|
306 |
+
"print(f\"Features: {processed_dataset.features}\")\n"
|
307 |
+
]
|
308 |
+
},
|
309 |
+
{
|
310 |
+
"cell_type": "code",
|
311 |
+
"execution_count": 15,
|
312 |
+
"metadata": {},
|
313 |
+
"outputs": [],
|
314 |
+
"source": [
|
315 |
+
"import argparse\n",
|
316 |
+
"import torch\n",
|
317 |
+
"from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments\n",
|
318 |
+
"import transformers\n",
|
319 |
+
"from peft import LoraConfig, get_peft_model\n",
|
320 |
+
"from datasets import load_dataset\n",
|
321 |
+
"from transformers.trainer_callback import TrainerCallback\n",
|
322 |
+
"import os\n",
|
323 |
+
"import random\n",
|
324 |
+
"import subprocess\n",
|
325 |
+
"from huggingface_hub import HfApi, hf_hub_download\n",
|
326 |
+
"\n",
|
327 |
+
"\n",
|
328 |
+
"# Custom callback to push to Hub\n",
|
329 |
+
"class PushToHubCallback(TrainerCallback):\n",
|
330 |
+
" def __init__(self, trainer, push_frequency):\n",
|
331 |
+
" self.trainer = trainer\n",
|
332 |
+
" self.push_frequency = push_frequency\n",
|
333 |
+
"\n",
|
334 |
+
" def on_step_end(self, args, state, control, **kwargs):\n",
|
335 |
+
" if state.global_step % self.push_frequency == 0:\n",
|
336 |
+
" self.trainer.save_model()\n",
|
337 |
+
" self.trainer.push_to_hub(\n",
|
338 |
+
" commit_message=f\"Training in progress - Step {state.global_step}\"\n",
|
339 |
+
" )\n"
|
340 |
+
]
|
341 |
+
},
|
342 |
+
{
|
343 |
+
"cell_type": "code",
|
344 |
+
"execution_count": 21,
|
345 |
+
"metadata": {},
|
346 |
+
"outputs": [
|
347 |
+
{
|
348 |
+
"data": {
|
349 |
+
"application/vnd.jupyter.widget-view+json": {
|
350 |
+
"model_id": "74a0f8d448004c048d8b0608fa3a61fd",
|
351 |
+
"version_major": 2,
|
352 |
+
"version_minor": 0
|
353 |
+
},
|
354 |
+
"text/plain": [
|
355 |
+
"VBox(children=(HTML(value='<center> <img\\nsrc=https://huggingface.co/front/assets/huggingface_logo-noborder.sv…"
|
356 |
+
]
|
357 |
+
},
|
358 |
+
"metadata": {},
|
359 |
+
"output_type": "display_data"
|
360 |
+
}
|
361 |
+
],
|
362 |
+
"source": [
|
363 |
+
"from huggingface_hub import notebook_login\n",
|
364 |
+
"\n",
|
365 |
+
"notebook_login()"
|
366 |
+
]
|
367 |
+
},
|
368 |
+
{
|
369 |
+
"cell_type": "code",
|
370 |
+
"execution_count": null,
|
371 |
+
"metadata": {},
|
372 |
+
"outputs": [
|
373 |
+
{
|
374 |
+
"name": "stderr",
|
375 |
+
"output_type": "stream",
|
376 |
+
"text": [
|
377 |
+
"max_steps is given, it will override any value given in num_train_epochs\n",
|
378 |
+
"/usr/local/lib/python3.10/dist-packages/torch/_dynamo/eval_frame.py:600: UserWarning: torch.utils.checkpoint: the use_reentrant parameter should be passed explicitly. In version 2.4 we will raise an exception if use_reentrant is not passed. use_reentrant=False is recommended, but if you need to preserve the current default behavior, you can pass use_reentrant=True. Refer to docs for more details on the differences between the two variants.\n",
|
379 |
+
" return fn(*args, **kwargs)\n",
|
380 |
+
"/usr/local/lib/python3.10/dist-packages/torch/utils/checkpoint.py:295: FutureWarning: `torch.cpu.amp.autocast(args...)` is deprecated. Please use `torch.amp.autocast('cpu', args...)` instead.\n",
|
381 |
+
" with torch.enable_grad(), device_autocast_ctx, torch.cpu.amp.autocast(**ctx.cpu_autocast_kwargs): # type: ignore[attr-defined]\n"
|
382 |
+
]
|
383 |
+
},
|
384 |
+
{
|
385 |
+
"data": {
|
386 |
+
"text/html": [
|
387 |
+
"\n",
|
388 |
+
" <div>\n",
|
389 |
+
" \n",
|
390 |
+
" <progress value='22' max='10000' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
|
391 |
+
" [ 22/10000 09:16 < 77:05:02, 0.04 it/s, Epoch 0.01/4]\n",
|
392 |
+
" </div>\n",
|
393 |
+
" <table border=\"1\" class=\"dataframe\">\n",
|
394 |
+
" <thead>\n",
|
395 |
+
" <tr style=\"text-align: left;\">\n",
|
396 |
+
" <th>Step</th>\n",
|
397 |
+
" <th>Training Loss</th>\n",
|
398 |
+
" </tr>\n",
|
399 |
+
" </thead>\n",
|
400 |
+
" <tbody>\n",
|
401 |
+
" <tr>\n",
|
402 |
+
" <td>1</td>\n",
|
403 |
+
" <td>5.558500</td>\n",
|
404 |
+
" </tr>\n",
|
405 |
+
" </tbody>\n",
|
406 |
+
"</table><p>"
|
407 |
+
],
|
408 |
+
"text/plain": [
|
409 |
+
"<IPython.core.display.HTML object>"
|
410 |
+
]
|
411 |
+
},
|
412 |
+
"metadata": {},
|
413 |
+
"output_type": "display_data"
|
414 |
+
}
|
415 |
+
],
|
416 |
+
"source": [
|
417 |
+
"hub_model_id = \"davisrbr/math-lora\"\n",
|
418 |
+
"tokenizer.pad_token = tokenizer.eos_token\n",
|
419 |
+
"torch.cuda.empty_cache()\n",
|
420 |
+
"trainer = transformers.Trainer(\n",
|
421 |
+
" model=model,\n",
|
422 |
+
" train_dataset=processed_dataset,\n",
|
423 |
+
" args=TrainingArguments(\n",
|
424 |
+
" per_device_train_batch_size=4,\n",
|
425 |
+
" gradient_accumulation_steps=8,\n",
|
426 |
+
" gradient_checkpointing=True,\n",
|
427 |
+
" warmup_steps=200,\n",
|
428 |
+
" max_steps=10000,\n",
|
429 |
+
" learning_rate=2e-4,\n",
|
430 |
+
" bf16=True,\n",
|
431 |
+
" logging_steps=25,\n",
|
432 |
+
" output_dir=\".\",\n",
|
433 |
+
" optim=\"adamw_bnb_8bit\",\n",
|
434 |
+
" logging_first_step=True,\n",
|
435 |
+
" push_to_hub=True,\n",
|
436 |
+
" hub_model_id=hub_model_id,\n",
|
437 |
+
" ),\n",
|
438 |
+
" data_collator=transformers.DataCollatorForLanguageModeling(tokenizer, mlm=False),\n",
|
439 |
+
")\n",
|
440 |
+
"model.config.use_cache = False\n",
|
441 |
+
"\n",
|
442 |
+
"push_frequency = 100\n",
|
443 |
+
"trainer.add_callback(PushToHubCallback(trainer, push_frequency,))\n",
|
444 |
+
"\n",
|
445 |
+
"trainer.train()\n",
|
446 |
+
"\n",
|
447 |
+
"final_commit_hash = trainer.push_to_hub(\"Training complete\")\n",
|
448 |
+
"print(f\"Training complete. Final commit hash: {final_commit_hash}\")"
|
449 |
+
]
|
450 |
+
},
|
451 |
+
{
|
452 |
+
"cell_type": "markdown",
|
453 |
+
"metadata": {
|
454 |
+
"id": "_0MOtwf3zdZp"
|
455 |
+
},
|
456 |
+
"source": [
|
457 |
+
"Run the cell below to run the training! For the sake of the demo, we just ran it for few steps just to showcase how to use this integration with existing tools on the HF ecosystem."
|
458 |
+
]
|
459 |
+
},
|
460 |
+
{
|
461 |
+
"cell_type": "code",
|
462 |
+
"execution_count": null,
|
463 |
+
"metadata": {
|
464 |
+
"colab": {
|
465 |
+
"base_uri": "https://localhost:8080/",
|
466 |
+
"height": 481
|
467 |
+
},
|
468 |
+
"id": "jq0nX33BmfaC",
|
469 |
+
"outputId": "7f470980-c49e-4230-b947-ad43510f1bee"
|
470 |
+
},
|
471 |
+
"outputs": [
|
472 |
+
{
|
473 |
+
"name": "stderr",
|
474 |
+
"output_type": "stream",
|
475 |
+
"text": [
|
476 |
+
"/usr/local/lib/python3.10/dist-packages/torch/utils/checkpoint.py:460: UserWarning: torch.utils.checkpoint: please pass in use_reentrant=True or use_reentrant=False explicitly. The default value of use_reentrant will be updated to be False in the future. To maintain current behavior, pass use_reentrant=True. It is recommended that you use use_reentrant=False. Refer to docs for more details on the differences between the two variants.\n",
|
477 |
+
" warnings.warn(\n"
|
478 |
+
]
|
479 |
+
},
|
480 |
+
{
|
481 |
+
"data": {
|
482 |
+
"text/html": [
|
483 |
+
"\n",
|
484 |
+
" <div>\n",
|
485 |
+
" \n",
|
486 |
+
" <progress value='10' max='10' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
|
487 |
+
" [10/10 13:02, Epoch 0/1]\n",
|
488 |
+
" </div>\n",
|
489 |
+
" <table border=\"1\" class=\"dataframe\">\n",
|
490 |
+
" <thead>\n",
|
491 |
+
" <tr style=\"text-align: left;\">\n",
|
492 |
+
" <th>Step</th>\n",
|
493 |
+
" <th>Training Loss</th>\n",
|
494 |
+
" </tr>\n",
|
495 |
+
" </thead>\n",
|
496 |
+
" <tbody>\n",
|
497 |
+
" <tr>\n",
|
498 |
+
" <td>1</td>\n",
|
499 |
+
" <td>2.042200</td>\n",
|
500 |
+
" </tr>\n",
|
501 |
+
" <tr>\n",
|
502 |
+
" <td>2</td>\n",
|
503 |
+
" <td>1.293400</td>\n",
|
504 |
+
" </tr>\n",
|
505 |
+
" <tr>\n",
|
506 |
+
" <td>3</td>\n",
|
507 |
+
" <td>1.447500</td>\n",
|
508 |
+
" </tr>\n",
|
509 |
+
" <tr>\n",
|
510 |
+
" <td>4</td>\n",
|
511 |
+
" <td>1.433600</td>\n",
|
512 |
+
" </tr>\n",
|
513 |
+
" <tr>\n",
|
514 |
+
" <td>5</td>\n",
|
515 |
+
" <td>1.725900</td>\n",
|
516 |
+
" </tr>\n",
|
517 |
+
" <tr>\n",
|
518 |
+
" <td>6</td>\n",
|
519 |
+
" <td>1.506400</td>\n",
|
520 |
+
" </tr>\n",
|
521 |
+
" <tr>\n",
|
522 |
+
" <td>7</td>\n",
|
523 |
+
" <td>1.549600</td>\n",
|
524 |
+
" </tr>\n",
|
525 |
+
" <tr>\n",
|
526 |
+
" <td>8</td>\n",
|
527 |
+
" <td>1.038300</td>\n",
|
528 |
+
" </tr>\n",
|
529 |
+
" <tr>\n",
|
530 |
+
" <td>9</td>\n",
|
531 |
+
" <td>1.603300</td>\n",
|
532 |
+
" </tr>\n",
|
533 |
+
" <tr>\n",
|
534 |
+
" <td>10</td>\n",
|
535 |
+
" <td>1.676400</td>\n",
|
536 |
+
" </tr>\n",
|
537 |
+
" </tbody>\n",
|
538 |
+
"</table><p>"
|
539 |
+
],
|
540 |
+
"text/plain": [
|
541 |
+
"<IPython.core.display.HTML object>"
|
542 |
+
]
|
543 |
+
},
|
544 |
+
"metadata": {},
|
545 |
+
"output_type": "display_data"
|
546 |
+
},
|
547 |
+
{
|
548 |
+
"data": {
|
549 |
+
"text/plain": [
|
550 |
+
"TrainOutput(global_step=10, training_loss=1.531658697128296, metrics={'train_runtime': 861.2678, 'train_samples_per_second': 0.046, 'train_steps_per_second': 0.012, 'total_flos': 56809829376000.0, 'train_loss': 1.531658697128296, 'epoch': 0.02})"
|
551 |
+
]
|
552 |
+
},
|
553 |
+
"execution_count": 6,
|
554 |
+
"metadata": {},
|
555 |
+
"output_type": "execute_result"
|
556 |
+
}
|
557 |
+
],
|
558 |
+
"source": [
|
559 |
+
"import transformers\n",
|
560 |
+
"\n",
|
561 |
+
"tokenizer.pad_token = tokenizer.eos_token\n",
|
562 |
+
"\n",
|
563 |
+
"trainer = transformers.Trainer(\n",
|
564 |
+
" model=model,\n",
|
565 |
+
" train_dataset=data[\"train\"],\n",
|
566 |
+
" args=transformers.TrainingArguments(\n",
|
567 |
+
" per_device_train_batch_size=1,\n",
|
568 |
+
" gradient_accumulation_steps=8,\n",
|
569 |
+
" gradient_checkpointing=True,\n",
|
570 |
+
" warmup_steps=2,\n",
|
571 |
+
" max_steps=10,\n",
|
572 |
+
" learning_rate=2e-4,\n",
|
573 |
+
" fp16=True,\n",
|
574 |
+
" logging_steps=1,\n",
|
575 |
+
" output_dir=\"outputs\",\n",
|
576 |
+
" optim=\"adamw_bnb_8bit\",\n",
|
577 |
+
" logging_first_step=True,\n",
|
578 |
+
" ),\n",
|
579 |
+
" data_collator=transformers.DataCollatorForLanguageModeling(tokenizer, mlm=False),\n",
|
580 |
+
")\n",
|
581 |
+
"model.config.use_cache = False # silence the warnings. Please re-enable for inference!\n",
|
582 |
+
"trainer.train()"
|
583 |
+
]
|
584 |
+
},
|
585 |
+
{
|
586 |
+
"cell_type": "code",
|
587 |
+
"execution_count": null,
|
588 |
+
"metadata": {
|
589 |
+
"id": "05iBmtP6X3Mq"
|
590 |
+
},
|
591 |
+
"outputs": [],
|
592 |
+
"source": []
|
593 |
+
}
|
594 |
+
],
|
595 |
+
"metadata": {
|
596 |
+
"accelerator": "GPU",
|
597 |
+
"colab": {
|
598 |
+
"gpuType": "T4",
|
599 |
+
"provenance": []
|
600 |
+
},
|
601 |
+
"gpuClass": "standard",
|
602 |
+
"kernelspec": {
|
603 |
+
"display_name": "Python 3 (ipykernel)",
|
604 |
+
"language": "python",
|
605 |
+
"name": "python3"
|
606 |
+
},
|
607 |
+
"language_info": {
|
608 |
+
"codemirror_mode": {
|
609 |
+
"name": "ipython",
|
610 |
+
"version": 3
|
611 |
+
},
|
612 |
+
"file_extension": ".py",
|
613 |
+
"mimetype": "text/x-python",
|
614 |
+
"name": "python",
|
615 |
+
"nbconvert_exporter": "python",
|
616 |
+
"pygments_lexer": "ipython3",
|
617 |
+
"version": "3.10.12"
|
618 |
+
}
|
619 |
+
},
|
620 |
+
"nbformat": 4,
|
621 |
+
"nbformat_minor": 4
|
622 |
+
}
|
test_ft.py
ADDED
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments
|
4 |
+
import transformers
|
5 |
+
from peft import LoraConfig, get_peft_model
|
6 |
+
from datasets import load_dataset
|
7 |
+
from transformers.trainer_callback import TrainerCallback
|
8 |
+
import os
|
9 |
+
import random
|
10 |
+
import subprocess
|
11 |
+
from huggingface_hub import HfApi, hf_hub_download
|
12 |
+
|
13 |
+
def generate_mmlu_slurm(model_path, hub_model_id, output_dir, num_gpus=1):
|
14 |
+
model_short_name = model_path.split('/')[-1]
|
15 |
+
filename = f"run_mmlu_{model_short_name}.sbatch"
|
16 |
+
|
17 |
+
port = random.randint(10000, 65535)
|
18 |
+
|
19 |
+
content = f"""#!/bin/bash
|
20 |
+
#SBATCH --nodes=1
|
21 |
+
#SBATCH --gpus-per-node={num_gpus}
|
22 |
+
#SBATCH --time=24:00:00
|
23 |
+
#SBATCH --job-name={port}_mmlu_{model_short_name}
|
24 |
+
#SBATCH --mail-user=mailto:[email protected]
|
25 |
+
#SBATCH --mail-type=ALL
|
26 |
+
|
27 |
+
source /opt/rh/devtoolset-10/enable
|
28 |
+
source /data/davis_brown/miniconda3/bin/activate
|
29 |
+
conda init
|
30 |
+
conda activate quip
|
31 |
+
|
32 |
+
CUDA_VISIBLE_DEVICES=0 lm_eval \\
|
33 |
+
--model hf \\
|
34 |
+
--model_args pretrained={model_path},parallelize=True,peft={hub_model_id} \\
|
35 |
+
--tasks mmlu \\
|
36 |
+
--device cuda:0 \\
|
37 |
+
--batch_size 8 \\
|
38 |
+
--output_path={output_dir}/{hub_model_id} \\
|
39 |
+
--num_fewshot 5
|
40 |
+
"""
|
41 |
+
|
42 |
+
with open(filename, 'w') as f:
|
43 |
+
f.write(content)
|
44 |
+
|
45 |
+
print(f"Generated MMLU evaluation SLURM script: {filename}")
|
46 |
+
return filename
|
47 |
+
|
48 |
+
def launch_mmlu_evaluation(model_path, hub_model_id, output_dir):
|
49 |
+
slurm_script = generate_mmlu_slurm(model_path, hub_model_id, output_dir)
|
50 |
+
try:
|
51 |
+
subprocess.run(["sbatch", slurm_script], check=True)
|
52 |
+
print(f"Submitted MMLU evaluation job: {slurm_script}")
|
53 |
+
except subprocess.CalledProcessError as e:
|
54 |
+
print(f"Failed to submit MMLU evaluation job: {e}")
|
55 |
+
|
56 |
+
# Custom callback to push to Hub
|
57 |
+
class PushToHubCallback(TrainerCallback):
|
58 |
+
def __init__(self, trainer, push_frequency):
|
59 |
+
self.trainer = trainer
|
60 |
+
self.push_frequency = push_frequency
|
61 |
+
|
62 |
+
def on_step_end(self, args, state, control, **kwargs):
|
63 |
+
if state.global_step % self.push_frequency == 0:
|
64 |
+
self.trainer.save_model()
|
65 |
+
self.trainer.push_to_hub(
|
66 |
+
commit_message=f"Training in progress - Step {state.global_step}"
|
67 |
+
)
|
68 |
+
def main(args):
|
69 |
+
if args.only_mmlu:
|
70 |
+
launch_mmlu_evaluation(args.model_id, args.hub_model_id, args.output_dir)
|
71 |
+
return
|
72 |
+
|
73 |
+
model_id = args.model_id
|
74 |
+
output_dir = args.output_dir
|
75 |
+
hub_model_id = args.hub_model_id
|
76 |
+
|
77 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
78 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", torch_dtype="auto", low_cpu_mem_usage=True)
|
79 |
+
|
80 |
+
target_modules = ['q_proj','k_proj','v_proj','o_proj','gate_proj','down_proj','up_proj', ]# 'lm_head']
|
81 |
+
|
82 |
+
config = LoraConfig(
|
83 |
+
r=args.lora_rank,
|
84 |
+
lora_alpha=args.lora_rank,
|
85 |
+
target_modules=target_modules,
|
86 |
+
lora_dropout=0.05,
|
87 |
+
bias="none",
|
88 |
+
task_type="CAUSAL_LM",
|
89 |
+
use_rslora=True
|
90 |
+
)
|
91 |
+
|
92 |
+
model = get_peft_model(model, config)
|
93 |
+
model.print_trainable_parameters()
|
94 |
+
model.enable_input_require_grads()
|
95 |
+
|
96 |
+
# data = load_dataset("togethercomputer/RedPajama-Data-1T-Sample")
|
97 |
+
data = load_dataset("open-web-math/open-web-math")
|
98 |
+
|
99 |
+
|
100 |
+
max_seq_length = args.max_seq_length
|
101 |
+
tokenizer.pad_token = tokenizer.eos_token
|
102 |
+
tokenizer.model_max_length = max_seq_length
|
103 |
+
|
104 |
+
def preprocess_function(examples):
|
105 |
+
return tokenizer(examples["text"], truncation=True, max_length=max_seq_length, padding="max_length")
|
106 |
+
|
107 |
+
processed_dataset = data["train"].map(preprocess_function, batched=True)
|
108 |
+
|
109 |
+
tokenizer.pad_token = tokenizer.eos_token
|
110 |
+
torch.cuda.empty_cache()
|
111 |
+
trainer = transformers.Trainer(
|
112 |
+
model=model,
|
113 |
+
train_dataset=processed_dataset,
|
114 |
+
args=TrainingArguments(
|
115 |
+
per_device_train_batch_size=args.batch_size,
|
116 |
+
gradient_accumulation_steps=args.gradient_accumulation_steps,
|
117 |
+
gradient_checkpointing=True,
|
118 |
+
warmup_steps=200,
|
119 |
+
max_steps=args.max_steps,
|
120 |
+
learning_rate=2e-4,
|
121 |
+
bf16=True,
|
122 |
+
logging_steps=25,
|
123 |
+
output_dir=output_dir,
|
124 |
+
optim="adamw_bnb_8bit",
|
125 |
+
logging_first_step=True,
|
126 |
+
push_to_hub=True,
|
127 |
+
hub_model_id=hub_model_id,
|
128 |
+
),
|
129 |
+
data_collator=transformers.DataCollatorForLanguageModeling(tokenizer, mlm=False),
|
130 |
+
)
|
131 |
+
model.config.use_cache = False
|
132 |
+
|
133 |
+
push_frequency = 100
|
134 |
+
trainer.add_callback(PushToHubCallback(trainer, push_frequency, hub_model_id))
|
135 |
+
|
136 |
+
trainer.train()
|
137 |
+
|
138 |
+
final_commit_hash = trainer.push_to_hub("Training complete")
|
139 |
+
print(f"Training complete. Final commit hash: {final_commit_hash}")
|
140 |
+
|
141 |
+
# MMLU Evaluation
|
142 |
+
if args.run_mmlu:
|
143 |
+
launch_mmlu_evaluation(model_id, hub_model_id, output_dir)
|
144 |
+
|
145 |
+
if __name__ == "__main__":
|
146 |
+
parser = argparse.ArgumentParser(description="Fine-tune a language model and/or run MMLU evaluation")
|
147 |
+
parser.add_argument("--model_id", type=str, default="ISTA-DASLab/Meta-Llama-3-8B-Instruct-AQLM-2Bit-1x16",
|
148 |
+
help="Model ID to fine-tune or evaluate")
|
149 |
+
parser.add_argument("--max_seq_length", type=int, default=2048, help="Maximum sequence length")
|
150 |
+
parser.add_argument("--output_dir", type=str, required=True, help="Output directory for checkpoints and results")
|
151 |
+
parser.add_argument("--hub_model_id", type=str,
|
152 |
+
default="davisrbr/ISTA-DASLab-Meta-Llama-3-8B-Instruct-AQLM-2Bit-1x16-hf-100000_r8_cont",
|
153 |
+
help="Hub model ID for pushing or LoRA weights")
|
154 |
+
parser.add_argument("--batch_size", type=int, default=1, help="Per-device batch size")
|
155 |
+
parser.add_argument("--gradient_accumulation_steps", type=int, default=8, help="Gradient accumulation steps")
|
156 |
+
parser.add_argument("--max_steps", type=int, default=50000, help="Maximum number of training steps")
|
157 |
+
parser.add_argument("--run_mmlu", action="store_true", help="Run MMLU evaluation after training")
|
158 |
+
parser.add_argument("--lora_rank", type=int, default=8, help="Rank of LoRA adaptation")
|
159 |
+
parser.add_argument("--only_mmlu", action="store_true", help="Only run MMLU evaluation without training")
|
160 |
+
parser.add_argument("--launch_slurm", action="store_true", help="Launch the entire script as a SLURM job")
|
161 |
+
parser.add_argument("--num_gpus", type=int, default=4, help="Number of GPUs to use for training")
|
162 |
+
parser.add_argument("--commit_hash", type=str, help="Specific commit hash to evaluate (for MMLU only)")
|
163 |
+
|
164 |
+
args = parser.parse_args()
|
165 |
+
main(args)
|
training_args.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:222b5a3aebccf46c6252f469180d59f06f801f667a1e0d747a680d0a72a218de
|
3 |
+
size 5176
|