|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD 3-Clause license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | +import random |
| 7 | +from typing import Optional |
| 8 | + |
| 9 | +import fire |
| 10 | +import pandas as pd |
| 11 | +import torch |
| 12 | +from utils import do_benchmarks, get_name_to_moe_shapes_iter |
| 13 | + |
| 14 | +from torchao.testing.training.roofline_utils import get_specs |
| 15 | + |
| 16 | + |
| 17 | +@torch.inference_mode() |
| 18 | +def run( |
| 19 | + n_limit: Optional[int] = None, |
| 20 | + out_filename: Optional[str] = None, |
| 21 | + M: Optional[int] = None, |
| 22 | + K: Optional[int] = None, |
| 23 | + N: Optional[int] = None, |
| 24 | + E: Optional[int] = None, # dim 0 of B tensor (num experts) |
| 25 | + use_gpu_kernel_time: bool = True, |
| 26 | + shape_gen_name="llama4_17bx16e", |
| 27 | + recipe: str = "rowwise", |
| 28 | +): |
| 29 | + device = "cuda" |
| 30 | + |
| 31 | + assert recipe in ("rowwise",), "unsupported" |
| 32 | + |
| 33 | + specs = get_specs() |
| 34 | + bf16_peak_tops = specs["bf16_peak_tops"] |
| 35 | + fp8_peak_tops = specs["fp8_peak_tops"] |
| 36 | + print(f"gpu_name: {torch.cuda.get_device_name(0)}") |
| 37 | + print(f"peak tops: bf16 {bf16_peak_tops:.2e}, fp8 {fp8_peak_tops:.2e}") |
| 38 | + headers = ( |
| 39 | + "name", |
| 40 | + "recipe", |
| 41 | + "M", |
| 42 | + "K", |
| 43 | + "N", |
| 44 | + "E", |
| 45 | + "time_s", |
| 46 | + "speedup", |
| 47 | + "fp8_speedup", |
| 48 | + ) |
| 49 | + results = [] |
| 50 | + |
| 51 | + dtype = torch.bfloat16 |
| 52 | + name_to_shapes = get_name_to_moe_shapes_iter(shape_gen_name, M, K, N, E) |
| 53 | + |
| 54 | + for idx, (name, (M, K, N, E)) in enumerate( |
| 55 | + name_to_shapes, |
| 56 | + ): |
| 57 | + if n_limit is not None and idx >= n_limit: |
| 58 | + break |
| 59 | + assert M % E == 0, ( |
| 60 | + "tokens (M) must be evenly divisible by num experts (E) for this benchmark" |
| 61 | + ) |
| 62 | + tops = 2 * M * N * K * E |
| 63 | + print("M, K, N, E:", M, K, N, E, f"tops: {tops:.2E}") |
| 64 | + |
| 65 | + # Run bf16 torch._grouped_mm baseline. |
| 66 | + A = torch.randn(M, K, device=device, dtype=dtype) |
| 67 | + B = torch.randn(E, K, N, device=device, dtype=dtype) |
| 68 | + offs = generate_jagged_offs(E, M) |
| 69 | + print(f"offs: {offs}") |
| 70 | + ref_time_sec, ref_tops_sec, ref_pct_top_peak = do_benchmarks( |
| 71 | + tops, |
| 72 | + bf16_peak_tops, |
| 73 | + use_gpu_kernel_time, |
| 74 | + torch._grouped_mm, |
| 75 | + A, |
| 76 | + B, |
| 77 | + offs, |
| 78 | + ) |
| 79 | + print( |
| 80 | + f"{dtype} time_sec {ref_time_sec:.2E}, tops/sec {ref_tops_sec:.2E}, pct_peak {ref_pct_top_peak:.3f}" |
| 81 | + ) |
| 82 | + del A |
| 83 | + del B |
| 84 | + |
| 85 | + # Run scaled_grouped_mm. |
| 86 | + A_hp = torch.randn(M, K, device=device) |
| 87 | + B_hp_t = ( |
| 88 | + torch.randn(E, K, N, device=device) |
| 89 | + .transpose(-2, -1) |
| 90 | + .contiguous() |
| 91 | + .transpose(-2, -1) |
| 92 | + ) |
| 93 | + |
| 94 | + if recipe == "rowwise": |
| 95 | + # TODO: add e5m2 |
| 96 | + A = A_hp.to(torch.float8_e4m3fn) |
| 97 | + B = B_hp_t.to(torch.float8_e4m3fn) |
| 98 | + peak_tops = fp8_peak_tops |
| 99 | + scale_a = torch.ones(M, device=device) |
| 100 | + scale_b = torch.ones(E, N, device=device) |
| 101 | + else: |
| 102 | + assert False, f"unknown recipe {recipe}" |
| 103 | + |
| 104 | + def do_scaled_grouped_mm(A, B): |
| 105 | + nonlocal scale_a |
| 106 | + nonlocal scale_b |
| 107 | + nonlocal offs |
| 108 | + return torch._scaled_grouped_mm(A, B, scale_a, scale_b, offs=offs) |
| 109 | + |
| 110 | + if recipe == "rowwise": |
| 111 | + do_matmul = do_scaled_grouped_mm |
| 112 | + else: |
| 113 | + raise ValueError(f"unknown recipe {recipe}") |
| 114 | + |
| 115 | + time_sec, tops_sec, pct_top_peak = do_benchmarks( |
| 116 | + tops, peak_tops, use_gpu_kernel_time, do_matmul, A, B |
| 117 | + ) |
| 118 | + print( |
| 119 | + f"time_sec {time_sec:.2E}, tops/sec {tops_sec:.2E}, pct_peak {pct_top_peak:.3f}" |
| 120 | + ) |
| 121 | + |
| 122 | + del A, B |
| 123 | + if scale_a is not None: |
| 124 | + del scale_a |
| 125 | + if scale_b is not None: |
| 126 | + del scale_b |
| 127 | + |
| 128 | + results.append( |
| 129 | + [ |
| 130 | + name, |
| 131 | + recipe, |
| 132 | + M, |
| 133 | + K, |
| 134 | + N, |
| 135 | + E, |
| 136 | + ref_time_sec, |
| 137 | + time_sec, |
| 138 | + ref_time_sec / time_sec, |
| 139 | + ] |
| 140 | + ) |
| 141 | + |
| 142 | + data_df = pd.DataFrame(results, columns=headers) |
| 143 | + print(data_df) |
| 144 | + |
| 145 | + if out_filename is not None: |
| 146 | + data_df.to_csv(out_filename) |
| 147 | + |
| 148 | + |
| 149 | +def generate_jagged_offs(E, M, dtype=torch.int32, device="cuda"): |
| 150 | + """ |
| 151 | + Generates a tensor of length E, containing random values divisible by 16, |
| 152 | + from 0 to M, in sorted order, and where the final value in the tensor is always M. |
| 153 | + Args: |
| 154 | + E (int): The length of the tensor. |
| 155 | + M (int): The maximum value in the tensor. |
| 156 | + Returns: |
| 157 | + torch.Tensor: A tensor of length E with the specified properties. |
| 158 | + """ |
| 159 | + # Ensure M is divisible by 16 |
| 160 | + if M % 16 != 0: |
| 161 | + raise ValueError("M must be divisible by 16") |
| 162 | + |
| 163 | + # Generate a list of possible values |
| 164 | + possible_values = [i for i in range(0, M + 1, 16)] |
| 165 | + |
| 166 | + # If E is larger than the number of possible values, raise an error |
| 167 | + if E > len(possible_values): |
| 168 | + raise ValueError("E cannot be larger than the number of possible values") |
| 169 | + |
| 170 | + # Randomly select E - 1 values from the possible values (excluding M) |
| 171 | + selected_values = torch.tensor(random.sample(possible_values[:-1], E - 1)) |
| 172 | + |
| 173 | + # Append M to the selected values |
| 174 | + selected_values = torch.cat((selected_values, torch.tensor([M]))) |
| 175 | + |
| 176 | + # Sort the selected values |
| 177 | + selected_values, _ = torch.sort(selected_values) |
| 178 | + |
| 179 | + return selected_values.to(dtype).to(device) |
| 180 | + |
| 181 | + |
| 182 | +def main() -> None: |
| 183 | + fire.Fire(run) |
| 184 | + |
| 185 | + |
| 186 | +if __name__ == "__main__": |
| 187 | + main() # pragma: no cover |
0 commit comments