Title: LEARNING RECURRENT BINARY/TERNARY WEIGHTS

Abstract: Recurrent neural networks (RNNs) have shown excellent performance in processing sequence data. However, they are both complex and memory intensive due to their recursive nature. These limitations make RNNs difficult to embed on mobile devices requiring real-time processes with limited hardware resources. To address the above issues, we introduce a method that can learn binary and ternary weights during the training phase to facilitate hardware implementations of RNNs. As a result, using this approach replaces all multiply-accumulate operations by simple accumulations, bringing significant benefits to custom hardware in terms of silicon area and power consumption. On the software side, we evaluate the performance (in terms of accuracy) of our method using long short-term memories (LSTMs) and gated recurrent units (GRUs) on various sequential models including sequence classification and language modeling. We demonstrate that our method achieves competitive results on the aforementioned tasks while using binary/ternary weights during the runtime. On the hardware side, we present custom hardware for accelerating the recurrent computations of LSTMs with binary/ternary weights. Ultimately, we show that LSTMs with binary/ternary weights can achieve up to 12× memory saving and 10× inference speedup compared to the full-precision hardware implementation design.

1. INTRODUCTION:

Convolutional neural networks (CNNs) have surpassed human-level accuracy in various complex tasks by obtaining a hierarchical representation with increasing levels of abstraction (Bengio (2009) ; Lecun et al. (2015) ). As a result, they have been adopted in many applications for learning hierarchical representation of spatial data. CNNs are constructed by stacking multiple convolutional layers often followed by fully-connected layers (Lecun et al. (1998) ). While the vast majority of network parameters (i.e. weights) are usually found in fully-connected layers, the computational complexity of CNNs is dominated by the multiply-accumulate operations required by convolutional layers (Yang et al. (2015) ). Recurrent neural networks (RNNs), on the other hand, have shown remarkable success in modeling temporal data (Mikolov et al. (2010) ; Graves (2013) ; Cho et al. (2014a) ; Sutskever et al. (2014) ; Vinyals et al. (2014) ). Similar to CNNs, RNNs are typically over-parameterized since they build on high-dimensional input/output/state vectors and suffer from high computational complexity due to their recursive nature (Xu et al. (2018) ; Han et al. (2015) ). As a result, the aforementioned limitations make the deployment of CNNs and RNNs difficult on mobile devices that require real-time inference processes with limited hardware resources.

Several techniques have been introduced in literature to address the above issues. In (Sainath et al. (2013) ; Jaderberg et al. (2014) ; Lebedev et al. (2014) ; Tai et al. (2015) ), it was shown that the weight matrix can be approximated using a lower rank matrix. In (Liu et al. (2015) ; Han et al. (2015) ; Wen et al. (2016) ; Ardakani et al. (2016) ), it was shown that a significant number of parameters in DNNs are noncontributory and can be pruned without any performance degradation in the final accuracy performance. Finally, quantization approaches were introduced in (Courbariaux et al. (2015) ; Lin et al. (2015) ; Courbariaux & Bengio (2016) ; Kim & Smaragdis (2016) ; Hubara et al. (2016b) ; Rastegari et al. (2016) ; Hubara et al. (2016a) ; Zhou et al. (2016) ; Li & Liu (2016) ; Zhu et al. (2016) ) to reduce the required bitwidth of weights/activations. In this way, power-hungry multiply-accumulate operations are replaced by simple accumulations while also reducing the number of memory accesses to the off-chip memory.

Considering the improvement factor of each of the above approaches in terms of energy and power reductions, quantization has proven to be the most beneficial for hardware implementations. However, all of the aforementioned quantization approaches focused on optimizing CNNs or fully-connected networks only. As a result, despite the remarkable success of RNNs in processing sequential data, RNNs have received the least attention for hardware implementations, when compared to CNNs and fully-connected networks. In fact, the recursive nature of RNNs makes their quantization difficult. In (Hou et al. (2016)) , for example, it was shown that the well-known BinaryConnect technique fails to binarize the parameters of RNNs due to the exploding gradient problem (Courbariaux et al. (2015) ). As a result, a binarized RNN was introduced in (Hou et al. (2016) ), with promising results on simple tasks and datasets. However it does not generalize well on tasks requiring large inputs/outputs (Xu et al. (2018) ). In (Xu et al. (2018) ; Hubara et al. (2016b) ), multi-bit quantized RNNs were introduced. These works managed to match their accuracy performance with their full-precision counterparts while using up to 4 bits for data representations.

In this paper, we propose a method that learns recurrent binary and ternary weights in RNNs during the training phase and eliminates the need for full-precision multiplications during the inference time. In this way, all weights are constrained to {+1, -1} or {+1, 0, -1} in binary or ternary representations, respectively. Using the proposed approach, RNNs with binary and ternary weights can achieve the performance accuracy of their full-precision counterparts. In summary, this paper makes the following contributions:

• We introduce a method for learning recurrent binary and ternary weights during both forward and backward propagation phases, reducing both the computation time and memory footprint required to store the extracted weights during the inference. • We perform a set of experiments on various sequential tasks, such as sequence classification, language modeling, and reading comprehension. We then demonstrate that our binary/ternary models can achieve near state-of-the-art results with greatly reduced computational complexity.1 • We present custom hardware to accelerate the recurrent computations of RNNs with binary or ternary weights. The proposed dedicated accelerator can save up to 12× of memory elements/bandwidth and speed up the recurrent computations by up to 10× when performing the inference computations.

2. RELATED WORK:

During the binarization process, each element of the full-precision weight matrix W ∈ R d I ×d J with entries w i,j is binarized by w i,j = α i,j w B i,j , where α i,j ≥ 0, i ∈ {1, . . . , d I }, j ∈ {1, . . . , d J } and w B i,j ∈ {-1, +1}. In BinaryConnect (Courbariaux et al. (2015) ), the binarized weight element w B i,j is obtained by the sign function while using a fixed scalling factor α for all the elements: w B i,j = α × sign(w i,j ). In TernaryConnect (Lin et al. (2015) ), values hesitating to be either +1 or -1 are clamped to zero to reduce the accuracy loss of binarization: w i,j = α i,j w T i,j where w T i,j ∈ {-1, 0, +1}. To further improve the precision accuracy, TernaryConnect stochastically assigns ternary values to the weight elements by performing w i,j = α×Bernoulli(|w i,j |)×sign(w i,j ) while using a fixed scaling factor α for each layer. Ternary weight networks (TWNs) were then proposed to learn the factor α by minimizing the L2 distance between the full-precision and ternary weights for each layer. Zhou et al. (2016) introduced DoReFa-Net as a method that can learn different bitwidths for weights, activations and gradients. Since the quantization functions used in the above works are not differentiable, the derivative of the loss l w.r.t the full-precision W is approximated by

EQUATION

where W B and W T denote binarized and ternarized weights, respectively. Zhu et al. (2016) introduced the trained ternary quantization (TTQ) method that uses two assymetric scaling parameters (α 1 for positive values and α 2 for negative values) to ternarize the weights. In loss-aware binarization (LAB) (Hou et al. (2016) ), the loss of binarization was explicitly considered. More precisely, the loss w.r.t the binarized weights is minimized using the proximal Newton algorithm. Hou & Kwok (2018) extended LAB to support different bitwidths for the weights. This method is called loss-aware quantization (LAQ). Recently, Guo et al. (2017) introduced a new method that builds the full-precision weight matrix W as k multiple binary weight matrices: w i,j ≈ k z=1 α k i,j β k i,j where β k i,j ∈ {-1, +1} and α k i,j > 0. Xu et al. (2018) also uses a binary search tree to efficiently derive the binary codes β k i,j , improving the prediction accuracy. While using multiple binary weight matrices reduces the bitwidth by a factor of 32× compared to its full-precision counterpart, it increases not only the number of parameters but also the number of operations by a factor of k (Xu et al. (2018) ).

Among the aforementioned methods, only works of Xu et al. (2018) and Hou & Kwok (2018) targeted RNNs to reduce their computational complexity and outperformed all the aforementioned methods in terms of the prediction accuracy. However, they have shown promising results only on specific temporal tasks: the former targeted only the character-level language modeling task on small datasets while the latter performs the word-level language modeling task and matches the performance of the full-precision model when using k = 4. Therefore, there are no binary models that can match the performance of the full-precision model on the word-level language modeling task. More generally, there are no binary/ternary models that can perform different temporal tasks while achieving similar prediction accuracy to its full-precision counterpart is missing in literature.

3. PRELIMINARIES:

Despite the remarkable success of RNNs in processing variable-length sequences, they suffer from the exploding gradient problem that occurs when learning long-term dependencies (Bengio et al. (1994) ; Pascanu et al. (2013) ). Therefore, various RNN architectures such as Long Short-Term Memory (LSTM) (Hochreiter & Schmidhuber (1997) ) and Gated Recurrent Unit (GRU) (Cho et al. (2014b) ) were introduced in literature to mitigate the exploding gradient problem. In this paper, we mainly focus on the LSTM architecture to learn recurrent binary/ternary weights due to their prevalent use in both academia and industry. The recurrent transition of LSTM is obtained by:

EQUATION

where shows that the main computational core of LSTM is dominated by the matrix multiplications. The recurrent weight matrices W f h , W ih , W oh , W gh , W f x , W ix , W ox and W gx also contain the majority of the model parameters. As such, we aim to compensate the computational complexity of the LSTM cell and reduce the number of memory accesses to the energy/power-hungry DRAM by binarizing or ternarizing the recurrent weights.

{W f h , W ih , W oh , W gh } ∈ R d h ×d h , {W f x , W ix , W ox , W gx } ∈ R dx×d h and {b f , b i , b o , b g } ∈ R d h denote

4 LEARNING RECURRENT BINARY/TERNARY WEIGHTS Hou et al. (2016) showed that the methods ignoring the loss of the binarization process fail to binarize the weights in LSTMs despite their remarkable performance on CNNs and fully-connected networks. In BinaryConnect as an example, the weights are binarized during the forward computations by thresholding while Eq. ( 1) is used to estimate the loss w.r.t. the full-precision weights without considering the quantization loss. When the training is over, both the full-precision and binarized weights can be then used to perform the inference computations of CNNs and fully-connected networks (Lin et al. (2015) ). However, using the aforementioned binarization approach in vanilla LSTMs fails to perform sequential tasks due to the gradient vanishing problem as discussed in (Hou et al. (2016) ). To further explore the cause of this problem, we performed a set of experiments. We first measured the probability density of the gates and hidden states of a binarized LSTM and observed that the binarized LSTM fail to control the flow of information (see Appendix A for more details). More specifically, the input gate i and the output gate o tend to let all information through, the gate g tends to block all information, and the forget gate f cannot decide to let which information through. In the second experiment, we measured the probability density of the input gate i before its non-linear function applied (i.e., i p = W ih h t-1 + W ix x t + b i ) at different iterations during the training process. In this experiment, we observed that the binarization process changes the probability density of the gates and hidden states during the training process, resulting in all positive values for i p and values centered around 1 for the input gate i (see Appendix A for more details). To address the above issue, we propose the use of batch normalization in order to learn binarized/ternarized recurrent weights.

It is well-known that a network trained using batch normalization is less susceptible to different settings of hyperparameters and changes in the distribution of the inputs to the model (Ioffe & Szegedy (2015) ). The batch normalization transform can be formulated as follows:

EQUATION

where x and denote the unnormalized vector and a regularization hyperparameter. The mean and standard deviation of the normalized vector are determined by model parameters φ and γ. The statistics E(x) and V(x) also denote the estimations of the mean and variance of the unnormalized vector for the current minibatch, respectively. Batch normalization is commonly applied to a layer where changing its parameters affects the distributions of the inputs to the next layer. This occurs frequently in RNN where its input at time t depends on its output at time t -1. Several works have investigated batch normalization in RNNs (Cooijmans et al. (2016) ; Laurent et al. (2016) ; Amodei et al. ( 2016)) to improve their convergence speed and performance.

The main goal of our method is to represent each element of the full-precision weight W either as w i,j = αw B i,j or w i,j = αw T i,j , where α is a fixed scaling factor for all the weights and initialized from Glorot & Bengio (2010) . To this end, we first divide each weight by the factor α to normalize the weights. We then compute the probability of getting binary or ternary values for each element of the full-precision matrix W by

EQUATION

for binarization and

EQUATION

for ternarization, where w N i,j denotes the normalized weight. Afterwards, we stochastically sample from the Bernoulli distribution to obtain binarized/ternarized weights as follows

w B i,j = Bernoulli(P (w i,j = 1)) × 2 -1, w T i,j = Bernoulli(P (w i,j = 1)) × sign(w i,j

). (6) Finally, we batch normalize the vector-matrix multiplications between the input and hidden state vectors with the binarized/ternarized weights W

B/T f h , W B/T ih , W B/T oh , W B/T gh , W B/T f x , W B/T ix , W B/T ox and W B/T

gx . More precisely, we perform the recurrent computations as In fact, batch normalization cancels out the effect of the binarization/ternarization on the distribution of the gates and states during the training process. Moreover, batch normalization regulates the scale of binarized/ternarized weights using its parameter φ in addition to α.

f t = σ BN(W B/T f h h t-1 ; φ f h , 0) + BN(W B/T f x x t ; φ f x , 0) + b f , i t = σ BN(W B/T ih h t-1 ; φ ih , 0) + BN(W B/T ix x t ; φ ix , 0) + b i , o t = σ BN(W B/T oh h t-1 ; φ oh , 0) + BN(W B/T ox x t ; φ ox , 0) + b o , g t = tanh BN(W B/T gh h t-1 ; φ gh , 0) + BN(W B/T gx x t ; φ gx , 0) + b g . (7)

So far, we have only considered the forward computations. During the parameter update, we use full-precision weights since the parameter updates are small values. To update the full-precision weights, we use Eq. ( 1) to estimate its gradient since the binarization/ternarization functions are indifferentiable (See Algorithm 1 and its details in Appendix B). It is worth noting that using batch normalization makes the training process slower due to the additional computations required to perform Eq. (3).

5. EXPERIMENTAL RESULTS AND DISCUSSIONS:

In this section, we evaluate the performance of the proposed LSTMs with binary/ternary weights on different temporal tasks to show the generality of our method. We defer hyperparameters and tasks details for each dataset to Appendix C due to the limited space.

5.1. CHARACTER-LEVEL LANGUAGE MODELING:

For the character-level modeling, the goal is to predict the next character and the performance is evaluated on bits per character (BPC) where lower BPC is desirable. We conduct quantization experiments on Penn Treebank (Marcus et al. (1993) ), War & Peace (Karpathy et al. (2015) ) and Linux Kernel (Karpathy et al. (2015) ) corpora. For Penn Treebank dataset, we use a similar LSTM model configuration and data preparation to Mikolov et al. (2012) . For War & Peace and Linux Kernel datasets, we also follow the LSTM model configurations and settings in (Karpathy et al. (2015) ). In order to evaluate the effectiveness of our method on a larger dataset for the character-level language modeling task, we use the Text8 dataset which was derived from Wikipedia. For this task, we use one LSTM layer of size 2000 and train it on sequences of length 180. We follow the data preparation approach and settings of Mikolov et al. (2012) . The test results are reported in Table 2 . While our models use recurrent binary or ternary weights during runtime, they achieve acceptable performance when compared to the full-precision models.

5.2. WORD-LEVEL LANGUAGE MODELING:

Similar to the character-level language modeling, the main goal of word-level modeling is to predict the next word. However, this task deals with large vocabulary sizes, making the model quantization difficult. Xu et al. (2018) introduced a multi-bit quantization method, referred to as alternating method, as a first attempt to reduce the complexity of the LSTMs used for this task. However, the alternating method only managed to almost match its performance with its full-precision counterpart using 4 bits (i.e., k = 4). However, there is a huge gap in the performance between its quantized model with 2 bits and the full-precision one. To show the effectiveness of our method over the alternating method, we use a small LSTM of size 300 similar to (Xu et al. (2018) ) for a fair comparison. We also examine the prediction accuracy of our method over the medium and large models introduced by Zaremba et al. ( 2014): the medium model contains an LSTM layer of size 650 and the large model contains two LSTM layers of size 1500. We also use the same settings described in (Mikolov et al. (2010) ) to prepare and train our model. Table 3 summarizes the performance of our models in terms of perplexity. The experimental results show that our binarized/ternarized models outperform the alternating method using 2-bit quantization in terms of both perplexity and the memory size. Moreover, our medium-size model with binary weights also has a substantial improvement over the alternating method using 4-bit quantization. Finally, our models with recurrent binary and ternary weights yield a comparable performance compared to their full-precision counterparts.

5.3. SEQUENTIAL MNIST:

We perform the MNIST classification task (Le et al. ( 2015)) by processing each image pixel at each time step. In this task, we process the pixels in scanline order. We train our models using an LSTM with 100 nodes, followed by a softmax classifier layer. Table 4 reports the test performance of our models with recurrent binary/ternary weights. While our binary model uses a lower bit precision and fewer operations for the recurrent computations compared to the alternating models, its loss of accuracy is small. On the other hand, our ternary model requires the same memory size and achieves the same accuracy as the alternating method while requiring 2× fewer operations.

5.4. QUESTION ANSWERING:

Hermann et al. ( 2015) recently introduced a challenging task that involves reading and comprehension of real news articles. More specifically, the main goal of this task is to answer questions about the context of the given article. To this end, they also introduced an architecture, called Attentive Reader, that exploits an attention mechanism to spot relevant information in the document. Attentive Reader uses two bidirectional LSTMs to encode the document and queries. To show the generality and effectiveness of our quantization method, we train Attentive Reader with our method to learn recurrent binary/ternary weights. We perform this task on the CNN corpus (Hermann et al. (2015) ) by replicating Attentive Reader and using the setting described in (Hermann et al. (2015) ). Table 5 shows the test accuracy of binarized/ternarized Attentive Reader. The simulation results show that our Attentive Reader with binary/ternary weights yields similar accuracy rate to its full-precision counterpart while requiring 32× smaller memory footprint.

5.5. DISCUSSIONS:

As discussed in Section 4, the training models ignoring the quantization loss fail to quantize the weights in LSTM while they perform well on CNNs and fully-connected networks. To address this problem, we proposed the use of batch normalization during the quantization process. To justify the importance of such a decision, we have performed different experiments over a wide range of temporal tasks and compared the accuracy performance of our binarization/ternarization method with binaryconnect as a method that ignores the quantization loss. The experimental results showed that binaryconnect method fails to learn binary/ternary weights. On the other hand, our method not only learns recurrent binary/ternary weights but also outperforms all the existing quantization methods in literature. It is also worth mentioning that the models trained with our method achieve a comparable accuracy performance w.r.t. their full-precision counterpart.

Figure 1 (a) shows a histogram of the binary/ternary weights of the LSTM layer used for characterlevel language modeling task on the Penn Treebank corpus. In fact, our model learns to use binary or ternary weights by steering the weights into the deterministic values of -1, 0 or 1. Despite the CNNs or fully-connected networks trained with binary/ternary weights that can use either real-valued or binary/ternary weights, the proposed LSTMs trained with binary/ternary can only perform the inference computations with binary/ternary weights. Moreover, the distribution of the weights is dominated by non-zero values for the model with ternary weights.

To show the effect of the probabilistic quantization on the prediction accuracy of temporal tasks, we adopted the ternarized network trained for the character-level language modeling tasks on the Penn Treebank corpus (see Section 5.1). We measured the prediction accuracy of this network on the test set over 10000 samples and reported the distribution of the prediction accuracy in Figure 1(b) . Figure 1(b) shows that the variance imposed by the stochastic ternarization on the prediction accuracy is very small and can be ignored. It is worth mentioning that we also have observed a similar behavior for other temporal tasks used in this paper.

Figure 2 illustrates the learning curves and generalization of our method to longer sequences on the validation set of the Penn Treebank corpus. In fact, the proposed training algorithm also tries to retains the main features of using batch normalization, i.e., fast convergence and good generalization over long sequences. Figure 2(a) shows that our model converges faster than the full-precision LSTM for the first few epochs. After a certain point, the convergence rate of our method decreases, that prevents the model from early overfitting. Figure 2(b ) also shows that our training method generalizes well over longer sequences than those seen during training. Similar to the full-precision baseline, our binary/ternary models learn to focus only on information relevant to the generation of the next target character. In fact, the prediction accuracy of our models improves as the sequence length increases since longer sequences provides more information from past for generation of the next target character.

While we have only applied our binarization/ternarization method on LSTMs, our method can be used to binarize/ternarize other recurrent architectures such as GRUs. To show the versatility of our method, we repeat the character-level language modeling task performed in Section 5.1 using GRUs on the Penn Treebank, War & Peace and Linux Kernel corpora. We also adopted the same network configurations and settings used in Section 5.1 for each of the aforementioned corpora. Table 6 summarizes the performance of our binarized/ternarized models. The simulation results show that our method can successfully binarize/ternarize the recurrent weights of GRUs.

As a final note, we have investigated the effect of using different batch sizes on the prediction accuracy of our binarized/ternarized models. To this end, we trained an LSTM of size 1000 over a sequence length of 100 and different batch sizes to perform the character-level language modeling task on the Penn Treebank corpus. Batch normalization cannot be used for the batch size of 1 as the output vector will be all zeros. Moreover, using a small batch size leads to a high variance when estimating the statistics of the unnormalized vector, and consequently a lower prediction accuracy than the baseline model without bath normalization, as shown in Figure 3 . On the other hand, the prediction accuracy of our binarization/ternarization models improves as the batch size increases, while the prediction accuracy of the baseline model decreases. 

6. HARDWARE IMPLEMENTATION:

The introduced binarized/ternarized recurrent models can be exploited by various dataflows such as DaDianNao (Chen et al. (2014) ) and TPU (Jouppi et al. (2017) ). In order to evaluate the effectiveness of LSTMs with recurrent binary/ternary weights, we build our binary/ternary architecture over DaDianNao as a baseline which has proven to be the most efficient dataflow for DNNs with sigmoid/tanh functions. In fact, DaDianNao achieves a speedup of 656× and reduces the energy by 184× over a GPU (Chen et al. (2014) ). Moreover, some hardware techniques can be adopted on top of DaDianNao to further speed up the computations. For instance, Zhang et al. (2016) showed that ineffectual computations of zero-valued weights can be skipped to improve the run-time performance of DaDianNao. In DaDianNao, a DRAM is used to store all the weights/activations and provide the required memory bandwidth for each multiply-accumulate (MAC) unit. For evaluation purposes, we consider two different application-specific integrated circuit (ASIC) architectures implementing Eq. ( 2): low-power implementation and high-speed inference engine. We build these two architectures based on the aforementioned dataflow. For the low-power implementation, we use 100 MAC units. We also use a 12-bit fixed-point representation for both weights and activations of the full-precision model as a baseline architecture. As a result, 12-bit multipliers are required to perform the recurrent computations. Note that using the 12-bit fixed-point representation for weights and activations guarantees no prediction accuracy loss in the full-precision models. For the LSTMs with recurrent binary/ternary weights, a 12-bit fixed-point representation is only used for activations and multipliers in the MAC units are replaced with low-cost multiplexers. Similarly, using 12-bit fixed-point representation for activations guarantees no prediction accuracy loss in the introduced binary/ternary models. We implemented our low-power inference engine for both the full-precision and binary/ternary-precision models in TSMC 65-nm CMOS technology. The synthesis results excluding the implementation cost of the DRAM are summarized in Table 7 . They show that using recurrent binary/ternary weights results in up to 9× lower power and 10.6× lower silicon area compared to the baseline when performing the inference computations at 400 MHz.

For the high-speed design, we consider the same silicon area and power consumption for both the fullprecision and binary/ternary-precision models. Since the MAC units of the binary/ternary-precision model require less silicon area and power consumption as a result of using multiplexers instead of multipliers, we can instantiate up to 10× more MAC units, resulting in up to 10× speedup compared to the full-precision model (see Table 7 ). It is also worth noting that the models using recurrent binary/ternary weights also require up to 12× less memory bandwidth than the full-precision models. More details on the proposed architecture are provided in Appendix D.

7. CONCLUSION:

In this paper, we introduced a method that learns recurrent binary/ternary weights and eliminates most of the full-precision multiplications of the recurrent computations during the inference. We showed that the proposed training method generalizes well over long sequences and across a wide range of temporal tasks such as word/character language modeling and pixel by pixel classification tasks. We also showed that learning recurrent binary/ternary weights brings a major benefit to custom hardware implementations by replacing full-precision multipliers with hardware-friendly multiplexers and reducing the memory bandwidth. For this purpose, we introduced two ASIC implementations: low-power and high-throughput implementations. The former architecture can save up to 9× power consumption and the latter speeds up the recurrent computations by a factor of 10. 

APPENDIX B:

Learning recurrent binary/ternary weights are performed in two steps: forward propagation and backward propagation.

Forward Propagation: A key point to learn recurrent binary/ternary weights is to batch-normalize the result of each vector-matrix multiplication with binary/ternary recurrent weights during the forward propagation. More precisely, we first binarize/ternarize the recurrent weights. Afterwards, the unit activations are computed while using the recurrent binarized/ternarized weights for each time step and recurrent layer. The unit activations are then normalized during the forward propagation.

Backward Propagation: During the backward propagation, the gradient with respects to each parameter of each layer is computed. Then, the updates for the parameters are obtained using a learning rule. During the parameter update, we use full-precision weights since the parameter updates are small values. More specifically, the recurrent weights are only binarized/ternarized during the forward propagation. Algorithm 1 summarizes the training method that learns recurrent binary/ternary weights. It is worth noting that batch normalizing the state unit c can optionally be used to better control over its relative contribution in the model. 

:

The codes for these tasks are available online at https://github.com/arashardakani/ Learning-Recurrent-Binary-Ternary-Weights

Figures and Tables:

Figure 1: (a) Distribution of W h used in the LSTM layer on the Penn Treebank corpus. (b) Distribution of the prediction accuracy on the Penn Treebank corpus when using stochastic ternarization over 10000 samples.

Figure 2: (a) The learning curve on the validation set of the Penn Treebank corpus. (b) Performance on the test set of the Penn Treebank corpus over longer sequences.

Figure 3: Effect of different batch sizes on the prediction accuracy of the character-level language modeling task on the Penn Treebank corpus.

Figure 5: Probability density of i p for (a) the BinaryConnect LSTM compared to (b) its full-precision counterpart for a single time step at different training iterations on the Penn Treebank character-level modeling task. The vertical axis denotes the training iterations.

Figure 5 illustrates the probability density of i p at different iterations during the training process. The simulation results show a shift in probability density of i p during the training process, resulting in all positive values for i p and values centered around 1 for the input gate i. In fact, the binarization process changes the probability density of the gates and hidden states during the training phase.

Training with recurrent binary/ternary weights. l is the cross entropy loss function. B/T() specifies the binarization/ternarization function. The batch normalization transform is also denoted byBN(•; φ, γ). L and T are also the number of LSTM layers and time steps, respectively. Data:Full-precision LSTM parameters W f h , W ih , W oh , W gh , W f x , W ix , W ox , W gx , b f , b i ,b o and b g for each layer. Batch normalization parameters for hidden-to-hidden and input-to-hidden states. The classifier parameters W s and b s . Input data x 1 , its corresponding targets y for each minibatch. /T ← B/T(W l f x ), W l ix B/T ← B/T(

the recurrent weights and bias. The parameters h ∈ R d h and c ∈ R d h are hidden states. The logistic sigmoid function and Hadamard product are denoted as σ and , respectively. The updates of the LSTM parameters are regulated through a set of gates: f t , i t , o t and g t . Eq. (2)

Table content: <table/>

Testing character-level BPC values of quantized LSTM models and size of their weight matrices in terms of KByte.

Table content: <table><tr><td>Linux Kernel War &amp; Peace Penn Treebank</td></tr></table>

Table1summarizes the performance of our binarized/ternarized models compared to state-of-the-art quantization methods reported in literature. All the models reported in Table1use an LSTM layer with 1000, 512 and 512 units on a sequence length of 100 for the experiments on Penn Treebank(Marcus et al. (1993)), War & Peace(Karpathy et al. (2015)) and Linux Kernel(Karpathy et al. (2015)) corpora, respectively. The experimental results show that our model with binary/ternary weights outperforms all the existing quantized models in terms of prediction accuracy. Moreover,

Table content: <table/>

Test character-level performance of our quantized models on the Text8 corpus.

Table content: <table><tr><td>Text8</td></tr></table>

Test performance of the proposed LSTM with recurrent binary/ternary weights on the Penn Treebank (PTB) corpus.

Table content: <table><tr><td>Word-PTB</td></tr></table>

Test accuracy of the proposed LSTM with recurrent binary/ternary weights on the pixel by pixel MNIST classification task.

Table content: <table><tr><td>MNIST</td></tr></table>

Test accuracy of Attentive Reader with recurrent binary/ternary weights on CNN questionanswering task.

Table content: <table><tr><td>CNN</td></tr></table>

Testing character-level BPC values of quantized GRU models and size of their weight matrices in terms of KByte.

Table content: <table><tr><td/><td/><td colspan="6">Linux Kernel War &amp; Peace Penn Treebank</td></tr><tr><td>Model</td><td>Precision</td><td>Test</td><td>Size</td><td>Test</td><td>Size</td><td>Test</td><td>Size</td></tr><tr><td>GRU (baseline)</td><td colspan="7">Full-precision 1.82 3772 1.75 3662 1.40 12612</td></tr><tr><td>GRU with binary weights (ours)</td><td>Binary</td><td>1.90</td><td>124</td><td>1.92</td><td>120</td><td>1.46</td><td>406</td></tr><tr><td>GRU with ternary weights (ours)</td><td>Ternary</td><td>1.81</td><td>241</td><td>1.82</td><td>235</td><td>1.41</td><td>799</td></tr></table>

Implementation results of the proposed binary/ternary models vs full-precision models.

Table content: <table><tr><td/><td/><td>Low-Power</td><td/><td/><td>High-Speed</td><td/></tr><tr><td>Model</td><td colspan="6">Full-Precision Binary Ternary Full-Precision Binary Ternary</td></tr><tr><td># MAC Units</td><td>100</td><td>100</td><td>100</td><td>100</td><td>1000</td><td>500</td></tr><tr><td>Throughput (GOps/sec)</td><td>80</td><td>80</td><td>80</td><td>80</td><td>800</td><td>400</td></tr><tr><td>Silicon Area (mm 2 )</td><td>2.56</td><td>0.24</td><td>0.42</td><td>2.56</td><td>2.54</td><td>2.16</td></tr><tr><td>Power (mW)</td><td>336</td><td>37</td><td>61</td><td>336</td><td>347</td><td>302</td></tr></table>

softmax(W s h L + b s ) 18 2. Backward Computations 19 Compute the loss function l knowing ŷ and y 20 Obtain the updates ∆W s and ∆b s by computing

Table content: <table><tr><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td colspan="3">W l ix )</td><td/><td/></tr><tr><td>6</td><td colspan="18">W l ox B/T ← B/T(W l ox ), W l gx B/T ← B/T(W l gx )</td><td/><td/></tr><tr><td>7</td><td colspan="5">for t = 1 to T do</td><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/></tr><tr><td>8</td><td colspan="21">f l t = σ BN(W l f h B/T h l t-1 ; φ l f h , 0) + BN(W l f x B/T x l t ; φ l f x , 0) + b l f</td></tr><tr><td>9</td><td colspan="21">i l t = σ BN(W l ih B/T h l t-1 ; φ l ih , 0) + BN(W l ix B/T x l t ; φ l ix , 0) + b l i</td></tr><tr><td>10</td><td colspan="21">o l t = σ BN(W l oh B/T h l t-1 ; φ l oh , 0) + BN(W l ox B/T x l t ; φ l ox , 0) + b l o</td></tr><tr><td>11</td><td colspan="21">g l t = tanh BN(W l gh B/T h l t-1 ; φ l gh , 0) + BN(W l gx B/T x l t ; φ l gx , 0) + b l g</td></tr><tr><td>12</td><td colspan="3">c l t = f l t</td><td colspan="4">c l t-1 + i l t</td><td/><td colspan="2">g l t</td><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/></tr><tr><td>13</td><td colspan="3">h l t = o l t</td><td colspan="10">tanh BN(c l t ; φ l c , γ l c )</td><td/><td/><td/><td/><td/><td/><td/></tr><tr><td>14</td><td>end</td><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/></tr><tr><td>15</td><td colspan="3">x l+1 = h l</td><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/></tr><tr><td colspan="2">16 end</td><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/></tr><tr><td colspan="21">17 ŷ = ∂l ∂W s</td><td>and</td><td>∂l ∂b s</td><td>, respectively</td></tr><tr><td colspan="5">21 for l = 1 to L do</td><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/></tr><tr><td>22</td><td colspan="21">Obtain the updates ∆W l f h , ∆W l ih , ∆W l oh , ∆W l gh , ∆W l f x , ∆W l ix , ∆W l ox and ∆W l gx by</td></tr><tr><td/><td colspan="3">computing</td><td colspan="4">∂l ∂W l f h B/T</td><td>,</td><td colspan="4">∂l ∂W l ih B/T</td><td>,</td><td/><td colspan="3">∂l ∂W l oh B/T</td><td>,</td><td colspan="3">∂l ∂W l gh B/T</td><td>,</td><td>∂l ∂W l f x B/T</td><td>,</td><td>∂l ix B/T ∂W l</td><td>,</td></tr><tr><td/><td colspan="3">∂l ∂W l ox B/T</td><td>and</td><td colspan="5">∂l gx B/T ∂W l</td><td colspan="6">, respectively</td><td/><td/><td/><td/><td/></tr><tr><td>23</td><td colspan="21">Obtain the updates ∆b l f , ∆b l i , ∆b l o , ∆b l g , ∆h l 0 and ∆c l 0 by computing</td><td>∂l ∂b l f</td><td>,</td><td>∂l ∂b l i</td><td>,</td><td>∂l o ∂b l</td><td>,</td></tr><tr><td/><td>∂l ∂b l g</td><td>,</td><td>∂l ∂h l 0</td><td>and</td><td>∂l 0 ∂c l</td><td colspan="7">, respectively</td><td/><td/><td/><td/><td/><td/><td/><td/></tr><tr><td>24</td><td colspan="21">Obtain the updates ∆φ l f h , ∆φ l ih , ∆φ l oh , ∆φ l gh , ∆φ l f x , ∆φ l ix , ∆φ l ox , ∆φ l gx , ∆φ l c and ∆γ l c</td></tr><tr><td/><td colspan="4">by computing</td><td colspan="2">∂l ∂φ l f h</td><td>,</td><td colspan="3">∂l ∂φ l ih</td><td>,</td><td colspan="2">∂l ∂φ l oh</td><td>,</td><td>∂l ∂φ l gh</td><td>,</td><td colspan="3">∂l ∂φ l f x</td><td>,</td><td>∂l ∂φ l ix</td><td>,</td><td>∂l ∂φ l gx</td><td>,</td><td>∂l ∂φ l ox</td><td>,</td><td>∂l ∂φ l c</td><td>and</td><td>∂l c ∂γ l</td><td>,</td></tr><tr><td/><td colspan="4">respectively</td><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/></tr><tr><td colspan="2">25 end</td><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/><td/></tr><tr><td colspan="19">26 Update the network parameters using their updates</td><td/><td/></tr></table>

Peer review (iclr2019):
 This work proposes a method for reducing memory requirements in RNN models via binary / ternary quantisation. The authors argue that binarising RNNs is due to a covariate shift, and address it with stochastic quantised weights and batch normalisation. The proposed RNN is tested on 6 sequence modelling tasks/datasets and shows drastic memory improvements compared to full-precision RNNs, with almost no loss in test performance. Based on the more efficient RNN cell, the authors furthermore describe a more efficient hardware implementation, compared to an implementation of the full-precision RNN. The core message I took away from this work is: “One can get away with stochastic binarised weights in a forward pass by compensating for it with batch normalisation”. Strengths: - substantial number of experiments (6 datasets), different domains - surprisingly simple methodological fix - substantial literature review - it has been argued that char-level / pixel-level RNNs present somewhat artificial tasks — even better that the authors test for a more realistic RNN application (Reading Comprehension) with an actually previously published model. Weaknesses: - little understanding is provided into _why_ covariance shift occurs/ why batch normalisation is so useful. The method works, but the authors could elaborate more on this, given that this is the core argument motivating the chosen method. - some statements are too bold/vague , e.g. page 3: “a binary/ternary model that can perform all temporal tasks” - unclear: by adapting a probabilistic formulation / sampling quantised weights, some variance is introduced. Does it matter for predictions (which should now also be stochastic)? How large is this variance? Even if negligible, it is not obvious and should be addressed. Other Questions / Comments - How dependent is the method on the batch size chosen? This is in particular relevant as smaller batches might yield poor empirical estimates for mean/var. What happens at batch size 1? Are predictions of poorer for smaller batches? - Section 2, second line — detail: case w_{i,j}=0 is not covered - equation (5): total probability mass does not add up to 1 - a direct comparison with models from previous work would have been interesting, where these previous methods also rely on batch normalisation - as I understand, the main contribution is in the inference (forward pass), not in training. It is somewhat misleading when the authors speak about “the proposed training algorithm” or “we introduced a training algorithm” - unclear: last sentence before section 6.