forked from ROCm/aiter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_quant.py
More file actions
131 lines (120 loc) · 3.33 KB
/
Copy pathtest_quant.py
File metadata and controls
131 lines (120 loc) · 3.33 KB
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
# SPDX-License-Identifier: MIT
# Copyright (C) 2024-2025, Advanced Micro Devices, Inc. All rights reserved.
from aiter.test_common import (
checkAllclose,
benchmark,
run_perftest,
)
import torch
import aiter
from aiter import dtypes
from aiter import get_hip_quant, get_torch_quant, get_triton_quant
import itertools
import argparse
import pandas as pd
torch.set_default_device("cuda")
@benchmark()
def test_quant(m, n, q_type, q_dtype, h_dtype):
dim = (m, n)
input = torch.randn(dim, dtype=h_dtype)
ref, ref_scale = get_torch_quant(q_type)(input, quant_dtype=q_dtype)
q_funcs = {
"triton": get_triton_quant,
"hip": get_hip_quant,
}
ret = {}
for name, q_func in q_funcs.items():
q_func = q_func(q_type)
(out, scale), us1 = run_perftest(q_func, input, quant_dtype=q_dtype)
err1 = checkAllclose(
ref.to(dtypes.fp32),
out.to(dtypes.fp32),
rtol=1e-3,
atol=1e-3,
msg=f"{name}: dynamic quant",
)
checkAllclose(
ref_scale.to(dtypes.fp32),
scale.to(dtypes.fp32),
rtol=1e-3,
atol=1e-3,
msg=f"{name}: dynamic quant scale",
)
ret[f"{name} dq"] = us1
ret[f"{name} dq err"] = err1
if q_type == aiter.QuantType.per_Tensor:
(out, scale), us2 = run_perftest(
q_func, input, ref_scale, quant_dtype=q_dtype
)
err2 = checkAllclose(
ref.to(dtypes.fp32),
out.to(dtypes.fp32),
rtol=1e-3,
atol=1e-3,
msg=f"{name}: static quant",
)
ret[f"{name} sq"] = us2
ret[f"{name} sq err"] = err2
return ret
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter,
description="config input of test",
)
parser.add_argument(
"-d",
"--dtype",
type=dtypes.str2Dtype,
nargs="*",
default=[dtypes.d_dtypes["fp16"], dtypes.d_dtypes["bf16"]],
help="""Data type.
e.g.: -d bf16""",
)
parser.add_argument(
"-n",
"--n",
type=int,
nargs="*",
default=[4096, 8192],
help="""N of mnk.
e.g.: -n 1024""",
)
parser.add_argument(
"-m",
"--m",
type=int,
nargs="*",
default=[1, 2, 16, 32, 64, 128, 192, 256, 512, 1024, 16384, 163840],
help="""M of mnk.
e.g.: -m 32""",
)
d_quant = {
"fp8_tensor": (aiter.QuantType.per_Tensor, dtypes.fp8),
"fp8_token": (aiter.QuantType.per_Token, dtypes.fp8),
"fp8_1x128": (aiter.QuantType.per_1x128, dtypes.fp8),
"i8_token": (aiter.QuantType.per_Token, dtypes.i8),
# 'fp4x2-1x32': (aiter.QuantType.per_1x32, dtypes.fp4x2),
}
parser.add_argument(
"-q",
"--quant",
type=str,
choices=list(d_quant.keys()),
nargs="*",
default=list(d_quant.keys()),
help="""Quantization type.
e.g.: -q fp8_tensor""",
)
args = parser.parse_args()
list_quant = [d_quant[key] for key in args.quant]
for (
(q_type, q_dtype),
h_dtype,
) in itertools.product(list_quant, args.dtype):
df = []
for n in args.n:
for m in args.m:
ret = test_quant(m, n, q_type, q_dtype, h_dtype)
df.append(ret)
df = pd.DataFrame(df)
df_md = df.to_markdown(index=False)
aiter.logger.info("quant summary (markdown):\n%s", df_md)