SlowGuess/NeurIPS_2023_OCR huggingface dataset:
Andrey Kuzmin, Markus Nagel, Mart van Baalen, Arash Behboodi, Tijmen Blankevoort
Qualcomm AI Research*
{akuzmin, markusn, mart, behboodi, tijmen}@qti=qualcomm.com
Neural network pruning and quantization techniques are almost as old as neural networks themselves. However, to date only ad-hoc comparisons between the two have been published. In this paper, we set out to answer the question on which is better: neural network quantization or pruning? By answering this question, we hope to inform design decisions made on neural network hardware going forward. We provide an extensive comparison between the two techniques for compressing deep neural networks. First, we give an analytical comparison of expected quantization and pruning error for general data distributions. Then, we provide lower bounds for the per-layer pruning and quantization error in trained networks, and compare these to empirical error after optimization. Finally, we provide an extensive experimental comparison for training 9 large-scale models on 4 tasks. Our results show that in most cases quantization outperforms pruning. Only in some scenarios with very high compression ratio, pruning might be beneficial from an accuracy standpoint.<sup>1</sup>
Recent advances in deep learning led to exceeding human-level performance in many tasks, including computer vision, machine translation, voice recognition, and language understanding. Real-world applications of DNNs rely heavily on their efficiency. Both mobile and cloud platforms greatly benefit from reduced latency and energy efficiency achieved by some form of model compression. In this work, we consider two mainstream techniques used in practice; pruning and quantization.
Pruning methods remove individual weights [70, 25], or sometimes groups of weights [28, 47]. This procedure can reduce the memory footprint. Furthermore, not having to perform the computations with weights that are zeroed out can make network inference more efficient. On the other hand, quantization reduces the bit-width used for both the weights and the computation used in networks, leading to both predictable memory savings and reductions in the necessary compute. In both scenarios, the hardware used for making use of these optimization schemes needs to take them into account.
Depending on the availability of training data and computing budget, most methods for pruning and quantization fall into one of two families. The first family includes fine-tuning approaches, namely quantization-aware training (QAT) and fine-tuning with pruning in the loop. The second family includes post-training approaches such as post-training quantization (PTQ). Previously, pruning techniques primarily relied on fine-tuning; however, some post-training pruning methods appeared recently as fine-tuning is not desirable for large language models [18].
Despite the importance of model efficiency and the plethora of approaches for pruning and quantization, the two fields are mostly disjoint. The literature presents little insight into which of the two
* Qualcomm AI Research is an initiative of Qualcomm Technologies, Inc.
$^{1}$ Code is available at https://github.com/Qualcomm-AI-research/pruning-vs-quantization
Figure 1: Comparison for a standard normal distribution. (left) Distributions after pruning and quantization for INT4 and  $75\%$  pruning. (middle) The squared error weighted by probability. (right) SNR for different compression ratios.
techniques is more accurate. In practice, there is only limited time to compress a network and limited energy to spend on making deep learning inference hardware. For this reason, we ask the question: Should one focus on quantization or pruning for compression?
We present an extensive study comparing pruning and quantization in equal settings. First, we consider different data distributions and analyze the conditions under which each method is preferable. We match our findings with real weight tensors from pre-trained models. Second, we consider a posttraining scenario and evaluate single-layer output errors for both methods. Because the comparison might depend on the specific choice of optimization method, we compare the two with theoretical bounds that apply regardless of the optimization method. Finally, we provide a full-model comparison for the most common scenario of fine-tuning networks after either pruning or quantization.
In our comparison, we intentionally avoid considering the hardware aspects of pruning and quantization. Instead, we focus solely on the accuracy of both methods, given similar theoretical compression ratios. A coarse discussion on the hardware necessary for both methods can be found in section 6.
2 Assumptions
In our work, we assume FP16 as the basic data type and measure any gains in compression with respect to it. Using FP16 for inference generally does not lead to a loss in accuracy. Neural networks are also very commonly trained with FP16, making it a common baseline. Thus, we compare  $50\%$  pruning sparsity to INT8 quantization,  $75\%$  sparsity to INT4 quantization and so forth. We also assume no overhead on storing the sparsity mask for pruning and relegate such hardware-specific implementations to section 6.
For the pruning experiments, we consider magnitude pruning. It is common to do fine-tuning after or during pruning [70]. Several works have independently shown that despite its simplicity, it is tough to improve upon magnitude pruning and fine-tuning [19, 4]. To our knowledge, no pruning algorithm exists that consistently outperforms this method.
For the quantization experiments, we use symmetric uniform quantization, which is defined by just the quantization scale factor and the bit-width. The scale is represented as a floating-point number and is used to map floating-point values to the integer grid. Further details on symmetric uniform quantization can be found in [49]. Uniform quantization is the standard in the quantization literature, and symmetric quantization is mostly employed for the weights. In all our experiments, we use a quantization range estimator minimizing the mean-squared error on weights by grid search [49].
3 Comparison on statistical distributions
Before diving into comparison results, we first describe theoretically what the quantization error and pruning error are. Looking at this with a theoretical lens helps with understanding the later experimental difference between the two methods. We start off by describing and analyzing both methods on simple data distributions.
In order to compare the error of pruning and quantization, we will frequently use the signal-to-noise ratio measure defined in the log scale:  $\mathrm{SNR}_{dB} = 10\log_{10}\left(\mathbb{E}\left[W^2\right] / \mathbb{E}\left[(W - F(W))^2\right]\right)$ , where  $F(W)$  is the quantization or pruning function. This measure is the same as a scaled logarithm of
Figure 2: Comparing the error of pruning and quantization for a student-t distribution, simulating the presence of significant outliers. We plot the results for different magnitudes of the outliers, as per the kurtosis on the x-axis. (left) the pruning error, which does not change under the presence of more severe outliers. (middle) the quantization SNR, which is reduced greatly when outliers increase (right) the trade-off regions where quantization and pruning are better.
an MSE measure. Both are often employed to analyze the sensitivity of neural network layers to quantization, and they are theoretically well-founded to correlate with network performance [41, 48].
3.1 Quantization error
For quantization, we consider symmetric uniform quantization, which is also called integer quantization. Given a bit-width  $b$  and the scale  $\delta$ , the grid nodes are defined as  $q_{i} = \delta i, i \in \{-2^{b}, \dots, 0, 2^{b} - 1\}$ . The quantization operation rounding-to-nearest  $Q(w)$  and the corresponding quantization error  $R(w)$  are defined as:
$$
Q (w) = q _ {i}, i = \underset {i} {\arg \min } | w - q _ {i} |, R (w) = Q (w) - w. \tag {1}
$$
Following [36] we model neural network weights as a random variable  $W \sim p(w)$ . The expected value of the quantization MSE can be expressed as follows:
$$
\mathbb {E} \left[ \left(Q (W) - W\right) ^ {2}\right) = \int_ {q _ {\min }} ^ {q _ {\max }} R ^ {2} (w) p (w) d w + \int_ {- \infty} ^ {q _ {\min }} \left(w - q _ {\min }\right) ^ {2} p (w) d w + \int_ {q _ {\max }} ^ {\infty} \left(q _ {\max } - w\right) ^ {2} p (w) d w, \tag {2}
$$
where  $q_{min} = \min_i q_i$  and  $q_{max} = \max_i q_i$  are the quantization range limits. The left term corresponds to the rounding error, and the right two terms correspond to the clipping error. We use this analytic formulation for our distribution results below, the details are given in appendix A.
3.2 Pruning error
We consider magnitude pruning  $T(x) = x \cdot \mathbb{1}_{-t \leq x \leq t}$ . This simply sets the values closest to zero to actual zero. Given this, the expected error of pruning is expressed as follows:
$$
\mathbb {E} \left[ T (W) ^ {2} \right] = \int_ {- t} ^ {t} w ^ {2} p (w) d w, \tag {3}
$$
where  $t$  is the threshold value that controls how much is pruned. Given the compression ratio  $c \in (0,1)$ , we find the threshold value which satisfies  $P(-t \leq W \leq t) = c$ . In case of a symmetric zero-mean distribution, the threshold can be expressed as  $t = F_W^{-1}\left(\frac{1}{2} + \frac{c}{2}\right)$ , where  $F(w) = P(W \leq w)$  is the CDF function and  $F^{-1}(p)$  is its inverse. The expected pruning error in equation 3 is similar to the clipping error for quantization (see the second and the third term in equation 2), and can also be computed analytically. We also use this formulation for our results below.
3.3 Analytical comparison
Standard normal distribution. Let us first look at a standard normal distribution. As many weights in neural networks are roughly Gaussian-shaped, this distribution is useful for our understanding of
Figure 3: (left) Comparison on all the weights from PyTorch model zoo (46 models) combined with 3 large language models (Bloom-3b, Llama-3b, OPT-2.7b). (left) Pruning SNR versus quantization SNR for every tensor. (right) Pruning is preferable at high compression ratios for tensors with high sample kurtosis values.
the comparison. As we can see from figure 1 (middle), the errors for both methods have very different behavior. The quantization error oscillates between the quantization nodes and has a moderate range. The pruning error effectively corresponds to rounding many weights to zero and thus has a higher error. As we can see in figure 1 (right), this results in a higher SNR for quantization, e.g. 19.1 dB for INT4 quantization versus only 5.6 dB for  $75\%$  pruning. We see similar results for different compression ratios. For this distribution, quantization achieves a much higher signal-to-noise ratio.
Distributions with heavy tails. The trade-off is expected to change when more significant outliers are introduced. The quantization grid is expected to be effected strongly by outliers as it increases the quantization grid in size, whereas the pruning method is expected to be hardly effected with outliers as it only affects weights around zero. We thus analyze both quantization and pruning errors in the presence of many outliers. To simulate a distribution with outliers, we use a truncated Student's-t distribution with  $\nu = 2$ , and a symmetric range  $(-r, r)$  (the PDF is defined in appendix B). This distribution is nice as it gives a non-trivial weight to the tail ends of the distribution close to  $r$ . The wider the range  $r$  is, the heavier are the tails of the distribution.
In order to introduce a quantitative measure of the number of outliers, we will use the distribution's kurtosis given by  $\mathrm{Kurt}[X] = \mathbb{E}\left[(X - \mu)^4\right] / \left(\mathbb{E}\left[(X - \mu)^2\right]\right)^2$ , where  $\mu$  is the mean. We will see later that this kurtosis measure is predictive of quantitation and pruning performance for real layers. To increase the number of outliers, we will increase the range  $r$ . The results are given in figure 2. The kurtosis range is chosen so that it includes most of the weights from the model zoo. We see that despite the significant outliers and high kurtosis, quantization still has higher SNR in most of the cases for moderate compression. Pruning is better however in the region of high clipping range and very high compression rate, e.g. 2-3 bits per value (see figure 2 on the right).
3.4 Experiments on real weight tensors
The previous discussion was mostly theoretical. We set out to see happens when we do a similar analysis on real neural network weights. In order to investigate this, we compare the pruning and quantization SNR on the weight tensors for all the pre-trained models from the PyTorch model  $\text{zoo}^2$  (46 models in total, the details are given in appendix E) combined with weight tensors from 3 large language models, namely Bloom-3b [3], Llama-3b [20], OPT-2.7b [67]. Each tensor is quantized using an integer grid of bit widths from 2 to 8. The results are shown in the figure 3(left). We see a similar trend to our previous discussion that pruning becomes more beneficial for lower bitwidth/higher sparsity ratios.
In order to match the analytical results from figure 2, we consider the sample kurtosis of every weight tensor given by  $k = \frac{1}{n}\sum_{i=1}^{n}(x_i - \overline{x})^4 / \left[\frac{1}{n}\sum_{i=1}^{n}(x_i - \overline{x})^2\right]^2$ . See figure 3 (right). We consider a range of kurtosis values for every quantization bit-width. Using a kernel density estimator, we compute the probability density of encountering a tensor for which pruning has higher SNR than
$^{2}$ https://pytorch.org/serve/model_zoo.html.
quantization SNR. We compare the PDF to that for quantization and thus determine the region where each method is preferable. The results are given in figure 3 on the right. We see that the results from the previous theoretical section (figure 2 on the right) hold very nicely. We can also see that as predicted, the kurtosis is indeed a good metric for predicting if a tensor should be quantized or pruned for optimal accuracy.
4 Per-layer comparison
Most PTQ methods compress the model layer by layer. Given one layer, we use the mean-squared error of the output activations as an objective for optimization. As [48] shows, minimizing per layer MSE on the output activations of each layer is a computationally affordable second-order approximation of the loss function. The local MSE objective correlates well with the task loss and is often used in practice in DNN compression and quantization literature [32, 40, 68]. Our experiments in appendix D confirm this. For the experiments in this section, we will use SNR as it represents a normalized version of MSE. As opposed to section 3 where we used SNR on weights, in this section, we will use SNR on the output activations instead.
The goal of a PTQ method is to minimize the error in the output activations of the compressed layer by optimizing over the quantized weights subject to integer range constraints. Similarly, for pruning, the weights are optimized subject to a sparsity constraint. As the underlying combinatorial optimization problem for both methods is NP-hard [56, 14], in practice, each method relies on some form of heuristic providing a reasonably good solution given a realistic compute budget. This means that any practical comparison between pruning and quantization would depend on the choice of the method for both and would be open to debate of the optimality of the algorithm. In order to eliminate this dependence, we provide a tight lower bound on the output errors for quantization. For pruning we provide a way to solve the problem exactly for moderate dimensionalities. This way, we can provide a comparison that holds regardless of the algorithm used for each method.
4.1 Post-training quantization
We set out to formulate a way by which we can get relatively tight bounds for comparison when quantizing a single layer with the MSE as the objective. The higher bound is simple to obtain by using a solution with a heuristic quantization algorithm, but for the lower bound, we have to reformulate the problem. The mean-squared error of the output activations of a quantized layer can be expressed as:
$$
\min  _ {\boldsymbol {w}} E (\boldsymbol {w}) = \left\| \boldsymbol {X} \delta \boldsymbol {w} - \boldsymbol {X} \boldsymbol {w} _ {\text {o r i g}} \right\| _ {2} ^ {2} \tag {4}
$$
$$
\begin{array}{l} \text {s . t .} \boldsymbol {w} \in \mathbb {Z} ^ {n}, \end{array}
$$
$$
w _ {m i n} \leq w _ {i} \leq w _ {m a x},
$$
where  $X$  is the input data in an unfolded form, and  $w_{orig}$  are the floating point weights. The quantized weights are computed as the product of the quantization scale  $\delta$ , and the integer weights  $w$ .  $w_{min}$  and  $w_{max}$  are the integer limits. We ignore the averaging operation to simplify the notation, as it is not important for optimization. We also note that this problem can be solved independently for each output channel of a convolution or every row of a fully-connected layer weight.
This problem is an instance of a mixed-integer quadratic program:
$$
\tilde {E} (\boldsymbol {w}) = \frac {1}{2} \boldsymbol {w} ^ {T} \boldsymbol {P} \boldsymbol {w} - \boldsymbol {q} ^ {T} \boldsymbol {w}, \tag {5}
$$
where  $\pmb{P} = 2\delta^{2}\pmb{X}^{T}\pmb{X}$ ,  $\pmb{q} = 2(\pmb{w}_{orig}^{T}\pmb{X}^{T})\pmb{X}\delta$ . In order to simplify the objective, we can omit the constant term that is irrelevant for the optimization  $c = \| \pmb{X}\pmb{w}_{orig}\|_{2}^{2}$ , i.e.  $\tilde{E}(\pmb{W}) = E(\pmb{W}) - c$ .
In order to find the lower bound of the objective, we follow [55] and relax the integer constraint to  $w_{i}(w_{i} - 1) \geq 0$ , which allows the weight to take values within the interval from 0 to 1. In order to
obtain the lower bound, we will consider the dual version of the relaxed problem:
$$
L (\boldsymbol {\lambda}) = \max  - \gamma , \tag {6}
$$
$$
\text {s . t .} \left[ \begin{array}{c c} \boldsymbol {P} - \operatorname {d i a g} (\boldsymbol {\lambda}) & q + \frac {1}{2} \lambda \\ (q + \frac {1}{2} \lambda) ^ {T} & \gamma \end{array} \right] \succeq 0,
$$
$$
\lambda \geq 0,
$$
where  $\lambda \in \mathbb{R}^n$ ,  $\gamma \in \mathbb{R}$ . The dual problem is convex, and its solution can be used as a lower bound on the solution of the original problem, i.e.,  $\tilde{E}(\boldsymbol{w}) \geq L(\boldsymbol{\lambda})$ . The dual has a semi-definite constraint which can be solved with a semi-definite programming (SDP) solver with  $\mathcal{O}(n^3)$  complexity. In our work, we used CVX solver [21]. As discussed in [55], this bound is a computationally efficient alternative to branch-and-bound approaches, while tightness is better than that for the alternative methods introduced in [5]. We use this approach for estimating the lower bound for MSE on the output activations for PTQ below.
4.2 Post-training pruning
We also need a similar lower bound for pruning for comparison. To the best of our knowledge we are not aware of the ways to provide a tight lower bound for this problem, therefore we formulate a way to solve a problem for moderate dimensionalities exactly. Similar to quantization, post-training pruning of one layer of the network can mathematically be expressed as solving the following optimization problem:
$$
E = \min  _ {\hat {\boldsymbol {w}}} \| \boldsymbol {X} \hat {\boldsymbol {w}} - \boldsymbol {X} \boldsymbol {w} _ {\text {o r i g}} \| _ {2} ^ {2} \tag {7}
$$
$$
\begin{array}{l} \text {s . t .} \| \hat {\boldsymbol {w}} \| _ {0} \leq s, \end{array}
$$
where the number of non-zero elements  $s$  in the solution is theoretically constrained by using the  $L_0$  norm, which is non-convex and not smooth. In order to solve the problem, we introduce the sparsity mask  $m \in \mathbb{R}^n$ :
$$
E (\boldsymbol {w}) = \min  _ {\boldsymbol {w}, \boldsymbol {m}} \| \boldsymbol {X} (\boldsymbol {m} \odot \boldsymbol {w}) - \boldsymbol {X} \boldsymbol {w} _ {\text {o r i g}} \| _ {2} ^ {2}, \tag {8}
$$
$$
\begin{array}{l} \text {s . t .} \| \boldsymbol {m} \| _ {1} = s, \end{array}
$$
$$
- \boldsymbol {m} \odot l \leq \hat {\boldsymbol {w}} \leq \boldsymbol {m} \odot u
$$
$$
l, u > 0, m _ {i} \in \{0, 1 \},
$$
where  $\odot$  is an element-wise product operation, and  $l, u \in \mathbb{R}$  are constants chosen such that any solution satisfies the constraint  $-\boldsymbol{m} \odot l \leq \hat{\boldsymbol{w}} \leq \boldsymbol{m} \odot u$ . We solve this problem using the branch-and-bound method implemented in the Gurobi solver [23] that gives the global solution.
4.3 Experiments
With our algorithms in the bag, we can now compare quantization versus pruning in the post-training settings with theoretical bounds. In each case, we analyze individual layers of several networks. Given a batch of input data, we optimize the pruned or quantized weights to minimize the error between the output activations and the output of the uncompressed layer. We provide a range between two SNR values for each method in each case. The performance of the heuristic method gives the first value, and the second value is given by the error lower bound or the global solution, which translates into SNR upper bound.
As a heuristic method for pruning, we use magnitude pruning with a fixed sparsity mask  $m$  and data-optimized weights  $w$  given by  $w = \operatorname*{argmin}_{\boldsymbol{w}} \| \boldsymbol{X}(\boldsymbol{m} \odot \boldsymbol{w}) - \boldsymbol{X}\boldsymbol{w}_{orig} \|_2^2$ . This is a convex problem and has a unique solution. As a heuristic method for quantization, we use the mixed-integer solver introduced in [55]. We clip every sample in order to satisfy the integer quantization range constraint.
We chose a representative set of 10 layers, including 9 convolutional layers (one  $3 \times 3$  convolutional layer and 8 point-wise convolutions) from MobileNet-V2, EfficientNet-lite, and Resnet-18, and one fully-connected layer from ViT. The full details for reproducing the experiments are given in appendix F. Due to the high computational complexity of the global solution for pruning, the layers
Figure 4: Comparison in the post-training scenario. Each box corresponds to a subset of one of 10 layers from the 4 different models that were used, with 7 different bit-width comparison points. The ranges of the box indicate the lower and higher-bounds found by the algorithms.
had to be split into chunks. The slice of 4 input channels over all output channels was used for  $3 \times 3$  convolutions. In the case of linear layers and point-wise convolutions, slices 36 input features over all the output features were used.
The results are shown in figure 4 grouped by bit-width. The rectangles indicate the full range of the pruning and quantization methods between the heuristic solution and the error lower bound or the global solution. Whenever a rectangle for each chunk intersects the diagonal line, the ranking of the two methods could depend on the optimization method, while in cases below or above the diagonal, the ranking is guaranteed regardless of the optimizer. We see that quantization mostly outperforms pruning for moderate compression, while methods become more comparable for higher compression ratios.
5 Full-model comparison
Now that we have seen the comparison between the methods in the PTQ setting, we turn to fine-tuning quantized and pruned models. This is the setting where pruning is applied in most, and it is possible that fine-tuning can change the models significantly enough that the performance between the two methods changes.