Skip to content

Commit 4dd49a4

Browse files
authored
add hasattr check for torch fp8 dtype (#1985)
Signed-off-by: xin3he <xin3.he@intel.com>
1 parent f2c454f commit 4dd49a4

File tree

1 file changed

+12
-6
lines changed
  • neural_compressor/torch/algorithms/weight_only

1 file changed

+12
-6
lines changed

neural_compressor/torch/algorithms/weight_only/utility.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,18 @@
9393

9494
FLOAT_MAPPING = {"nf4": NF4, "fp4": FP4_BNB, "fp4_e2m1_bnb": FP4_BNB, "fp4_e2m1": FP4_E2M1}
9595
INT_MAPPING = {"nf4": NF4_BIT, "fp4": FP4_BNB_BIT, "fp4_e2m1_bnb": FP4_BNB_BIT, "fp4_e2m1": FP4_E2M1_BIT}
96-
FP8_MAPPING = {
97-
"fp8_e5m2": torch.float8_e5m2,
98-
"fp8_e5m2fnuz": torch.float8_e5m2fnuz,
99-
"fp8_e4m3fn": torch.float8_e4m3fn,
100-
"fp8_e4m3fnuz": torch.float8_e4m3fnuz,
101-
}
96+
if hasattr(torch, "float8_e5m2") and hasattr(torch, "float8_e4m3fn"):
97+
FP8_MAPPING = {
98+
"fp8_e5m2": torch.float8_e5m2,
99+
"fp8_e4m3fn": torch.float8_e4m3fn,
100+
}
101+
if hasattr(torch, "float8_e5m2fnuz") and hasattr(torch, "float8_e4m3fnuz"):
102+
FP8_MAPPING = {
103+
"fp8_e5m2": torch.float8_e5m2,
104+
"fp8_e4m3fn": torch.float8_e4m3fn,
105+
"fp8_e5m2fnuz": torch.float8_e5m2fnuz,
106+
"fp8_e4m3fnuz": torch.float8_e4m3fnuz,
107+
}
102108

103109

104110
def quantize_4bit(tensor, quantile=1.0, dtype="nf4", return_int=False, **kwargs):

0 commit comments

Comments
 (0)