Skip to content

Commit be6ec27

Browse files
Codebase Refactor
1 parent c0fdc13 commit be6ec27

File tree

10 files changed

+148
-281
lines changed

10 files changed

+148
-281
lines changed

DeepQuant/Export.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ def exportQuantModel(
299299
# f=EXPORT_FOLDER / "4_model_dequant_moved.onnx",
300300
f=onnxFile,
301301
opset_version=13,
302-
keep_initializers_as_inputs=True,
302+
keep_initializers_as_inputs=False, # FBRANCASI: This prevent the onnx warnings
303303
do_constant_folding=False,
304304
input_names=["input"],
305305
output_names=["output"],

Tests/TestConv.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
# Victor Jung <[email protected]>
66
# Federico Brancasi <[email protected]>
77

8-
98
import pytest
109
import torch
1110
import torch.nn as nn
@@ -19,8 +18,9 @@
1918

2019

2120
class QuantConvNet(nn.Module):
21+
"""Simple quantized CNN with a single conv layer."""
2222

23-
convAndLinQuantParams = {
23+
convQuantParams = {
2424
"bias": True,
2525
"weight_bit_width": 4,
2626
"bias_quant": Int32Bias,
@@ -30,31 +30,26 @@ class QuantConvNet(nn.Module):
3030
"return_quant_tensor": True,
3131
}
3232

33-
def __init__(self, in_channels: int = 1) -> None:
33+
def __init__(self, inChannels: int = 1) -> None:
3434
super().__init__()
3535
self.inputQuant = qnn.QuantIdentity(return_quant_tensor=True)
36-
3736
self.conv1 = qnn.QuantConv2d(
38-
in_channels=in_channels,
37+
in_channels=inChannels,
3938
out_channels=16,
4039
kernel_size=3,
4140
padding=1,
42-
**QuantConvNet.convAndLinQuantParams,
41+
**QuantConvNet.convQuantParams,
4342
)
4443

4544
def forward(self, x: torch.Tensor) -> torch.Tensor:
46-
4745
x = self.inputQuant(x)
4846
x = self.conv1(x)
49-
5047
return x
5148

5249

5350
@pytest.mark.SingleLayerTests
5451
def deepQuantTestConv() -> None:
55-
5652
torch.manual_seed(42)
57-
5853
model = QuantConvNet().eval()
5954
sampleInput = torch.randn(1, 1, 28, 28)
6055
exportQuantModel(model, sampleInput, debug=True)

Tests/TestLinear.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@
44
#
55
# Federico Brancasi <[email protected]>
66

7-
87
import pytest
9-
10-
### PyTorch Imports ###
118
import torch
129
import torch.nn as nn
13-
14-
### Brevitas Import ###
1510
import brevitas.nn as qnn
1611
from brevitas.quant.scaled_int import (
1712
Int8ActPerTensorFloat,
@@ -22,15 +17,14 @@
2217

2318

2419
class QuantLinearNet(nn.Module):
20+
"""Simple quantized network with a single linear layer."""
2521

26-
def __init__(self, in_features: int = 16, hidden_features: int = 32) -> None:
22+
def __init__(self, inFeatures: int = 16, hiddenFeatures: int = 32) -> None:
2723
super().__init__()
28-
2924
self.inputQuant = qnn.QuantIdentity(return_quant_tensor=True)
30-
3125
self.linear1 = qnn.QuantLinear(
32-
in_features=in_features,
33-
out_features=hidden_features,
26+
in_features=inFeatures,
27+
out_features=hiddenFeatures,
3428
bias=True,
3529
weight_bit_width=4,
3630
bias_quant=Int32Bias,
@@ -41,19 +35,14 @@ def __init__(self, in_features: int = 16, hidden_features: int = 32) -> None:
4135
)
4236

4337
def forward(self, x: torch.Tensor) -> torch.Tensor:
44-
4538
x = self.inputQuant(x)
4639
x = self.linear1(x)
47-
4840
return x
4941

5042

5143
@pytest.mark.SingleLayerTests
5244
def deepQuantTestLinear() -> None:
53-
5445
torch.manual_seed(42)
55-
5646
model = QuantLinearNet().eval()
5747
sampleInput = torch.randn(1, 4, 16)
58-
5948
exportQuantModel(model, sampleInput, debug=True)

Tests/TestMHSA.py

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#
55
# Federico Brancasi <[email protected]>
66

7-
87
import pytest
98
import torch
109
import torch.nn as nn
@@ -21,22 +20,18 @@
2120

2221

2322
class QuantMHSANet(nn.Module):
23+
"""Simple quantized network with multi-head self-attention."""
2424

25-
def __init__(self, embed_dim: int, num_heads: int) -> None:
26-
"""
27-
Args:
28-
embed_dim: The dimension of each embedding vector.
29-
num_heads: The number of attention heads.
30-
"""
25+
def __init__(self, embedDim: int, numHeads: int) -> None:
3126
super().__init__()
3227
self.inputQuant = qnn.QuantIdentity(return_quant_tensor=True)
3328
self.mha = qnn.QuantMultiheadAttention(
34-
embed_dim=embed_dim,
35-
num_heads=num_heads,
29+
embed_dim=embedDim,
30+
num_heads=numHeads,
3631
dropout=0.0,
3732
bias=True,
38-
packed_in_proj=False, # separate Q, K, V
39-
batch_first=False, # expects (sequence, batch, embed_dim)
33+
packed_in_proj=False, # FBRANCASI: separate Q, K, V
34+
batch_first=False, # FBRANCASI: expects (sequence, batch, embed_dim)
4035
in_proj_input_quant=Int8ActPerTensorFloat,
4136
in_proj_weight_quant=Int8WeightPerTensorFloat,
4237
in_proj_bias_quant=Int32Bias,
@@ -51,27 +46,14 @@ def __init__(self, embed_dim: int, num_heads: int) -> None:
5146
)
5247

5348
def forward(self, x: Tensor) -> Tensor:
54-
"""
55-
Forward pass that first quantizes the input, then applies multi-head attention.
56-
57-
Args:
58-
x: Input tensor of shape [sequence_len, batch_size, embed_dim].
59-
60-
Returns:
61-
A tuple (output, None) as per the Brevitas MHA API, where output has shape
62-
[sequence_len, batch_size, embed_dim].
63-
"""
6449
x = self.inputQuant(x)
6550
out = self.mha(x, x, x)
6651
return out
6752

6853

6954
@pytest.mark.SingleLayerTests
7055
def deepQuantTestMHSA() -> None:
71-
7256
torch.manual_seed(42)
73-
74-
model = QuantMHSANet(embed_dim=16, num_heads=4).eval()
57+
model = QuantMHSANet(embedDim=16, numHeads=4).eval()
7558
sampleInput = torch.randn(10, 2, 16)
76-
7759
exportQuantModel(model, sampleInput)

Tests/TestMobileNetV3Small.py

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
33
# SPDX-License-Identifier: Apache-2.0
44
#
5-
# Victor Juing <[email protected]>
5+
# Victor Jung <[email protected]>
66

77
import pytest
88
import torch
@@ -23,18 +23,10 @@
2323

2424

2525
def prepareMBNetV3Model() -> nn.Module:
26-
"""
27-
Prepare a quantized MobileNetV3Small model for testing.
28-
Steps:
29-
1) Load the torchvision MobileNetV3Small.
30-
2) Convert it to eval mode.
31-
3) Preprocess and adapt average pooling.
32-
4) Quantize it using Brevitas.
33-
34-
Returns:
35-
A quantized MobileNetV3Small model ready for export tests.
36-
"""
37-
baseModel = models.mobilenet_v3_small(weights=models.MobileNet_V3_Small_Weights.IMAGENET1K_V1)
26+
"""Prepare a quantized MobileNetV3Small model for testing."""
27+
baseModel = models.mobilenet_v3_small(
28+
weights=models.MobileNet_V3_Small_Weights.IMAGENET1K_V1
29+
)
3830
baseModel = baseModel.eval()
3931

4032
computeLayerMap = {
@@ -99,9 +91,7 @@ def prepareMBNetV3Model() -> nn.Module:
9991
baseModel = preprocess_for_quantize(
10092
baseModel, equalize_iters=20, equalize_scale_computation="range"
10193
)
102-
baseModel = AdaptiveAvgPoolToAvgPool().apply(
103-
baseModel, torch.ones(1, 3, 224, 224)
104-
)
94+
baseModel = AdaptiveAvgPoolToAvgPool().apply(baseModel, torch.ones(1, 3, 224, 224))
10595

10696
quantizedModel = quantize(
10797
graph_model=baseModel,
@@ -115,10 +105,7 @@ def prepareMBNetV3Model() -> nn.Module:
115105

116106
@pytest.mark.ModelTests
117107
def deepQuantTestMobileNetV3Small() -> None:
118-
119108
torch.manual_seed(42)
120-
121-
quantizedModel = prepareMBNetV3Model()
109+
model = prepareMBNetV3Model()
122110
sampleInput = torch.randn(1, 3, 224, 224)
123-
124-
exportQuantModel(quantizedModel, sampleInput, debug=True)
111+
exportQuantModel(model, sampleInput, debug=True)

0 commit comments

Comments
 (0)