Make RepVGG Greater Again: A Quantization-Aware Approach
- Publication
- AAAI 2024Conference
Cite this paper · BibTeX
The citation identifies the linked source version. It cites the original paper and its authors.
@inproceedings{reparameterizationqarepvgg2024,
title = {{Make RepVGG Greater Again: A Quantization-Aware Approach}},
author = {Xiangxiang Chu and Liang Li and Bo Zhang},
year = {2024},
booktitle = {AAAI},
url = {https://doi.org/10.1609/aaai.v38i10.29045},
doi = {10.1609/aaai.v38i10.29045}
}
RepVGG leverages an over-parameterized network with multiple branches during the training phase, and merges these branches into a single one during inference, a process termed "re-parameterization."
Defining the Problem
This "re-parameterization" design is widely used as a fundamental component in many scenarios, covering both low-level and high-level visual tasks.
Recent popular object detection methods like YOLOv6 and YOLOv7 are built on such components.
However...
RepVGG cannot be quantized!
The heading describes a severe accuracy collapse, not a mathematical impossibility of quantizing RepVGG. The results concern the tested INT8 setup, not every quantization method.
Quantization is an effective model compression method that maps network weights and input data to lower precision (usually INT8) for faster computation, significantly enhancing computational efficiency.
- Post-training quantization (PTQ) is the most common scheme, requiring only a few batches of images to calibrate quantization parameters without additional training.
- Quantization-aware training (QAT) methods are also proposed to improve quantization accuracy. QAT usually involves intrusively modifying training code and requires additional costs, so it is typically used only when PTQ does not yield satisfactory results and when training code is available.
There is a trade-off: quantization typically results in some loss of accuracy, so it's used only if it can be applied without significantly impacting performance for deployment tasks.
- But RepVGG loses about 20 percentage points of accuracy after quantization!
This makes it completely impractical!
Why?
The authors first evaluate the performance of several RepVGG models using the official code. As shown in the table above, after standard PTQ (post-training quantization), RepVGG-A0's Top-1 accuracy on the ImageNet validation dataset drops significantly from 72.4% to 52.2%, a total decrease of 20.2 percentage points!
Quantization of a tensor is typically represented as:
This simplified expression omits the zero-point and illustrates symmetric quantization. rounds to the nearest integer; it is not the ceiling function, and truncates values beyond the quantization range. is a scaling factor that maps the tensor to a given range, defined as:
where and are boundary values chosen to better represent the distribution of . If there is no clipping error and rounding error is uniform over one quantization interval, its variance is approximately . Narrowing the range reduces rounding error but can increase clipping error, so the two must be balanced.
In practice, these values are chosen in various ways. Sometimes the maximum and minimum values are used directly, such as for weight quantization; sometimes they are chosen by optimizing the quantization error's MSE or entropy, typically for activation quantization. The quality of the selection depends on many factors, such as the tensor's variance and the presence of outliers.
For neural networks, two main components need quantization and can lead to accuracy degradation: weights and activations. Activations also serve as inputs to the next layer, so errors accumulate layer by layer. Therefore, good quantization performance of a neural network primarily requires two basic conditions:
- : The weight distribution is quantization-friendly and feasible in range.
- : The activation distribution (i.e., the model's response to input features) is also quantization-friendly.
Empirically, we define quantization-friendly weight or activation distributions as those with small variance and few outliers. Violating either condition results in poor quantization performance.
The authors then show the weight distribution of the RepVGG-A0 model:
The figure above shows the weight distribution for each layer and the corresponding standard deviation.
Observation reveals that these weights are concentrated around zero, with no significant outliers, satisfying condition .
This prompts the authors to further verify condition by examining whether the activation values significantly worsen the quantization effect.
Unfortunately, activation values depend on the input and are coupled with learned weights.
Thus, the authors do not make any assumptions about the distribution of weights or activations and analyze the biases of each branch.
Regularization Amplifies Activation Variance
Before further discussion, let's introduce the computation operations in a typical RepVGG block.
Here, the same naming conventions as RepVGG are used for better understanding.
Specifically, denotes the kernel of the convolution, where and are the numbers of input and output channels, respectively. For RepVGG, . For the BN layer after the convolution, , , , and represent the mean, standard deviation, scaling factor, and bias, respectively. For the BN in the Identity branch, , , , are used.
Let and represent the input and output, respectively, with denoting convolution operation.
Let , , and be the outputs of the Identity, , and branches, respectively.
For a block with an identity branch, restrict the analysis to stride 1 and , , and .
Here is the branch sum before ReLU:
Next, the BN operation of the branch can be written as:
where denotes element-wise multiplication, and is a small value (default ) to ensure numerical stability. This means BN changes the statistics (mean and variance) of its input.
A shift does not change variance, but the mean can still affect clipping and quantization error under fixed-range or symmetric quantization. From a probabilistic perspective, given a random variable and scalar , the variance of , , equals .
Let , then:
Here, the value of controls the expansion or reduction of 's variance, leading to either better or worse quantization performance.
Similar conclusions can be drawn for the and Identity branches.
Based on the above analysis, the authors delve into the details of RepVGG:
A crucial yet often overlooked component is a specially designed custom weight decay that enhances accuracy and facilitates quantization by regularizing the multi-branch weights, similar to regularizing their equivalent fused convolution kernels.
This may lead to quantization-friendly fused weights.
The official get_custom_L2 code uses the following center-kernel term. The complete loss also includes ordinary on the outer ring of the 3×3 kernel:
The paper associates this normalization with enlarged activation variance. However, the official code detaches and : this loss does not backpropagate through the denominator into the BN scale parameters. The proposed explanation is therefore not a direct gradient mechanism proved by this expression. The ablation does show that ordinary improves accuracy under the tested PTQ setup.
To address the issue of increased variance, a simple and straightforward approach is to remove the denominator from the above formula, resulting in:
The results are shown in the table below:
Removing the denominator term yields an FP32 accuracy of 71.5%, which is 0.7 percentage points below the reproduced 72.2% baseline.
However, the quantization performance improves significantly to 61.2%. Still, this method requires inconvenient equivalent transformations.
Another promising approach is to directly apply ordinary .
The results showed that, besides simplicity, ordinary achieved slightly better results than the preceding variant.
Thus, this method is chosen as the default implementation (M1).
Uncontrollable Outlier Weights
Ordinary lowers FP32 accuracy by 0.5 percentage points but raises INT8 accuracy by 11.3 points relative to the reproduced baseline. A substantial gap remains. With the special regularizer removed, the authors next inspect the weight distribution.
First, we can derive the fused weights as:
where is used to match the shape of the convolution kernel.
In this architecture, and are trainable parameters, while is a fixed identity matrix that does not decay during training. The scalars and depend on the output of the convolutional layers in the and branches, respectively.
However, depends directly on the distribution of the preceding block’s post-ReLU output, and the Identity branch is special because the activations pass through the ReLU layer before entering the BatchNorm layer.
This operation is very dangerous because if a channel is entirely inactive (i.e., contains only zeros), it generates a very small and a large scale factor . This issue is common in networks widely using ReLU.
If this happens, the outlier scale factor can dominate the distribution of the fused convolution kernels, significantly affecting their quantization preference.
This explains the poor quantization performance due to the violation of , leading to severe quantization errors.
The authors further display the of the three branches, as shown below.
In Layers 5 and 6, the maximum from the Identity branch is 692.1107 and 1477.3732, respectively.
The authors repeated the experiment three times, and this phenomenon recurred.
Solving the Problem
Based on ordinary loss, the authors address the issues by altering the re-parameterization structure.
The final design removes BN from the Identity and branches and adds an extra BN after the sum of the three branches. The authors first remove only the Identity branch's BN (M2) to address the uncontrollable outlier weights and satisfy condition .
They call the combination of M1 and M2 S2; its results appear in the third row of the table below. Removing the BN and adding post-sum BN come later as M3 and M4.
Error analysis of weight quantization indicates that this design indeed meets condition . The FP32 accuracy of this model is relatively low at 70.7%, with an INT8 accuracy of 62.5%, still impractical.
This prompted the authors to verify if it violated condition . The authors observe enlarged variance after summing the branches. The variance also depends on covariance: .
This phenomenon frequently occurs in RepVGG's design. Since ReLU is the activation function in RepVGG, if most inputs are below zero (dead neurons), it's harmful. Conversely, if all inputs are above zero, it's also undesirable due to losing non-linearity. Empirically, many modern high-performance CNN models have zero mean before ReLU.
Assuming this, we have:
If the and branches have the same mean, we get:
Here because S2 removes identity-branch BN and its input follows the preceding ReLU. The mean identity follows from . Variance enlargement additionally requires covariance analysis or empirical evidence:
The plotted distributions illustrate the issue in the tested model; it cannot be deduced from equal means alone.
To better control variance, several simple methods have potential, as shown in the table below:
The authors remove BN from the branch (M3) because it has the best INT8 accuracy among the variants in this comparison. They call the combination of M1, M2 and M3 S3. This design achieves 70.1% Top-1 FP32 accuracy and 69.5% INT8 accuracy on ImageNet, significantly improving quantization performance.
However, its FP32 accuracy is still low.
To address the distribution changes after branch summation, the authors add an additional batch normalization (M4) after summing the three branches to stabilize training, naming the combination of M1 through M4 S4.
The post-BN does not affect the equivalent convolution kernel fusion during deployment. This further boosts the A0 model's Top-1 FP32 accuracy on ImageNet from 70.1% to 72.2%. Additionally, its INT8 accuracy increases to 70.4%.
In summary, combining the four modifications above (from M1 to M4) ultimately forms QARepVGG, maintaining RepVGG's excellent FP32 performance while significantly surpassing RepVGG in INT8 performance.
Discussion
Performance on ImageNet
The research shows that RepVGG's INT8 accuracy is severely impacted, far below FP32.
For example, RepVGG-B0's Top-1 accuracy drops from 75.1% to 40.2%. In contrast, the FP32-to-INT8 accuracy gaps for the QARepVGG models in the table are all within 2 percentage points.
Notably, RepVGG with grouped convolutions performs particularly poorly. After PTQ, RepVGG-B2g4's accuracy drops from 78.5% to 13.7%, a decrease of 64.8 percentage points. In contrast, QARepVGG-B2g4's accuracy drops by only 0.7 percentage points, showing more stable quantization in these tested model configurations.
In comparison with RepOpt-VGG, which uses gradient re-parameterization, its quantization accuracy is highly sensitive to scale search quality. With only partial pre-trained models released, the authors re-trained RepOpt-VGG-A0/B0 models.
Results show that RepOpt-VGG-A0 achieves 70.3% FP32 accuracy on ImageNet, 2.1 percentage points below official RepVGG-A0. Although its quantization performance improves over original RepVGG, the table's PTQ accuracy remains below the corresponding QARepVGG models. The authors' method also avoids the additional 240 CIFAR-100 training epochs used for RepOpt-VGG's scale search.
Finally, the authors compared the two architectures under QAT using the NVIDIA quantization toolkit. QARepVGG+QAT has 1.0 to 5.6 percentage points higher INT8 accuracy than RepVGG+QAT across the three models in the table. The QAT stage runs for 10 epochs, with the first three used for warm-up and an initial learning rate of 0.01.
Conclusion
Through theoretical and quantitative analysis, this study delves into the failure reasons for quantizing the famous re-parameterization-based structure RepVGG. The study finds that the original structure and training setup amplify quantization errors in the tested PTQ experiments and accumulate to produce poor results. To address this issue, the research team redesigned the structure, introducing QARepVGG. This improved design yields quantization-friendly weights and activation distributions.
QARepVGG maintains the excellent FP32 performance of RepVGG while greatly simplifying the final deployment quantization process. This study emphasizes the importance of quantization awareness in architecture design to improve overall model performance. Through this design, QARepVGG demonstrates superior quantization performance, providing valuable insights for future model designs.