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
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "19fe0df6",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "# !pip3 install https://github.com/casper-hansen/AutoAWQ/releases/download/v0.1.8/autoawq-0.1.8+cu118-cp310-cp310-linux_x86_64.whl"
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "code",
15
+ "execution_count": 2,
16
+ "id": "20861f3e",
17
+ "metadata": {},
18
+ "outputs": [],
19
+ "source": [
20
+ "from awq import AutoAWQForCausalLM\n",
21
+ "from transformers import AutoConfig, AwqConfig, AutoTokenizer, AutoModelForCausalLM\n",
22
+ "import torch\n",
23
+ "\n",
24
+ "model_path = 'mesolitica/malaysian-tinyllama-1.1b-16k-instructions-v3'"
25
+ ]
26
+ },
27
+ {
28
+ "cell_type": "code",
29
+ "execution_count": 3,
30
+ "id": "9939ad4e",
31
+ "metadata": {
32
+ "scrolled": true
33
+ },
34
+ "outputs": [],
35
+ "source": [
36
+ "model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype = torch.bfloat16)"
37
+ ]
38
+ },
39
+ {
40
+ "cell_type": "code",
41
+ "execution_count": 4,
42
+ "id": "fdb86f50",
43
+ "metadata": {},
44
+ "outputs": [],
45
+ "source": [
46
+ "!rm -rf test"
47
+ ]
48
+ },
49
+ {
50
+ "cell_type": "code",
51
+ "execution_count": 5,
52
+ "id": "72e76288",
53
+ "metadata": {},
54
+ "outputs": [],
55
+ "source": [
56
+ "model.save_pretrained('./test', safe_serialization = False)"
57
+ ]
58
+ },
59
+ {
60
+ "cell_type": "code",
61
+ "execution_count": 6,
62
+ "id": "aa245150",
63
+ "metadata": {},
64
+ "outputs": [],
65
+ "source": [
66
+ "model = AutoAWQForCausalLM.from_pretrained('./test')"
67
+ ]
68
+ },
69
+ {
70
+ "cell_type": "code",
71
+ "execution_count": 7,
72
+ "id": "d3949cf4",
73
+ "metadata": {},
74
+ "outputs": [
75
+ {
76
+ "name": "stderr",
77
+ "output_type": "stream",
78
+ "text": [
79
+ "AWQ: 100%|██████████| 22/22 [02:25<00:00, 6.59s/it]\n"
80
+ ]
81
+ }
82
+ ],
83
+ "source": [
84
+ "quant_path = 'malaysian-tinyllama-1.1b-16k-instructions-v3-awq'\n",
85
+ "quant_config = { \"zero_point\": True, \"q_group_size\": 128, \"w_bit\": 4, \"version\": \"GEMM\" }\n",
86
+ "\n",
87
+ "tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)\n",
88
+ "model.quantize(tokenizer, quant_config=quant_config, calib_data = 'mesolitica/malaysian-calibration')"
89
+ ]
90
+ },
91
+ {
92
+ "cell_type": "code",
93
+ "execution_count": 8,
94
+ "id": "ee290c1e",
95
+ "metadata": {},
96
+ "outputs": [
97
+ {
98
+ "name": "stderr",
99
+ "output_type": "stream",
100
+ "text": [
101
+ "WARNING:root:`quant_config.json` is being deprecated in the future in favor of quantization_config in config.json.\n"
102
+ ]
103
+ },
104
+ {
105
+ "data": {
106
+ "text/plain": [
107
+ "('malaysian-tinyllama-1.1b-16k-instructions-v3-awq/tokenizer_config.json',\n",
108
+ " 'malaysian-tinyllama-1.1b-16k-instructions-v3-awq/special_tokens_map.json',\n",
109
+ " 'malaysian-tinyllama-1.1b-16k-instructions-v3-awq/tokenizer.json')"
110
+ ]
111
+ },
112
+ "execution_count": 8,
113
+ "metadata": {},
114
+ "output_type": "execute_result"
115
+ }
116
+ ],
117
+ "source": [
118
+ "model.save_quantized(quant_path, safetensors = False)\n",
119
+ "tokenizer.save_pretrained(quant_path)"
120
+ ]
121
+ },
122
+ {
123
+ "cell_type": "code",
124
+ "execution_count": 9,
125
+ "id": "737f2403",
126
+ "metadata": {},
127
+ "outputs": [
128
+ {
129
+ "data": {
130
+ "text/plain": [
131
+ "CommitInfo(commit_url='https://huggingface.co/mesolitica/malaysian-tinyllama-1.1b-16k-instructions-v3-AWQ/commit/909193caec2d94a495535d9033f5dcd975686356', commit_message='Upload tokenizer', commit_description='', oid='909193caec2d94a495535d9033f5dcd975686356', pr_url=None, pr_revision=None, pr_num=None)"
132
+ ]
133
+ },
134
+ "execution_count": 9,
135
+ "metadata": {},
136
+ "output_type": "execute_result"
137
+ }
138
+ ],
139
+ "source": [
140
+ "tokenizer.push_to_hub('mesolitica/malaysian-tinyllama-1.1b-16k-instructions-v3-AWQ')"
141
+ ]
142
+ },
143
+ {
144
+ "cell_type": "code",
145
+ "execution_count": 10,
146
+ "id": "ed92c8ee",
147
+ "metadata": {},
148
+ "outputs": [
149
+ {
150
+ "data": {
151
+ "text/plain": [
152
+ "CommitInfo(commit_url='https://huggingface.co/mesolitica/malaysian-tinyllama-1.1b-16k-instructions-v3-AWQ/commit/a259a50f290eb1e648396698d9a0dcac7d33d5a2', commit_message='Upload config', commit_description='', oid='a259a50f290eb1e648396698d9a0dcac7d33d5a2', pr_url=None, pr_revision=None, pr_num=None)"
153
+ ]
154
+ },
155
+ "execution_count": 10,
156
+ "metadata": {},
157
+ "output_type": "execute_result"
158
+ }
159
+ ],
160
+ "source": [
161
+ "quantization_config = AwqConfig(\n",
162
+ " bits=quant_config['w_bit'],\n",
163
+ " group_size=quant_config['q_group_size'],\n",
164
+ " zero_point=quant_config['zero_point'],\n",
165
+ " backend='autoawq',\n",
166
+ " version=quant_config['version'].lower(),\n",
167
+ ")\n",
168
+ "\n",
169
+ "config = AutoConfig.from_pretrained(model_path)\n",
170
+ "config.quantization_config = quantization_config\n",
171
+ "\n",
172
+ "config.push_to_hub('mesolitica/malaysian-tinyllama-1.1b-16k-instructions-v3-AWQ')"
173
+ ]
174
+ },
175
+ {
176
+ "cell_type": "code",
177
+ "execution_count": 11,
178
+ "id": "c74b2f45",
179
+ "metadata": {},
180
+ "outputs": [
181
+ {
182
+ "name": "stdout",
183
+ "output_type": "stream",
184
+ "text": [
185
+ "config.json\t\tquant_config.json\t tokenizer_config.json\r\n",
186
+ "generation_config.json\tspecial_tokens_map.json\r\n",
187
+ "pytorch_model.bin\ttokenizer.json\r\n"
188
+ ]
189
+ },
190
+ {
191
+ "name": "stderr",
192
+ "output_type": "stream",
193
+ "text": [
194
+ "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
195
+ "To disable this warning, you can either:\n",
196
+ "\t- Avoid using `tokenizers` before the fork if possible\n",
197
+ "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
198
+ ]
199
+ }
200
+ ],
201
+ "source": [
202
+ "!ls malaysian-tinyllama-1.1b-16k-instructions-v3-awq"
203
+ ]
204
+ },
205
+ {
206
+ "cell_type": "code",
207
+ "execution_count": 12,
208
+ "id": "2e0fb591",
209
+ "metadata": {},
210
+ "outputs": [],
211
+ "source": [
212
+ "from huggingface_hub import HfApi\n",
213
+ "\n",
214
+ "api = HfApi()"
215
+ ]
216
+ },
217
+ {
218
+ "cell_type": "code",
219
+ "execution_count": 13,
220
+ "id": "dd06cfa2",
221
+ "metadata": {},
222
+ "outputs": [
223
+ {
224
+ "data": {
225
+ "application/vnd.jupyter.widget-view+json": {
226
+ "model_id": "2c8f617cd86c41e9ae97e6ed0e9cca0a",
227
+ "version_major": 2,
228
+ "version_minor": 0
229
+ },
230
+ "text/plain": [
231
+ "pytorch_model.bin: 0%| | 0.00/766M [00:00<?, ?B/s]"
232
+ ]
233
+ },
234
+ "metadata": {},
235
+ "output_type": "display_data"
236
+ },
237
+ {
238
+ "data": {
239
+ "text/plain": [
240
+ "CommitInfo(commit_url='https://huggingface.co/mesolitica/malaysian-tinyllama-1.1b-16k-instructions-v3-AWQ/commit/704674a60b13d46f7aa6bc4c7f08fd1fbf52aa01', commit_message='Upload pytorch_model.bin with huggingface_hub', commit_description='', oid='704674a60b13d46f7aa6bc4c7f08fd1fbf52aa01', pr_url=None, pr_revision=None, pr_num=None)"
241
+ ]
242
+ },
243
+ "execution_count": 13,
244
+ "metadata": {},
245
+ "output_type": "execute_result"
246
+ }
247
+ ],
248
+ "source": [
249
+ "api.upload_file(\n",
250
+ " path_or_fileobj='malaysian-tinyllama-1.1b-16k-instructions-v3-awq/pytorch_model.bin',\n",
251
+ " path_in_repo=\"pytorch_model.bin\",\n",
252
+ " repo_id='mesolitica/malaysian-tinyllama-1.1b-16k-instructions-v3-AWQ',\n",
253
+ " repo_type=\"model\",\n",
254
+ ")"
255
+ ]
256
+ },
257
+ {
258
+ "cell_type": "code",
259
+ "execution_count": 14,
260
+ "id": "1383ff2c",
261
+ "metadata": {},
262
+ "outputs": [
263
+ {
264
+ "data": {
265
+ "text/plain": [
266
+ "CommitInfo(commit_url='https://huggingface.co/mesolitica/malaysian-tinyllama-1.1b-16k-instructions-v3-AWQ/commit/ff8dcd9a8d0fce1c30a19c0e2c9cea3f4efbcb28', commit_message='Upload quant_config.json with huggingface_hub', commit_description='', oid='ff8dcd9a8d0fce1c30a19c0e2c9cea3f4efbcb28', pr_url=None, pr_revision=None, pr_num=None)"
267
+ ]
268
+ },
269
+ "execution_count": 14,
270
+ "metadata": {},
271
+ "output_type": "execute_result"
272
+ }
273
+ ],
274
+ "source": [
275
+ "api.upload_file(\n",
276
+ " path_or_fileobj='malaysian-tinyllama-1.1b-16k-instructions-v3-awq/quant_config.json',\n",
277
+ " path_in_repo=\"quant_config.json\",\n",
278
+ " repo_id='mesolitica/malaysian-tinyllama-1.1b-16k-instructions-v3-AWQ',\n",
279
+ " repo_type=\"model\",\n",
280
+ ")"
281
+ ]
282
+ },
283
+ {
284
+ "cell_type": "code",
285
+ "execution_count": 15,
286
+ "id": "5852ec02",
287
+ "metadata": {},
288
+ "outputs": [
289
+ {
290
+ "name": "stdout",
291
+ "output_type": "stream",
292
+ "text": [
293
+ "The history saving thread hit an unexpected error (OperationalError('database is locked')).History will not be written to the database.\n"
294
+ ]
295
+ },
296
+ {
297
+ "data": {
298
+ "application/vnd.jupyter.widget-view+json": {
299
+ "model_id": "5429442d416848c99e511592225aae50",
300
+ "version_major": 2,
301
+ "version_minor": 0
302
+ },
303
+ "text/plain": [
304
+ "config.json: 0%| | 0.00/965 [00:00<?, ?B/s]"
305
+ ]
306
+ },
307
+ "metadata": {},
308
+ "output_type": "display_data"
309
+ },
310
+ {
311
+ "name": "stderr",
312
+ "output_type": "stream",
313
+ "text": [
314
+ "You have loaded an AWQ model on CPU and have a CUDA device available, make sure to set your model on a GPU device in order to run your model.\n"
315
+ ]
316
+ },
317
+ {
318
+ "data": {
319
+ "application/vnd.jupyter.widget-view+json": {
320
+ "model_id": "4d153ddd40174e7d8c6e6c26e6593c0a",
321
+ "version_major": 2,
322
+ "version_minor": 0
323
+ },
324
+ "text/plain": [
325
+ "pytorch_model.bin: 0%| | 0.00/766M [00:00<?, ?B/s]"
326
+ ]
327
+ },
328
+ "metadata": {},
329
+ "output_type": "display_data"
330
+ }
331
+ ],
332
+ "source": [
333
+ "quantized_model = AutoModelForCausalLM.from_pretrained('mesolitica/malaysian-tinyllama-1.1b-16k-instructions-v3-AWQ')\n",
334
+ "_ = quantized_model.cuda()"
335
+ ]
336
+ },
337
+ {
338
+ "cell_type": "code",
339
+ "execution_count": 16,
340
+ "id": "66895e20",
341
+ "metadata": {},
342
+ "outputs": [],
343
+ "source": [
344
+ "messages = [\n",
345
+ " {'role': 'user', 'content': 'KWSP tu apa'}\n",
346
+ "]\n",
347
+ "prompt = tokenizer.apply_chat_template(messages, tokenize = False)\n",
348
+ "inputs = tokenizer([prompt], return_tensors='pt', add_special_tokens=False).to('cuda')"
349
+ ]
350
+ },
351
+ {
352
+ "cell_type": "code",
353
+ "execution_count": 17,
354
+ "id": "4b320f33",
355
+ "metadata": {},
356
+ "outputs": [
357
+ {
358
+ "name": "stderr",
359
+ "output_type": "stream",
360
+ "text": [
361
+ "Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.\n"
362
+ ]
363
+ },
364
+ {
365
+ "name": "stdout",
366
+ "output_type": "stream",
367
+ "text": [
368
+ "CPU times: user 6.36 s, sys: 6.36 ms, total: 6.37 s\n",
369
+ "Wall time: 6.47 s\n"
370
+ ]
371
+ },
372
+ {
373
+ "data": {
374
+ "text/plain": [
375
+ "'<s> [INST] KWSP tu apa [/INST]KWSP, singkatan untuk Kumpulan Wang Simpanan Pekerja, ialah Kumpulan Wang Simpanan Pekerja di Malaysia, yang merupakan dana simpanan pekerja. KWSP bertujuan untuk menyediakan simpanan persaraan dan faedah keselamatan sosial untuk ahli KWSP (dana caruman dan majikan) dengan menyediakan pelaburan boleh beli. Dana ini diuruskan oleh KWSP, sebuah syarikat milik kerajaan di Malaysia. KWSP terdiri daripada simpanan caruman pekerja dan simpanan majikan, dengan peruntukan yang berkaitan dengan skim simpanan yang berbeza di Malaysia. Dana ini menggunakan pelaburan yang dibuat oleh pencarum dan pembayar caruman untuk menjana dividen dan faedah, yang seterusnya menyokong matlamat kerajaan untuk menyediakan simpanan persaraan yang selamat dan mencukupi untuk pekerja.</s>'"
376
+ ]
377
+ },
378
+ "execution_count": 17,
379
+ "metadata": {},
380
+ "output_type": "execute_result"
381
+ }
382
+ ],
383
+ "source": [
384
+ "%%time\n",
385
+ "\n",
386
+ "generate_kwargs = dict(\n",
387
+ " inputs,\n",
388
+ " max_new_tokens=1024,\n",
389
+ " top_p=0.95,\n",
390
+ " top_k=50,\n",
391
+ " temperature=0.9,\n",
392
+ " do_sample=True,\n",
393
+ " num_beams=1,\n",
394
+ ")\n",
395
+ "r = quantized_model.generate(**generate_kwargs)\n",
396
+ "tokenizer.decode(r[0])"
397
+ ]
398
+ },
399
+ {
400
+ "cell_type": "code",
401
+ "execution_count": null,
402
+ "id": "a9a93555",
403
+ "metadata": {},
404
+ "outputs": [],
405
+ "source": []
406
+ }
407
+ ],
408
+ "metadata": {
409
+ "kernelspec": {
410
+ "display_name": "Python 3 (ipykernel)",
411
+ "language": "python",
412
+ "name": "python3"
413
+ },
414
+ "language_info": {
415
+ "codemirror_mode": {
416
+ "name": "ipython",
417
+ "version": 3
418
+ },
419
+ "file_extension": ".py",
420
+ "mimetype": "text/x-python",
421
+ "name": "python",
422
+ "nbconvert_exporter": "python",
423
+ "pygments_lexer": "ipython3",
424
+ "version": "3.10.12"
425
+ }
426
+ },
427
+ "nbformat": 4,
428
+ "nbformat_minor": 5
429
+ }