File size: 21,376 Bytes
2051791
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
import copy
import logging
import os

# from adapter.awq import create_awq_model
# from adapter.awq import quantize_awq_model
from datetime import datetime
from pathlib import Path

import pandas as pd
import torch
from auto_gptq import BaseQuantizeConfig as GPTQQuantConfig
from hqq.core.quantize import BaseQuantizeConfig as HQQQuantConfig
from transformers import BitsAndBytesConfig

from lm_quant_toolkit.adapter.autoawq import (
    create_autoawq_model,
    quantize_autoawq_model,
)
from lm_quant_toolkit.adapter.autogptq import (
    create_autogptq_model,
    quantize_autogptq_model,
)
from lm_quant_toolkit.adapter.bnb import create_bnb_model, quantize_bnb_model
from lm_quant_toolkit.adapter.fp16 import create_fp16_model
from lm_quant_toolkit.adapter.hqq import create_hqq_model, quantize_hqq_model
from lm_quant_toolkit.adapter.mxq import create_mxq_model, quantize_mxq_model
from lm_quant_toolkit.eval.common import (
    HQQ_CONFIGS,
    _dump_cuda_mem_snapshot,
    _reset_peak_memory_stats,
    cleanup,
    combine_metrics,
    get_memory_metrics,
    get_mxq_quant_meta_data_file,
    persist_progress,
    save_partial_metric,
)
from lm_quant_toolkit.eval.leaderboard import eval_llm_leaderboard
from lm_quant_toolkit.eval.perplexity import eval_ppls

ALL_MODELS = [
    "meta-llama/Llama-2-7b-hf",
    "meta-llama/Llama-2-13b-hf",
    "meta-llama/Meta-Llama-3-8B",
    "meta-llama/Llama-3.1-8B",
]

MXQ_CONFIGS = [
    (
        f"{bits:.2f}".replace(".", "_"),
        HQQQuantConfig(mixed=True, budget=bits, quant_scale=True),
    )
    for bits in [5.00, 4.75, 4.50, 4.25, 4.01, 3.76, 3.50, 3.00, 2.75, 2.48]
]

BNB_CONFIGS = [
    (
        "b4g64",
        BitsAndBytesConfig(
            load_in_4bit=True,
            bnb_4bit_quant_type="nf4",
            bnb_4bit_compute_dtype="float16",
            bnb_4bit_use_double_quant=True,
        ),
    ),
    (
        "b8g128",
        BitsAndBytesConfig(
            load_in_8bit=True,
            bnb_4bit_compute_dtype="float16",
        ),
    ),
]


AUTOAWQ_CONFIGS = [
    ("b4g32", {"w_bit": 4, "q_group_size": 32, "zero_point": True, "version": "GEMM"}),
    ("b4g64", {"w_bit": 4, "q_group_size": 64, "zero_point": True, "version": "GEMM"}),
    (
        "b4g128",
        {"w_bit": 4, "q_group_size": 128, "zero_point": True, "version": "GEMM"},
    ),
    # 3-bit not supported by AutoAWQ right now
    # ("b3g64", {"w_bit": 3, "q_group_size": 64, "zero_point": True, 'version':'gemv_fast'}),
    # ("b3g128", {"w_bit": 3, "q_group_size": 128, "zero_point": True, 'version':'gemv_fast'}),
]

GPTQ_CONFIGS = [
    (
        "b8g32",
        GPTQQuantConfig(bits=8, group_size=32, damp_percent=0.01, desc_act=False),
    ),
    (
        "b8g64",
        GPTQQuantConfig(bits=8, group_size=64, damp_percent=0.01, desc_act=False),
    ),
    (
        "b8g128",
        GPTQQuantConfig(bits=8, group_size=128, damp_percent=0.01, desc_act=False),
    ),
    (
        "b4g32",
        GPTQQuantConfig(bits=4, group_size=32, damp_percent=0.01, desc_act=False),
    ),
    (
        "b4g64",
        GPTQQuantConfig(bits=4, group_size=64, damp_percent=0.01, desc_act=False),
    ),
    (
        "b4g128",
        GPTQQuantConfig(bits=4, group_size=128, damp_percent=0.01, desc_act=False),
    ),
    (
        "b3g32",
        GPTQQuantConfig(bits=3, group_size=32, damp_percent=0.01, desc_act=False),
    ),
    (
        "b3g64",
        GPTQQuantConfig(bits=3, group_size=64, damp_percent=0.01, desc_act=False),
    ),
    (
        "b3g128",
        GPTQQuantConfig(bits=3, group_size=128, damp_percent=0.01, desc_act=False),
    ),
]


def gen_experiment_items(models, tasks):
    dikts = []
    for algo, spec in tasks.items():
        configs = spec["configs"]
        for config in configs:
            for model_id in models:
                dikts.append(
                    {
                        "model": model_id,
                        "cfg": config[0],
                        "task_type": spec["type"],
                        "algo": algo,
                    }
                )
    return pd.DataFrame(dikts)


def _setup_fn(algo, spec):
    match algo:
        case "fp16":
            spec["create_fn"] = create_fp16_model
            spec["quantize_fn"] = None
        case "awq":
            spec["create_fn"] = create_autoawq_model
            spec["quantize_fn"] = quantize_autoawq_model
        case "gptq":
            spec["create_fn"] = create_autogptq_model
            spec["quantize_fn"] = quantize_autogptq_model
        case "hqq":
            spec["create_fn"] = create_hqq_model
            spec["quantize_fn"] = quantize_hqq_model
        case "bnb":
            spec["create_fn"] = create_bnb_model
            spec["quantize_fn"] = quantize_bnb_model
        case "mxq":
            spec["create_fn"] = create_mxq_model
            spec["quantize_fn"] = quantize_mxq_model
        case _:
            raise ValueError(f"Invalid algo: {algo}")


def do_expermient(
    experiment_name,
    models,
    tasks,
    quant_dir="snapshots",
    result_dir="results",
    log_dir="logs",
    track_cuda_memory=False,
    **kwargs,
):
    df_all = gen_experiment_items(models, tasks)
    progress_path = os.path.join(result_dir, experiment_name, "progress.csv")
    if Path(progress_path).exists():
        df_saved = pd.read_csv(progress_path)
        df_all = df_all.merge(
            df_saved, how="left", on=["model", "cfg", "task_type", "algo"]
        )
        # filter already processed repos, equivalent to SQL is null
        df_todo = df_all.query("status != status or status != 1")
    else:
        df_all["status"] = 0
        df_all["completion_time"] = ""
        df_todo = df_all
    print("*" * 72)
    print("Sub-task list:")
    print(df_all)
    cnt_todo, cnt_tot = len(df_todo), len(df_all)
    print(f"Todo:{cnt_todo}, Done: {cnt_tot - cnt_todo}, Total: {cnt_tot}")
    if cnt_todo == 0:
        print("Tasks completed!")
    print("*" * 72)
    if cnt_todo == 0:
        return

    df_todo = df_todo.sort_values(by=["model", "cfg"], ascending=False)
    for idx, row in df_todo.iterrows():
        model_id = row["model"]
        algo = row["algo"]
        task_type = row["task_type"]
        cfg = row["cfg"]
        spec = tasks[algo]
        _setup_fn(algo, spec)
        config = [c for c in spec["configs"] if c[0] == cfg][0]
        quant_fn = spec["quantize_fn"]
        metric = _init_metrics(model_id, algo, config)
        print("*" * 72)
        if task_type == "quant":
            print(f"Quantizing {algo} on {model_id} w/ config: {cfg}...")
        elif task_type == "eval_ppl":
            print(f"Evaluating {algo} PPL on {model_id} w/ config: {cfg}...")
        elif task_type == "eval_leaderboard":
            print(
                f"Evaluating {algo} LLM Leaderboard benchmarks on {model_id} w/ config: {cfg}..."
            )
        else:
            print(
                f"Evaluating {algo} model storage metrics on {model_id} w/ config: {cfg}..."
            )
        print("*" * 72)

        if track_cuda_memory:
            torch.cuda.memory._record_memory_history()
        _reset_peak_memory_stats()
        if task_type != "eval_leaderboard":
            create_fn = spec["create_fn"]
            model, tokenizer, quantized, model_file_size = create_fn(
                model_id, config[1], cfg, quant_fn is not None, quant_dir
            )

            if not quantized and quant_fn:
                # avoid interventions between models
                quant_config = copy.deepcopy(config[1])
                if algo == "mxq":
                    ok, metric_fp = get_mxq_quant_meta_data_file(model_id)
                    if not ok:
                        print(
                            f"Quantization meta data file: {metric_fp} doesn't exists!"
                        )
                        return
                    quant_config["quant_metrics_file"] = metric_fp
                    quant_config["weight_algo"] = kwargs.get("weight_algo", None)
                    quant_config["boost_layers"] = kwargs.get("boost_layers", None)
                    quant_config["decline_layers"] = kwargs.get("decline_layers", None)
                    quant_config["boost_stop"] = kwargs.get("boost_stop", None)
                    quant_config["decline_stop"] = kwargs.get("decline_stop", None)
                    quant_config["ablation"] = kwargs.get("ablation", None)
                    quant_config["top_m_layer"] = kwargs.get("top_m_layer", None)
                    quant_config["factor"] = kwargs.get("factor", None)
                model, duration, model_file_size = quant_fn(
                    model,
                    tokenizer,
                    quant_config,
                    model_id,
                    cfg,
                    quant_dir,
                )
                metric["quant_duration"] = duration
            if task_type == "eval_model_storage":
                allot, reserved = get_memory_metrics()
                metric["load_mem_allot"] = allot
                metric["load_mem_reserved"] = reserved
                metric["model_storage_size"] = model_file_size

            elif task_type == "eval_ppl":
                # Evaluate the quantized model
                metric = eval_ppls(model, tokenizer, metric)
                metric["ppl_mem_allot"], metric["ppl_mem_reserved"] = (
                    get_memory_metrics()
                )
            if track_cuda_memory:
                _dump_cuda_mem_snapshot(experiment_name, model_id, algo, result_dir)
            cleanup(model)
        else:
            metric = eval_llm_leaderboard(
                experiment_name,
                model_id,
                algo,
                cfg,
                quant_fn is not None,
                metric,
                quant_dir,
                result_dir,
            )
            metric["leaderboard_mem_allot"], metric["leaderboard_mem_reserved"] = (
                get_memory_metrics()
            )
            if track_cuda_memory:
                _dump_cuda_mem_snapshot(experiment_name, model_id, algo, result_dir)
        save_partial_metric(experiment_name, algo, model_id, cfg, metric, result_dir)
        df_all.loc[
            (df_all["model"] == model_id)
            & (df_all["cfg"] == cfg)
            & (df_all["algo"] == algo)
            & (df_all["task_type"] == task_type),
            ["status", "completion_time"],
        ] = 1, datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        persist_progress(df_all, progress_path)
    # combine metrics
    combine_metrics(experiment_name, result_dir)


def _init_metrics(model_id, algo, config):
    return {
        "model": model_id.split("/")[1],
        "algo": algo,
        "config": config[0],
        "config_detail": str(config[1]).replace("\n", ""),
        "quant_duration": 0,
        "model_storage_size": 0,
        "load_mem_allot": 0,
        "load_mem_reserved": 0,
        "ppl_mem_allot": 0,
        "ppl_mem_reserved": 0,
        "leaderboard_mem_allot": 0,
        "leaderboard_mem_reserved": 0,
        "quant_duration": 0,
        "ppl_wikitext": 0,
        "ppl_c4": 0,
        "duration_wikitext": 0,
        "duration_c4": 0,
        "duration_leaderboard": 0,
        "ifeval": 0,
        "bbh": 0,
        "mathlevel5": 0,
        "gpqa": 0,
        "musr": 0,
        "mmlupro": 0,
    }


def do_expermient_fdata(
    experiment_name,
    models,
    tasks,
    track_cuda_memory=False,
):
    do_expermient(
        experiment_name,
        models,
        tasks,
        quant_dir="/fdata/llm/mxq/snapshots",
        result_dir="/fdata/llm/mxq/results",
        log_dir="/fdata/llm/mxq/logs",
        track_cuda_memory=track_cuda_memory,
    )


########################################################################
#  Quantization experiments
########################################################################


def experiment_quant_hqq():
    models = ALL_MODELS
    tasks = {
        "hqq": {
            "type": "quant",
            "configs": HQQ_CONFIGS,
        },
    }
    do_expermient_fdata(
        "quant_hqq",
        models,
        tasks,
    )


def experiment_quant_mxq():
    models = ALL_MODELS
    type = "quant"
    algo = "mxq"
    tasks = {
        algo: {
            "type": type,
            "configs": MXQ_CONFIGS,
        },
    }
    do_expermient_fdata(f"{type}_{algo}_mxq", models, tasks)


def experiment_quant_awq():
    # models = [ALL_MODELS[0], ALL_MODELS[2]]
    models = [ALL_MODELS[1]]
    type = "quant"
    algo = "awq"
    tasks = {
        algo: {
            "type": type,
            "configs": AUTOAWQ_CONFIGS,
        },
    }
    do_expermient_fdata(f"{type}_{algo}", models, tasks)


def experiment_quant_gptq():
    models = ALL_MODELS
    type = "quant"
    algo = "gptq"
    tasks = {
        algo: {
            "type": type,
            "configs": GPTQ_CONFIGS,
        },
    }
    do_expermient_fdata(f"{type}_{algo}", models, tasks)


def experiment_quantize_405B():
    models = [
        "meta-llama/Meta-Llama-3.1-405B-Instruct",
    ]

    tasks = {
        "hqq": {
            "type": "quant",
            "configs": HQQ_CONFIGS[1:2],
        },
    }
    do_expermient(
        "quant_hqq_405B",
        models,
        tasks,
        quant_dir="/data/gqq-eval/snapshots/",
    )


########################################################################
#  Perplexity evaluation experiments
########################################################################


def experiment_ppl_eval_fp16():
    models = ALL_MODELS
    type = "eval_ppl"
    algo = "fp16"
    tasks = {
        algo: {
            "type": type,
            "configs": [
                ("base", {}),
            ],
        },
    }
    do_expermient_fdata(f"{type}_{algo}", models, tasks)


def experiment_ppl_eval_awq():
    models = [ALL_MODELS[0], ALL_MODELS[2]]
    type = "eval_ppl"
    algo = "awq"
    tasks = {
        algo: {
            "type": type,
            "configs": AUTOAWQ_CONFIGS[1:2],
        },
    }
    do_expermient_fdata(f"{type}_{algo}", models, tasks)


def experiment_ppl_eval_gptq():
    models = ALL_MODELS
    type = "eval_ppl"
    algo = "gptq"
    tasks = {
        algo: {
            "type": type,
            "configs": GPTQ_CONFIGS,
        },
    }
    do_expermient_fdata(f"{type}_{algo}", models, tasks)


def experiment_ppl_eval_hqq():
    models = ALL_MODELS
    type = "eval_ppl"
    algo = "hqq"
    tasks = {
        algo: {
            "type": type,
            "configs": HQQ_CONFIGS,
        },
    }
    do_expermient_fdata(f"{type}_{algo}", models, tasks)


########################################################################
#  Open LLM Leaderboard evaluation experiments
########################################################################


def experiment_llm_leaderboard_fp16():
    models = [ALL_MODELS[0], ALL_MODELS[2]]
    type = "eval_leaderboard"
    algo = "fp16"
    tasks = {
        algo: {
            "type": type,
            "configs": [
                ("base", {}),
            ],
        },
    }
    do_expermient_fdata(f"{type}_{algo}", models, tasks)


def experiment_llm_leaderboard_autogptq():
    models = ALL_MODELS
    type = "eval_leaderboard"
    algo = "gptq"
    tasks = {
        algo: {
            "type": type,
            "configs": GPTQ_CONFIGS,
        },
    }
    do_expermient_fdata(f"{type}_{algo}", models, tasks)


def experiment_llm_leaderboard_hqq():
    models = ALL_MODELS
    type = "eval_leaderboard"
    algo = "hqq"
    tasks = {
        algo: {
            "type": type,
            "configs": HQQ_CONFIGS,
        },
    }
    do_expermient_fdata(f"{type}_{algo}", models, tasks)


def experiment_llm_leaderboard_mxq():
    models = ALL_MODELS
    type = "eval_leaderboard"
    algo = "mxq"
    tasks = {
        algo: {
            "type": type,
            "configs": MXQ_CONFIGS,
        },
    }
    do_expermient_fdata(f"{type}_{algo}", models, tasks)


def experiment_llm_leaderboard_autoawq():
    models = ALL_MODELS[0:1]
    type = "eval_leaderboard"
    algo = "awq"
    tasks = {
        algo: {
            "type": type,
            "configs": AUTOAWQ_CONFIGS[0:2],
        },
    }
    do_expermient_fdata(f"{type}_{algo}", models, tasks)


########################################################################
#  Mixed Quant Eval experiments
########################################################################


def experiment_quant_ppl_eval_mxq_comprise():
    models = ALL_MODELS
    equiv_mxq_configs = []
    nbits = [4.06, 4.10, 4.15, 4.19, 4.24, 4.28, 4.33]
    for bits in nbits:
        cfg_name = f"mxq-{str(bits).replace('.', '_')}"
        equiv_mxq_configs.append(
            (cfg_name, HQQQuantConfig(mixed=True, budget=bits, quant_scale=True))
        )
    quant_tasks = {
        "hqq": {
            "type": "quant",
            "configs": equiv_mxq_configs,
        },
    }
    ppl_tasks = {
        "hqq": {
            "type": "eval_ppl",
            "configs": equiv_mxq_configs,
        },
    }
    do_expermient_fdata("quant_mxq_compromise", models, quant_tasks)
    do_expermient_fdata("eval_mxq_compromise", models, ppl_tasks)


########################################################################
#  Misc experiments
########################################################################


def experiment_fp16_llama3_8B_OOM():
    models = ALL_MODELS[-1:]
    type = "eval_ppl"
    algo = "fp16"
    tasks = {
        algo: {
            "type": type,
            "configs": [
                ("base", {}),
            ],
        },
    }
    do_expermient_fdata(
        f"{type}_llama3_8B_OOM_{algo}",
        models,
        tasks,
        track_cuda_memory=True,
    )


def experiment_fp16_vs_hqq_eval_gpu_mem():
    models = ALL_MODELS[-1:]
    type = "eval_ppl"
    algo = "fp16"
    tasks = {
        algo: {
            "type": type,
            "configs": [
                ("base", {}),
            ],
        },
    }
    do_expermient_fdata("experiment_fp16_vs_hqq_eval_gpu_mem", models, tasks)
    algo = "hqq"
    tasks = {
        algo: {
            "type": type,
            "configs": HQQ_CONFIGS[1:2],
        },
    }
    do_expermient_fdata("experiment_fp16_vs_hqq_eval_gpu_mem", models, tasks)


def experiment_eval_model_storage():
    models = ALL_MODELS
    type = "eval_model_storage"
    tasks = {
        "fp16": {
            "type": type,
            "configs": [
                ("base", {}),
            ],
        },
        "mxq": {
            "type": type,
            "configs": MXQ_CONFIGS,
        },
        "hqq": {
            "type": type,
            "configs": HQQ_CONFIGS,
        },
        "awq": {
            "type": type,
            "configs": AUTOAWQ_CONFIGS,
        },
        "gptq": {"type": type, "configs": GPTQ_CONFIGS},
    }
    for i in range(5):
        do_expermient_fdata(f"eval_model_storge_{i}", models, tasks)


def experiment_eval_ppl_all():
    models = ALL_MODELS
    type = "eval_ppl"
    tasks = {
        "fp16": {
            "type": type,
            "configs": [
                ("base", {}),
            ],
        },
        "mxq": {
            "type": type,
            "configs": MXQ_CONFIGS,
        },
        "hqq": {
            "type": type,
            "configs": HQQ_CONFIGS,
        },
        "awq": {
            "type": type,
            "configs": AUTOAWQ_CONFIGS,
        },
        "gptq": {"type": type, "configs": GPTQ_CONFIGS},
    }
    do_expermient_fdata("experiment_eval_ppl_all", models, tasks)


def experiment_debug_quant_hqq():
    models = [ALL_MODELS[1]]
    type = "eval_model_storage"
    algo = "hqq"
    tasks = {
        algo: {
            "type": type,
            "configs": HQQ_CONFIGS[1:2],
        },
    }

    do_expermient(
        f"debug_{type}_{algo}",
        models,
        tasks,
        quant_dir="/fdata/llm/mxq/snapshots-debug",
        result_dir="/fdata/llm/mxq/results",
    )


def main():
    logging.basicConfig(
        format="%(asctime)s %(levelname)s [%(name)s] %(message)s",
        level=logging.INFO,
        datefmt="%Y-%m-%d %H:%M:%S",
    )

    # experiment_llm_leaderboard_autogptq()
    # experiment_llm_leaderboard_fp16()
    # experiment_llm_leaderboard_hqq()
    # experiment_llm_leaderboard_autoawq()
    # experiment_quant_hqq()
    # experiment_quant_mxq()
    # experiment_quant_awq()
    # experiment_quant_gptq()
    # experiment_ppl_eval_fp16()
    # experiment_ppl_eval_hqq()
    # experiment_ppl_eval_gptq()
    # experiment_ppl_eval_awq()
    # experiment_fp16_llama3_8B_OOM()
    # experiment_fp16_vs_hqq_eval_gpu_mem()
    # experiment_debug_quant_hqq()
    # experiment_eval_model_storage()
    # experiment_eval_ppl_all()
    experiment_debug_quant_hqq()


if __name__ == "__main__":
    # os.environ['HF_DATASETS_OFFLINE'] = '1'

    max_threads = str(min(8, os.cpu_count()))
    os.environ["OMP_NUM_THREADS"] = max_threads
    os.environ["OPENBLAS_NUM_THREADS"] = max_threads
    os.environ["MKL_NUM_THREADS"] = max_threads
    os.environ["VECLIB_MAXIMUM_THREADS"] = max_threads
    os.environ["NUMEXPR_NUM_THREADS"] = max_threads
    os.environ["NUMEXPR_MAX_THREADS"] = max_threads
    os.environ["HF_HOME"] = "/data/hugginface/"

    main()