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
#!/usr/bin/env python3
"""Shared datasets, collate functions and consumer work for the pytest-benchmark guards.
The comparison with PyTorch (compare_torch.py) has its own workloads in real_world.py.
"""
# 1MB payload — pre-allocated once at import time.
# __getitem__ never allocates a large buffer; only the 32-byte digest is created.
= * 4096 # 1 048 576 bytes
# ---------------------------------------------------------------------------
# dataloader_rs datasets
# ---------------------------------------------------------------------------
"""Trivial dataset returning the index. Isolates loader/sampler overhead."""
=
return
return
"""InMemoryDs with ``__getitems__``: one Python call per batch, not per item."""
return
"""SHA-256 dataset: ≈ 0.5 ms per item, GIL-releasing C extension, minimal allocation.
Each call hashes 1MB of data via hashlib (C extension that releases the GIL)
and returns only a 32-byte digest — ideal for parallelism benchmarks.
"""
=
return
=
return # 32 bytes
# ---------------------------------------------------------------------------
# Collate functions
# ---------------------------------------------------------------------------
"""Collate a list of ints by summing them."""
return
"""Collate a list of byte strings by concatenating them."""
return b
# ---------------------------------------------------------------------------
# Consumer work
# ---------------------------------------------------------------------------
"""Stand-in for a training step: waits without holding the GIL, like a GPU step."""