File size: 1,813 Bytes
55c92b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
def sort_with_indices(num_list: list[float], reverse: bool = True) -> list[tuple[int, float]]:
    """
    将数字列表从大到小排序,并返回(原索引, 对应值)的列表
    
    Args:
        num_list: 待排序的数字列表
    
    Returns:
        按值降序排列的(原索引, 值)元组列表
    
    Example:
        >>> lst = [3.2, 1.5, 4.8]
        >>> sort_with_indices(lst)
        [(2, 4.8), (0, 3.2), (1, 1.5)]
    """
    # 用enumerate获取(索引, 值)元组,再按值降序排序
    sorted_items = sorted(enumerate(num_list), key=lambda x: x[1], reverse=reverse)
    return sorted_items

import json
import os
module_name = "mlp"
metric_dir = "/mnt/bn/life-mllm/users/cxr/quantization/quantization_metric/metrics"
# file_path = f"/mnt/bn/life-mllm/users/cxr/quantization/quantization_metric/metrics/alpha/alpha_{module_name}_Llama-2-7b-hf.json"
model_name = "Llama-3.1-8B"

sec_dirs = os.listdir(metric_dir)
file_paths = [os.path.join(metric_dir, model_name, f"{sec_dir}.json") for sec_dir in sec_dirs]
# print(file_paths)
for file_path in file_paths:
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            data = json.load(f)

        # if "alpha" in file_path or "alpha_hat" in file_path or "coherence" in file_path:
        #     sorted_items = sort_with_indices(data, reverse=True)
        # elif "stable_rank" in file_path or "effective_rank" in file_path or "ZD" in file_path or "head_diversity" in file_path:
        #     sorted_items = sort_with_indices(data, reverse=False)
        
        sorted_items = sort_with_indices(data, reverse=True)
        sorted_items = [a[0] for a in sorted_items]
        print(f"{file_path}: ")
        print(sorted_items)
        print()
    except:
        print(f"error in {file_path}")
        print()