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/Feat (trunc avg pool): Update truncation and average pool behaviour #1042

Open
wants to merge 3 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
5 changes: 3 additions & 2 deletions src/brevitas/core/quant/int.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,13 @@ def forward(self, x: Tensor, scale: Tensor, zero_point: Tensor,
output_bit_width = self.msb_clamp_bit_width_impl()
trunc_bit_width = input_bit_width - output_bit_width
trunc_scale = 2.0 ** trunc_bit_width
output_scale = scale * trunc_scale
y = y / trunc_scale
y = self.float_to_int_impl(y)
y = y - zero_point
y = y * scale
y = y * output_scale
y = self.delay_wrapper(x, y)
return y, scale, zero_point, output_bit_width
return y, output_scale, zero_point, output_bit_width


class DecoupledRescalingIntQuantWithInput(DecoupledRescalingIntQuant):
Expand Down
8 changes: 2 additions & 6 deletions src/brevitas/nn/quant_avg_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def _avg_scaling(self):
else:
return self.kernel_size * self.kernel_size

# TODO: Replace with functional call
def forward(self, input: Union[Tensor, QuantTensor]):
x = self.unpack_input(input)

Expand All @@ -62,8 +63,6 @@ def forward(self, input: Union[Tensor, QuantTensor]):

if isinstance(x, QuantTensor) and self.is_trunc_quant_enabled:
y = AvgPool2d.forward(self, x)
rescaled_value = y.value * self._avg_scaling
y = y.set(value=rescaled_value)
y = self.trunc_quant(y)
else:
y = AvgPool2d.forward(self, _unpack_quant_tensor(x))
Expand Down Expand Up @@ -111,6 +110,7 @@ def compute_kernel_size_stride(input_shape, output_shape):
stride_list.append(stride)
return kernel_size_list, stride_list

# TODO: Replace with functional call
def forward(self, input: Union[Tensor, QuantTensor]):
x = self.unpack_input(input)

Expand All @@ -122,10 +122,6 @@ def forward(self, input: Union[Tensor, QuantTensor]):

if isinstance(x, QuantTensor) and self.is_trunc_quant_enabled:
y = AdaptiveAvgPool2d.forward(self, x)
k_size, stride = self.compute_kernel_size_stride(x.value.shape[2:], y.value.shape[2:])
reduce_size = reduce(mul, k_size, 1)
rescaled_value = y.value * reduce_size # remove avg scaling
y = y.set(value=rescaled_value)
y = self.trunc_quant(y)
else:
y = AdaptiveAvgPool2d.forward(self, _unpack_quant_tensor(x))
Expand Down
2 changes: 2 additions & 0 deletions src/brevitas/quant_tensor/int_torch_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def avg_pool2d_handler(
avg_scaling = kernel_size * kernel_size

quant_input = quant_input.set(value=x)
quant_input = quant_input.set(scale=quant_input.scale / avg_scaling)
quant_input = quant_input.set(bit_width=max_acc_bit_width(quant_input.bit_width, avg_scaling))
return quant_input

Expand All @@ -133,6 +134,7 @@ def adaptive_avg_pool2d_handler(quant_input, output_shape):
reduce_size = reduce(mul, k_size, 1)

quant_input = quant_input.set(value=x)
quant_input = quant_input.set(scale=quant_input.scale / reduce_size)
quant_input = quant_input.set(bit_width=max_acc_bit_width(quant_input.bit_width, reduce_size))
return quant_input

Expand Down
Loading