|
| 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 | +# this benchmarking script is a modified version of the original script from: https://github.com/drisspg/transformer_nuggets/blob/main/transformer_nuggets/utils/benchmark.py |
| 7 | + |
| 8 | +from dataclasses import dataclass |
| 9 | +from typing import List |
| 10 | + |
| 11 | +import torch |
| 12 | +from tabulate import tabulate |
| 13 | +from tqdm import tqdm |
| 14 | + |
| 15 | +from benchmarks.utils import benchmark_cuda_function_in_microseconds |
| 16 | +from torchao.prototype.mx_formats.kernels import triton_mxfp8_dequant_dim0 |
| 17 | +from torchao.prototype.mx_formats.mx_tensor import to_dtype, to_mx |
| 18 | + |
| 19 | +device = torch.device("cuda") |
| 20 | + |
| 21 | +# Needed since changing args to function causes recompiles |
| 22 | +torch._dynamo.config.cache_size_limit = 1000 |
| 23 | + |
| 24 | + |
| 25 | +@dataclass(frozen=True) |
| 26 | +class ExperimentConfig: |
| 27 | + input_shape: tuple[int] |
| 28 | + |
| 29 | + |
| 30 | +@dataclass(frozen=True) |
| 31 | +class ExperimentResult: |
| 32 | + # time |
| 33 | + torch_us: float |
| 34 | + triton_us: float |
| 35 | + torch_gbps: float |
| 36 | + triton_gbps: float |
| 37 | + |
| 38 | + |
| 39 | +@dataclass(frozen=True) |
| 40 | +class Experiment: |
| 41 | + config: ExperimentConfig |
| 42 | + result: ExperimentResult |
| 43 | + |
| 44 | + |
| 45 | +def get_configs() -> List[ExperimentConfig]: |
| 46 | + input_shapes = [ |
| 47 | + # (local_batch_size, seq_len, dim) |
| 48 | + (1, 8192, 7168), |
| 49 | + (2, 8192, 7168), |
| 50 | + (4, 8192, 7168), |
| 51 | + (8, 8192, 7168), |
| 52 | + ] |
| 53 | + configs = [] |
| 54 | + for shape in input_shapes: |
| 55 | + configs.append( |
| 56 | + ExperimentConfig( |
| 57 | + input_shape=shape, |
| 58 | + ) |
| 59 | + ) |
| 60 | + return configs |
| 61 | + |
| 62 | + |
| 63 | +def run_experiment(config: ExperimentConfig) -> ExperimentResult: |
| 64 | + block_size = 32 |
| 65 | + input_shape = config.input_shape |
| 66 | + input_tensor = torch.randn( |
| 67 | + *input_shape, |
| 68 | + dtype=torch.bfloat16, |
| 69 | + device=device, |
| 70 | + ) |
| 71 | + |
| 72 | + e8m0_scales, e4m3_data = to_mx(input_tensor, torch.float8_e4m3fn, block_size) |
| 73 | + |
| 74 | + # Bench torch dequant |
| 75 | + to_dtype_c = torch.compile(to_dtype) |
| 76 | + elem_dtype, target_dtype = torch.float8_e4m3fn, torch.bfloat16 |
| 77 | + torch_output = to_dtype_c( |
| 78 | + e4m3_data, |
| 79 | + e8m0_scales, |
| 80 | + elem_dtype, |
| 81 | + block_size, |
| 82 | + target_dtype, |
| 83 | + ) |
| 84 | + torch_us = benchmark_cuda_function_in_microseconds( |
| 85 | + to_dtype_c, |
| 86 | + e4m3_data, |
| 87 | + e8m0_scales, |
| 88 | + elem_dtype, |
| 89 | + block_size, |
| 90 | + target_dtype, |
| 91 | + ) |
| 92 | + |
| 93 | + # Bench triton kernel |
| 94 | + _ = triton_mxfp8_dequant_dim0( |
| 95 | + e4m3_data, |
| 96 | + e8m0_scales, |
| 97 | + target_dtype, |
| 98 | + block_size, |
| 99 | + ) |
| 100 | + triton_us = benchmark_cuda_function_in_microseconds( |
| 101 | + triton_mxfp8_dequant_dim0, |
| 102 | + e4m3_data, |
| 103 | + e8m0_scales, |
| 104 | + target_dtype, |
| 105 | + block_size, |
| 106 | + ) |
| 107 | + |
| 108 | + # mem bw calculations |
| 109 | + bytes_per_input_el = torch.finfo(elem_dtype).bits / 8 |
| 110 | + bytes_per_output_el = torch.finfo(target_dtype).bits / 8 |
| 111 | + bytes_per_scale_el = torch.finfo(torch.float8_e8m0fnu).bits / 8 |
| 112 | + |
| 113 | + read_bytes = ( |
| 114 | + e4m3_data.numel() * bytes_per_input_el |
| 115 | + + e8m0_scales.numel() * bytes_per_scale_el |
| 116 | + ) |
| 117 | + write_bytes = torch_output.numel() * bytes_per_output_el |
| 118 | + |
| 119 | + torch_gbps = ((read_bytes + write_bytes) / 1e9) / (torch_us / 1e6) |
| 120 | + triton_gbps = ((read_bytes + write_bytes) / 1e9) / (triton_us / 1e6) |
| 121 | + |
| 122 | + return ExperimentResult( |
| 123 | + torch_us=torch_us, |
| 124 | + triton_us=triton_us, |
| 125 | + triton_gbps=triton_gbps, |
| 126 | + torch_gbps=torch_gbps, |
| 127 | + ) |
| 128 | + |
| 129 | + |
| 130 | +def print_results(experiments: List[Experiment]): |
| 131 | + headers = [ |
| 132 | + "input_shape", |
| 133 | + "torch_us", |
| 134 | + "triton_us", |
| 135 | + "torch_gbps", |
| 136 | + "triton_gbps", |
| 137 | + "triton_speedup", |
| 138 | + ] |
| 139 | + rows = [] |
| 140 | + for experiment in experiments: |
| 141 | + triton_speedup = round( |
| 142 | + experiment.result.torch_us / experiment.result.triton_us, 3 |
| 143 | + ) |
| 144 | + rows.append( |
| 145 | + [ |
| 146 | + str(experiment.config.input_shape), |
| 147 | + experiment.result.torch_us, |
| 148 | + experiment.result.triton_us, |
| 149 | + round(experiment.result.torch_gbps, 3), |
| 150 | + round(experiment.result.triton_gbps, 3), |
| 151 | + f"{triton_speedup}x", |
| 152 | + ] |
| 153 | + ) |
| 154 | + print(tabulate(rows, headers=headers)) |
| 155 | + |
| 156 | + |
| 157 | +def main(): |
| 158 | + torch.random.manual_seed(123) |
| 159 | + configs = get_configs() |
| 160 | + results = [] |
| 161 | + for config in tqdm(configs): |
| 162 | + result = run_experiment(config) |
| 163 | + results.append(Experiment(config=config, result=result)) |
| 164 | + |
| 165 | + # Use Tabulate to print results |
| 166 | + print_results(results) |
| 167 | + |
| 168 | + |
| 169 | +if __name__ == "__main__": |
| 170 | + main() |
0 commit comments