-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_bgen_benchmarks.py
More file actions
executable file
·447 lines (404 loc) · 16.6 KB
/
Copy pathrun_bgen_benchmarks.py
File metadata and controls
executable file
·447 lines (404 loc) · 16.6 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#!/usr/bin/env python3
"""Benchmark equivalent BGEN genotype matrices across Python readers.
Every reader materializes the same canonical ``float32`` array from the same
BGEN file. The runner rejects any cross-reader disagreement in shape, variant
positions, sample order, or values, so a completed run is evidence that the
readers agree, not just that they finished.
"""
from __future__ import annotations
import argparse
import hashlib
import importlib.metadata
import json
import os
import platform
import statistics
import subprocess
import sys
from pathlib import Path
import psutil
READERS = ("polars-bio", "snputils", "bgen", "pysnptools")
# The `bgen` package is the independent oracle every other reader is checked
# against; snputils uses it the same way in its own published benchmark.
REFERENCE_READER = "bgen"
DISTRIBUTIONS = {
"polars-bio": "polars-bio",
"snputils": "snputils",
"bgen": "bgen",
"pysnptools": "pysnptools",
}
WORKLOADS = ("dosage", "probabilities")
def file_sha256(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as handle:
for chunk in iter(lambda: handle.read(8 * 1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def bgen_is_phased(path: Path) -> bool:
"""Read the phased flag of the first variant's probability block."""
import struct
import zlib
with path.open("rb") as handle:
offset, header_length, _variants, n_samples = struct.unpack(
"<IIII", handle.read(16)
)
handle.seek(4 + header_length - 4)
compression = struct.unpack("<I", handle.read(4))[0] & 0b11
handle.seek(offset + 4)
def text(size_bytes: int) -> None:
length = struct.unpack(
"<H" if size_bytes == 2 else "<I", handle.read(size_bytes)
)[0]
handle.read(length)
text(2) # variant ID
text(2) # rsid
text(2) # chromosome
handle.read(4) # position
for _ in range(struct.unpack("<H", handle.read(2))[0]):
text(4) # allele
block_length = struct.unpack("<I", handle.read(4))[0]
if compression == 0:
block = handle.read(block_length)
else:
handle.read(4) # declared decompressed length
payload = handle.read(block_length - 4)
if compression == 1:
block = zlib.decompress(payload)
else:
import zstandard
block = zstandard.ZstdDecompressor().decompress(payload)
return bool(block[8 + n_samples])
def parse_result(output: str) -> dict:
for line in output.splitlines():
if line.startswith("BGEN_RESULT:"):
return json.loads(line.removeprefix("BGEN_RESULT:"))
raise RuntimeError(f"child did not emit BGEN_RESULT:\n{output}")
def run_one(python: str, env: dict[str, str], timeout: int) -> dict:
completed = subprocess.run(
[python, "-m", "benchmarks.bgen_matrix"],
check=False,
capture_output=True,
text=True,
env=env,
timeout=timeout,
)
print(completed.stdout, end="")
if completed.stderr:
print(completed.stderr, file=sys.stderr, end="")
if completed.returncode != 0:
raise subprocess.CalledProcessError(
completed.returncode,
completed.args,
output=completed.stdout,
stderr=completed.stderr,
)
return parse_result(completed.stdout)
def summarize(runs: list[dict]) -> dict:
times = [run["time_seconds"] for run in runs]
memories = [run["peak_rss_mb"] for run in runs]
return {
"runs": len(runs),
"mode": runs[0]["mode"],
"threads": runs[0]["threads"],
"time_seconds_median": round(statistics.median(times), 3),
"time_seconds_mean": round(statistics.mean(times), 3),
"time_seconds_stdev": round(statistics.stdev(times), 3)
if len(times) > 1
else 0.0,
"peak_rss_mb_median": round(statistics.median(memories), 1),
"peak_rss_mb_mean": round(statistics.mean(memories), 1),
"peak_rss_mb_stdev": round(statistics.stdev(memories), 1)
if len(memories) > 1
else 0.0,
"value_sha256": runs[0]["value_sha256"],
"emission_order_descents": [run["emission_order_descents"] for run in runs],
"raw": runs,
}
def installed_versions() -> dict[str, str]:
versions = {}
for reader, distribution in DISTRIBUTIONS.items():
try:
versions[reader] = importlib.metadata.version(distribution)
except importlib.metadata.PackageNotFoundError:
versions[reader] = "not-installed"
for distribution in ("numpy", "polars", "pyarrow"):
try:
versions[distribution] = importlib.metadata.version(distribution)
except importlib.metadata.PackageNotFoundError:
versions[distribution] = "not-installed"
return versions
def run_verification(
python: str, env: dict[str, str], left: str, right: str, workload: str, timeout: int
) -> dict:
"""Compare two readers element-wise in one process."""
child_env = env.copy()
child_env.update(
{
"BGEN_READER": right,
"BGEN_MODE": workload,
"BGEN_VERIFY_LEFT": left,
"BGEN_VERIFY_RIGHT": right,
"THREAD_NUM": "1",
"POLARS_MAX_THREADS": "1",
}
)
completed = subprocess.run(
[python, "-m", "benchmarks.bgen_verify"],
check=True,
capture_output=True,
text=True,
env=child_env,
timeout=timeout,
)
for line in completed.stdout.splitlines():
if line.startswith("BGEN_VERIFY:"):
return json.loads(line.removeprefix("BGEN_VERIFY:"))
raise RuntimeError(f"child did not emit BGEN_VERIFY:\n{completed.stdout}")
def check_equivalence(runs: list[dict]) -> dict:
"""Fail unless every reader produced the same content for a workload."""
by_workload: dict[str, list[dict]] = {}
for run in runs:
by_workload.setdefault(run["workload"], []).append(run)
checked = {}
for workload, workload_runs in by_workload.items():
reference = next(
(run for run in workload_runs if run["reader"] == REFERENCE_READER),
workload_runs[0],
)
# Shape and identity must match exactly for the comparison to mean
# anything, so a disagreement there is a hard failure.
for run in workload_runs:
for field in (
"rows",
"samples",
"width",
"values",
"position_sha256",
"sample_sha256",
):
if run[field] != reference[field]:
raise AssertionError(
f"{workload}: {run['reader']} (t={run['threads']}) disagrees "
f"with {reference['reader']} in {field}: "
f"{run[field]!r} != {reference[field]!r}"
)
# polars-bio is the reader under test, so it must reproduce the oracle
# bit for bit at every partition count.
for run in workload_runs:
if run["reader"] == "polars-bio" and run["value_sha256"] != reference["value_sha256"]:
raise AssertionError(
f"{workload}: polars-bio (t={run['threads']}) does not reproduce "
f"{reference['reader']} exactly: {run['value_sha256']} != "
f"{reference['value_sha256']}"
)
# Other readers may round differently; record which ones matched rather
# than hiding the difference behind a tolerance.
bit_identical = sorted(
{
run["reader"]
for run in workload_runs
if run["value_sha256"] == reference["value_sha256"]
}
)
differing = sorted(
{
run["reader"]
for run in workload_runs
if run["value_sha256"] != reference["value_sha256"]
}
)
checked[workload] = {
"reference_reader": reference["reader"],
"rows": reference["rows"],
"samples": reference["samples"],
"width": reference["width"],
"values": reference["values"],
"position_sha256": reference["position_sha256"],
"sample_sha256": reference["sample_sha256"],
"value_sha256": reference["value_sha256"],
"readers_checked": sorted({run["reader"] for run in workload_runs}),
"thread_counts_checked": sorted({run["threads"] for run in workload_runs}),
"bit_identical_to_reference": bit_identical,
"differing_from_reference": differing,
}
return checked
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--runs", type=int, default=3)
parser.add_argument("--python", default=sys.executable)
parser.add_argument("--timeout", type=int, default=3600)
parser.add_argument("--readers", nargs="+", choices=READERS, default=list(READERS))
parser.add_argument(
"--workloads", nargs="+", choices=WORKLOADS, default=["dosage"]
)
parser.add_argument(
"--bgen",
default="/Users/mwiewior/research/data/BGEN/chr22.first-25000.bgen",
)
parser.add_argument("--expected-rows", type=int, default=25000)
parser.add_argument("--expected-samples", type=int, default=2548)
parser.add_argument(
"--polars-bio-partitions",
nargs="+",
type=int,
default=[1],
help="target_partitions values to measure for polars-bio; other readers "
"are single-threaded and always run once per round",
)
parser.add_argument("--output", default="results/bgen_reader_benchmark.json")
args = parser.parse_args()
if args.runs < 1 or args.timeout < 1:
parser.error("--runs and --timeout must be positive")
if any(value < 1 for value in args.polars_bio_partitions):
parser.error("--polars-bio-partitions values must be positive")
path = Path(args.bgen).expanduser().resolve()
if not path.is_file():
parser.error(f"BGEN file does not exist: {path}")
phased = bgen_is_phased(path)
# pysnptools' BGEN reader asserts unphased input, so a phased file has no
# comparable pysnptools result rather than a slow one.
unsupported = []
readers = []
for reader in args.readers:
if reader == "pysnptools" and phased:
unsupported.append(
{
"reader": reader,
"reason": "pysnptools.distreader.Bgen requires unphased BGEN input",
}
)
continue
readers.append(reader)
if not readers:
parser.error("every requested reader is unsupported for this file")
combinations = []
for workload in args.workloads:
for reader in readers:
threads = args.polars_bio_partitions if reader == "polars-bio" else [1]
combinations.extend(
(workload, reader, count) for count in threads
)
raw = {f"{workload}:{reader}:t{count}": [] for workload, reader, count in combinations}
base_env = os.environ.copy()
base_env.update(
{
"BGEN_PATH": str(path),
"BGEN_EXPECTED_ROWS": str(args.expected_rows),
"BGEN_EXPECTED_SAMPLES": str(args.expected_samples),
"OMP_NUM_THREADS": "1",
"OPENBLAS_NUM_THREADS": "1",
"MKL_NUM_THREADS": "1",
"VECLIB_MAXIMUM_THREADS": "1",
"NUMEXPR_NUM_THREADS": "1",
"TQDM_DISABLE": "1",
}
)
for round_index in range(args.runs):
shift = round_index % len(combinations)
order = combinations[shift:] + combinations[:shift]
if round_index % 2:
order.reverse()
for order_index, (workload, reader, threads) in enumerate(order, start=1):
print(
f"\nRound {round_index + 1}/{args.runs}, "
f"{order_index}/{len(order)}: {reader} {workload} t={threads}"
)
env = base_env.copy()
env["BGEN_READER"] = reader
env["BGEN_MODE"] = workload
env["THREAD_NUM"] = str(threads)
env["POLARS_MAX_THREADS"] = str(threads)
env["RAYON_NUM_THREADS"] = str(threads)
result = run_one(args.python, env, args.timeout)
result["round"] = round_index + 1
result["order_in_round"] = order_index
raw[f"{workload}:{reader}:t{threads}"].append(result)
all_runs = [run for runs in raw.values() for run in runs]
equivalence = check_equivalence(all_runs)
verifications = []
for workload in args.workloads:
for reader in readers:
if reader == REFERENCE_READER:
continue
print(f"\nVerifying {reader} against {REFERENCE_READER} ({workload})")
verifications.append(
run_verification(
args.python, base_env, reader, REFERENCE_READER, workload, args.timeout
)
)
for check in verifications:
if check["left"] == "polars-bio" and check["bitwise_differences"]:
raise AssertionError(
f"polars-bio differs from {check['right']} in "
f"{check['bitwise_differences']} of {check['cells']} cells"
)
results: dict[str, dict] = {workload: {} for workload in args.workloads}
for workload, reader, threads in combinations:
key = reader if reader != "polars-bio" else f"polars-bio-t{threads}"
results[workload][key] = summarize(raw[f"{workload}:{reader}:t{threads}"])
comparisons = {}
for workload, readers in results.items():
snputils = readers.get("snputils")
if snputils is None:
continue
comparisons[workload] = {
key: {
"speedup_over_snputils": round(
snputils["time_seconds_median"] / summary["time_seconds_median"], 3
),
"peak_rss_ratio_vs_snputils": round(
summary["peak_rss_mb_median"] / snputils["peak_rss_mb_median"], 3
),
}
for key, summary in readers.items()
if key != "snputils"
}
payload = {
"metadata": {
"workloads": args.workloads,
"bgen_path": str(path),
"bgen_size_bytes": path.stat().st_size,
"bgen_sha256": file_sha256(path),
"rows": args.expected_rows,
"samples": args.expected_samples,
"phased": phased,
"polars_bio_partitions": args.polars_bio_partitions,
"python": platform.python_version(),
"platform": platform.platform(),
"machine": platform.machine(),
"logical_cpu_count": psutil.cpu_count(logical=True),
"physical_cpu_count": psutil.cpu_count(logical=False),
"memory_total_bytes": psutil.virtual_memory().total,
"versions": installed_versions(),
"polars_bio_ref": os.environ.get("POLARS_BIO_REF"),
"datafusion_bio_formats_ref": os.environ.get("DATAFUSION_BIO_FORMATS_REF"),
"polars_bio_build_profile": os.environ.get("POLARS_BIO_BUILD_PROFILE"),
"polars_bio_rustflags": os.environ.get("POLARS_BIO_RUSTFLAGS"),
"timing_scope": "source open, header/index discovery, block decompression, "
"probability decoding, and final C-contiguous float32 materialization; "
"imports and thread-pool configuration excluded",
"equivalence_note": "value and position hashes are taken after sorting rows "
"by variant position, so they compare content independently of the order in "
"which DataFusion coalesces partitions",
},
"unsupported": unsupported,
"equivalence": equivalence,
"verification": verifications,
"results": results,
"comparisons": comparisons,
}
output_path = Path(args.output)
output_path.parent.mkdir(parents=True, exist_ok=True)
output_path.write_text(json.dumps(payload, indent=2, sort_keys=True) + "\n")
print(f"\nWrote {output_path}")
for workload, readers in results.items():
print(f"\n{workload}:")
for reader, summary in sorted(
readers.items(), key=lambda item: item[1]["time_seconds_median"]
):
print(
f" {reader:<22} {summary['time_seconds_median']:>8.3f} s "
f"{summary['peak_rss_mb_median']:>9.1f} MB"
)
if __name__ == "__main__":
main()