Neural Network Compression using Binarization and Few Full-Precision
  Weights
Quantization and pruning are two effective Deep Neural Networks model
compression methods. In this paper, we propose Automatic Prune Binarization
(APB), a novel compression technique combining quantization with pruning. APB
enhances the representational capability of binary networks using a few
full-precision weights. Our technique jointly maximizes the accuracy of the
network while minimizing its memory impact by deciding whether each weight
should be binarized or kept in full precision. We show how to efficiently
perform a forward pass through layers compressed using APB by decomposing it
into a binary and a sparse-dense matrix multiplication. Moreover, we design two
novel efficient algorithms for extremely quantized matrix multiplication on
CPU, leveraging highly efficient bitwise operations. The proposed algorithms
are 6.9x and 1.5x faster than available state-of-the-art solutions. We
extensively evaluate APB on two widely adopted model compression datasets,
namely CIFAR10 and ImageNet. APB delivers better accuracy/memory trade-off
compared to state-of-the-art methods based on i) quantization, ii) pruning, and
iii) combination of pruning and quantization. APB outperforms quantization in
the accuracy/efficiency trade-off, being up to 2x faster than the 2-bit
quantized model with no loss in accuracy.
Franco Maria Nardini, Cosimo Rulli, Salvatore Trani, Rossano Venturini
# Neural Network Compression using Binarization and Few Full-Precision Weights

###### Abstract

Quantization and pruning are known to be two effective Deep Neural Networks model compression methods. In this paper, we propose _Automatic Prune Binarization_ (APB), a novel compression technique combining quantization with pruning. APB enhances the representational capability of binary networks using a few full-precision weights. Our technique jointly maximizes the accuracy of the network while minimizing its memory impact by deciding whether each weight should be binarized or kept in full precision. We show how to efficiently perform a forward pass through layers compressed using APB by decomposing it into a binary and a sparse-dense matrix multiplication. Moreover, we design two novel efficient algorithms for extremely quantized matrix multiplication on CPU, leveraging highly efficient bitwise operations. The proposed algorithms are \(6.9\times\) and \(1.5\times\) faster than available state-of-the-art solutions. We perform an extensive evaluation of APB on two widely adopted model compression datasets, namely CIFAR-10 and ImageNet. APB shows to deliver better accuracy/memory trade-off compared to state-of-the-art methods based on i) quantization, ii) pruning, and ii) combination of pruning and quantization. APB outperforms quantization also in the accuracy/efficiency trade-off, being up to \(2\times\) faster than the \(2\)-bits quantized model with no loss in accuracy.

 Deep Neural Networks, Model Compression, Matrix Multiplication, Image Classification.

## 1 Introduction

Deep Neural Networks (DNNs) had an unprecedented impact on computer vision and achieve state-of-the-art performance in many different tasks, such as image classification [40], semantic segmentation [15], and object detection [35]. Indeed, DNNs huge computational requirements pose severe challenges to their pervasive application. Model compression is the field of Deep Learning devoted to decreasing the computational requirements of Deep Neural Networks (DNNs) by leveraging their largely proven over-parametrization [2].

Quantization techniques reduce the number of bits required to represent the network parameters, thus offering compelling properties in terms of space saving and inference speedup. In this line, binarization, i.e., 1-bit quantization, reduces the memory burden of \(32\times\) with respect to its full-precision counterpart and converts floating-point multiplications into cheap bitwise operations [34]. Despite the work done in this field [33, 46, 48], binary networks still struggle to match the performance of the corresponding full-precision model. At the same time, \(2\) or \(3\)-bits quantization approaches are closing the gap with full precision models [24, 47], but do not ensure the compelling properties of binary networks, especially in terms of inference speedup on CPU.

Pruning techniques remove network parameters to produce sparse models as accurate as their original dense versions [11, 13, 38]. Pruning allows for consistent memory savings as only the non-zero parameters and their positions need to be saved. Despite the reduction of Floating Points OPerations (FLOPs), pruning a neural network does not imply a remarkable inference speedup until extreme sparsity levels are achieved, i.e., \(>95\%\), as CPUs and GPUs are optimized for dense computation [49]. To summarize, binarization techniques allow for fast neural inference but do not match the performance of the equivalent full-precision models. On the other hand, pruning techniques can produce highly effective sparse networks but do not deliver consistent speedup until extreme sparsity ratios are reached. Observe that quantization and pruning adopt complementary approaches to compress a neural network. Quantization maintains network parameters' cardinality while shrinking the parameters' representation, i.e., the number of distinct values a parameter can assume. Instead, pruning reduces

Fig. 1: Graphical illustration of Automatic Prune Binarization (APB). The weights in the binarization interval are converted to \(\{-\alpha,\alpha\}\) according to their sign while the remaining values are kept in full precision.
the cardinality of non-trivial parameters but it allows the surviving ones to span in the original representation, i.e., real numbers represented using \(32\) bits. Our aim is to blend these approaches together to leverage the properties of quantization, especially binarization, while empowering its representational capability with few full-precision entries.

In this paper, we propose Automatic Prune Binarization (APB), a novel compression method that combines low-bit quantization (binarization) with pruning. APB allows each parameter to assume either a binary or a full-precision representation (Figure 1). APB jointly maximizes the accuracy achieved by the network while minimizing its memory impact by identifying an optimal partition of the network parameters among these two sets. In detail, it works by identifying a binarization interval (centered in \(0\)), and the parameters falling in this interval are represented using one bit. As done by other state-of-the-art binarization approaches, according to their sign, binarized parameters are converted to \(\{\neg\alpha,\alpha\}\), where \(\alpha\) is a learned layer-wise scalar value (Figure 1). On the other hand, parameters outside the binarization interval are kept in full precision. By doing so, APB produces two different overlapping networks, i.e., a binary network and an extremely sparse full-precision network. Their combined expressiveness allows for improving the performance of binary networks without the need of doubling the bits required to represent the weights, as done in \(2\)-bits quantization. This goal can be achieved when the number of full-precision weights is sufficiently low. In this case, APB also offers compelling inference properties. In fact, we experimentally show that the overhead given by the sparse network is negligible at our sparsity ratios. Moreover, we develop and present two novel matrix multiplication algorithms for CPU in extreme quantization scenarios. Our approach works by remapping a \(q\)-bits matrix in a set of \(q+1\) binary matrices. Then, these matrices can be efficiently multiplied by leveraging cheap bitwise operations. We provide a high-performance implementation of these novel algorithms on CPU and show that our implementation can be much faster than currently available, general-purpose solutions [21] for quantized matrix multiplication. To the best of our knowledge, these are the first techniques tailored for extremely quantized matrices on CPU, except for the binary case. This allows the evaluation of the efficiency of highly quantized networks on CPU for the first time. Overall, we experimentally show that APB outperforms the performance of state-of-the-art quantization approaches in terms of memory/accuracy and efficiency/accuracy trade-offs.

In detail, the novel contributions of this work are:

* we introduce Automatic Prune Binarization (APB). This novel compression framework jointly maximizes the accuracy achieved by the network while minimizing its memory impact by deciding whether each weight should be binarized or kept in full precision. We show how the problem of partitioning the set of weights can be addressed using Stochastic Gradient Descent.
* we address the problem of the efficiency of quantized networks on CPU. We first show that the forward pass through a layer compressed using APB can be decomposed into a binary multiplication and a sparse binary multiplication. We discuss the efficiency of matrix multiplication as a function of the operand's bit width. We then design two novel matrix multiplication routines based on efficient bitwise operations available on CPU for extreme quantization scenarios. The source code of APB and our novel bitwise matrix multiplication routines will be released upon publication of the manuscript.
* we provide an extensive experimental evaluation on two public datasets used in model compression, i.e., CIFAR-10 [22] and ImageNet [5]. Experiments show that APB offers better accuracy/memory trade-off compared to any state-of-the-art compression method based on i) quantization, ii) pruning, iii) the combination of quantization and pruning.
* we evaluate the performance of our novel low-bits matrix multiplication routines when employed for the forward pass of neural networks on ImageNet. Our methods are \(6.85\times\) and \(1.5\times\) faster than available state-of-the-art solutions. Moreover, APB shows superior performance even in the accuracy/efficiency trade-off, being \(2\times\) faster than the \(2\)-bits quantized model with no loss in accuracy.

The rest of the paper is organized as follows: in Section 2 we present the related work in binarization, low-bits quantization, pruning, and efficient inference on CPU for neural networks. In Section 3 we present our novel APB compression framework mixing full-precision and binary weights. In Section 4 we discuss the efficiency of matrix multiplication at different bit widths and we introduce our novel low-bits matrix multiplication routines. Section 5 presents an experimental evaluation of APB. First, we discuss the memory compression performance of APB (Section 5.2). Then, we evaluate the performance of our matrix multiplication algorithms and compare the execution time of the quantized networks against APB (Section 5.3). Finally, Section 6 concludes the work and draws some future lines of research.

## 2 Related Work

Our method lies at the intersection of two families of compressors, i.e., _binarization/low-bit quantization_[4] and _pruning_[23, 14]. In the following, we review the main contributions in these lines along with the ones investigating _efficient inference_ algorithms on quantized networks [31].

**Binarization**. Pioneering work on binarization are BinaryConnect [4] and XNOR-Net [34]. These methods rely on the use of the _sign_ function to constrain the weights in \(\{\neg 1,1\}\) and to scale them by the mean of their absolute value. More recent work leverages advanced techniques to train highly-accurate binary models. Recently, in that line, some works show that maximizing the entropy of the binarized weights is an effective approach [25, 33]. Xu _et al._ show the importance of _latent weights_, i.e., full-precision weights used during backpropagation and weight update. The authors focus on _dead weights_, i.e., weights that are rarely updated due to their distance to the origin. Authors show that these weights are responsible for hampering the
training process and they propose a tailored Rectified Clamp. Unit to reviy those weights. Liu _et al._ tackle the problem of frequent weight flipping, i.e., weights changing their sign, by employing two learnable scaling gradient factors for the activations, one for each of the binary states (\(\{\cdot 1,1\}\)) [27]. Another family of approaches proposes architectural changes to the network to quantize. Bi-Real Net adds a double skip-connection on the ResNet architecture to sum the real-valued input with the features obtained after a binary convolution [29]. A well-known solution in this field is ReActNet [28]. Here, the authors duplicate the input channels of convolutional layers, introduce tailored activation functions for binary networks and employ knowledge distillation to enhance the training phase. Hu _et al._ introduce _Squeeze_ and _Expand_ layers aiming at combining input and output activations [18].

**Low-bits Quantization**. Quantization techniques often rely on the Straight-Through Estimator (STE) [1] to propagate the gradients through non-differentiable quantization functions. Lee _et al._ propose to overcome the limits of STE by exploiting an element-wise gradient correction method [24]. Their approach, named Element-Wise Gradient Scaling (EWGS), scales the gradient of each full-precision weight accordingly to three factors: i) its sign, ii) the gap between full-precision and quantized value, iii) a scaling factor learned through the approximation of the Hessian. SLB employs a continuous relaxation strategy to overcome the gradient mismatch problem [48]. Each weight of the network is mapped to a probability distribution that represents the values it can assume with a \(q\)-bits representation. The values associated with the highest probabilities are selected as quantization values. Yamamoto proposes a non-uniform quantization method for pre-activations ResNet models [44]. The approach is based on learnable functions that wrap the quantization process to allow effective tune the quantization levels [47].

**Pruning**. Pruning techniques effectively sparsify neural networks with small/no accuracy degradation.1 Recently, a plethora of different pruning methods has been developed. For a comprehensive discussion, we recommend the reading of dedicated surveys [26]. We highlight that the importance of high absolute-value weights in neural networks was first discovered in pruning techniques. In fact, magnitude-based heuristics save high absolute-value weights while zeroing out the others. Han _et al._ are the first to apply magnitude-based pruning in conjunction with re-training of the network to mitigate the possible performance degradation [12]. Lately, magnitude pruning has been improved by introducing re-winding, namely reassigning the surviving parameters to their initialization values, showing that it outperforms fine-tuning on several network architectures and datasets [36]. A lot of effort has been spent in training sparse networks (or pruning them at initialization), rather than pruning after training. This interest is motivated by the so-called "Lottery Ticket Hypothesis", namely the existence of highly effective sparse networks in randomly initialized dense networks [8].

Footnote 1: with pruning, we always refer to element-wise pruning. See Liang _et al._[26] for a complete analysis of the difference between element-wise and structured pruning.

**Combination of Pruning and Quantization** Several works explore the combination of pruning and quantization, to fruitfully exploit the advantages provided by both techniques [6, 12, 25, 41, 42]. Han _et al._[12] apply clustering algorithm on the weights surviving the pruning phase, then optimize the value of the centroids using the average of the weight gradients. Bayesian Bits [42] is an approach where mixed precision quantization is combined with pruning. In particular, network weights are either quantized to a power-of-two-bit width or zeroed out in a data-driven fashion. In "Multi-Prize Lottery Ticket" (MPT) [6], Diffenderfer _et al._ mix pruning and binarization by i) discovering highly-effective sub-networks in neural networks, ii) binarizing the surviving values.

**Efficient Inference**. Nurvitadhi _et al._ study the efficiency of binary multiplication on different hardware platforms. Authors estimate a speedup of \(2\times\) of binary over single-precision multiplication on a CPU equipped with \(64\)-bit bitwise instructions [31]. Regarding efficient inference of binary network on CPU, a major contribution is BitFlow, a binary convolution algorithm (_Pressed-Conv_) based on bit-packing on the channel dimension [19]. BitFlow provides \(1.8\times\) speedup compared to naive binary convolution. In this line, DaBnn is an efficient inference framework for binary neural networks on mobile platforms powered by ARM processors [50]. Our work is the first one studying the efficiency of low-bits quantized neural networks on CPU.

**Our Contribution**. APB approach is orthogonal to other techniques mixing pruning and quantization. As an example, in works combining binarization and pruning [6], parameters are either zero or binary (\(\{\cdot 1,+1\}\)). APB, instead, enriches the expressiveness of binary networks with full-precision parameters in the same framework that effectively mixes binarization and pruning. This means that weights in APB-compressed networks are either binary or _full-precision_. To the best of our knowledge, we are the first to jointly optimize binary and full-precision parameters in a novel and end-to-end compression framework.

## 3 Automatic Prune Binarization

We now describe APB, our novel compression framework that adds a few full-precision values to binary networks. First, we introduce the role of binarization. Second, we show how large absolute-value weights play different roles in binarization and pruning. Third, we formally introduce APB and show how its parameters can be optimized by leveraging Stochastic Gradient Descent (SGD). Finally, we show how to decompose the matrix multiplication into dense-dense and sparse-dense matrix multiplications.

**Binarization**. Let us consider \(W\in\mathbb{R}^{n}\) as the set of weights of a neural network. The scope of binarization is to employ a single bit to store each weight \(w\in W\), forcing \(w\) to be \(\{\cdot 1,+1\}\). Previous studies show that it is possible to enhance the expressiveness of the model by scaling the weights with a scalar \(\alpha\)[34] so as to remap them to \(\{\cdot\alpha,+\alpha\}\). We rely on re-scaled binarization that asks for the definition of a Bin operator defined as follows:

\[\texttt{Bin}(w)=\alpha\cdot\texttt{sign}(w)=\left\{\begin{array}{ll}+\alpha &\text{if }w\geq 0\\ -\alpha&\text{if }w<0.\end{array}\right. \tag{1}\]
The Bin operator is defined as a function of \(sign(w)\), whose derivative is zero almost everywhere. This hinders the usage of gradient-based approaches for training the model. For this reason, gradients are approximated with the Straight Through Estimator (STE) [1]. STE works by using a surrogate differentiable function \(g(w)\) that approximates \(sign(w)\). Hence, we can use the derivative of \(g()\) in place of the derivative of \(sign()\). In practice, STE imposes that:

\[\frac{\partial\texttt{Bin}(\texttt{w})}{\partial w}=\frac{\partial g(w)}{ \partial w}. \tag{2}\]

This derivative is used to update the full-precision weights \(W_{l}\), which are referred as _latent weights_[46] in the context of binarization. The final binary matrix \(W\) is then obtained by simply applying the Bin operator over \(W_{l}\).

**Large Absolute-value Weights**. Given the STE training strategy, the binary weight \(w\) is updated only when there is a flip of sign in the corresponding latent weight \(w_{l}\in W_{l}\), as a result of the gradient update. In a recent work [46], authors identified the problem of large absolute-value parameters that diverge from the zero-centered Laplacian distribution characterizing latent weights, namely _dead weights_. These weights interfere with the optimization phase giving a reduced chance to change their sign. In practice, they freeze part of the network, hindering the training process.

Even in pruning, large absolute-value weights play a central role. Here, network layers are sparsified by zeroing out less important weights, and the importance of each parameter is chosen according to a heuristic. Interestingly, a simple yet very effective heuristic to determine the importance of a parameter is its magnitude [13, 11]. Several works show that a small portion (\(<10\%\)) of large absolute-value weights is enough to match the performance of the dense model.

Large absolute-value weights thus play contrasting roles in binarization and pruning techniques. In binarization, they hamper and slow down the training process due to the low likelihood of changing the sign. In pruning, they synthesize the expressiveness of the overall model. We build our approach on this discrepancy: we leverage the advantages that large-absolute weights provide in pruning while mitigating the drawbacks associated with binarization. This is the key intuition underlying APB, our novel method for effective compression of neural networks. APB keeps high absolute-value weights in full precision and it bianarizes the remaining ones. For this purpose, APB defines a symmetric binarization interval around zero (Figure 1): weights falling outside this interval are kept in full-precision, while the others are mapped to \(\{-\alpha,+\alpha\}\) according to their sign. In this regard, APB can be interpreted as a pruning technique where small absolute-value weights are binarized instead of being zeroed out [13]. The amplitude of the binarization interval and the value of the scalar \(\alpha\) are optimized during training. As shown in Figure 2, APB compresses the network by applying pruning and binarization in parallel, i.e., each weight is either full-precision or binary. Conversely, in classical approaches mixing pruning and quantization/binarization, each parameter is either zeroed-out or quantized/binarized [12, 41, 42]. Within APB, two different networks coexist during the training: a binary and a sparse network.

**APB**. Given the considerations on the role of large absolute-value weights, APB employs weight magnitude to determine whether weight should be binarized or kept in full precision. Our approach is partially inspired by [45], which introduces a trainable threshold to determine if a weight should be set to 0 or 1. The operator APB on a weight \(w\) is defined as:

\[\texttt{APB}(\texttt{w})=\left\{\begin{array}{ll}\texttt{sign}(w)\,\alpha &\text{ if }|w|\leq\alpha+\delta\\ w&\text{ otherwise,}\end{array}\right. \tag{3}\]

with \(\delta\) being the amplitude of the binarization interval exceeding \(\alpha\). Figure 3 graphically depicts how APB works. The weights whose absolute value ranges in \([0,\alpha+\delta]\) are binarized (orange area), while the parameters falling outside this interval are kept in full precision (green area).

If \(w\) is within the binarization interval, APB is not differentiable. In this case, we apply STE by employing the identity function, i.e., \(id(x)=x\), as \(g(w)\)[1]. Thus, the derivative becomes:

\[\frac{\partial\texttt{APB}}{\partial w}=\left\{\begin{array}{ll}g^{\prime}(w )&\text{ if }|w|\leq\alpha+\delta\\ 1&\text{ otherwise.}\end{array}\right. \tag{4}\]

Assuming \(\delta\geq 0\), we can re-write

\[|w|\leq\alpha+\delta\quad\Rightarrow\quad\frac{|w|-\alpha}{\delta}\leq 1. \tag{5}\]

To ease the notation, we define \(\hat{w}=\frac{|w|-\alpha}{\delta}\). This entails:

\[\texttt{APB}(w)=\left\{\begin{array}{ll}\texttt{sign}(w)\,\alpha&\text{ if }\hat{w}\leq 1\\ w&\text{ otherwise.}\end{array}\right. \tag{6}\]

We define the indicator function of the set of binarized weight as \(\chi_{B}:=\mathds{1}(\hat{w}\leq 1)\). We can now define the gradients of \(\alpha\) and \(\delta\) by unrolling the derivatives of the loss function \(\mathcal{L}\). We introduce the indicator function to constrain \(\alpha\) and \(\delta\) to depend exclusively on the binarized weights and by leaving them independent from those weights that APB lefts full-precision.

\[\frac{\partial\mathcal{L}}{\partial\delta}=\frac{1}{n}\sum_{\alpha}\frac{ \partial\mathcal{L}}{\partial\hat{w}}\frac{\partial\hat{w}}{\partial\delta} \chi_{B}=\frac{1}{\delta^{2}n}\sum\frac{\partial\mathcal{L}}{\partial\hat{w}}( \alpha-|w|)\chi_{B}. \tag{7}\]

Figure 2: Network weights representation in binarization (left), pruning (center), APB (right). In APB, binary and full-precision weights coexist in the same matrix. For full-precision entries, we require to store also the index inside the weight matrix.
\[\frac{\partial\mathcal{L}}{\partial\alpha}=\frac{1}{n}\sum\frac{\partial\mathcal{L} }{\partial\bar{w}}\frac{\partial\hat{w}}{\partial\alpha}=-\frac{1}{\delta n} \sum\frac{\partial\mathcal{L}}{\partial\bar{w}}\chi_{B}. \tag{8}\]

We also observe that

\[\frac{\partial\mathcal{L}}{\partial w}=\frac{1}{\delta}\frac{\partial\mathcal{ L}}{\partial\bar{w}}\text{sign}(w). \tag{9}\]

We can compute \(\frac{\partial\mathcal{L}}{\partial\alpha}\) and \(\frac{\partial\mathcal{L}}{\partial\bar{\delta}}\) using \(\frac{\partial\mathcal{L}}{\partial w}\), which is the standard derivative of the weights w.r.t to the cost function, obtained using the backpropagation algorithm [37].

**Memory Impact**. We denote with \(\texttt{Mem}(W)\) the memory impact of the matrix \(W\in\mathbb{R}^{n}\) compressed using APB, expressed in bits. Assuming to have \(s\) full-precision surviving entries, we need to store \(n-s\) binary weights and \(s\) full-precision values with their position. However, since \(s\ll n\) and for easing the matrix multiplication, the binary matrix is fully represented. Hence:

\[\texttt{Mem}(W)=(n-s)+s(b_{v}+b_{p})\simeq n+s(b_{v}+b_{p}) \tag{10}\]

where \(b_{v}\) is the bit width of the full precision values (\(32\) for floating point) and \(b_{p}\) represents the bits needed to store their positions in the matrix. For each neural architecture under evaluation, we compute \(b_{p}\) as \(\max_{i}\log_{2}(k_{i}-1)+1\), where \(k_{i}\) is the dimension of layer \(i\).

**Inference Considerations**. We now discuss how to efficiently multiply a weight matrix \(A\), compressed using APB, against an input activation matrix \(B\).2 For every \(A_{i}\in A\), we define the mask associated with \(A\):

Footnote 2: Convolution can be converted to matrix multiplication using the _im2col_ technique [3].

\[\texttt{Mask}(A_{i})=\left\{\begin{array}{ll}1&\text{ if }A_{i}\in\{ \neg\alpha,+\alpha\}\\ 0&\text{ otherwise.}\end{array}\right. \tag{11}\]

We decompose \(A=A^{\text{bin}}+A^{\text{full}}\), where \(A^{\text{bin}}\in\{\neg\alpha,+\alpha\}^{n}\) is a binary matrix and \(A^{\text{full}}\in\mathbb{R}^{n}\) is a sparse full-precision matrix. \(A^{\text{bin}}\) is defined as \(A^{\text{bin}}_{i}=\alpha\cdot\text{sign}(A_{i})\), while \(A^{\text{full}}\) is given by

\[A^{\text{full}}_{i}=\left\{\begin{array}{ll}0&\text{ if }\texttt{Mask}(A_{i})=1\\ A_{i}-A^{\text{bin}}_{i}&\text{ otherwise.}\end{array}\right. \tag{12}\]

Given an input matrix \(B\) and the distributive property of matrix multiplication, we can write

\[C=A\cdot B=(A^{\text{bin}}+A^{\text{full}})\cdot B=A^{\text{bin}}\cdot B+A^{ \text{full}}\cdot B. \tag{13}\]

Since \(A^{\text{bin}}\) is a binary matrix, the only overhead introduced by APB is a sparse-dense matrix multiplication. Due to the extreme sparsity ratios of \(A^{\text{full}}\), the sparse-dense multiplication can be efficiently performed with tailored implementations such as LIBXSMM [17].

## 4 Bitwise Matrix Multiplication

The efficiency of neural inference heavily relies on the efficiency of matrix multiplication (MM). MM has been widely studied [10, 20, 43] due to its paramount role in many scientific applications. Here we discuss the efficiency of MM at different quantization levels and we introduce our novel routines for optimized matrix multiplication in low-bit configurations. In the following, we employ the notation w/a to specify the bit width of the weights and the activations matrices, respectively. In particular, we show how to implement efficient \(1/2,2/2\) MM using logical and bitwise operators. To the best of our knowledge, we are the first to investigate the efficiency of such low-bits configurations on CPU.

**Efficiency of Matrix Multiplication**. Matrix multiplication is known to be a memory-bounded problem. Indeed, several techniques have been developed to mitigate this aspect, and, nowadays, its efficiency mostly depends on the available computational power.3 The core operation of MM is the _update_ function \(c\gets c+ab\), which is recursively applied on portions of the input matrices \(a,b\) to incrementally compute the output \(c\). In the context of neural inference, \(c\), \(a\), and \(b\) represent the output, the weights, and the input of each layer, respectively. Modern CPUs allow performing the operation above with the _fused-multiply add_ (fma) instruction, which computes the update with the same latency and throughput of an add instruction [7]. Furthermore, modern CPUs can rely on instruction-level parallelism (SIMD) which allows the processing of multiple inputs at the same time. As an example, the _mm512_fmadd_ps instruction computes the operation \(c\gets c+ab\) on three vectors of \(16\) full-precision values each. The theoretical peak performance (\(tpp\)) measures how many update functions (up) can be carried out per second (sec) in an ideal scenario assuming that the memory cost is negligible, i.e.,

Footnote 3: [https://en.algorithmica.org/hpc/algorithms/matmul/](https://en.algorithmica.org/hpc/algorithms/matmul/)

\[tpp=cf\cdot tp\cdot v\ \frac{\texttt{up}}{\text{sec}}, \tag{14}\]

where \(cf\) is the clock frequency, \(tp\) is the throughput of the fma instruction and \(v\) is the number of operands that can be stored on a CPU register. \(v\) is computed by dividing the width of the SIMD register, e.g., \(512\) for avx-512, by the number of bits of the operand.

Network quantization exploits the performance gain obtained by reducing the bit width of weights and activations, hinging on the extreme flexibility of neural networks to model compression. Observe that every time that the operand bit width halves, twice the data fit into the same CPU register (\(v\)). Consequently, \(tpp\) doubles each time the

Fig. 3: Automatic Prune Binarization (apB) applied on the network parameters \(W\). The width of the binarization interval (orange area) is defined by \(\alpha+\delta\). A weight \(w_{i}\in W\) is binarized if \(|w_{i}|\leq\alpha+\delta\), otherwise it is kept in full-precision (green area).
operand bit width halves. However, on modern CPUs the fma instruction exists only for double/single/half-precision float and \(8\)-bits integer values. For smaller bit widths, e.g., \(2\) or \(3\) bits, the use of the fma instruction requires to upcast the operands to the closest supported data type, i.e., \(8\) bits. The situation is different for a binary network as it allows to implement MM by leveraging addition/subtraction or bitwise operations. This motivates us to investigate fast bitwise matrix multiplication techniques for efficient neural inference on CPU. We now discuss the \(1/32\) and the \(1/1\) cases, then we present our novel bitwise matrix multiplication routines for the \(1/2\) and the \(2/2\) scenarios.

**1/32**. In this quantization schema, weights \(w\) are constrained to assume values in \(\{\text{-}1,1\}\). Conversely, activations \(a\) are kept in full precision, i.e., they are represented by using 32-bit floating point. This configuration is explored in several state-of-the-art quantization works [9, 33, 51], as it features a \(32\times\) memory saving compared to the single-precision representation. In principle, binary weights convert multiplication into additions and subtractions [34]. We will now show that this conversion does not deliver remarkable speedup over classical multiplication. Given \(w\in\{\text{-}1,1\}\), in Equation 15 we show how to rewrite the dot product between \(w\) and \(a\) into a series of additions and subtractions. We can write,

\[w\cdot a=\sum_{i=1}^{n}w_{i}a_{i}=\sum_{j\in\mathcal{I}_{+}}a_{j}-\sum_{j\in \mathcal{I}_{-}}a_{j}. \tag{15}\]

where \(\mathcal{I}_{+}=\{i\mid w_{i}=1\}\) and \(\mathcal{I}_{-}=\{i\mid w_{i}=\text{-}1\}\). In detail, \(I_{+}\) is the set of indexes of positive weights (\(w_{i}=1\)), while \(I_{-}\) keeps track of negative weights (\(w_{i}=\text{-}1\)). In this formulation, we simply accumulate the activations in correspondence with positive weights and then subtract the sum of the activation in correspondence with negative weights. Indeed, the fma operation achieves the same latency and throughput as the add/sub operations on modern CPUs.4 This means that the update function \(c\gets c+ab\) (Equation 15) costs \(2\times\) the fma-based full-precision matrix multiplication. We can think of a more efficient approach that reduces the update function to a single addition operation. Let us define

Footnote 4: [https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html](https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html)

\[T=\sum_{i=1}^{n}a_{i},\quad I_{+}=\sum_{j\in\mathcal{I}_{+}}a_{j},\quad I_{-}= \sum_{j\in\mathcal{I}_{-}}a_{j}. \tag{16}\]

with \(T=I_{+}+I_{-}\) by construction. \(T\) is the sum of the activations along the columns and it does not depend on the weights \(w\). This means that \(T\) can be computed at the beginning of the multiplication and then re-used for all the columns of \(a\). Moreover, it can be computed in \(\Theta(n^{2})\) time as it requires the sum of \(n\) values along \(n\) columns. Its impact is negligible compared to \(\Theta(n^{3})\) -- time needed to run the matrix multiplication. \(T\) can be exploited to avoid the computation of one between \(I_{+}\) or \(I_{-}\). For example, we can obtain \(I_{-}\) as \(I_{-}=2\,I_{+}-T\). Thus, we can compute \(w\cdot a\) as

\[w\cdot a=I_{+}-I_{-}=2\,I_{+}-T.\]

The cost of multiplying by \(2\) and subtracting \(T\) is negligible, as it does not depend on the size of the input. The cost of multiplying \(w\in\{\text{-}1,1\}\) and \(a\) is dominated by the computation of \(I_{+}\). It can be efficiently implemented by leveraging a masked floating point addition, where the mask identifies the \(i\in I_{+}\). On recent Intel's instruction set AVX512, the masked floating point addition can be implemented by using the _mm512_add_ps instruction. Anyway, this instruction has the same latency and throughput of fma. As a consequence, it does not offer any computational advantage compared to 32-bit floating point MM. Moreover, the binary representation for the weights is useless in this case, as they require to be converted into 32-bit float numbers when performing the vectorized _mm512_add_ps on CPU. This can be done in two ways: i) the representation of the binary weights is expanded in memory with the consequence of achieving the same memory footprint of full-precision computation, or ii) they are directly expanded to \(32\) bits when moved to the CPU registers with the consequence of having an overhead due to bit extraction. We conclude that the \(1/32\) scenario does not offer strong computational advantages compared to \(32\) floating point implementation. The observations we draw also hold if activations are quantized to \(8\) or \(16\) bits.

**1/1**. Several works investigate how to effectively train fully binary neural networks [18, 25, 33, 34, 46, 27], where both weights \(w\) and activations \(a\) can assume two values, i.e., \(\{\text{-}1,+1\}\). Besides the memory savings achieved, \(1/1\) also allows fast inference. In fact, given two binary vectors \(u,v\in\{\text{-}1,1\}^{n}\), their dot product can be computed as

\[u\cdot v=n-2\cdot\text{popcount}(\text{xor}(u,v)). \tag{17}\]

We observe that Equation 17 holds for every \(u^{\gamma}=\{\text{-}\beta,\gamma\}^{n}\), \(v^{\beta}=\{\text{-}\beta,\beta\}^{n}\), as \(u^{\gamma}\cdot v^{\beta}=\gamma\beta(u\cdot v)\). It is, in fact, a common practice to add scalar multipliers to enrich the expressiveness of binary networks.

We compute the \(tpp\) for binary multiplication to compare it with full-precision computation. This requires estimating the optimal execution flow for the three avx-512 operators (xor, popcount, add) required to perform the update function. We assume to work on a modern CPU such as the Intel SkyLake architecture, equipped with \(512\)-bit registers and the popcount instruction that enables the computation of the popcount operation on \(512\)-bit registers. Recall that modern architectures have different execution units, each associated with a different port.5 Instructions employing different ports can be executed during the same clock cycle. Moreover, if an instruction uses \(k\) different ports, it is executed \(k\) times in the same clock cycle. In this architecture, xor and add work on port \(\{0,5\}\) (throughput \(2\)), while popcount works exclusively on port \(5\) (throughput \(1\)) [7]. Hence, even in a perfectly-pipelined execution flow, the computation of the update function in a single clock cycle, as done in the dense case using the fma operation, is unfeasible. This would be possible only if all the operations (xor, popcount, and add) were assigned to different ports. In the best-case scenario, we can perform \(2\) complete updates (\(6\) instructions, \(1024\) values) every \(3\) clock cycle (\(2\) instructions per clock cycle). The number of values processed per clock cycle, on average, is\(\frac{1024}{3}\simeq 341\). The same processor, equipped with avx-512 registers and the
fma instruction with throughput \(2\), can process \(\frac{512}{32}=16\) 32-bits floating point values per port, namely \(32\) floating point values per clock cycle. This means that the theoretical speedup offered by binary multiplication is \(10.5\times\). We show that our implementation of binary multiplication reaches the theoretical speedup compared to a state-of-the-art library for matrix multiplication, oneDNN.6 In practice, the empirical speedup over the equivalent single-precision MM can be up to \(16\times\) on rectangular matrices. In fact, it has been shown that dense multiplication does not reach the \(tpp\) on non-squared matrices [30], while our bitwise routine can. We will detail this aspect in our experimental evaluation, Section 5.3.

Footnote 6: [https://github.com/oneapi-src/oneDNN](https://github.com/oneapi-src/oneDNN)

**1/2**. This quantization schema achieves more accurate models than \(1/1\). State-of-the-art quantization approaches such as SLB [48] and EWGS [24] have shown that \(1/2\) delivers up to \(9\) points of Top1 accuracy gain on the Image Classification task on ImageNet compared to the \(1/1\) quantization. Despite the effectiveness improvements achieved, no efficient MM routines have been proposed for this configuration on CPU. In this regard, we design a novel matrix multiplication routine for \(1/2\) quantization that exploits bitwise operations as in the \(1/1\) case.

Let us consider a binary vector \(w\in\{\neg,\gamma\}^{n}\) and a 2-bits uniformly quantized vector \(a=\{a_{0},a_{1},a_{2},a_{3}\}^{n}\), with \(a_{p}=ps\), with \(p\in\{0,1,2,3\}\) and where \(s\) represents the distance between quantization levels and \(p\) is the index of the quantized value. We want to efficiently compute \(w\cdot a\) using logical and bitwise operators, as in Equation 17. \(a\) stores the activations of a neural model, so it is generally quantized to positive values (asymmetric quantization7). This is due to the choice of the ReLU as an activation function, which zeros-out negative inputs. For the purpose of our technique, it is useful to re-map \(a\) as a zero-centered symmetric distribution. We scale \(a\) by its mean. We obtain \(\bar{a}\) as

Footnote 7: [https://pytorch.org/blog/quantization-in-practice/](https://pytorch.org/blog/quantization-in-practice/)

\[\bar{a_{i}}=a_{i}-\mu=a_{i}-\frac{b_{0}+b_{3}}{2}=a_{i}-\frac{3}{2}s\]

\(\forall i=0,\ldots,n\). This converts the computation of \(w\cdot a\) into

\[w\cdot a=\sum_{i=1}^{n}w_{i}(\bar{a}_{i}+\mu)=\sum_{i=1}^{n}w_{i}\bar{a}_{i}+ \sum_{i=1}^{n}w_{i}\mu.\]

The last term is the sum along the rows of the matrix weight scaled by \(\mu\). Thus, it can be computed _offline_, before the matrix multiplication starts, as both terms are known a priori. Let us consider the update function \(c\gets c\ +ab\). We can initialize the matrix \(c\) with these pre-computed row-wise terms, rather than with zeros, to avoid any impact of this computation on the performance. Observe that \(\bar{a_{i}}\in\{\frac{3\pi}{2},\frac{s}{2},\frac{s}{2},\frac{3\pi}{2}\}\). Given that the multiplication by \(\gamma\) and \(s\) do not impact the performance, we need to multiply a \(\{\neg,1,1\}\) binary vector against a 2-bits vector whose value are in \(\mathcal{D}_{a}=\{-\frac{3}{2},-\frac{1}{2},\frac{1}{2},\frac{3}{2}\}\). \(\mathcal{D}_{a}\) is composed of two pairs of zero-symmetric values, namely \(\{-\frac{3}{2},\frac{3}{2}\}\) and \(\{-\frac{1}{2},\frac{1}{2}\}\). This suggests the possibility to employ a tailored representation made of multiple binary vectors. Hence, we represent \(a\) using three binary vectors, computed using the transformation

\[T:\{\mathcal{D}_{a}\}^{n}\rightarrow\{0,1\}^{n\times 3} \tag{18}\] \[a\rightarrow\{t,h,m\}\]

The definition of \(T\) is reported in Table I. Observe that, from now on, we will represent binary vectors using \(\{0,1\}\) instead of \(\{-1,1\}\). This will ease the explanation of our approach and it is also coherent on how binary vectors are actually represented during computation. The transformation \(T\) generates two binary vectors \(t,h\), and a binary mask \(m\). The names of the vectors suggest that \(t\) (three halves) refers to \(\{-\frac{3}{2},\frac{3}{2}\}\), while \(h\) (one \(h\)alf) refers to \(\{-\frac{1}{2},\frac{1}{2}\}\).

Two binary vectors, i.e., \(t\) and \(h\), are built according to the following rules.

* a bit set to \(1\) uniquely identifies the entry of the original vector \(a\). This means that if \(t_{i}=1\Rightarrow a_{i}=\frac{3}{2}\) and if \(h_{i}=1\Rightarrow a_{i}=\frac{1}{2}\).
* a bit set to \(0\) is intentionally ambiguous : \(t_{i}=0\Rightarrow a_{i}\in\{-\frac{3}{2},-\frac{1}{2},\frac{1}{2}\}\), \(h_{i}=0\Rightarrow a_{i}\in\{\frac{3}{2},-\frac{1}{2},\frac{3}{2}\}\).

The mask \(m\) is introduced to determine the uncertain entries of \(t\) and \(h\). We have that

* \(m_{i}=1\wedge t_{i}=0\Rightarrow a_{i}=\neg 3/2\);
* \(m_{i}=0\wedge h_{i}=0\Rightarrow a_{i}=\neg 1/2\).

To ease the notation, we define the set of _active entries_ for \(t\) as \(E_{t}=\{i\mid m_{i}=1\}\). The set of active entries for \(h\) is \(E_{h}=\{i\mid\bar{m}_{i}=1\}\). We highlight that \(T\) is a bijective function that permits uniquely matching \(a\) with its ternary representation \(\{t,h,m\}\). By using the mask \(m\), we introduce a memory overhead of one bit per each value of \(a\). On the other hand, \(m\) is necessary to split the \(1/2\) multiplication into two \(1/1\) multiplications. The idea is to multiply the binary vector \(w\) with \(t\) and \(h\) respectively by involving in the computation only the active entries identified by the mask \(m\). The considerations above inherently define a ternary operator \(\texttt{mbm}(x,y,z)\), which we define on two generic binary vectors \(x,y\), and a binary mask \(z\). In this case, \(x\) plays the role of the weight vector, \(y\) represents one between \(t\) and \(h\), while \(z\) is the mask corresponding to \(y\), i.e., \(m\) and \(\bar{m}\) when masking \(t\) and \(h\), respectively. The set of active entries of \(y\) is \(E_{y}=\{i\mid z_{i}=1\}\).

We now discuss how to implement the mbm operator. First, let us focus on Equation 17. It works by subtracting from the total number of bits involved in the computation, i.e., \(n\), the number of times the bits of \(u\) and \(v\) present different values, multiplied by \(2\). Our approach to compute \(\texttt{mbm}(x,y,z)\) is to multiply \(x\) and \(y\) using Equation 17, and then exploit the mask \(z\) to fix the result. The are two corrections to be applied. The first one is on the number of bits actually involved in the computation that is identified by the cardinality of the active entries: \(|E_{y}|=\sum_{i}z_{i}\), which can be computed with a \(\texttt{popcount}(z)\). Now we need to count how many times \(x\) and \(y\) present different values in correspondence of active entries. First, we compute \(d=\texttt{xor}(x,y)\)
\(d_{i}=1\) implies \(x_{i}\neq y_{i}\). These entries should be taken into account only if \(z_{i}=1\), i.e., only in correspondence with active entries of the vector \(y\). This can be computed with \(\mathtt{popcount}(\mathtt{and}(\mathtt{xor}(x,y),z))\).

Given these considerations, \(\mathtt{mbm}\) is defined as

\[\mathtt{mbm}(x,y,z)=\mathtt{popcount}(z)-\\ 2\ \mathtt{popcount}(\mathtt{and}(\mathtt{xor}(x,y),z)). \tag{19}\]

The overall \(1/2\) routine based on bitwise operations consists in applying the \(\mathtt{mbm}\) routine twice, first on the active entries of \(t\) identified by \(m\), and second on the active entries of \(h\) identified by \(\bar{m}\).

\[w\cdot a=\mathtt{mbm}(w,t,m)+\mathtt{mbm}(w,h,\bar{m}). \tag{20}\]

As mentioned in Section 4, \(\mathtt{mbm}\) can be efficiently implemented using the \(\mathtt{\_mm}\mathtt{512\_ternarylogic\_epi64}\) instruction. This instruction allows to compute any ternary logic function, i.e., any logic function with three inputs, and has the same latency and throughput of the \(\mathtt{xor}\) instruction. That said, the cost of executing the \(\mathtt{mbm}\) operator is the same as the one of computing Equation 17. \(\mathtt{popcount}(z)\) is the sum along the columns of a bit matrix. It runs in \(\Theta(n^{2})\) and its cost is negligible with respect to the cost of matrix multiplication, i.e., \(\Theta(n^{3})\). For this reason, we estimate that \(tpp_{1/2}\simeq 2\,tpp_{1/1}\).

**2/2.** This quantization schema almost fills the effectiveness gap with full-precision networks [24, 48, 51, 9]. In this quantization schema, both the weight vector and the activation vector are \(2\)-bit quantized vectors. The naive \(2/2\) MM can be obtained via an up-cast to \(8/8\) that allows the use of the \(\mathtt{fma}\) instruction as discussed before.

We design an efficient alternative solution, where both the weights and the activations are decomposed into three binary vectors. The first step is to zero-center the two vectors by following the procedure described for the \(1/2\) case. By doing that, we obtain the following \(w\) and \(a\).

\[w=s_{w}\{\cdot\frac{3}{2},\cdot\frac{1}{2},\frac{1}{2},\frac{3}{2}\},\quad a=s _{a}\{\frac{3}{2},\cdot\frac{1}{2},\frac{1}{2},\frac{3}{2}\}\]

where \(s_{w}\) and \(s_{a}\) represent the quantization step for \(w\) and \(a\), respectively. The idea behind the \(2/2\) routine is to apply the \(1/2\) routine twice. Thus, the transformation \(T\) is applied to both \(w\) and \(a\). We obtain \(T(w)=\{t_{w},h_{w},m_{w}\}\) and \(T(a)=\{t_{a},h_{a},m_{a}\}\). The multiplication is decomposed as follows

* the active entries of \(t_{w}\) identified by \(m_{w}\), are multiplied by the active entries of \(t_{a}\), identified by \(m_{a}\);
* the active entries of \(t_{w}\), identified by \(m_{w}\), are multiplied by the active entries of \(h_{a}\), identified by \(\bar{m}_{a}\);
* the active entries of \(h_{w}\), identified by \(\bar{m}_{w}\), are multiplied by the active entries of \(t_{a}\), identified by \(m_{a}\);
* the active entries of \(h_{w}\), identified by \(\bar{m}_{w}\), are multiplied by the active entries of \(h_{a}\), identified by \(\bar{m}_{a}\).

Finally, the results are summed together. We observe that for each one of the four bullets above, we have two different sets of active entries. The actual set of active entries is given by their intersection. This is implemented by a logical and between the two masks involved. To summarize, our novel routine for \(2/2\) MM allows to compute \(w\cdot a\) as

\[w\cdot a= \mathtt{mbm}(t_{w},h_{w},m_{w}\wedge m_{a})+\] \[\mathtt{mbm}(t_{w},h_{a},m_{w}\wedge\bar{m}_{a})+\] \[\mathtt{mbm}(h_{w},t_{a},\bar{m}_{w}\wedge m_{a})+\] \[\mathtt{mbm}(h_{w},h_{a},\bar{m}_{w}\wedge\bar{m}_{a}).\]

Hence, \(tpp_{2/2}\simeq 4\,tpp_{1/2}\simeq 8\,tpp_{1/1}\).

## 5 Experimental Evaluation

In this section, we present our experimental evaluation of \(\mathtt{APB}\). Our empirical analysis consists of two different steps. In the former one (Section 5.2), we assess the memory compression capabilities of \(\mathtt{APB}\) by comparing it against i) pruning, ii) quantization, and iii) pruning combined with quantization/binarization. In the latter (Section 5.3), we perform an efficiency analysis by evaluating the performance of our novel low-bits matrix multiplication algorithms against existing solutions on CPU. Moreover, we leverage our routines to compare the execution time of neural networks at different bit-width and compare them to \(\mathtt{APB}\)-compressed models.

### _Experimental Setup_

**Datasets.** We comprehensively evaluate \(\mathtt{APB}\) against several state-of-the-art competitors on two widely adopted datasets for Image Classification, namely CIFAR-10 [22] and ImageNet [5]. The CIFAR-10 dataset consists of a set of \(60\)K \(32\times 32\) images split into \(50\)K and \(10\)K training/test samples respectively, labeled with \(10\) classes. The ImageNet dataset consists of \(1.2\)M training images and about \(50\)K test images labeled with \(1\),\(000\) classes.

**Network Architectures**. We evaluate the performance of \(\mathtt{APB}\) in compressing ResNet-18/20/56 [16] and VGG-Small [39] on the CIFAR-10 dataset, and ResNet-18/34/50 and WideResNet-50 on ImageNet, which are the benchmark architectures for quantization methods. We apply \(\mathtt{APB}\) on all the layers of the networks, except the first the last, and the downsampling layers, unless differently specified. The effectiveness of the models is measured in terms of Top1 classification accuracy.

**Training Details**. We apply \(\mathtt{APB}\) after initializing the network weights with pre-trained models. For each layer \(i\), we set \(\alpha_{i}=\mu_{i}\) and \(\delta_{i}=3\sigma_{i}\), where \(\mu_{i}\) is the mean of \(|w_{i}|\) and \(\sigma_{i}\) is the standard deviation of \(w_{i}\). Under the assumption that \(w_{i}\sim\mathcal{N}(\mu,\,\sigma^{2})\)[13], this ensure high compression rate at initialization. We employ an SGD optimizer with cosine annealing learning rate scheduler. On CIFAR-10, we train for \(500\) epochs, with a batch size of \(128\) and a learning rate of \(1\)e-\(2\). On ImageNet, we train for \(100\) epochs when using real-values activations and \(150\) for \(2\)-bits activations, with batch size \(256\) and learning rate \(1\)e-\(3\). We freeze the value of \(\alpha\) and \(\delta\) when reaching half of the epochs of each training to allow fine-tuning the surviving values. \(\mathtt{APB}\) is implemented in PyTorch [32].

**Weight Decay**. The compression aggressiveness of \(\mathtt{APB}\) can be controlled with the weight decay \(\lambda\), namely the \(L_{2}\) penalty applied on the weights of a neural network at
training time. The larger the weight decay, the lower (on average) the absolute value of the trained parameters. As we do not apply the weight decay to \(\alpha\) and \(\delta\), the amplitude of the binarization interval is not directly affected by its value. As a consequence, increasing \(\lambda\) will force more weights to fall into the binarization interval, thus incrementing the percentage of binary values w.r.t to full-precision ones and delivering higher compression.

### _Compression Performance of_Apb _with 32-bits activations_

In this section, we evaluate the performance of APB as a memory compression framework. For this purpose, we consider models whose activations are kept in \(32\)-bits. Recall that, even when weights are binarized, maintaining activations in full precision prevents any inference advantage, as discussed in Section 4, paragraph "\(1/32\)".

We compare APB against several different techniques, namely i) quantization, ii) pruning, iii) combination of pruning and binarization. Compression methods adopt two main approaches. The first one, which is adopted for example by pruning techniques, compresses all the network layers. We call these methods _All-Layers Compressors_ (AL). The second one, which is adopted by binarization, low-bits quantization, and pruning + binarization, works by leaving in full precision the first, the last, and the downsample layers if any. We name these solutions _Convolutional Layers Compressors_ (CL).

As already mentioned in Section 5.1, APB belongs to CL methods. Despite that, in our experiments, we compare it against methods belonging to both families. To ease the comparison, we report the results in two different tables. Table II reports the results for AL methods, while Table III reports the results for CL methods. In Table II, we report the CL weights bit-width and the AL weight bit-width to allow a direct comparison between the two tables. Both for AL and CL, we measure the memory impact as average _weights bit width_, namely the ratio between the number of bits required to store a model and its parameters. When comparing against CL methods, the first, the last, and the downsample layers of the network are excluded from the computation, while these are included when comparing against AL methods.
on CIFAR-10 and ImageNet and report the results in Table 2. All the low-bits quantization techniques belong to the CL category described above. For each quantization scheme, we report the best-performing competitor. The state-of-the-art on CIFAR-10 is SLB [48] for all quantization schemes except the \(1/32\) case on ResNet-20, where the best performance is achieved by IR-Net [33]. On ImageNet, the state-of-the-art solution is EWGS [24] for both ResNet-18 and ResNet-34.

Results show that APB outperforms all other state-of-the-art quantization approaches by providing a superior solution in terms of space and accuracy. Indeed, APB achieves higher accuracy than the \(2\)-bits quantization while saving up to \(2\times\) memory on CIFAR-10 and \(1.4\times\) on ImageNet. In comparison to \(1\)-bit quantization, APB presents a negligible memory overhead on CIFAR-10. In fact, the surviving full-precision values for these models account for about \(0.1\%\) of the total weights. On ImageNet, the number of surviving full-precision weights required by APB is larger, as demonstrated by the \(0.4\) more bits per weight. However, this memory overhead is strongly counterbalanced by \(1.9\) and \(1.0\) Top1 accuracy points of gain for ResNet-18 and ResNet-34 respectively, compared to \(1\)-bit quantization. On ResNet-18, our models even outperform \(2\)-bits quantization by \(0.4\) Top1 accuracy points, despite its reduced memory footprint.

**Comparison with combinations of pruning and binarization.** Recently, some works explore the combination of pruning & binarization [25, 6]. These methods adopt an orthogonal approach with respect to APB. While with APB weights are either binary or full-precision, with existing approaches combining pruning and binarization, the weights are either binary or _zero_. We compare the performance of APB with respect to Multi Prize Ticket (MPT) [6] in terms of memory compression. MPT works by discovering highly effective sparse sub-networks in sufficiently over-parameterized neural networks, without the need for further training. Moreover, they apply the _sign_ function to binarize the surviving weights, thus providing a sparse and binary network.

We argue that sparsification does not provide memory advantages if the non-zero weights are binary. Consider a tensor of size \(n\) with \(nnz\) non-zero entries. The cost of storing a non-zero entry is given by the number of bits to its value \(b_{v}\) plus the number of bits to save its position \(b_{p}\). In this case, i.e., \(b_{v}=1\), as surviving weights are binarized. In MPT [6], the authors propose a sparsification ratio of \(80\%\) for this architecture. This means that one weight out of five is stored. Storing \(5\) weights in pure binary format only costs \(5\) bits, so if \(b_{p}>4\), the sparse-binary format does not provide memory footprint advantages compared to the pure binary one. Due to the large size of network layers, \(4\) bits are not enough to store the indexes of non-zero values. A more memory-efficient approach would be to store one bit for every weight to indicate whether the corresponding entry is pruned. In this case, the total memory impact would be \(n+nnz\), which we approximate with \(n\) for simplicity.

Table 2 reports the evaluation results of APB against MPT. APB outperforms the performance of MPT both on ResNet-18 for CIFAR-10, and for WideResNet-50 on ImageNet. For ResNet-18 in CIFAR-10, APB matches the memory compression of MPT and also delivers a slight (\(0.2\)) effectiveness improvement. For WideResNet-50 on ImageNet, the model learned with APB achieves more than \(3\) points of Top1 Accuracy improvement w.r.t. MPT. with a memory overhead of only \(0.1\) bits per weight.

#### 5.2.2 Compression of All Layers (AL)

We now compare APB to methods compressing the whole network. These approaches also tackle the first, the last, and the downsample convolutional layers. Batch Normalization layers are left in full precision as their impact is negligible, as they account for less than \(0.5\%\) of network parameters in all the evaluated architectures.

Table 2 shows that the impact of the layers left out by CL approaches can be considerable. On ImageNet, we observe that the non-compressed parameters account for an overhead of \(2\) bits per weight for ResNet-18 and \(6.2\) for ResNet-50. These numbers are obtained as the difference between the CL and the AL value in the "Weights Bit width CL (AL)" column of Table 2. On ResNet-18, we observe that the \(70\%\) of the overhead depends on the last fully-connected layer. We experimentally verify that these last fully-connected layers can be quantized to 8-bit integers with a simple post-training quantization (PTQ) solution, that converts the weights from a floating point 32 representation to 8-bit integers, 8 without harming the Top1 accuracy of the model. This halves the overhead of the non-compressed layers, reducing it to \(0.9\) bits per weight. For ResNet-50, quantizing the last fully-connected layer to 8-bits allows reducing the overhead to \(4.3\) bit per weight.

Footnote 8: [https://pytorch.org/docs/stable/quantization.html](https://pytorch.org/docs/stable/quantization.html)

**Comparison with pruning.** We compare the memory compression capabilities of APB against state-of-the-art pruning techniques, namely magnitude-based pruning with learning-rate rewinding [36]. We report the results of this comparison in Table 3. In the original article [36], the authors express the compression rate as the inverse of the sparsity ratio, i.e., \(5\%\) sparsity corresponds to \(20\times\) compression rate. Indeed, this does not account for the space required to store the indexes of the nonzero entries. We recompute the weights bit width by taking it into account. We compare APB against pruning on ResNet-20, ResNet-56 on CIFAR-10, and on ResNet-50 on ImageNet. On CIFAR-10, APB vastly outperforms pruning in terms of memory/accuracy trade-off. When compressing ResNet-20, APB delivers a model which is \(3.4\times\) smaller but \(1.2\) points more accurate. Regarding ResNet-56, APB can deliver the same level of accuracy of a sparsified model with a \(3.3\times\) memory saving, or the same level of memory compression with \(1.7\) points of Top1 accuracy improvement. On ImageNet, we observe that APB combined with Post Training Quantization (PTQ) on the last layer allows matching the performance of pruning using ResNet-50 as the backbone. Furthermore, we perform an experiment where we apply APB on the downsample convolutional layer of this architecture, marked with \(\dagger\) in Table 3. For this model, the downsample convolutional layers account for the \(10\%\) of the total memory impact. Thus, we can obtain a model which only suffers from \(0.5\) accuracy degradation compared to pruning, but allows saving up to \(2.5\times\) memory footprint.
**Comparison with combinations of pruning and quantization.** We compare with Bayesian Bits (BB) [42], a state-of-the-art compression approach mixing quantization at different bit widths with pruning. In detail, we compare in terms of bit width of their methods against our models learned with APB. As for the other experiments, we use ResNet-18 on Imagenet as the model for comparison and report the results in Table III. As mentioned, we also apply PTQ on the final fully-connected layer of ResNet18. Compared to BB, APB can deliver the same accuracy but allows us to save up \(2.1\times\). For the sake of fairness, we point out that activations in BB are quantized to mixed precision (\(2/4\) bits), which can decrease the accuracy of the model. We also compare to a model compressed with APB whose activations are quantized to 2-bits, marked with the \(\ddagger\) symbol in Table III. Even if employing reduced bit width for activations, this model gains a \(1.5\times\) memory savings with reduced performance degradation.

### _Efficiency Evaluation_

In this Section, we provide an extensive efficiency evaluation of our low-bits matrix multiplication routines and of APB-compressed networks. First, we assess the performance of our low-bits matrix multiplication routines, comparing them against state-of-the-art dense matrix multiplication frameworks. Then, we show that our APB compressed networks outperform existing quantization methods in terms of efficiency-accuracy trade-off.

**Low-bits Matrix Multiplication.** We now compare the efficiency of our Matrix Multiplication (MM) routines, i.e., \(1/1,1/2\), \(2/2\), against state-of-the-art highly-optimized MM CPU libraries. In detail, we measure the achieved GFLOPs at different sizes of the matrices and we compare them to their \(tpp\). \(tpp\) is computed according to Equation 14 and it is reported in Figure 4 as dotted lines. Experiments are conducted on an Intel Xeon Gold 5318Y CPU, clocked at \(3.4\) GHz and equipped with the AVX-512 instruction set. Our novel inference routines are written in C++ and compiled with the -O3 option using GCC 11.2.0, with single-threaded execution. Figure 4 reports an experimental comparison of square matrices. We report the size of the matrices under multiplication, i.e., on the \(x\)-axis. We include in the comparison the performance of dense matrix multiplication as implemented in the oneDNN library. This library is the state-of-the-art solution for dense multiplication, employing industrial-level optimizations such as Just In Time (JIT) code compilation. Figure 4 shows how large-enough matrices allow all the tested algorithms, i.e., our bitwiese MM routines and the oneDNN library for dense MM, to achieve their best performance and get close to their \(tpp\). The experimental results show that the \(1/1\), \(1/2\), and \(2/2\) routines reach up to \(93\%\), \(88\%\), and \(85\%\) of their theoretical peak performance, respectively. For large enough matrices, oneDNN GFLOPs range between the \(86\%\) and the \(90\%\) of the \(tpp\) of dense MM. The results reported in Figure 4 confirm that our bitwise multiplication routines are properly implemented, given that the gap with \(tpp\) is within the \(15\%\). Further optimizations may be employed, such as blocking strategies or micro-kernel parameters optimization according to the CPU architecture. These are optimization strategies that are commonly applied in the dense case [10]. Although interesting, these optimizations go beyond the scope of this work and we leave their investigation as future work.

Square matrices allow MM routines to get close to their \(tpp\) at any bit width. Indeed, in DNN inference, rectangular matrices are much more common than squared ones. To evaluate the efficiency of our novel matrix multiplication algorithms on a real use case, we test them on the shapes obtained by applying the _im2col_ transformation on the layers of ResNet-like architectures. We include the \(8/8\) MM in our analysis by employing the implementation provided by the FBGEMM [21] library.

Results are reported in Figure 5, where the \(x\)-axis indicates the shapes of the matrices under evaluation and the \(y\)-axis reports the speedup compared to the dense MM. We adopt two different implementations for dense MM. The first one is OneDNN, as in Figure 4. BLIS [20] is an open-source GEMM library, with assembly-level architecture-tailored optimizations, and presents an optimization level closer to our C++ implementation. Observe that, on the shapes under evaluation, oneDNN is 60% faster than BLIS, which is a remarkable speedup considering that they employ the same algorithm for matrix multiplication.

Figure 5 shows that our novel MM routines are significantly faster than dense multiplication. For example, the \(1/1\) routine delivers up to \(15\times\) speedup compared to oneDNN and up to \(25\times\) compared to BLIS. In this case, we witness to a speedup larger than the theoretical estimated one, i.e., \(10.5\times\). This is caused by the poor performance of dense MM on rectangular-shaped matrices [30]. In fact, rectangular matrices do not allow fully masking the memory-bounded nature of the MM operation.9 This is evident in Figure 5, where, on the shapes under evaluation, oneDNN reaches between the \(50\%\) and the \(70\%\) of \(tpp\). Conversely, \(1/1\), \(1/2\) and \(2/2\) deliver respectively at least the \(85\%\), \(80\%\) and \(75\%\) of their theoretical peak performance.

Footnote 9: [https://en.algorithmica.org/hpc/algorithms/matmul/](https://en.algorithmica.org/hpc/algorithms/matmul/)

Overall, the forward pass on ResNet-18 achieves \(6.85\times\) and \(1.5\times\) speedup compared to FBGEMM, the state-of-the-art MM library for quantized models on CPU, when employing the \(1/2\) and \(2/2\) MM routines, respectively. To the best of our knowledge, FBGEMM is the best available solution for MM at low bit widths, excluding our novel routines. Our solution allows, for the first time, to efficiently

Fig. 4: GFLOPs performance analysis of our novel bitwise MM routines against the oneDNN library used for dense MM. The analysis is performed on square matrices, i.e., \(M\) = \(K\) = \(N\), where \(M\) is the number of rows of the first operand, \(K\) is the shared dimension, and \(N\) is the number of columns of the second operand.
exploit the plethora of different quantization methods for \(1/2\) and \(2/2\) directly on CPU. Also, the involved algorithms are novel and can be implemented on all the available computing platforms, such as GPUs or FPGAs. We also observe that the performance ratio between different low-bits implementation precisely matches the predictions we made in Section 4: the \(1/2\) quantization schema is \(2\times\) slower than the \(1/1\), while the \(2/2\) is \(8\times\) slower.

**Efficiency/Accuracy Trade-offs.** We now compare the efficiency-accuracy trade-off that different low-bits quantization methods deliver compared to APB. We perform the analysis using the ResNet-18 architecture on the ImageNet dataset. We compute the inference time as the total MM time. The time spent on the first and the last layer, as well as on the batch-normalization and down-sampling layers, is ignored as these layers are not compressed in any of the approaches -- as in Table II. The comparison involves networks quantized with the \(1/1\), \(1/2\), and \(2/2\) configurations. The \(1/1\) accuracy is achieved by using SA-BNN [27], while \(1/2\) and \(2/2\) are achieved by using EWGS [24]. Both methods are state-of-the-art low-bits quantization approaches. We do not include methods such as ReActNet [28] or ElasticLink [18] that leverage custom architectures. We observe that APB could be easily employed in conjunction with these approaches, which is also part of our future work. In this analysis, APB is used to compress the weights of the network, while activations are quantized to \(2\) bits using the EWGS quantizer. We also experiment APB with \(1\)-bit quantized activations and we achieved worse performance than \(1/2\) quantization. This aspect is detailed in the subsequent paragraph.

The inference time reported for APB is the sum of the time for \(1/2\) MM and the time for sparse-dense MM, as shown in Equation 13. LIBXSMM [17] is used for sparse-dense matrix multiplication.

Figure 6 reports the results of the comparison in terms of speedup w.r.t. both BLIS and oneDNN (\(x\)-axis) and Top1 accuracy (\(y\)-axis). APB largely dominates over \(2/2\) quantization provided by EWGS. Our models can deliver \(1.9\times\) speedup with \(0.7\) Top1 accuracy improvement or \(2.0\times\) speedup at the same accuracy level of the \(2/2\) quantized model. The sparsity of surviving full-precision values per layer spans in \(96\)-\(99\%\). In this range, we experimentally verify that sparse MM is always faster than the \(2/2\) routine, it matches the performance of \(1/2\) at about \(97\%\), and it is faster than \(1/1\) at \(99\%\). Moreover, thanks to our novel MM routine, the \(1/2\) models offer more than \(11\times\) speedup with a performance degradation of \(8\%\) w.r.t. to the full-precision model. Regarding \(1/1\), its extreme speedup asks for a significant accuracy degradation, i.e., \(7\) points of Top1 accuracy compared to the original full-precision model.

**Comparison with pure binarization.** We experiment with the combination of APB with \(1\)-bit activations. We observe that the performance of our models outperforms by margin pure binary networks, by reaching \(63.0\) of Top1 accuracy. Anyway, this effectiveness improvement comes at the price of an elevated number of surviving full-precision weights, reaching \(8\%\) in some cases. The cost of these full-precision weights at inference time matches or sometimes surpasses the cost of binary multiplication. In practice, this means that the \(1/2\) scenario offers superior performance in terms of efficiency/accuracy tradeoff. This is coherent with the network quantization literature, where different works prove that reducing the precision of the activation worsens the effectiveness of the models more than reducing the precision of the weights

## 6 Conclusions and Future Work

We proposed APB, a novel compression technique that merges binarization and pruning together to exploit the benefits provided by these two orthogonal techniques. We showed that APB jointly maximizes the accuracy achieved by the network while minimizing its memory impact by identifying an optimal partition of the network parameters among these two sets. Furthermore, we presented two novel matrix multiplication algorithms for extreme low-bits configurations, namely \(1/2\) and \(2/2\), where \(1/2\) refers to binary weights and \(2\)-bits activations, while in the \(2/2\) configuration both weights and activations are quantized to \(2\) bits. We performed a comprehensive experimental evaluation on two widely-adopted benchmark datasets, i.e., CIFAR-10 and ImageNet. Experiments show that APB

Fig. 5: Comparison between matrix multiplication routines at different quantization levels. \(M\) is the number of rows of the first operand, \(K\) is the shared dimension, and \(N\) is the number of columns of the second operand.

Fig. 6: Efficiency/Accuracy trade-offs of ResNet-18 on ImageNet using different quantization schema.
achieves better accuracy/memory trade-off w.r.t. to state-of-the-art compression methods based on i) quantization, ii) pruning, iii) the combination of pruning and quantization. Our novel matrix multiplication routines deliver a major speedup compared to the existing solution for low-bits matrix multiplication on CPU, ranging from \(6.9\times\) for the \(1/2\) configuration to \(1.5\times\) for the \(2/2\) configuration. Moreover, the experimental results show that APB is \(2\times\) faster than the 2-bit quantized model with no loss in accuracy. On the one hand, our novel matrix algorithms open up to exploiting quantized networks on CPU. Also, they may boost the investigation of \(1/2\) quantization scenario, given that a very fast inference engine is available. On the other hand, we show that APB-compressed networks, where binary and full-precision weights are mixed in the same weight tensor, allow for better performance compared to fixed quantization, e.g., \(2\)-bits. The importance of a reduced portion of full-precision weights, evidenced by pruning techniques in previous work, is stressed again in this new hybrid format.

As future work, we plan to extend APB to automatically identify the optimal quantization schema for each layer activation, so as to improve the efficiency/accuracy trade-off. This would be possible due to the availability of efficient matrix multiplication routines covering several quantization schemas. Moreover, we are interested in applying APB in conjunction with highly-effective custom architectures, such as ElasticLink [18].
