Repository navigation
Expand file tree
/
Copy pathkernel_speed.py
More file actions
executable file
·104 lines (84 loc) · 4.73 KB
/
Copy pathkernel_speed.py
File metadata and controls
executable file
·104 lines (84 loc) · 4.73 KB
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
#!/usr/bin/env python
"""Times the two things pycleora does - building the graph and one embedding
iteration - on a synthetic power-law graph, with whatever pycleora is installed.
python benchmarks/kernel_speed.py # 2^20 nodes, 12M edges, 1024 dimensions
python benchmarks/kernel_speed.py --scale 18 --edges 2500000 --threads 10,4,1
It only uses calls that exist in pycleora 3.2 as well (``SparseMatrix.from_files``,
``embed_fast``), so the same script compares releases: run it once per installed version.
The graph is an R-MAT graph (Chakrabarti et al., 2004; a = 0.57, b = c = 0.19), whose
skewed degrees are what makes the propagation hard to parallelise. It is written to a
file as one two-node hyperedge per line and read with the column
``complex::reflexive::node``.
Reported, best of ``--repeats`` runs each:
* build file to sparse matrix
* per iteration (embed with ``--iterations`` iterations - embed with none) / iterations:
one propagation and row normalisation of the whole embedding matrix
* row by row the same for the in-place propagation of an existing array, which
reads whole rows instead of working through the matrix in blocks of
columns (pycleora >= 4.0; the results of the two are bit-identical)
"""
import argparse
import os
import tempfile
import time
import numpy as np
COLUMNS = "complex::reflexive::node"
def rmat_edges(scale, num_edges, seed=0, a=0.57, b=0.19, c=0.19):
"""Edges of an R-MAT graph with ``2 ** scale`` node ids; self-loops removed."""
generator = np.random.default_rng(seed)
source = np.zeros(num_edges, dtype=np.int64)
target = np.zeros(num_edges, dtype=np.int64)
for _ in range(scale):
draw = generator.random(num_edges)
source = (source << 1) | (draw >= a + b)
target = (target << 1) | (((draw >= a) & (draw < a + b)) | (draw >= a + b + c))
keep = source != target
return source[keep], target[keep]
def write_edge_list(path, source, target, block=1_000_000):
with open(path, "w") as handle:
for start in range(0, len(source), block):
lines = np.char.add(np.char.add(source[start:start + block].astype(str), " "),
target[start:start + block].astype(str))
handle.write("\n".join(lines.tolist()) + "\n")
def best(function, repeats):
times = []
for _ in range(repeats):
started = time.perf_counter()
function()
times.append(time.perf_counter() - started)
return min(times)
def main():
parser = argparse.ArgumentParser(description=__doc__.split("\n")[0])
parser.add_argument("--scale", type=int, default=20, help="2 ** scale node ids (default 20)")
parser.add_argument("--edges", type=int, default=12_000_000)
parser.add_argument("--dim", type=int, default=1024)
parser.add_argument("--iterations", type=int, default=4)
parser.add_argument("--repeats", type=int, default=3)
parser.add_argument("--threads", default=None,
help="comma-separated thread counts (pycleora >= 4.0); default: the library's own default")
args = parser.parse_args()
import pycleora
from pycleora import SparseMatrix
version = getattr(pycleora, "__version__", "3.x")
source, target = rmat_edges(args.scale, args.edges)
with tempfile.TemporaryDirectory() as directory:
path = os.path.join(directory, "rmat.tsv")
write_edge_list(path, source, target)
for threads in [None] if args.threads is None else [int(t) for t in args.threads.split(",")]:
workers = {} if threads is None else {"num_workers": threads}
graphs = []
build = best(lambda: graphs.append(SparseMatrix.from_files([path], COLUMNS, **workers)), args.repeats)
graph = graphs[-1]
nothing = best(lambda: graph.embed_fast(args.dim, 0, **workers), args.repeats)
rounds = best(lambda: graph.embed_fast(args.dim, args.iterations, **workers), args.repeats)
row_by_row = ""
if hasattr(graph, "left_markov_propagate_normalized_inplace"):
vectors = graph.embed_fast(args.dim, 0, **workers)
in_place = best(lambda: graph.left_markov_propagate_normalized_inplace(vectors, args.iterations, **workers),
args.repeats)
row_by_row = f" row by row {in_place / args.iterations:.3f} s"
print(f"pycleora {version} threads={'default' if threads is None else threads:>7} "
f"{graph.num_entities:,} nodes {len(source):,} edges dim {args.dim} "
f"build {build:.2f} s per iteration {(rounds - nothing) / args.iterations:.3f} s{row_by_row}", flush=True)
if __name__ == "__main__":
main()