File size: 6,453 Bytes
1e80dec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""The flat, long-format merged_models.csv (codebase-audit brief section 8).

One row per model pair x merging method x alignment configuration x benchmark x task x metric,
built by joining summary.csv to merges.csv and models.csv. Nothing here is entered by hand.

Fields no result file records (parent checkpoint hashes, code commit, evaluation timestamp,
dataset revision for most historical runs) are left null, and `limitations` says why.
`result_status` keeps apart the cases that must not be conflated:
    evaluated                 merge and both parents scored under one protocol
    parent_baseline_missing   merge scored, but a parent was not scored in the same experiment
    naive_baseline_missing    no naive (unaligned plain-average) twin in the same experiment
    naive_baseline_not_applicable  the method has no plain-average twin by design (chat vector, OT)
    evaluation_failed         the source records a failed run
    unverified                scored, but the source flags the value (e.g. calibration overlap)
Pairs x methods that were never run have no row: "not evaluated" is absence, never a filled value.
"""
import hashlib

import pandas as pd

FLAT = [
    # identity and provenance
    "experiment_id", "merge_id", "model_family", "model_a", "model_b",
    "model_a_checkpoint", "model_b_checkpoint", "model_a_hash", "model_b_hash",
    "model_a_parameters", "model_b_parameters", "architecture", "tokenizer_a", "tokenizer_b",
    "initialisation_relationship", "training_relationship", "tokenizer_relationship",
    "provenance_group",
    # method
    "merge_method", "alignment_method", "vocabulary_transport", "interpolation_strength",
    "method_configuration", "random_seed", "copies_parent", "is_control",
    # evaluation
    "benchmark", "task", "split", "dataset_revision", "evaluation_samples", "evaluation_metric",
    "metric_direction", "model_a_score", "model_b_score", "better_parent_score",
    "naive_merge_score", "merged_model_score", "merge_loss", "merge_gain", "merge_success",
    "alignment_gain",
    # reproducibility
    "code_commit", "config_hash", "evaluation_timestamp", "checkpoint_path",
    "evaluation_status", "result_status", "limitations", "notes", "source_file",
]

NO_NAIVE_REFERENCE = {"chat_vector", "ot_fusion"}

NOT_RECORDED = ("model_a_hash, model_b_hash, code_commit and evaluation_timestamp are not recorded "
                "in the historical result files")


def _status(r):
    if str(r["evaluation_status"]) == "failed":
        return "evaluation_failed"
    if pd.isna(r["best_parent_value"]):
        return "parent_baseline_missing"
    if str(r["evaluation_status"]) in ("unverified", "partial"):
        return "unverified"
    if pd.isna(r["naive_value"]):
        # chat-vector transfer and OT fusion have no plain-average twin by design
        if r["merge_method"] in NO_NAIVE_REFERENCE:
            return "naive_baseline_not_applicable"
        return "naive_baseline_missing"
    return "evaluated"


def flatten(summary: pd.DataFrame, merges: pd.DataFrame, models: pd.DataFrame,
            evals: pd.DataFrame) -> pd.DataFrame:
    m = merges.set_index("merge_id")
    mod = models.set_index("model_id")
    # per-measurement metadata from the merge's own evaluation row
    ev = evals[evals["merge_id"].fillna("") != ""].drop_duplicates(
        ["merge_id", "task_name", "dataset_config", "split", "metric_name"]).set_index(
        ["merge_id", "task_name", "dataset_config", "split", "metric_name"])

    def g(df, key, col):
        try:
            v = df.at[key, col]
        except KeyError:
            return None
        return None if (isinstance(v, float) and pd.isna(v)) else v

    rows = []
    for _, s in summary.iterrows():
        r = m.loc[s["merge_id"]]
        a, b = r["parent_a"], r["parent_b"]
        ek = (s["merge_id"], s["task_name"], s["dataset_config"], s["split"], s["metric_name"])
        cfg = "" if pd.isna(r["merge_config"]) else str(r["merge_config"])
        out = {
            "experiment_id": s["experiment_id"], "merge_id": s["merge_id"],
            "model_family": r["experiment_family"], "model_a": a, "model_b": b,
            "model_a_checkpoint": g(mod, a, "checkpoint_uri"), "model_b_checkpoint": g(mod, b, "checkpoint_uri"),
            "model_a_hash": None, "model_b_hash": None,
            "model_a_parameters": g(mod, a, "parameters"), "model_b_parameters": g(mod, b, "parameters"),
            "architecture": r["model_architecture"],
            "tokenizer_a": g(mod, a, "tokenizer"), "tokenizer_b": g(mod, b, "tokenizer"),
            "initialisation_relationship": r["initialisation_relationship"],
            "training_relationship": r["training_relationship"],
            "tokenizer_relationship": r["tokenizer_relationship"],
            "provenance_group": f"{r['initialisation_relationship']}|{r['training_relationship']}|{r['tokenizer_relationship']}",
            "merge_method": r["merge_method"], "alignment_method": r["alignment_method"],
            "vocabulary_transport": r["vocabulary_transport"], "interpolation_strength": r["merge_alpha"],
            "method_configuration": cfg, "random_seed": r["experiment_seed"],
            "copies_parent": r["copies_parent"], "is_control": r["is_control"],
            "benchmark": s["task_name"], "task": s["dataset_config"], "split": s["split"],
            "dataset_revision": None, "evaluation_samples": g(ev, ek, "n_examples"),
            "evaluation_metric": s["metric_name"], "metric_direction": s["metric_direction"],
            "model_a_score": s["parent_a_value"], "model_b_score": s["parent_b_value"],
            "better_parent_score": s["best_parent_value"], "naive_merge_score": s["naive_value"],
            "merged_model_score": s["method_value"], "merge_loss": s["method_merge_loss"],
            "merge_gain": s["merge_gain"], "merge_success": s["merge_success"],
            "alignment_gain": s["alignment_gain"],
            "code_commit": None, "config_hash": hashlib.sha1(cfg.encode()).hexdigest()[:12] if cfg else None,
            "evaluation_timestamp": None, "checkpoint_path": r["checkpoint_uri"],
            "evaluation_status": g(ev, ek, "evaluation_status") or r["evaluation_status"],
            "limitations": NOT_RECORDED, "notes": r["notes"], "source_file": r["source_file"],
        }
        out["result_status"] = _status(s)
        rows.append(out)
    return pd.DataFrame(rows, columns=FLAT)