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

Unexpected output shape value for BatchNorm1dToQuantScaleBias #450

Open
green-cabbage opened this issue Nov 11, 2022 · 3 comments
Open

Unexpected output shape value for BatchNorm1dToQuantScaleBias #450

green-cabbage opened this issue Nov 11, 2022 · 3 comments

Comments

@green-cabbage
Copy link

Hey, I have been trying to implement brevitas' version of nn.BatchNorm1d(), and I receive this error:
ValueError: Using a target size (torch.Size([64, 1])) that is different to the input size (torch.Size([1, 30, 64, 1])) is deprecated. Please ensure they have the same size.

I have backtracked the error to the point where I believe this is the cause. When I run this:

input_dim = 30
batch_dim = 64
x = torch.rand(64,30)
quant_batchnorm = qnn.BatchNorm1dToQuantScaleBias(
                  num_features=input_dim,
                  return_quant_tensor=True
)  
print(quant_batchnorm(x).shape)

the code prints: torch.Size([1, 30, 64, 30]) when I would expect it to return something like torch.Size([64,30]). Do you guys know why the quant layer returns in that specific shape of [1, input_dim, batch_dim, input_dim] ? Is this behavior intentional? If so why?
Thanks in advance

@green-cabbage
Copy link
Author

Issue solved:

There was two small changes I made which made it work.

First, the default parameter runtime_shape=(1, -1, 1) for class BatchNorm1dToQuantScaleBias should actually be runtime_shape=(1, -1) . At least that's the shape that worked for me, and makes sense in the context of torch.nn.BatchNorm1d

Second, I was initially getting runtime_shape=(1, -1, 1, 1) because that is class ScaleBias's default value, and class BatchNorm1dToQuantScaleBias wasn't passing down runtime_shapeparameter properly.
I solved this by changing from ScaleBias.__init__(self, num_features, bias) to ScaleBias.__init__(self, num_features, bias, runtime_shape=runtime_shape) when initializing class QuantScaleBias

If you would like, I can make a pull request with these changes.

@Giuseppe5
Copy link
Collaborator

Hi,

Thanks for looking into this!
Please feel free to open a PR on the dev branch with the fix.

@MikeHatchi
Copy link

Hi @green-cabbage many thanks, I've applied the solution.
I have not noticed normalization actually. In my case, for eg.: among the input data, I had 1.32.... and after BN1dScaleBias 1.39 au lieu de instead of 0.something.

Hi @Giuseppe5 , I was wondering why BN1dScaleBias operation is a missing topic as BN1d is a very largely used in non-quantize context? Is it because as quantization is about low precision, then it is not supposed to use BN logic?

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

3 participants