|
| 1 | +"""Benchmark RAG label accumulation (``accumulate_labels``). |
| 2 | +
|
| 3 | +The code review replaced the per-thread ``n_threads x n_nodes`` map-of-maps with |
| 4 | +a single combined-key ``(node, other)`` histogram per thread, and made the |
| 5 | +per-node argmax single-threaded. This script times ``accumulate_labels`` across |
| 6 | +thread counts (to confirm the allocation win and rule out a high-thread-count |
| 7 | +regression from the now-sequential argmax) and across ``other``-label |
| 8 | +cardinalities. |
| 9 | +
|
| 10 | +Inputs: a deterministic block-wise over-segmentation (each node is a contiguous |
| 11 | +block, as in a real over-segmentation), so the RAG adjacency is local. |
| 12 | +
|
| 13 | +Not part of the test suite. Run:: |
| 14 | +
|
| 15 | + python development/graph/benchmark_accumulate_labels.py --repeats 11 |
| 16 | +""" |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import argparse |
| 20 | +from statistics import median |
| 21 | +from time import perf_counter |
| 22 | + |
| 23 | +import numpy as np |
| 24 | + |
| 25 | +import bioimage_cpp as bic |
| 26 | + |
| 27 | + |
| 28 | +def _timeit(fn, repeats: int, warmup: int = 1) -> dict: |
| 29 | + for _ in range(warmup): |
| 30 | + fn() |
| 31 | + timings = [] |
| 32 | + for _ in range(repeats): |
| 33 | + t0 = perf_counter() |
| 34 | + fn() |
| 35 | + timings.append(perf_counter() - t0) |
| 36 | + return {"median": median(timings), "min": min(timings), "n": repeats} |
| 37 | + |
| 38 | + |
| 39 | +def _make_labels(shape=(40, 256, 256), coarsen=(2, 4, 4)) -> np.ndarray: |
| 40 | + coarse_shape = tuple(s // c for s, c in zip(shape, coarsen)) |
| 41 | + n_nodes = int(np.prod(coarse_shape)) |
| 42 | + coarse = np.arange(n_nodes, dtype=np.uint64).reshape(coarse_shape) |
| 43 | + block = np.ones(coarsen, dtype=np.uint64) |
| 44 | + return np.ascontiguousarray(np.kron(coarse, block)) |
| 45 | + |
| 46 | + |
| 47 | +def run(repeats: int = 11) -> dict: |
| 48 | + labels = _make_labels() |
| 49 | + rag = bic.graph.region_adjacency_graph(labels) |
| 50 | + n_nodes = int(rag.number_of_nodes) |
| 51 | + rng = np.random.default_rng(0) |
| 52 | + |
| 53 | + results: dict[str, dict] = {} |
| 54 | + for n_other, tag in ((8, "dense8"), (2000, "sparse2000")): |
| 55 | + other = rng.integers(0, n_other, size=labels.shape).astype(np.uint64) |
| 56 | + other = np.ascontiguousarray(other) |
| 57 | + for threads in (1, 4, 8): |
| 58 | + def fn(t=threads, o=other): |
| 59 | + bic.graph.features.accumulate_labels(rag, labels, o, number_of_threads=t) |
| 60 | + |
| 61 | + key = f"accumulate_labels_{tag}_t{threads}" |
| 62 | + results[key] = _timeit(fn, repeats) |
| 63 | + results[key]["meta"] = { |
| 64 | + "n_nodes": n_nodes, |
| 65 | + "n_pixels": int(labels.size), |
| 66 | + "n_other": n_other, |
| 67 | + "threads": threads, |
| 68 | + } |
| 69 | + return results |
| 70 | + |
| 71 | + |
| 72 | +def main() -> None: |
| 73 | + parser = argparse.ArgumentParser(description=__doc__) |
| 74 | + parser.add_argument("--repeats", type=int, default=11) |
| 75 | + args = parser.parse_args() |
| 76 | + results = run(repeats=args.repeats) |
| 77 | + for name, r in results.items(): |
| 78 | + print(f"{name:<32} median={r['median'] * 1e3:8.3f} ms min={r['min'] * 1e3:8.3f} ms") |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + main() |
0 commit comments