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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
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-rag'"
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
+ "data": {
77
+ "application/vnd.jupyter.widget-view+json": {
78
+ "model_id": "cd5f09268d5848dfa914559c17517de4",
79
+ "version_major": 2,
80
+ "version_minor": 0
81
+ },
82
+ "text/plain": [
83
+ "tokenizer_config.json: 0%| | 0.00/1.52k [00:00<?, ?B/s]"
84
+ ]
85
+ },
86
+ "metadata": {},
87
+ "output_type": "display_data"
88
+ },
89
+ {
90
+ "data": {
91
+ "application/vnd.jupyter.widget-view+json": {
92
+ "model_id": "ae1ea0a4878e4fffa3a7a79de8bab6cb",
93
+ "version_major": 2,
94
+ "version_minor": 0
95
+ },
96
+ "text/plain": [
97
+ "tokenizer.json: 0%| | 0.00/1.84M [00:00<?, ?B/s]"
98
+ ]
99
+ },
100
+ "metadata": {},
101
+ "output_type": "display_data"
102
+ },
103
+ {
104
+ "data": {
105
+ "application/vnd.jupyter.widget-view+json": {
106
+ "model_id": "2366fa88ea66490da15cb43a70958b87",
107
+ "version_major": 2,
108
+ "version_minor": 0
109
+ },
110
+ "text/plain": [
111
+ "special_tokens_map.json: 0%| | 0.00/552 [00:00<?, ?B/s]"
112
+ ]
113
+ },
114
+ "metadata": {},
115
+ "output_type": "display_data"
116
+ },
117
+ {
118
+ "name": "stderr",
119
+ "output_type": "stream",
120
+ "text": [
121
+ "AWQ: 100%|██████████| 22/22 [02:20<00:00, 6.37s/it]\n"
122
+ ]
123
+ }
124
+ ],
125
+ "source": [
126
+ "quant_path = 'malaysian-tinyllama-1.1b-16k-instructions-rag-awq'\n",
127
+ "quant_config = { \"zero_point\": True, \"q_group_size\": 128, \"w_bit\": 4, \"version\": \"GEMM\" }\n",
128
+ "\n",
129
+ "tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)\n",
130
+ "model.quantize(tokenizer, quant_config=quant_config, calib_data = 'mesolitica/malaysian-calibration')"
131
+ ]
132
+ },
133
+ {
134
+ "cell_type": "code",
135
+ "execution_count": 8,
136
+ "id": "ee290c1e",
137
+ "metadata": {},
138
+ "outputs": [
139
+ {
140
+ "name": "stderr",
141
+ "output_type": "stream",
142
+ "text": [
143
+ "WARNING:root:`quant_config.json` is being deprecated in the future in favor of quantization_config in config.json.\n"
144
+ ]
145
+ },
146
+ {
147
+ "data": {
148
+ "text/plain": [
149
+ "('malaysian-tinyllama-1.1b-16k-instructions-rag-awq/tokenizer_config.json',\n",
150
+ " 'malaysian-tinyllama-1.1b-16k-instructions-rag-awq/special_tokens_map.json',\n",
151
+ " 'malaysian-tinyllama-1.1b-16k-instructions-rag-awq/tokenizer.json')"
152
+ ]
153
+ },
154
+ "execution_count": 8,
155
+ "metadata": {},
156
+ "output_type": "execute_result"
157
+ }
158
+ ],
159
+ "source": [
160
+ "model.save_quantized(quant_path, safetensors = False)\n",
161
+ "tokenizer.save_pretrained(quant_path)"
162
+ ]
163
+ },
164
+ {
165
+ "cell_type": "code",
166
+ "execution_count": 9,
167
+ "id": "737f2403",
168
+ "metadata": {},
169
+ "outputs": [
170
+ {
171
+ "data": {
172
+ "text/plain": [
173
+ "CommitInfo(commit_url='https://huggingface.co/mesolitica/malaysian-tinyllama-1.1b-16k-instructions-rag-AWQ/commit/6f906fb7dab784cdf9de4b625578fa9dd25d5c7d', commit_message='Upload tokenizer', commit_description='', oid='6f906fb7dab784cdf9de4b625578fa9dd25d5c7d', pr_url=None, pr_revision=None, pr_num=None)"
174
+ ]
175
+ },
176
+ "execution_count": 9,
177
+ "metadata": {},
178
+ "output_type": "execute_result"
179
+ }
180
+ ],
181
+ "source": [
182
+ "tokenizer.push_to_hub('mesolitica/malaysian-tinyllama-1.1b-16k-instructions-rag-AWQ')"
183
+ ]
184
+ },
185
+ {
186
+ "cell_type": "code",
187
+ "execution_count": 10,
188
+ "id": "ed92c8ee",
189
+ "metadata": {},
190
+ "outputs": [
191
+ {
192
+ "data": {
193
+ "text/plain": [
194
+ "CommitInfo(commit_url='https://huggingface.co/mesolitica/malaysian-tinyllama-1.1b-16k-instructions-rag-AWQ/commit/41fbcd7dd92f45fe71a5bccc3a48cff27979b59d', commit_message='Upload config', commit_description='', oid='41fbcd7dd92f45fe71a5bccc3a48cff27979b59d', pr_url=None, pr_revision=None, pr_num=None)"
195
+ ]
196
+ },
197
+ "execution_count": 10,
198
+ "metadata": {},
199
+ "output_type": "execute_result"
200
+ }
201
+ ],
202
+ "source": [
203
+ "quantization_config = AwqConfig(\n",
204
+ " bits=quant_config['w_bit'],\n",
205
+ " group_size=quant_config['q_group_size'],\n",
206
+ " zero_point=quant_config['zero_point'],\n",
207
+ " backend='autoawq',\n",
208
+ " version=quant_config['version'].lower(),\n",
209
+ ")\n",
210
+ "\n",
211
+ "config = AutoConfig.from_pretrained(model_path)\n",
212
+ "config.quantization_config = quantization_config\n",
213
+ "\n",
214
+ "config.push_to_hub('mesolitica/malaysian-tinyllama-1.1b-16k-instructions-rag-AWQ')"
215
+ ]
216
+ },
217
+ {
218
+ "cell_type": "code",
219
+ "execution_count": 11,
220
+ "id": "c74b2f45",
221
+ "metadata": {},
222
+ "outputs": [
223
+ {
224
+ "name": "stdout",
225
+ "output_type": "stream",
226
+ "text": [
227
+ "config.json\t\tquant_config.json\t tokenizer_config.json\r\n",
228
+ "generation_config.json\tspecial_tokens_map.json\r\n",
229
+ "pytorch_model.bin\ttokenizer.json\r\n"
230
+ ]
231
+ },
232
+ {
233
+ "name": "stderr",
234
+ "output_type": "stream",
235
+ "text": [
236
+ "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
237
+ "To disable this warning, you can either:\n",
238
+ "\t- Avoid using `tokenizers` before the fork if possible\n",
239
+ "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
240
+ ]
241
+ }
242
+ ],
243
+ "source": [
244
+ "!ls malaysian-tinyllama-1.1b-16k-instructions-rag-awq"
245
+ ]
246
+ },
247
+ {
248
+ "cell_type": "code",
249
+ "execution_count": 12,
250
+ "id": "2e0fb591",
251
+ "metadata": {},
252
+ "outputs": [],
253
+ "source": [
254
+ "from huggingface_hub import HfApi\n",
255
+ "\n",
256
+ "api = HfApi()"
257
+ ]
258
+ },
259
+ {
260
+ "cell_type": "code",
261
+ "execution_count": 13,
262
+ "id": "dd06cfa2",
263
+ "metadata": {},
264
+ "outputs": [
265
+ {
266
+ "data": {
267
+ "application/vnd.jupyter.widget-view+json": {
268
+ "model_id": "3d3700ff70fe467ea5dcf7e933c12d73",
269
+ "version_major": 2,
270
+ "version_minor": 0
271
+ },
272
+ "text/plain": [
273
+ "pytorch_model.bin: 0%| | 0.00/766M [00:00<?, ?B/s]"
274
+ ]
275
+ },
276
+ "metadata": {},
277
+ "output_type": "display_data"
278
+ },
279
+ {
280
+ "data": {
281
+ "text/plain": [
282
+ "CommitInfo(commit_url='https://huggingface.co/mesolitica/malaysian-tinyllama-1.1b-16k-instructions-rag-AWQ/commit/40801f6263ebf6127720f9d5bf6037557c565af2', commit_message='Upload pytorch_model.bin with huggingface_hub', commit_description='', oid='40801f6263ebf6127720f9d5bf6037557c565af2', pr_url=None, pr_revision=None, pr_num=None)"
283
+ ]
284
+ },
285
+ "execution_count": 13,
286
+ "metadata": {},
287
+ "output_type": "execute_result"
288
+ }
289
+ ],
290
+ "source": [
291
+ "api.upload_file(\n",
292
+ " path_or_fileobj='malaysian-tinyllama-1.1b-16k-instructions-rag-awq/pytorch_model.bin',\n",
293
+ " path_in_repo=\"pytorch_model.bin\",\n",
294
+ " repo_id='mesolitica/malaysian-tinyllama-1.1b-16k-instructions-rag-AWQ',\n",
295
+ " repo_type=\"model\",\n",
296
+ ")"
297
+ ]
298
+ },
299
+ {
300
+ "cell_type": "code",
301
+ "execution_count": 14,
302
+ "id": "1383ff2c",
303
+ "metadata": {},
304
+ "outputs": [
305
+ {
306
+ "data": {
307
+ "text/plain": [
308
+ "CommitInfo(commit_url='https://huggingface.co/mesolitica/malaysian-tinyllama-1.1b-16k-instructions-rag-AWQ/commit/021f56454c537c0658279317a6157218a3d04479', commit_message='Upload quant_config.json with huggingface_hub', commit_description='', oid='021f56454c537c0658279317a6157218a3d04479', pr_url=None, pr_revision=None, pr_num=None)"
309
+ ]
310
+ },
311
+ "execution_count": 14,
312
+ "metadata": {},
313
+ "output_type": "execute_result"
314
+ }
315
+ ],
316
+ "source": [
317
+ "api.upload_file(\n",
318
+ " path_or_fileobj='malaysian-tinyllama-1.1b-16k-instructions-rag-awq/quant_config.json',\n",
319
+ " path_in_repo=\"quant_config.json\",\n",
320
+ " repo_id='mesolitica/malaysian-tinyllama-1.1b-16k-instructions-rag-AWQ',\n",
321
+ " repo_type=\"model\",\n",
322
+ ")"
323
+ ]
324
+ },
325
+ {
326
+ "cell_type": "code",
327
+ "execution_count": 15,
328
+ "id": "5852ec02",
329
+ "metadata": {},
330
+ "outputs": [
331
+ {
332
+ "data": {
333
+ "application/vnd.jupyter.widget-view+json": {
334
+ "model_id": "a7b3555660f64d56857595133fe3550b",
335
+ "version_major": 2,
336
+ "version_minor": 0
337
+ },
338
+ "text/plain": [
339
+ "config.json: 0%| | 0.00/966 [00:00<?, ?B/s]"
340
+ ]
341
+ },
342
+ "metadata": {},
343
+ "output_type": "display_data"
344
+ },
345
+ {
346
+ "name": "stderr",
347
+ "output_type": "stream",
348
+ "text": [
349
+ "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"
350
+ ]
351
+ },
352
+ {
353
+ "data": {
354
+ "application/vnd.jupyter.widget-view+json": {
355
+ "model_id": "dbcc6cdc52ac4c11b9facfc071dd0c40",
356
+ "version_major": 2,
357
+ "version_minor": 0
358
+ },
359
+ "text/plain": [
360
+ "pytorch_model.bin: 0%| | 0.00/766M [00:00<?, ?B/s]"
361
+ ]
362
+ },
363
+ "metadata": {},
364
+ "output_type": "display_data"
365
+ }
366
+ ],
367
+ "source": [
368
+ "quantized_model = AutoModelForCausalLM.from_pretrained('mesolitica/malaysian-tinyllama-1.1b-16k-instructions-rag-AWQ')\n",
369
+ "_ = quantized_model.cuda()"
370
+ ]
371
+ },
372
+ {
373
+ "cell_type": "code",
374
+ "execution_count": 18,
375
+ "id": "66895e20",
376
+ "metadata": {},
377
+ "outputs": [],
378
+ "source": [
379
+ "messages = [\n",
380
+ " {'role': 'user', 'content': 'KWSP tu apa'}\n",
381
+ "]\n",
382
+ "prompt = tokenizer.apply_chat_template(messages, tokenize = False)\n",
383
+ "inputs = tokenizer([prompt], return_tensors='pt', add_special_tokens=False).to('cuda')"
384
+ ]
385
+ },
386
+ {
387
+ "cell_type": "code",
388
+ "execution_count": 19,
389
+ "id": "4b320f33",
390
+ "metadata": {},
391
+ "outputs": [
392
+ {
393
+ "name": "stderr",
394
+ "output_type": "stream",
395
+ "text": [
396
+ "Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.\n"
397
+ ]
398
+ },
399
+ {
400
+ "name": "stdout",
401
+ "output_type": "stream",
402
+ "text": [
403
+ "CPU times: user 5.65 s, sys: 0 ns, total: 5.65 s\n",
404
+ "Wall time: 5.65 s\n"
405
+ ]
406
+ },
407
+ {
408
+ "data": {
409
+ "text/plain": [
410
+ "'<s> [INST] KWSP tu apa [/INST]KWSP atau Skim Simpanan Pendidikan Nasional ialah skim yang ditubuhkan oleh kerajaan Malaysia untuk membantu ibu bapa membiayai pendidikan anak-anak mereka. Skim ini membolehkan individu menyumbang sejumlah wang daripada pendapatan mereka kepada akaun KWSP untuk digunakan sebagai dana pendidikan pada masa hadapan. Dengan menyumbang kepada KWSP, ibu bapa boleh menggunakan dana tersebut untuk membiayai yuran pendidikan, kos buku dan peralatan sekolah, yuran ujian, dan perbelanjaan lain yang berkaitan dengan pendidikan anak-anak mereka. Skim ini adalah inisiatif yang membantu ibu bapa untuk menyediakan persediaan kewangan yang diperlukan untuk pendidikan anak-anak mereka tanpa perlu mengeluarkan wang dari poket sendiri.</s>'"
411
+ ]
412
+ },
413
+ "execution_count": 19,
414
+ "metadata": {},
415
+ "output_type": "execute_result"
416
+ }
417
+ ],
418
+ "source": [
419
+ "%%time\n",
420
+ "\n",
421
+ "generate_kwargs = dict(\n",
422
+ " inputs,\n",
423
+ " max_new_tokens=1024,\n",
424
+ " top_p=0.95,\n",
425
+ " top_k=50,\n",
426
+ " temperature=0.9,\n",
427
+ " do_sample=True,\n",
428
+ " num_beams=1,\n",
429
+ ")\n",
430
+ "r = quantized_model.generate(**generate_kwargs)\n",
431
+ "tokenizer.decode(r[0])"
432
+ ]
433
+ },
434
+ {
435
+ "cell_type": "code",
436
+ "execution_count": null,
437
+ "id": "a9a93555",
438
+ "metadata": {},
439
+ "outputs": [],
440
+ "source": []
441
+ }
442
+ ],
443
+ "metadata": {
444
+ "kernelspec": {
445
+ "display_name": "Python 3 (ipykernel)",
446
+ "language": "python",
447
+ "name": "python3"
448
+ },
449
+ "language_info": {
450
+ "codemirror_mode": {
451
+ "name": "ipython",
452
+ "version": 3
453
+ },
454
+ "file_extension": ".py",
455
+ "mimetype": "text/x-python",
456
+ "name": "python",
457
+ "nbconvert_exporter": "python",
458
+ "pygments_lexer": "ipython3",
459
+ "version": "3.10.12"
460
+ }
461
+ },
462
+ "nbformat": 4,
463
+ "nbformat_minor": 5
464
+ }