Skip to content

Exporting weights and biases error #2

@ERYAGNIK003

Description

@ERYAGNIK003

I am creating a LeNet architecture using brevitas to be deployed on an edge device by exporting weights and biases in C code. I have defined the model as below ,

class BrevitasLeNetQAT(nn.Module):
    def __init__(self, num_classes=10, in_channels=1, weight_bits=8, act_bits=8):
        super().__init__()
        wq = Int8WeightPerTensorFixedPoint
        aq = Int8ActPerTensorFixedPoint
        bq = Int32Bias
        self.quant_inp = QuantIdentity(bit_width=act_bits, act_quant=aq,return_quant_tensor=True)
        self.conv1 = QuantConv2d(in_channels, 6, 5, stride=1, padding=0,
                                 weight_bit_width=weight_bits, weight_quant=wq,
                                 bias=True, bias_quant=bq, cache_inference_quant_bias=True)
        self.relu1 = QuantReLU(bit_width=act_bits, act_quant=aq,return_quant_tensor=True)
        self.pool1 = nn.MaxPool2d(2, 2)
        self.conv2 = QuantConv2d(6, 16, 5, stride=1, padding=0,
                                 weight_bit_width=weight_bits, weight_quant=wq,
                                 bias=True, bias_quant=bq, cache_inference_quant_bias=True)
        self.relu2 = QuantReLU(bit_width=act_bits, act_quant=aq,return_quant_tensor=True)
        self.pool2 = nn.MaxPool2d(2, 2)
        self.fc1 = QuantLinear(16 * 4 * 4, 120, weight_bit_width=weight_bits, weight_quant=wq,
                               bias=True, bias_quant=bq, cache_inference_quant_bias=True)
        self.relu3 = QuantReLU(bit_width=act_bits, act_quant=aq,return_quant_tensor=True)
        self.fc2 = QuantLinear(120, 84, weight_bit_width=weight_bits, weight_quant=wq,
                               bias=True, bias_quant=bq, cache_inference_quant_bias=True)
        self.relu4 = QuantReLU(bit_width=act_bits, act_quant=aq,return_quant_tensor=True)
        self.fc3 = QuantLinear(84, num_classes, weight_bit_width=weight_bits, weight_quant=wq,
                               bias=True, bias_quant=bq, cache_inference_quant_bias=True)

    def forward(self, x):
        x = self.quant_inp(x)
        x = self.pool1(self.relu1(self.conv1(x)))
        x = self.pool2(self.relu2(self.conv2(x)))
        x = x.view(x.size(0), -1)
        x = self.relu3(self.fc1(x))
        x = self.relu4(self.fc2(x))
        x = self.fc3(x)
        return x

Training is completed on MNIST dataset without any errors. But, when I extract those quantised biases using below code , it is throwing an error of "RuntimeError: Input scale required with Int32Bias"

if module.bias is not None and hasattr(module, "quant_bias"):
        b_quant = module.quant_bias()
        # Ensure it's flattened and converted to the appropriate type
        b_int32 = b_quant.int().detach().cpu().numpy().flatten().astype(np.int32)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions