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
|
import functools
from typing import Any, Callable, Dict, List, Tuple
import torch
Feedback = float
Choice = str
Value = Any
CHOICE_COL = "choice"
FEEDBACK_COL = "feedback"
class AHFeature:
"""
The context, that AutoHeuristic stores, is a list of features. AutoHeuristic needs to know whether a feature is
categorical (i.e., not a continuous variable) to learn a machine learning model.
"""
def __init__(self, name: str, value: Value, is_categorical: bool = False) -> None:
self.name = name
self.value = value
self.is_categorical = is_categorical
class AHOperation:
"""
AHOperation can be used to augment the data collected by AutoHeuristic.
One might for example store features like m, k, n, but also want to use
features like m*n, or k*n, to learn a heuristic. Instead of storing features
that can be created from the collected data, one can use AHOperation to
create new features from the collected data.
"""
def __init__(
self, name: str, func: Callable[[Any], Value], is_categorical: bool = False
) -> None:
self.name = name
self.func = func
self.is_categorical = is_categorical
def apply_operation(self, data: Any) -> None:
data[self.name] = self.func(data)
class AHContext:
"""
This class is used to specify which information AutoHeuristic should store. For each choice, AutoHeursitic will
store the context and the collected feedback. The context could be something like the shape of a tensor, i.e.,
information that will help to learn a heuristic.
"""
features: List[AHFeature]
context_dict: Dict[str, Value]
def __init__(self) -> None:
self.features = []
self.context_dict = {}
def add_feature(
self, name: str, value: Value, is_categorical: bool = False
) -> None:
self.features.append(AHFeature(name, value, is_categorical=is_categorical))
self.context_dict[name] = value
def get_numerical_and_categorical_features(self) -> Tuple[List[str], List[str]]:
numerical_features = []
categorical_features = []
for feature in self.features:
if feature.is_categorical:
categorical_features.append(feature.name)
else:
numerical_features.append(feature.name)
return numerical_features, categorical_features
def get_feature_names_csv(self) -> str:
return ",".join(feature.name for feature in self.features)
def get_feature_values_csv(self) -> str:
return ",".join(str(feature.value) for feature in self.features)
def get_value(self, name: str) -> Value:
return self.context_dict[name]
def apply_operations(self, operations: List[AHOperation]) -> None:
for op in operations:
op.apply_operation(self.context_dict)
class AHMetadata:
def __init__(
self,
shared_memory: Any,
device_capa: Tuple[int, int],
choices: List[Choice],
name: str,
) -> None:
# use amount of shared_memory and device_capability to identify GPU
# TODO(AlnisM): there might be a better way to do this
self.shared_memory = shared_memory
self.device_capa = device_capa
self.choices = choices
self.name = name
def to_dict(self) -> Dict[str, Value]:
return {
"shared_memory": self.shared_memory,
"device_capa": self.device_capa,
"name": self.name,
}
def get_metadata_str_from_log(log_path: str) -> str:
with open(log_path, newline="") as file:
json_string = file.readline().strip()
return json_string
def check_minsize(context: AHContext, minsize: int) -> bool:
return (
context.get_value("m") >= minsize
and context.get_value("k") >= minsize
and context.get_value("n") >= minsize
)
def pad_mm_precondition(metadata: AHMetadata, context: AHContext) -> bool:
if metadata.shared_memory == 166912 and metadata.device_capa == (8, 0):
# A100 precondition
return check_minsize(context, 512)
elif metadata.shared_memory == 232448 and metadata.device_capa == (9, 0):
# H100 precondition
return check_minsize(context, 768)
return True
def get_mixedmm_precondition(metadata: AHMetadata, context: AHContext) -> bool:
m = context.get_value("m")
k = context.get_value("k")
n = context.get_value("n")
if m > 128 or k < 1024 or n < 1024:
return False
mat1_iscontig = context.get_value("mat1_iscontig")
mat2_iscontig = context.get_value("mat2_iscontig")
return mat1_iscontig and not mat2_iscontig
def get_mult_dims_ops() -> List[AHOperation]:
m_times_k_op = AHOperation("m*k", lambda data: data["m"] * data["k"])
m_times_n_op = AHOperation("m*n", lambda data: data["m"] * data["n"])
k_times_n_op = AHOperation("k*n", lambda data: data["k"] * data["n"])
return [m_times_k_op, m_times_n_op, k_times_n_op]
def get_arith_intensity(data: Any) -> float:
m = data["m"]
k = data["k"]
n = data["n"]
if m == 0 or k == 0 or n == 0:
return 0.0
return m * k * n / (m * k + k * n + m * n)
def pad_mm_operations() -> List[AHOperation]:
mult_dims_ops = get_mult_dims_ops()
k_div_m_times_n_op = AHOperation(
"k/(m*n)", lambda data: data["k"] / (data["m"] * data["n"])
)
def bfloat_perf_hit(data: Any) -> bool:
m = data["m"]
k = data["k"]
n = data["n"]
is_bfloat = str(data["mat1_dtype"]) == "torch.bfloat16"
return k > (m * 1024) and k > (n * 1024) and is_bfloat
bfloat_perf_hit_op = AHOperation(
"bfloat_perf_hit", bfloat_perf_hit, is_categorical=True
)
arith_intensity_op = AHOperation("arith_intensity", get_arith_intensity)
dims_need_padding_ops = get_dims_need_padding_ops()
dims_multiple_ops = get_dims_multiple_ops()
is_contig_ops = get_is_contig_ops()
ah_operations = mult_dims_ops + [
k_div_m_times_n_op,
bfloat_perf_hit_op,
arith_intensity_op,
]
ah_operations.extend(dims_need_padding_ops)
ah_operations.extend(dims_multiple_ops)
ah_operations.extend(is_contig_ops)
return ah_operations
def between_op(data: Any, dim: str, lower: int, upper: int) -> bool:
return data[dim] >= lower and data[dim] <= upper
def between_ops() -> List[AHOperation]:
dims = ["m", "k", "n"]
limits = [(1, 16), (17, 32), (33, 64), (65, 128), (129, 256)]
ah_operations = []
for dim in dims:
for lower, upper in limits:
between_op_fn = functools.partial(
between_op, dim=dim, lower=lower, upper=upper
)
# using 'LEQ' instead of '<=' because '<=' cannot be exported to dot
between_op_name = f"{lower}LEQ{dim}LEQ{upper}"
ah_operations.append(
AHOperation(between_op_name, between_op_fn, is_categorical=True)
)
return ah_operations
def pow2_op(data: Any, dim: str, exponent: int) -> bool:
return data[dim] == 2**exponent
def mm_operations() -> List[AHOperation]:
mult_dims_ops = get_mult_dims_ops()
arith_intensity_op = AHOperation("arith_intensity", get_arith_intensity)
return mult_dims_ops + [arith_intensity_op]
def mixed_mm_operations() -> List[AHOperation]:
return mm_operations() + between_ops()
def is_multiple(data: Any, dim: str, mult: int) -> bool:
return data[dim] % mult == 0
def get_dims_multiple_ops() -> List[AHOperation]:
multiples = [2, 4, 8, 16, 32]
dims = ["m", "k", "n"]
dims_multiple_ops = []
for dim in dims:
for mult in multiples:
is_multiple_fn = functools.partial(is_multiple, dim=dim, mult=mult)
dims_multiple_op = AHOperation(
f"{dim}_multiple_{mult}", is_multiple_fn, is_categorical=True
)
dims_multiple_ops.append(dims_multiple_op)
return dims_multiple_ops
def get_dims_need_padding_ops() -> List[AHOperation]:
def mat1_innermost_needs_padding_fn(data: Any) -> bool:
mat1_stride_0 = data["mat1_stride_0"]
mat1_stride_1 = data["mat1_stride_1"]
m_padded_length = data["m_padded_length"]
k_padded_length = data["k_padded_length"]
mat1_innermost_needs_padding = False
if mat1_stride_0 == 1 and m_padded_length != 0:
mat1_innermost_needs_padding = True
if mat1_stride_1 == 1 and k_padded_length != 0:
mat1_innermost_needs_padding = True
return mat1_innermost_needs_padding
mat1_innermost_op = AHOperation(
"mat1_innermost_needs_padding",
mat1_innermost_needs_padding_fn,
is_categorical=True,
)
def mat2_innermost_needs_padding_fn(data: Any) -> bool:
mat2_stride_0 = data["mat2_stride_0"]
mat2_stride_1 = data["mat2_stride_1"]
k_padded_length = data["k_padded_length"]
n_padded_length = data["n_padded_length"]
mat2_innermost_needs_padding = False
if mat2_stride_0 == 1 and k_padded_length != 0:
mat2_innermost_needs_padding = True
if mat2_stride_1 == 1 and n_padded_length != 0:
mat2_innermost_needs_padding = True
return mat2_innermost_needs_padding
mat2_innermost_op = AHOperation(
"mat2_innermost_needs_padding",
mat2_innermost_needs_padding_fn,
is_categorical=True,
)
def num_dims_needs_padding_fn(data: Any) -> int:
m_padded_length = data["m_padded_length"]
k_padded_length = data["k_padded_length"]
n_padded_length = data["n_padded_length"]
num_dims_needs_padding = 0
if m_padded_length != 0:
num_dims_needs_padding += 1
if k_padded_length != 0:
num_dims_needs_padding += 1
if n_padded_length != 0:
num_dims_needs_padding += 1
return num_dims_needs_padding
num_dims_op = AHOperation("num_dims_needs_padding", num_dims_needs_padding_fn)
return [mat1_innermost_op, mat2_innermost_op, num_dims_op]
def get_is_contig_ops() -> List[AHOperation]:
def mat1_is_contig_fn(data: Any) -> bool:
stride_0 = data["mat1_stride_0"]
stride_1 = data["mat1_stride_1"]
k = data["k"]
return stride_0 == k and stride_1 == 1
mat1_is_contig_op = AHOperation(
"mat1_iscontig", mat1_is_contig_fn, is_categorical=True
)
def mat2_is_contig_fn(data: Any) -> bool:
stride_0 = data["mat2_stride_0"]
stride_1 = data["mat2_stride_1"]
n = data["n"]
return stride_0 == n and stride_1 == 1
mat2_is_contig_op = AHOperation(
"mat2_iscontig", mat2_is_contig_fn, is_categorical=True
)
return [mat1_is_contig_op, mat2_is_contig_op]
def context_add_strides(context: AHContext, name: str, stride: Tuple[int, ...]) -> None:
for i, s in enumerate(stride):
context.add_feature(f"{name}_stride_{i}", s)
def context_add_using_tf32(context: AHContext, dtype: torch.dtype) -> None:
using_tf32 = "not_float_32"
if dtype == torch.float32:
using_tf32 = torch.backends.cuda.matmul.allow_tf32
context.add_feature("using_tf32", using_tf32, is_categorical=True)
|