Multiple updates and refactorings (#231)
This commit is contained in:
+111
-14
@@ -3,8 +3,13 @@ import torch
|
||||
|
||||
import deep_gemm
|
||||
from deep_gemm.testing import (
|
||||
bench, bench_kineto,
|
||||
calc_diff, count_bytes
|
||||
bench_kineto,
|
||||
calc_diff, count_bytes,
|
||||
get_arch_major, test_filter
|
||||
)
|
||||
from deep_gemm.utils.math import (
|
||||
ceil_div,
|
||||
per_block_cast_to_fp8, per_channel_cast_to_fp8, per_token_cast_to_fp8
|
||||
)
|
||||
|
||||
|
||||
@@ -33,8 +38,8 @@ def test_bmk_bnk_mn() -> None:
|
||||
|
||||
def test_bhr_hdr_bhd():
|
||||
print('Testing "bhr, hdr -> bhd":')
|
||||
for b in (128, 4096, 8192):
|
||||
for h, r, d in [(128, 512, 128)]:
|
||||
for h, r, d in [(128, 512, 128), (8, 4096, 1024)]:
|
||||
for b in (4, 32, 128, 4096, 8192):
|
||||
x = torch.randn((b, h, r), device='cuda', dtype=torch.bfloat16)
|
||||
fy = torch.randn((h, d, r + 128), device='cuda', dtype=torch.bfloat16)
|
||||
y = fy[:, :, :r]
|
||||
@@ -43,18 +48,20 @@ def test_bhr_hdr_bhd():
|
||||
deep_gemm.einsum('bhr,hdr->bhd', x, y, z)
|
||||
assert calc_diff(z, ref_z) < 1e-10
|
||||
|
||||
t = bench_kineto(lambda: deep_gemm.einsum('bhr,hdr->bhd', x, y, z), 'nvjet', suppress_kineto_output=True)
|
||||
t = bench_kineto(lambda: deep_gemm.einsum('bhr,hdr->bhd', x, y, z), 'gemm', suppress_kineto_output=True)
|
||||
t_cublaslt = bench_kineto(lambda: deep_gemm.einsum('bhr,hdr->bhd', x, y, z, use_cublaslt=True), 'nvjet', suppress_kineto_output=True)
|
||||
print(f' > Perf ({b=:4.0f}, {h=}, {r=}, {d=}): ',
|
||||
f'{t * 1e6:4.0f} us | '
|
||||
f'{2 * b * h * r * d / t / 1e12:.0f} TFLOPS | '
|
||||
f'{count_bytes((x, y, z)) / t / 1e9:.0f} GB/s')
|
||||
f'{2 * b * h * r * d / t / 1e12:4.0f} TFLOPS | '
|
||||
f'{count_bytes((x, y, z)) / t / 1e9:4.0f} GB/s | '
|
||||
f'{t_cublaslt / t:4.2f} x')
|
||||
print()
|
||||
|
||||
|
||||
def test_bhd_hdr_bhr():
|
||||
print('Testing "bhd, hdr -> bhr":')
|
||||
for b in (128, 4096, 8192):
|
||||
for h, r, d in [(128, 512, 128)]:
|
||||
for h, r, d in [(128, 512, 128), (8, 4096, 1024)]:
|
||||
for b in (4, 32, 128, 4096, 8192):
|
||||
x = torch.randn((b, h, d), device='cuda', dtype=torch.bfloat16)
|
||||
fy = torch.randn((h, d, r + 128), device='cuda', dtype=torch.bfloat16)
|
||||
y = fy[:, :, :r]
|
||||
@@ -63,17 +70,103 @@ def test_bhd_hdr_bhr():
|
||||
deep_gemm.einsum('bhd,hdr->bhr', x, y, z)
|
||||
assert calc_diff(z, ref_z) < 1e-10
|
||||
|
||||
t = bench_kineto(lambda: deep_gemm.einsum('bhd,hdr->bhr', x, y, z), 'nvjet', suppress_kineto_output=True)
|
||||
t = bench_kineto(lambda: deep_gemm.einsum('bhd,hdr->bhr', x, y, z), 'gemm', suppress_kineto_output=True)
|
||||
t_cublaslt = bench_kineto(lambda: deep_gemm.einsum('bhd,hdr->bhr', x, y, z, use_cublaslt=True), 'nvjet', suppress_kineto_output=True)
|
||||
print(f' > Perf ({b=:4.0f}, {h=}, {r=}, {d=}): ',
|
||||
f'{t * 1e6:4.0f} us | '
|
||||
f'{2 * b * h * r * d / t / 1e12:.0f} TFLOPS | '
|
||||
f'{count_bytes((x, y, z)) / t / 1e9:.0f} GB/s')
|
||||
f'{2 * b * h * r * d / t / 1e12:4.0f} TFLOPS | '
|
||||
f'{count_bytes((x, y, z)) / t / 1e9:4.0f} GB/s | '
|
||||
f'{t_cublaslt / t:4.2f} x')
|
||||
print()
|
||||
|
||||
|
||||
@test_filter(lambda: get_arch_major() >= 10)
|
||||
def test_fp8_bhr_hdr_bhd(use_ue8m0: bool = True):
|
||||
print('Testing FP8 "bhr, hdr -> bhd":')
|
||||
for h, r, d in [(8, 4096, 1024)]:
|
||||
for b in (4, 32, 128, 4096, 8192):
|
||||
x = torch.randn((b, h, r), device='cuda', dtype=torch.bfloat16)
|
||||
y = torch.randn((h, d, r), device='cuda', dtype=torch.bfloat16)
|
||||
ref_z = torch.einsum('bhr,hdr->bhd', x, y)
|
||||
|
||||
x_fp8 = per_token_cast_to_fp8(x.view(-1, r), use_ue8m0=use_ue8m0)
|
||||
x_fp8 = x_fp8[0].view(b, h, r), x_fp8[1].view(b, h, ceil_div(r, 128))
|
||||
y_fp8 = (torch.empty_like(y, dtype=torch.float8_e4m3fn),
|
||||
torch.empty((h, ceil_div(d, 128), ceil_div(r, 128)), device='cuda', dtype=torch.float))
|
||||
for i in range(h):
|
||||
y_fp8[0][i], y_fp8[1][i] = per_block_cast_to_fp8(y[i], use_ue8m0=use_ue8m0)
|
||||
z = torch.empty((b, h, d), device='cuda', dtype=torch.bfloat16)
|
||||
|
||||
deep_gemm.fp8_einsum('bhr,hdr->bhd', x_fp8, y_fp8, z)
|
||||
assert calc_diff(z, ref_z) < 1e-3
|
||||
|
||||
t = bench_kineto(lambda: deep_gemm.fp8_einsum('bhr,hdr->bhd', x_fp8, y_fp8, z), 'fp8_gemm', suppress_kineto_output=True)
|
||||
t_cublaslt = bench_kineto(lambda: deep_gemm.einsum('bhr,hdr->bhd', x, y, z, use_cublaslt=True), 'nvjet', suppress_kineto_output=True)
|
||||
print(f' > Perf ({b=:4.0f}, {h=}, {r=}, {d=}): ',
|
||||
f'{t * 1e6:4.0f} us | '
|
||||
f'{2 * b * h * r * d / t / 1e12:4.0f} TFLOPS | '
|
||||
f'{count_bytes((x_fp8, y_fp8, z)) / t / 1e9:4.0f} GB/s | '
|
||||
f'{t_cublaslt / t:4.2f} x')
|
||||
print()
|
||||
|
||||
|
||||
@test_filter(lambda: get_arch_major() >= 10)
|
||||
def test_fp8_bhd_hdr_bhr(use_ue8m0: bool = True):
|
||||
print('Testing FP8 "bhd, hdr -> bhr":')
|
||||
for h, r, d in [(8, 4096, 1024)]:
|
||||
for b in (4, 32, 128, 4096, 8192):
|
||||
x = torch.randn((b, h, d), device='cuda', dtype=torch.bfloat16)
|
||||
y = torch.randn((h, d, r), device='cuda', dtype=torch.bfloat16)
|
||||
ref_z = torch.einsum('bhd,hdr->bhr', x, y)
|
||||
|
||||
x_fp8 = per_token_cast_to_fp8(x.view(-1, d), use_ue8m0=use_ue8m0)
|
||||
x_fp8 = x_fp8[0].view(b, h, d), x_fp8[1].view(b, h, ceil_div(d, 128))
|
||||
y_fp8 = (torch.empty_like(y, dtype=torch.float8_e4m3fn),
|
||||
torch.empty((h, ceil_div(d, 128), ceil_div(r, 128)), device='cuda', dtype=torch.float))
|
||||
for i in range(h):
|
||||
y_fp8[0][i], y_fp8[1][i] = per_block_cast_to_fp8(y[i], use_ue8m0=use_ue8m0)
|
||||
z = torch.empty((b, h, r), device='cuda', dtype=torch.bfloat16)
|
||||
|
||||
deep_gemm.fp8_einsum('bhd,hdr->bhr', x_fp8, y_fp8, z)
|
||||
assert calc_diff(z, ref_z) < 1e-3
|
||||
|
||||
t = bench_kineto(lambda: deep_gemm.fp8_einsum('bhd,hdr->bhr', x_fp8, y_fp8, z), 'fp8_gemm', suppress_kineto_output=True)
|
||||
t_cublaslt = bench_kineto(lambda: deep_gemm.einsum('bhd,hdr->bhr', x, y, z, use_cublaslt=True), 'nvjet', suppress_kineto_output=True)
|
||||
print(f' > Perf ({b=:4.0f}, {h=}, {r=}, {d=}): ',
|
||||
f'{t * 1e6:4.0f} us | '
|
||||
f'{2 * b * h * r * d / t / 1e12:4.0f} TFLOPS | '
|
||||
f'{count_bytes((x_fp8, y_fp8, z)) / t / 1e9:4.0f} GB/s | '
|
||||
f'{t_cublaslt / t:4.2f} x')
|
||||
print()
|
||||
|
||||
|
||||
@test_filter(lambda: get_arch_major() >= 10)
|
||||
def test_fp8_bhd_bhr_hdr(use_ue8m0: bool = True):
|
||||
print('Testing FP8 "bhd, bhr -> hdr":')
|
||||
for h, r, d in [(8, 4096, 1024)]:
|
||||
for b in (4096, 8192):
|
||||
x = torch.randn((b, h, d), device='cuda', dtype=torch.bfloat16)
|
||||
y = torch.randn((b, h, r), device='cuda', dtype=torch.bfloat16)
|
||||
z_0 = torch.randn((h, d, r), device='cuda', dtype=torch.float32) * 10
|
||||
ref_z = z_0 + torch.einsum('bhd,bhr->hdr', x, y)
|
||||
|
||||
x_fp8 = per_channel_cast_to_fp8(x.view(b, -1), use_ue8m0=use_ue8m0)
|
||||
y_fp8 = per_channel_cast_to_fp8(y.view(b, -1), use_ue8m0=use_ue8m0)
|
||||
x_fp8 = (x_fp8[0].view(b, h, d), x_fp8[1].view(ceil_div(b, 128), h, d))
|
||||
y_fp8 = (y_fp8[0].view(b, h, r), y_fp8[1].view(ceil_div(b, 128), h, r))
|
||||
z = z_0.clone()
|
||||
deep_gemm.fp8_einsum('bhd,bhr->hdr', x_fp8, y_fp8, z, z, recipe=(1, 1, 128))
|
||||
assert calc_diff(z, ref_z) < 1e-3
|
||||
|
||||
t = bench_kineto(lambda: deep_gemm.fp8_einsum('bhd,bhr->hdr', x_fp8, y_fp8, z, z, recipe=(1, 1, 128)), 'fp8_gemm', suppress_kineto_output=True)
|
||||
print(f' > Perf ({b=:4.0f}, {h=}, {r=}, {d=}): ',
|
||||
f'{t * 1e6:4.0f} us | '
|
||||
f'{2 * b * h * r * d / t / 1e12:4.0f} TFLOPS | '
|
||||
f'{count_bytes((x_fp8, y_fp8, z, z)) / t / 1e9:4.0f} GB/s')
|
||||
print()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
torch.backends.cuda.matmul.allow_tf32 = True
|
||||
torch.backends.cudnn.allow_tf32 = True
|
||||
torch.manual_seed(0)
|
||||
random.seed(0)
|
||||
|
||||
@@ -83,3 +176,7 @@ if __name__ == '__main__':
|
||||
test_bmk_bnk_mn()
|
||||
test_bhr_hdr_bhd()
|
||||
test_bhd_hdr_bhr()
|
||||
|
||||
test_fp8_bhr_hdr_bhd()
|
||||
test_fp8_bhd_hdr_bhr()
|
||||
test_fp8_bhd_bhr_hdr()
|
||||
|
||||
Reference in New Issue
Block a user