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

Fix (quant_tensor): clean-up QT creation #1140

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/brevitas/quant_tensor/base_quant_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ class IntQuantTensorBase(NamedTuple):
scale: Tensor
zero_point: Tensor
bit_width: Tensor
signed_t: Tensor
training_t: Tensor
signed: bool
training: bool


class FloatQuantTensorBase(NamedTuple):
Expand All @@ -108,11 +108,11 @@ class FloatQuantTensorBase(NamedTuple):
exponent_bit_width: Tensor
mantissa_bit_width: Tensor
exponent_bias: Tensor
saturating_t: Tensor
saturating: bool
inf_values: List[str]
nan_values: List[str]
signed_t: Tensor
training_t: Tensor
signed: bool
training: bool


class GroupwiseFloatQuantTensorBase(NamedTuple):
Expand All @@ -124,11 +124,11 @@ class GroupwiseFloatQuantTensorBase(NamedTuple):
exponent_bit_width: Tensor
mantissa_bit_width: Tensor
exponent_bias: Tensor
saturating_t: Tensor
saturating: bool
inf_values: List[str]
nan_values: List[str]
signed_t: Tensor
training_t: Tensor
signed: bool
training: bool


class GroupwisIntQuantTensorBase(NamedTuple):
Expand All @@ -138,8 +138,8 @@ class GroupwisIntQuantTensorBase(NamedTuple):
group_size: Tensor
group_dim: Tensor
bit_width: Tensor
signed_t: Tensor
training_t: Tensor
signed: bool
training: bool


def _unpack_quant_tensor(input_data):
Expand Down
18 changes: 1 addition & 17 deletions src/brevitas/quant_tensor/float_quant_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ def __new__(
exponent_bias = torch.tensor(exponent_bias, dtype=torch.float)
if not isinstance(saturating, torch.Tensor):
saturating = torch.tensor(saturating, dtype=torch.bool)
if not isinstance(signed, torch.Tensor):
signed = torch.tensor(signed, dtype=torch.bool)
if not isinstance(training, torch.Tensor):
training = torch.tensor(training, dtype=torch.bool)
quant_tensor = super().__new__(
cls,
value,
Expand All @@ -62,18 +58,6 @@ def __new__(
training)
return quant_tensor

@property
def signed(self):
return self.signed_t.item()

@property
def training(self):
return self.training_t.item()

@property
def saturating(self):
return self.saturating_t.item()

@property
def eps(self):
return torch.finfo(self.scale.dtype).tiny
Expand Down Expand Up @@ -315,7 +299,7 @@ def __mul__(self, other):
return output

def __str__(self):
return f"FloatQuantTensor(value={self.value}, scale={self.scale}, zero_point={self.zero_point}, exponent_bit_width={self.exponent_bit_width}, mantissa_bit_width={self.mantissa_bit_width}, exponent_bias={self.exponent_bias}, inf_values={self.inf_values}, nan_values={self.nan_values}, signed_t={self.signed_t}, training_t={self.training_t})"
return f"FloatQuantTensor(value={self.value}, scale={self.scale}, zero_point={self.zero_point}, exponent_bit_width={self.exponent_bit_width}, mantissa_bit_width={self.mantissa_bit_width}, exponent_bias={self.exponent_bias}, inf_values={self.inf_values}, nan_values={self.nan_values}, signed={self.signed}, training={self.training})"

def __truediv__(self, other):
if isinstance(other, QuantTensor):
Expand Down
21 changes: 2 additions & 19 deletions src/brevitas/quant_tensor/groupwise_float_quant_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@ def __new__(
mantissa_bit_width = torch.tensor(mantissa_bit_width, dtype=torch.float)
if not isinstance(exponent_bias, torch.Tensor):
exponent_bias = torch.tensor(exponent_bias, dtype=torch.float)
if not isinstance(saturating, torch.Tensor):
saturating = torch.tensor(saturating, dtype=torch.bool)
if not isinstance(signed, torch.Tensor):
signed = torch.tensor(signed, dtype=torch.bool)
if not isinstance(training, torch.Tensor):
training = torch.tensor(training, dtype=torch.bool)

quant_tensor = super().__new__(
cls,
value,
Expand All @@ -66,18 +61,6 @@ def __new__(
training)
return quant_tensor

@property
def signed(self):
return self.signed_t.item()

@property
def training(self):
return self.training_t.item()

@property
def saturating(self):
return self.saturating_t.item()

def __torch_function__(self, func, types, args=(), kwargs=None):
if kwargs is None:
kwargs = {}
Expand Down Expand Up @@ -305,7 +288,7 @@ def __mul__(self, other):
return output

def __str__(self):
return f"GroupwiseFloatQuantTensor(value={self.value}, scale={self.scale}, zero_point={self.zero_point}, group_size={self.group_size}, group_dim={self.group_dim}, exponent_bit_width={self.exponent_bit_width}, mantissa_bit_width={self.mantissa_bit_width}, exponent_bias={self.exponent_bias}, inf_values={self.inf_values}, nan_values={self.nan_values}, signed_t={self.signed_t}, training_t={self.training_t})"
return f"GroupwiseFloatQuantTensor(value={self.value}, scale={self.scale}, zero_point={self.zero_point}, group_size={self.group_size}, group_dim={self.group_dim}, exponent_bit_width={self.exponent_bit_width}, mantissa_bit_width={self.mantissa_bit_width}, exponent_bias={self.exponent_bias}, inf_values={self.inf_values}, nan_values={self.nan_values}, signed={self.signed}, training={self.training})"

def __truediv__(self, other):
if isinstance(other, QuantTensor):
Expand Down
18 changes: 3 additions & 15 deletions src/brevitas/quant_tensor/groupwise_int_quant_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,10 @@ def __new__(cls, value, scale, zero_point, group_size, group_dim, bit_width, sig
zero_point = torch.tensor(zero_point, dtype=torch.float)
if not isinstance(bit_width, torch.Tensor):
bit_width = torch.tensor(bit_width, dtype=torch.float)
if not isinstance(signed, torch.Tensor):
signed = torch.tensor(signed, dtype=torch.bool)
if not isinstance(training, torch.Tensor):
training = torch.tensor(training, dtype=torch.bool)
quant_tensor = super().__new__(
cls, value, scale, zero_point, group_size, group_dim, bit_width, signed, training)
return quant_tensor

@property
def signed(self):
return self.signed_t.item()

@property
def training(self):
return self.training_t.item()

@property
def saturating(self):
return self.saturating_t.item()
Expand Down Expand Up @@ -166,9 +154,9 @@ def int(self, float_datatype=False):
else:
return int_value.type(torch.float32)
else:
if self.bit_width <= 8. and self.signed_t.item():
if self.bit_width <= 8. and self.signed:
return int_value.to(torch.int8)
elif self.bit_width <= 8. and not self.signed_t.item():
elif self.bit_width <= 8. and not self.signed:
return int_value.to(torch.uint8)
else:
return int_value.to(torch.int32)
Expand Down Expand Up @@ -278,7 +266,7 @@ def __mul__(self, other):
return output

def __str__(self):
return f"GroupwiseIntQuantTensor(value={self.value}, scale={self.scale}, zero_point={self.zero_point}, group_size={self.group_size}, group_dim={self.group_dim}, bit_width={self.bit_width}, signed_t={self.signed_t}, training_t={self.training_t})"
return f"GroupwiseIntQuantTensor(value={self.value}, scale={self.scale}, zero_point={self.zero_point}, group_size={self.group_size}, group_dim={self.group_dim}, bit_width={self.bit_width}, signed={self.signed}, training={self.training})"

def __truediv__(self, other):
if isinstance(other, QuantTensor):
Expand Down
18 changes: 3 additions & 15 deletions src/brevitas/quant_tensor/int_quant_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,9 @@ def __new__(cls, value, scale, zero_point, bit_width, signed, training):
zero_point = torch.tensor(zero_point, dtype=torch.float)
if not isinstance(bit_width, torch.Tensor):
bit_width = torch.tensor(bit_width, dtype=torch.float)
if not isinstance(signed, torch.Tensor):
signed = torch.tensor(signed, dtype=torch.bool)
if not isinstance(training, torch.Tensor):
training = torch.tensor(training, dtype=torch.bool)
quant_tensor = super().__new__(cls, value, scale, zero_point, bit_width, signed, training)
return quant_tensor

@property
def signed(self):
return self.signed_t.item()

@property
def training(self):
return self.training_t.item()

def __torch_function__(self, func, types, args=(), kwargs=None):
if kwargs is None:
kwargs = {}
Expand Down Expand Up @@ -118,9 +106,9 @@ def int(self, float_datatype=False):
else:
return int_value.type(torch.float32)
else:
if self.bit_width <= 8. and self.signed_t.item():
if self.bit_width <= 8. and self.signed:
return int_value.to(torch.int8)
elif self.bit_width <= 8. and not self.signed_t.item():
elif self.bit_width <= 8. and not self.signed:
return int_value.to(torch.uint8)
else:
return int_value.to(torch.int32)
Expand Down Expand Up @@ -321,7 +309,7 @@ def __mul__(self, other):
return output

def __str__(self):
return f"IntQuantTensor(value={self.value}, scale={self.scale}, zero_point={self.zero_point}, bit_width={self.bit_width}, signed_t={self.signed_t}, training_t={self.training_t})"
return f"IntQuantTensor(value={self.value}, scale={self.scale}, zero_point={self.zero_point}, bit_width={self.bit_width}, signed={self.signed}, training={self.training})"

def __truediv__(self, other):
if isinstance(other, IntQuantTensor):
Expand Down
Loading