Download code/03_roots.py from akilhassane/open-model-lineage: direct link, hf CLI and curl.
- Browser
- Download file 1.83 kB
-
https://huggingface.co/datasets/akilhassane/open-model-lineage/resolve/main/code/03_roots.py
- Command line
-
hf download hf://datasets/akilhassane/open-model-lineage/code/03_roots.py
-
curl -L -o 03_roots.py https://huggingface.co/datasets/akilhassane/open-model-lineage/resolve/main/code/03_roots.py
1.83 kB
| """Resolve every model to its ultimate root(s) and generation depth. | |
| A root is a model with no declared parent, or a parent that is not in the public listing | |
| (deleted, private or mistyped: kept as an 'external' root under its own name). | |
| A model descending from k roots (merges) gives each root weight 1/k. | |
| Outputs: roots_of.csv.gz (id, root, weight, depth), root_authors.csv (derivative counts). | |
| """ | |
| import sys | |
| from collections import defaultdict | |
| import pandas as pd | |
| D = sys.argv[1] if len(sys.argv) > 1 else "data" | |
| sys.setrecursionlimit(100000) | |
| N = pd.read_csv(f"{D}/nodes.csv.gz", low_memory=False) | |
| E = pd.read_csv(f"{D}/edges.csv.gz") | |
| ids = set(N.id) | |
| par = defaultdict(list) | |
| for c, p in zip(E.child, E.parent): par[c].append(p) | |
| memo = {} | |
| def roots(x, stack=()): | |
| if x in memo: return memo[x] | |
| ps = [p for p in par.get(x, []) if p not in stack and p != x] | |
| if not ps: | |
| memo[x] = ({x}, 0); return memo[x] | |
| rs, dep = set(), 0 | |
| for p in ps: | |
| r, d = roots(p, stack + (x,)) if p in ids or p in par else ({p}, 0) | |
| rs |= r; dep = max(dep, d + 1) | |
| memo[x] = (rs, dep); return memo[x] | |
| # iterative warm-up in topological-ish order avoids deep recursion on long chains | |
| rows = [] | |
| for x in N.id: | |
| rs, d = roots(x) | |
| w = 1 / len(rs) | |
| for r in rs: rows.append((x, r, w, d)) | |
| R = pd.DataFrame(rows, columns=["id", "root", "weight", "depth"]) | |
| R["root_in_listing"] = R.root.isin(ids) | |
| R["root_author"] = R.root.str.split("/").str[0] | |
| R.to_csv(f"{D}/roots_of.csv.gz", index=False) | |
| der = R[R.depth > 0] | |
| top = (der.groupby("root_author").weight.sum().sort_values(ascending=False) | |
| .rename("derivatives").reset_index()) | |
| top.to_csv(f"{D}/root_authors.csv", index=False) | |
| print("models", N.shape[0], "derivatives", der.id.nunique(), "max depth", R.depth.max()) | |
| print(top.head(80).to_string()) | |