File size: 2,336 Bytes
a4054b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# MIT License, Bryan Cheng, 2026. One-off edit (D-038 follow-up): hardware-configuration centering in crosses.py.
p = "src/analysis/crosses.py"
s = open(p).read()
s = s.replace('''    deg = np.array([r.get("ppl_ratio", 1.0) > 3 for r in recs])
    return D, A, Y, deg
''', '''    deg = np.array([r.get("ppl_ratio", 1.0) > 3 for r in recs])
    hw = np.array([f"{r.get('gpu', '?')}|{r.get('bs', '?')}" for r in recs])
    return D, A, Y, deg, hw


def hw_center(Y, hw, rows):
    """Offspring are measured on several GPU models / batch sizes (D-035, D-038). Parents are assigned at random, so
    hardware is independent of genotype; each hardware group is shifted to the overall mean, with group and overall
    means estimated on `rows` only (training rows in analyze, so held-out offspring do not inform the adjustment)."""
    out = Y.copy()
    groups = np.unique(hw)
    if len(groups) < 2:
        return out
    with np.errstate(all="ignore"):
        mu = np.nanmean(Y[:, rows], 1)
        for g in groups:
            gm = hw == g
            if (gm & rows).sum() == 0:
                continue
            out[:, gm] = Y[:, gm] - np.nanmean(Y[:, gm & rows], 1)[:, None] + mu[:, None]
    return out
''')
s = s.replace('''    D, A, Y, deg = load_offspring(a.design, P, prim)
    npar, S = len(D["parents"]), A.shape[1]
    ref = D["parents"].index("__base__")
    keep = ~deg
''', '''    D, A, Y, deg, hw = load_offspring(a.design, P, prim)
    npar, S = len(D["parents"]), A.shape[1]
    ref = D["parents"].index("__base__")
    keep = ~deg
    hw_groups = {g: int((hw[keep] == g).sum()) for g in np.unique(hw[keep])}
''')
s = s.replace('''    if a.cmd == "layers":
        var = np.zeros(S)''', '''    if a.cmd == "layers":
        Y = hw_center(Y, hw, keep)
        var = np.zeros(S)''')
s = s.replace('''    te[rng.permutation(n)[: n // 5]] = True
''', '''    te[rng.permutation(n)[: n // 5]] = True
    train_full = keep.copy()
    train_full[keep] = ~te
    Y = hw_center(Y, hw, train_full)
''')
s = s.replace('''"resolution": D.get("resolution"), "traits": {}}''', '''"resolution": D.get("resolution"), "hardware_groups": hw_groups, "traits": {}}''')
assert s.count("hw_center(Y, hw") == 3 and '"hardware_groups": hw_groups' in s and "D, A, Y, deg, hw = load_offspring" in s
open(p, "w").write(s)
print("crosses hw ok")