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
|
from . import benchmark
import itertools
import numpy as np
import torch
class BroadcastMulBench(benchmark.Benchmark):
def __init__(self, mode, device, dtype, case, M, N, K):
super().__init__(mode, device, dtype)
self.case = case
self.M = M
self.N = N
self.K = K
if case == "row":
self.d1 = self.rand(
[M, N, 1], device=device, dtype=dtype, requires_grad=self.requires_grad
)
self.d2 = self.rand(
[M, 1, K], device=device, dtype=dtype, requires_grad=self.requires_grad
)
elif case == "mid":
self.d1 = self.rand(
[M, N, 1], device=device, dtype=dtype, requires_grad=self.requires_grad
)
self.d2 = self.rand(
[1, N, K], device=device, dtype=dtype, requires_grad=self.requires_grad
)
elif case == "col":
self.d1 = self.rand(
[M, 1, K], device=device, dtype=dtype, requires_grad=self.requires_grad
)
self.d2 = self.rand(
[1, N, K], device=device, dtype=dtype, requires_grad=self.requires_grad
)
else:
raise ValueError("invalid case: %s" % (case))
self.inputs = [self.d1, self.d2]
def forward(self, d1, d2):
y = d1 + d2
return y
def reference(self):
return self.numpy(self.d1) + self.numpy(self.d2)
def config(self):
return [self.M, self.N, self.K]
@staticmethod
def default_configs():
return [[128, 256, 128]]
def memory_workload(self):
if self.mode == "fwd":
sol_count = 1
algorithmic_count = 1
else:
sol_count = (1) + (1)
algorithmic_count = 1 + (1 + 1)
buffer_size = self.M * self.N * self.K
return {
"sol": buffer_size * sol_count,
"algorithmic": buffer_size * algorithmic_count,
}
class BroadcastRowBench(BroadcastMulBench):
def __init__(self, mode, device, dtype, M, N, K):
super(BroadcastRowBench, self).__init__(mode, device, dtype, "row", M, N, K)
@staticmethod
def module():
return "broadcast_row"
class BroadcastMidBench(BroadcastMulBench):
def __init__(self, mode, device, dtype, M, N, K):
super(BroadcastMidBench, self).__init__(mode, device, dtype, "mid", M, N, K)
@staticmethod
def module():
return "broadcast_mid"
class BroadcastColBench(BroadcastMulBench):
def __init__(self, mode, device, dtype, M, N, K):
super(BroadcastColBench, self).__init__(mode, device, dtype, "col", M, N, K)
@staticmethod
def module():
return "broadcast_col"
class BroadcastThreeArgs(benchmark.Benchmark):
def __init__(self, mode, device, dtype, M, N, K, L):
super().__init__(mode, device, dtype)
self.M = M
self.N = N
self.K = K
self.L = L
self.d1 = self.rand([M, N], device=device, dtype=dtype, requires_grad=self.requires_grad)
self.d2 = self.rand([K, M, 1], device=device, dtype=dtype, requires_grad=self.requires_grad)
self.d3 = self.rand(
[L, K, 1, 1], device=device, dtype=dtype, requires_grad=self.requires_grad
)
self.inputs = [self.d1, self.d2, self.d3]
def forward(self, d1, d2, d3):
y = d1 + d2 + d3
return y
def reference(self):
return self.numpy(self.d1) + self.numpy(self.d2) + self.numpy(self.d3)
def config(self):
return [self.M, self.N, self.K, self.L]
@staticmethod
def default_configs():
return [[32, 16, 64, 128]]
def memory_workload(self):
if self.mode == "fwd":
sol_count = 1
algorithmic_count = 1
else:
sol_count = (1) + (1)
algorithmic_count = 1 + (1 + 1 + 1)
buffer_size = self.M * self.N * self.K * self.L * 4
return {
"sol": buffer_size * sol_count,
"algorithmic": buffer_size * algorithmic_count,
}
@staticmethod
def module():
return "broadcast_3args"
# benchmark.register_benchmark_class(BroadcastRowBench)
# benchmark.register_benchmark_class(BroadcastMidBench)
# benchmark.register_benchmark_class(BroadcastColBench)
# benchmark.register_benchmark_class(BroadcastThreeArgs)
# TODO: merge this with elementwise bench
# A template class for elementwise operations.
# A derived class will override the class instance to customize its behavior.
class BroadcastBench(benchmark.Benchmark):
# List of customization class variables.
op_str = None
binary_op_pt_func = None
binary_op_np_func = None
unary_op_pt_func = None
unary_op_np_func = None
split_input = True
def __init__(self, mode, device, dtype, M, N, K):
super().__init__(mode, device, dtype)
self.M = M
self.N = N
self.K = K
self.d1 = self.rand([M, N], device=device, dtype=dtype, requires_grad=self.requires_grad)
self.d2 = self.rand([K, 1, N], device=device, dtype=dtype, requires_grad=self.requires_grad)
self.d3 = self.rand([M, N], device=device, dtype=dtype, requires_grad=self.requires_grad)
self.d4 = self.rand([K, M, 1], device=device, dtype=dtype, requires_grad=self.requires_grad)
self.inputs = [self.d1, self.d2, self.d3, self.d4]
def _eval(self, d1, d2, d3, d4, binary_op, unary_op):
if not binary_op:
def binary_op(x, y):
return x + y
if not unary_op:
def unary_op(x):
return x
if self.split_input:
d1 = unary_op(d1)
d2 = unary_op(d2)
d3 = unary_op(d3)
d4 = unary_op(d4)
else:
d1, d2, d3, d4 = (
unary_op(d1),
unary_op(d2),
unary_op(d1 + 0.001),
unary_op(d4),
)
a = binary_op(d1, d2)
b = binary_op(d3, d4)
c = a + b
return c
def forward(self, d1, d2, d3, d4):
binary_op = self.__class__.binary_op_pt_func
unary_op = self.__class__.unary_op_pt_func
return self._eval(d1, d2, d3, d4, binary_op, unary_op)
def reference(self):
binary_op = self.__class__.binary_op_np_func
unary_op = self.__class__.unary_op_np_func
[d1, d2, d3, d4] = [self.numpy(d) for d in [self.d1, self.d2, self.d3, self.d4]]
return self._eval(d1, d2, d3, d4, binary_op, unary_op)
def config(self):
return [self.M, self.N, self.K]
@classmethod
def module(cls):
return "broadcast_" + cls.op_str
def memory_workload(self):
input_count = len(self.inputs)
if self.mode == "fwd":
if self.split_input:
sol_count = 1
algorithmic_count = 1
else:
sol_count = 1
algorithmic_count = 1
else:
if self.split_input:
sol_count = 1
algorithmic_count = input_count
else:
sol_count = 1
algorithmic_count = input_count
buffer_size = self.M * self.N * self.K * 4
return {
"sol": buffer_size * sol_count,
"algorithmic": buffer_size * algorithmic_count,
}
@staticmethod
def default_configs():
return [[1 << 8, 1 << 7, 1 << 9]]
def register_broadcast_ops():
binary_op_list = [
["mul", lambda a, b: a * b],
["add", lambda a, b: a + b],
["sub", lambda a, b: a - b],
["div", lambda a, b: a / (b + 1e-4)],
[
"pow",
lambda a, b: torch.pow(a, b),
lambda a, b: np.power(a, b),
], # no fuson triggered
["max", lambda a, b: torch.max(a, b), lambda a, b: np.maximum(a, b)],
["min", lambda a, b: torch.min(a, b), lambda a, b: np.minimum(a, b)],
]
unary_op_list = [
["erf", lambda x: torch.erf(x), lambda x: np.erf(x)],
["exp", lambda x: torch.exp(x), lambda x: np.exp(x)],
["sin", lambda x: torch.sin(x), lambda x: np.sin(x)],
["cos", lambda x: torch.cos(x), lambda x: np.cos(x)],
]
for split_input, binary_op in itertools.product([True, False], binary_op_list):
# Make a copy of BroadcastBench
if len(binary_op) == 2:
[op_str, op_pt_func] = binary_op
op_np_func = op_pt_func
elif len(binary_op) == 3:
[op_str, op_pt_func, op_np_func] = binary_op
split_str = "split" if split_input else "shared"
op_str = split_str + "_" + op_str
bm_cls = type("BroadcastBench_" + op_str, (BroadcastBench,), {})
bm_cls.op_str = op_str
bm_cls.binary_op_pt_func = op_pt_func
bm_cls.binary_op_np_func = op_np_func
bm_cls.split_input = split_input
benchmark.register_benchmark_class(bm_cls)
for split_input, unary_op in itertools.product([True, False], unary_op_list):
# Make a copy of BroadcastBench
if len(unary_op) == 2:
[op_str, op_pt_func] = unary_op
op_np_func = op_pt_func
elif len(unary_op) == 3:
[op_str, op_pt_func, op_np_func] = unary_op
split_str = "split" if split_input else "shared"
op_str = split_str + "_" + op_str
bm_cls = type("BroadcastBench_" + op_str, (BroadcastBench,), {})
bm_cls.op_str = op_str
bm_cls.unary_op_pt_func = op_pt_func
bm_cls.unary_op_np_func = op_np_func
bm_cls.split_input = split_input
benchmark.register_benchmark_class(bm_cls)
register_broadcast_ops()
|