File size: 2,830 Bytes
f640c46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Memory-only patch for the OKMeans authors' code (github.com/MasterZhou1/Robust-Clustering, commit d820666).

Their `compute_distances_to_centers_vectorized` and `assign_to_clusters_vectorized` broadcast an n x k x d float64
array (4.3 GB on KDDFULL, 7.2 GB on SUSY) plus its square, which exceeds a 15 GB machine on the 5M-row datasets.
This appends row-chunked wrappers to `algorithms/kmeans/vectorized.py`. Every row's distances, argmin and minimum
are computed independently of the other rows, so the wrappers return bit-identical arrays; this is checked by
re-running the small datasets with the patch and comparing every cost to the unpatched runs.

It also registers the four held-out datasets of ARIES #2 (COVTYPE-5/10, DEEP-5/10; k = 10) in their
experiments/config.py, so their own benchmark driver can run on them. No algorithm code is touched by that part.

Usage: python benchmarks/patch_official_memory.py PATH_TO_CLONE   (idempotent)
"""
import os, sys

MARK = "# --- ARIES memory patch"
PATCH = '''

# --- ARIES memory patch: row-chunked wrappers. Rows are independent, so results are bit-identical. ---
_ARIES_CHUNK = 262144
_aries_dist_orig = compute_distances_to_centers_vectorized
_aries_assign_orig = assign_to_clusters_vectorized


def compute_distances_to_centers_vectorized(X, centers, squared=True):
    if X.shape[0] <= _ARIES_CHUNK:
        return _aries_dist_orig(X, centers, squared)
    return np.concatenate([_aries_dist_orig(X[s:s + _ARIES_CHUNK], centers, squared)
                           for s in range(0, X.shape[0], _ARIES_CHUNK)])


def assign_to_clusters_vectorized(X, centers):
    if X.shape[0] <= _ARIES_CHUNK:
        return _aries_assign_orig(X, centers)
    parts = [_aries_assign_orig(X[s:s + _ARIES_CHUNK], centers) for s in range(0, X.shape[0], _ARIES_CHUNK)]
    return np.concatenate([p[0] for p in parts]), np.concatenate([p[1] for p in parts])
'''

path = os.path.join(sys.argv[1], "algorithms", "kmeans", "vectorized.py")
src = open(path).read()
if MARK in src:
    print("already patched:", path)
else:
    open(path, "a").write(PATCH)
    print("patched:", path)

cfg = os.path.join(sys.argv[1], "experiments", "config.py")
src = open(cfg).read()
CFG_MARK = "# --- ARIES held-out datasets"
if CFG_MARK not in src:
    open(cfg, "a").write(CFG_MARK + """ (same recipe as SKIN/SUSY: z-score, 1% uniform outliers, k = 10) ---
DATASETS.update({
    "COVTYPE-5": _dataset("COVTYPE-5.npz", k=10, memory_gb=1.0, base_time=0.5),
    "COVTYPE-10": _dataset("COVTYPE-10.npz", k=10, memory_gb=1.0, base_time=0.5),
    "DEEP-5": _dataset("DEEP-5.npz", k=10, memory_gb=4.0, base_time=2.0),
    "DEEP-10": _dataset("DEEP-10.npz", k=10, memory_gb=4.0, base_time=2.0),
})
""")
    print("registered held-out datasets:", cfg)
else:
    print("held-out datasets already registered:", cfg)