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

Feature Request: More padding mode. #994

Closed
Soptq opened this issue Jul 28, 2024 · 4 comments
Closed

Feature Request: More padding mode. #994

Soptq opened this issue Jul 28, 2024 · 4 comments

Comments

@Soptq
Copy link

Soptq commented Jul 28, 2024

In many applications we want to make sure that the padding alters the local feature structure and global statistics the least (e.g. Image-to-image translation). Using zero padding in these applications will result in peaking edges[1][2].

However, only zero padding is supported by brevitas by now. I wonder is it possible to also support other padding mode like reflection or symmetric padding in brevitas? or is there a workaround?

[1] https://arxiv.org/abs/1703.10593
[2] https://arxiv.org/abs/1811.11718

@Giuseppe5
Copy link
Collaborator

Giuseppe5 commented Jul 29, 2024

I think it should be relatively straightforward to implement your own padding mode.
Inherit a new QuantConv from brevitas' one, then you'd need to override the init and this:

def inner_forward_impl(self, x: Tensor, quant_weight: Tensor, quant_bias: Optional[Tensor]):
if self.is_same_padded_strided:
return self.conv2d_same_zeros_pad_stride(x, quant_weight, quant_bias)
else:
return self._conv_forward(x, quant_weight, quant_bias)

in order to call your custom padding mode. As long as you call torch.nn.functional.conv2d (or 1d, etc.) with the input/weight/bias tensor that are passed to inner_forward_impl, everything else with respect to quantization is handled automatically.

@Giuseppe5
Copy link
Collaborator

If you think that this feature could be beneficial for others and are willing to contribute to Brevitas, we would be more than happy to accept a PR.

@Soptq
Copy link
Author

Soptq commented Jul 31, 2024

Sure, I will try to make a PR later ;)

@Giuseppe5
Copy link
Collaborator

Closing this now. Feel free to re-open if you need more help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants