A LTERNATING M ULTI - BIT Q UANTIZATION FOR R E - CURRENT N EURAL N ETWORKS
Anonymous authors
Paper under double-blind review
A BSTRACT
Recurrent neural networks have achieved excellent performance in many applications.
However, on portable devices with limited resources, the models are often too large to deploy.
For applications on the server with large scale concurrent requests, the latency during inference can also be very critical for costly computing resources.
In this work, we address these problems by quantizing the network, both weights and activations, into multiple binary codes {− 1 , +1 } .
We formulate the quantization as an optimization problem.
Under the key observation that once the quantization coefﬁcients are ﬁxed the binary codes can be derived efﬁciently by binary search tree, alternating minimization is then applied.
We test the quantization for two well-known RNNs, i.e., long short term memory (LSTM) and gate recurrent unit (GRU), on the language models.
Compared with the full-precision counter part, by 2 -bit quantization we can achieve ∼ 16 × memory saving and potential ∼ 14 × inference acceleration on CPUs, with only a reasonable loss in the accuracy.
By 3 -bit quantization, we can achieve almost no loss in the accuracy or even surpass the original model, with ∼ 10 .
5 × memory saving and potential ∼ 6 .
5 × inference acceleration.
Both results beat the exiting quantization works with large margins.
I NTRODUCTION
Recurrent neural networks (RNNs) are speciﬁc type of neural networks which are designed to model the sequence data.
In last decades, various RNN architectures have been proposed, such as LongShort-Term Memory (LSTM) (Hochreiter & Schmidhuber, 1997) and Gated Recurrent Units Cho et al.
They have enabled the RNNs to achieve state-of-art performance in many applications, e.g., language models (Mikolov et al., 2010), neural machine translation (Sutskever et al., 2014;
Wu et al., 2016), automatic speech recognition (Graves et al., 2013), image captions (Vinyals et al., 2015), etc.
However, the models often build on high dimensional input/output,e.g., large vocabulary in language models, or very deep inner recurrent networks, making the models have too many parameters to deploy on portable devices with limited resources.
In addition, RNNs can only be executed sequentially with dependence on current hidden states.
This causes large latency during inference.
For applications in the server with large scale concurrent requests, e.g., on-line machine translation and speech recognition, large latency leads to limited requests processed per machine to meet the stringent response time requirements.
Thus much more costly computing resources are in demand for RNN based models.
To alleviate the above problems, several techniques can be employed, i.e., low rank approximation (Sainath et al., 2013; Jaderberg et al., 2014; Lebedev et al., 2014;
Tai et al., 2016), sparsity (Liu et al., 2015; Han et al., 2015; 2016; Wen et al., 2016), and quantization.
All of them are build on the redundancy of current networks and can be combined.
In this work, we mainly focus on quantization based methods.
More precisely, we are to quantize all parameters into multiple binary codes {− 1 , +1 } .
The idea of quantizing both weights and activations is ﬁrstly proposed by (Hubara et al., 2016a).
It has shown that even 1 -bit binarization can achieve reasonably good performance in some visual classiﬁcation tasks.
Compared with the full precision counterpart, binary weights reduce the memory by a factor of 32 .
And the costly arithmetic operations between weights and activations can then be replaced by cheap XNOR and bitcount operations(Hubara et al., 2016a), which potentially leads to much acceleration.
Rastegari et al.
(2016) further incorporate a real coefﬁcient to compensate for the binarization error.
They apply the method to the challenging ImageNet dataset and achieve better performance than pure binarization in (Hubara et al., 2016a).
However, it is still of large gap compared with the full precision networks.
To bridge this gap, some recent works (Hubara et al., 2016b; Zhou et al., 2016; 2017) further employ quantization with more bits and achieve plausible performance.
Meanwhile, quite an amount of works, e.g., (Courbariaux et al., 2015; Li et al., 2016;
Zhu et al., 2017; Guo et al., 2017), quantize the weights only.
Although much memory saving can be achieved, the acceleration is very limited in modern computing devices (Rastegari et al., 2016).
Among all existing quantization works, most of them focus on convolutional neural networks (CNNs) while pay less attention to RNNs.
As mentioned earlier, the latter is also very demanding.
Recently, (Hou et al., 2017) showed that binarized LSTM with preconditioned coefﬁcients can achieve promising performance in some easy tasks such as predicting the next character.
However, for RNNs with large input/output, e.g., large vocabulary in language models, it is still very challenging for quantization.
Both works of Hubara et al.
(2016b) and Zhou et al.
(2017) test the effectiveness of their multi-bit quantized RNNs to predict the next word.
Although using up to 4 -bits, the results with quantization still have noticeable gap with those with full precision.
This motivates us to ﬁnd a better method to quantize RNNs.
The main contribution of this work is as follows:
(a) We formulate the multi-bit quantization as an optimization problem.
The binary codes {− 1 , +1 } are learned instead of rule-based.
For the ﬁrst time, we observe that the codes can be derived by the binary search tree once the coefﬁcients are knowns in advance, see, e.g., Algorithm 1.
Thus the whole optimization is eased by removing the discrete unknowns, which are very difﬁcult to handle.
(b) We propose to use alternating minimization to tackle the quantization problem.
By separating the binary codes and real coefﬁcients into two parts, we can solve the subproblem efﬁciently when one part is ﬁxed.
With properly initialization, we only need two alternating cycles to get high precision approximation, which is effective enough to even quantize the activations on-line.
(c) We systematically evaluate the effectiveness of our alternating quantization on language models.
Two well-known RNN structures,
i
e., LSTM and GRU, are tested with different quantization bits.
Compared with the full-precision counterpart, by 2 -bit quantization we can achieve ∼ 16 × memory saving and potential ∼ 14 × inference acceleration on CPUs, with a reasonable loss on the accuracy.
By 3 -quantization, we can achieve almost no loss in accuracy or even surpass the original model with ∼ 10 .
5 × memory saving and potential ∼ 6 .
5 × inference acceleration.
Both results beat the exiting quantization works with large margins.
E XISTING M ULTI - BIT Q UANTIZATION M ETHODS
Before introducing our proposed multi-bit quantization, we ﬁrst summarize existing works as follows: a
Uniform
quantization method (
Rastegari
et al.,
Hubara
et al., 2016b
) ﬁrstly scales its value in the range x ∈ [ − 1 , 1] .
Then it adopts the following k -bit quantization:
[Equation]
after which the method scales back to the original range.
Such quantization is rule based thus is very easy to implement.
The intrinsic beneﬁt is that when computing inner product of two quantized vectors, it can employ cheap bit shift and count operations to replace costly multiplications and additions operations.
However, the method can be far from optimum when quantizing non
-uniform data, which is believed to be the trained weights and activations of deep neural network (Zhou et al., 2017).
(b) Balanced quantization (Zhou et al., 2017) alleviates the drawbacks of the uniform quantization by ﬁrstly equalizing the data.
The method constructs 2 k intervals which contain roughly the same percentage of the data.
Then it linearly maps the center of each interval to the corresponding quantization code in (1).
Although sounding more reasonable than the uniform one, the afﬁne transform on the centers can still be suboptimal.
In addition, there is no guarantee that the evenly spaced partition is more suitable if compared with the non-evenly spaced partition for a speciﬁc data distribution.
Quantize to [--αα 
-][Figure] α
[Figure]
Quantize to Quantize
[Figure]
Quantize to α 1 -α 2 -α 1 +αα [-
-]1 +α
[Figure] α [Figure]
And the partition intervals are optimally separated by the middle points of adjacent quantization codes,
, − and
(c) Greedy approximation (Guo et al., 2017) instead tries to learn the quantization by tackling the following problem:
[Equation]
, with b i ∈ {− 1 , +1 } n .
For k = 1 , the above problem has a closed-form solution (Rastegari et al., 2016).
Greedy approximation extends to k -bit ( k > 1 ) quantization by sequentially minimizing the residue.
That is
[Equation]
with r
[Equation]
w
[Equation]
Then the optimal solution is given as
[Equation]
and b
[Equation]
[Equation]
Greedy approximation is very efﬁcient to implement in modern computing devices.
Although not able to reach a high precision solution, the formulation of minimizing quantization error is very promising.
(d) Reﬁned greedy approximation (Guo et al., 2017) extends to further decrease the quantization error.
In the j -th iteration after minimizing problem (3), the method adds one extra step to reﬁne all computed { α i } ji =1 with the least squares solution:
[Equation]
In experiments of quantizing the weights of CNN, the reﬁned approximation is veriﬁed to be better than the original greedy one.
However, as we will show later, the reﬁned method is still far from satisfactory for quantization accuracy.
O UR A LTERNATING M ULTI - BIT Q UANTIZATION
Now we introduce our quantization method.
We tackle the same minimization problem as (2).
For simplicity, we ﬁrstly consider the problem with k = 2 .
Suppose that α 1 and α 2 are known in advance with α 1 ≥ α 2 ≥ 0 , then the quantization codes are restricted to v = {− α 1 − α 2 , − α 1 + α 2 , α 1 − α 2 , α 1 + α 2 } .
For any entry w of w in problem (2), its quantization code is determined by the least distance to all codes.
Consequently, we can partition the number axis into 4 intervals.
And each interval corresponds to one particular quantization code.
The common point of two adjacent intervals then becomes the middle point of the two quantization codes, i.e., − α 1 , 0 , and α 1 .
Fig.
1 gives an illustration.
For the general k -bit quantization, suppose that { α i } ki =1 are known and we have all possible codes in ascending order, i.e., v = {− (cid:80) ki
=1 α i , . . .
, (cid:80) ki
[-=
-]=1 α i } .
Similarly, we can partition the number axis into 2 k intervals, in which the boundaries are determined by the centers of two adjacent codes in v , i.e., { ( v i + v i +1 ) / 2 } 2 k − 1 i =1 .
However, directly comparing per entry with all the boundaries needs 2 k comparisons, which is very inefﬁcient.
Instead, we can make use of the ascending property in v .
Hierarchically, we partition the codes of v evenly into two ordered sub-vectors, i.e., v 1: m/ 2 and v m/ 2+1: m with m deﬁned as the length of v . If w < ( v m/ 2 + v m/ 2+1 ) / 2 , its feasible codes is then
-α 1 -α-α 1 +αα 1 -αα
+α-αα
{ w is the real value to be quantized} { v is the vector of quantization codes in ascending order} m = length( v ) if m =
= 1 then return v end if w ( v m/ 2 + m/ 2+1 ) / 2 then
BST ( w, v m 2+1: m ) else
BST ( w, v m ) end
Require :
Full precision weight w ∈ R n , number of bits k , total iterations T =
Ensure optimally restricted to v 1: m/ 2 .
And if w ≥ ( v m/ 2 + v m/ 2+1 ) / 2 , its feasible code becomes v m/ 2+1: m .
By recursively evenly partition the ordered feasible codes, we can then efﬁciently determine the optimal code for per entry by only k comparisons.
The whole procedure is in fact a binary search tree.
We summarize it in Algorithm 1.
Note that once getting the quantization code, it is straightforward to map to the binary code b .
Also, by maintaining a mask vector with the same size as w to indicate the partitions, we could operate BST for all entries simultaneously.
To give a better illustration, we give a binary tree example for k = 2 in Fig. 2.
Note that for k = 2 , we can even derive the optimal codes by a closed form solution, i.e., b 1 = sign( w ) and b 2 = sign( w − α 1 b 1 ) with α 1 ≥ α 2 ≥ 0 .
Under the above observation, let us reconsider the reﬁned greedy approximation (Guo et al., 2017) introduced in Section 2.
After modiﬁcation on the computed { α i } j i =1 as (5), { b i } j i =2 are no longer optimal while the method keeps all of them ﬁxed.
To improve the reﬁned greedy approximation, alternating minimizing { α i } ki =1 and { b i } ki =1 becomes a natural choice.
Once getting { b i } ki =1 as described above, we can optimize { α i } ki =1 as (5).
In real experiments, we ﬁnd that by greedy initialization as (4), only 2 alternating cycles is good enough to ﬁnd high precision quantization.
For better illustration, we summarize our alternating minimization in Algorithm 2.
For updating { α i } ki =1 , we need k 2 n + kn operations, combining kn operations for determine the binary code. For total T
[Figure]
[Figure] [Figure]
[Figure] [Figure]
[Figure] [Figure]
[Figure]
-1 -  * ( + ) … … 1 -  … 0.
) ( + 0.15 0.
[Figure]
alternating cycles, we thus need 2 kn + T (2 kn + k 2 n ) operations to quantize w ∈ R n into k -bit, with the extra 2 kn corresponding to greedy initialization.
A PPLY A LTERNATING M ULTI - BIT Q UANTIZATION TO RNN S
Implementation .
We ﬁrstly introduce the implementation details for quantizing RNN.
For simplicity, we consider the one layer LSTM for language model.
The goal is to predict the next word indexed by t in a sequence of one-hot word tokens ( y ∗ 1 , . . .
, y ∗ N ) as follows:
[Equation]
where σ represents the activation function.
In the above formulation, the multiplication between the weight matrices and the vectors x t and h t occupy most of the computation.
This is also where we apply quantization to.
For the weight matrices, instead of on the whole, we quantize them row by row.
During the matrix vector product, we can ﬁrstly execute the binary multiplication.
Then element-wisely multiply the obtained binary vector with the high precision scaling coefﬁcients.
Thus little extra computation results while much more freedom is brought to better approximate the weights.
We give an illustration on the left part of Fig.
Due to one-hot word tokens, x t corresponds to one speciﬁc row in the quantized W e .
It needs no more quantization.
Different from the weight matrices, h t depends on the input, which needs to be quantized on-line during inference.
For consistent notation with existing work, e.g., (Hubara et al., 2016b; Zhou et al., 2017), we also call quantizing on h t as quantizing on activation.
For W ∈ R m × n and h t ∈ R n , the standard matrix-vector product needs mn operations.
Note that some modern CPUs can fuse the multiplication and addition as a single-cycle operation (Rastegari et al., 2016).
Thus only quantizing the weights will not deliver acceleration.
For the quantized product between k w -bit W and k h -bit h t , we have k w k h mn binary operations and 2 k 2 h n + 6 k h n + k w k h m non-binary operations, where 2 k 2 h n + 6 k h n corresponds to the cost of alternating approximation ( T = 2 ) and k w k h m corresponds to the ﬁnal product with coefﬁcients.
In the current generation of
CPUs, we can perform 64 binary operations in one clock of CPU (Rastegari et al., 2016).
Therefore the acceleration can be computed as γ =
[Equation]
Suppose that LSTM has hidden states n = 1024 , then we have W h ∈ R 4096 × 1024 .
The acceleration ratio becomes roughly 14 × for ( k h , k w ) =
(2 , 2) and 6 . 5 × for ( k h , k w ) =
As indicated in the left part of Fig.
3, the binary multiplication can be conducted sequentially by associativity.
Although the operation is suitable for parallel computing by synchronously conducting the multiplication, this needs extra effort for parallelization.
We instead concatenate the binary codes as shown in the right part of Fig.
Under such modiﬁcation, we are able to make full use of the much optimized inner parallel matrix multiplication, which gives the possibility for further acceleration.
The ﬁnal result is then obtained by adding all partitioned vectors together, which has little extra computation.
Training.
As ﬁrstly proposed by Courbariaux et al.
(2015), during the training of quantized neural network, directly adding the moderately small gradients to quantized weights will result in no change on it.
So they maintain a full precision weight to accumulate the gradients then apply quantization in every mini-batch.
In fact, the whole procedure can be mathematically formulated as a bi-level optimization (Colson et al., 2007) problem:
[Equation]
Denote the quantized weight as ˆ w = (cid:80) ki =
α i b i .
In the forward propagation, we derive ˆ w from the full precision w in the lower-level problem and apply it to the upper-level function f ( · ) , i.e., RNN in this paper.
During the backward propagation, the derivative ∂f∂ ˆ w is propagated back to w through the lower-level function.
Due to the discreteness of b i , it is very hard to model the implicit dependence of ˆ w on w .
So we also adopt the “straight-through estimate” as (Courbariaux et al., 2015), i.e., ∂f∂ w = ∂f∂ ˆ w .
To compute the derivative on the quantized hidden state h t , the same trick is applied.
During the training, we ﬁnd the same phenomenon as Hubara et al.
(2016b) that some entries of w can grow very large, which become outliers and harm the quantization.
Here we simply clip w in the range of [ − 1 , 1] .
E XPERIMENTS ON THE L ANGUAGE M ODELS
In this subsection, we conduct quantization experiments on language models.
The two most wellknown recurrent neural networks, i.e., LSTM (Hochreiter & Schmidhuber, 1997) and GRU (Cho et al., 2014), are evaluated.
As they are to predict the next word, the performance is measured by perplexity per word (PPW) metric.
For all experiments, we initialize with the pre-trained model and using vanilla SGD.
The initial learning rate is set to 20 .
Every epoch we evaluate on the validation dataset and record the best value.
When the validation error exceeds the best record, we decrease learning rate by a factor of 1 . 2 .
Training is terminated once the learning rate less than 0 .
001 or reaching the maximum epochs, i.e., 80 .
The gradient norm is clipped in the range [ − 0 .
We unroll the network for 30 time steps and regularize it with the standard dropout (probability of dropping out units equals to 0 . 5 ) (Zaremba et al., 2014).
For simplicity of notation, we denote the methods using uniform, balanced, greedy, reﬁned greedy, and our alternating quantization as Uniform,
Balanced, Greedy, Reﬁned, and Alternating, respectively.
Peen Tree Bank.
We ﬁrst conduct experiments on the Peen Tree Bank (PTB) corpus (Marcus et al., 1993), using the standard preprocessed splits with a 10 K size vocabulary (Mikolov, 2012).
The
PTB dataset contains 929 K training tokens, 73 K validation tokens, and 82 K test tokens.
For fair comparison with existing works, we also use LSTM and GRU with 1 hidden layer of size 300 .
To have a glance at the approximation ability of different quantization methods as detailed in Section 2, we ﬁrstly conduct experiments by directly quantizing the trained full precision weight (neither quantization on activation nor retraining).
Results on LSTM and GRU are shown in Table 1 and Table 2, respectively.
The left part records the relative mean squared error of quantized weight matrices with full precision one.
We can see that our proposed Alternating can get much lower error across all varying bit.
We also measure the testing PPW for the quantized weight as shown in the right part of Table 1 and 2.
The results are in consistent with the left part, where less errors result in lower testing PPW.
Note that Uniform and Balanced quantization are rule-based and not aim at minimizing the error.
Thus they can have much worse result by direct approximation.
We also repeat the experiment on other datasets.
For both LSTM and GRU, the results are very similar to here.
[Table]
[Table]
[Table] [Table]
We then conduct experiments by quantizing both weights and activations.
We train with the batch size 20 .
The ﬁnal result is shown in Table 3.
Besides comparing with the existing works, we also conduct experiment for Reﬁned as a competitive baseline.
We do not include Greedy as it is already shown to be inferior to the reﬁned one, see Table 1 and Table 2.
As Table 3 shows, our full precision model can attain lower PPW than the existing works.
However, when considering the gap between quantized model with the full precision one, our alternating quantized neural network is still far better than existing works, i.e., Uniform (Hubara et al., 2016b) and Balanced (Zhou et al., 2017).
Compared with Reﬁned, our Alternating quantization can achieve compatible performance using 1 -bit less quantization on weights or activations.
In other words, under the same tolerance of accuracy drop, Alternating executes faster and uses less memory than Reﬁned.
We can see that our 3 /weights/activations quantized LSTM can achieve even better performance than full precision one.
A possible explanation is due to the regularization introduced by quantization (Hubara et al., 2016b).
Wikidata-2 (Merity et al., 2017) is a dataset released recently as an alternative to PTB.
It contains 2088 K training, 217 K validation, and 245 K test tokens, and has a vocabulary of 33 K words, which is [Table] [Table] roughly 2 times larger in dataset size
[Table]
and 3 times larger in vocabulary than PTB.
We train with one layer’s hidden state of size 512 and set the batch size to 100 .
The result is shown in Table 4.
Similar to PTB, our Alternating can use 1 -bit less quantization to attain compatible or even lower PPW than
Reﬁned.
Text8.
In order to determine whether Alternating remains effective with a larger dataset, we perform experiments on the Text8 corpus (Mikolov et al., 2014).
Here we follow the same setting as (Xie et al., 2017).
The ﬁrst 90 M characters are used for training, the next 5 M for validation, and the ﬁnal 5M for testing, resulting in 15 .
3 M training tokens, 848 K validation tokens, and 855 K test tokens.
We also preprocess the data by mapping all words which appear 10 or fewer times to the unknown token, resulting in a 42 K size vocabulary.
We train LSTM and GRU with one hidden layer of sizeand set the batch size to 100 .
The result is shown in Table 5.
For LSTM on the left part, Alternating achieves excellent performance.
By only 2 -bit quantization on weights and activations, it exceeds
Reﬁned with 3 -bit.
The 2 -bit result is even better than that reported in (Xie et al., 2017), where LSTM adding noisy schemes for regularization can only attain 110 . 6 testing PPW.
For GRU on the right part, although Alternating is much better than Reﬁned, the 3 -bit quantization still has gap with full precision one.
We attribute that to the uniﬁed setting of hyper-parameters across all experiments.
With speciﬁcally tuned hyper-parameters on this dataset, one may make up for that gap.
C ONCLUSIONS
In this work, we address the limitations of RNNs, i.e., large memory and high latency, by quantization.
We formulate the quantization by minimizing the approximation error.
Under the key observation that some parameters can be singled out when others ﬁxed, a simple yet effective alternating method is proposed.
We apply it to quantize LSTM and GRU.
By 2 -bit weights and activations, we achieve only a reasonably accuracy loss compared with full precision one, with ∼ 16 × reduction in memory and potential ∼ 14 × acceleration on CPUs.
By 3 -bit quantization, we can attain compatible or even better result than the full precision one, with ∼ 10 .
5 × reduction in memory and potential ∼ 6 .
5 × acceleration.
Both beat existing works with a large margin.
The method employed here is very general.
It is not difﬁcult to incorporate the low-rank approximation and sparsity for further compression and acceleration or extend to quantize CNNs.