forked from NVIDIA/cutile-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
165 lines (128 loc) · 4.8 KB
/
conftest.py
File metadata and controls
165 lines (128 loc) · 4.8 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
# SPDX-FileCopyrightText: Copyright (c) <2025> NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
import torch
import pytest
import cuda_timer
import subprocess
import sys
import math
def dtype_id(dtype):
match(dtype):
case torch.float8_e4m3fn: return "f8e4m3fn"
case torch.float8_e5m2: return "f8e5m2"
case torch.float16: return "f16"
case torch.bfloat16: return "bf16"
case torch.float32: return "f32"
case torch.float64: return "f64"
case torch.int32: return "i32"
case torch.int64: return "i64"
case torch.bool: return "bool"
case torch.complex32: return "c32"
case torch.complex64: return "c64"
case torch.complex128: return "c128"
case torch.uint32: return "u32"
case torch.uint64: return "u64"
case torch.int16: return "i16"
case torch.int8: return "i8"
def _size_suffix(_size):
suffix = 1024 ** 4
suffix_map = {
1024 ** 4: "T",
1024 ** 3: "G",
1024 ** 2: "M",
1024: "K",
1: "",
}
while suffix > 0:
if _size % suffix == 0:
return f"{_size // suffix}{suffix_map[suffix]}"
suffix //= 1024
def shape_id(shape):
shape_tokens = [_size_suffix(x) for x in shape]
return '-'.join(str(x) for x in shape_tokens)
def shape_size_id(shape):
overall_size = math.prod(shape)
shape_size_tokens = [_size_suffix(overall_size)]
shape_size_tokens.extend([
"x".join(_size_suffix(x) for x in shape)
])
return '-'.join(str(x) for x in shape_size_tokens)
# TODO: add float64.
float_dtypes = [torch.float16, torch.bfloat16, torch.float32]
int_dtypes = [torch.int32, torch.int64, torch.int16, torch.int8]
bool_dtypes = [torch.bool]
uint_dtypes = [torch.uint32, torch.uint64]
arithmetic_dtypes = int_dtypes + uint_dtypes + float_dtypes + bool_dtypes
@pytest.fixture(params=float_dtypes, ids=dtype_id)
def float_dtype(request):
return request.param
@pytest.fixture(params=int_dtypes, ids=dtype_id)
def int_dtype(request):
return request.param
@pytest.fixture(params=bool_dtypes, ids=dtype_id)
def bool_dtype(request):
return request.param
@pytest.fixture(params=uint_dtypes, ids=dtype_id)
def uint_dtype(request):
return request.param
# ----- For pytest benchmark
@pytest.fixture
def benchmark(benchmark):
# Patch benchmark fixture to use cuda timer
benchmark._timer = cuda_timer.time
return benchmark
@pytest.fixture(params=["cutile", "cutile_autotune", "torch"])
def backend(request):
"""A fixture to automatically find the corresponding cutile/torch implementation of
the benchmark target.
Examples:
If the request function is named "bench_matmul", we will look for `torch_matmul`
and `cutile_matmul` as different backend implementation to `matmul`.
"""
func_name = request.function.__name__
if not func_name.startswith("bench_"):
raise RuntimeError(f"Benchmark function must starts with \"bench_\", got {func_name}")
base_name = func_name[len("bench_"):]
backend_name = f'{request.param}_{base_name}'
if request.param == "cutile_autotune" and not hasattr(request.module, backend_name):
pytest.skip(f"Backend '{backend_name}' not implemented in {request.module.__name__}")
return getattr(request.module, backend_name)
def pytest_benchmark_update_machine_info(config, machine_info):
# TODO: PyTorch version, Driver version, and SM version
pass
def pytest_benchmark_update_json(config, benchmarks, output_json):
"""
Automatically add throughput (TF/s) and bandwidth (GB/s)
to the extra_info field in the pytest-benchmark JSON output,
if 'flops' and 'bytes_rw' are present.
"""
for bench in output_json["benchmarks"]:
extra = bench.get("extra_info", {})
mean_time = bench["stats"]["mean"]
# Bandwidth: bytes_rw / mean_time -> GB/s
if "bytes_rw" in extra and mean_time > 0:
gb_s = float(extra["bytes_rw"]) / mean_time / 1e9
extra["bandwidth_GBps"] = gb_s
else:
extra["bandwidth_GBps"] = None
# Throughput: flop_count / mean_time -> TF/s
if "flop_count" in extra and mean_time > 0:
tf_s = float(extra["flop_count"]) / mean_time / 1e12
extra["throughput_TFps"] = tf_s
else:
extra["throughput_TFps"] = None
bench["extra_info"] = extra
@pytest.fixture(scope="session")
def numba_cuda():
smoke_test = """
import numpy
from numba import cuda
cuda.to_device(numpy.ones(10))
"""
result = subprocess.run([sys.executable, "-c", smoke_test],
capture_output=True)
if result.returncode != 0:
pytest.xfail(f"Numba smoke test failed {result.returncode}. Skip.")
import numba
return numba.cuda