File size: 9,723 Bytes
18e32a8
 
 
 
 
 
 
 
 
 
 
 
 
 
0ad8c9c
 
22c7d63
0ad8c9c
18e32a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ad8c9c
18e32a8
0ad8c9c
 
18e32a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ad8c9c
18e32a8
 
 
 
 
 
 
 
 
0ad8c9c
18e32a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ad8c9c
18e32a8
 
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

import pandas as pd
import numpy as np
from rouge_score import rouge_scorer
from joblib import Parallel, delayed
from selfrank.algos.greedy import SelfRankGreedy
from selfrank.algos.iterative import SelfRank
from selfrank.algos.baseline import MCARank
from selfrank.algos.triplet import equality, rouge, noisy_equality
import matplotlib.pyplot as plt
from itertools import zip_longest
from uuid import uuid4
import csv, os
from functools import partial
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


def generate_data(max_acc, min_acc, nmodels, nanswers, nquestions) -> tuple[pd.DataFrame, list]:

    np.random.seed(42)
    # Spread model accuracies between min and max
    model_acc = np.linspace(max_acc, min_acc, nmodels)

    gt_and_model_ans = np.zeros(
        (nquestions, nmodels + 1), dtype=int
    )  # array to store ground truth and model ans

    # Create ground truth answers i.e. first column
    for i in range(nquestions):
        gt_and_model_ans[i][0] = np.random.randint(nanswers)

    for i in range(0, nmodels):
        no_of_entries_frm_gt = np.ceil(model_acc[i] / 100 * (nquestions)).astype(int)
        # print(no_of_entries_frm_gt)
        offsets_to_match = np.random.permutation(nquestions)[0:no_of_entries_frm_gt]
        # print(offsets_to_match)
        for j in range(nquestions):
            if j in offsets_to_match:
                gt_and_model_ans[j][i + 1] = gt_and_model_ans[j][0]
            else:
                lst_wo_gt = list(range(nanswers))
                lst_wo_gt.remove(gt_and_model_ans[j][0])
                gt_and_model_ans[j][i + 1] = lst_wo_gt[np.random.randint(nanswers - 1)]

    # print(gt_and_model_ans)
    filename = str(uuid4())

    fields = ["GT"]
    for i in range(nmodels):
        fields.append("M" + str(i + 1))

    # writing to csv file
    with open(filename, "w") as csvfile:
        # creating a csv writer object
        csvwriter = csv.writer(csvfile)

        # writing the fields
        csvwriter.writerow(fields)

        # writing the data rows
        csvwriter.writerows(gt_and_model_ans)

    df = pd.read_csv(filename)
    os.remove(filename)

    true_ranking = [f"M{i}" for i in range(1, nmodels + 1)]

    return df, true_ranking

def synth_executor(acc_range: tuple[float, float], nmodels, nanswers, nquestions, noise, method) -> tuple[str, dict]:


    min_acc, max_acc = acc_range
    logger.info(f"Synth experiment: min_acc:{min_acc}, max_acc:{max_acc}, nmodels: {nmodels}, nanswers: {nanswers}, nquestions: {nquestions}, noise:{noise}, method:{method}.")

    df, true_ranking = generate_data(max_acc, min_acc, nmodels, nanswers, nquestions)

    if noise == 0.:
        comp = equality
    else:
        comp = partial(noisy_equality, p=noise)
    
    df = df.drop(columns=["GT"])
    MODELS = df.columns.tolist()

    if method == "Full":
        ranker = SelfRank(MODELS, comp, true_ranking)
        ranker.fit(df)

        # outputs of interest
        out = {
            "true_ranking": true_ranking,
            "estimated_ranking": ranker.ranking,
            "rbo": ranker.measure(metric="rbo"),
            "map-1": ranker.measure(metric='mapk', k=1),
            "map-3": ranker.measure(metric='mapk', k=3),
            "map-5": ranker.measure(metric='mapk', k=5),
            "map-10": ranker.measure(metric='mapk', k=10)
        }

    elif method == "Greedy":
        ranker = SelfRankGreedy(MODELS, comp, true_ranking)
        ranker.fit(df)
        out = {
            "true_ranking": true_ranking,
            "estimated_ranking": ranker.ranking,
            "rbo": ranker.measure(metric="rbo"),
            "map-1": ranker.measure(metric='mapk', k=1),
            "map-3": ranker.measure(metric='mapk', k=3),
            "map-5": ranker.measure(metric='mapk', k=5),
            "map-10": ranker.measure(metric='mapk', k=10)
        }
    elif method == 'MCA':
        ranker = MCARank(MODELS, comp, true_ranking)
        ranker.fit(df, measure='noisy_equality', p=noise)
        out = {
            "true_ranking": true_ranking,
            "estimated_ranking": ranker.ranking,
            "rbo": ranker.measure(metric="rbo"),
            "map-1": ranker.measure(metric='mapk', k=1),
            "map-3": ranker.measure(metric='mapk', k=3),
            "map-5": ranker.measure(metric='mapk', k=5),
            "map-10": ranker.measure(metric='mapk', k=10)
        }
    else:
        raise ValueError(f"{method} not understood.")

    eval_metrics = (
            f"<h2 style='color: purple;'> Evaluation measures </h2>"
            f"Rank-Biased Overlap: {out['rbo']:0.3f}<br>"
            f"MAP-3              : {out['map-3']:0.3f}<br>"
            f"MAP-5              : {out['map-5']:0.3f}<br>"
            f"MAP-10             : {out['map-10']: 0.3f}."
        )

    out_plot = ranker.plot("synth")
    plt.close(out_plot)

    return "synth.png", eval_metrics



def benchmark_executor(data, mmlu_subject, evaluation, nmodels, nrows, method
    ) -> tuple[pd.DataFrame, plt.figure]:
        """Main execution flow for benchmarks"""

        logger.info(f"Benchmark experiment: benchmark:{data}, mmlu subject: {mmlu_subject}, evaluation:{evaluation}, nmodels:{nmodels}, nquestions: {nrows}, method: {method}.")
        seed = 40
        np.random.seed(seed)

        match data:
            case "MMLU":
                adf = pd.read_pickle(f"data/mmlu_subject_{mmlu_subject}.pkl")

            case "CNN/DM":
                adf = pd.read_pickle(f"data/cnndm.pkl")

            case "XSUM":
                adf = pd.read_pickle(f"data/xsum.pkl")

            case _:
                raise ValueError(f"'{data}' not understood.")

        MODELS = adf.model.unique()

        # Sample fewer models if so needed
        if nmodels != "All":
            if nmodels < len(MODELS):

                MODELS = np.random.choice(MODELS, nmodels, replace=False).tolist()
                adf = adf[adf.model.isin(MODELS)]

        match data:
            case "MMLU":
                keys = [
                    "id",
                    "trial_id",
                    "perturbation",
                ]  # MMLU has this extra parameter
            case "CNN/DM" | "XSUM":
                keys = ["id", "trial_id"]
            case _:
                pass

        df = adf.pivot_table(
            columns="model",
            index=keys,
            values="output",
            aggfunc="first",
        )

        # Filter by number of rows
        df.dropna(inplace=True)
        if nrows != "All":
            if nrows < df.shape[0]:
                df = df.sample(nrows, random_state=seed)

        # Compute true ranking
        adf = adf.set_index(keys).loc[df.index].reset_index()

        if evaluation == "Rouge":

            def __true_rouge(x, scorer):
                return scorer.score(x["reference"], x["output"])["rouge2"].fmeasure

            scorer = rouge_scorer.RougeScorer(["rouge2"], use_stemmer=True)
            adf["rouge"] = Parallel(n_jobs=-1, batch_size=128)(
                delayed(__true_rouge)(i, scorer) for _, i in adf.iterrows()
            )

            # Method 2 - look at "win rates" - for each question, see which model
            # wins (i.e. has the best ROUGE score)
            idx = adf.groupby(["id", "trial_id"])["rouge"].idxmax()
            win_rates = adf.loc[idx].model.value_counts()
            win_rate_rank = win_rates.index.tolist()

            # include models with nowins at the bottom
            no_wins = list(set(MODELS) - set(win_rate_rank))
            true_ranking = win_rate_rank + no_wins
            evaluator = rouge

        elif evaluation == "Equality":

            # Compute the true ranking (multiple choice - so use equality between
            # LLM response and reference-value)
            adf["C"] = (adf.output == adf.reference).astype(int)
            true_ranking = (
                adf.groupby("model")["C"]
                .apply(lambda x: sum(x) / len(x))
                .sort_values(ascending=False)
                .index.tolist()
            )
            evaluator = equality

        else:
            raise ValueError(f"'{evaluation}' not understood.")

        match method:
            case "Full":
                ranker = SelfRank(MODELS, evaluator, true_ranking)

            case "Greedy":
                ranker = SelfRankGreedy(MODELS, evaluator, true_ranking)

            case "MCA":
                raise NotImplementedError
            case _:
                raise ValueError(f"'{method}' not understood.")

        # generate outputs
        ranker.fit(df)
        ranks = ranker.ranking
        
        ranks = [
            j + i for i, j in zip_longest(ranks, ["πŸ₯‡ ", "πŸ₯ˆ ", "πŸ₯‰ "], fillvalue="")
        ]
        out_df = pd.DataFrame({"rank": range(1, len(true_ranking) + 1), "model": ranks})

        out_metrics = {
            "rbo": ranker.measure(metric="rbo"),
            "map-1": ranker.measure(metric="mapk", k=1),
            "map-3": ranker.measure(metric="mapk", k=3),
            "map-5": ranker.measure(metric="mapk", k=5),
            "map-10": ranker.measure(metric="mapk", k=10),
            "evaluations": evaluator.calls,
        }
        eval_metrics = (
            f"<h2 style='color: purple;'> Evaluation measures </h2>"
            f"Rank-Biased Overlap: {out_metrics['rbo']:0.3f}<br>"
            f"MAP-3              : {out_metrics['map-3']:0.3f}<br>"
            f"MAP-5              : {out_metrics['map-5']:0.3f}<br>"
            f"MAP-10             : {out_metrics['map-10']: 0.3f}."
        )

        out_plot = ranker.plot()
        plt.close(out_plot)

        return out_df, "output.png", eval_metrics