forked from apache/mahout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_pytorch_ref.py
More file actions
391 lines (341 loc) · 13 KB
/
benchmark_pytorch_ref.py
File metadata and controls
391 lines (341 loc) · 13 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
#!/usr/bin/env python3
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Benchmark: QDP Rust+CUDA vs PyTorch reference implementation.
Compares encoding throughput in two modes:
* **encode-only** (default): Data is pre-generated on the target device
before the timer starts. Both PyTorch and Mahout time encoding kernels
only, giving the fairest kernel-vs-kernel comparison.
* **end-to-end**: Data generation, CPU→GPU transfer, and encoding are all
inside the timer. Matches the full pipeline cost users actually pay.
Usage:
# Fair kernel comparison (default):
python benchmark_pytorch_ref.py --qubits 16 --batches 100 --batch-size 64
# Full pipeline comparison:
python benchmark_pytorch_ref.py --mode end-to-end --qubits 16 --batches 100
# Specific frameworks:
python benchmark_pytorch_ref.py --encoding-method angle --frameworks pytorch-gpu,mahout
"""
from __future__ import annotations
import argparse
import statistics
import time
import torch
from utils import generate_batch_data
FRAMEWORK_CHOICES = ("pytorch-cpu", "pytorch-gpu", "mahout")
def _parse_frameworks(raw: str) -> list[str]:
"""Parse comma-separated framework list; ``'all'`` expands to all choices."""
if raw.strip().lower() == "all":
return list(FRAMEWORK_CHOICES)
names = [s.strip() for s in raw.split(",")]
for n in names:
if n not in FRAMEWORK_CHOICES:
raise argparse.ArgumentTypeError(
f"Unknown framework {n!r}. Choose from: {', '.join(FRAMEWORK_CHOICES)}"
)
return names
def _sample_dim(encoding_method: str, num_qubits: int) -> int:
if encoding_method == "basis":
return 1
if encoding_method == "angle":
return num_qubits
if encoding_method == "iqp":
return num_qubits + num_qubits * (num_qubits - 1) // 2
return 1 << num_qubits
def _generate_batches(
total: int,
batch_size: int,
sample_dim: int,
encoding_method: str,
device: str,
) -> list[torch.Tensor]:
"""Pre-generate batch data using the shared utility (deterministic)."""
batches = []
for b in range(total):
np_data = generate_batch_data(
batch_size,
sample_dim,
encoding_method,
seed=42 + b,
)
if encoding_method == "basis":
t = torch.tensor(np_data.flatten(), dtype=torch.float64, device=device)
else:
t = torch.tensor(np_data, dtype=torch.float64, device=device)
batches.append(t)
return batches
def run_pytorch(
*,
num_qubits: int,
total_batches: int,
batch_size: int,
encoding_method: str,
warmup_batches: int,
device: str,
) -> tuple[float, float]:
"""Run PyTorch reference encoding and return (duration_sec, vectors_per_sec)."""
from qumat_qdp.torch_ref import encode
dim = _sample_dim(encoding_method, num_qubits)
all_batches = _generate_batches(
warmup_batches + total_batches,
batch_size,
dim,
encoding_method,
device,
)
# Warmup.
for b in range(warmup_batches):
encode(all_batches[b], num_qubits, encoding_method, device=device)
if device.startswith("cuda"):
torch.cuda.synchronize()
# Timed run.
start = time.perf_counter()
for b in range(warmup_batches, len(all_batches)):
encode(all_batches[b], num_qubits, encoding_method, device=device)
if device.startswith("cuda"):
torch.cuda.synchronize()
duration = time.perf_counter() - start
total_vectors = total_batches * batch_size
vps = total_vectors / duration if duration > 0 else 0.0
return duration, vps
def run_mahout(
*,
num_qubits: int,
total_batches: int,
batch_size: int,
encoding_method: str,
warmup_batches: int,
device_id: int,
) -> tuple[float, float]:
"""Run QDP Rust+CUDA pipeline and return (duration_sec, vectors_per_sec)."""
from qumat_qdp.api import QdpBenchmark
result = (
QdpBenchmark(device_id=device_id)
.backend("rust")
.qubits(num_qubits)
.encoding(encoding_method)
.batches(total_batches, size=batch_size)
.warmup(warmup_batches)
.run_throughput()
)
return result.duration_sec, result.vectors_per_sec
def run_mahout_encode_only(
*,
num_qubits: int,
total_batches: int,
batch_size: int,
encoding_method: str,
warmup_batches: int,
device_id: int,
) -> tuple[float, float]:
"""Run Mahout encoding from pre-generated CUDA tensors (encode-only)."""
from _qdp import QdpEngine
engine = QdpEngine(device_id)
device = f"cuda:{device_id}"
dim = _sample_dim(encoding_method, num_qubits)
all_batches = _generate_batches(
warmup_batches + total_batches,
batch_size,
dim,
encoding_method,
device,
)
# Rust basis encoding expects int64 CUDA tensors.
if encoding_method == "basis":
all_batches = [b.to(torch.int64) for b in all_batches]
# Warmup.
for b in range(warmup_batches):
qt = engine.encode(all_batches[b], num_qubits, encoding_method)
_ = torch.utils.dlpack.from_dlpack(qt)
torch.cuda.synchronize()
# Timed run.
start = time.perf_counter()
for b in range(warmup_batches, len(all_batches)):
qt = engine.encode(all_batches[b], num_qubits, encoding_method)
_ = torch.utils.dlpack.from_dlpack(qt)
torch.cuda.synchronize()
duration = time.perf_counter() - start
total_vectors = total_batches * batch_size
return duration, total_vectors / duration if duration > 0 else 0.0
def run_pytorch_end_to_end(
*,
num_qubits: int,
total_batches: int,
batch_size: int,
encoding_method: str,
warmup_batches: int,
device: str,
) -> tuple[float, float]:
"""Run PyTorch encoding with data generation inside the timer (end-to-end)."""
from qumat_qdp.torch_ref import encode
dim = _sample_dim(encoding_method, num_qubits)
# Warmup (data gen outside timer is OK for warmup).
for b in range(warmup_batches):
np_data = generate_batch_data(batch_size, dim, encoding_method, seed=42 + b)
t = torch.tensor(
np_data.flatten() if encoding_method == "basis" else np_data,
dtype=torch.float64,
device=device,
)
encode(t, num_qubits, encoding_method, device=device)
if device.startswith("cuda"):
torch.cuda.synchronize()
# Timed run: data gen + transfer + encode.
start = time.perf_counter()
for b in range(total_batches):
np_data = generate_batch_data(
batch_size,
dim,
encoding_method,
seed=42 + warmup_batches + b,
)
t = torch.tensor(
np_data.flatten() if encoding_method == "basis" else np_data,
dtype=torch.float64,
device=device,
)
encode(t, num_qubits, encoding_method, device=device)
if device.startswith("cuda"):
torch.cuda.synchronize()
duration = time.perf_counter() - start
total_vectors = total_batches * batch_size
return duration, total_vectors / duration if duration > 0 else 0.0
def main() -> None:
parser = argparse.ArgumentParser(description="QDP vs PyTorch reference benchmark")
parser.add_argument(
"--mode",
default="encode-only",
choices=["encode-only", "end-to-end"],
help="'encode-only': data pre-generated on GPU, times encoding only. "
"'end-to-end': data generation + transfer + encoding all timed.",
)
parser.add_argument("--qubits", type=int, default=16)
parser.add_argument("--batches", type=int, default=100)
parser.add_argument("--batch-size", type=int, default=64)
parser.add_argument(
"--encoding-method",
default="amplitude",
choices=["amplitude", "angle", "basis", "iqp"],
)
parser.add_argument("--warmup", type=int, default=5)
parser.add_argument("--trials", type=int, default=3)
parser.add_argument("--device-id", type=int, default=0)
parser.add_argument(
"--frameworks",
type=_parse_frameworks,
default=list(FRAMEWORK_CHOICES),
)
args = parser.parse_args()
enc = args.encoding_method
mode = args.mode
print(f"\n{'=' * 60}")
print(f"QDP vs PyTorch Reference Benchmark ({mode})")
print(f"{'=' * 60}")
print(f" Mode: {mode}")
print(f" Qubits: {args.qubits}")
print(f" Batches: {args.batches}")
print(f" Batch size: {args.batch_size}")
print(f" Encoding: {enc}")
print(f" Warmup: {args.warmup}")
print(f" Trials: {args.trials}")
print(f" Total vecs: {args.batches * args.batch_size:,}")
print(f"{'=' * 60}")
if mode == "encode-only":
print(" Note: 'encode-only' times encoding kernels only;")
print(" data is pre-generated on the target device.")
else:
print(" Note: 'end-to-end' times data generation + transfer + encoding.")
print()
results: dict[str, float] = {} # framework -> median vps
for fw in args.frameworks:
trial_vps: list[float] = []
try:
for trial in range(args.trials):
if fw == "pytorch-cpu":
pytorch_fn = (
run_pytorch if mode == "encode-only" else run_pytorch_end_to_end
)
dur, vps = pytorch_fn(
num_qubits=args.qubits,
total_batches=args.batches,
batch_size=args.batch_size,
encoding_method=enc,
warmup_batches=args.warmup,
device="cpu",
)
elif fw == "pytorch-gpu":
if not torch.cuda.is_available():
print(f" {fw:20s} SKIPPED (no CUDA)")
break
pytorch_fn = (
run_pytorch if mode == "encode-only" else run_pytorch_end_to_end
)
dur, vps = pytorch_fn(
num_qubits=args.qubits,
total_batches=args.batches,
batch_size=args.batch_size,
encoding_method=enc,
warmup_batches=args.warmup,
device=f"cuda:{args.device_id}",
)
elif fw == "mahout":
if not torch.cuda.is_available():
print(f" {fw:20s} SKIPPED (no CUDA)")
break
if mode == "encode-only":
dur, vps = run_mahout_encode_only(
num_qubits=args.qubits,
total_batches=args.batches,
batch_size=args.batch_size,
encoding_method=enc,
warmup_batches=args.warmup,
device_id=args.device_id,
)
else:
dur, vps = run_mahout(
num_qubits=args.qubits,
total_batches=args.batches,
batch_size=args.batch_size,
encoding_method=enc,
warmup_batches=args.warmup,
device_id=args.device_id,
)
else:
continue
trial_vps.append(vps)
if trial_vps:
median = statistics.median(trial_vps)
results[fw] = median
print(
f" {fw:20s} {median:>12,.0f} vec/s (median of {len(trial_vps)} trials)"
)
except Exception as e:
print(f" {fw:20s} ERROR: {e}")
# Speedup ratios.
if len(results) > 1:
print(f"\n{'Speedup Ratios':^60}")
print(f"{'-' * 60}")
baselines = [k for k in ("pytorch-gpu", "pytorch-cpu") if k in results]
for base in baselines:
base_vps = results[base]
for fw in results:
if fw != base and fw not in baselines and base_vps > 0:
ratio = results[fw] / base_vps
print(f" {fw} vs {base}: {ratio:.1f}x")
print()
if __name__ == "__main__":
main()