-
Notifications
You must be signed in to change notification settings - Fork 4
Fix: Multiple bug fixes #4
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
Open
JanCSEM
wants to merge
4
commits into
pulp-platform:devel
Choose a base branch
from
JanCSEM:channel-wise-scales
base: devel
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+197
−4
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8890e21
Fix scaling factor Tensor -> Float conversion if Tensor has more than…
JanCSEM 1d4f293
Allow for node arguments to be a tuple or list
JanCSEM 9402356
Fix MHA to return a tuple for consistency with pytorch and brevitas i…
JanCSEM 648155a
Add tests for channel wise weight quantization
JanCSEM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # Copyright 2025 ETH Zurich and University of Bologna. | ||
| # Licensed under the Apache License, Version 2.0, see LICENSE for details. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Victor Jung <jungvi@iis.ee.ethz.ch> | ||
| # Federico Brancasi <fbrancasi@ethz.ch> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You deserve the authorship of this test 😁 |
||
|
|
||
| import pytest | ||
| import torch | ||
| import torch.nn as nn | ||
| import brevitas.nn as qnn | ||
| from brevitas.quant.scaled_int import ( | ||
| Int8ActPerTensorFloat, | ||
| Int32Bias, | ||
| Int8WeightPerChannelFloat | ||
| ) | ||
| from DeepQuant.ExportBrevitas import exportBrevitas | ||
|
|
||
|
|
||
| class QuantConvNet(nn.Module): | ||
|
|
||
| convAndLinQuantParams = { | ||
| "bias": True, | ||
| "weight_bit_width": 4, | ||
| "bias_quant": Int32Bias, | ||
| "input_quant": Int8ActPerTensorFloat, | ||
| "weight_quant": Int8WeightPerChannelFloat, | ||
| "output_quant": Int8ActPerTensorFloat, | ||
| "return_quant_tensor": True, | ||
| } | ||
|
|
||
| def __init__(self, in_channels: int = 1) -> None: | ||
| super().__init__() | ||
| self.inputQuant = qnn.QuantIdentity(return_quant_tensor=True) | ||
|
|
||
| self.conv1 = qnn.QuantConv2d( | ||
| in_channels=in_channels, | ||
| out_channels=16, | ||
| kernel_size=3, | ||
| padding=1, | ||
| **QuantConvNet.convAndLinQuantParams | ||
| ) | ||
|
|
||
| def forward(self, x: torch.Tensor) -> torch.Tensor: | ||
|
|
||
| x = self.inputQuant(x) | ||
| x = self.conv1(x) | ||
|
|
||
| return x | ||
|
|
||
|
|
||
| @pytest.mark.SingleLayerTests | ||
| def deepQuantTestConv() -> None: | ||
|
|
||
| torch.manual_seed(42) | ||
|
|
||
| model = QuantConvNet().eval() | ||
| sampleInput = torch.randn(1, 1, 28, 28) | ||
| exportBrevitas(model, sampleInput, debug=True) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| # Copyright 2025 ETH Zurich and University of Bologna. | ||
| # Licensed under the Apache License, Version 2.0, see LICENSE for details. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Federico Brancasi <fbrancasi@ethz.ch> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You deserve the authorship of this test 😁 |
||
|
|
||
|
|
||
| import pytest | ||
| import torch | ||
| import torch.nn as nn | ||
| import brevitas.nn as qnn | ||
| from brevitas.quant.scaled_int import ( | ||
| Int8ActPerTensorFloat, | ||
| Int32Bias, | ||
| Int8WeightPerChannelFloat, | ||
| ) | ||
| from DeepQuant.ExportBrevitas import exportBrevitas | ||
|
|
||
|
|
||
| class SimpleQuantCNN(nn.Module): | ||
| """ | ||
| A simple quantized CNN that includes: | ||
| - Input quantization | ||
| - Two QuantConv2d layers with Quantized ReLU | ||
| - MaxPool2d | ||
| - A final QuantLinear layer | ||
| """ | ||
|
|
||
| convAndLinQuantParams = { | ||
| "bias": True, | ||
| "weight_bit_width": 4, | ||
| "bias_quant": Int32Bias, | ||
| "input_quant": Int8ActPerTensorFloat, | ||
| "weight_quant": Int8WeightPerChannelFloat, | ||
| "output_quant": Int8ActPerTensorFloat, | ||
| "return_quant_tensor": True, | ||
| } | ||
|
|
||
| def __init__(self, in_channels: int = 1, num_classes: int = 10) -> None: | ||
| """ | ||
| Args: | ||
| in_channels: Number of input channels (e.g., 1 for grayscale). | ||
| num_classes: Number of output classes for the final linear layer. | ||
| """ | ||
| super().__init__() | ||
| self.inputQuant = qnn.QuantIdentity(return_quant_tensor=True) | ||
|
|
||
| self.conv1 = qnn.QuantConv2d( | ||
| in_channels=in_channels, | ||
| out_channels=16, | ||
| kernel_size=3, | ||
| padding=1, | ||
| **SimpleQuantCNN.convAndLinQuantParams | ||
| ) | ||
| self.relu1 = qnn.QuantReLU(bit_width=4, return_quant_tensor=True) | ||
| self.pool1 = nn.MaxPool2d(kernel_size=2) | ||
|
|
||
| self.conv2 = qnn.QuantConv2d( | ||
| in_channels=16, | ||
| out_channels=32, | ||
| kernel_size=3, | ||
| padding=1, | ||
| **SimpleQuantCNN.convAndLinQuantParams | ||
| ) | ||
| self.relu2 = qnn.QuantReLU(bit_width=4, return_quant_tensor=True) | ||
| self.pool2 = nn.MaxPool2d(kernel_size=2) | ||
|
|
||
| self.flatten = nn.Flatten() | ||
| self.fc = qnn.QuantLinear( | ||
| in_features=32 * 7 * 7, # If input is 28x28, shape after pooling is 7x7 | ||
| out_features=num_classes, | ||
| **SimpleQuantCNN.convAndLinQuantParams | ||
| ) | ||
|
|
||
| def forward(self, x: torch.Tensor) -> torch.Tensor: | ||
| """ | ||
| Forward pass of the SimpleQuantCNN. | ||
| Args: | ||
| x: Input tensor of shape [batch_size, in_channels, height, width]. | ||
| Returns: | ||
| A quantized output tensor (batch_size, num_classes). | ||
| """ | ||
| x = self.inputQuant(x) | ||
|
|
||
| x = self.conv1(x) | ||
| x = self.relu1(x) | ||
| x = self.pool1(x) | ||
|
|
||
| x = self.conv2(x) | ||
| x = self.relu2(x) | ||
| x = self.pool2(x) | ||
|
|
||
| x = self.flatten(x) | ||
| x = self.fc(x) | ||
| return x | ||
|
|
||
|
|
||
| @pytest.mark.ModelTests | ||
| def deepQuantTestSimpleCNN() -> None: | ||
|
|
||
| torch.manual_seed(42) | ||
|
|
||
| model = SimpleQuantCNN().eval() | ||
| sampleInput = torch.randn(1, 1, 28, 28) | ||
|
|
||
| exportBrevitas(model, sampleInput, debug=True) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could display a warning when an arg is None or non-node. I'm just scared that this change backfire at some point.