Designing strong baselines for ternary neural network quantization
  through support and mass equalization
Deep neural networks (DNNs) offer the highest performance in a wide range of
applications in computer vision. These results rely on over-parameterized
backbones, which are expensive to run. This computational burden can be
dramatically reduced by quantizing (in either data-free (DFQ), post-training
(PTQ) or quantization-aware training (QAT) scenarios) floating point values to
ternary values (2 bits, with each weight taking value in {-1,0,1}). In this
context, we observe that rounding to nearest minimizes the expected error given
a uniform distribution and thus does not account for the skewness and kurtosis
of the weight distribution, which strongly affects ternary quantization
performance. This raises the following question: shall one minimize the highest
or average quantization error? To answer this, we design two operators: TQuant
and MQuant that correspond to these respective minimization tasks. We show
experimentally that our approach allows to significantly improve the
performance of ternary quantization through a variety of scenarios in DFQ, PTQ
and QAT and give strong insights to pave the way for future research in deep
neural network quantization.
Edouard Yvinec, Arnaud Dapogny, Kevin Bailly
Designing Strong Baselines for Ternary Neural Network Quantization Through Support and Mass Equalization

###### Abstract

Deep neural networks (DNNs) offer the highest performance in a wide range of applications in computer vision. These results rely on over-parameterized backbones, which are expensive to run. This computational burden can be dramatically reduced by quantizing (in either data-free (DFQ), post-training (PTQ) or quantization-aware training (QAT) scenarios) floating point values to ternary values (2 bits, with each weight taking value in \(\{-1,0,1\}\)). In this context, we observe that rounding to nearest minimizes the expected error given a uniform distribution and thus does not account for the skewness and kurtosis of the weight distribution, which strongly affects ternary quantization performance. This raises the following question: shall one minimize the highest or average quantization error? To answer this, we design two operators: TQuant and MQuant that correspond to these respective minimization tasks. We show experimentally that our approach allows to significantly improve the performance of ternary quantization through a variety of scenarios in DFQ, PTQ and QAT and give strong insights to pave the way for future research in deep neural network quantization.

Edouard Yvinec\({}^{1,2}\), Arnaud Dapogny\({}^{2}\), Kevin Bailly\({}^{1,2}\)+ Sorbonne Universite\({}^{1}\), CNRS, ISIR, f-75005, 4 Place Jussieu 75005 Paris, France

Datakalab\({}^{2}\), 114 boulevard Malesherbes, 75017 Paris, France

Footnote †: This work has been supported by the french National Association for Research and Technology (ANRT), the company Datakalab (CIFRE convention C20/1396) and by the French National Agency (ANR) (FacIL, project ANR-17-CE33-0002). This work was granted access to the HPC resources of IDRIS under the allocation 2022-AD011013384 made by GENCI.

Quantization, Deep Learning, Computer Vision

## 1 Introduction

As the performance of deep neural network grows, so do their computational requirements: in computer vision, popular architectures such as ResNet [1], MobileNet V2 [2] and EfficientNet [3] rely on high expressivity from millions of parameters to effectively tackle challenging tasks such as classification [4], object detection [5] and segmentation [6].

In order to deploy these models using lower power consumption and less expensive hardware, many compression techniques have been developed to reduce the latency and memory footprint of large convolutional neural networks (CNN). Quantization is one of the most efficient of these techniques and consists in converting floating values with large bit-width to fixed point values encoded on lower bit-width. Quantization can be performed in three major contexts. First, data-free quantization (DFQ) [7, 8, 9, 10, 11], where, quantization is applied without data, hopefully not degrading the model accuracy. Second, post training quantization (PTQ) [12, 13, 14]: in this setup, one seeks to tune the quantization operator given an already trained model and a calibration set (usually a fraction of the training set). Third, quantization aware training (QAT) [15, 16, 17, 18, 19, 20, 21], in which, given a model and a training set, custom gradient descent proxies are usually implemented so as to circumvent zero gradients that stems from the rounding operation.

While noawadays most methods are successful in quantizing models to 8 or 4 bits while preserving the accuracy [22, 13, 23, 11], lower bit-widths remain an open challenge. In particular, ternary quantization, where weights are mapped to \(\{-1,0,1\}\)[24, 12] is an extreme form of neural network quantization, that comes with its specific challenges. While most quantization techniques rely on a rounding operation [22, 7, 8, 10], they struggle to preserve high accuracies when applied in the ternary quantization regime. In this work, we show that this limitation is at least partly due to the distribution of the weights: in fact, assuming a bell-shaped distribution [7, 25], the tails of the distribution, which represent very few parameters, will be quantized to non-zero values. Consequently, the naive rounding operation erases most of the learned operations which leads to huge accuracy drops. Based on this observation, we conclude that the naive quantization operator, that amounts to minimizing the expected error given a uniform distribution (round to nearest) is particularly ill-suited in ternary quantization. This leads to the following question: _shall one minimize the highest quantization error or the average quantization error on the weights?_ To answer this question we design two quantization operators: TQuant, which minimizes the highest quantization error, and MQuant, which minimizes the average quantization error assuming a bell shaped distribution. In Figure 1, we illustrate these two operators. We empirically show that these operators achieve strong improvements in a broad range of scenarios for DFQ,
PTQ and QAT.

## 2 Methodology

Let's consider \(F\) a network with \(L\) layers and weights \(W_{l}\) for each layer indexed by \(l\). We note \(Q\) the \(b\)-bits quantization operator that quantizes the weights \(W_{l}\) from \([\min\{W_{l}\};\max\{W_{l}\}]\subset\mathbb{R}\) to the quantization interval \([-2^{b-1};2^{b-1}-1]\cap\mathbb{Z}\) and \(W_{l}^{q}\) the quantized weights and is defined as:

\[(W^{q})=Q(W)=\left\lfloor\frac{W}{\lambda}\right\rfloor \tag{1}\]

where \(\lfloor\cdot\rceil\) denotes the rounding operation and \(\lambda\) is a row-wise rescaling factor selected such that each coordinate of \(W^{q}\) falls in the quantization interval, _i.e._\((\lambda)_{i}\) is the maximum between \(\frac{|\min\{(W)_{i}\}|}{2^{b-1}}\) and \(\frac{|\max\{(W)_{i}\}|}{2^{b-1}-1}\). When the number of bits \(b\) is large, e.g. \(b=4\) or \(b=8\), most of the scalar values \(w\) of \(W\) will not be quantized to the extreme values of the quantized interval \([-2^{b-1};2^{b-1}-1]\) and as such not considering the tail of the distribution does not affect performance. This is not true when \(b=2\) (ternary) as can be seen in Figure 1 (a). Only the tails of the distribution are quantized to non-zero values which represent very few values. To reduce this effect, we propose to quantization operators, namely TQuant (\(Q_{T}\)) and MQuant (\(Q_{M}\)), that either leads to minimize the highest scalar quantization error or to equalize the support of each ternary value, respectively.

### Support Equalization

We define the ternary quantization operator \(Q_{T}\) such that we have an equality of the measure of each pre-image of \(-1,0\) and \(1\), _i.e._

\[\begin{cases}Q_{T}^{-1}(\{-1\})=\left[-\lambda;-\frac{\lambda}{3}\right]\\ Q_{T}^{-1}(\{0\})=\left[-\frac{\lambda}{3};\frac{\lambda}{3}\right]\\ Q_{T}^{-1}(\{1\})=\left[\frac{1}{3};\lambda\right]\end{cases} \tag{2}\]

Consequently, all quantized values are mapped from ranges equal in size, a third of the original support. This is achieved by using two thirds of the scaling factor instead, in equation 1. Then, the maximal error between any scalar \(w\in W\) and the quantization \(Q^{-1}(Q(w))\) is \(\frac{\lambda}{3}\). This operator balances the support of the original weight values with the quantization space \(\{-1,0,1\}\). Thus \(Q_{T}\) minimises the maximal error per weight value. However it doesn't balance the mass. More formally the number of weight values assigned to \(-1\) is not necessarily equal to the number of values assigned to \(1\) nor \(0\). To achieve the mass equalization, we introduce MQuant.

### Mass Equalization

For each value \(v\in\{-1,0,1\}\) the spaces \(Q_{M}^{-1}(\{v\})\) have equal mass. This is achieved using \(\lambda\times\frac{5}{7\sqrt{2}}\) as a scaling factor.

**Theorem 1**.: _Under a centered Gaussian prior, the \(Q_{M}\) operator minimizes the expected error per scalar weight value._

We provide the proof in Appendix A. This theoretical result allows us to compare the behaviour of the error in ternary quantization. _Which is more important, minimizing the maximum error or the expected error from quantization?_ In what follows, we provide empirical answers to this question, and show that both operators lead to significantly more robust baselines for ternary quantization.

## 3 Experiments

We evaluate the proposed quantization operators in three contexts: quantization without data (data-free), quantization with a calibration set (PTQ) and quantization with full dataset (QAT). To provide insightful results for real world applications, we consider standard CNN backbones on ImageNet [4], such as ResNet 50 [1], MobileNet V2 [2] and Efficient-Net B0 [3]. In the context of tiny machine learning, we also considered ResNet 8 on Cifar10.

### Data-Free Quantization

For our evaluation in the context of data-free quantization, we compare our two operators TQuant and MQuant with the baseline SQuant [9] that achieves state-of-the-art accuracy in

Figure 1: Comparison of the ternary distribution (colored Dirac distributions) from different quantization operators: the naive quantization operator \(Q\), the mass balancing operator \(Q_{M}\) and the proposed ternary operator \(Q_{T}\).
data-free quantization. In a similar vein as in [10] we evaluate our approach using different expansions orders that allow to find several accuracy-computational burden trade-offs.

In Figure 2, we compare the influence of the quantization operator on the accuracy for ternary weight quantization. We observe across all three tested architectures that both MQuant and TQuant offer higher accuracies than SQuant by a significant margin. For instance, an expansion of order \(3\) with TQuant on ResNet 50 offers \(71.72\) points higher accuracy and MQuant offers \(58.83\) higher accuracy for an order 4 expansion. In parallel, TQuant reaches full-precision accuracy with half the expansion of SQuant and \(50\%\) less than MQuant which shows the better performance of the proposed method. This suggests that the proposed ternary-specific operators TQuand and MQuant enable overall stronger baselines than SQuant.

In Figure 3, we apply the expansion quantization to both weights and activations. We observe very similar results. For instance, on MobileNet V2 and EfficientNet B0, only TQuant reaches the full-precision accuracy in a reasonable expansion order. It is worth noting that MQuant is significantly less efficient on activation quantization due to the lack of data. Formally, the scaling factors are derived from the standard deviations stored in the batch normalization layers which give a much better estimation of the maximum values (TQuant) than the cdf (MQunat). In a data-driven context, MQuant would be very similar to the method applied in [26]. However, TQuant doesn't suffer from this limitation and vastly outperforms SQuant as a quantization operator for data-free quantization in ternary representation.

### Post-Training Quantization

We modified the two most popular methods: AdaRound [13] and BrecQ [14] to use with both MQuant and TQuant for weight ternary quantization (W2/A32). These techniques consist in learning, for each scalar value, whether it shall be rounded up or down. The procedure is agnostic to the scaling factor \(\lambda\). In our results, the rounding operation is determined by the PTQ method while the scales are given by the operator. In Table 1, we report our results on PTQ. First, we observe marginal slow down in terms of processing time when using our custom operators which is due to the added multiplicative term on the weight scaling. It is worth noting that the resulting quantized models will all share the exact same runtime. Second, we observe significant accuracy improvement when using TQuant on both PTQ methods, adding \(28.7\)% and \(32.22\)% accuracy with AdaRound and BrecQ respectively. Furthermore, it appears that MQuant even outperforms TQuant which suggests that when using a calibration sets, minimizing the average quantization error offers better performance than minimizing the highest quantization error.

### Quantization Aware Training

In QAT, the quantized weights and their scales are learned from scratch. In practice, the weights are kept in full-precision (float32) during the training process. The forward pass uses their quantized version while the backward updates the full-precision values. In our tests, the quantized values

\begin{table}
\begin{tabular}{|c|c|c|c|} \hline PTQ method & operator & accuracy & Processing Time \\ \hline - & - & 89.100 & - \\ \hline \multirow{3}{*}{AdaRound} & native & 11.790 \(\pm\) 3.210 & 5m01 \\  & MQuant & **42.910**\(\pm\) 0.620 & 5m18 \\  & TQuant & 40.490 \(\pm\) 0.250 & 5m18 \\ \hline \multirow{3}{*}{BrecQ} & native & 25.780 \(\pm\) 2.440 & 3m45 \\  & MQuant & **63.540**\(\pm\) 0.850 & 3m50 \\ \cline{1-1}  & TQuant & 58.000 \(\pm\) 1.120 & 3m50 \\ \hline \end{tabular}
\end{table}
Table 1: Accuracy results of a quantized (W2/A32) ResNet 8 for Cifar10. The process is performed on a RTX 2070. We report the quantization pre-processing time in minutes (m).

\begin{table}
\begin{tabular}{|c|c|c|c|} \hline  & Baseline & MQuant & TQuant \\ \hline accuracy & 42.910 \(\pm\) 14.61 & 68.250 \(\pm\) 6.26 & **82.620**\(\pm\) 2.43 \\ \hline \end{tabular}
\end{table}
Table 2: Accuracy results of ResNet 8 for Cifar10, quantized in W2/A4 using straight through estimation [27].

Figure 3: Comparison between the proposed TQuant, MQuant and the state-of-the-art SQuant [9] operator for ternary weights and activation expansion (W2/A2).

Figure 2: Comparison between the proposed TQuant, MQuant and the state-of-the-art SQuant [9] operator for ternary weights expansion and fixed 8 bits activations (W2/A8). The ImageNet top1 accuracy is reported for ResNet 50, MobileNet V2 and EfficientNet B0.
applied in the forward passes are derived using the naive rounding operator (eq 1) and scales from the given operator. In Table 2, we report our results for ResNet 8 on Cifar10. We observe that both TQuant and MQuant improve the accuracy of the resulting model with TQuant almost enabling full-precision accuracy. TQuant also stabilizes the result as the variance on the accuracy drops from the original \(14.610\)% down to \(2.429\)%. We conclude that, in QAT, minimizing the highest quantization error provides the best results for ternary quantization.

From all our experiments in different quantization context, we deduce several insights on ternary quantization:

* when _no data_ is available, we should minimize the _maximum_ quantization error (TQuant),
* when _few data_ are available, we should minimize the _average_ quantization error (MQaut),
* when _all the data_ is available, we should minimize the _maximum_ quantization error (TQuant).

## 4 Conclusion

In this work, we investigated ternary quantization of deep neural networks. This very low bit representation offers massive inference speed-ups and energy consumption reduction enabling deployment on edge devices of large neural networks but at the cost of the accuracy. We highlight that part of the accuracy degradation arises from the rounding operation itself which is unsuitable for preserving the predictive function. To tackle this limitation, we propose two quantization operator, namely TQuant and MQuant, that respectively minimize the highest and average quantization errors assuming the correct empirical prior. Extensive testing in data-free quantization, post-training quantization and quantization-aware training contexts show that the proposed ideas, albeit simple, leads to designing stronger baselines for ternary deep neural network compression as compared with existing state-of-the-art methods.

## Appendix A Proof of Theorem 1

Proof.: Let \(w\) be a scalar value from a symmetric distribution \(W\) with support \(\left.\right]-\lambda;\lambda\right|\). A quantization operator \(Q_{a}^{q}\) is defined by two parameters \(a\) and \(q\) such that

\[Q_{a}^{q}:w\mapsto\begin{cases}q&\text{if }w\geq a\\ 0&\text{if }w\in\left.\right]-a;a[\\ -q&\text{otherwise}\end{cases} \tag{3}\]

We want to solve the following minimization problem:

\[\min_{a,q}\mathbb{E}\left[\left\|Q_{a}^{q}(w)-w\right\|\right] \tag{4}\]

We develop the expression \(\mathbb{E}\left[\left\|Q_{a}^{q}(w)-w\right\|\right]\) in a sum of integrals

\[\mathbb{E}\left[\left\|Q_{a}^{q}(w)-w\right\|\right]= \int_{-\lambda}^{-a}\left\|w+q\right\|d\mathbb{P}(w) \tag{5}\] \[+\int_{-a}^{a}\left\|w\right\|d\mathbb{P}(w)+\int_{a}^{\lambda} \left\|w-q\right\|d\mathbb{P}(w)\]

Assuming, \(\left\|\dot{\parallel}\right.\) is the quadratic error, then \(\int_{a}^{\lambda}\left\|w-q\right\|d\mathbb{P}(w)\) is minimized by \(q=\mathbb{E}\mathbbm{1}[\left.{}_{-\lambda;-a}\right]w]\). By hypothesis, the distribution is symmetrical, _i.e._\(\mathbb{P}(w)\) is even. Therefore \(\mathbb{E}[\mathbbm{1}_{\left[-\lambda;-a\right]w}]=-\mathbb{E}[\mathbbm{1}_{ \left[a;\lambda\right]}w]\) and

\[\mathbb{E}\left[\left\|Q_{a}^{q}(w)-w\right\|\right]=\mathbb{V}[\mathbbm{1}_ {\left[-a;a\right]w}]+2\mathbb{V}[\mathbbm{1}_{\left[a;\lambda\right]}w] \tag{6}\]

Assuming a Gaussian prior over \(W\) (centered and reduced for the sake of simplicity), the variance terms correspond to variances of truncated Gaussian distribution [28]:

\[\begin{cases}\mathbb{V}[\mathbbm{1}_{\left[-a;a\right]}w]=1-\frac{2a\phi(a)}{ 2\Phi(a)-1}\\ \mathbb{V}[\mathbbm{1}_{\left[a;\lambda\right]}w]=1-\frac{\lambda\phi(\lambda) -a\phi(a)}{\Phi(\lambda)-\Phi(a)}-\left(\frac{\phi(\lambda)-\phi(a)}{\Phi( \lambda)-\Phi(a)}\right)^{2}\end{cases} \tag{7}\]

where \(\phi\) is the density function of the Gaussian distribution and \(\Phi\) the cumulative distribution function. To solve the minimization problem, we search for the critical points in \(a\). We recall that \(\phi^{\prime}(a)=-2a\phi(a)\) and \(\Phi^{\prime}(a)=\phi(a)\).

\[\begin{split}\frac{\partial\mathbb{E}\left[\left\|Q_{a}^{q}(w)- w\right\|\right]}{\partial a}&=2\phi(a)\frac{(2a^{2}+1)(2\Phi(a)-1)-2a\phi(a)}{ (2\Phi(a)-1)^{2}}\\ &+\phi(a)\frac{\lambda\phi(\lambda)-a\phi(a)-(2a^{2}+1)(\Phi( \lambda)-\Phi(a))}{(\Phi(\lambda)-\Phi(a))^{2}}\\ &+\phi(a)\frac{\phi(\lambda)-\phi(a)-2a(\Phi(\lambda)-\Phi(a))}{( \Phi(\lambda)-\Phi(a))^{2}}\frac{\phi(\lambda)-\phi(a)}{\Phi(\lambda)-\Phi(a)} \end{split} \tag{8}\]

To solve \(\frac{\partial\mathbb{E}[\left\|Q_{a}^{q}(w)-w\right\|]}{\partial a}=0\) for \(a\), we simplify the equation to get

\[\begin{split} 2\frac{(2a^{2}+1)(2\Phi(a)-1)-2a\phi(a)}{(2\Phi(a)-1)^{2 }}\left(\Phi(\lambda)-\Phi(a)\right)^{2}-\frac{(\phi(\lambda)-\phi(a))^{2}}{ \Phi(\lambda)-\Phi(a)}\\ =(\lambda-2a)\phi(\lambda)+a\phi(a)-(2a^{2}+1)(\Phi(\lambda)-\Phi( a))\end{split} \tag{9}\]

To solve this equation, we assume that \(\Phi(\lambda)-\Phi(a)\approx\Phi(-a)\) and \(\phi(\lambda)\approx 0\). Consequently, we obtain the second order polynomial: \(2a^{2}+1-5a\phi(a)-3\phi(a)^{2}=0\). We deduce, by solving the polynomial in \(a\) and in \(\phi(a)\):

\[\begin{cases}a=\frac{1}{4}\left(\sqrt{49\phi(a)^{2}-8}+5\phi(a)\right)\\ \phi(a)=\frac{1}{6}\left(\sqrt{12+49a^{2}}-5a\right)\end{cases} \tag{10}\]

We find \(a=\frac{5}{7\sqrt{2}}\) and as such \(\Phi^{-1}(a)\approx\frac{2}{3}\) which corresponds to the definition of \(Q_{M}\). 
