File size: 8,418 Bytes
e5e2eeb a1e5ca8 e5e2eeb a1e5ca8 e5e2eeb |
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 |
import collections
import math
import re
from typing import Any, Dict, Sequence
import torch
import triton
from .diff_engine import DiffCase
def make_fwd_key(batch_size, seq_len, dim):
return f"forward : ({batch_size}, {seq_len}, {dim})"
def make_bwd_key(batch_size, seq_len, dim):
return f"backward : ({batch_size}, {seq_len}, {dim})"
def parse_config_string(config_str):
match = re.match(r"(\w+)\s*:\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)",
config_str)
if not match:
raise ValueError(f"Invalid config string: {config_str}")
_, bs, sl, d = match.groups()
return int(bs), int(sl), int(d)
def make_fwd_benchmark_for_case(
*,
case: DiffCase,
configs: Sequence[tuple[int, int, int]],
plot_name: str,
ylabel: str = "us",
line_vals=("naive", "cuda", "speedup"),
line_names: Dict[str, str] | None = None,
dtype=torch.bfloat16,
eps: float = 1e-6,
time_unit_scale: float = 1000,
):
timings_ms = collections.defaultdict(dict)
line_vals = list(line_vals)
line_names = line_names or {v: v.title() for v in line_vals}
x_vals = [list(_) for _ in configs]
@triton.testing.perf_report(
triton.testing.Benchmark(x_names=["dim", "batch_size", "seq_len"],
x_vals=x_vals,
line_arg="provider",
line_vals=line_vals,
line_names=[line_names[v] for v in line_vals],
ylabel=ylabel,
plot_name=plot_name,
args={}))
def bench(dim, batch_size, seq_len, provider):
key = make_fwd_key(dim, batch_size, seq_len)
I = case.build_inputs(batch_size, seq_len, dim, dtype, eps)
if provider == "speedup":
return timings_ms["naive"][key] / timings_ms["cuda"][key]
obj = case.make_naive(I) if provider == "naive" else case.make_cuda(I)
run = lambda: case.forward(obj, I)
ms = triton.testing.do_bench(run)
timings_ms[provider][key] = ms
return time_unit_scale * ms
return bench
def make_fwd_benchmark_plot_for_case(
*,
case: DiffCase,
configs: Sequence[tuple[int, int, int]],
plot_name: str,
ylabel: str = "Relative Speedup",
line_vals=("naive", "cuda"),
line_names: Dict[str, str] | None = None,
dtype=torch.bfloat16,
eps: float = 1e-6,
):
timings_ms = collections.defaultdict(dict)
spdup_ratio = list()
line_vals = list(line_vals)
line_names = line_names or {v: v.title() for v in line_vals}
x_vals = [make_fwd_key(*_) for _ in configs]
x_vals.append("Geometric Mean")
@triton.testing.perf_report(
triton.testing.Benchmark(x_names=["config"],
x_vals=x_vals,
line_arg="provider",
line_vals=line_vals,
line_names=[line_names[v] for v in line_vals],
ylabel=ylabel,
plot_name=plot_name,
args={}))
def bench(config, provider):
if config == "Geometric Mean":
if provider == "cuda":
return round(math.prod(spdup_ratio)**(1 / len(spdup_ratio)), 2)
else:
return 1.00
batch_size, seq_len, dim = parse_config_string(config)
I = case.build_inputs(batch_size, seq_len, dim, dtype, eps)
obj = case.make_naive(I) if provider == "naive" else case.make_cuda(I)
run = lambda: case.forward(obj, I)
ms = triton.testing.do_bench(run)
timings_ms[provider][config] = ms
if provider == "cuda":
ratio = timings_ms["naive"][config] / timings_ms["cuda"][config]
spdup_ratio.append(ratio)
return round(ratio, 2)
else:
return 1.00
return bench
def make_bwd_benchmark_for_case(
*,
case: DiffCase,
configs: Sequence[tuple[int, int, int]],
plot_name: str,
ylabel: str = "us",
line_vals=("naive", "cuda", "speedup"),
line_names: Dict[str, str] | None = None,
dtype=torch.bfloat16,
eps: float = 1e-6,
time_unit_scale: float = 1000,
):
timings_ms = collections.defaultdict(dict)
line_vals = list(line_vals)
line_names = line_names or {v: v.title() for v in line_vals}
x_vals = [list(_) for _ in configs]
@triton.testing.perf_report(
triton.testing.Benchmark(x_names=["dim", "batch_size", "seq_len"],
x_vals=x_vals,
line_arg="provider",
line_vals=line_vals,
line_names=[line_names[v] for v in line_vals],
ylabel=ylabel,
plot_name=plot_name,
args={}))
def bench(dim, batch_size, seq_len, provider):
key = make_bwd_key(dim, batch_size, seq_len)
I = case.build_inputs(batch_size, seq_len, dim, dtype, eps)
if provider == "speedup":
return timings_ms["naive"][key] / timings_ms["cuda"][key]
obj = case.make_naive(I) if provider == "naive" else case.make_cuda(I)
y = case.forward(obj, I)
gin = list(case.grad_inputs(I)) + list(obj.parameters())
if isinstance(y, torch.Tensor):
g = [torch.randn_like(y)]
else:
g = [torch.randn_like(r) for r in y]
run = lambda: torch.autograd.grad(y,
gin,
g,
retain_graph=True,
create_graph=False,
allow_unused=False)
ms = triton.testing.do_bench(run)
timings_ms[provider][key] = ms
return time_unit_scale * ms
return bench
def make_bwd_benchmark_plot_for_case(
*,
case: DiffCase,
configs: Sequence[tuple[int, int, int]],
plot_name: str,
ylabel: str = "Relative Speedup",
line_vals=("naive", "cuda"),
line_names: Dict[str, str] | None = None,
dtype=torch.bfloat16,
eps: float = 1e-6,
):
timings_ms = collections.defaultdict(dict)
spdup_ratio = list()
line_vals = list(line_vals)
line_names = line_names or {v: v.title() for v in line_vals}
x_vals = [make_bwd_key(*_) for _ in configs]
x_vals.append("Geometric Mean")
@triton.testing.perf_report(
triton.testing.Benchmark(x_names=["config"],
x_vals=x_vals,
line_arg="provider",
line_vals=line_vals,
line_names=[line_names[v] for v in line_vals],
ylabel=ylabel,
plot_name=plot_name,
args={}))
def bench(config, provider):
if config == "Geometric Mean":
if provider == "cuda":
return round(math.prod(spdup_ratio)**(1 / len(spdup_ratio)), 2)
else:
return 1.00
batch_size, seq_len, dim = parse_config_string(config)
I = case.build_inputs(batch_size, seq_len, dim, dtype, eps)
obj = case.make_naive(I) if provider == "naive" else case.make_cuda(I)
y = case.forward(obj, I)
gin = list(case.grad_inputs(I)) + list(obj.parameters())
if isinstance(y, torch.Tensor):
g = [torch.randn_like(y)]
else:
g = [torch.randn_like(r) for r in y]
run = lambda: torch.autograd.grad(y,
gin,
g,
retain_graph=True,
create_graph=False,
allow_unused=False)
ms = triton.testing.do_bench(run)
timings_ms[provider][config] = ms
if provider == "cuda":
ratio = timings_ms["naive"][config] / timings_ms["cuda"][config]
spdup_ratio.append(ratio)
return round(ratio, 2)
else:
return 1.00
return bench
|