Add arc fp8 support (#9232)

* add fp8 support

* add log

* fix style
This commit is contained in:
Yina Chen 2023-10-20 17:15:07 +08:00 committed by GitHub
parent 26850ebd36
commit 0383306688
3 changed files with 15 additions and 6 deletions

View file

@ -32,7 +32,8 @@ ggml_tensor_qtype = {"sym_int4": 2, # q4_0 in ggml
"sym_int8": 8, # q8_0 in ggml "sym_int8": 8, # q8_0 in ggml
"nf4": 10, "nf4": 10,
"nf3": 11, "nf3": 11,
"fp16": 12} "fp16": 12,
"fp8": 15}
_llama_quantize_type = {"q4_0": 2, _llama_quantize_type = {"q4_0": 2,
"q4_1": 3, "q4_1": 3,

View file

@ -146,6 +146,9 @@ def _optimize_pre(model):
def ggml_convert_low_bit(model, qtype, optimize_model=True, def ggml_convert_low_bit(model, qtype, optimize_model=True,
convert_shape_only=False, device="cpu", convert_shape_only=False, device="cpu",
modules_to_not_convert=None): modules_to_not_convert=None):
logger.info(f"Converting the current model to "
f"{list(ggml_tensor_qtype.keys())[list(ggml_tensor_qtype.values()).index(qtype)]} "
f"format......")
modules_to_not_convert = [] if modules_to_not_convert is None else modules_to_not_convert modules_to_not_convert = [] if modules_to_not_convert is None else modules_to_not_convert
if optimize_model: if optimize_model:

View file

@ -64,6 +64,7 @@ SYM_INT4 = ggml_tensor_qtype["sym_int4"]
SYM_INT8 = ggml_tensor_qtype["sym_int8"] SYM_INT8 = ggml_tensor_qtype["sym_int8"]
NF4 = ggml_tensor_qtype["nf4"] NF4 = ggml_tensor_qtype["nf4"]
NF3 = ggml_tensor_qtype["nf3"] NF3 = ggml_tensor_qtype["nf3"]
FP8 = ggml_tensor_qtype["fp8"]
def ggml_convert_qtype(tensor: torch.Tensor, qtype: int, def ggml_convert_qtype(tensor: torch.Tensor, qtype: int,
@ -87,9 +88,13 @@ def ggml_convert_qtype(tensor: torch.Tensor, qtype: int,
device=device) device=device)
if not convert_shape_only and device != 'meta': if not convert_shape_only and device != 'meta':
dst = ctypes.c_void_p(dst_tensor.data.data_ptr()) if qtype == FP8:
hist = (ctypes.c_int64 * 16)() import linear_q4_0
ggml.ggml_quantize_tensor(src, dst, qtype, n, k, hist) linear_q4_0.cvt_fp32_e4m3_rne(tensor, dst_tensor, n, k)
else:
dst = ctypes.c_void_p(dst_tensor.data.data_ptr())
hist = (ctypes.c_int64 * 16)()
ggml.ggml_quantize_tensor(src, dst, qtype, n, k, hist)
return dst_tensor return dst_tensor
@ -378,8 +383,8 @@ class LowBitLinear(nn.Linear):
else: else:
# CPU logic # CPU logic
# todo may need to set a different number on different platforms # todo may need to set a different number on different platforms
invalidInputError(self.qtype != NF3 and self.qtype != NF4, invalidInputError(self.qtype != NF3 and self.qtype != NF4 and self.qtype != FP8,
"NF3 and NF4 quantization are currently not supported on CPU") "NF3, NF4 and FP8 quantization are currently not supported on CPU")
if IS_SERVER and (not IS_SPR) and \ if IS_SERVER and (not IS_SPR) and \
self.qtype == SYM_INT4 and x_2d.shape[0] >= TORCH_LINEAR_THRESHOLD: self.qtype == SYM_INT4 and x_2d.shape[0] >= TORCH_LINEAR_THRESHOLD:
x0_fp32 = ggml_int4_convert_fp32(x0, self.weight_shape, self.weight_length) x0_fp32 = ggml_int4_convert_fp32(x0, self.weight_shape, self.weight_length)