Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2024-05-30 Add FP8 PTQ #1877

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
48 changes: 32 additions & 16 deletions paddleslim/quant/observers/uniform.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,24 @@ def __init__(
def qmin_qmax(self):
""" Calculate the range of the quantized integer based on the specified
quant_bits, sign, and symmetric properties."""
if self._sign:
self._qmin = -2**(self.bit_length() - 1)
self._qmax = 2**(self.bit_length() - 1) - 1
else:
self._qmin = 0
self._qmax = 2**self.bit_length()
if isinstance(self._quant_bits,tuple):
if (self._quant_bits[0]==4 and self._quant_bits[1]==3 and len(self._quant_bits)==2):
self._qmin = -448.0
self._qmax = 448.0
elif (self._quant_bits[0]==5 and self._quant_bits[1]==2 and len(self._quant_bits)==2):
self._qmin = 57344.0
self._qmax = 57344.0
else:
raise NotImplementedError(
"Currently, only float8_e4m3 and float8_e5m2 formats are supported. Please set quant_bits to (4,3) or (5,2) for the corresponding format."
)
else:
if self._sign:
self._qmin = -2**(self.bit_length() - 1)
self._qmax = 2**(self.bit_length() - 1) - 1
else:
self._qmin = 0
self._qmax = 2**self.bit_length()
return self._qmin, self._qmax

@abc.abstractmethod
Expand All @@ -87,15 +99,19 @@ def cal_scales_zero_points(self) -> Tuple[float, float]:
# It is important to ensure that common operations like zero padding do not cause quantization errors.
_min = min(self.min_value(), 0.)
_max = max(self.max_value(), 0.)

if self._symmetric:
self._scale = max(-_min, _max)
if self._sign:
self._zero_point = 0
if isinstance(self._quant_bits,tuple):
_abs_max = max(-_min, _max)
self._scale = _qmax / _abs_max
self._zero_point = 0
else:
if self._symmetric:
self._scale = max(-_min, _max)
if self._sign:
self._zero_point = 0
else:
self._zero_point = (_qmax + _qmin) / 2
else:
self._zero_point = (_qmax + _qmin) / 2
else:
self._scale = (_max - _min) / float(_qmax - _qmin)
self._zero_point = _qmin - round(_min / self._scale)
self._zero_point = np.clip(self._zero_point, _qmin, _qmax)
self._scale = (_max - _min) / float(_qmax - _qmin)
self._zero_point = _qmin - round(_min / self._scale)
self._zero_point = np.clip(self._zero_point, _qmin, _qmax)
return self._scale, self._zero_point