Skip to content

Commit 090b1ed

Browse files
jananisriramfacebook-github-bot
authored andcommitted
Add amax as default per-tensor scaling factor for fp8_gemm benchmark (#339)
Summary: Pull Request resolved: #339 Add `amax` (absolute maximum) as the default scaling factor for per-tensor scaling with fp8 workloads, as is used in practice. Also add a command-line argument that allows the user to define a scaling factor for per-tensor scaling. Reviewed By: NikhilAPatel Differential Revision: D80577628
1 parent 91e3662 commit 090b1ed

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

tritonbench/operators/fp8_gemm/fp8_gemm.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ def parse_args(args):
4141
parser.add_argument("--m", type=int)
4242
parser.add_argument("--k", type=int)
4343
parser.add_argument("--n", type=int)
44+
parser.add_argument("--per-tensor-scale-a", type=float, default=None)
45+
parser.add_argument("--per-tensor-scale-b", type=float, default=None)
4446
return parser.parse_args(args)
4547

4648

@@ -54,7 +56,20 @@ def __init__(
5456
super().__init__(tb_args, extra_args)
5557
self.extra_args = parse_args(extra_args)
5658

59+
def _get_dtype(self):
60+
if self.extra_args.scaling_rowwise:
61+
return torch.bfloat16
62+
else:
63+
return torch.float16
64+
5765
def get_input_iter(self):
66+
def _get_scale_per_tensor(x: torch.Tensor, custom_scale: float = None) -> torch.Tensor:
67+
# For tensor-wise scaling, kernel requires a float32 scale tensor
68+
if custom_scale:
69+
return torch.tensor(custom_scale, dtype=torch.float32, device=x.device)
70+
scale = torch.finfo(torch.float8_e4m3fn).max / x.abs().max()
71+
return scale.to(torch.float32)
72+
5873
def args(m, n, k):
5974
a = torch.randn(m, k, device=self.device).to(torch.float16)
6075
b = torch.randn(k, n, device=self.device).to(torch.float16).T.contiguous().T
@@ -64,8 +79,8 @@ def args(m, n, k):
6479
scale_a = torch.ones((M, 1), dtype=torch.float32, device=a.device)
6580
scale_b = torch.ones((1, N), dtype=torch.float32, device=b.device)
6681
else:
67-
scale_a = torch.tensor(1.0, device=a.device)
68-
scale_b = torch.tensor(1.0, device=a.device)
82+
scale_a = _get_scale_per_tensor(a, custom_scale=self.extra_args.per_tensor_scale_a)
83+
scale_b = _get_scale_per_tensor(b, custom_scale=self.extra_args.per_tensor_scale_b)
6984

7085
# Kernels expect dtype=float8_e4m3fn
7186
a = a.to(torch.float8_e4m3fn)
@@ -103,16 +118,10 @@ def get_x_val(self, example_inputs) -> float:
103118
_, n = b.size()
104119
return (m, n, k)
105120

106-
def _get_out_dtype(self):
107-
if self.extra_args.scaling_rowwise:
108-
return torch.bfloat16
109-
else:
110-
return torch.float16
111-
112121
@register_benchmark(baseline=True)
113122
def torch_fp8_gemm(self, a, b, scale_a, scale_b):
114123
return lambda: torch._scaled_mm(
115-
a, b, scale_a, scale_b, use_fast_accum=True, out_dtype=self._get_out_dtype()
124+
a, b, scale_a, scale_b, use_fast_accum=True, out_dtype=self._get_dtype()
116125
)
117126

118127
@register_benchmark()
@@ -129,7 +138,7 @@ def pt2_fp8_gemm(self, a, b, scale_a, scale_b) -> Callable:
129138
scale_a,
130139
scale_b,
131140
use_fast_accum=True,
132-
out_dtype=self._get_out_dtype(),
141+
out_dtype=self._get_dtype(),
133142
)
134143
compiled = torch.compile(f, dynamic=False)
135144
compiled(a, b)

0 commit comments

Comments
 (0)