|
| 1 | +import torch |
| 2 | +from torch.utils._pytree import tree_map_only |
| 3 | +from torch.utils.flop_counter import FlopCounterMode |
| 4 | + |
| 5 | + |
| 6 | +def _get_device_tflops(dtype): |
| 7 | + # for some reason the function from PyTorch is giving |
| 8 | + # wildly different TFlops compared to the specs. I'm |
| 9 | + # using had-coded values for now that I pulled from xFormers |
| 10 | + # https://github.com/fairinternal/xformers/blob/main/xformers/profiler/device_limits.py |
| 11 | + # TODO: fix PyTorch's implementation |
| 12 | + # from torch._inductor.utils import get_device_tflops |
| 13 | + |
| 14 | + device = None |
| 15 | + device_name = torch.cuda.get_device_name(device) |
| 16 | + assert "H100" in device_name, f"Only H100 supported from now, got {device_name}" |
| 17 | + |
| 18 | + return { |
| 19 | + torch.float64: 67, |
| 20 | + # NOTE: NVIDIA gives all numbers "with 2:4 sparsity" |
| 21 | + # but we want the full GEMM numbers |
| 22 | + torch.float32: 989 // 2, |
| 23 | + torch.float16: 1979 // 2, |
| 24 | + torch.bfloat16: 1979 // 2, |
| 25 | + torch.int8: 3958 // 2, |
| 26 | + }[dtype] |
| 27 | + |
| 28 | + |
| 29 | +def _get_sharded_shape(spec): |
| 30 | + mesh = spec.mesh |
| 31 | + tensor_shape = spec.tensor_meta.shape |
| 32 | + # TODO: take dtype into account as well |
| 33 | + # tensor_dtype = spec.tensor_meta.dtype |
| 34 | + placements = spec.placements |
| 35 | + # TODO: find a better heuristic other than |
| 36 | + # running DTensor |
| 37 | + new_tensor_shape = list(tensor_shape) |
| 38 | + for mesh_size, placement in zip(mesh.shape, placements): |
| 39 | + if placement.is_shard(): |
| 40 | + dim = placement.dim |
| 41 | + new_tensor_shape[dim] = ( |
| 42 | + new_tensor_shape[dim] + mesh_size - 1 |
| 43 | + ) // mesh_size |
| 44 | + return new_tensor_shape |
| 45 | + |
| 46 | + |
| 47 | +def estimate_strategy_runtime_cost(node, strategy): |
| 48 | + if node.op != "call_function": |
| 49 | + return 0 |
| 50 | + # suppose only matmul-like ops |
| 51 | + if not isinstance(node.target, torch._ops.OpOverload): |
| 52 | + return 0 |
| 53 | + |
| 54 | + if node.target.is_view: |
| 55 | + return 0 |
| 56 | + |
| 57 | + args = tree_map_only(torch.fx.Node, lambda x: x.meta["val"], node.args) |
| 58 | + kwargs = tree_map_only(torch.fx.Node, lambda x: x.meta["val"], node.kwargs) |
| 59 | + fake_mode = next(arg.fake_mode for arg in args if isinstance(arg, torch._subclasses.fake_tensor.FakeTensor)) |
| 60 | + assert len(kwargs) == 0 |
| 61 | + args_shapes = tuple(_get_sharded_shape(spec) for spec in strategy.input_specs) |
| 62 | + |
| 63 | + counter = 0 |
| 64 | + args = list(args) |
| 65 | + for i, arg in enumerate(args): |
| 66 | + if isinstance(arg, torch.Tensor): |
| 67 | + with fake_mode: |
| 68 | + args[i] = torch.empty(args_shapes[counter], device=arg.device, dtype=arg.dtype) |
| 69 | + counter += 1 |
| 70 | + |
| 71 | + # TODO: maybe cache the flop_counter to avoid recreating it |
| 72 | + # all the time |
| 73 | + with FlopCounterMode(display=False) as flop_counter: |
| 74 | + out = node.target(*args, **kwargs) |
| 75 | + |
| 76 | + flops = flop_counter.get_total_flops() |
| 77 | + |
| 78 | + # TODO: fix this |
| 79 | + dtype = strategy.input_specs[0].tensor_meta.dtype |
| 80 | + |
| 81 | + # TODO: use PyTorch's version once it's giving correct results |
| 82 | + gpu_flops = _get_device_tflops(dtype) * 10 ** 12 |
| 83 | + |
| 84 | + # suppose 50% efficiency for the operator |
| 85 | + factor = 1 / 0.5 |
| 86 | + compute_time = factor * flops / gpu_flops * 1e6 # us |
| 87 | + |
| 88 | + return compute_time |
0 commit comments